1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 var EXPORTED_SYMBOLS = ["CDP"];
9 const { XPCOMUtils } = ChromeUtils.import(
10 "resource://gre/modules/XPCOMUtils.jsm"
13 XPCOMUtils.defineLazyModuleGetters(this, {
14 JSONHandler: "chrome://remote/content/cdp/JSONHandler.jsm",
15 RecommendedPreferences:
16 "chrome://remote/content/shared/RecommendedPreferences.jsm",
17 TargetList: "chrome://remote/content/cdp/targets/TargetList.jsm",
20 // Map of CDP-specific preferences that should be set via
21 // RecommendedPreferences.
22 const RECOMMENDED_PREFS = new Map([
23 // Prevent various error message on the console
24 // jest-puppeteer asserts that no error message is emitted by the console
26 "browser.contentblocking.features.standard",
27 "-tp,tpPrivate,cookieBehavior0,-cm,-fp",
29 // Accept all cookies (see behavior definitions in nsICookieService.idl)
30 ["network.cookie.cookieBehavior", 0],
34 * Entry class for the Chrome DevTools Protocol support.
36 * It holds the list of available targets (tabs, main browser), and also
37 * sets up the necessary handlers for the HTTP server.
39 * @see https://chromedevtools.github.io/devtools-protocol
43 * Creates a new instance of the CDP class.
45 * @param {RemoteAgent} agent
46 * Reference to the Remote Agent instance.
50 this.targetList = null;
51 this._running = false;
55 const mainTarget = this.targetList.getMainProcessTarget();
56 return mainTarget.wsDebuggerURL;
60 * Starts the CDP support.
67 // Note: Ideally this would only be set at the end of the method. However
68 // since start() is async, we prefer to set the flag early in order to
69 // avoid potential race conditions.
72 RecommendedPreferences.applyPreferences(RECOMMENDED_PREFS);
74 this.agent.server.registerPrefixHandler("/json/", new JSONHandler(this));
76 this.targetList = new TargetList();
77 this.targetList.on("target-created", (eventName, target) => {
78 this.agent.server.registerPathHandler(target.path, target);
80 this.targetList.on("target-destroyed", (eventName, target) => {
81 this.agent.server.registerPathHandler(target.path, null);
84 await this.targetList.watchForTargets();
86 Cu.printStderr(`DevTools listening on ${this.address}\n`);
90 * Stops the CDP support.
98 this.targetList?.destructor();
99 this.targetList = null;
101 RecommendedPreferences.restorePreferences(RECOMMENDED_PREFS);
103 this._running = false;