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 = ["JSONHandler"];
9 const { XPCOMUtils } = ChromeUtils.import(
10 "resource://gre/modules/XPCOMUtils.jsm"
13 XPCOMUtils.defineLazyModuleGetters(this, {
14 Services: "resource://gre/modules/Services.jsm",
16 HTTP_404: "chrome://remote/content/server/HTTPD.jsm",
17 HTTP_505: "chrome://remote/content/server/HTTPD.jsm",
18 Log: "chrome://remote/content/shared/Log.jsm",
19 Protocol: "chrome://remote/content/cdp/Protocol.jsm",
20 RemoteAgentError: "chrome://remote/content/cdp/Error.jsm",
27 "/json/version": this.getVersion.bind(this),
28 "/json/protocol": this.getProtocol.bind(this),
29 "/json/list": this.getTargetList.bind(this),
34 const mainProcessTarget = this.cdp.targetList.getMainProcessTarget();
36 const { userAgent } = Cc[
37 "@mozilla.org/network/protocol;1?name=http"
38 ].getService(Ci.nsIHttpProtocolHandler);
41 Browser: `${Services.appinfo.name}/${Services.appinfo.version}`,
42 "Protocol-Version": "1.0",
43 "User-Agent": userAgent,
45 "WebKit-Version": "1.0",
46 webSocketDebuggerUrl: mainProcessTarget.toJSON().webSocketDebuggerUrl,
51 return Protocol.Description;
55 return [...this.cdp.targetList];
58 // nsIHttpRequestHandler
60 handle(request, response) {
61 if (request.method != "GET") {
65 if (!(request.path in this.routes)) {
70 const body = this.routes[request.path]();
71 const payload = JSON.stringify(body, null, Log.verbose ? "\t" : null);
73 response.setStatusLine(request.httpVersion, 200, "OK");
74 response.setHeader("Content-Type", "application/json");
75 response.write(payload);
77 new RemoteAgentError(e).notify();
84 get QueryInterface() {
85 return ChromeUtils.generateQI(["nsIHttpRequestHandler"]);