Skip to content

Commit e7c3435

Browse files
authored
Dart Debug Extension gracefully handles disconnection (#1582)
1 parent 2fcf4e0 commit e7c3435

File tree

5 files changed

+4565
-4525
lines changed

5 files changed

+4565
-4525
lines changed

dwds/debug_extension/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.29
2+
3+
- Notify the debugger and inspector panels when the debug session is disconnected.
4+
15
## 1.28
26

37
- Support Chrome 100 updates to the remote debugging protocol.

dwds/debug_extension/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: extension
22
publish_to: none
3-
version: 1.28.0
3+
version: 1.29.0
44
homepage: https://github.com/dart-lang/webdev
55
description: >-
66
A chrome extension for Dart debugging.

dwds/debug_extension/web/background.dart

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ void main() {
104104

105105
// When a Dart application tab is closed, detach the corresponding debug
106106
// session:
107-
tabsOnRemovedAddListener(allowInterop(_maybeDetachDebugSessionForTab));
107+
tabsOnRemovedAddListener(allowInterop(_removeAndDetachDebugSessionForTab));
108108

109109
// When a debug session is detached, remove the reference to it:
110110
onDetachAddListener(allowInterop((Debuggee source, DetachReason reason) {
111-
_maybeRemoveDebugSessionForTab(source.tabId);
111+
_removeDebugSessionForTab(source.tabId);
112112
}));
113113

114114
// Save the tab ID for the opened DevTools.
@@ -277,14 +277,38 @@ void _maybeAttachDebugSession(
277277
}
278278
}
279279

280-
void _maybeDetachDebugSessionForTab(int tabId, _) {
281-
final removedTabId = _maybeRemoveDebugSessionForTab(tabId);
280+
// Tries to remove the debug session for the specified tab, and detach the
281+
// debugger associated with that debug session.
282+
void _removeAndDetachDebugSessionForTab(int tabId, _) {
283+
final removedTabId = _removeDebugSessionForTab(tabId);
282284

283285
if (removedTabId != -1) {
284286
detach(Debuggee(tabId: removedTabId), allowInterop(() {}));
285287
}
286288
}
287289

290+
// Tries to remove the debug session for the specified tab. If no session is
291+
// found, returns -1. Otherwise returns the tab ID.
292+
int _removeDebugSessionForTab(int tabId) {
293+
var session = _debugSessions.firstWhere(
294+
(session) => session.appTabId == tabId || session.devtoolsTabId == tabId,
295+
orElse: () => null);
296+
if (session != null) {
297+
session.socketClient.close();
298+
_debugSessions.remove(session);
299+
300+
// Notify the Dart DevTools panel that the session has been detached by
301+
// setting the URI to an empty string:
302+
_updateOrCreateDevToolsPanel(session.appId, (panel) {
303+
panel.devToolsUri = '';
304+
});
305+
306+
return session.appTabId;
307+
} else {
308+
return -1;
309+
}
310+
}
311+
288312
void _maybeSaveDevToolsTabId(Tab tab) async {
289313
// Remembers the ID of the DevTools tab.
290314
//
@@ -338,28 +362,6 @@ void _forwardMessageToExternalExtensions(
338362
}
339363
}
340364

341-
// Tries to remove the debug session for the specified tab. If no session is
342-
// found, returns -1. Otherwise returns the tab ID.
343-
int _maybeRemoveDebugSessionForTab(int tabId) {
344-
var session = _debugSessions.firstWhere(
345-
(session) => session.appTabId == tabId || session.devtoolsTabId == tabId,
346-
orElse: () => null);
347-
if (session != null) {
348-
session.socketClient.close();
349-
_debugSessions.remove(session);
350-
351-
// Notify the Dart DevTools panel that the session has been detached by
352-
// setting the URI to an empty string:
353-
_updateOrCreateDevToolsPanel(session.appId, (panel) {
354-
panel.devToolsUri = '';
355-
});
356-
357-
return session.appTabId;
358-
} else {
359-
return -1;
360-
}
361-
}
362-
363365
void _notifyPanelScriptOfChanges(List<DevToolsPanel> panels) {
364366
for (final panel in panels) {
365367
sendSimpleMessage(panel.panelId,
@@ -500,12 +502,12 @@ Future<void> _startSseClient(
500502
}
501503
}, onDone: () {
502504
_tabIdToEncodedUri.remove(currentTab.id);
503-
client.close();
505+
_removeAndDetachDebugSessionForTab(currentTab.id, null);
504506
return;
505507
}, onError: (_) {
506508
_tabIdToEncodedUri.remove(currentTab.id);
507509
alert('Lost app connection.');
508-
client.close();
510+
_removeAndDetachDebugSessionForTab(currentTab.id, null);
509511
}, cancelOnError: true);
510512

511513
client.sink.add(jsonEncode(serializers.serialize(DevToolsRequest((b) => b

0 commit comments

Comments
 (0)