dds.dart 3.09 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 21
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;

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

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

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

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

90
  Future<void> shutdown() async => _ddsInstance?.shutdown();
91
}