Skip to content

Commit

Permalink
Merge pull request #54 from google/add_methods
Browse files Browse the repository at this point in the history
add several debugger and runtime methods
  • Loading branch information
devoncarew authored May 2, 2020
2 parents f564bce + 97fc143 commit b0797c9
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 58 deletions.
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ linter:
- avoid_init_to_null
- directives_ordering
- slash_for_doc_comments
- prefer_const_constructors
- prefer_const_constructors_in_immutables
- prefer_const_declarations
- prefer_const_literals_to_create_immutables
- prefer_final_fields
7 changes: 7 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# webkit_inspection_protocol.dart

## 0.5.1
- add `Runtime.evaluate`
- add `Debugger.setBreakpoint`
- add `Debugger.removeBreakpoint`
- add `Debugger.evaluateOnCallFrame`
- add `Debugger.getPossibleBreakpoints`

## 0.5.0+1
- fixed a bug in reading type of `WipScope`

Expand Down
15 changes: 12 additions & 3 deletions lib/src/console.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@ import '../webkit_inspection_protocol.dart';
class WipConsole extends WipDomain {
WipConsole(WipConnection connection) : super(connection);

Future enable() => sendCommand('Console.enable');
Future disable() => sendCommand('Console.disable');
Future clearMessages() => sendCommand('Console.clearMessages');
Future<WipResponse> enable() => sendCommand('Console.enable');

Future<WipResponse> disable() => sendCommand('Console.disable');

Future<WipResponse> clearMessages() => sendCommand('Console.clearMessages');

Stream<ConsoleMessageEvent> get onMessage => eventStream(
'Console.messageAdded',
(WipEvent event) => new ConsoleMessageEvent(event));

Stream<ConsoleClearedEvent> get onCleared => eventStream(
'Console.messagesCleared',
(WipEvent event) => new ConsoleClearedEvent(event));
Expand All @@ -27,7 +30,9 @@ class ConsoleMessageEvent extends WrappedWipEvent {
Map get _message => params['message'] as Map;

String get text => _message['text'] as String;

String get level => _message['level'] as String;

String get url => _message['url'] as String;

Iterable<WipConsoleCallFrame> getStackTrace() {
Expand All @@ -52,8 +57,12 @@ class WipConsoleCallFrame {
WipConsoleCallFrame.fromMap(this._map);

int get columnNumber => _map['columnNumber'] as int;

String get functionName => _map['functionName'] as String;

int get lineNumber => _map['lineNumber'] as int;

String get scriptId => _map['scriptId'] as String;

String get url => _map['url'] as String;
}
191 changes: 178 additions & 13 deletions lib/src/debugger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,140 @@ class WipDebugger extends WipDomain {
});
}

Future enable() => sendCommand('Debugger.enable');
Future disable() => sendCommand('Debugger.disable');
Future<WipResponse> enable() => sendCommand('Debugger.enable');

Future<WipResponse> disable() => sendCommand('Debugger.disable');

Future<String> getScriptSource(String scriptId) async =>
(await sendCommand('Debugger.getScriptSource',
params: {'scriptId': scriptId}))
.result['scriptSource'] as String;

Future pause() => sendCommand('Debugger.pause');
Future resume() => sendCommand('Debugger.resume');
Future<WipResponse> pause() => sendCommand('Debugger.pause');

Future<WipResponse> resume() => sendCommand('Debugger.resume');

Future<WipResponse> stepInto() => sendCommand('Debugger.stepInto');

Future<WipResponse> stepOut() => sendCommand('Debugger.stepOut');

Future stepInto() => sendCommand('Debugger.stepInto');
Future stepOut() => sendCommand('Debugger.stepOut');
Future stepOver() => sendCommand('Debugger.stepOver');
Future<WipResponse> stepOver() => sendCommand('Debugger.stepOver');

Future setPauseOnExceptions(PauseState state) =>
sendCommand('Debugger.setPauseOnExceptions',
params: {'state': _pauseStateToString(state)});
Future<WipResponse> setPauseOnExceptions(PauseState state) {
return sendCommand('Debugger.setPauseOnExceptions',
params: {'state': _pauseStateToString(state)});
}

/// Sets JavaScript breakpoint at a given location.
///
/// - `location`: Location to set breakpoint in
/// - `condition`: Expression to use as a breakpoint condition. When
/// specified, debugger will only stop on the breakpoint if this expression
/// evaluates to true.
Future<SetBreakpointResponse> setBreakpoint(
WipLocation location, {
String condition,
}) async {
Map<String, dynamic> params = {
'location': location.toJsonMap(),
};
if (condition != null) {
params['condition'] = condition;
}

final WipResponse response =
await sendCommand('Debugger.setBreakpoint', params: params);

if (response.result.containsKey('exceptionDetails')) {
throw new ExceptionDetails(
response.result['exceptionDetails'] as Map<String, dynamic>);
} else {
return new SetBreakpointResponse(response.json);
}
}

/// Removes JavaScript breakpoint.
Future<WipResponse> removeBreakpoint(String breakpointId) {
return sendCommand('Debugger.removeBreakpoint',
params: {'breakpointId': breakpointId});
}

/// Evaluates expression on a given call frame.
///
/// - `callFrameId`: Call frame identifier to evaluate on
/// - `expression`: Expression to evaluate
/// - `returnByValue`: Whether the result is expected to be a JSON object that
/// should be sent by value
Future<RemoteObject> evaluateOnCallFrame(
String callFrameId,
String expression, {
bool returnByValue,
}) async {
Map<String, dynamic> params = {
'callFrameId': callFrameId,
'expression': expression,
};
if (returnByValue != null) {
params['returnByValue'] = returnByValue;
}

final WipResponse response =
await sendCommand('Debugger.evaluateOnCallFrame', params: params);

if (response.result.containsKey('exceptionDetails')) {
throw new ExceptionDetails(
response.result['exceptionDetails'] as Map<String, dynamic>);
} else {
return new RemoteObject(
response.result['result'] as Map<String, dynamic>);
}
}

/// Returns possible locations for breakpoint. scriptId in start and end range
/// locations should be the same.
///
/// - `start`: Start of range to search possible breakpoint locations in
/// - `end`: End of range to search possible breakpoint locations in
/// (excluding). When not specified, end of scripts is used as end of range.
/// - `restrictToFunction`: Only consider locations which are in the same
/// (non-nested) function as start.
Future<List<WipBreakLocation>> getPossibleBreakpoints(
WipLocation start, {
WipLocation end,
bool restrictToFunction,
}) async {
Map<String, dynamic> params = {
'start': start.toJsonMap(),
};
if (end != null) {
params['end'] = end.toJsonMap();
}
if (restrictToFunction != null) {
params['restrictToFunction'] = restrictToFunction;
}

final WipResponse response =
await sendCommand('Debugger.getPossibleBreakpoints', params: params);

if (response.result.containsKey('exceptionDetails')) {
throw new ExceptionDetails(
response.result['exceptionDetails'] as Map<String, dynamic>);
} else {
List locations = response.result['locations'];
return List.from(locations.map((map) => WipBreakLocation(map)));
}
}

Stream<DebuggerPausedEvent> get onPaused => eventStream(
'Debugger.paused', (WipEvent event) => new DebuggerPausedEvent(event));

Stream<GlobalObjectClearedEvent> get onGlobalObjectCleared => eventStream(
'Debugger.globalObjectCleared',
(WipEvent event) => new GlobalObjectClearedEvent(event));

Stream<DebuggerResumedEvent> get onResumed => eventStream(
'Debugger.resumed', (WipEvent event) => new DebuggerResumedEvent(event));

Stream<ScriptParsedEvent> get onScriptParsed => eventStream(
'Debugger.scriptParsed',
(WipEvent event) => new ScriptParsedEvent(event));
Expand Down Expand Up @@ -72,6 +180,8 @@ class ScriptParsedEvent extends WrappedWipEvent {
ScriptParsedEvent(WipEvent event)
: this.script = new WipScript(event.params),
super(event);

String toString() => script.toString();
}

class GlobalObjectClearedEvent extends WrappedWipEvent {
Expand All @@ -86,6 +196,7 @@ class DebuggerPausedEvent extends WrappedWipEvent {
DebuggerPausedEvent(WipEvent event) : super(event);

String get reason => params['reason'] as String;

Object get data => params['data'];

Iterable<WipCallFrame> getCallFrames() => (params['callFrames'] as List)
Expand All @@ -100,9 +211,12 @@ class WipCallFrame {
WipCallFrame(this._map);

String get callFrameId => _map['callFrameId'] as String;

String get functionName => _map['functionName'] as String;

WipLocation get location =>
new WipLocation(_map['location'] as Map<String, dynamic>);

WipRemoteObject get thisObject =>
new WipRemoteObject(_map['this'] as Map<String, dynamic>);

Expand All @@ -117,9 +231,24 @@ class WipLocation {

WipLocation(this._map);

int get columnNumber => _map['columnNumber'] as int;
int get lineNumber => _map['lineNumber'] as int;
String get scriptId => _map['scriptId'] as String;
WipLocation.fromValues(String scriptId, int lineNumber, {int columnNumber})
: _map = {} {
_map['scriptId'] = scriptId;
_map['lineNumber'] = lineNumber;
if (columnNumber != null) {
_map['columnNumber'] = columnNumber;
}
}

String get scriptId => _map['scriptId'];

int get lineNumber => _map['lineNumber'];

int get columnNumber => _map['columnNumber'];

Map<String, dynamic> toJsonMap() {
return _map;
}

String toString() => '[${scriptId}:${lineNumber}:${columnNumber}]';
}
Expand All @@ -130,10 +259,15 @@ class WipRemoteObject {
WipRemoteObject(this._map);

String get className => _map['className'] as String;

String get description => _map['description'] as String;

String get objectId => _map['objectId'] as String;

String get subtype => _map['subtype'] as String;

String get type => _map['type'] as String;

Object get value => _map['value'];
}

Expand All @@ -143,12 +277,19 @@ class WipScript {
WipScript(this._map);

String get scriptId => _map['scriptId'] as String;

String get url => _map['url'] as String;

int get startLine => _map['startLine'] as int;

int get startColumn => _map['startColumn'] as int;

int get endLine => _map['endLine'] as int;

int get endColumn => _map['endColumn'] as int;

bool get isContentScript => _map['isContentScript'] as bool;

String get sourceMapURL => _map['sourceMapURL'] as String;

String toString() => '[script ${scriptId}: ${url}]';
Expand All @@ -168,3 +309,27 @@ class WipScope {
WipRemoteObject get object =>
new WipRemoteObject(_map['object'] as Map<String, dynamic>);
}

class WipBreakLocation extends WipLocation {
WipBreakLocation(Map<String, dynamic> map) : super(map);

WipBreakLocation.fromValues(String scriptId, int lineNumber,
{int columnNumber, String type})
: super.fromValues(scriptId, lineNumber, columnNumber: columnNumber) {
if (type != null) {
_map['type'] = type;
}
}

/// Allowed Values: `debuggerStatement`, `call`, `return`.
String get type => _map['type'];
}

/// The response from [WipDebugger.setBreakpoint].
class SetBreakpointResponse extends WipResponse {
SetBreakpointResponse(Map<String, dynamic> json) : super(json);

String get breakpointId => result['breakpointId'];

WipLocation get actualLocation => WipLocation(result['actualLocation']);
}
5 changes: 3 additions & 2 deletions lib/src/log.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import '../webkit_inspection_protocol.dart';
class WipLog extends WipDomain {
WipLog(WipConnection connection) : super(connection);

Future enable() => sendCommand('Log.enable');
Future disable() => sendCommand('Log.disable');
Future<WipResponse> enable() => sendCommand('Log.enable');

Future<WipResponse> disable() => sendCommand('Log.disable');

Stream<LogEntry> get onEntryAdded =>
eventStream('Log.entryAdded', (WipEvent event) => new LogEntry(event));
Expand Down
12 changes: 6 additions & 6 deletions lib/src/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import '../webkit_inspection_protocol.dart';
class WipPage extends WipDomain {
WipPage(WipConnection connection) : super(connection);

Future enable() => sendCommand('Page.enable');
Future disable() => sendCommand('Page.disable');
Future<WipResponse> enable() => sendCommand('Page.enable');

Future navigate(String url) =>
Future<WipResponse> disable() => sendCommand('Page.disable');

Future<WipResponse> navigate(String url) =>
sendCommand('Page.navigate', params: {'url': url});

Future reload({bool ignoreCache, String scriptToEvaluateOnLoad}) {
Future<WipResponse> reload(
{bool ignoreCache, String scriptToEvaluateOnLoad}) {
var params = <String, dynamic>{};

if (ignoreCache != null) {
params['ignoreCache'] = ignoreCache;
}

if (scriptToEvaluateOnLoad != null) {
params['scriptToEvaluateOnLoad'] = scriptToEvaluateOnLoad;
}
Expand Down
Loading

0 comments on commit b0797c9

Please sign in to comment.