launchDevTools method

Future<Map<String, dynamic>> launchDevTools(
  1. Map<String, dynamic> params,
  2. Uri vmServiceUri,
  3. String devToolsUrl,
  4. bool headlessMode,
  5. bool machineMode,
)

Implementation

Future<Map<String, dynamic>> launchDevTools(
  Map<String, dynamic> params,
  Uri vmServiceUri,
  String devToolsUrl,
  bool headlessMode,
  bool machineMode,
) async {
  // First see if we have an existing DevTools client open that we can
  // reuse.
  final canReuse =
      params.containsKey('reuseWindows') && params['reuseWindows'] == true;
  final shouldNotify =
      params.containsKey('notify') && params['notify'] == true;
  final page = params['page'];
  if (canReuse &&
      await _tryReuseExistingDevToolsInstance(
        vmServiceUri,
        page,
        shouldNotify,
      )) {
    _emitLaunchEvent(
      reused: true,
      notified: shouldNotify,
      pid: null,
      machineMode: machineMode,
    );
    return {
      'reused': true,
      'notified': shouldNotify,
    };
  }

  final uriParams = <String, dynamic>{};

  // Copy over queryParams passed by the client
  params['queryParams']?.forEach((key, value) => uriParams[key] = value);

  // Add the URI to the VM service
  uriParams['uri'] = vmServiceUri.toString();

  final devToolsUri = Uri.parse(devToolsUrl);
  final uriToLaunch = _buildUriToLaunch(uriParams, page, devToolsUri);

  // TODO(dantup): When ChromeOS has support for tunneling all ports we can
  // change this to always use the native browser for ChromeOS and may wish to
  // handle this inside `browser_launcher`; https://crbug.com/848063.
  final useNativeBrowser = _isChromeOS &&
      _isAccessibleToChromeOSNativeBrowser(devToolsUri) &&
      _isAccessibleToChromeOSNativeBrowser(vmServiceUri);
  int? browserPid;
  if (useNativeBrowser) {
    await Process.start('x-www-browser', [uriToLaunch.toString()]);
  } else {
    final args = headlessMode
        ? [
            '--headless',
            // When running headless, Chrome will quit immediately after loading
            // the page unless we have the debug port open.
            '--remote-debugging-port=9223',
            '--disable-gpu',
            '--no-sandbox',
          ]
        : <String>[];
    final proc = await Chrome.start([uriToLaunch.toString()], args: args);
    browserPid = proc.pid;
  }
  _emitLaunchEvent(
      reused: false,
      notified: false,
      pid: browserPid!,
      machineMode: machineMode);
  return {
    'reused': false,
    'notified': false,
    'pid': browserPid,
  };
}