blob: 94f7dd0178b9c8bedb3fc1b91bfd730faceda2a5 [file] [log] [blame] [view]
Danil Somsikov4054d1e2023-04-13 08:22:061# Chrome DevTools Protocol
2
3## Definitions
4### Domain
5A group of CDP methods and events, which can usually be individually enabled or
6disabled and individually supported by different types of targets.
7
8*Source: [Chrome DevTools Walkthrough](https://docs.google.com/document/d/1QG7P18wDIhB_twNbkvEAbgJfNoTkyTRXf-6Nk2TTYPI/edit#)*
9
10### Agent (in blink) or Handler (in browser)
11TLDR: Backend implementation of a domain that syncs with the frontend.
12
13Agents and Handlers are backend classes that implement individual protocol
14domains. Some domains are implemented as agents in the renderer process (either
15in Blink or v8; no agents are currently present in the content/renderer so far)
16and some as handlers in the browser process (implemented either by the content
17layer or by the embedder). A domain may be implemented by multiple agents or
18handlers spanning several layers and processes, e.g. have instances in
19`chrome/`, `content/` and `blink/renderer/` . A single command may be handled by
20 multiple layers in the following sequence:
21 embedder, content browser, blink, V8. An agent may indicate that it has
22 completed handling the command or let the command fall-through to the
23 next layer.
24
25
26*Source:
27 - [Chrome DevTools Contribution Guide](https://docs.google.com/document/d/1WNF-KqRSzPLUUfZqQG5AFeU_Ll8TfWYcJasa_XGf7ro/edit#)
28 - [third_party/blink/public/devtools_protocol - chromium/src - Git at Google](https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/public/devtools_protocol/)
29
30
31## Background
32
33### browser_protocol.pdl
34
35The `browser_protocol.pdl` file is the source of truth that is used to generate
36the `browser_protocol.json` file and the `.ts`, `.h` and `.cc` files.
37
38However, there are multiple `browser_protocol.pdl` files across the codebase.
39The main one is located at (in your chromium repo):
40
41`third_party/blink/public/devtools_protocol/browser_protocol.pdl`
42
43This is the one you should modify and the other ones will be synced using
44different commands explained later.
45
46Theres also a separate `js_protocol.pdl` that has only V8-specific domains.
47
48## Create a new Protocol
49
50### 1- Create your domain on the Chromium repository
51
52#### 1.1- Add your domain to the main browser_protocol.pdl file in your
53
54chromium repository To create a new domain, you should add your new domain to
55`third_party/blink/public/devtools_protocol/browser_protocol.pdl`
56
57#### 1.2- Modify headless/BUILD.gn
58Open the file `headless/BUILD.gn` in your chromium repository and the name of
59your new domain under the devtools_domain section.
60
61#### 1.3- Modify third_party/blink/renderer/core/inspector/BUILD.gn
62Open the file `third_party/blink/renderer/core/inspector/BUILD.gn` in your
63chromium repository and add the new protocol files to be generated under the
64outputs section. The convention is to use the domain name converting it from
65camel case to snake case.
66
67The protocol files generated will list the methods that your Agent will need to
68implement to enable the communication between the Chrome DevTools and Chromium.
69
70#### 1.4- Modify inspector_protocol_config.json
71
72Open the file
73`third_party/blink/renderer/core/inspector/inspector_protocol_config.json` and
74under `protocol.options`, add your new domain:
75
76```
77{
78 "domain": "YourNewDomain"
79}
80```
81
82#### 1.5- Build chromium
83Build chromium as you normally would.
84
85#### 1.6- See the newly generated protocol C++ files
86Your new domain files should be generated in this folder:
87
88```
Etienne Noel0d4ffb32023-06-16 18:31:5189src/out/Default/gen/third_party/blink/renderer/core/inspector/protocol/your_new_domain.cc
90src/out/Default/gen/third_party/blink/renderer/core/inspector/protocol/your_new_domain.h
Danil Somsikov4054d1e2023-04-13 08:22:0691```
92
Etienne Noel0d4ffb32023-06-16 18:31:5193#### 1.7- Add the agent to the devtools-frontend/front_end/core/protocol_client/InspectorBackend.ts
94
95Open the file `devtools-frontend/front_end/core/protocol_client/InspectorBackend.ts` and add a new method to expose your Agent.
96
97```
98youNewDomainAgent(): ProtocolProxyApi.YourNewAgentApi {
99 return this.getAgent('YourNewAgent');
100}
101```
102
103#### 1.8- Add the newly generated protocol files to the BUILD.gn
104
105In your chromium repository, open the file
106`third_party/blink/renderer/core/inspector/BUILD.gn` and add the newly generated
107protocol files. This will make them available to be used later in the Agent
108class.
109
110```
111outputs = [
112...
113"inspector/protocol/your_new_domain.cc",
114"inspector/protocol/your_new_domain.h",
115```
Danil Somsikov4054d1e2023-04-13 08:22:06116
117### 2- Sync the previous modifications with the Chrome DevTools repository
118#### 2.1- Sync the browser_protocol files
119As mentioned before, there are many browser_protocol files. To synchronize the
120modifications, run this command from your Chrome DevTools repository:
121
122```
123scripts/deps/roll_deps.py ~/chromium/src .
124```
125
126The first parameter is the path from your Chrome DevTools repository to the
127chromium repository. The second parameter is the root of the
128Chrome DevTools repository.
129
130#### 2.2- Generate the protocol resources
131Other resources must be generated (ts files for example) using this command
132(in your Chrome DevTools repository):
133
134```
135npm run generate-protocol-resources
136```
137
138### 3- Create the Agent on the Chromium repository
139
140#### 3.1- Create the Agent class
141The Agent class should inherit the newly created protocol
142`protocol::YourNewDomainAgent::Metainfo`
143
144```
145class MODULES_EXPORT YourNewDomainAgent final : public InspectorBaseAgent<protocol::YourNewDomainAgent::Metainfo> {
146```
147
148#### 3.2- Register the Agent
149The Agent must be registered with the Session. To do so, go to the file
150`third_party/blink/renderer/modules/modules_initializer.cc` and append it to the
151 session in this method:
152
153```
154void ModulesInitializer::InitInspectorAgentSession {
155
156session->CreateAndAppend<YourNewAgent>...
157```
158
159### 4- Setup the communication from the Chrome DevTools repository
160In the TypeScript class, your agent can now be accessible directly from the
161target. With the target, you can simply access your agent like this:
162
163```
164initialize(target: SDK.Target.Target) {
165 target.yourNewDomainAgent();
166}
167```