Skip to content

Commit

Permalink
Adding api to get dtd uri from devtools server (#7054)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderDake committed Jan 18, 2024
1 parent ea6e7aa commit 9d0530f
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 9 deletions.
26 changes: 21 additions & 5 deletions packages/devtools_app/lib/initialization.dart
Expand Up @@ -6,6 +6,7 @@ import 'package:devtools_app_shared/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_web_plugins/url_strategy.dart';
import 'package:logging/logging.dart';

import 'src/app.dart';
import 'src/framework/app_error_handling.dart';
Expand All @@ -19,6 +20,9 @@ import 'src/shared/globals.dart';
import 'src/shared/preferences.dart';
import 'src/shared/primitives/url_utils.dart';
import 'src/shared/primitives/utils.dart';
import 'src/shared/server/server.dart' as server;

final _log = Logger('initializtion');

/// Handles necessary initialization then runs DevTools.
///
Expand Down Expand Up @@ -76,7 +80,6 @@ Future<void> initializeDevTools({
// Initialize the framework before we do anything else, otherwise the
// StorageController won't be initialized and preferences won't be loaded.
await initializeFramework();

await _initDTDConnection();

final preferences = PreferencesController();
Expand All @@ -103,10 +106,23 @@ void _maybeInitForIntegrationTestMode({
}

Future<void> _initDTDConnection() async {
final queryParams = loadQueryParams();
final dtdUri = queryParams['dtdUri'];
if (dtdUri != null) {
await dtdManager.connect(Uri.parse(dtdUri));
try {
// Get the dtdUri from the devtools server
final dtdUri = await server.getDtdUri();

if (dtdUri != null) {
await dtdManager.connect(dtdUri);
} else {
_log.info('No DTD uri provided from the server during initialization.');
}
} catch (e, st) {
// Dtd failing to connect does not interfere with devtools starting up so
// catch any errors and report them.
reportError(
e,
errorType: 'Failed to initialize the DTD connection.',
stack: st,
);
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/devtools_app/lib/src/service/dtd_manager.dart
Expand Up @@ -4,10 +4,13 @@

import 'package:dtd/dtd.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';

import '../framework/app_error_handling.dart';
import '../shared/globals.dart';

final _log = Logger('dtd_manager');

/// Manages a connection to the Dart Tooling Daemon.
class DTDManager {
ValueListenable<DTDConnection?> get connection => _connection;
Expand All @@ -23,6 +26,7 @@ class DTDManager {

try {
_connection.value = await DartToolingDaemon.connect(uri);
_log.info('Successfully connected to DTD at: $uri');
} catch (e, st) {
notificationService.pushError(
'Failed to connect to the Dart Tooling Daemon',
Expand Down
21 changes: 21 additions & 0 deletions packages/devtools_app/lib/src/shared/server/_dtd_api.dart
@@ -0,0 +1,21 @@
// Copyright 2024 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file.

part of 'server.dart';

/// Asks the Devtools Server to return a Dart Tooling Daemon uri if it has one.
Future<Uri?> getDtdUri() async {
if (isDevToolsServerAvailable) {
final uri = Uri(path: DtdApi.apiGetDtdUri);
final resp = await request(uri.toString());
if (resp?.statusOk ?? false) {
final parsedResult = json.decode(resp!.body);

final uriString = parsedResult[DtdApi.uriPropertyName];

return Uri.parse(uriString);
}
}
return null;
}
1 change: 1 addition & 0 deletions packages/devtools_app/lib/src/shared/server/server.dart
Expand Up @@ -22,6 +22,7 @@ part '_deep_links_api.dart';
part '_extensions_api.dart';
part '_release_notes_api.dart';
part '_survey_api.dart';
part '_dtd_api.dart';

final _log = Logger('_server_web');

Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app/release_notes/NEXT_RELEASE_NOTES.md
Expand Up @@ -15,6 +15,7 @@ significantly improves the user experience when using DevTools embedded
an IDE. (#7030)[https://github.com/flutter/devtools/pull/7030]
* Added support for filtering with regular expressions in the Logging, Network, and CPU profiler
pages - (#7027)[https://github.com/flutter/devtools/pull/7027]
* Add a DevTools server interaction for getting the DTD uri. - (#7054)[https://github.com/flutter/devtools/pull/7054]

## Inspector updates

Expand Down
3 changes: 3 additions & 0 deletions packages/devtools_shared/CHANGELOG.md
@@ -1,3 +1,6 @@
# 6.0.4
* Add `apiGetDtdUri` to the server api.

# 6.0.3
* `CompareMixin` is now generic, implementing `Comparable<T>` instead of
`Comparable<dynamic>`, and it's operators each therefore accept a `T`
Expand Down
5 changes: 5 additions & 0 deletions packages/devtools_shared/lib/src/devtools_api.dart
Expand Up @@ -62,6 +62,11 @@ const baseAppSizeFilePropertyName = 'appSizeBase';

const testAppSizeFilePropertyName = 'appSizeTest';

abstract class DtdApi {
static const apiGetDtdUri = '${apiPrefix}getDtdUri';
static const uriPropertyName = 'dtdUri';
}

abstract class ExtensionsApi {
/// Serves any available extensions and returns a list of their configurations
/// to DevTools.
Expand Down
8 changes: 7 additions & 1 deletion packages/devtools_shared/lib/src/server/server_api.dart
Expand Up @@ -36,6 +36,7 @@ class ServerApi {
required ExtensionsManager extensionsManager,
required DeeplinkManager deeplinkManager,
ServerApi? api,
String? Function()? dtdUri,
}) {
api ??= ServerApi();
final queryParams = request.requestedUri.queryParameters;
Expand Down Expand Up @@ -252,7 +253,12 @@ class ServerApi {
queryParams,
deeplinkManager,
);

case DtdApi.apiGetDtdUri:
return api.setCompleted(
json.encode({
DtdApi.uriPropertyName: dtdUri?.call(),
}),
);
default:
return api.notImplemented();
}
Expand Down
6 changes: 3 additions & 3 deletions packages/devtools_shared/pubspec.yaml
@@ -1,12 +1,12 @@
name: devtools_shared
description: Package of shared Dart structures between devtools_app, dds, and other tools.

version: 6.0.3
version: 6.0.4

repository: https://github.com/flutter/devtools/tree/master/packages/devtools_shared

environment:
sdk: '>=3.3.0-91.0.dev <4.0.0'
sdk: ">=3.3.0-91.0.dev <4.0.0"

dependencies:
args: ^2.4.2
Expand All @@ -17,7 +17,7 @@ dependencies:
shelf: ^1.1.0
sse: ^4.1.2
usage: ^4.0.0
vm_service: '>=13.0.0 <15.0.0'
vm_service: ">=13.0.0 <15.0.0"
web_socket_channel: ^2.4.0
webkit_inspection_protocol: ">=0.5.0 <2.0.0"
yaml: ^3.1.2
Expand Down
25 changes: 25 additions & 0 deletions packages/devtools_shared/test/server/server_api_test.dart
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:convert';
import 'dart:io';

import 'package:devtools_shared/devtools_shared.dart';
Expand Down Expand Up @@ -150,6 +151,30 @@ void main() {
expect(fakeManager.receivedConfiguration, configuration);
expect(fakeManager.receivedTarget, target);
});

test('handle apiGetDtdUri', () async {
const dtdUri = 'ws://dtd:uri';
final request = Request(
'get',
Uri(
scheme: 'https',
host: 'localhost',
path: DtdApi.apiGetDtdUri,
),
);
final fakeManager = FakeDeeplinkManager();
final response = await ServerApi.handle(
request,
extensionsManager: ExtensionsManager(buildDir: '/'),
deeplinkManager: fakeManager,
dtdUri: () => dtdUri,
);
expect(response.statusCode, HttpStatus.ok);
expect(
await response.readAsString(),
jsonEncode({DtdApi.uriPropertyName: dtdUri}),
);
});
}

class FakeDeeplinkManager extends DeeplinkManager {
Expand Down

0 comments on commit 9d0530f

Please sign in to comment.