Bug 1608587 [wpt PR 21137] - Update wpt metadata, a=testonly
[gecko.git] / remote / JSONHandler.jsm
blob61f47443ba6025c2e2d7ca0c1620e1bf56cc4024
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 { HTTP_404, HTTP_505 } = ChromeUtils.import(
10   "chrome://remote/content/server/HTTPD.jsm"
12 const { Log } = ChromeUtils.import("chrome://remote/content/Log.jsm");
13 const { Protocol } = ChromeUtils.import("chrome://remote/content/Protocol.jsm");
14 const { RemoteAgentError } = ChromeUtils.import(
15   "chrome://remote/content/Error.jsm"
18 class JSONHandler {
19   constructor(agent) {
20     this.agent = agent;
21     this.routes = {
22       "/json/version": this.getVersion.bind(this),
23       "/json/protocol": this.getProtocol.bind(this),
24       "/json/list": this.getTargetList.bind(this),
25     };
26   }
28   getVersion() {
29     const mainProcessTarget = this.agent.targets.getMainProcessTarget();
30     return {
31       Browser: "Firefox",
32       "Protocol-Version": "1.0",
33       "User-Agent": "Mozilla",
34       "V8-Version": "1.0",
35       "WebKit-Version": "1.0",
36       webSocketDebuggerUrl: mainProcessTarget.toJSON().webSocketDebuggerUrl,
37     };
38   }
40   getProtocol() {
41     return Protocol.Description;
42   }
44   getTargetList() {
45     return [...this.agent.targets];
46   }
48   // nsIHttpRequestHandler
50   handle(request, response) {
51     if (request.method != "GET") {
52       throw HTTP_404;
53     }
55     if (!(request.path in this.routes)) {
56       throw HTTP_404;
57     }
59     try {
60       const body = this.routes[request.path]();
61       const payload = JSON.stringify(body, null, Log.verbose ? "\t" : null);
63       response.setStatusLine(request.httpVersion, 200, "OK");
64       response.setHeader("Content-Type", "application/json");
65       response.write(payload);
66     } catch (e) {
67       new RemoteAgentError(e).notify();
68       throw HTTP_505;
69     }
70   }
72   // XPCOM
74   get QueryInterface() {
75     return ChromeUtils.generateQI([Ci.nsIHttpRequestHandler]);
76   }