Skip to content

Commit 27ba9e0

Browse files
szuendDevtools-frontend LUCI CQ
authored andcommitted
[protocol] Introduce CDPConnection interface
This CL proposes to introduce a new abstraction layer that will allow us to utilize puppeteer connections in DevTools. At the moment we have 2 layers: 1) Raw transport: Sending and receiving strings. Current implementation: ConnectionTransport 2) Message and session handling: Parsing strings into messages, correlating requests with their responses and dispatching them on the right session. Current implementation: SessionRouter The new CDPConnection would be introduced inbetween: It uses an existing transport and would only handle messages. That is, serialization/deserializatrion and matching requests with their responses. A follow-up CL will add a concerete DevTools implementation that moves the existing pieces out of SessionRouter. As part of this refactoring, we also introduce an 'inspectable' version of the connection: The protocol monitor and our test harnesses require raw inspection of the CDP traffic. At the moment, these are global functions ona global `test` object that everyone can install, with the downside they can overwrite each other. The proposed 'RawMessageObserver' can handle both `test.dumpProtocol` as well as `test.onMessageSent` and `test.onMessageReceived`. Note: We use an observer pattern rather then the DevTools event system for performance reasons: CDP is generally rather talkative. Using an `ObjectWrapper` with an `CDP_EVENT_RECEIVED` event would mean we have to do a Map lookup to get the list of listeners for each CDP event. With an observer, we can at least avoid the map lookup. [email protected], [email protected] Bug: 453469270 Change-Id: Ia226c2137274a73752b586e1fc924f6b91413f6a Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/7075672 Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Simon Zünd <[email protected]> Reviewed-by: Philip Pfaffe <[email protected]>
1 parent 28778f6 commit 27ba9e0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

config/gni/devtools_grd_files.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,7 @@ grd_files_unbundled_sources = [
895895
"front_end/core/platform/TypescriptUtilities.js",
896896
"front_end/core/platform/UIString.js",
897897
"front_end/core/platform/UserVisibleError.js",
898+
"front_end/core/protocol_client/CDPConnection.js",
898899
"front_end/core/protocol_client/ConnectionTransport.js",
899900
"front_end/core/protocol_client/InspectorBackend.js",
900901
"front_end/core/protocol_client/NodeURL.js",

front_end/core/protocol_client/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import("../../../scripts/build/typescript/typescript.gni")
88

99
devtools_module("protocol_client") {
1010
sources = [
11+
"CDPConnection.ts",
1112
"ConnectionTransport.ts",
1213
"InspectorBackend.ts",
1314
"NodeURL.ts",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2025 The Chromium Authors
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import type {ProtocolMapping} from '../../generated/protocol-mapping.js';
6+
7+
/**
8+
* Allows the sending and receiving of CDP commands and the notification of CDP events to observers.
9+
*
10+
* An instance of a CDPConnection "owns" the full transport channel and no other CDP traffic must
11+
* be proxied over it. This is because each implementation needs to manage "message IDs", which
12+
* would conflict with any other shared traffic.
13+
*/
14+
export interface CDPConnection {
15+
send<T extends keyof ProtocolMapping.Commands>(
16+
method: T, params: ProtocolMapping.Commands[T]['paramsType'][0],
17+
sessionId: string|undefined): Promise<ProtocolMapping.Commands[T]['returnType']|{getError(): string}>;
18+
19+
observe(observer: CDPConnectionObserver): void;
20+
unobserve(observer: CDPConnectionObserver): void;
21+
}
22+
23+
export interface CDPConnectionObserver {
24+
onEvent<T extends keyof ProtocolMapping.Events>(event: ProtocolMapping.Events[T]): void;
25+
onDisconnect(reason: string): void;
26+
}
27+
28+
/**
29+
* The protocol monitor and test harness require inspection of raw CDP message traffic.
30+
*/
31+
export interface DebuggableCDPConnection extends CDPConnection {
32+
observeMessages(observer: RawMessageObserver): void;
33+
unobserveMessages(observer: RawMessageObserver): void;
34+
}
35+
36+
export interface RawMessageObserver {
37+
onMessageReceived(message: unknown): void;
38+
onMessageSent(message: unknown): void;
39+
}

0 commit comments

Comments
 (0)