browser/extensions/shumway/content/ShumwayUtils.jsm

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Copyright 2012 Mozilla Foundation
michael@0 2 *
michael@0 3 * Licensed under the Apache License, Version 2.0 (the "License");
michael@0 4 * you may not use this file except in compliance with the License.
michael@0 5 * You may obtain a copy of the License at
michael@0 6 *
michael@0 7 * http://www.apache.org/licenses/LICENSE-2.0
michael@0 8 *
michael@0 9 * Unless required by applicable law or agreed to in writing, software
michael@0 10 * distributed under the License is distributed on an "AS IS" BASIS,
michael@0 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
michael@0 12 * See the License for the specific language governing permissions and
michael@0 13 * limitations under the License.
michael@0 14 */
michael@0 15
michael@0 16 var EXPORTED_SYMBOLS = ["ShumwayUtils"];
michael@0 17
michael@0 18 const RESOURCE_NAME = 'shumway';
michael@0 19 const EXT_PREFIX = 'shumway@research.mozilla.org';
michael@0 20 const SWF_CONTENT_TYPE = 'application/x-shockwave-flash';
michael@0 21 const PREF_PREFIX = 'shumway.';
michael@0 22 const PREF_DISABLED = PREF_PREFIX + 'disabled';
michael@0 23 const PREF_IGNORE_CTP = PREF_PREFIX + 'ignoreCTP';
michael@0 24
michael@0 25 let Cc = Components.classes;
michael@0 26 let Ci = Components.interfaces;
michael@0 27 let Cm = Components.manager;
michael@0 28 let Cu = Components.utils;
michael@0 29
michael@0 30 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
michael@0 31 Cu.import('resource://gre/modules/Services.jsm');
michael@0 32 Cu.import('resource://shumway/ShumwayStreamConverter.jsm');
michael@0 33
michael@0 34 let Svc = {};
michael@0 35 XPCOMUtils.defineLazyServiceGetter(Svc, 'mime',
michael@0 36 '@mozilla.org/mime;1',
michael@0 37 'nsIMIMEService');
michael@0 38 XPCOMUtils.defineLazyServiceGetter(Svc, 'pluginHost',
michael@0 39 '@mozilla.org/plugin/host;1',
michael@0 40 'nsIPluginHost');
michael@0 41
michael@0 42 function getBoolPref(pref, def) {
michael@0 43 try {
michael@0 44 return Services.prefs.getBoolPref(pref);
michael@0 45 } catch (ex) {
michael@0 46 return def;
michael@0 47 }
michael@0 48 }
michael@0 49
michael@0 50 function log(str) {
michael@0 51 dump(str + '\n');
michael@0 52 }
michael@0 53
michael@0 54 // Register/unregister a constructor as a factory.
michael@0 55 function Factory() {}
michael@0 56 Factory.prototype = {
michael@0 57 register: function register(targetConstructor) {
michael@0 58 var proto = targetConstructor.prototype;
michael@0 59 this._classID = proto.classID;
michael@0 60
michael@0 61 var factory = XPCOMUtils._getFactory(targetConstructor);
michael@0 62 this._factory = factory;
michael@0 63
michael@0 64 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 65 registrar.registerFactory(proto.classID, proto.classDescription,
michael@0 66 proto.contractID, factory);
michael@0 67 },
michael@0 68
michael@0 69 unregister: function unregister() {
michael@0 70 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
michael@0 71 registrar.unregisterFactory(this._classID, this._factory);
michael@0 72 this._factory = null;
michael@0 73 }
michael@0 74 };
michael@0 75
michael@0 76 let converterFactory = new Factory();
michael@0 77 let overlayConverterFactory = new Factory();
michael@0 78
michael@0 79 let ShumwayUtils = {
michael@0 80 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
michael@0 81 _registered: false,
michael@0 82
michael@0 83 init: function init() {
michael@0 84 if (this.enabled)
michael@0 85 this._ensureRegistered();
michael@0 86 else
michael@0 87 this._ensureUnregistered();
michael@0 88
michael@0 89 // Listen for when shumway is completely disabled.
michael@0 90 Services.prefs.addObserver(PREF_DISABLED, this, false);
michael@0 91 },
michael@0 92
michael@0 93 // nsIObserver
michael@0 94 observe: function observe(aSubject, aTopic, aData) {
michael@0 95 if (this.enabled)
michael@0 96 this._ensureRegistered();
michael@0 97 else
michael@0 98 this._ensureUnregistered();
michael@0 99 },
michael@0 100
michael@0 101 /**
michael@0 102 * shumway is only enabled if the global switch enabling is true.
michael@0 103 * @return {boolean} Wether or not it's enabled.
michael@0 104 */
michael@0 105 get enabled() {
michael@0 106 return !getBoolPref(PREF_DISABLED, true);
michael@0 107 },
michael@0 108
michael@0 109 _ensureRegistered: function _ensureRegistered() {
michael@0 110 if (this._registered)
michael@0 111 return;
michael@0 112
michael@0 113 // Load the component and register it.
michael@0 114 converterFactory.register(ShumwayStreamConverter);
michael@0 115 overlayConverterFactory.register(ShumwayStreamOverlayConverter);
michael@0 116
michael@0 117 var ignoreCTP = getBoolPref(PREF_IGNORE_CTP, true);
michael@0 118
michael@0 119 Svc.pluginHost.registerPlayPreviewMimeType(SWF_CONTENT_TYPE, ignoreCTP);
michael@0 120
michael@0 121 this._registered = true;
michael@0 122
michael@0 123 log('Shumway is registered');
michael@0 124 },
michael@0 125
michael@0 126 _ensureUnregistered: function _ensureUnregistered() {
michael@0 127 if (!this._registered)
michael@0 128 return;
michael@0 129
michael@0 130 // Remove the contract/component.
michael@0 131 converterFactory.unregister();
michael@0 132 overlayConverterFactory.unregister();
michael@0 133
michael@0 134 Svc.pluginHost.unregisterPlayPreviewMimeType(SWF_CONTENT_TYPE);
michael@0 135
michael@0 136 this._registered = false;
michael@0 137
michael@0 138 log('Shumway is unregistered');
michael@0 139 }
michael@0 140 };

mercurial