Skip to content

Commit

Permalink
Move ResolvedUriManager from devtools_app to devtools_app_shared (
Browse files Browse the repository at this point in the history
#6738)

* Move `ResolvedUriManager` from `devtools_app` to `devtools_app_shared`

* fix test
  • Loading branch information
kenzieschmoll authored Nov 15, 2023
1 parent e5d14fa commit 9ae5819
Show file tree
Hide file tree
Showing 11 changed files with 65 additions and 98 deletions.
1 change: 0 additions & 1 deletion packages/devtools_app/lib/devtools_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export 'src/screens/vm_developer/vm_developer_tools_controller.dart';
export 'src/screens/vm_developer/vm_developer_tools_screen.dart';
export 'src/screens/vm_developer/vm_service_private_extensions.dart';
export 'src/service/json_to_service_cache.dart';
export 'src/service/resolved_uri_manager.dart';
export 'src/service/service_extensions.dart';
export 'src/service/service_manager.dart';
export 'src/service/service_registrations.dart';
Expand Down
8 changes: 5 additions & 3 deletions packages/devtools_app/lib/src/screens/debugger/codeview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1498,16 +1498,18 @@ Future<String?> fetchScriptLocationFullFilePath(
if (packagePath != null) {
final isolateId = serviceConnection
.serviceManager.isolateManager.selectedIsolate.value!.id!;
filePath = serviceConnection.resolvedUriManager.lookupFileUri(
filePath =
serviceConnection.serviceManager.resolvedUriManager.lookupFileUri(
isolateId,
packagePath,
);
if (filePath == null) {
await serviceConnection.resolvedUriManager.fetchFileUris(
await serviceConnection.serviceManager.resolvedUriManager.fetchFileUris(
isolateId,
[packagePath],
);
filePath = serviceConnection.resolvedUriManager.lookupFileUri(
filePath =
serviceConnection.serviceManager.resolvedUriManager.lookupFileUri(
isolateId,
packagePath,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ class CpuProfileData {
final resolvedUrl =
stackFrameJson[CpuProfileData.resolvedUrlKey] as String?;
if (resolvedUrl != null && resolvedUrl.isNotEmpty) {
final packageUri = serviceConnection.resolvedUriManager
final packageUri = serviceConnection.serviceManager.resolvedUriManager
.lookupPackageUri(isolateId, resolvedUrl);
if (packageUri != null) {
stackFrameJson[CpuProfileData.resolvedPackageUriKey] = packageUri;
Expand All @@ -624,14 +624,14 @@ class CpuProfileData {
}
}

await serviceConnection.resolvedUriManager.fetchPackageUris(
await serviceConnection.serviceManager.resolvedUriManager.fetchPackageUris(
isolateId,
urisWithoutPackageUri.toList(),
);

for (var stackFrameJson in stackFramesWaitingOnPackageUri) {
final resolvedUri = stackFrameJson[CpuProfileData.resolvedUrlKey];
final packageUri = serviceConnection.resolvedUriManager
final packageUri = serviceConnection.serviceManager.resolvedUriManager
.lookupPackageUri(isolateId, resolvedUri);
if (packageUri != null) {
stackFrameJson[CpuProfileData.resolvedPackageUriKey] = packageUri;
Expand Down
10 changes: 3 additions & 7 deletions packages/devtools_app/lib/src/service/service_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import '../shared/feature_flags.dart';
import '../shared/globals.dart';
import '../shared/title.dart';
import '../shared/utils.dart';
import 'resolved_uri_manager.dart';
import 'service_registrations.dart' as registrations;
import 'timeline_streams.dart';
import 'vm_flags.dart';
Expand Down Expand Up @@ -75,8 +74,6 @@ class ServiceConnectionManager {

final consoleService = ConsoleService();

final resolvedUriManager = ResolvedUriManager();

InspectorServiceBase? get inspectorService => _inspectorService;
InspectorServiceBase? _inspectorService;

Expand All @@ -92,7 +89,6 @@ class ServiceConnectionManager {

Future<void> _beforeOpenVmService(VmServiceWrapper? service) async {
consoleService.vmServiceOpened(service!);
resolvedUriManager.vmServiceOpened();
await vmFlagManager.vmServiceOpened(service);
timelineStreamManager.vmServiceOpened(
service,
Expand Down Expand Up @@ -149,7 +145,6 @@ class ServiceConnectionManager {
generateDevToolsTitle();
vmFlagManager.vmServiceClosed();
timelineStreamManager.vmServiceClosed();
resolvedUriManager.vmServiceClosed();
consoleService.handleVmServiceClosed();
_inspectorService?.onIsolateStopped();
_inspectorService?.dispose();
Expand Down Expand Up @@ -185,8 +180,9 @@ class ServiceConnectionManager {
if (rootLib == null) return null;

final selectedIsolateRefId = mainIsolateRef.id!;
await resolvedUriManager.fetchFileUris(selectedIsolateRefId, [rootLib]);
return resolvedUriManager.lookupFileUri(
await serviceManager.resolvedUriManager
.fetchFileUris(selectedIsolateRefId, [rootLib]);
return serviceManager.resolvedUriManager.lookupFileUri(
selectedIsolateRefId,
rootLib,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void main() {
),
);
// Prefetch File Uris
await serviceConnection.resolvedUriManager.fetchFileUris(
await serviceConnection.serviceManager.resolvedUriManager.fetchFileUris(
serviceConnection
.serviceManager.isolateManager.selectedIsolate.value!.id!,
[mockScriptRef.uri!],
Expand Down
99 changes: 29 additions & 70 deletions packages/devtools_app/test/shared/resolved_uri_manager_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@

import 'dart:async';

import 'package:devtools_app/devtools_app.dart';
import 'package:devtools_app_shared/utils.dart';
import 'package:devtools_app_shared/service.dart';
import 'package:devtools_test/devtools_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:vm_service/vm_service.dart';

void main() {
late ResolvedUriManager resolvedUriManager;
final service = createMockVmServiceWrapperWithDefaults();

setGlobal(
ServiceConnectionManager,
FakeServiceConnectionManager(service: service),
);
resolvedUriManager = ResolvedUriManager();
late MockVmServiceWrapper service;

const String isolateId = 'anIsolateId';
const uri1 = 'this/is/a/uri1';
const uri2 = 'this/is/a/uri2';
const packageUri1 = 'uri/am/i1';
const packageUri2 = 'uri/am/i2';

setUp(() {
service = createMockVmServiceWrapperWithDefaults();
resolvedUriManager = ResolvedUriManager();
});

group('lifecycle', () {
setUp(() {
when(
unawaited(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, [uri1]),
),
).thenAnswer(
when(unawaited(service.lookupPackageUris(isolateId, [uri1]))).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [packageUri1])),
);
});
Expand All @@ -50,7 +43,7 @@ void main() {
});

test('does nothing after vmServiceClosed', () async {
resolvedUriManager.vmServiceOpened();
resolvedUriManager.vmServiceOpened(service);
resolvedUriManager.vmServiceClosed();

await resolvedUriManager.fetchPackageUris(isolateId, [uri1]);
Expand All @@ -65,7 +58,7 @@ void main() {

group('file to package mappings', () {
setUp(() {
resolvedUriManager.vmServiceOpened();
resolvedUriManager.vmServiceOpened(service);
});

test('lookupPackageUri when uri is unknown', () {
Expand All @@ -75,10 +68,7 @@ void main() {
});

test('lookupPackageUris', () async {
when(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, [uri1, uri2]),
).thenAnswer(
when(service.lookupPackageUris(isolateId, [uri1, uri2])).thenAnswer(
(realInvocation) =>
Future.value(UriList(uris: [packageUri1, packageUri2])),
);
Expand All @@ -96,16 +86,10 @@ void main() {
});

test('remembers already fetched uris', () async {
when(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, [uri1]),
).thenAnswer(
when(service.lookupPackageUris(isolateId, [uri1])).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [packageUri1])),
);
when(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, [uri2]),
).thenAnswer(
when(service.lookupPackageUris(isolateId, [uri2])).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [packageUri2])),
);

Expand All @@ -130,16 +114,10 @@ void main() {
const String isolateId2 = 'anIsolateId2';
const String packageUriFromDifferentIsolate =
'this/is/a/third/packageUri3';
when(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, [uri1]),
).thenAnswer(
when(service.lookupPackageUris(isolateId, [uri1])).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [packageUri1])),
);
when(
serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId2, [uri1]),
).thenAnswer(
when(service.lookupPackageUris(isolateId2, [uri1])).thenAnswer(
(realInvocation) =>
Future.value(UriList(uris: [packageUriFromDifferentIsolate])),
);
Expand All @@ -158,12 +136,7 @@ void main() {
});

test('preserves the reverse package to file mapping', () async {
when(
serviceConnection.serviceManager.service!.lookupPackageUris(
isolateId,
[uri1],
),
).thenAnswer(
when(service.lookupPackageUris(isolateId, [uri1])).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [packageUri1])),
);

Expand All @@ -182,7 +155,7 @@ void main() {

group('package to file mappings', () {
setUp(() {
resolvedUriManager.vmServiceOpened();
resolvedUriManager.vmServiceOpened(service);
});

test('lookupFileUri when package is unknown', () {
Expand All @@ -193,10 +166,8 @@ void main() {

test('lookupFileUri', () async {
when(
serviceConnection.serviceManager.service!.lookupResolvedPackageUris(
isolateId,
[packageUri1, packageUri2],
),
service
.lookupResolvedPackageUris(isolateId, [packageUri1, packageUri2]),
).thenAnswer(
(realInvocation) => Future.value(UriList(uris: [uri1, uri2])),
);
Expand All @@ -215,16 +186,12 @@ void main() {
});

test('remembers already fetched file paths', () async {
when(
serviceConnection.serviceManager.service!
.lookupResolvedPackageUris(isolateId, [packageUri1]),
).thenAnswer(
when(service.lookupResolvedPackageUris(isolateId, [packageUri1]))
.thenAnswer(
(realInvocation) => Future.value(UriList(uris: [uri1])),
);
when(
serviceConnection.serviceManager.service!
.lookupResolvedPackageUris(isolateId, [packageUri2]),
).thenAnswer(
when(service.lookupResolvedPackageUris(isolateId, [packageUri2]))
.thenAnswer(
(realInvocation) => Future.value(UriList(uris: [uri2])),
);

Expand All @@ -249,16 +216,12 @@ void main() {
const String isolateId2 = 'anIsolateId2';
const String fileUriFromDifferentIsolate =
'file:///this/is/a/third/fileUri3';
when(
serviceConnection.serviceManager.service!
.lookupResolvedPackageUris(isolateId, [packageUri1]),
).thenAnswer(
when(service.lookupResolvedPackageUris(isolateId, [packageUri1]))
.thenAnswer(
(realInvocation) => Future.value(UriList(uris: [uri1])),
);
when(
serviceConnection.serviceManager.service!
.lookupResolvedPackageUris(isolateId2, [packageUri1]),
).thenAnswer(
when(service.lookupResolvedPackageUris(isolateId2, [packageUri1]))
.thenAnswer(
(realInvocation) =>
Future.value(UriList(uris: [fileUriFromDifferentIsolate])),
);
Expand All @@ -277,12 +240,8 @@ void main() {
});

test('preserves the reverse file to package mapping', () async {
when(
serviceConnection.serviceManager.service!.lookupResolvedPackageUris(
isolateId,
[packageUri1],
),
).thenAnswer(
when(service.lookupResolvedPackageUris(isolateId, [packageUri1]))
.thenAnswer(
(realInvocation) => Future.value(UriList(uris: [uri1])),
);

Expand Down
2 changes: 2 additions & 0 deletions packages/devtools_app_shared/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## 0.0.8
* Add `ServiceManager.resolvedUriManager` for looking up package and file uris from
a VM service connection.
* Migrate from `dart:html` to `package:web`.

## 0.0.7
Expand Down
1 change: 1 addition & 0 deletions packages/devtools_app_shared/lib/service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export 'src/service/eval_on_dart_library.dart';
export 'src/service/flutter_version.dart';
export 'src/service/isolate_manager.dart' hide TestIsolateManager;
export 'src/service/isolate_state.dart';
export 'src/service/resolved_uri_manager.dart';
export 'src/service/service_extension_manager.dart'
hide TestServiceExtensionManager;
export 'src/service/service_manager.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import '../shared/globals.dart';
import 'package:vm_service/vm_service.dart';

/// Manager for handling package Uri lookup and caching.
class ResolvedUriManager {
final class ResolvedUriManager {
VmService? _service;

_PackagePathMappings? _packagePathMappings;

/// Initializes the [ResolvedUriManager]
void vmServiceOpened() {
void vmServiceOpened(VmService service) {
_service = service;
_packagePathMappings = _PackagePathMappings();
}

/// Cleans up the resources of the [ResolvedUriManager]
void vmServiceClosed() {
_service = null;
_packagePathMappings = null;
}

Expand All @@ -27,9 +31,8 @@ class ResolvedUriManager {
Future<void> fetchPackageUris(String isolateId, List<String> uris) async {
if (uris.isEmpty) return;
if (_packagePathMappings != null) {
final packageUris = (await serviceConnection.serviceManager.service!
.lookupPackageUris(isolateId, uris))
.uris;
final packageUris =
(await _service!.lookupPackageUris(isolateId, uris)).uris;

if (packageUris != null) {
_packagePathMappings!.addMappings(
Expand All @@ -50,9 +53,9 @@ class ResolvedUriManager {
/// [packageUris] List of uris to fetch full file paths for.
Future<void> fetchFileUris(String isolateId, List<String> packageUris) async {
if (_packagePathMappings != null) {
final fileUris = (await serviceConnection.serviceManager.service!
.lookupResolvedPackageUris(isolateId, packageUris))
.uris;
final fileUris =
(await _service!.lookupResolvedPackageUris(isolateId, packageUris))
.uris;

// [_packagePathMappings] could have been set to null during the async gap
// so check that it is non-null again here.
Expand Down
Loading

0 comments on commit 9ae5819

Please sign in to comment.