Skip to content

Commit

Permalink
Reland "Add support for Dart Development Service (DDS) in Flutter Too…
Browse files Browse the repository at this point in the history
…ls (flutter#61276)" (flutter#61395)

This reverts commit 5b9c6e2.
  • Loading branch information
bkonyi authored and Pragya007 committed Aug 5, 2020
1 parent 55be696 commit eb3b577
Show file tree
Hide file tree
Showing 25 changed files with 211 additions and 33 deletions.
4 changes: 4 additions & 0 deletions dev/tracing_tests/test/image_painting_event_test.dart
Expand Up @@ -38,6 +38,10 @@ void main() {
await binding.endOfFrame;
});

tearDownAll(() {
vmService.dispose();
});

test('Image painting events - deduplicates across frames', () async {
final Completer<Event> completer = Completer<Event>();
vmService.onExtensionEvent.first.then(completer.complete);
Expand Down
49 changes: 49 additions & 0 deletions packages/flutter_tools/lib/src/base/dds.dart
@@ -0,0 +1,49 @@
// Copyright 2014 The Flutter Authors. 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 'package:dds/dds.dart' as dds;
import 'package:meta/meta.dart';

import 'io.dart' as io;
import 'logger.dart';

/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes.
class DartDevelopmentService {
DartDevelopmentService({@required this.logger});

final Logger logger;
dds.DartDevelopmentService _ddsInstance;

Future<void> startDartDevelopmentService(
Uri observatoryUri,
bool ipv6,
) async {
final Uri ddsUri = Uri(
scheme: 'http',
host: (ipv6 ?
io.InternetAddress.loopbackIPv6 :
io.InternetAddress.loopbackIPv4
).host,
port: 0,
);
logger.printTrace(
'Launching a Dart Developer Service (DDS) instance at $ddsUri, '
'connecting to VM service at $observatoryUri.',
);
try {
_ddsInstance = await dds.DartDevelopmentService.startDartDevelopmentService(
observatoryUri,
serviceUri: ddsUri,
);
logger.printTrace('DDS is listening at ${_ddsInstance.uri}.');
} on dds.DartDevelopmentServiceException catch (e) {
logger.printError('Warning: Failed to start DDS: ${e.message}');
}
}

Future<void> shutdown() async => await _ddsInstance?.shutdown();
}
3 changes: 2 additions & 1 deletion packages/flutter_tools/lib/src/commands/attach.dart
Expand Up @@ -100,6 +100,7 @@ class AttachCommand extends FlutterCommand {
'and progress in machine friendly format.',
);
usesTrackWidgetCreation(verboseHelp: verboseHelp);
addDdsOptions(verboseHelp: verboseHelp);
hotRunnerFactory ??= HotRunnerFactory();
}

Expand Down Expand Up @@ -372,7 +373,7 @@ class AttachCommand extends FlutterCommand {
);
flutterDevice.observatoryUris = observatoryUris;
final List<FlutterDevice> flutterDevices = <FlutterDevice>[flutterDevice];
final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(getBuildInfo());
final DebuggingOptions debuggingOptions = DebuggingOptions.enabled(getBuildInfo(), disableDds: boolArg('disable-dds'));

return getBuildInfo().isDebug
? hotRunnerFactory.build(
Expand Down
4 changes: 4 additions & 0 deletions packages/flutter_tools/lib/src/commands/drive.dart
Expand Up @@ -244,6 +244,10 @@ class DriveCommand extends RunCommandBase {
throwToolExit('Application failed to start. Will not run test. Quitting.', exitCode: 1);
}
observatoryUri = result.observatoryUri.toString();
// TODO(bkonyi): add web support (https://github.com/flutter/flutter/issues/61259)
if (!isWebPlatform) {
await device.dds.startDartDevelopmentService(Uri.parse(observatoryUri), ipv6);
}
} else {
globals.printStatus('Will connect to already running application instance.');
observatoryUri = stringArg('use-existing-app');
Expand Down
2 changes: 2 additions & 0 deletions packages/flutter_tools/lib/src/commands/run.dart
Expand Up @@ -218,6 +218,7 @@ class RunCommand extends RunCommandBase {
'Currently this is only supported on Android devices. This option '
'cannot be paired with --use-application-binary.'
);
addDdsOptions(verboseHelp: verboseHelp);
}

@override
Expand Down Expand Up @@ -384,6 +385,7 @@ class RunCommand extends RunCommandBase {
buildInfo,
startPaused: boolArg('start-paused'),
disableServiceAuthCodes: boolArg('disable-service-auth-codes'),
disableDds: boolArg('disable-dds'),
dartFlags: stringArg('dart-flags') ?? '',
useTestFonts: boolArg('use-test-fonts'),
enableSoftwareRendering: boolArg('enable-software-rendering'),
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter_tools/lib/src/device.dart
Expand Up @@ -16,6 +16,7 @@ import 'application_package.dart';
import 'artifacts.dart';
import 'base/config.dart';
import 'base/context.dart';
import 'base/dds.dart';
import 'base/file_system.dart';
import 'base/io.dart';
import 'base/logger.dart';
Expand Down Expand Up @@ -547,6 +548,12 @@ abstract class Device {
/// Get the port forwarder for this device.
DevicePortForwarder get portForwarder;

/// Get the DDS instance for this device.
DartDevelopmentService get dds => _dds ??= DartDevelopmentService(
logger: globals.logger,
);
DartDevelopmentService _dds;

/// Clear the device's logs.
void clearLogs();

Expand Down Expand Up @@ -720,6 +727,7 @@ class DebuggingOptions {
this.buildInfo, {
this.startPaused = false,
this.disableServiceAuthCodes = false,
this.disableDds = false,
this.dartFlags = '',
this.enableSoftwareRendering = false,
this.skiaDeterministicRendering = false,
Expand Down Expand Up @@ -762,6 +770,7 @@ class DebuggingOptions {
startPaused = false,
dartFlags = '',
disableServiceAuthCodes = false,
disableDds = false,
enableSoftwareRendering = false,
skiaDeterministicRendering = false,
traceSkia = false,
Expand All @@ -781,6 +790,7 @@ class DebuggingOptions {
final bool startPaused;
final String dartFlags;
final bool disableServiceAuthCodes;
final bool disableDds;
final bool enableSoftwareRendering;
final bool skiaDeterministicRendering;
final bool traceSkia;
Expand Down
10 changes: 10 additions & 0 deletions packages/flutter_tools/lib/src/fuchsia/fuchsia_device.dart
Expand Up @@ -52,6 +52,13 @@ Future<vm_service.VmService> _kDefaultFuchsiaIsolateDiscoveryConnector(Uri uri)
return connectToVmService(uri);
}

Future<void> _kDefaultDartDevelopmentServiceStarter(
Device device,
Uri observatoryUri,
) async {
await device.dds.startDartDevelopmentService(observatoryUri, true);
}

/// Read the log for a particular device.
class _FuchsiaLogReader extends DeviceLogReader {
_FuchsiaLogReader(this._device, this._systemClock, [this._app]);
Expand Down Expand Up @@ -695,6 +702,7 @@ class FuchsiaIsolateDiscoveryProtocol {
this._device,
this._isolateName, [
this._vmServiceConnector = _kDefaultFuchsiaIsolateDiscoveryConnector,
this._ddsStarter = _kDefaultDartDevelopmentServiceStarter,
this._pollOnce = false,
]);

Expand All @@ -704,6 +712,7 @@ class FuchsiaIsolateDiscoveryProtocol {
final String _isolateName;
final Completer<Uri> _foundUri = Completer<Uri>();
final Future<vm_service.VmService> Function(Uri) _vmServiceConnector;
final Future<void> Function(Device, Uri) _ddsStarter;
// whether to only poll once.
final bool _pollOnce;
Timer _pollingTimer;
Expand Down Expand Up @@ -746,6 +755,7 @@ class FuchsiaIsolateDiscoveryProtocol {
final int localPort = await _device.portForwarder.forward(port);
try {
final Uri uri = Uri.parse('http://[$_ipv6Loopback]:$localPort');
await _ddsStarter(_device, uri);
service = await _vmServiceConnector(uri);
_ports[port] = service;
} on SocketException catch (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/flutter_tools/lib/src/ios/devices.dart
Expand Up @@ -437,7 +437,7 @@ class IOSDevice extends Device {
);
final Uri localUri = await fallbackDiscovery.discover(
assumedDevicePort: assumedObservatoryPort,
deivce: this,
device: this,
usesIpv6: ipv6,
hostVmservicePort: debuggingOptions.hostVmServicePort,
packageId: packageId,
Expand Down
4 changes: 2 additions & 2 deletions packages/flutter_tools/lib/src/ios/fallback_discovery.dart
Expand Up @@ -67,7 +67,7 @@ class FallbackDiscovery {
Future<Uri> discover({
@required int assumedDevicePort,
@required String packageId,
@required Device deivce,
@required Device device,
@required bool usesIpv6,
@required int hostVmservicePort,
@required String packageName,
Expand All @@ -84,7 +84,7 @@ class FallbackDiscovery {
try {
final Uri result = await _mDnsObservatoryDiscovery.getObservatoryUri(
packageId,
deivce,
device,
usesIpv6: usesIpv6,
hostVmservicePort: hostVmservicePort,
);
Expand Down
21 changes: 20 additions & 1 deletion packages/flutter_tools/lib/src/resident_runner.dart
Expand Up @@ -211,6 +211,8 @@ class FlutterDevice {
ReloadMethod reloadMethod,
GetSkSLMethod getSkSLMethod,
PrintStructuredErrorLogMethod printStructuredErrorLogMethod,
bool disableDds = false,
bool ipv6 = false,
}) {
final Completer<void> completer = Completer<void>();
StreamSubscription<void> subscription;
Expand All @@ -221,7 +223,12 @@ class FlutterDevice {
globals.printTrace('Connecting to service protocol: $observatoryUri');
isWaitingForVm = true;
vm_service.VmService service;

if (!disableDds) {
await device.dds.startDartDevelopmentService(
observatoryUri,
ipv6,
);
}
try {
service = await connectToVmService(
observatoryUri,
Expand Down Expand Up @@ -952,13 +959,15 @@ abstract class ResidentRunner {
Future<void> exit() async {
_exited = true;
await shutdownDevtools();
await shutdownDartDevelopmentService();
await stopEchoingDeviceLog();
await preExit();
await exitApp();
}

Future<void> detach() async {
await shutdownDevtools();
await shutdownDartDevelopmentService();
await stopEchoingDeviceLog();
await preExit();
appFinished();
Expand Down Expand Up @@ -1127,6 +1136,14 @@ abstract class ResidentRunner {
);
}

Future<void> shutdownDartDevelopmentService() async {
await Future.wait<void>(
flutterDevices.map<Future<void>>(
(FlutterDevice device) => device.device?.dds?.shutdown()
).where((Future<void> element) => element != null)
);
}

@protected
void cacheInitialDillCompilation() {
if (_dillOutputPath != null) {
Expand Down Expand Up @@ -1180,9 +1197,11 @@ abstract class ResidentRunner {
reloadSources: reloadSources,
restart: restart,
compileExpression: compileExpression,
disableDds: debuggingOptions.disableDds,
reloadMethod: reloadMethod,
getSkSLMethod: getSkSLMethod,
printStructuredErrorLogMethod: printStructuredErrorLog,
ipv6: ipv6,
);
// This will wait for at least one flutter view before returning.
final Status status = globals.logger.startProgress(
Expand Down
13 changes: 13 additions & 0 deletions packages/flutter_tools/lib/src/runner/flutter_command.dart
Expand Up @@ -298,6 +298,19 @@ abstract class FlutterCommand extends Command<void> {
_usesPortOption = true;
}

void addDdsOptions({@required bool verboseHelp}) {
argParser.addFlag(
'disable-dds',
hide: !verboseHelp,
help: 'Disable the Dart Developer Service (DDS). This flag should only be provided'
' when attaching to an application with an existing DDS instance (e.g.,'
' attaching to an application currently connected to by "flutter run") or'
' when running certain tests.\n'
'Note: passing this flag may degrade IDE functionality if a DDS instance is not'
' already connected to the target application.'
);
}

/// Gets the vmservice port provided to in the 'observatory-port' or
/// 'host-vmservice-port option.
///
Expand Down
15 changes: 9 additions & 6 deletions packages/flutter_tools/lib/src/test/flutter_platform.dart
Expand Up @@ -4,6 +4,7 @@

import 'dart:async';

import 'package:dds/dds.dart';
import 'package:meta/meta.dart';
import 'package:package_config/package_config.dart';
import 'package:stream_channel/stream_channel.dart';
Expand Down Expand Up @@ -514,7 +515,7 @@ class FlutterPlatform extends PlatformPlugin {
Uri processObservatoryUri;
_pipeStandardStreamsToConsole(
process,
reportObservatoryUri: (Uri detectedUri) {
reportObservatoryUri: (Uri detectedUri) async {
assert(processObservatoryUri == null);
assert(explicitObservatoryPort == null ||
explicitObservatoryPort == detectedUri.port);
Expand All @@ -527,13 +528,14 @@ class FlutterPlatform extends PlatformPlugin {
globals.printTrace('test $ourTestCount: using observatory uri $detectedUri from pid ${process.pid}');
}
processObservatoryUri = detectedUri;
await DartDevelopmentService.startDartDevelopmentService(processObservatoryUri);
{
globals.printTrace('Connecting to service protocol: $processObservatoryUri');
final Future<vm_service.VmService> localVmService = connectToVmService(processObservatoryUri,
compileExpression: _compileExpressionService);
localVmService.then((vm_service.VmService vmservice) {
unawaited(localVmService.then((vm_service.VmService vmservice) {
globals.printTrace('Successfully connected to service protocol: $processObservatoryUri');
});
}));
}
gotProcessObservatoryUri.complete();
watcher?.handleStartedProcess(
Expand Down Expand Up @@ -870,8 +872,9 @@ class FlutterPlatform extends PlatformPlugin {
void _pipeStandardStreamsToConsole(
Process process, {
void startTimeoutTimer(),
void reportObservatoryUri(Uri uri),
Future<void> reportObservatoryUri(Uri uri),
}) {

const String observatoryString = 'Observatory listening on ';
for (final Stream<List<int>> stream in <Stream<List<int>>>[
process.stderr,
Expand All @@ -881,7 +884,7 @@ class FlutterPlatform extends PlatformPlugin {
.transform<String>(utf8.decoder)
.transform<String>(const LineSplitter())
.listen(
(String line) {
(String line) async {
if (line == _kStartTimeoutTimerMessage) {
if (startTimeoutTimer != null) {
startTimeoutTimer();
Expand All @@ -894,7 +897,7 @@ class FlutterPlatform extends PlatformPlugin {
try {
final Uri uri = Uri.parse(line.substring(observatoryString.length));
if (reportObservatoryUri != null) {
reportObservatoryUri(uri);
await reportObservatoryUri(uri);
}
} on Exception catch (error) {
globals.printError('Could not parse shell observatory port message: $error');
Expand Down
6 changes: 0 additions & 6 deletions packages/flutter_tools/lib/src/tracing.dart
Expand Up @@ -22,12 +22,6 @@ class Tracing {
Tracing(this.vmService);

static const String firstUsefulFrameEventName = kFirstFrameRasterizedEventName;

static Future<Tracing> connect(Uri uri) async {
final vm_service.VmService observatory = await connectToVmService(uri);
return Tracing(observatory);
}

final vm_service.VmService vmService;

Future<void> startTracing() async {
Expand Down

0 comments on commit eb3b577

Please sign in to comment.