Bug 1351385, part 2 - Lazily load JsonViewService. r=Honza

This avoids triggering the devtools loading system just to register a
factory.

MozReview-Commit-ID: BVMfCRlG9kv

--HG--
extra : rebase_source : 06dadfb180ae8855a7edfba38322d08afeb8dc8e
This commit is contained in:
Andrew McCreight 2017-03-28 09:13:05 -07:00
Родитель b9480f3867
Коммит 87776ec0a5
2 изменённых файлов: 37 добавлений и 41 удалений

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

@ -6,8 +6,7 @@
"use strict";
const {Cc, Ci, Cu, Cm, Cr, components} = require("chrome");
const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
const {Cc, Ci, Cu} = require("chrome");
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
const Services = require("Services");
@ -28,12 +27,6 @@ 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 JSON_VIEW_CONTRACT_ID = "@mozilla.org/streamconv;1?from=" +
JSON_VIEW_MIME_TYPE + "&to=*/*";
const JSON_VIEW_CLASS_ID = components.ID("{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}");
const JSON_VIEW_CLASS_DESCRIPTION = "JSONView converter";
// Localization
loader.lazyGetter(this, "jsonViewStrings", () => {
return Services.strings.createBundle(
@ -327,36 +320,6 @@ function createInstance() {
return new Converter();
}
const JsonViewFactory = {
createInstance: function (outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return createInstance();
}
};
function register() {
if (!registrar.isCIDRegistered(JSON_VIEW_CLASS_ID)) {
registrar.registerFactory(JSON_VIEW_CLASS_ID,
JSON_VIEW_CLASS_DESCRIPTION,
JSON_VIEW_CONTRACT_ID,
JsonViewFactory);
return true;
}
return false;
}
function unregister() {
if (registrar.isCIDRegistered(JSON_VIEW_CLASS_ID)) {
registrar.unregisterFactory(JSON_VIEW_CLASS_ID, JsonViewFactory);
return true;
}
return false;
}
exports.JsonViewService = {
register: register,
unregister: unregister
createInstance: createInstance,
};

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

@ -6,6 +6,9 @@
"use strict";
const Ci = Components.interfaces;
const Cm = Components.manager;
const Cr = Components.results;
const Cu = Components.utils;
const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
@ -30,6 +33,25 @@ XPCOMUtils.defineLazyGetter(this, "JsonViewSniffer", function () {
// Constants
const JSON_VIEW_PREF = "devtools.jsonview.enabled";
const JSON_VIEW_MIME_TYPE = "application/vnd.mozilla.json.view";
const JSON_VIEW_CONTRACT_ID = "@mozilla.org/streamconv;1?from=" +
JSON_VIEW_MIME_TYPE + "&to=*/*";
const JSON_VIEW_CLASS_ID = Components.ID("{d8c9acee-dec5-11e4-8c75-1681e6b88ec1}");
const JSON_VIEW_CLASS_DESCRIPTION = "JSONView converter";
/*
* Create instances of the JSON view converter.
* This is done in the .js file rather than a .jsm to avoid creating
* a compartment at startup when no JSON is being viewed.
*/
const JsonViewFactory = {
createInstance: function (outer, iid) {
if (outer) {
throw Cr.NS_ERROR_NO_AGGREGATION;
}
return JsonViewService.createInstance();
}
};
/**
* Listen for 'devtools.jsonview.enabled' preference changes and
@ -77,12 +99,23 @@ ConverterObserver.prototype = {
register: function () {
JsonViewSniffer.register();
JsonViewService.register();
const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
if (!registrar.isCIDRegistered(JSON_VIEW_CLASS_ID)) {
registrar.registerFactory(JSON_VIEW_CLASS_ID,
JSON_VIEW_CLASS_DESCRIPTION,
JSON_VIEW_CONTRACT_ID,
JsonViewFactory);
}
},
unregister: function () {
JsonViewSniffer.unregister();
JsonViewService.unregister();
const registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
if (registrar.isCIDRegistered(JSON_VIEW_CLASS_ID)) {
registrar.unregisterFactory(JSON_VIEW_CLASS_ID, JsonViewFactory);
}
},
isEnabled: function () {