2015-09-28 14:33:20 +03:00
|
|
|
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
|
|
|
|
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
|
|
|
|
/* 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";
|
|
|
|
|
2018-03-01 22:19:56 +03:00
|
|
|
const {Ci, Cu, CC} = require("chrome");
|
2018-05-09 04:36:22 +03:00
|
|
|
const ChromeUtils = require("ChromeUtils");
|
2016-02-27 15:51:10 +03:00
|
|
|
const Services = require("Services");
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2015-09-28 14:34:03 +03:00
|
|
|
loader.lazyRequireGetter(this, "NetworkHelper",
|
|
|
|
"devtools/shared/webconsole/network-helper");
|
2018-03-13 15:49:39 +03:00
|
|
|
loader.lazyGetter(this, "debugJsModules", function() {
|
2018-06-01 13:36:09 +03:00
|
|
|
const {AppConstants} = require("resource://gre/modules/AppConstants.jsm");
|
2018-03-13 15:49:39 +03:00
|
|
|
return !!(AppConstants.DEBUG_JS_MODULES);
|
2017-06-03 21:58:00 +03:00
|
|
|
});
|
2015-09-28 14:34:03 +03:00
|
|
|
|
2017-09-23 21:10:04 +03:00
|
|
|
const BinaryInput = CC("@mozilla.org/binaryinputstream;1",
|
|
|
|
"nsIBinaryInputStream", "setInputStream");
|
|
|
|
const BufferStream = CC("@mozilla.org/io/arraybuffer-input-stream;1",
|
|
|
|
"nsIArrayBufferInputStream", "setData");
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2018-05-08 14:25:47 +03:00
|
|
|
const kCSP = "default-src 'none' ; script-src resource:; ";
|
|
|
|
|
2015-09-28 14:33:20 +03:00
|
|
|
// Localization
|
2016-12-19 20:00:13 +03:00
|
|
|
loader.lazyGetter(this, "jsonViewStrings", () => {
|
|
|
|
return Services.strings.createBundle(
|
|
|
|
"chrome://devtools/locale/jsonview.properties");
|
|
|
|
});
|
2015-09-28 14:33:20 +03:00
|
|
|
|
|
|
|
/**
|
2015-09-28 14:48:33 +03:00
|
|
|
* This object detects 'application/vnd.mozilla.json.view' content type
|
|
|
|
* and converts it into a JSON Viewer application that allows simple
|
|
|
|
* JSON inspection.
|
2015-09-28 14:33:20 +03:00
|
|
|
*
|
2015-09-28 14:48:33 +03:00
|
|
|
* Inspired by JSON View: https://github.com/bhollis/jsonview/
|
2015-09-28 14:33:20 +03:00
|
|
|
*/
|
2016-12-19 20:00:13 +03:00
|
|
|
function Converter() {}
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2016-12-19 20:00:13 +03:00
|
|
|
Converter.prototype = {
|
2018-05-09 04:36:22 +03:00
|
|
|
QueryInterface: ChromeUtils.generateQI([
|
2016-12-19 20:00:13 +03:00
|
|
|
Ci.nsIStreamConverter,
|
|
|
|
Ci.nsIStreamListener,
|
2018-10-19 15:55:39 +03:00
|
|
|
Ci.nsIRequestObserver,
|
2016-12-19 20:00:13 +03:00
|
|
|
]),
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2015-10-29 20:48:29 +03:00
|
|
|
get wrappedJSObject() {
|
|
|
|
return this;
|
|
|
|
},
|
2015-09-28 14:33:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This component works as such:
|
|
|
|
* 1. asyncConvertData captures the listener
|
2016-03-18 08:26:53 +03:00
|
|
|
* 2. onStartRequest fires, initializes stuff, modifies the listener
|
|
|
|
* to match our output type
|
2017-11-14 03:34:21 +03:00
|
|
|
* 3. onDataAvailable decodes and inserts data into a text node
|
2017-09-23 21:10:04 +03:00
|
|
|
* 4. onStopRequest flushes data and spits back to the listener
|
2016-03-18 08:26:53 +03:00
|
|
|
* 5. convert does nothing, it's just the synchronous version
|
|
|
|
* of asyncConvertData
|
2015-09-28 14:33:20 +03:00
|
|
|
*/
|
2018-03-12 21:24:38 +03:00
|
|
|
convert: function(fromStream, fromType, toType, ctx) {
|
2016-03-18 08:26:53 +03:00
|
|
|
return fromStream;
|
2015-09-28 14:33:20 +03:00
|
|
|
},
|
|
|
|
|
2018-03-12 21:24:38 +03:00
|
|
|
asyncConvertData: function(fromType, toType, listener, ctx) {
|
2016-03-18 08:26:53 +03:00
|
|
|
this.listener = listener;
|
2015-09-28 14:33:20 +03:00
|
|
|
},
|
|
|
|
|
2019-02-28 02:42:27 +03:00
|
|
|
onDataAvailable: function(request, inputStream, offset, count) {
|
2017-11-14 03:34:21 +03:00
|
|
|
// Decode and insert data.
|
2018-06-01 13:36:09 +03:00
|
|
|
const buffer = new ArrayBuffer(count);
|
2017-11-14 03:34:21 +03:00
|
|
|
new BinaryInput(inputStream).readArrayBuffer(count, buffer);
|
2017-11-24 23:43:49 +03:00
|
|
|
this.decodeAndInsertBuffer(buffer);
|
2015-09-28 14:33:20 +03:00
|
|
|
},
|
|
|
|
|
2019-02-28 02:41:54 +03:00
|
|
|
onStartRequest: function(request) {
|
2017-05-31 03:23:00 +03:00
|
|
|
// Set the content type to HTML in order to parse the doctype, styles
|
2017-11-14 03:34:21 +03:00
|
|
|
// and scripts. The JSON will be manually inserted as text.
|
2017-05-31 03:23:00 +03:00
|
|
|
request.QueryInterface(Ci.nsIChannel);
|
|
|
|
request.contentType = "text/html";
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const headers = getHttpHeaders(request);
|
2018-04-27 20:58:25 +03:00
|
|
|
|
2018-03-08 23:46:13 +03:00
|
|
|
// Enforce strict CSP:
|
|
|
|
try {
|
|
|
|
request.QueryInterface(Ci.nsIHttpChannel);
|
2018-05-08 14:25:47 +03:00
|
|
|
request.setResponseHeader("Content-Security-Policy", kCSP, false);
|
|
|
|
request.setResponseHeader("Content-Security-Policy-Report-Only", "", false);
|
2018-03-08 23:46:13 +03:00
|
|
|
} catch (ex) {
|
|
|
|
// If this is not an HTTP channel we can't and won't do anything.
|
|
|
|
}
|
|
|
|
|
2017-09-23 21:10:04 +03:00
|
|
|
// Don't honor the charset parameter and use UTF-8 (see bug 741776).
|
2017-05-31 03:23:00 +03:00
|
|
|
request.contentCharset = "UTF-8";
|
2017-11-24 23:43:49 +03:00
|
|
|
this.decoder = new TextDecoder("UTF-8");
|
2017-05-03 09:09:00 +03:00
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
// Changing the content type breaks saving functionality. Fix it.
|
|
|
|
fixSave(request);
|
2017-05-25 21:04:00 +03:00
|
|
|
|
2017-01-24 01:18:21 +03:00
|
|
|
// Because content might still have a reference to this window,
|
|
|
|
// force setting it to a null principal to avoid it being same-
|
|
|
|
// origin with (other) content.
|
2017-05-25 21:04:00 +03:00
|
|
|
request.loadInfo.resetPrincipalToInheritToNullPrincipal();
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
// Start the request.
|
2019-02-28 02:41:54 +03:00
|
|
|
this.listener.onStartRequest(request);
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
// Initialize stuff.
|
2018-06-01 13:36:09 +03:00
|
|
|
const win = NetworkHelper.getWindowForRequest(request);
|
2018-04-27 20:58:25 +03:00
|
|
|
this.data = exportData(win, headers);
|
2017-11-14 03:34:21 +03:00
|
|
|
insertJsonData(win, this.data.json);
|
|
|
|
win.addEventListener("contentMessage", onContentMessage, false, true);
|
2017-10-15 19:45:41 +03:00
|
|
|
keepThemeUpdated(win);
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2017-09-23 21:10:04 +03:00
|
|
|
// Send the initial HTML code.
|
2018-06-01 13:36:09 +03:00
|
|
|
const buffer = new TextEncoder().encode(initialHTML(win.document)).buffer;
|
|
|
|
const stream = new BufferStream(buffer, 0, buffer.byteLength);
|
2019-02-28 02:42:27 +03:00
|
|
|
this.listener.onDataAvailable(request, stream, 0, stream.available());
|
2017-05-31 03:23:00 +03:00
|
|
|
},
|
|
|
|
|
2019-02-28 02:41:54 +03:00
|
|
|
onStopRequest: function(request, statusCode) {
|
2017-09-23 21:10:04 +03:00
|
|
|
// Flush data.
|
2017-11-24 23:43:49 +03:00
|
|
|
this.decodeAndInsertBuffer(new ArrayBuffer(0), true);
|
2017-09-23 21:10:04 +03:00
|
|
|
|
|
|
|
// Stop the request.
|
2019-02-28 02:41:54 +03:00
|
|
|
this.listener.onStopRequest(request, statusCode);
|
2015-09-28 14:33:20 +03:00
|
|
|
this.listener = null;
|
2017-09-23 21:10:04 +03:00
|
|
|
this.decoder = null;
|
|
|
|
this.data = null;
|
|
|
|
},
|
|
|
|
|
2017-11-14 03:34:21 +03:00
|
|
|
// Decodes an ArrayBuffer into a string and inserts it into the page.
|
2018-03-12 21:24:38 +03:00
|
|
|
decodeAndInsertBuffer: function(buffer, flush = false) {
|
2017-11-14 03:34:21 +03:00
|
|
|
// Decode the buffer into a string.
|
2018-06-01 13:36:09 +03:00
|
|
|
const data = this.decoder.decode(buffer, {stream: !flush});
|
2017-09-23 21:10:04 +03:00
|
|
|
|
2017-11-14 03:34:21 +03:00
|
|
|
// Using `appendData` instead of `textContent +=` is important to avoid
|
|
|
|
// repainting previous data.
|
|
|
|
this.data.json.appendData(data);
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2017-05-25 21:04:00 +03:00
|
|
|
};
|
2017-01-03 13:58:30 +03:00
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
// Lets "save as" save the original JSON, not the viewer.
|
|
|
|
// To save with the proper extension we need the original content type,
|
|
|
|
// which has been replaced by application/vnd.mozilla.json.view
|
|
|
|
function fixSave(request) {
|
2017-10-23 02:11:22 +03:00
|
|
|
let match;
|
2017-05-31 03:23:00 +03:00
|
|
|
if (request instanceof Ci.nsIHttpChannel) {
|
|
|
|
try {
|
2018-06-01 13:36:09 +03:00
|
|
|
const header = request.getResponseHeader("Content-Type");
|
2017-10-23 02:11:22 +03:00
|
|
|
match = header.match(/^(application\/(?:[^;]+\+)?json)(?:;|$)/);
|
2017-05-31 03:23:00 +03:00
|
|
|
} catch (err) {
|
|
|
|
// Handled below
|
|
|
|
}
|
|
|
|
} else {
|
2018-06-01 13:36:09 +03:00
|
|
|
const uri = request.QueryInterface(Ci.nsIChannel).URI.spec;
|
2017-10-23 02:11:22 +03:00
|
|
|
match = uri.match(/^data:(application\/(?:[^;,]+\+)?json)[;,]/);
|
2017-05-31 03:23:00 +03:00
|
|
|
}
|
2017-10-23 02:11:22 +03:00
|
|
|
let originalType;
|
|
|
|
if (match) {
|
|
|
|
originalType = match[1];
|
|
|
|
} else {
|
|
|
|
originalType = "application/json";
|
2017-05-31 03:23:00 +03:00
|
|
|
}
|
|
|
|
request.QueryInterface(Ci.nsIWritablePropertyBag);
|
|
|
|
request.setProperty("contentType", originalType);
|
|
|
|
}
|
|
|
|
|
2018-04-27 20:58:25 +03:00
|
|
|
function getHttpHeaders(request) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const headers = {
|
2018-04-27 20:58:25 +03:00
|
|
|
response: [],
|
2018-10-19 15:55:39 +03:00
|
|
|
request: [],
|
2018-04-27 20:58:25 +03:00
|
|
|
};
|
|
|
|
// The request doesn't have to be always nsIHttpChannel
|
|
|
|
// (e.g. in case of data: URLs)
|
|
|
|
if (request instanceof Ci.nsIHttpChannel) {
|
|
|
|
request.visitResponseHeaders({
|
|
|
|
visitHeader: function(name, value) {
|
|
|
|
headers.response.push({name: name, value: value});
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2018-04-27 20:58:25 +03:00
|
|
|
});
|
|
|
|
request.visitRequestHeaders({
|
|
|
|
visitHeader: function(name, value) {
|
|
|
|
headers.request.push({name: name, value: value});
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2018-04-27 20:58:25 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
// Exports variables that will be accessed by the non-privileged scripts.
|
2018-04-27 20:58:25 +03:00
|
|
|
function exportData(win, headers) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const json = new win.Text();
|
|
|
|
const JSONView = Cu.cloneInto({
|
2018-05-11 17:08:47 +03:00
|
|
|
debugJsModules,
|
|
|
|
headers,
|
|
|
|
json,
|
|
|
|
readyState: "uninitialized",
|
|
|
|
Locale: {
|
|
|
|
$STR: key => {
|
|
|
|
try {
|
|
|
|
return jsonViewStrings.GetStringFromName(key);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
return undefined;
|
|
|
|
}
|
2018-10-19 15:55:39 +03:00
|
|
|
},
|
2018-05-11 17:08:47 +03:00
|
|
|
},
|
|
|
|
}, win, {
|
|
|
|
cloneFunctions: true,
|
|
|
|
wrapReflectors: true,
|
|
|
|
});
|
|
|
|
try {
|
|
|
|
Object.defineProperty(Cu.waiveXrays(win), "JSONView", {
|
|
|
|
value: JSONView,
|
|
|
|
configurable: true,
|
|
|
|
enumerable: true,
|
|
|
|
writable: true,
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
Cu.reportError(error);
|
|
|
|
}
|
|
|
|
return {json};
|
2017-05-31 03:23:00 +03:00
|
|
|
}
|
|
|
|
|
2017-11-14 03:34:21 +03:00
|
|
|
// Builds an HTML string that will be used to load stylesheets and scripts.
|
2017-05-31 03:23:00 +03:00
|
|
|
function initialHTML(doc) {
|
2017-11-14 03:34:21 +03:00
|
|
|
// Creates an element with the specified type, attributes and children.
|
|
|
|
function element(type, attributes = {}, children = []) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const el = doc.createElement(type);
|
|
|
|
for (const [attr, value] of Object.entries(attributes)) {
|
2017-11-14 03:34:21 +03:00
|
|
|
el.setAttribute(attr, value);
|
|
|
|
}
|
|
|
|
el.append(...children);
|
|
|
|
return el;
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:23:00 +03:00
|
|
|
let os;
|
2018-06-01 13:36:09 +03:00
|
|
|
const platform = Services.appinfo.OS;
|
2017-05-31 03:23:00 +03:00
|
|
|
if (platform.startsWith("WINNT")) {
|
|
|
|
os = "win";
|
|
|
|
} else if (platform.startsWith("Darwin")) {
|
|
|
|
os = "mac";
|
|
|
|
} else {
|
|
|
|
os = "linux";
|
|
|
|
}
|
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const baseURI = "resource://devtools-client-jsonview/";
|
2017-05-31 03:23:00 +03:00
|
|
|
|
|
|
|
return "<!DOCTYPE html>\n" +
|
2017-11-14 03:34:21 +03:00
|
|
|
element("html", {
|
2017-05-31 03:23:00 +03:00
|
|
|
"platform": os,
|
2017-06-03 21:58:00 +03:00
|
|
|
"class": "theme-" + Services.prefs.getCharPref("devtools.theme"),
|
2018-10-19 15:55:39 +03:00
|
|
|
"dir": Services.locale.isAppLocaleRTL ? "rtl" : "ltr",
|
2017-11-14 03:34:21 +03:00
|
|
|
}, [
|
|
|
|
element("head", {}, [
|
2018-05-08 14:25:47 +03:00
|
|
|
element("meta", {
|
|
|
|
"http-equiv": "Content-Security-Policy",
|
|
|
|
content: kCSP,
|
|
|
|
}),
|
2017-11-14 03:34:21 +03:00
|
|
|
element("link", {
|
|
|
|
rel: "stylesheet",
|
|
|
|
type: "text/css",
|
|
|
|
href: baseURI + "css/main.css",
|
|
|
|
}),
|
2017-11-28 22:10:54 +03:00
|
|
|
]),
|
2017-11-30 13:19:29 +03:00
|
|
|
element("body", {}, [
|
|
|
|
element("div", {"id": "content"}, [
|
2018-10-19 15:55:39 +03:00
|
|
|
element("div", {"id": "json"}),
|
2017-11-28 18:28:21 +03:00
|
|
|
]),
|
|
|
|
element("script", {
|
|
|
|
src: baseURI + "lib/require.js",
|
|
|
|
"data-main": baseURI + "viewer-config.js",
|
|
|
|
}),
|
|
|
|
]),
|
2017-11-14 03:34:21 +03:00
|
|
|
]).outerHTML;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We insert the received data into a text node, which should be appended into
|
|
|
|
// the #json element so that the JSON is still displayed even if JS is disabled.
|
|
|
|
// However, the HTML parser is not synchronous, so this function uses a mutation
|
|
|
|
// observer to detect the creation of the element. Then the text node is appended.
|
|
|
|
function insertJsonData(win, json) {
|
2018-03-12 21:24:38 +03:00
|
|
|
new win.MutationObserver(function(mutations, observer) {
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const {target, addedNodes} of mutations) {
|
2017-11-14 03:34:21 +03:00
|
|
|
if (target.nodeType == 1 && target.id == "content") {
|
2018-06-01 13:36:09 +03:00
|
|
|
for (const node of addedNodes) {
|
2017-11-14 03:34:21 +03:00
|
|
|
if (node.nodeType == 1 && node.id == "json") {
|
|
|
|
observer.disconnect();
|
|
|
|
node.append(json);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).observe(win.document, {
|
|
|
|
childList: true,
|
|
|
|
subtree: true,
|
|
|
|
});
|
2017-05-31 03:23:00 +03:00
|
|
|
}
|
|
|
|
|
2017-10-15 19:45:41 +03:00
|
|
|
function keepThemeUpdated(win) {
|
2018-06-01 13:36:09 +03:00
|
|
|
const listener = function() {
|
|
|
|
const theme = Services.prefs.getCharPref("devtools.theme");
|
2017-10-15 19:45:41 +03:00
|
|
|
win.document.documentElement.className = "theme-" + theme;
|
|
|
|
};
|
|
|
|
Services.prefs.addObserver("devtools.theme", listener);
|
2018-03-12 21:24:38 +03:00
|
|
|
win.addEventListener("unload", function(event) {
|
2017-10-15 19:45:41 +03:00
|
|
|
Services.prefs.removeObserver("devtools.theme", listener);
|
|
|
|
win = null;
|
|
|
|
}, {once: true});
|
|
|
|
}
|
|
|
|
|
2017-05-25 21:04:00 +03:00
|
|
|
// Chrome <-> Content communication
|
|
|
|
function onContentMessage(e) {
|
|
|
|
// Do not handle events from different documents.
|
2018-06-01 13:36:09 +03:00
|
|
|
const win = this;
|
2017-05-25 21:04:00 +03:00
|
|
|
if (win != e.target) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2018-06-01 13:36:09 +03:00
|
|
|
const value = e.detail.value;
|
2017-05-25 21:04:00 +03:00
|
|
|
switch (e.detail.type) {
|
|
|
|
case "save":
|
2018-03-01 22:19:56 +03:00
|
|
|
Services.cpmm.sendAsyncMessage(
|
2017-09-20 01:01:21 +03:00
|
|
|
"devtools:jsonview:save", value);
|
2017-05-25 21:04:00 +03:00
|
|
|
}
|
|
|
|
}
|
2015-09-28 14:33:20 +03:00
|
|
|
|
2017-03-28 18:45:03 +03:00
|
|
|
function createInstance() {
|
|
|
|
return new Converter();
|
|
|
|
}
|
|
|
|
|
2015-09-28 14:34:03 +03:00
|
|
|
exports.JsonViewService = {
|
2017-03-28 19:13:05 +03:00
|
|
|
createInstance: createInstance,
|
2016-03-18 08:26:53 +03:00
|
|
|
};
|