Skip to content

Commit 6edef57

Browse files
Jayson ChenDevtools-frontend LUCI CQ
authored andcommitted
Make disconnect dialog customizable
The current disconnect dialog is hard coded to websocket disconnect. In order to utilize the disconnect dialog in rehydrated session while supplying customizable message, we need to make the following small refactor so we're able to provide customized message in the dialog. Example: https://imgur.com/a/xTUuNj1 Bug: 337909145 Change-Id: I5d869bc54fb6b46e4dcbfa17d5d9c2a755262d37 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6074656 Reviewed-by: Paul Irish <[email protected]> Commit-Queue: Paul Irish <[email protected]> Reviewed-by: Robert Paveza <[email protected]>
1 parent 3d10ad4 commit 6edef57

File tree

7 files changed

+35
-29
lines changed

7 files changed

+35
-29
lines changed

front_end/core/sdk/Connections.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5+
import * as i18n from '../../core/i18n/i18n.js';
56
import * as Common from '../common/common.js';
67
import * as Host from '../host/host.js';
78
import type * as Platform from '../platform/platform.js';
@@ -10,6 +11,14 @@ import * as Root from '../root/root.js';
1011

1112
import {RehydratingConnection} from './RehydratingConnection.js';
1213

14+
const UIStrings = {
15+
/**
16+
*@description Text on the remote debugging window to indicate the connection is lost
17+
*/
18+
websocketDisconnected: 'WebSocket disconnected',
19+
};
20+
const str_ = i18n.i18n.registerUIStrings('core/sdk/Connections.ts', UIStrings);
21+
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
1322
export class MainConnection implements ProtocolClient.InspectorBackend.Connection {
1423
onMessage: ((arg0: (Object|string)) => void)|null;
1524
#onDisconnect: ((arg0: string) => void)|null;
@@ -80,10 +89,12 @@ export class WebSocketConnection implements ProtocolClient.InspectorBackend.Conn
8089
#socket: WebSocket|null;
8190
onMessage: ((arg0: (Object|string)) => void)|null;
8291
#onDisconnect: ((arg0: string) => void)|null;
83-
#onWebSocketDisconnect: (() => void)|null;
92+
#onWebSocketDisconnect: ((message: Platform.UIString.LocalizedString) => void)|null;
8493
#connected: boolean;
8594
#messages: string[];
86-
constructor(url: Platform.DevToolsPath.UrlString, onWebSocketDisconnect: () => void) {
95+
constructor(
96+
url: Platform.DevToolsPath.UrlString,
97+
onWebSocketDisconnect: (message: Platform.UIString.LocalizedString) => void) {
8798
this.#socket = new WebSocket(url);
8899
this.#socket.onerror = this.onError.bind(this);
89100
this.#socket.onopen = this.onOpen.bind(this);
@@ -111,7 +122,7 @@ export class WebSocketConnection implements ProtocolClient.InspectorBackend.Conn
111122

112123
private onError(): void {
113124
if (this.#onWebSocketDisconnect) {
114-
this.#onWebSocketDisconnect.call(null);
125+
this.#onWebSocketDisconnect.call(null, i18nString(UIStrings.websocketDisconnected));
115126
}
116127
if (this.#onDisconnect) {
117128
// This is called if error occurred while connecting.
@@ -133,7 +144,7 @@ export class WebSocketConnection implements ProtocolClient.InspectorBackend.Conn
133144

134145
private onClose(): void {
135146
if (this.#onWebSocketDisconnect) {
136-
this.#onWebSocketDisconnect.call(null);
147+
this.#onWebSocketDisconnect.call(null, i18nString(UIStrings.websocketDisconnected));
137148
}
138149
if (this.#onDisconnect) {
139150
this.#onDisconnect.call(null, 'websocket closed');
@@ -266,21 +277,23 @@ export class ParallelConnection implements ParallelConnectionInterface {
266277
}
267278

268279
export async function initMainConnection(
269-
createRootTarget: () => Promise<void>, websocketConnectionLost: () => void): Promise<void> {
270-
ProtocolClient.InspectorBackend.Connection.setFactory(createMainConnection.bind(null, websocketConnectionLost));
280+
createRootTarget: () => Promise<void>,
281+
onConnectionLost: (message: Platform.UIString.LocalizedString) => void): Promise<void> {
282+
ProtocolClient.InspectorBackend.Connection.setFactory(createMainConnection.bind(null, onConnectionLost));
271283
await createRootTarget();
272284
Host.InspectorFrontendHost.InspectorFrontendHostInstance.connectionReady();
273285
}
274286

275-
function createMainConnection(websocketConnectionLost: () => void): ProtocolClient.InspectorBackend.Connection {
287+
function createMainConnection(onConnectionLost: (message: Platform.UIString.LocalizedString) => void):
288+
ProtocolClient.InspectorBackend.Connection {
276289
if (Root.Runtime.getPathName().includes('rehydrated_devtools_app')) {
277-
return new RehydratingConnection();
290+
return new RehydratingConnection(onConnectionLost);
278291
}
279292
const wsParam = Root.Runtime.Runtime.queryParam('ws');
280293
const wssParam = Root.Runtime.Runtime.queryParam('wss');
281294
if (wsParam || wssParam) {
282295
const ws = (wsParam ? `ws://${wsParam}` : `wss://${wssParam}`) as Platform.DevToolsPath.UrlString;
283-
return new WebSocketConnection(ws, websocketConnectionLost);
296+
return new WebSocketConnection(ws, onConnectionLost);
284297
}
285298
if (Host.InspectorFrontendHost.InspectorFrontendHostInstance.isHostedMode()) {
286299
return new StubConnection();

front_end/core/sdk/RehydratingConnection.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import * as Common from '../../core/common/common.js';
2727
import type * as Protocol from '../../generated/protocol.js';
2828
import * as i18n from '../i18n/i18n.js';
29-
import {UserVisibleError} from '../platform/platform.js';
29+
import type * as Platform from '../platform/platform.js';
3030
import type * as ProtocolClient from '../protocol_client/protocol_client.js';
3131

3232
import * as EnhancedTraces from './EnhancedTracesParser.js';
@@ -68,19 +68,21 @@ export class RehydratingConnection implements ProtocolClient.InspectorBackend.Co
6868
onMessage: ((arg0: Object) => void)|null = null;
6969
traceEvents: unknown[] = [];
7070
sessions: Map<number, RehydratingSessionBase> = new Map();
71+
#onConnectionLost: (message: Platform.UIString.LocalizedString) => void;
7172
#rehydratingWindow: Window&typeof globalThis;
7273
#onReceiveHostWindowPayloadBound = this.#onReceiveHostWindowPayload.bind(this);
7374

74-
constructor() {
75+
constructor(onConnectionLost: (message: Platform.UIString.LocalizedString) => void) {
7576
// If we're invoking this class, we're in the rehydrating pop-up window. Rename window for clarity.
77+
this.#onConnectionLost = onConnectionLost;
7678
this.#rehydratingWindow = window;
7779
this.#setupMessagePassing();
7880
}
7981

8082
#setupMessagePassing(): void {
8183
this.#rehydratingWindow.addEventListener('message', this.#onReceiveHostWindowPayloadBound);
8284
if (!this.#rehydratingWindow.opener) {
83-
throw new UserVisibleError.UserVisibleError(i18nString(UIStrings.noHostWindow));
85+
this.#onConnectionLost(i18nString(UIStrings.noHostWindow));
8486
}
8587
this.#rehydratingWindow.opener.postMessage({type: 'REHYDRATING_WINDOW_READY'});
8688
}
@@ -97,7 +99,7 @@ export class RehydratingConnection implements ProtocolClient.InspectorBackend.Co
9799
await this.startHydration(reader.result as string);
98100
};
99101
reader.onerror = (): void => {
100-
throw new UserVisibleError.UserVisibleError(i18nString(UIStrings.errorLoadingLog));
102+
this.#onConnectionLost(i18nString(UIStrings.errorLoadingLog));
101103
};
102104
reader.readAsText(traceFile);
103105
}

front_end/entrypoints/inspector_main/InspectorMain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export class InspectorMainImpl implements Common.Runnable.Runnable {
107107
if (type !== SDK.Target.Type.TAB) {
108108
void target.runtimeAgent().invoke_runIfWaitingForDebugger();
109109
}
110-
}, Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);
110+
}, Components.TargetDetachedDialog.TargetDetachedDialog.connectionLost);
111111

112112
new SourcesPanelIndicator();
113113
new BackendSettingsSync();

front_end/entrypoints/js_app/js_app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export class JsMainImpl implements Common.Runnable.Runnable {
6060
const target = SDK.TargetManager.TargetManager.instance().createTarget(
6161
'main', i18nString(UIStrings.main), SDK.Target.Type.NODE, null);
6262
void target.runtimeAgent().invoke_runIfWaitingForDebugger();
63-
}, Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);
63+
}, Components.TargetDetachedDialog.TargetDetachedDialog.connectionLost);
6464
}
6565
}
6666

front_end/entrypoints/node_app/NodeMain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class NodeMainImpl implements Common.Runnable.Runnable {
4646
const target = SDK.TargetManager.TargetManager.instance().createTarget(
4747
'main', i18nString(UIStrings.main), SDK.Target.Type.BROWSER, null);
4848
target.setInspectedURL('Node.js' as Platform.DevToolsPath.UrlString);
49-
}, Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);
49+
}, Components.TargetDetachedDialog.TargetDetachedDialog.connectionLost);
5050
}
5151
}
5252

front_end/entrypoints/worker_app/WorkerMain.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class WorkerMainImpl implements Common.Runnable.Runnable {
3939
}
4040
SDK.TargetManager.TargetManager.instance().createTarget(
4141
'main', i18nString(UIStrings.main), SDK.Target.Type.ServiceWorker, null);
42-
}, Components.TargetDetachedDialog.TargetDetachedDialog.webSocketConnectionLost);
42+
}, Components.TargetDetachedDialog.TargetDetachedDialog.connectionLost);
4343
new MobileThrottling.NetworkPanelIndicator.NetworkPanelIndicator();
4444
}
4545
}

front_end/ui/legacy/components/utils/TargetDetachedDialog.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,12 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import * as i18n from '../../../../core/i18n/i18n.js';
5+
import type * as Platform from '../../../../core/platform/platform.js';
66
import * as SDK from '../../../../core/sdk/sdk.js';
77
import type * as ProtocolProxyApi from '../../../../generated/protocol-proxy-api.js';
88
import type * as Protocol from '../../../../generated/protocol.js';
99
import * as UI from '../../legacy.js';
1010

11-
const UIStrings = {
12-
/**
13-
*@description Text on the remote debugging window to indicate the connection is lost
14-
*/
15-
websocketDisconnected: 'WebSocket disconnected',
16-
};
17-
const str_ = i18n.i18n.registerUIStrings('ui/legacy/components/utils/TargetDetachedDialog.ts', UIStrings);
18-
const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_);
1911
export class TargetDetachedDialog extends SDK.SDKModel.SDKModel<void> implements ProtocolProxyApi.InspectorDispatcher {
2012
private static hideCrashedDialog: (() => void)|null;
2113
constructor(target: SDK.Target.Target) {
@@ -33,9 +25,8 @@ export class TargetDetachedDialog extends SDK.SDKModel.SDKModel<void> implements
3325
UI.RemoteDebuggingTerminatedScreen.RemoteDebuggingTerminatedScreen.show(reason);
3426
}
3527

36-
static webSocketConnectionLost(): void {
37-
UI.RemoteDebuggingTerminatedScreen.RemoteDebuggingTerminatedScreen.show(
38-
i18nString(UIStrings.websocketDisconnected));
28+
static connectionLost(message: Platform.UIString.LocalizedString): void {
29+
UI.RemoteDebuggingTerminatedScreen.RemoteDebuggingTerminatedScreen.show(message);
3930
}
4031

4132
targetCrashed(): void {

0 commit comments

Comments
 (0)