Bug 1132203 - Load JSON viewer only for top level documents; r=bz

--HG--
extra : rebase_source : b58c0faf87ae906f592e068aa2f54cb2acc38152
This commit is contained in:
Jan Odvarko 2015-09-28 13:48:33 +02:00
Родитель eb00d68fbe
Коммит 1a8cc9ec95
5 изменённых файлов: 142 добавлений и 38 удалений

Просмотреть файл

@ -28,15 +28,20 @@ const childProcessMessageManager =
// Must be power of 2. Used to copy the data stream in onStopRequest.
const SEGMENT_SIZE = Math.pow(2, 17);
const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" + JSON_VIEW_MIME_TYPE + "&to=*/*";
const CLASS_ID = "{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}";
// Localization
var jsonViewStrings = Services.strings.createBundle(
"chrome://browser/locale/devtools/jsonview.properties");
/**
* This object detects application/json content type and converts it
* into a JSON Viewer application that allows simple JSON inspection.
* This object detects 'application/vnd.mozilla.json.view' content type
* and converts it into a JSON Viewer application that allows simple
* JSON inspection.
*
* Based on JSON View: https://github.com/bhollis/jsonview/
* Inspired by JSON View: https://github.com/bhollis/jsonview/
*/
var Converter = Class({
extends: Unknown,
@ -274,9 +279,6 @@ var Converter = Class({
});
// Stream converter component definition
const CONTRACT_ID = "@mozilla.org/streamconv;1?from=application/json&to=*/*";
const CLASS_ID = "{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}";
var service = xpcom.Service({
id: components.ID(CLASS_ID),
contract: CONTRACT_ID,
@ -290,7 +292,6 @@ function register() {
xpcom.register(service);
return true;
}
return false;
}
@ -299,7 +300,6 @@ function unregister() {
xpcom.unregister(service);
return true;
}
return false;
}

Просмотреть файл

@ -12,32 +12,31 @@ const Ci = Components.interfaces;
const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
const {devtools} = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
// Load JsonView service module (converter-child.js) as soon as required.
// Load JsonView services lazily.
XPCOMUtils.defineLazyGetter(this, "JsonViewService", function() {
const {devtools} = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
const {JsonViewService} = devtools.require("devtools/client/jsonview/converter-child");
return JsonViewService;
});
const JSON_TYPE = "application/json";
const GECKO_VIEWER = "Gecko-Content-Viewers";
XPCOMUtils.defineLazyGetter(this, "JsonViewSniffer", function() {
const {JsonViewSniffer} = devtools.require("devtools/client/jsonview/converter-sniffer");
return JsonViewSniffer;
});
// Constants
const JSON_VIEW_PREF = "devtools.jsonview.enabled";
const GECKO_TYPE_MAPPING = "ext-to-type-mapping";
/**
* Listen for 'devtools.jsonview.enabled' preference changes and
* register/unregister the JSON View XPCOM service as appropriate.
* register/unregister the JSON View XPCOM services as appropriate.
*/
function ConverterObserver() {
}
ConverterObserver.prototype = {
initialize: function() {
this.geckoViewer = categoryManager.getCategoryEntry(GECKO_VIEWER, JSON_TYPE);
// Only the DevEdition has this feature available by default.
// Users need to manually flip 'devtools.jsonview.enabled' preference
// to have it available in other distributions.
@ -74,29 +73,13 @@ ConverterObserver.prototype = {
},
register: function() {
if (JsonViewService.register()) {
// Delete default JSON viewer (text)
categoryManager.deleteCategoryEntry(GECKO_VIEWER, JSON_TYPE, false);
// Append new *.json -> application/json type mapping
this.geckoMapping = categoryManager.addCategoryEntry(GECKO_TYPE_MAPPING,
"json", JSON_TYPE, false, true);
}
JsonViewSniffer.register();
JsonViewService.register();
},
unregister: function() {
if (JsonViewService.unregister()) {
categoryManager.addCategoryEntry(GECKO_VIEWER, JSON_TYPE,
this.geckoViewer, false, false);
if (this.geckoMapping) {
categoryManager.addCategoryEntry(GECKO_TYPE_MAPPING, "json",
this.geckoMapping, false, true);
} else {
categoryManager.deleteCategoryEntry(GECKO_TYPE_MAPPING,
JSON_TYPE, false)
}
}
JsonViewSniffer.unregister();
JsonViewService.unregister();
},
isEnabled: function() {

Просмотреть файл

@ -0,0 +1,102 @@
/* -*- 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";
const {Cc, Ci, components} = require("chrome");
const xpcom = require("sdk/platform/xpcom");
const {Unknown} = require("sdk/platform/xpcom");
const {Class} = require("sdk/core/heritage");
const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
loader.lazyRequireGetter(this, "NetworkHelper",
"devtools/shared/webconsole/network-helper");
// Constants
const JSON_TYPE = "application/json";
const CONTRACT_ID = "@mozilla.org/devtools/jsonview-sniffer;1";
const CLASS_ID = "{4148c488-dca1-49fc-a621-2a0097a62422}";
const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
const JSON_VIEW_TYPE = "JSON View";
const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
/**
* This component represents a sniffer (implements nsIContentSniffer
* interface) responsible for changing top level 'application/json'
* document types to: 'application/vnd.mozilla.json.view'.
*
* This internal type is consequently rendered by JSON View component
* that represents the JSON through a viewer interface.
*/
var Sniffer = Class({
extends: Unknown,
interfaces: [
"nsIContentSniffer",
],
get wrappedJSObject() this,
getMIMETypeFromContent: function(aRequest, aData, aLength) {
// JSON View is enabled only for top level loads only.
if (!NetworkHelper.isTopLevelLoad(aRequest)) {
return "";
}
if (aRequest instanceof Ci.nsIChannel) {
try {
if (aRequest.contentDisposition == Ci.nsIChannel.DISPOSITION_ATTACHMENT) {
return "";
}
} catch (e) {
// Channel doesn't support content dispositions
}
// Check the response content type and if it's application/json
// change it to new internal type consumed by JSON View.
if (aRequest.contentType == JSON_TYPE) {
return JSON_VIEW_MIME_TYPE;
}
}
return "";
}
});
var service = xpcom.Service({
id: components.ID(CLASS_ID),
contract: CONTRACT_ID,
Component: Sniffer,
register: false,
unregister: false
});
function register() {
if (!xpcom.isRegistered(service)) {
xpcom.register(service);
categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE,
CONTRACT_ID, false, false);
return true;
}
return false;
}
function unregister() {
if (xpcom.isRegistered(service)) {
categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY,
JSON_VIEW_TYPE, false)
xpcom.unregister(service);
return true;
}
return false;
}
exports.JsonViewSniffer = {
register: register,
unregister: unregister
}

Просмотреть файл

@ -13,6 +13,7 @@ DIRS += [
DevToolsModules(
'converter-child.js',
'converter-observer.js',
'converter-sniffer.js',
'json-viewer.js',
'main.js',
'utils.js',

Просмотреть файл

@ -264,6 +264,24 @@ var NetworkHelper = {
return null;
},
/**
* Determines whether the request has been made for the top level document.
*
* @param nsIHttpChannel aRequest
* @returns Boolean True if the request represents the top level document.
*/
isTopLevelLoad: function(aRequest)
{
if (aRequest instanceof Ci.nsIChannel) {
let loadInfo = aRequest.loadInfo;
if (loadInfo && loadInfo.parentOuterWindowID == loadInfo.outerWindowID) {
return (aRequest.loadFlags & Ci.nsIChannel.LOAD_DOCUMENT_URI);
}
}
return false;
},
/**
* Loads the content of aUrl from the cache.
*