handleNotifyForVmServiceConnection static method

Future<Response> handleNotifyForVmServiceConnection(
  1. ServerApi api,
  2. Map<String, String> queryParams,
  3. DtdInfo? dtd
)

Implementation

static Future<shelf.Response> handleNotifyForVmServiceConnection(
  ServerApi api,
  Map<String, String> queryParams,
  DtdInfo? dtd,
) async {
  final missingRequiredParams = ServerApi._checkRequiredParameters(
    const [apiParameterValueKey, apiParameterVmServiceConnected],
    queryParams: queryParams,
    api: api,
    requestName: apiNotifyForVmServiceConnection,
  );
  if (missingRequiredParams != null) return missingRequiredParams;

  final dtdUri = dtd?.localUri;
  final dtdSecret = dtd?.secret;
  if (dtdUri == null || dtdSecret == null) {
    // If DevTools server did not start DTD, there is nothing for us to do.
    // This assertion may change in the future if the DevTools server has
    // other functionality that interacts with VM service connections from
    // DevTools app clients.
    return api.success();
  }

  final connectedAsString = queryParams[apiParameterVmServiceConnected]!;
  final bool connected;
  try {
    connected = bool.parse(connectedAsString);
  } catch (e) {
    return api.badRequest(
      'Cannot parse $apiParameterVmServiceConnected parameter:\n$e',
    );
  }

  final vmServiceUriAsString = queryParams[apiParameterValueKey]!;
  final vmServiceUri = normalizeVmServiceUri(vmServiceUriAsString);
  if (vmServiceUri == null) {
    return api.badRequest(
      'Cannot normalize VM service URI: $vmServiceUriAsString',
    );
  }

  DartToolingDaemon? dartToolingDaemon;
  try {
    dartToolingDaemon = await DartToolingDaemon.connect(dtd!.localUri);

    final detectRootResponse = await detectRootPackageForVmService(
      vmServiceUriAsString: vmServiceUriAsString,
      vmServiceUri: vmServiceUri,
      connected: connected,
      api: api,
      dtd: dartToolingDaemon,
    );
    if (detectRootResponse.success) {
      final rootUri = detectRootResponse.uri;
      if (rootUri == null) {
        return api.success();
      }
      return await updateDtdWorkspaceRoots(
        dartToolingDaemon,
        dtdConnectionInfo: dtd,
        rootFromVmService: rootUri,
        connected: connected,
        api: api,
      );
    } else {
      return api.serverError(detectRootResponse.message);
    }
  } catch (e) {
    return api.serverError('$e');
  } finally {
    await dartToolingDaemon?.close();
  }
}