]> git.donarmstrong.com Git - dactyl.git/blob - common/bootstrap.js
Import 1.0rc1 supporting Firefox up to 11.*
[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             var hardSuffix = /^[^\/]*/.exec(fields[2])[0];
199
200             resources.push(fields[1], fields[1] + suffix);
201             resourceProto.setSubstitution(fields[1], getURI(fields[2]));
202             resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
203         }
204     }
205
206     // Flush the cache if necessary, just to be paranoid
207     let pref = "extensions.dactyl.cacheFlushCheck";
208     let val  = addon.version + "-" + hardSuffix;
209     if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
210         var cacheFlush = true;
211         Services.obs.notifyObservers(null, "startupcache-invalidate", "");
212         Services.prefs.setCharPref(pref, val);
213     }
214
215     try {
216         module(DISABLE_ACR).init(addon.id);
217     }
218     catch (e) {
219         reportError(e);
220     }
221
222     if (JSMLoader) {
223         // Temporary hacks until platforms and dactyl releases that don't
224         // support Cu.unload are phased out.
225         if (Cu.unload) {
226             // Upgrading from dactyl release without Cu.unload support.
227             Cu.unload(BOOTSTRAP_JSM);
228             for (let [name] in Iterator(JSMLoader.globals))
229                 Cu.unload(~name.indexOf(":") ? name : "resource://dactyl" + JSMLoader.suffix + "/" + name);
230         }
231         else if (JSMLoader.bump != 6) {
232             // We're in a version without Cu.unload support and the
233             // JSMLoader interface has changed. Bump off the old one.
234             Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
235                 Cu.import(BOOTSTRAP_JSM, global));
236         }
237     }
238
239     if (!JSMLoader || JSMLoader.bump !== 6 || Cu.unload)
240         Cu.import(BOOTSTRAP_JSM, global);
241
242     JSMLoader.name = name;
243     JSMLoader.bootstrap = this;
244
245     JSMLoader.load(BOOTSTRAP_JSM, global);
246
247     JSMLoader.init(suffix);
248     JSMLoader.cacheFlush = cacheFlush;
249     JSMLoader.load("base.jsm", global);
250
251     if (!(BOOTSTRAP_CONTRACT in Cc)) {
252         // Use Sandbox to prevent closures over this scope
253         let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].getService());
254         let factory = Cu.evalInSandbox("({ createInstance: function () this })", sandbox);
255
256         factory.classID         = Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}");
257         factory.contractID      = BOOTSTRAP_CONTRACT;
258         factory.QueryInterface  = XPCOMUtils.generateQI([Ci.nsIFactory]);
259         factory.wrappedJSObject = factory;
260
261         manager.registerFactory(factory.classID, String(factory.classID),
262                                 BOOTSTRAP_CONTRACT, factory);
263     }
264
265     Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
266
267     for each (let component in components)
268         component.register();
269
270     Services.obs.notifyObservers(null, "dactyl-rehash", null);
271     updateVersion();
272
273     updateLoader();
274     if (addon !== addonData)
275         require(global, "main");
276 }
277
278 function shutdown(data, reason) {
279     debug("bootstrap: shutdown " + reasonToString(reason));
280     if (reason != APP_SHUTDOWN) {
281         try {
282             module(DISABLE_ACR).cleanup();
283             if (Cu.unload)
284                 Cu.unload(DISABLE_ACR);
285         }
286         catch (e) {
287             reportError(e);
288         }
289
290         if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
291             Services.obs.notifyObservers(null, "dactyl-purge", null);
292
293         Services.obs.notifyObservers(null, "dactyl-cleanup", reasonToString(reason));
294         Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
295
296         JSMLoader.purge();
297         for each (let [category, entry] in categories)
298             categoryManager.deleteCategoryEntry(category, entry, false);
299         for each (let resource in resources)
300             resourceProto.setSubstitution(resource, null);
301     }
302 }
303
304 function uninstall(data, reason) {
305     debug("bootstrap: uninstall " + reasonToString(reason));
306     if (reason == ADDON_UNINSTALL) {
307         Services.prefs.deleteBranch("extensions.dactyl.");
308
309         if (BOOTSTRAP_CONTRACT in Cc) {
310             let service = Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject;
311             manager.unregisterFactory(service.classID, service);
312         }
313     }
314 }
315
316 function reasonToString(reason) {
317     for each (let name in ["disable", "downgrade", "enable",
318                            "install", "shutdown", "startup",
319                            "uninstall", "upgrade"])
320         if (reason == global["ADDON_" + name.toUpperCase()] ||
321             reason == global["APP_" + name.toUpperCase()])
322             return name;
323 }
324
325 function install(data, reason) { debug("bootstrap: install " + reasonToString(reason)); }
326
327 // vim: set fdm=marker sw=4 ts=4 et: