2015-09-28 14:34:03 +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";
|
|
|
|
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
|
|
|
const {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
|
|
|
|
const {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
|
2015-10-14 17:12:03 +03:00
|
|
|
|
|
|
|
// Load devtools module lazily.
|
|
|
|
XPCOMUtils.defineLazyGetter(this, "devtools", function() {
|
|
|
|
const {devtools} = Cu.import("resource://gre/modules/devtools/shared/Loader.jsm", {});
|
|
|
|
return devtools;
|
|
|
|
});
|
2015-09-28 14:34:03 +03:00
|
|
|
|
2015-09-28 14:48:33 +03:00
|
|
|
// Load JsonView services lazily.
|
2015-09-28 14:34:03 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "JsonViewService", function() {
|
|
|
|
const {JsonViewService} = devtools.require("devtools/client/jsonview/converter-child");
|
|
|
|
return JsonViewService;
|
|
|
|
});
|
|
|
|
|
2015-09-28 14:48:33 +03:00
|
|
|
XPCOMUtils.defineLazyGetter(this, "JsonViewSniffer", function() {
|
|
|
|
const {JsonViewSniffer} = devtools.require("devtools/client/jsonview/converter-sniffer");
|
|
|
|
return JsonViewSniffer;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Constants
|
2015-09-28 14:34:03 +03:00
|
|
|
const JSON_VIEW_PREF = "devtools.jsonview.enabled";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listen for 'devtools.jsonview.enabled' preference changes and
|
2015-09-28 14:48:33 +03:00
|
|
|
* register/unregister the JSON View XPCOM services as appropriate.
|
2015-09-28 14:34:03 +03:00
|
|
|
*/
|
|
|
|
function ConverterObserver() {
|
|
|
|
}
|
|
|
|
|
|
|
|
ConverterObserver.prototype = {
|
|
|
|
initialize: function() {
|
|
|
|
// 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.
|
|
|
|
if (this.isEnabled()) {
|
|
|
|
this.register();
|
|
|
|
}
|
|
|
|
|
|
|
|
Services.prefs.addObserver(JSON_VIEW_PREF, this, false);
|
|
|
|
Services.obs.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
},
|
|
|
|
|
|
|
|
observe: function(subject, topic, data) {
|
|
|
|
switch (topic) {
|
|
|
|
case "xpcom-shutdown":
|
|
|
|
this.onShutdown();
|
|
|
|
break;
|
|
|
|
case "nsPref:changed":
|
|
|
|
this.onPrefChanged();
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
onShutdown: function() {
|
|
|
|
Services.prefs.removeObserver(JSON_VIEW_PREF, observer);
|
|
|
|
Services.obs.removeObserver(observer, "xpcom-shutdown");
|
|
|
|
},
|
|
|
|
|
|
|
|
onPrefChanged: function() {
|
|
|
|
if (this.isEnabled()) {
|
|
|
|
this.register();
|
|
|
|
} else {
|
|
|
|
this.unregister();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
register: function() {
|
2015-09-28 14:48:33 +03:00
|
|
|
JsonViewSniffer.register();
|
|
|
|
JsonViewService.register();
|
2015-09-28 14:34:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
unregister: function() {
|
2015-09-28 14:48:33 +03:00
|
|
|
JsonViewSniffer.unregister();
|
|
|
|
JsonViewService.unregister();
|
2015-09-28 14:34:03 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
isEnabled: function() {
|
|
|
|
return Services.prefs.getBoolPref(JSON_VIEW_PREF);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// Listen to JSON View 'enable' pref and perform dynamic
|
|
|
|
// registration or unregistration of the main application
|
|
|
|
// component.
|
|
|
|
var observer = new ConverterObserver();
|
|
|
|
observer.initialize();
|