Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding api to get dtd uri from devtools server #7054

Merged
merged 8 commits into from Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 17 additions & 4 deletions packages/devtools_app/lib/initialization.dart
Expand Up @@ -19,6 +19,7 @@ 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;

/// Handles necessary initialization then runs DevTools.
///
Expand Down Expand Up @@ -76,6 +77,7 @@ 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();

await _initDTDConnection();

Expand Down Expand Up @@ -103,10 +105,21 @@ 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);
}
} 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
23 changes: 23 additions & 0 deletions packages/devtools_app/lib/src/shared/server/_dtd_api.dart
@@ -0,0 +1,23 @@
// Copyright 2020 The Chromium Authors. All rights reserved.
CoderDake marked this conversation as resolved.
Show resolved Hide resolved
// 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,
CoderDake marked this conversation as resolved.
Show resolved Hide resolved
);
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
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. - ()[]

## 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
9 changes: 8 additions & 1 deletion packages/devtools_shared/lib/src/server/server_api.dart
Expand Up @@ -10,6 +10,7 @@ import 'dart:io';

import 'package:shelf/shelf.dart' as shelf;

import '../../devtools_shared.dart';
CoderDake marked this conversation as resolved.
Show resolved Hide resolved
import '../deeplink/deeplink_manager.dart';
import '../devtools_api.dart';
import '../extensions/extension_enablement.dart';
Expand All @@ -36,6 +37,7 @@ class ServerApi {
required ExtensionsManager extensionsManager,
required DeeplinkManager deeplinkManager,
ServerApi? api,
String? Function()? dtdUri,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this a function instead of a String so that it will always be up to date with the latest value on the server?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are correct :)

}) {
api ??= ServerApi();
final queryParams = request.requestedUri.queryParameters;
Expand Down Expand Up @@ -252,7 +254,12 @@ class ServerApi {
queryParams,
deeplinkManager,
);

case DtdApi.apiGetDtdUri:
return api.setCompleted(
json.encode({
DtdApi.uriPropertyName: dtdUri == null ? null : dtdUri(),
CoderDake marked this conversation as resolved.
Show resolved Hide resolved
}),
);
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