2013-10-01 03:24:58 +04:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ["ShumwayUtils"];
|
|
|
|
|
|
|
|
const PREF_PREFIX = 'shumway.';
|
|
|
|
const PREF_DISABLED = PREF_PREFIX + 'disabled';
|
|
|
|
|
|
|
|
let Cc = Components.classes;
|
|
|
|
let Ci = Components.interfaces;
|
|
|
|
let Cm = Components.manager;
|
|
|
|
let Cu = Components.utils;
|
|
|
|
|
|
|
|
Cu.import('resource://gre/modules/XPCOMUtils.jsm');
|
|
|
|
Cu.import('resource://gre/modules/Services.jsm');
|
|
|
|
|
|
|
|
function getBoolPref(pref, def) {
|
|
|
|
try {
|
|
|
|
return Services.prefs.getBoolPref(pref);
|
|
|
|
} catch (ex) {
|
|
|
|
return def;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(str) {
|
|
|
|
dump(str + '\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
let ShumwayUtils = {
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
|
|
|
|
_registered: false,
|
|
|
|
|
|
|
|
init: function init() {
|
|
|
|
if (this.enabled)
|
|
|
|
this._ensureRegistered();
|
|
|
|
else
|
|
|
|
this._ensureUnregistered();
|
|
|
|
|
2015-01-30 23:53:50 +03:00
|
|
|
Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageBroadcaster)
|
|
|
|
.addMessageListener('Shumway:Chrome:isEnabled', this);
|
|
|
|
|
2013-10-01 03:24:58 +04:00
|
|
|
// Listen for when shumway is completely disabled.
|
|
|
|
Services.prefs.addObserver(PREF_DISABLED, this, false);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
observe: function observe(aSubject, aTopic, aData) {
|
|
|
|
if (this.enabled)
|
|
|
|
this._ensureRegistered();
|
|
|
|
else
|
|
|
|
this._ensureUnregistered();
|
|
|
|
},
|
2015-01-30 23:53:50 +03:00
|
|
|
|
|
|
|
receiveMessage: function(message) {
|
|
|
|
switch (message.name) {
|
|
|
|
case 'Shumway:Chrome:isEnabled':
|
|
|
|
return this.enabled;
|
|
|
|
}
|
|
|
|
},
|
2013-10-01 03:24:58 +04:00
|
|
|
|
|
|
|
/**
|
2013-10-18 01:11:52 +04:00
|
|
|
* shumway is only enabled if the global switch enabling is true.
|
2013-10-01 03:24:58 +04:00
|
|
|
* @return {boolean} Wether or not it's enabled.
|
|
|
|
*/
|
|
|
|
get enabled() {
|
|
|
|
return !getBoolPref(PREF_DISABLED, true);
|
|
|
|
},
|
|
|
|
|
|
|
|
_ensureRegistered: function _ensureRegistered() {
|
|
|
|
if (this._registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Load the component and register it.
|
2015-01-30 23:53:50 +03:00
|
|
|
Cu.import('resource://shumway/ShumwayBootstrapUtils.jsm');
|
|
|
|
ShumwayBootstrapUtils.register();
|
2013-10-01 03:24:58 +04:00
|
|
|
|
|
|
|
this._registered = true;
|
|
|
|
|
|
|
|
log('Shumway is registered');
|
2015-01-30 23:53:50 +03:00
|
|
|
|
|
|
|
let globalMM = Cc['@mozilla.org/globalmessagemanager;1']
|
|
|
|
.getService(Ci.nsIFrameScriptLoader);
|
|
|
|
globalMM.broadcastAsyncMessage('Shumway:Child:refreshSettings');
|
2013-10-01 03:24:58 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_ensureUnregistered: function _ensureUnregistered() {
|
|
|
|
if (!this._registered)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Remove the contract/component.
|
2015-01-30 23:53:50 +03:00
|
|
|
ShumwayBootstrapUtils.unregister();
|
|
|
|
Cu.unload('resource://shumway/ShumwayBootstrapUtils.jsm');
|
2013-10-01 03:24:58 +04:00
|
|
|
|
|
|
|
this._registered = false;
|
|
|
|
|
|
|
|
log('Shumway is unregistered');
|
2015-01-30 23:53:50 +03:00
|
|
|
|
|
|
|
let globalMM = Cc['@mozilla.org/globalmessagemanager;1']
|
|
|
|
.getService(Ci.nsIFrameScriptLoader);
|
|
|
|
globalMM.broadcastAsyncMessage('Shumway:Child:refreshSettings');
|
2013-10-01 03:24:58 +04:00
|
|
|
}
|
|
|
|
};
|