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.

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

mercurial