File size: 4,635 Bytes
a4ff84b
 
 
 
 
 
b0336a8
 
a4ff84b
 
b0336a8
 
 
a4ff84b
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ff84b
b0336a8
a4ff84b
b0336a8
a4ff84b
 
 
b0336a8
 
 
 
a4ff84b
 
 
 
 
 
 
b0336a8
 
 
a4ff84b
 
b0336a8
a4ff84b
b0336a8
 
a4ff84b
b0336a8
 
a4ff84b
b0336a8
a4ff84b
 
b0336a8
 
 
 
a4ff84b
b0336a8
a4ff84b
 
 
b0336a8
a4ff84b
 
 
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ff84b
 
 
 
b0336a8
 
a4ff84b
b0336a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a4ff84b
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
// 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';
import 'package:dds/dds_launcher.dart';
import 'package:meta/meta.dart';

import '../artifacts.dart';
import '../device.dart';
import '../globals.dart' as globals;
import 'io.dart' as io;
import 'logger.dart';

export 'package:dds/dds.dart'
    show DartDevelopmentServiceException, ExistingDartDevelopmentServiceException;

typedef DDSLauncherCallback =
    Future<DartDevelopmentServiceLauncher> Function({
      required Uri remoteVmServiceUri,
      Uri? serviceUri,
      bool enableAuthCodes,
      bool serveDevTools,
      Uri? devToolsServerAddress,
      bool enableServicePortFallback,
      List<String> cachedUserTags,
      String? dartExecutable,
      String? google3WorkspaceRoot,
    });

// TODO(fujino): This should be direct injected, rather than mutable global state.
/// Used by tests to override the DDS spawn behavior for mocking purposes.
@visibleForTesting
DDSLauncherCallback ddsLauncherCallback = DartDevelopmentServiceLauncher.start;

/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes.
class DartDevelopmentService with DartDevelopmentServiceLocalOperationsMixin {
  DartDevelopmentService({required Logger logger}) : _logger = logger;

  DartDevelopmentServiceLauncher? _ddsInstance;

  Uri? get uri => _ddsInstance?.uri ?? _existingDdsUri;
  Uri? _existingDdsUri;

  Future<void> get done => _completer.future;
  final Completer<void> _completer = Completer<void>();

  final Logger _logger;

  @override
  Future<void> startDartDevelopmentService(
    Uri vmServiceUri, {
    int? ddsPort,
    bool? disableServiceAuthCodes,
    bool? ipv6,
    bool enableDevTools = true,
    bool cacheStartupProfile = false,
    String? google3WorkspaceRoot,
    Uri? devToolsServerAddress,
  }) async {
    assert(_ddsInstance == null);
    final Uri ddsUri = Uri(
      scheme: 'http',
      host:
          ((ipv6 ?? false) ? io.InternetAddress.loopbackIPv6 : io.InternetAddress.loopbackIPv4)
              .host,
      port: ddsPort ?? 0,
    );
    _logger.printTrace(
      'Launching a Dart Developer Service (DDS) instance at $ddsUri, '
      'connecting to VM service at $vmServiceUri.',
    );
    void completeFuture() {
      if (!_completer.isCompleted) {
        _completer.complete();
      }
    }

    try {
      _ddsInstance = await ddsLauncherCallback(
        remoteVmServiceUri: vmServiceUri,
        serviceUri: ddsUri,
        enableAuthCodes: disableServiceAuthCodes != true,
        // Enables caching of CPU samples collected during application startup.
        cachedUserTags: cacheStartupProfile ? const <String>['AppStartUp'] : const <String>[],
        devToolsServerAddress: devToolsServerAddress,
        google3WorkspaceRoot: google3WorkspaceRoot,
        dartExecutable: globals.artifacts!.getArtifactPath(Artifact.engineDartBinary),
      );
      unawaited(_ddsInstance!.done.whenComplete(completeFuture));
    } on DartDevelopmentServiceException catch (e) {
      _logger.printTrace('Warning: Failed to start DDS: ${e.message}');
      if (e is ExistingDartDevelopmentServiceException) {
        _existingDdsUri = e.ddsUri;
      }
      completeFuture();
      rethrow;
    }
  }

  void shutdown() => _ddsInstance?.shutdown();
}

/// Contains common functionality that can be used with any implementation of
/// [DartDevelopmentService].
mixin DartDevelopmentServiceLocalOperationsMixin {
  Future<void> startDartDevelopmentService(
    Uri vmServiceUri, {
    int? ddsPort,
    bool? disableServiceAuthCodes,
    bool? ipv6,
    bool enableDevTools = true,
    bool cacheStartupProfile = false,
    String? google3WorkspaceRoot,
    Uri? devToolsServerAddress,
  });

  /// A convenience method used to create a [DartDevelopmentService] instance
  /// from a [DebuggingOptions] instance.
  Future<void> startDartDevelopmentServiceFromDebuggingOptions(
    Uri vmServiceUri, {
    required DebuggingOptions debuggingOptions,
  }) => startDartDevelopmentService(
    vmServiceUri,
    ddsPort: debuggingOptions.ddsPort,
    disableServiceAuthCodes: debuggingOptions.disableServiceAuthCodes,
    ipv6: debuggingOptions.ipv6,
    enableDevTools: debuggingOptions.enableDevTools,
    cacheStartupProfile: debuggingOptions.cacheStartupProfile,
    google3WorkspaceRoot: debuggingOptions.google3WorkspaceRoot,
    devToolsServerAddress: debuggingOptions.devToolsServerAddress,
  );
}