@@ -39,46 +39,35 @@ const showClass = 'show';
3939const warningBannerId = 'warningBanner' ;
4040const warningMsgId = 'warningMsg' ;
4141
42+ int get _tabId => chrome.devtools.inspectedWindow.tabId;
43+
4244void main () {
4345 _registerListeners ();
4446 _setColorThemeToMatchChromeDevTools ();
4547 _maybeUpdateFileABugLink ();
4648}
4749
4850void _registerListeners () {
49- chrome.storage.onChanged.addListener (allowInterop (_handleDebugInfoChanges ));
51+ chrome.storage.onChanged.addListener (allowInterop (_handleStorageChanges ));
5052 chrome.runtime.onMessage.addListener (allowInterop (_handleRuntimeMessages));
5153 final launchDebugConnectionButton =
5254 document.getElementById (launchDebugConnectionButtonId) as ButtonElement ;
5355 launchDebugConnectionButton.addEventListener ('click' , _launchDebugConnection);
56+
57+ _maybeInjectDevToolsIframe ();
5458}
5559
5660void _handleRuntimeMessages (
5761 dynamic jsRequest, MessageSender sender, Function sendResponse) async {
5862 if (jsRequest is ! String ) return ;
59- final tabId = chrome.devtools.inspectedWindow.tabId;
60- interceptMessage <DevToolsUrl >(
61- message: jsRequest,
62- expectedType: MessageType .devToolsUrl,
63- expectedSender: Script .background,
64- expectedRecipient: Script .debuggerPanel,
65- messageHandler: (DevToolsUrl devToolsUrl) async {
66- if (devToolsUrl.tabId != tabId) {
67- debugWarn (
68- 'Received DevTools URL, but Dart app tab does not match current tab.' );
69- return ;
70- }
71- connecting = false ;
72- _injectDevToolsIframe (devToolsUrl.url);
73- });
7463
7564 interceptMessage <DebugStateChange >(
7665 message: jsRequest,
7766 expectedType: MessageType .debugStateChange,
7867 expectedSender: Script .background,
7968 expectedRecipient: Script .debuggerPanel,
8069 messageHandler: (DebugStateChange debugStateChange) async {
81- if (debugStateChange.tabId != tabId ) {
70+ if (debugStateChange.tabId != _tabId ) {
8271 debugWarn (
8372 'Received debug state change request, but Dart app tab does not match current tab.' );
8473 return ;
@@ -95,8 +84,8 @@ void _handleRuntimeMessages(
9584 expectedRecipient: Script .debuggerPanel,
9685 messageHandler: (ConnectFailure connectFailure) async {
9786 debugLog (
98- 'Received connect failure for ${connectFailure .tabId } vs $tabId ' );
99- if (connectFailure.tabId != tabId ) {
87+ 'Received connect failure for ${connectFailure .tabId } vs $_tabId ' );
88+ if (connectFailure.tabId != _tabId ) {
10089 return ;
10190 }
10291 connecting = false ;
@@ -106,12 +95,25 @@ void _handleRuntimeMessages(
10695 });
10796}
10897
109- void _handleDebugInfoChanges (Object _, String storageArea) async {
98+ void _handleStorageChanges (Object storageObj, String storageArea) {
99+ // We only care about session storage objects:
110100 if (storageArea != 'session' ) return ;
111- final debugInfo = await fetchStorageObject <DebugInfo >(
112- type: StorageObject .debugInfo,
113- tabId: chrome.devtools.inspectedWindow.tabId,
101+
102+ interceptStorageChange <DebugInfo >(
103+ storageObj: storageObj,
104+ expectedType: StorageObject .debugInfo,
105+ tabId: _tabId,
106+ changeHandler: _handleDebugInfoChanges,
107+ );
108+ interceptStorageChange <String >(
109+ storageObj: storageObj,
110+ expectedType: StorageObject .devToolsUri,
111+ tabId: _tabId,
112+ changeHandler: _handleDevToolsUriChanges,
114113 );
114+ }
115+
116+ void _handleDebugInfoChanges (DebugInfo ? debugInfo) async {
115117 if (debugInfo == null && isDartApp) {
116118 isDartApp = false ;
117119 _showWarningBanner ('Dart app is no longer open.' );
@@ -122,10 +124,16 @@ void _handleDebugInfoChanges(Object _, String storageArea) async {
122124 }
123125}
124126
127+ void _handleDevToolsUriChanges (String ? devToolsUri) async {
128+ if (devToolsUri != null ) {
129+ _injectDevToolsIframe (devToolsUri);
130+ }
131+ }
132+
125133void _maybeUpdateFileABugLink () async {
126134 final debugInfo = await fetchStorageObject <DebugInfo >(
127135 type: StorageObject .debugInfo,
128- tabId: chrome.devtools.inspectedWindow.tabId ,
136+ tabId: _tabId ,
129137 );
130138 final isInternal = debugInfo? .isInternalBuild ?? false ;
131139 if (isInternal) {
@@ -204,9 +212,8 @@ void _hideWarningBanner() {
204212void _launchDebugConnection (Event _) async {
205213 _updateElementVisibility (launchDebugConnectionButtonId, visible: false );
206214 _updateElementVisibility (loadingSpinnerId, visible: true );
207- final dartAppTabId = chrome.devtools.inspectedWindow.tabId;
208215 final json = jsonEncode (serializers.serialize (DebugStateChange ((b) => b
209- ..tabId = dartAppTabId
216+ ..tabId = _tabId
210217 ..newState = DebugStateChange .startDebugging)));
211218 sendRuntimeMessage (
212219 type: MessageType .debugStateChange,
@@ -224,15 +231,24 @@ void _maybeHandleConnectionTimeout() async {
224231 }
225232}
226233
227- void _injectDevToolsIframe (String devToolsUrl) {
234+ void _maybeInjectDevToolsIframe () async {
235+ final devToolsUri = await fetchStorageObject <String >(
236+ type: StorageObject .devToolsUri, tabId: _tabId);
237+ if (devToolsUri != null ) {
238+ _injectDevToolsIframe (devToolsUri);
239+ }
240+ }
241+
242+ void _injectDevToolsIframe (String devToolsUri) {
243+ connecting = false ;
228244 final iframeContainer = document.getElementById (iframeContainerId);
229245 if (iframeContainer == null ) return ;
230246 final panelBody = document.getElementById (panelBodyId);
231247 final panelType = panelBody? .getAttribute (panelAttribute) ?? 'debugger' ;
232248 final iframe = document.createElement ('iframe' );
233249 iframe.setAttribute (
234250 'src' ,
235- '$devToolsUrl &embed=true&page=$panelType &backgroundColor=$devToolsBackgroundColor ' ,
251+ '$devToolsUri &embed=true&page=$panelType &backgroundColor=$devToolsBackgroundColor ' ,
236252 );
237253 _hideWarningBanner ();
238254 _updateElementVisibility (landingPageId, visible: false );
0 commit comments