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

MozReview-Commit-ID: AClST5BXfK1

--HG--
extra : rebase_source : 725e2374c42cbf7b55fd53acce8f9755af19fbba
This commit is contained in:
Alexandre Poirot 2016-11-15 09:30:46 -08:00
Родитель b76dc18132
Коммит a4f35a668b
2 изменённых файлов: 73 добавлений и 59 удалений

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

@ -6,16 +6,17 @@
"use strict";
const {Cu, 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 {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",
"devtools/shared/webconsole/network-helper");
loader.lazyRequireGetter(this, "Events",
"sdk/dom/events");
loader.lazyRequireGetter(this, "Clipboard",
"sdk/clipboard");
loader.lazyRequireGetter(this, "JsonViewUtils",
"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 CONTRACT_ID = "@mozilla.org/streamconv;1?from=" +
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
let jsonViewStrings = Services.strings.createBundle(
"chrome://devtools/locale/jsonview.properties");
loader.lazyGetter(this, "jsonViewStrings", () => {
return Services.strings.createBundle(
"chrome://devtools/locale/jsonview.properties");
});
/**
* 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/
*/
let Converter = Class({
extends: Unknown,
function Converter() {}
interfaces: [
"nsIStreamConverter",
"nsIStreamListener",
"nsIRequestObserver"
],
Converter.prototype = {
QueryInterface: XPCOMUtils.generateQI([
Ci.nsIStreamConverter,
Ci.nsIStreamListener,
Ci.nsIRequestObserver
]),
get wrappedJSObject() {
return this;
@ -301,28 +305,32 @@ let Converter = Class({
Clipboard.set(value, "text");
}
});
};
// Stream converter component definition
let service = xpcom.Service({
id: components.ID(CLASS_ID),
contract: CONTRACT_ID,
Component: Converter,
register: false,
unregister: false
});
const Factory = {
createInstance: function (outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return new Converter();
}
};
function register() {
if (!xpcom.isRegistered(service)) {
xpcom.register(service);
if (!registrar.isCIDRegistered(CLASS_ID)) {
registrar.registerFactory(CLASS_ID,
CLASS_DESCRIPTION,
CONTRACT_ID,
Factory);
return true;
}
return false;
}
function unregister() {
if (xpcom.isRegistered(service)) {
xpcom.unregister(service);
if (registrar.isCIDRegistered(CLASS_ID)) {
registrar.unregisterFactory(CLASS_ID, Factory);
return true;
}
return false;

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

@ -6,25 +6,30 @@
"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 {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
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 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_TYPE = "JSON View";
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
* 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
* that represents the JSON through a viewer interface.
*/
var Sniffer = Class({
extends: Unknown,
function Sniffer() {}
interfaces: [
"nsIContentSniffer",
],
Sniffer.prototype = {
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentSniffer]),
get wrappedJSObject() {
return this;
},
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) {
// JSON View is enabled only for top level loads only.
if (!isTopLevelLoad(request)) {
return "";
}
try {
if (request.contentDisposition ==
Ci.nsIChannel.DISPOSITION_ATTACHMENT) {
@ -69,19 +71,23 @@ var Sniffer = Class({
return "";
}
});
};
var service = xpcom.Service({
id: components.ID(CLASS_ID),
contract: CONTRACT_ID,
Component: Sniffer,
register: false,
unregister: false
});
const Factory = {
createInstance: function (outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return new Sniffer();
}
};
function register() {
if (!xpcom.isRegistered(service)) {
xpcom.register(service);
if (!registrar.isCIDRegistered(CLASS_ID)) {
registrar.registerFactory(CLASS_ID,
CLASS_DESCRIPTION,
CONTRACT_ID,
Factory);
categoryManager.addCategoryEntry(CONTENT_SNIFFER_CATEGORY, JSON_VIEW_TYPE,
CONTRACT_ID, false, false);
return true;
@ -91,10 +97,10 @@ function register() {
}
function unregister() {
if (xpcom.isRegistered(service)) {
if (registrar.isCIDRegistered(CLASS_ID)) {
registrar.unregisterFactory(CLASS_ID, Factory);
categoryManager.deleteCategoryEntry(CONTENT_SNIFFER_CATEGORY,
JSON_VIEW_TYPE, false);
xpcom.unregister(service);
return true;
}
return false;