1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const lazy = {};
ChromeUtils.defineESModuleGetters(lazy, {
JSONHandler: "chrome://remote/content/cdp/JSONHandler.sys.mjs",
Log: "chrome://remote/content/shared/Log.sys.mjs",
RecommendedPreferences:
"chrome://remote/content/shared/RecommendedPreferences.sys.mjs",
TargetList: "chrome://remote/content/cdp/targets/TargetList.sys.mjs",
});
ChromeUtils.defineLazyGetter(lazy, "logger", () =>
lazy.Log.get(lazy.Log.TYPES.CDP)
);
ChromeUtils.defineLazyGetter(lazy, "textEncoder", () => new TextEncoder());
// Map of CDP-specific preferences that should be set via
// RecommendedPreferences.
const RECOMMENDED_PREFS = new Map([
// Prevent various error message on the console
// jest-puppeteer asserts that no error message is emitted by the console
[
"browser.contentblocking.features.standard",
"-tp,tpPrivate,cookieBehavior0,-cm,-fp",
],
// Accept all cookies (see behavior definitions in nsICookieService.idl)
["network.cookie.cookieBehavior", 0],
]);
/**
* Entry class for the Chrome DevTools Protocol support.
*
* It holds the list of available targets (tabs, main browser), and also
* sets up the necessary handlers for the HTTP server.
*
* @see https://chromedevtools.github.io/devtools-protocol
*/
export class CDP {
/**
* Creates a new instance of the CDP class.
*
* @param {RemoteAgent} agent
* Reference to the Remote Agent instance.
*/
constructor(agent) {
this.agent = agent;
this.targetList = null;
this._running = false;
this._activePortPath;
}
get address() {
const mainTarget = this.targetList.getMainProcessTarget();
return mainTarget.wsDebuggerURL;
}
get mainTargetPath() {
const mainTarget = this.targetList.getMainProcessTarget();
return mainTarget.path;
}
/**
* Starts the CDP support.
*/
async start() {
if (this._running) {
return;
}
// Note: Ideally this would only be set at the end of the method. However
// since start() is async, we prefer to set the flag early in order to
// avoid potential race conditions.
this._running = true;
lazy.RecommendedPreferences.applyPreferences(RECOMMENDED_PREFS);
// Starting CDP too early can cause issues with clients in not being able
// to find any available target. Also when closing the application while
// it's still starting up can cause shutdown hangs. As such CDP will be
// started when the initial application window has finished initializing.
lazy.logger.debug(`Waiting for initial application window`);
await this.agent.browserStartupFinished;
this.agent.server.registerPrefixHandler("/", new lazy.JSONHandler(this));
this.targetList = new lazy.TargetList();
this.targetList.on("target-created", (eventName, target) => {
this.agent.server.registerPathHandler(target.path, target);
});
this.targetList.on("target-destroyed", (eventName, target) => {
this.agent.server.registerPathHandler(target.path, null);
});
await this.targetList.watchForTargets();
Cu.printStderr(`DevTools listening on ${this.address}\n`);
// Write connection details to DevToolsActivePort file within the profile.
this._activePortPath = PathUtils.join(
PathUtils.profileDir,
"DevToolsActivePort"
);
const data = `${this.agent.port}\n${this.mainTargetPath}`;
try {
await IOUtils.write(this._activePortPath, lazy.textEncoder.encode(data));
} catch (e) {
lazy.logger.warn(
`Failed to create ${this._activePortPath} (${e.message})`
);
}
}
/**
* Stops the CDP support.
*/
async stop() {
if (!this._running) {
return;
}
try {
await IOUtils.remove(this._activePortPath);
} catch (e) {
lazy.logger.warn(
`Failed to remove ${this._activePortPath} (${e.message})`
);
}
try {
this.targetList?.destructor();
this.targetList = null;
lazy.RecommendedPreferences.restorePreferences(RECOMMENDED_PREFS);
} catch (e) {
lazy.logger.error("Failed to stop protocol", e);
} finally {
this._running = false;
}
}
}
|