browser/extensions/shumway/content/ShumwayUtils.jsm

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:f386b100936a
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 */
15
16 var EXPORTED_SYMBOLS = ["ShumwayUtils"];
17
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';
24
25 let Cc = Components.classes;
26 let Ci = Components.interfaces;
27 let Cm = Components.manager;
28 let Cu = Components.utils;
29
30 Cu.import('resource://gre/modules/XPCOMUtils.jsm');
31 Cu.import('resource://gre/modules/Services.jsm');
32 Cu.import('resource://shumway/ShumwayStreamConverter.jsm');
33
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');
41
42 function getBoolPref(pref, def) {
43 try {
44 return Services.prefs.getBoolPref(pref);
45 } catch (ex) {
46 return def;
47 }
48 }
49
50 function log(str) {
51 dump(str + '\n');
52 }
53
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;
60
61 var factory = XPCOMUtils._getFactory(targetConstructor);
62 this._factory = factory;
63
64 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
65 registrar.registerFactory(proto.classID, proto.classDescription,
66 proto.contractID, factory);
67 },
68
69 unregister: function unregister() {
70 var registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
71 registrar.unregisterFactory(this._classID, this._factory);
72 this._factory = null;
73 }
74 };
75
76 let converterFactory = new Factory();
77 let overlayConverterFactory = new Factory();
78
79 let ShumwayUtils = {
80 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver]),
81 _registered: false,
82
83 init: function init() {
84 if (this.enabled)
85 this._ensureRegistered();
86 else
87 this._ensureUnregistered();
88
89 // Listen for when shumway is completely disabled.
90 Services.prefs.addObserver(PREF_DISABLED, this, false);
91 },
92
93 // nsIObserver
94 observe: function observe(aSubject, aTopic, aData) {
95 if (this.enabled)
96 this._ensureRegistered();
97 else
98 this._ensureUnregistered();
99 },
100
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 },
108
109 _ensureRegistered: function _ensureRegistered() {
110 if (this._registered)
111 return;
112
113 // Load the component and register it.
114 converterFactory.register(ShumwayStreamConverter);
115 overlayConverterFactory.register(ShumwayStreamOverlayConverter);
116
117 var ignoreCTP = getBoolPref(PREF_IGNORE_CTP, true);
118
119 Svc.pluginHost.registerPlayPreviewMimeType(SWF_CONTENT_TYPE, ignoreCTP);
120
121 this._registered = true;
122
123 log('Shumway is registered');
124 },
125
126 _ensureUnregistered: function _ensureUnregistered() {
127 if (!this._registered)
128 return;
129
130 // Remove the contract/component.
131 converterFactory.unregister();
132 overlayConverterFactory.unregister();
133
134 Svc.pluginHost.unregisterPlayPreviewMimeType(SWF_CONTENT_TYPE);
135
136 this._registered = false;
137
138 log('Shumway is unregistered');
139 }
140 };

mercurial