Bug 1728059 [wpt PR 30223] - Add Subresource Web Bundle tests for non utf-8 encoding...
[gecko.git] / remote / cdp / CDP.jsm
blobf06fe60c27846e1137d058fdfdf76b0313ed8404
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/. */
5 "use strict";
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",
18 });
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
25   [
26     "browser.contentblocking.features.standard",
27     "-tp,tpPrivate,cookieBehavior0,-cm,-fp",
28   ],
29   // Accept all cookies (see behavior definitions in nsICookieService.idl)
30   ["network.cookie.cookieBehavior", 0],
31 ]);
33 /**
34  * Entry class for the Chrome DevTools Protocol support.
35  *
36  * It holds the list of available targets (tabs, main browser), and also
37  * sets up the necessary handlers for the HTTP server.
38  *
39  * @see https://chromedevtools.github.io/devtools-protocol
40  */
41 class CDP {
42   /**
43    * Creates a new instance of the CDP class.
44    *
45    * @param {RemoteAgent} agent
46    *     Reference to the Remote Agent instance.
47    */
48   constructor(agent) {
49     this.agent = agent;
50     this.targetList = null;
51     this._running = false;
52   }
54   get address() {
55     const mainTarget = this.targetList.getMainProcessTarget();
56     return mainTarget.wsDebuggerURL;
57   }
59   /**
60    * Starts the CDP support.
61    */
62   async start() {
63     if (this._running) {
64       return;
65     }
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.
70     this._running = true;
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);
79     });
80     this.targetList.on("target-destroyed", (eventName, target) => {
81       this.agent.server.registerPathHandler(target.path, null);
82     });
84     await this.targetList.watchForTargets();
86     Cu.printStderr(`DevTools listening on ${this.address}\n`);
87   }
89   /**
90    * Stops the CDP support.
91    */
92   stop() {
93     if (!this._running) {
94       return;
95     }
97     try {
98       this.targetList?.destructor();
99       this.targetList = null;
101       RecommendedPreferences.restorePreferences(RECOMMENDED_PREFS);
102     } finally {
103       this._running = false;
104     }
105   }