dds.dart 3.15 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
// @dart = 2.8

7 8
import 'dart:async';

9 10 11
import 'package:dds/dds.dart' as dds;
import 'package:meta/meta.dart';

12
import 'common.dart';
13 14 15
import 'io.dart' as io;
import 'logger.dart';

16 17 18 19 20 21 22 23
@visibleForTesting
Future<dds.DartDevelopmentService> Function(
  Uri,
  {bool enableAuthCodes,
  bool ipv6,
  Uri serviceUri,
}) ddsLauncherCallback = dds.DartDevelopmentService.startDartDevelopmentService;

24 25 26 27 28 29 30 31
/// 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;

32 33
  Uri get uri => _ddsInstance?.uri ?? _existingDdsUri;
  Uri _existingDdsUri;
34

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

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

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