dds.dart 3.34 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:async';

7 8 9
import 'package:dds/dds.dart' as dds;
import 'package:meta/meta.dart';

10
import 'common.dart';
11 12 13
import 'io.dart' as io;
import 'logger.dart';

14 15
@visibleForTesting
Future<dds.DartDevelopmentService> Function(
16 17
  Uri remoteVmServiceUri, {
  bool enableAuthCodes,
18
  bool ipv6,
19
  Uri? serviceUri,
20
  List<String> cachedUserTags,
21 22
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;

23 24 25
/// Helper class to launch a [dds.DartDevelopmentService]. Allows for us to
/// mock out this functionality for testing purposes.
class DartDevelopmentService {
26
  dds.DartDevelopmentService? _ddsInstance;
27

28 29
  Uri? get uri => _ddsInstance?.uri ?? _existingDdsUri;
  Uri? _existingDdsUri;
30

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

34
  Future<void> startDartDevelopmentService(
35 36 37 38 39
    Uri observatoryUri, {
    required Logger logger,
    int? hostPort,
    bool? ipv6,
    bool? disableServiceAuthCodes,
40
    bool cacheStartupProfile = false,
41
  }) async {
42 43
    final Uri ddsUri = Uri(
      scheme: 'http',
44
      host: ((ipv6 ?? false) ? io.InternetAddress.loopbackIPv6 : io.InternetAddress.loopbackIPv4).host,
45
      port: hostPort ?? 0,
46 47 48 49 50 51
    );
    logger.printTrace(
      'Launching a Dart Developer Service (DDS) instance at $ddsUri, '
      'connecting to VM service at $observatoryUri.',
    );
    try {
52
      _ddsInstance = await ddsLauncherCallback(
53 54
          observatoryUri,
          serviceUri: ddsUri,
55
          enableAuthCodes: disableServiceAuthCodes != true,
56
          ipv6: ipv6 ?? false,
57 58
          // Enables caching of CPU samples collected during application startup.
          cachedUserTags: cacheStartupProfile ? const <String>['AppStartUp'] : const <String>[],
59
        );
60
      unawaited(_ddsInstance?.done.whenComplete(() {
61 62 63 64
        if (!_completer.isCompleted) {
          _completer.complete();
        }
      }));
65
      logger.printTrace('DDS is listening at ${_ddsInstance?.uri}.');
66
    } on dds.DartDevelopmentServiceException catch (e) {
67
      logger.printTrace('Warning: Failed to start DDS: ${e.message}');
68
      if (e.errorCode == dds.DartDevelopmentServiceException.existingDdsInstanceError) {
69 70 71 72 73
        try {
          _existingDdsUri = Uri.parse(
            e.message.split(' ').firstWhere((String e) => e.startsWith('http'))
          );
        } on StateError {
74 75 76
          if (e.message.contains('Existing VM service clients prevent DDS from taking control.')) {
            throwToolExit('${e.message}. Please rebuild your application with a newer version of Flutter.');
          }
77 78
          logger.printError(
            'DDS has failed to start and there is not an existing DDS instance '
79 80
            'available to connect to. Please file an issue at https://github.com/flutter/flutter/issues '
            'with the following error message:\n\n ${e.message}.'
81
          );
82 83
          // DDS was unable to start for an unknown reason. Raise a StateError
          // so it can be reported by the crash reporter.
84 85
          throw StateError(e.message);
        }
86
      }
87 88 89
      if (!_completer.isCompleted) {
        _completer.complete();
      }
90 91 92 93
      rethrow;
    }
  }

94
  Future<void> shutdown() async => _ddsInstance?.shutdown();
95
}