-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathdwds_vm_client.dart
369 lines (338 loc) · 12.5 KB
/
dwds_vm_client.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:convert';
import 'package:dwds/src/events.dart';
import 'package:dwds/src/services/chrome_debug_exception.dart';
import 'package:dwds/src/services/chrome_proxy_service.dart';
import 'package:dwds/src/services/debug_service.dart';
import 'package:dwds/src/utilities/shared.dart';
import 'package:dwds/src/utilities/synchronized.dart';
import 'package:logging/logging.dart';
import 'package:uuid/uuid.dart';
import 'package:vm_service/vm_service.dart';
import 'package:vm_service_interface/vm_service_interface.dart';
import 'package:webkit_inspection_protocol/webkit_inspection_protocol.dart';
final _logger = Logger('DwdsVmClient');
// A client of the vm service that registers some custom extensions like
// hotRestart.
class DwdsVmClient {
final VmService client;
final StreamController<Map<String, Object>> _requestController;
final StreamController<Map<String, Object?>> _responseController;
static const int kFeatureDisabled = 100;
static const String kFeatureDisabledMessage = 'Feature is disabled.';
/// Null until [close] is called.
///
/// All subsequent calls to [close] will return this future.
Future<void>? _closed;
/// Synchronizes hot restarts to avoid races.
final _hotRestartQueue = AtomicQueue();
DwdsVmClient(this.client, this._requestController, this._responseController);
Future<void> close() => _closed ??= () async {
await _requestController.close();
await _responseController.close();
await client.dispose();
}();
static Future<DwdsVmClient> create(
DebugService debugService,
DwdsStats dwdsStats,
) async {
// Set up hot restart as an extension.
final requestController = StreamController<Map<String, Object>>();
final responseController = StreamController<Map<String, Object?>>();
VmServerConnection(
requestController.stream,
responseController.sink,
debugService.serviceExtensionRegistry,
debugService.chromeProxyService,
);
final client =
VmService(responseController.stream.map(jsonEncode), (request) {
if (requestController.isClosed) {
_logger.warning(
'Attempted to send a request but the connection is closed:\n\n'
'$request');
return;
}
requestController.sink.add(Map<String, Object>.from(jsonDecode(request)));
});
final chromeProxyService =
debugService.chromeProxyService as ChromeProxyService;
final dwdsVmClient =
DwdsVmClient(client, requestController, responseController);
// Register '_flutter.listViews' method on the chrome proxy service vm.
// In native world, this method is provided by the engine, but the web
// engine is not aware of the VM uri or the isolates.
//
// Issue: https://github.com/dart-lang/webdev/issues/1315
client.registerServiceCallback('_flutter.listViews', (request) async {
final vm = await chromeProxyService.getVM();
final isolates = vm.isolates;
return <String, dynamic>{
'result': <String, Object>{
'views': <Object>[
for (var isolate in isolates ?? [])
<String, Object>{
'id': isolate.id,
'isolate': isolate.toJson(),
},
],
},
};
});
await client.registerService('_flutter.listViews', 'DWDS');
client.registerServiceCallback(
'hotRestart',
(request) => captureElapsedTime(
() => dwdsVmClient.hotRestart(chromeProxyService, client),
(_) => DwdsEvent.hotRestart(),
),
);
await client.registerService('hotRestart', 'DWDS');
client.registerServiceCallback(
'fullReload',
(request) => captureElapsedTime(
() => _fullReload(chromeProxyService),
(_) => DwdsEvent.fullReload(),
),
);
await client.registerService('fullReload', 'DWDS');
client.registerServiceCallback('ext.dwds.screenshot', (_) async {
await chromeProxyService.remoteDebugger.enablePage();
final response = await chromeProxyService.remoteDebugger
.sendCommand('Page.captureScreenshot');
return {'result': response.result};
});
await client.registerService('ext.dwds.screenshot', 'DWDS');
client.registerServiceCallback('ext.dwds.sendEvent', (event) async {
_processSendEvent(event, dwdsStats);
return {'result': Success().toJson()};
});
await client.registerService('ext.dwds.sendEvent', 'DWDS');
client.registerServiceCallback('ext.dwds.emitEvent', (event) async {
emitEvent(
DwdsEvent(
event['type'] as String,
event['payload'] as Map<String, dynamic>,
),
);
return {'result': Success().toJson()};
});
await client.registerService('ext.dwds.emitEvent', 'DWDS');
client.registerServiceCallback('_yieldControlToDDS', (request) async {
final ddsUri = request['uri'] as String?;
if (ddsUri == null) {
return RPCError(
request['method'] as String,
RPCErrorKind.kInvalidParams.code,
"'Missing parameter: 'uri'",
).toMap();
}
return DebugService.yieldControlToDDS(ddsUri)
? {'result': Success().toJson()}
: {
'error': {
'code': kFeatureDisabled,
'message': kFeatureDisabledMessage,
'data':
'Existing VM service clients prevent DDS from taking control.',
},
};
});
await client.registerService('_yieldControlToDDS', 'DWDS');
return dwdsVmClient;
}
Future<Map<String, dynamic>> hotRestart(
ChromeProxyService chromeProxyService,
VmService client,
) {
return _hotRestartQueue.run(() => _hotRestart(chromeProxyService, client));
}
}
void _processSendEvent(
Map<String, dynamic> event,
DwdsStats dwdsStats,
) {
final type = event['type'] as String?;
final payload = event['payload'] as Map<String, dynamic>?;
switch (type) {
case 'DevtoolsEvent':
{
_logger.finest('Received DevTools event: $event');
final action = payload?['action'] as String?;
final screen = payload?['screen'] as String?;
if (screen != null && action == 'pageReady') {
_recordDwdsStats(dwdsStats, screen);
} else {
_logger.finest('Ignoring unknown event: $event');
}
}
}
}
void _recordDwdsStats(DwdsStats dwdsStats, String screen) {
if (dwdsStats.isFirstDebuggerReady) {
final devToolsStart = dwdsStats.devToolsStart;
final debuggerStart = dwdsStats.debuggerStart;
if (devToolsStart != null) {
final devToolLoadTime =
DateTime.now().difference(devToolsStart).inMilliseconds;
emitEvent(DwdsEvent.devToolsLoad(devToolLoadTime, screen));
_logger.fine('DevTools load time: $devToolLoadTime ms');
}
if (debuggerStart != null) {
final debuggerReadyTime =
DateTime.now().difference(debuggerStart).inMilliseconds;
emitEvent(DwdsEvent.debuggerReady(debuggerReadyTime, screen));
_logger.fine('Debugger ready time: $debuggerReadyTime ms');
}
} else {
_logger.finest('Debugger and DevTools stats are already recorded.');
}
}
Future<int> tryGetContextId(
ChromeProxyService chromeProxyService, {
int retries = 3,
}) async {
const waitInMs = 50;
for (var retry = 0; retry < retries; retry++) {
final tryId = await chromeProxyService.executionContext.id;
if (tryId != null) return tryId;
await Future.delayed(const Duration(milliseconds: waitInMs));
}
throw StateError('No context with the running Dart application.');
}
Future<Map<String, dynamic>> _hotRestart(
ChromeProxyService chromeProxyService,
VmService client,
) async {
_logger.info('Attempting a hot restart');
chromeProxyService.terminatingIsolates = true;
await _disableBreakpointsAndResume(client, chromeProxyService);
try {
_logger.info('Attempting to get execution context ID.');
await tryGetContextId(chromeProxyService);
_logger.info('Got execution context ID.');
} on StateError catch (e) {
// We couldn't find the execution context. `hotRestart` may have been
// triggered in the middle of a full reload.
return {
'error': {
'code': RPCErrorKind.kInternalError.code,
'message': e.message,
},
};
}
// Start listening for isolate create events before issuing a hot
// restart. Only return success after the isolate has fully started.
final stream = chromeProxyService.onEvent('Isolate');
try {
// If we should pause isolates on start, then only run main once we get a
// resume event.
final pauseIsolatesOnStart = chromeProxyService.pauseIsolatesOnStart;
if (pauseIsolatesOnStart) {
_waitForResumeEventToRunMain(chromeProxyService);
}
// Generate run id to hot restart all apps loaded into the tab.
final runId = const Uuid().v4().toString();
_logger.info('Issuing \$dartHotRestartDwds request');
await chromeProxyService.inspector.jsEvaluate(
'\$dartHotRestartDwds(\'$runId\', $pauseIsolatesOnStart);',
awaitPromise: true,
);
_logger.info('\$dartHotRestartDwds request complete.');
} on WipError catch (exception) {
final code = exception.error?['code'];
final message = exception.error?['message'];
// This corresponds to `Execution context was destroyed` which can
// occur during a hot restart that must fall back to a full reload.
if (code != RPCErrorKind.kServerError.code) {
return {
'error': {
'code': code,
'message': message,
'data': exception,
},
};
}
} on ChromeDebugException catch (exception) {
// Exceptions thrown by the injected client during hot restart.
return {
'error': {
'code': RPCErrorKind.kInternalError.code,
'message': '$exception',
},
};
}
_logger.info('Waiting for Isolate Start event.');
await stream.firstWhere((event) => event.kind == EventKind.kIsolateStart);
chromeProxyService.terminatingIsolates = false;
_logger.info('Successful hot restart');
return {'result': Success().toJson()};
}
void _waitForResumeEventToRunMain(
ChromeProxyService chromeProxyService,
) {
final issuedReadyToRunMainCompleter = Completer<void>();
final resumeEventsSubscription =
chromeProxyService.resumeAfterHotRestartEventsStream.listen((_) async {
await chromeProxyService.inspector.jsEvaluate('\$dartReadyToRunMain();');
if (!issuedReadyToRunMainCompleter.isCompleted) {
issuedReadyToRunMainCompleter.complete();
}
});
safeUnawaited(
issuedReadyToRunMainCompleter.future.then((_) {
resumeEventsSubscription.cancel();
}),
);
}
Future<Map<String, dynamic>> _fullReload(
ChromeProxyService chromeProxyService,
) async {
_logger.info('Attempting a full reload');
await chromeProxyService.remoteDebugger.enablePage();
await chromeProxyService.remoteDebugger.pageReload();
_logger.info('Successful full reload');
return {'result': Success().toJson()};
}
Future<void> _disableBreakpointsAndResume(
VmService client,
ChromeProxyService chromeProxyService,
) async {
_logger.info('Attempting to disable breakpoints and resume the isolate');
final vm = await client.getVM();
final isolates = vm.isolates;
if (isolates == null || isolates.isEmpty) {
throw StateError('No active isolate to resume.');
}
final isolateId = isolates.first.id;
if (isolateId == null) {
throw StateError('No active isolate to resume.');
}
await chromeProxyService.disableBreakpoints();
try {
// Any checks for paused status result in race conditions or hangs
// at this point:
//
// - `getIsolate()` and check for status:
// the app might still pause on existing breakpoint.
//
// - `pause()` and wait for `Debug.paused` event:
// chrome does not send the `Debug.Paused `notification
// without shifting focus to chrome.
//
// Instead, just try resuming and
// ignore failures indicating that the app is already running:
//
// WipError -32000 Can only perform operation while paused.
await client.resume(isolateId);
} on RPCError catch (e, s) {
if (!e.message.contains('Can only perform operation while paused')) {
_logger.severe('Hot restart failed to resume exiting isolate', e, s);
rethrow;
}
}
_logger.info('Successfully disabled breakpoints and resumed the isolate');
}