michael@0: /* Copyright 2012 Mozilla Foundation michael@0: * michael@0: * Licensed under the Apache License, Version 2.0 (the "License"); michael@0: * you may not use this file except in compliance with the License. michael@0: * You may obtain a copy of the License at michael@0: * michael@0: * http://www.apache.org/licenses/LICENSE-2.0 michael@0: * michael@0: * Unless required by applicable law or agreed to in writing, software michael@0: * distributed under the License is distributed on an "AS IS" BASIS, michael@0: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. michael@0: * See the License for the specific language governing permissions and michael@0: * limitations under the License. michael@0: */ michael@0: michael@0: var EXPORTED_SYMBOLS = ["ShumwayUtils"]; michael@0: michael@0: const RESOURCE_NAME = 'shumway'; michael@0: const EXT_PREFIX = 'shumway@research.mozilla.org'; michael@0: const SWF_CONTENT_TYPE = 'application/x-shockwave-flash'; michael@0: const PREF_PREFIX = 'shumway.'; michael@0: const PREF_DISABLED = PREF_PREFIX + 'disabled'; michael@0: const PREF_IGNORE_CTP = PREF_PREFIX + 'ignoreCTP'; michael@0: michael@0: let Cc = Components.classes; michael@0: let Ci = Components.interfaces; michael@0: let Cm = Components.manager; michael@0: let Cu = Components.utils; michael@0: michael@0: Cu.import('resource://gre/modules/XPCOMUtils.jsm'); michael@0: Cu.import('resource://gre/modules/Services.jsm'); michael@0: Cu.import('resource://shumway/ShumwayStreamConverter.jsm'); michael@0: michael@0: let Svc = {}; michael@0: XPCOMUtils.defineLazyServiceGetter(Svc, 'mime', michael@0: '@mozilla.org/mime;1', michael@0: 'nsIMIMEService'); michael@0: XPCOMUtils.defineLazyServiceGetter(Svc, 'pluginHost', michael@0: '@mozilla.org/plugin/host;1', michael@0: 'nsIPluginHost'); michael@0: michael@0: function getBoolPref(pref, def) { michael@0: try { michael@0: return Services.prefs.getBoolPref(pref); michael@0: } catch (ex) { michael@0: return def; michael@0: } michael@0: } michael@0: michael@0: function log(str) { michael@0: dump(str + '\n'); michael@0: } michael@0: michael@0: // Register/unregister a constructor as a factory. michael@0: function Factory() {} michael@0: Factory.prototype = { michael@0: register: function register(targetConstructor) { michael@0: var proto = targetConstructor.prototype; michael@0: this._classID = proto.classID; michael@0: michael@0: var factory = XPCOMUtils._getFactory(targetConstructor); michael@0: this._factory = factory; michael@0: michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.registerFactory(proto.classID, proto.classDescription, michael@0: proto.contractID, factory); michael@0: }, michael@0: michael@0: unregister: function unregister() { michael@0: var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); michael@0: registrar.unregisterFactory(this._classID, this._factory); michael@0: this._factory = null; michael@0: } michael@0: }; michael@0: michael@0: let converterFactory = new Factory(); michael@0: let overlayConverterFactory = new Factory(); michael@0: michael@0: let ShumwayUtils = { michael@0: QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), michael@0: _registered: false, michael@0: michael@0: init: function init() { michael@0: if (this.enabled) michael@0: this._ensureRegistered(); michael@0: else michael@0: this._ensureUnregistered(); michael@0: michael@0: // Listen for when shumway is completely disabled. michael@0: Services.prefs.addObserver(PREF_DISABLED, this, false); michael@0: }, michael@0: michael@0: // nsIObserver michael@0: observe: function observe(aSubject, aTopic, aData) { michael@0: if (this.enabled) michael@0: this._ensureRegistered(); michael@0: else michael@0: this._ensureUnregistered(); michael@0: }, michael@0: michael@0: /** michael@0: * shumway is only enabled if the global switch enabling is true. michael@0: * @return {boolean} Wether or not it's enabled. michael@0: */ michael@0: get enabled() { michael@0: return !getBoolPref(PREF_DISABLED, true); michael@0: }, michael@0: michael@0: _ensureRegistered: function _ensureRegistered() { michael@0: if (this._registered) michael@0: return; michael@0: michael@0: // Load the component and register it. michael@0: converterFactory.register(ShumwayStreamConverter); michael@0: overlayConverterFactory.register(ShumwayStreamOverlayConverter); michael@0: michael@0: var ignoreCTP = getBoolPref(PREF_IGNORE_CTP, true); michael@0: michael@0: Svc.pluginHost.registerPlayPreviewMimeType(SWF_CONTENT_TYPE, ignoreCTP); michael@0: michael@0: this._registered = true; michael@0: michael@0: log('Shumway is registered'); michael@0: }, michael@0: michael@0: _ensureUnregistered: function _ensureUnregistered() { michael@0: if (!this._registered) michael@0: return; michael@0: michael@0: // Remove the contract/component. michael@0: converterFactory.unregister(); michael@0: overlayConverterFactory.unregister(); michael@0: michael@0: Svc.pluginHost.unregisterPlayPreviewMimeType(SWF_CONTENT_TYPE); michael@0: michael@0: this._registered = false; michael@0: michael@0: log('Shumway is unregistered'); michael@0: } michael@0: };