Skip to content

Handle detecting Dart app when tab changes #1796

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

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 35 additions & 13 deletions dwds/debug_extension_mv3/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ void _registerListeners() {
chrome.runtime.onMessage.addListener(allowInterop(_handleRuntimeMessages));
chrome.tabs.onRemoved
.addListener(allowInterop((tabId, _) => maybeRemoveLifelinePort(tabId)));
// Update the extension icon on tab navigation:
chrome.tabs.onActivated.addListener(allowInterop((ActiveInfo info) {
_updateIcon(info.tabId);
}));
chrome.windows.onFocusChanged.addListener(allowInterop((_) async {
final currentTab = await _getTab();
if (currentTab?.id != null) {
_updateIcon(currentTab!.id);
}
}));

// Detect clicks on the Dart Debug Extension icon.
chrome.action.onClicked.addListener(allowInterop(_startDebugSession));
Expand Down Expand Up @@ -86,27 +96,39 @@ void _handleRuntimeMessages(
expectedSender: Script.detector,
expectedRecipient: Script.background,
messageHandler: (DebugInfo debugInfo) async {
final currentTab = await _getTab();
final currentUrl = currentTab?.url ?? '';
final appUrl = debugInfo.appUrl ?? '';
if (currentTab == null ||
currentUrl.isEmpty ||
appUrl.isEmpty ||
currentUrl != appUrl) {
console.warn(
'Dart app detected at $appUrl but current tab is $currentUrl.');
final dartTab = sender.tab;
if (dartTab == null) {
console.warn('Received debug info but tab is missing.');
return;
}
// Save the debug info for the Dart app in storage:
await setStorageObject<DebugInfo>(
type: StorageObject.debugInfo,
value: debugInfo,
tabId: currentTab.id);
type: StorageObject.debugInfo, value: debugInfo, tabId: dartTab.id);
// Update the icon to show that a Dart app has been detected:
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
final currentTab = await _getTab();
if (currentTab?.id == dartTab.id) {
_setDebuggableIcon();
}
});
}

void _updateIcon(int activeTabId) async {
final debugInfo = await _fetchDebugInfo(activeTabId);
if (debugInfo != null) {
_setDebuggableIcon();
} else {
_setDefaultIcon();
}
}

void _setDebuggableIcon() {
chrome.action.setIcon(IconInfo(path: 'dart.png'), /*callback*/ null);
}

void _setDefaultIcon() {
chrome.action.setIcon(IconInfo(path: 'dart_grey.png'), /*callback*/ null);
}

Future<DebugInfo?> _fetchDebugInfo(int tabId) {
return fetchStorageObject<DebugInfo>(
type: StorageObject.debugInfo,
Expand Down
22 changes: 22 additions & 0 deletions dwds/debug_extension_mv3/web/chrome_api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,29 @@ class Tabs {

external Object create(TabInfo tabInfo);

external OnActivatedHandler get onActivated;

external OnRemovedHandler get onRemoved;
}

@JS()
@anonymous
class OnActivatedHandler {
external void addListener(void Function(ActiveInfo activeInfo) callback);
}

@JS()
@anonymous
class OnRemovedHandler {
external void addListener(void Function(int tabId, dynamic info) callback);
}

@JS()
@anonymous
class ActiveInfo {
external int get tabId;
}

@JS()
@anonymous
class TabInfo {
Expand Down Expand Up @@ -218,6 +232,14 @@ class Tab {
@anonymous
class Windows {
external Object create(WindowInfo? createData);

external OnFocusChangedHandler get onFocusChanged;
}

@JS()
@anonymous
class OnFocusChangedHandler {
external void addListener(void Function(int windowId) callback);
}

@JS()
Expand Down
Binary file added dwds/debug_extension_mv3/web/dart_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.