Bug 1302996 - Remove SDK dependencies for JSONView modules loaded on startup. r=jryans

MozReview-Commit-ID: AClST5BXfK1
This commit is contained in:
Alexandre Poirot 2016-12-19 09:00:13 -08:00
Родитель 5e34fcb962
Коммит 2576f85ec2
2 изменённых файлов: 73 добавлений и 59 удалений

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

@ -6,16 +6,17 @@
"use strict"; "use strict";
const {Cc, Ci, components} = require("chrome"); const {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
const Services = require("Services"); const Services = require("Services");
const {Class} = require("sdk/core/heritage");
const {Unknown} = require("sdk/platform/xpcom");
const xpcom = require("sdk/platform/xpcom");
const Events = require("sdk/dom/events");
const Clipboard = require("sdk/clipboard");
loader.lazyRequireGetter(this, "NetworkHelper", loader.lazyRequireGetter(this, "NetworkHelper",
"devtools/shared/webconsole/network-helper"); "devtools/shared/webconsole/network-helper");
loader.lazyRequireGetter(this, "Events",
"sdk/dom/events");
loader.lazyRequireGetter(this, "Clipboard",
"sdk/clipboard");
loader.lazyRequireGetter(this, "JsonViewUtils", loader.lazyRequireGetter(this, "JsonViewUtils",
"devtools/client/jsonview/utils"); "devtools/client/jsonview/utils");
@ -30,11 +31,14 @@ const SEGMENT_SIZE = Math.pow(2, 17);
const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view"; const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" + const CONTRACT_ID = "@mozilla.org/streamconv;1?from=" +
JSON_VIEW_MIME_TYPE + "&to=*/*"; JSON_VIEW_MIME_TYPE + "&to=*/*";
const CLASS_ID = "{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}"; const CLASS_ID = components.ID("{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}");
const CLASS_DESCRIPTION = "JSONView converter";
// Localization // Localization
let jsonViewStrings = Services.strings.createBundle( loader.lazyGetter(this, "jsonViewStrings", () => {
"chrome://devtools/locale/jsonview.properties"); return Services.strings.createBundle(
"chrome://devtools/locale/jsonview.properties");
});
/** /**
* This object detects 'application/vnd.mozilla.json.view' content type * This object detects 'application/vnd.mozilla.json.view' content type
@ -43,14 +47,14 @@ let jsonViewStrings = Services.strings.createBundle(
* *
* Inspired by JSON View: https://github.com/bhollis/jsonview/ * Inspired by JSON View: https://github.com/bhollis/jsonview/
*/ */
let Converter = Class({ function Converter() {}
extends: Unknown,
interfaces: [ Converter.prototype = {
"nsIStreamConverter", QueryInterface: XPCOMUtils.generateQI([
"nsIStreamListener", Ci.nsIStreamConverter,
"nsIRequestObserver" Ci.nsIStreamListener,
], Ci.nsIRequestObserver
]),
get wrappedJSObject() { get wrappedJSObject() {
return this; return this;
@ -305,28 +309,32 @@ let Converter = Class({
Clipboard.set(value, "text"); Clipboard.set(value, "text");
} }
}); };
// Stream converter component definition const Factory = {
let service = xpcom.Service({ createInstance: function (outer, iid) {
id: components.ID(CLASS_ID), if (outer) {
contract: CONTRACT_ID, throw Cr.NS_ERROR_NO_AGGREGATION;
Component: Converter, }
register: false, return new Converter();
unregister: false }
}); };
function register() { function register() {
if (!xpcom.isRegistered(service)) { if (!registrar.isCIDRegistered(CLASS_ID)) {
xpcom.register(service); registrar.registerFactory(CLASS_ID,
CLASS_DESCRIPTION,
CONTRACT_ID,
Factory);
return true; return true;
} }
return false; return false;
} }
function unregister() { function unregister() {
if (xpcom.isRegistered(service)) { if (registrar.isCIDRegistered(CLASS_ID)) {
xpcom.unregister(service); registrar.unregisterFactory(CLASS_ID, Factory);
return true; return true;
} }
return false; return false;

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

@ -6,25 +6,30 @@
"use strict"; "use strict";
const {Cc, Ci, components} = require("chrome"); const {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
const xpcom = require("sdk/platform/xpcom"); const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
const {Unknown} = require("sdk/platform/xpcom"); const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
const {Class} = require("sdk/core/heritage");
const categoryManager = Cc["@mozilla.org/categorymanager;1"] const categoryManager = Cc["@mozilla.org/categorymanager;1"]
.getService(Ci.nsICategoryManager); .getService(Ci.nsICategoryManager);
loader.lazyRequireGetter(this, "NetworkHelper",
"devtools/shared/webconsole/network-helper");
// Constants // Constants
const JSON_TYPE = "application/json"; const JSON_TYPE = "application/json";
const CONTRACT_ID = "@mozilla.org/devtools/jsonview-sniffer;1"; const CONTRACT_ID = "@mozilla.org/devtools/jsonview-sniffer;1";
const CLASS_ID = "{4148c488-dca1-49fc-a621-2a0097a62422}"; const CLASS_ID = components.ID("{4148c488-dca1-49fc-a621-2a0097a62422}");
const CLASS_DESCRIPTION = "JSONView content sniffer";
const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view"; const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
const JSON_VIEW_TYPE = "JSON View"; const JSON_VIEW_TYPE = "JSON View";
const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers"; const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
function isTopLevelLoad(request) {
let loadInfo = request.loadInfo;
if (loadInfo && loadInfo.isTopLevelLoad) {
return (request.loadFlags & Ci.nsIChannel.LOAD_DOCUMENT_URI);
}
return false;
}
/** /**
* This component represents a sniffer (implements nsIContentSniffer * This component represents a sniffer (implements nsIContentSniffer
* interface) responsible for changing top level 'application/json' * interface) responsible for changing top level 'application/json'
@ -33,24 +38,21 @@ const CONTENT_SNIFFER_CATEGORY = "net-content-sniffers";
* This internal type is consequently rendered by JSON View component * This internal type is consequently rendered by JSON View component
* that represents the JSON through a viewer interface. * that represents the JSON through a viewer interface.
*/ */
var Sniffer = Class({ function Sniffer() {}
extends: Unknown,
interfaces: [ Sniffer.prototype = {
"nsIContentSniffer", QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentSniffer]),
],
get wrappedJSObject() { get wrappedJSObject() {
return this; return this;
}, },
getMIMETypeFromContent: function (request, data, length) { getMIMETypeFromContent: function (request, data, length) {
// JSON View is enabled only for top level loads only.
if (!NetworkHelper.isTopLevelLoad(request)) {
return "";
}
if (request instanceof Ci.nsIChannel) { if (request instanceof Ci.nsIChannel) {
// JSON View is enabled only for top level loads only.
if (!isTopLevelLoad(request)) {
return "";
}
try { try {
if (request.contentDisposition == if (request.contentDisposition ==
Ci.nsIChannel.DISPOSITION_ATTACHMENT) { Ci.nsIChannel.DISPOSITION_ATTACHMENT) {
@ -69,19 +71,23 @@ var Sniffer = Class({
return ""; return "";
} }
}); };
var service = xpcom.Service({ const Factory = {
id: components.ID(CLASS_ID), createInstance: function (outer, iid) {
contract: CONTRACT_ID, if (outer) {
Component: Sniffer, throw Cr.NS_ERROR_NO_AGGREGATION;
register: false, }
unregister: false return new Sniffer();
}); }
};
function register() { function register() {
if (!xpcom.isRegistered(service)) { if (!registrar.isCIDRegistered(CLASS_ID)) {
xpcom.register(service); registrar.registerFactory(CLASS_ID,
CLASS_DESCRIPTION,
CONTRACT_ID,
Factory);
categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE, categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE,
CONTRACT_ID, false, false); CONTRACT_ID, false, false);
return true; return true;
@ -91,10 +97,10 @@ function register() {
} }
function unregister() { function unregister() {
if (xpcom.isRegistered(service)) { if (registrar.isCIDRegistered(CLASS_ID)) {
registrar.unregisterFactory(CLASS_ID, Factory);
categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY, categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY,
JSON_VIEW_TYPE, false); JSON_VIEW_TYPE, false);
xpcom.unregister(service);
return true; return true;
} }
return false; return false;