Bug 1728059 [wpt PR 30223] - Add Subresource Web Bundle tests for non utf-8 encoding...
[gecko.git] / remote / cdp / JSONHandler.jsm
blob15973f94e23d885b91f3f6941de93229762f9623
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 = ["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",
21 });
23 class JSONHandler {
24   constructor(cdp) {
25     this.cdp = cdp;
26     this.routes = {
27       "/json/version": this.getVersion.bind(this),
28       "/json/protocol": this.getProtocol.bind(this),
29       "/json/list": this.getTargetList.bind(this),
30     };
31   }
33   getVersion() {
34     const mainProcessTarget = this.cdp.targetList.getMainProcessTarget();
36     const { userAgent } = Cc[
37       "@mozilla.org/network/protocol;1?name=http"
38     ].getService(Ci.nsIHttpProtocolHandler);
40     return {
41       Browser: `${Services.appinfo.name}/${Services.appinfo.version}`,
42       "Protocol-Version": "1.0",
43       "User-Agent": userAgent,
44       "V8-Version": "1.0",
45       "WebKit-Version": "1.0",
46       webSocketDebuggerUrl: mainProcessTarget.toJSON().webSocketDebuggerUrl,
47     };
48   }
50   getProtocol() {
51     return Protocol.Description;
52   }
54   getTargetList() {
55     return [...this.cdp.targetList];
56   }
58   // nsIHttpRequestHandler
60   handle(request, response) {
61     if (request.method != "GET") {
62       throw HTTP_404;
63     }
65     if (!(request.path in this.routes)) {
66       throw HTTP_404;
67     }
69     try {
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);
76     } catch (e) {
77       new RemoteAgentError(e).notify();
78       throw HTTP_505;
79     }
80   }
82   // XPCOM
84   get QueryInterface() {
85     return ChromeUtils.generateQI(["nsIHttpRequestHandler"]);
86   }