]> git.donarmstrong.com Git - dactyl.git/blob - common/bootstrap.js
Merge tag 'upstream/1.1+hg7904'
[dactyl.git] / common / bootstrap.js
1 // Copyright (c) 2010-2014 by Kris Maglione <maglione.k@gmail.com>
2 //
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
5 //
6 // See https://wiki.mozilla.org/Extension_Manager:Bootstrapped_Extensions
7 // for details.
8 "use strict";
9
10 const global = this;
11
12 var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
13
14 function module(uri) Cu.import(uri, {});
15
16 const DEBUG = true;
17
18 __defineGetter__("BOOTSTRAP", () => "resource://" + moduleName + "/bootstrap.jsm");
19
20 var { AddonManager } = module("resource://gre/modules/AddonManager.jsm");
21 var { XPCOMUtils }   = module("resource://gre/modules/XPCOMUtils.jsm");
22 var { Services }     = module("resource://gre/modules/Services.jsm");
23
24 var Timer = Components.Constructor("@mozilla.org/timer;1", "nsITimer", "initWithCallback");
25
26 const resourceProto = Services.io.getProtocolHandler("resource")
27                               .QueryInterface(Ci.nsIResProtocolHandler);
28 const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
29 const manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
30
31 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
32
33 var name = "dactyl";
34
35 function reportError(e) {
36     let stack = e.stack || Error().stack;
37     dump("\n" + name + ": bootstrap: " + e + "\n" + stack + "\n");
38     Cu.reportError(e);
39     Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
40                                        .logStringMessage(stack);
41 }
42 function debug(...args) {
43     if (DEBUG)
44         dump(name + ": " + args.join(", ") + "\n");
45 }
46
47 function httpGet(uri) {
48     let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
49     xmlhttp.overrideMimeType("text/plain");
50     xmlhttp.open("GET", uri.spec || uri, false);
51     xmlhttp.send(null);
52     return xmlhttp;
53 }
54
55 let moduleName;
56 let initialized = false;
57 let addon = null;
58 let addonData = null;
59 let basePath = null;
60 let bootstrap;
61 let bootstrap_jsm;
62 let components = {};
63 let getURI = null;
64
65 let JSMLoader = {
66     SANDBOX: Cu.nukeSandbox,
67
68     get addon() addon,
69
70     currentModule: null,
71
72     factories: [],
73
74     get name() name,
75
76     get module() moduleName,
77
78     globals: {},
79     modules: {},
80
81     times: {
82         all: 0,
83         add: function add(major, minor, delta) {
84             this.all += delta;
85
86             this[major] = (this[major] || 0) + delta;
87             if (minor) {
88                 minor = ":" + minor;
89                 this[minor] = (this[minor] || 0) + delta;
90                 this[major + minor] = (this[major + minor] || 0) + delta;
91             }
92         },
93         clear: function clear() {
94             for (let key in this)
95                 if (typeof this[key] !== "number")
96                     delete this[key];
97         }
98     },
99
100     getTarget: function getTarget(url) {
101         let uri = Services.io.newURI(url, null, null);
102         if (uri.schemeIs("resource"))
103             return resourceProto.resolveURI(uri);
104
105         let chan = Services.io.newChannelFromURI(uri);
106         try { chan.cancel(Cr.NS_BINDING_ABORTED); } catch (e) {}
107         return chan.name;
108     },
109
110     _atexit: [],
111
112     atexit: function atexit(arg, self) {
113         if (typeof arg !== "string")
114             this._atexit.push(arguments);
115         else
116             for each (let [fn, self] in this._atexit)
117                 try {
118                     fn.call(self, arg);
119                 }
120                 catch (e) {
121                     reportError(e);
122                 }
123     },
124
125     _load: function _load(name, target) {
126         let urls = [name];
127         if (name.indexOf(":") === -1)
128             urls = this.config["module-paths"].map(path => path + name + ".jsm");
129
130         for each (let url in urls)
131             try {
132                 var uri = this.getTarget(url);
133                 if (uri in this.globals)
134                     return this.modules[name] = this.globals[uri];
135
136                 this.globals[uri] = this.modules[name];
137                 bootstrap_jsm.loadSubScript(url, this.modules[name], "UTF-8");
138                 return;
139             }
140             catch (e) {
141                 debug("Loading " + name + ": " + e);
142                 delete this.globals[uri];
143
144                 if (typeof e != "string")
145                     throw e;
146             }
147
148         throw Error("No such module: " + name);
149     },
150
151     load: function load(name, target) {
152         if (!this.modules.hasOwnProperty(name)) {
153             this.modules[name] = this.modules.base ? bootstrap.create(this.modules.base)
154                                                    : bootstrap.import({ JSMLoader: this, module: global.module });
155
156             let currentModule = this.currentModule;
157             this.currentModule = this.modules[name];
158
159             try {
160                 this._load(name, this.modules[name]);
161             }
162             catch (e) {
163                 delete this.modules[name];
164                 reportError(e);
165                 throw e;
166             }
167             finally {
168                 this.currentModule = currentModule;
169             }
170         }
171
172         let module = this.modules[name];
173         if (target)
174             for each (let symbol in module.EXPORTED_SYMBOLS)
175                 try {
176                     Object.defineProperty(target, symbol, {
177                         configurable: true,
178                         enumerable: true,
179                         writable: true,
180                         value: module[symbol]
181                     });
182                 }
183                 catch (e) {
184                     target[symbol] = module[symbol];
185                 }
186
187         return module;
188     },
189
190     // Cuts down on stupid, fscking url mangling.
191     get loadSubScript() bootstrap_jsm.loadSubScript,
192
193     cleanup: function cleanup() {
194         for (let factory of this.factories.splice(0))
195             manager.unregisterFactory(factory.classID, factory);
196     },
197
198     Factory: function Factory(class_) ({
199         __proto__: class_.prototype,
200
201         createInstance: function (outer, iid) {
202             try {
203                 if (outer != null)
204                     throw Cr.NS_ERROR_NO_AGGREGATION;
205                 if (!class_.instance)
206                     class_.instance = new class_();
207                 return class_.instance.QueryInterface(iid);
208             }
209             catch (e) {
210                 Cu.reportError(e);
211                 throw e;
212             }
213         }
214     }),
215
216     registerFactory: function registerFactory(factory) {
217         manager.registerFactory(factory.classID,
218                                 String(factory.classID),
219                                 factory.contractID,
220                                 factory);
221         this.factories.push(factory);
222     }
223 };
224
225 function init() {
226     debug("bootstrap: init");
227
228     let manifest = JSON.parse(httpGet(getURI("config.json"))
229                                 .responseText);
230
231     if (!manifest.categories)
232         manifest.categories = [];
233
234     for (let [classID, { contract, path, categories }] of Iterator(manifest.components || {})) {
235         components[classID] = new FactoryProxy(getURI(path).spec, classID, contract);
236         if (categories)
237             for (let [category, id] in Iterator(categories))
238                 manifest.categories.push([category, id, contract]);
239     }
240
241     for (let [category, id, value] of manifest.categories)
242         categoryManager.addCategoryEntry(category, id, value,
243                                          false, true);
244
245     for (let [pkg, path] in Iterator(manifest.resources || {})) {
246         moduleName = moduleName || pkg;
247         resourceProto.setSubstitution(pkg, getURI(path));
248     }
249
250     JSMLoader.config = manifest;
251
252     bootstrap_jsm = module(BOOTSTRAP);
253     if (!JSMLoader.SANDBOX)
254         bootstrap = bootstrap_jsm;
255     else {
256         bootstrap = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(),
257                                { sandboxName: BOOTSTRAP });
258         Services.scriptloader.loadSubScript(BOOTSTRAP, bootstrap);
259     }
260     bootstrap.require = JSMLoader.load("base").require;
261
262     let pref = "extensions.dactyl.cacheFlushCheck";
263     let val  = addon.version;
264     if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
265         var cacheFlush = true;
266         Services.prefs.setCharPref(pref, val);
267     }
268
269     Services.obs.notifyObservers(null, "dactyl-rehash", null);
270
271     JSMLoader.bootstrap = global;
272
273     JSMLoader.load("config", global);
274     JSMLoader.load("main", global);
275
276     JSMLoader.cacheFlush = cacheFlush;
277     JSMLoader.load("base", global);
278
279     if (!(BOOTSTRAP_CONTRACT in Cc)) {
280         // Use Sandbox to prevent closures over this scope
281         let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance());
282         let factory = Cu.evalInSandbox("({ createInstance: function () this })", sandbox);
283
284         factory.classID         = Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}");
285         factory.contractID      = BOOTSTRAP_CONTRACT;
286         factory.QueryInterface  = XPCOMUtils.generateQI([Ci.nsIFactory]);
287         factory.wrappedJSObject = factory;
288
289         manager.registerFactory(factory.classID, String(factory.classID),
290                                 BOOTSTRAP_CONTRACT, factory);
291     }
292
293     Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
294
295     for each (let component in components)
296         component.register();
297
298     updateVersion();
299
300     if (addon !== addonData)
301         require("main", global);
302 }
303
304 /**
305  * Performs necessary migrations after a version change.
306  */
307 function updateVersion() {
308     function isDev(ver) /^hg|pre$/.test(ver);
309     try {
310         if (typeof require === "undefined" || addon === addonData)
311             return;
312
313         JSMLoader.load("prefs", global);
314         config.lastVersion = localPrefs.get("lastVersion", null);
315
316         localPrefs.set("lastVersion", addon.version);
317
318         // We're switching from a nightly version to a stable or
319         // semi-stable version or vice versa.
320         //
321         // Disable automatic updates when switching to nightlies,
322         // restore the default action when switching to stable.
323         if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
324             addon.applyBackgroundUpdates =
325                 AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE"
326                                                   : "AUTOUPDATE_DEFAULT"];
327     }
328     catch (e) {
329         reportError(e);
330     }
331 }
332
333 function startup(data, reason) {
334     debug("bootstrap: startup " + reasonToString(reason));
335     basePath = data.installPath;
336
337     if (!initialized) {
338         initialized = true;
339
340         debug("bootstrap: init " + data.id);
341
342         addonData = data;
343         addon = data;
344         name = data.id.replace(/@.*/, "");
345         AddonManager.getAddonByID(addon.id, function (a) {
346             addon = a;
347
348             updateVersion();
349             if (typeof require !== "undefined")
350                 require("main", global);
351         });
352
353         if (basePath.isDirectory())
354             getURI = function getURI(path) {
355                 let uri = Services.io.newFileURI(basePath);
356                 uri.path += path;
357                 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
358             };
359         else
360             getURI = function getURI(path)
361                 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec.replace(/!/g, "%21") + "!" +
362                                    "/" + path, null, null);
363
364         try {
365             init();
366         }
367         catch (e) {
368             reportError(e);
369         }
370     }
371 }
372
373 /**
374  * An XPCOM class factory proxy. Loads the JavaScript module at *url*
375  * when an instance is to be created and calls its NSGetFactory method
376  * to obtain the actual factory.
377  *
378  * @param {string} url The URL of the module housing the real factory.
379  * @param {string} classID The CID of the class this factory represents.
380  */
381 function FactoryProxy(url, classID, contractID) {
382     this.url = url;
383     this.classID = Components.ID(classID);
384     this.contractID = contractID;
385 }
386 FactoryProxy.prototype = {
387     QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
388     register: function () {
389         debug("bootstrap: register: " + this.classID + " " + this.contractID);
390
391         JSMLoader.registerFactory(this);
392     },
393     get module() {
394         debug("bootstrap: create module: " + this.contractID);
395
396         Object.defineProperty(this, "module", { value: {}, enumerable: true });
397         JSMLoader.load(this.url, this.module);
398         return this.module;
399     },
400     createInstance: function (iids) {
401         return let (factory = this.module.NSGetFactory(this.classID))
402             factory.createInstance.apply(factory, arguments);
403     }
404 }
405
406 var timer;
407 function shutdown(data, reason) {
408     let strReason = reasonToString(reason);
409     debug("bootstrap: shutdown " + strReason);
410
411     if (reason != APP_SHUTDOWN) {
412         if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
413             Services.obs.notifyObservers(null, "dactyl-purge", null);
414
415         Services.obs.notifyObservers(null, "dactyl-cleanup", strReason);
416         Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
417
418         JSMLoader.atexit(strReason);
419         JSMLoader.cleanup(strReason);
420
421         for each (let [category, entry] in JSMLoader.config.categories)
422             categoryManager.deleteCategoryEntry(category, entry, false);
423         for (let resource in JSMLoader.config.resources)
424             resourceProto.setSubstitution(resource, null);
425
426         timer = Timer(() => {
427             bootstrap_jsm.require = null;
428             if (JSMLoader.SANDBOX)
429                 Cu.nukeSandbox(bootstrap);
430             else
431                 Cu.unload(BOOTSTRAP);
432             bootstrap = null;
433             bootstrap_jsm = null;
434         }, 5000, Ci.nsITimer.TYPE_ONE_SHOT);
435     }
436 }
437
438 function uninstall(data, reason) {
439     debug("bootstrap: uninstall " + reasonToString(reason));
440     if (reason == ADDON_UNINSTALL) {
441         Services.prefs.deleteBranch("extensions.dactyl.");
442
443         if (BOOTSTRAP_CONTRACT in Cc) {
444             let service = Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject;
445             manager.unregisterFactory(service.classID, service);
446         }
447     }
448 }
449
450 function reasonToString(reason) {
451     for each (let name in ["disable", "downgrade", "enable",
452                            "install", "shutdown", "startup",
453                            "uninstall", "upgrade"])
454         if (reason == global["ADDON_" + name.toUpperCase()] ||
455             reason == global["APP_" + name.toUpperCase()])
456             return name;
457 }
458
459 function install(data, reason) { debug("bootstrap: install " + reasonToString(reason)); }
460
461 // vim: set fdm=marker sw=4 ts=4 et: