]> git.donarmstrong.com Git - dactyl.git/blob - common/bootstrap.js
Import 1.0 supporting Firefox up to 14.*
[dactyl.git] / common / bootstrap.js
1 // Copyright (c) 2010-2011 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
9 const NAME = "bootstrap";
10 const global = this;
11
12 var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
13
14 function module(uri) {
15     let obj = {};
16     Cu.import(uri, obj);
17     return obj;
18 }
19
20 const { AddonManager } = module("resource://gre/modules/AddonManager.jsm");
21 const { XPCOMUtils }   = module("resource://gre/modules/XPCOMUtils.jsm");
22 const { Services }     = module("resource://gre/modules/Services.jsm");
23
24 const resourceProto = Services.io.getProtocolHandler("resource")
25                               .QueryInterface(Ci.nsIResProtocolHandler);
26 const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
27 const manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
28
29 const DISABLE_ACR        = "resource://dactyl-content/disable-acr.jsm";
30 const BOOTSTRAP_JSM      = "resource://dactyl/bootstrap.jsm";
31 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
32
33 var JSMLoader = BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
34 var name = "dactyl";
35
36 function reportError(e) {
37     dump("\n" + name + ": bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
38     Cu.reportError(e);
39 }
40 function debug(msg) {
41     dump(name + ": " + msg + "\n");
42 }
43
44 function httpGet(url) {
45     let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
46     xmlhttp.overrideMimeType("text/plain");
47     xmlhttp.open("GET", url, false);
48     xmlhttp.send(null);
49     return xmlhttp;
50 }
51
52 let initialized = false;
53 let addon = null;
54 let addonData = null;
55 let basePath = null;
56 let categories = [];
57 let components = {};
58 let resources = [];
59 let getURI = null;
60
61 function updateLoader() {
62     try {
63         JSMLoader.loader = Cc["@dactyl.googlecode.com/extra/utils"].getService(Ci.dactylIUtils);
64     }
65     catch (e) {};
66 }
67
68 /**
69  * Performs necessary migrations after a version change.
70  */
71 function updateVersion() {
72     try {
73         function isDev(ver) /^hg|pre$/.test(ver);
74         if (typeof require === "undefined" || addon === addonData)
75             return;
76
77         require(global, "config");
78         require(global, "prefs");
79         config.lastVersion = localPrefs.get("lastVersion", null);
80
81         localPrefs.set("lastVersion", addon.version);
82
83         // We're switching from a nightly version to a stable or
84         // semi-stable version or vice versa.
85         //
86         // Disable automatic updates when switching to nightlies,
87         // restore the default action when switching to stable.
88         if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
89             addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
90     }
91     catch (e) {
92         reportError(e);
93     }
94 }
95
96 function startup(data, reason) {
97     debug("bootstrap: startup " + reasonToString(reason));
98     basePath = data.installPath;
99
100     if (!initialized) {
101         initialized = true;
102
103         debug("bootstrap: init" + " " + data.id);
104
105         addonData = data;
106         addon = data;
107         name = data.id.replace(/@.*/, "");
108         AddonManager.getAddonByID(addon.id, function (a) {
109             addon = a;
110
111             updateLoader();
112             updateVersion();
113             if (typeof require !== "undefined")
114                 require(global, "main");
115         });
116
117         if (basePath.isDirectory())
118             getURI = function getURI(path) {
119                 let uri = Services.io.newFileURI(basePath);
120                 uri.path += path;
121                 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
122             };
123         else
124             getURI = function getURI(path)
125                 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec.replace(/!/g, "%21") + "!" +
126                                    "/" + path, null, null);
127
128         try {
129             init();
130         }
131         catch (e) {
132             reportError(e);
133         }
134     }
135 }
136
137 /**
138  * An XPCOM class factory proxy. Loads the JavaScript module at *url*
139  * when an instance is to be created and calls its NSGetFactory method
140  * to obtain the actual factory.
141  *
142  * @param {string} url The URL of the module housing the real factory.
143  * @param {string} classID The CID of the class this factory represents.
144  */
145 function FactoryProxy(url, classID) {
146     this.url = url;
147     this.classID = Components.ID(classID);
148 }
149 FactoryProxy.prototype = {
150     QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
151     register: function () {
152         debug("bootstrap: register: " + this.classID + " " + this.contractID);
153
154         JSMLoader.registerFactory(this);
155     },
156     get module() {
157         debug("bootstrap: create module: " + this.contractID);
158
159         Object.defineProperty(this, "module", { value: {}, enumerable: true });
160         JSMLoader.load(this.url, this.module);
161         return this.module;
162     },
163     createInstance: function (iids) {
164         return let (factory = this.module.NSGetFactory(this.classID))
165             factory.createInstance.apply(factory, arguments);
166     }
167 }
168
169 function init() {
170     debug("bootstrap: init");
171
172     let manifestURI = getURI("chrome.manifest");
173     let manifest = httpGet(manifestURI.spec)
174             .responseText
175             .replace(/^\s*|\s*$|#.*/g, "")
176             .replace(/^\s*\n/gm, "");
177
178     let suffix = "-";
179     let chars = "0123456789abcdefghijklmnopqrstuv";
180     for (let n = Date.now(); n; n = Math.round(n / chars.length))
181         suffix += chars[n % chars.length];
182
183     for each (let line in manifest.split("\n")) {
184         let fields = line.split(/\s+/);
185         switch(fields[0]) {
186         case "category":
187             categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
188             categories.push([fields[1], fields[2]]);
189             break;
190         case "component":
191             components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
192             break;
193         case "contract":
194             components[fields[2]].contractID = fields[1];
195             break;
196
197         case "resource":
198             resources.push(fields[1], fields[1] + suffix);
199             resourceProto.setSubstitution(fields[1], getURI(fields[2]));
200             resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
201         }
202     }
203
204     // Flush the cache if necessary, just to be paranoid
205     let pref = "extensions.dactyl.cacheFlushCheck";
206     let val  = addon.version;
207     if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
208         var cacheFlush = true;
209         Services.obs.notifyObservers(null, "startupcache-invalidate", "");
210         Services.prefs.setCharPref(pref, val);
211     }
212
213     try {
214         module(DISABLE_ACR).init(addon.id);
215     }
216     catch (e) {
217         reportError(e);
218     }
219
220     if (JSMLoader) {
221         // Temporary hacks until platforms and dactyl releases that don't
222         // support Cu.unload are phased out.
223         if (Cu.unload) {
224             // Upgrading from dactyl release without Cu.unload support.
225             Cu.unload(BOOTSTRAP_JSM);
226             for (let [name] in Iterator(JSMLoader.globals))
227                 Cu.unload(~name.indexOf(":") ? name : "resource://dactyl" + JSMLoader.suffix + "/" + name);
228         }
229         else if (JSMLoader.bump != 6) {
230             // We're in a version without Cu.unload support and the
231             // JSMLoader interface has changed. Bump off the old one.
232             Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
233                 Cu.import(BOOTSTRAP_JSM, global));
234         }
235     }
236
237     if (!JSMLoader || JSMLoader.bump !== 6 || Cu.unload)
238         Cu.import(BOOTSTRAP_JSM, global);
239
240     JSMLoader.name = name;
241     JSMLoader.bootstrap = this;
242
243     JSMLoader.load(BOOTSTRAP_JSM, global);
244
245     JSMLoader.init(suffix);
246     JSMLoader.cacheFlush = cacheFlush;
247     JSMLoader.load("base.jsm", global);
248
249     if (!(BOOTSTRAP_CONTRACT in Cc)) {
250         // Use Sandbox to prevent closures over this scope
251         let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].getService());
252         let factory = Cu.evalInSandbox("({ createInstance: function () this })", sandbox);
253
254         factory.classID         = Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}");
255         factory.contractID      = BOOTSTRAP_CONTRACT;
256         factory.QueryInterface  = XPCOMUtils.generateQI([Ci.nsIFactory]);
257         factory.wrappedJSObject = factory;
258
259         manager.registerFactory(factory.classID, String(factory.classID),
260                                 BOOTSTRAP_CONTRACT, factory);
261     }
262
263     Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
264
265     for each (let component in components)
266         component.register();
267
268     Services.obs.notifyObservers(null, "dactyl-rehash", null);
269     updateVersion();
270
271     updateLoader();
272     if (addon !== addonData)
273         require(global, "main");
274 }
275
276 function shutdown(data, reason) {
277     debug("bootstrap: shutdown " + reasonToString(reason));
278     if (reason != APP_SHUTDOWN) {
279         try {
280             module(DISABLE_ACR).cleanup();
281             if (Cu.unload)
282                 Cu.unload(DISABLE_ACR);
283         }
284         catch (e) {
285             reportError(e);
286         }
287
288         if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
289             Services.obs.notifyObservers(null, "dactyl-purge", null);
290
291         Services.obs.notifyObservers(null, "dactyl-cleanup", reasonToString(reason));
292         Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
293
294         JSMLoader.purge();
295         for each (let [category, entry] in categories)
296             categoryManager.deleteCategoryEntry(category, entry, false);
297         for each (let resource in resources)
298             resourceProto.setSubstitution(resource, null);
299     }
300 }
301
302 function uninstall(data, reason) {
303     debug("bootstrap: uninstall " + reasonToString(reason));
304     if (reason == ADDON_UNINSTALL) {
305         Services.prefs.deleteBranch("extensions.dactyl.");
306
307         if (BOOTSTRAP_CONTRACT in Cc) {
308             let service = Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject;
309             manager.unregisterFactory(service.classID, service);
310         }
311     }
312 }
313
314 function reasonToString(reason) {
315     for each (let name in ["disable", "downgrade", "enable",
316                            "install", "shutdown", "startup",
317                            "uninstall", "upgrade"])
318         if (reason == global["ADDON_" + name.toUpperCase()] ||
319             reason == global["APP_" + name.toUpperCase()])
320             return name;
321 }
322
323 function install(data, reason) { debug("bootstrap: install " + reasonToString(reason)); }
324
325 // vim: set fdm=marker sw=4 ts=4 et: