2019-03-08 01:18:45 +03:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["JSONHandler"];
|
|
|
|
|
2019-03-10 15:52:48 +03:00
|
|
|
const {HTTP_404, HTTP_505} = ChromeUtils.import("chrome://remote/content/server/HTTPD.jsm");
|
2019-03-08 01:18:45 +03:00
|
|
|
const {Log} = ChromeUtils.import("chrome://remote/content/Log.jsm");
|
|
|
|
const {Protocol} = ChromeUtils.import("chrome://remote/content/Protocol.jsm");
|
2019-03-10 15:52:48 +03:00
|
|
|
const {RemoteAgentError} = ChromeUtils.import("chrome://remote/content/Error.jsm");
|
2019-03-08 01:18:45 +03:00
|
|
|
|
|
|
|
class JSONHandler {
|
|
|
|
constructor(agent) {
|
|
|
|
this.agent = agent;
|
|
|
|
this.routes = {
|
|
|
|
"/json/version": this.getVersion.bind(this),
|
|
|
|
"/json/protocol": this.getProtocol.bind(this),
|
|
|
|
"/json/list": this.getTargetList.bind(this),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
getVersion() {
|
2019-03-10 15:51:49 +03:00
|
|
|
const mainProcessTarget = this.agent.targets.getMainProcessTarget();
|
|
|
|
return {
|
|
|
|
"Browser": "Firefox",
|
|
|
|
"Protocol-Version": "1.0",
|
|
|
|
"User-Agent": "Mozilla",
|
|
|
|
"V8-Version": "1.0",
|
|
|
|
"WebKit-Version": "1.0",
|
|
|
|
"webSocketDebuggerUrl": mainProcessTarget.toJSON().webSocketDebuggerUrl,
|
|
|
|
};
|
2019-03-08 01:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getProtocol() {
|
|
|
|
return Protocol.Description;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTargetList() {
|
|
|
|
return [...this.agent.targets];
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsIHttpRequestHandler
|
|
|
|
|
|
|
|
handle(request, response) {
|
|
|
|
if (request.method != "GET") {
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(request.path in this.routes)) {
|
|
|
|
throw HTTP_404;
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:52:48 +03:00
|
|
|
try {
|
|
|
|
const body = this.routes[request.path]();
|
|
|
|
const payload = JSON.stringify(body, sanitise, Log.verbose ? "\t" : undefined);
|
2019-03-08 01:18:45 +03:00
|
|
|
|
2019-03-10 15:52:48 +03:00
|
|
|
response.setStatusLine(request.httpVersion, 200, "OK");
|
|
|
|
response.setHeader("Content-Type", "application/json");
|
|
|
|
response.write(payload);
|
|
|
|
} catch (e) {
|
|
|
|
new RemoteAgentError(e).notify();
|
|
|
|
throw HTTP_505;
|
|
|
|
}
|
2019-03-08 01:18:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// XPCOM
|
|
|
|
|
|
|
|
get QueryInterface() {
|
|
|
|
return ChromeUtils.generateQI([Ci.nsIHttpRequestHandler]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filters out null and empty strings
|
|
|
|
function sanitise(key, value) {
|
|
|
|
if (value === null || (typeof value == "string" && value.length == 0)) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|