From 552bdd8977b60f9d1eecc4c0dc92edbb93c3a6bf Mon Sep 17 00:00:00 2001 From: Elliott Brooks <21270878+elliette@users.noreply.github.com> Date: Wed, 23 Nov 2022 17:56:04 -0800 Subject: [PATCH 1/2] Handle detecting Dart app when tab changes --- dwds/debug_extension_mv3/web/background.dart | 55 ++++++++++++++----- dwds/debug_extension_mv3/web/chrome_api.dart | 22 ++++++++ dwds/debug_extension_mv3/web/dart_grey.png | Bin 0 -> 2814 bytes 3 files changed, 64 insertions(+), 13 deletions(-) create mode 100644 dwds/debug_extension_mv3/web/dart_grey.png diff --git a/dwds/debug_extension_mv3/web/background.dart b/dwds/debug_extension_mv3/web/background.dart index b74913654..d99486159 100644 --- a/dwds/debug_extension_mv3/web/background.dart +++ b/dwds/debug_extension_mv3/web/background.dart @@ -26,6 +26,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)); @@ -50,27 +60,46 @@ 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 = await 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( - 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 _fetchDebugInfo(int tabId) { + return fetchStorageObject( + type: StorageObject.debugInfo, + tabId: tabId, + ); +} + Future _getTab() async { final query = QueryInfo(active: true, currentWindow: true); final tabs = List.from(await promiseToFuture(chrome.tabs.query(query))); diff --git a/dwds/debug_extension_mv3/web/chrome_api.dart b/dwds/debug_extension_mv3/web/chrome_api.dart index de0250499..7b66939c8 100644 --- a/dwds/debug_extension_mv3/web/chrome_api.dart +++ b/dwds/debug_extension_mv3/web/chrome_api.dart @@ -155,15 +155,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 { @@ -196,6 +210,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() diff --git a/dwds/debug_extension_mv3/web/dart_grey.png b/dwds/debug_extension_mv3/web/dart_grey.png new file mode 100644 index 0000000000000000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PQEs+~UO#fDTCU zu-2ua!QiV8OBI}dbpPliQ6Vt2=?nFuVyzas+Z~A32i(-6-Z*_EaR7(6v@Yp|SF48? z^tqYyk5qET+dP51MgcZ$>m?fDUB z%D7YZ)He;MMsV~7P7zc&gCdYM_4{c>V(^RE`B^b*?F?;oCgxaI3-uG!S?Y;IB;U!D z7Xv45$20fCPCvLwsD$08D?@(4*b?60t=kGGs8a*M=9O#PHVF4|OPEO5l}5~;1MRcB zGmwP#{WB;h#dPA_Ux-x3ogF8S-Is{6|iQOIX)$a4Ur}QyyUd4-K6w!=jwXRSTWP@=5XaU>>A(A5Q>#G!70Usl|Yd z7X1%6)L zRyfsiWK9n%E?A2e4X<8CAc&IofnP#&CNPO?w04{O2VBmc5U@o>KD|u zNb6S=1B}W1!SsEK?^97EAnX^)ktBn`;o<1j<(W6H&Z1-ovVbHG0Tn7Z=hY62WQI6I zMe_%0TU%RAzuuov@)ePNJs|iY->{CW3rxcGN#JDuwJP4d-em*Mn7&oC@3`|@3>r$|MTt2y@Y0x~t z(<3oiXFyI{z_4D Date: Wed, 23 Nov 2022 19:29:55 -0800 Subject: [PATCH 2/2] Fix analyzer errors --- dwds/debug_extension_mv3/web/background.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dwds/debug_extension_mv3/web/background.dart b/dwds/debug_extension_mv3/web/background.dart index d99486159..ac602785b 100644 --- a/dwds/debug_extension_mv3/web/background.dart +++ b/dwds/debug_extension_mv3/web/background.dart @@ -60,7 +60,7 @@ void _handleRuntimeMessages( expectedSender: Script.detector, expectedRecipient: Script.background, messageHandler: (DebugInfo debugInfo) async { - final dartTab = await sender.tab; + final dartTab = sender.tab; if (dartTab == null) { console.warn('Received debug info but tab is missing.'); return;