1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/extensions/shumway/content/ShumwayUtils.jsm Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,140 @@ 1.4 +/* Copyright 2012 Mozilla Foundation 1.5 + * 1.6 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.7 + * you may not use this file except in compliance with the License. 1.8 + * You may obtain a copy of the License at 1.9 + * 1.10 + * http://www.apache.org/licenses/LICENSE-2.0 1.11 + * 1.12 + * Unless required by applicable law or agreed to in writing, software 1.13 + * distributed under the License is distributed on an "AS IS" BASIS, 1.14 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.15 + * See the License for the specific language governing permissions and 1.16 + * limitations under the License. 1.17 + */ 1.18 + 1.19 +var EXPORTED_SYMBOLS = ["ShumwayUtils"]; 1.20 + 1.21 +const RESOURCE_NAME = 'shumway'; 1.22 +const EXT_PREFIX = 'shumway@research.mozilla.org'; 1.23 +const SWF_CONTENT_TYPE = 'application/x-shockwave-flash'; 1.24 +const PREF_PREFIX = 'shumway.'; 1.25 +const PREF_DISABLED = PREF_PREFIX + 'disabled'; 1.26 +const PREF_IGNORE_CTP = PREF_PREFIX + 'ignoreCTP'; 1.27 + 1.28 +let Cc = Components.classes; 1.29 +let Ci = Components.interfaces; 1.30 +let Cm = Components.manager; 1.31 +let Cu = Components.utils; 1.32 + 1.33 +Cu.import('resource://gre/modules/XPCOMUtils.jsm'); 1.34 +Cu.import('resource://gre/modules/Services.jsm'); 1.35 +Cu.import('resource://shumway/ShumwayStreamConverter.jsm'); 1.36 + 1.37 +let Svc = {}; 1.38 +XPCOMUtils.defineLazyServiceGetter(Svc, 'mime', 1.39 + '@mozilla.org/mime;1', 1.40 + 'nsIMIMEService'); 1.41 +XPCOMUtils.defineLazyServiceGetter(Svc, 'pluginHost', 1.42 + '@mozilla.org/plugin/host;1', 1.43 + 'nsIPluginHost'); 1.44 + 1.45 +function getBoolPref(pref, def) { 1.46 + try { 1.47 + return Services.prefs.getBoolPref(pref); 1.48 + } catch (ex) { 1.49 + return def; 1.50 + } 1.51 +} 1.52 + 1.53 +function log(str) { 1.54 + dump(str + '\n'); 1.55 +} 1.56 + 1.57 +// Register/unregister a constructor as a factory. 1.58 +function Factory() {} 1.59 +Factory.prototype = { 1.60 + register: function register(targetConstructor) { 1.61 + var proto = targetConstructor.prototype; 1.62 + this._classID = proto.classID; 1.63 + 1.64 + var factory = XPCOMUtils._getFactory(targetConstructor); 1.65 + this._factory = factory; 1.66 + 1.67 + var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); 1.68 + registrar.registerFactory(proto.classID, proto.classDescription, 1.69 + proto.contractID, factory); 1.70 + }, 1.71 + 1.72 + unregister: function unregister() { 1.73 + var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar); 1.74 + registrar.unregisterFactory(this._classID, this._factory); 1.75 + this._factory = null; 1.76 + } 1.77 +}; 1.78 + 1.79 +let converterFactory = new Factory(); 1.80 +let overlayConverterFactory = new Factory(); 1.81 + 1.82 +let ShumwayUtils = { 1.83 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]), 1.84 + _registered: false, 1.85 + 1.86 + init: function init() { 1.87 + if (this.enabled) 1.88 + this._ensureRegistered(); 1.89 + else 1.90 + this._ensureUnregistered(); 1.91 + 1.92 + // Listen for when shumway is completely disabled. 1.93 + Services.prefs.addObserver(PREF_DISABLED, this, false); 1.94 + }, 1.95 + 1.96 + // nsIObserver 1.97 + observe: function observe(aSubject, aTopic, aData) { 1.98 + if (this.enabled) 1.99 + this._ensureRegistered(); 1.100 + else 1.101 + this._ensureUnregistered(); 1.102 + }, 1.103 + 1.104 + /** 1.105 + * shumway is only enabled if the global switch enabling is true. 1.106 + * @return {boolean} Wether or not it's enabled. 1.107 + */ 1.108 + get enabled() { 1.109 + return !getBoolPref(PREF_DISABLED, true); 1.110 + }, 1.111 + 1.112 + _ensureRegistered: function _ensureRegistered() { 1.113 + if (this._registered) 1.114 + return; 1.115 + 1.116 + // Load the component and register it. 1.117 + converterFactory.register(ShumwayStreamConverter); 1.118 + overlayConverterFactory.register(ShumwayStreamOverlayConverter); 1.119 + 1.120 + var ignoreCTP = getBoolPref(PREF_IGNORE_CTP, true); 1.121 + 1.122 + Svc.pluginHost.registerPlayPreviewMimeType(SWF_CONTENT_TYPE, ignoreCTP); 1.123 + 1.124 + this._registered = true; 1.125 + 1.126 + log('Shumway is registered'); 1.127 + }, 1.128 + 1.129 + _ensureUnregistered: function _ensureUnregistered() { 1.130 + if (!this._registered) 1.131 + return; 1.132 + 1.133 + // Remove the contract/component. 1.134 + converterFactory.unregister(); 1.135 + overlayConverterFactory.unregister(); 1.136 + 1.137 + Svc.pluginHost.unregisterPlayPreviewMimeType(SWF_CONTENT_TYPE); 1.138 + 1.139 + this._registered = false; 1.140 + 1.141 + log('Shumway is unregistered'); 1.142 + } 1.143 +};