1 // Copyright (c) 2010-2011 by Kris Maglione <maglione.k@gmail.com>
3 // This work is licensed for reuse under an MIT license. Details are
4 // given in the LICENSE.txt file included with this file.
6 // See https://wiki.mozilla.org/Extension_Manager:Bootstrapped_Extensions
12 var { classes: Cc, interfaces: Ci, results: Cr, utils: Cu } = Components;
14 function module(uri) Cu.import(uri, {});
18 __defineGetter__("BOOTSTRAP", () => "resource://" + moduleName + "/bootstrap.jsm");
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");
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);
29 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
33 function reportError(e) {
34 let stack = e.stack || Error().stack;
35 dump("\n" + name + ": bootstrap: " + e + "\n" + stack + "\n");
37 Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
38 .logStringMessage(stack);
40 function debug(...args) {
42 dump(name + ": " + args.join(", ") + "\n");
45 function httpGet(uri) {
46 let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
47 xmlhttp.overrideMimeType("text/plain");
48 xmlhttp.open("GET", uri.spec || uri, false);
54 let initialized = false;
66 SANDBOX: Cu.nukeSandbox && false,
76 get module() moduleName,
83 add: function add(major, minor, delta) {
86 this[major] = (this[major] || 0) + delta;
89 this[minor] = (this[minor] || 0) + delta;
90 this[major + minor] = (this[major + minor] || 0) + delta;
93 clear: function clear() {
95 if (typeof this[key] !== "number")
100 getTarget: function getTarget(url) {
101 let uri = Services.io.newURI(url, null, null);
102 if (uri.schemeIs("resource"))
103 return resourceProto.resolveURI(uri);
105 let chan = Services.io.newChannelFromURI(uri);
106 try { chan.cancel(Cr.NS_BINDING_ABORTED); } catch (e) {}
112 atexit: function atexit(arg, self) {
113 if (typeof arg !== "string")
114 this._atexit.push(arguments);
116 for each (let [fn, self] in this._atexit)
125 _load: function _load(name, target) {
127 if (name.indexOf(":") === -1)
128 urls = this.config["module-paths"].map(path => path + name + ".jsm");
130 for each (let url in urls)
132 var uri = this.getTarget(url);
133 if (uri in this.globals)
134 return this.modules[name] = this.globals[uri];
136 this.globals[uri] = this.modules[name];
137 bootstrap_jsm.loadSubScript(url, this.modules[name]);
141 debug("Loading " + name + ": " + e);
142 delete this.globals[uri];
144 if (typeof e != "string")
148 throw Error("No such module: " + name);
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 });
156 let currentModule = this.currentModule;
157 this.currentModule = this.modules[name];
160 this._load(name, this.modules[name]);
163 delete this.modules[name];
168 this.currentModule = currentModule;
172 let module = this.modules[name];
174 for each (let symbol in module.EXPORTED_SYMBOLS)
175 target[symbol] = module[symbol];
180 // Cuts down on stupid, fscking url mangling.
181 get loadSubScript() bootstrap_jsm.loadSubScript,
183 cleanup: function unregister() {
184 for each (let factory in this.factories.splice(0))
185 manager.unregisterFactory(factory.classID, factory);
188 Factory: function Factory(class_) ({
189 __proto__: class_.prototype,
191 createInstance: function (outer, iid) {
194 throw Cr.NS_ERROR_NO_AGGREGATION;
195 if (!class_.instance)
196 class_.instance = new class_();
197 return class_.instance.QueryInterface(iid);
206 registerFactory: function registerFactory(factory) {
207 manager.registerFactory(factory.classID,
208 String(factory.classID),
211 this.factories.push(factory);
216 debug("bootstrap: init");
218 let manifestURI = getURI("chrome.manifest");
219 let manifest = httpGet(manifestURI)
221 .replace(/#(resource)#/g, "$1")
222 .replace(/^\s*|\s*$|#.*/g, "")
223 .replace(/^\s*\n/gm, "");
225 for each (let line in manifest.split("\n")) {
226 let fields = line.split(/\s+/);
229 categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
230 categories.push([fields[1], fields[2]]);
233 components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
236 components[fields[2]].contractID = fields[1];
240 moduleName = moduleName || fields[1];
241 resources.push(fields[1]);
242 resourceProto.setSubstitution(fields[1], getURI(fields[2]));
246 JSMLoader.config = JSON.parse(httpGet("resource://dactyl-local/config.json").responseText);
248 bootstrap_jsm = module(BOOTSTRAP);
249 if (!JSMLoader.SANDBOX)
250 bootstrap = bootstrap_jsm;
252 bootstrap = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance(),
253 { sandboxName: BOOTSTRAP });
254 Services.scriptloader.loadSubScript(BOOTSTRAP, bootstrap);
256 bootstrap.require = JSMLoader.load("base").require;
258 // Flush the cache if necessary, just to be paranoid
259 let pref = "extensions.dactyl.cacheFlushCheck";
260 let val = addon.version;
261 if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
262 var cacheFlush = true;
263 Services.obs.notifyObservers(null, "startupcache-invalidate", "");
264 Services.prefs.setCharPref(pref, val);
268 //JSMLoader.load("disable-acr").init(addon.id);
274 Services.obs.notifyObservers(null, "dactyl-rehash", null);
276 JSMLoader.bootstrap = global;
278 JSMLoader.load("config", global);
279 JSMLoader.load("main", global);
281 JSMLoader.cacheFlush = cacheFlush;
282 JSMLoader.load("base", global);
284 if (!(BOOTSTRAP_CONTRACT in Cc)) {
285 // Use Sandbox to prevent closures over this scope
286 let sandbox = Cu.Sandbox(Cc["@mozilla.org/systemprincipal;1"].createInstance());
287 let factory = Cu.evalInSandbox("({ createInstance: function () this })", sandbox);
289 factory.classID = Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}");
290 factory.contractID = BOOTSTRAP_CONTRACT;
291 factory.QueryInterface = XPCOMUtils.generateQI([Ci.nsIFactory]);
292 factory.wrappedJSObject = factory;
294 manager.registerFactory(factory.classID, String(factory.classID),
295 BOOTSTRAP_CONTRACT, factory);
298 Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
300 for each (let component in components)
301 component.register();
305 if (addon !== addonData)
306 require("main", global);
310 * Performs necessary migrations after a version change.
312 function updateVersion() {
313 function isDev(ver) /^hg|pre$/.test(ver);
315 if (typeof require === "undefined" || addon === addonData)
318 JSMLoader.load("prefs", global);
319 config.lastVersion = localPrefs.get("lastVersion", null);
321 localPrefs.set("lastVersion", addon.version);
323 // We're switching from a nightly version to a stable or
324 // semi-stable version or vice versa.
326 // Disable automatic updates when switching to nightlies,
327 // restore the default action when switching to stable.
328 if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
329 addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
336 function startup(data, reason) {
337 debug("bootstrap: startup " + reasonToString(reason));
338 basePath = data.installPath;
343 debug("bootstrap: init " + data.id);
347 name = data.id.replace(/@.*/, "");
348 AddonManager.getAddonByID(addon.id, function (a) {
352 if (typeof require !== "undefined")
353 require("main", global);
356 if (basePath.isDirectory())
357 getURI = function getURI(path) {
358 let uri = Services.io.newFileURI(basePath);
360 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
363 getURI = function getURI(path)
364 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec.replace(/!/g, "%21") + "!" +
365 "/" + path, null, null);
377 * An XPCOM class factory proxy. Loads the JavaScript module at *url*
378 * when an instance is to be created and calls its NSGetFactory method
379 * to obtain the actual factory.
381 * @param {string} url The URL of the module housing the real factory.
382 * @param {string} classID The CID of the class this factory represents.
384 function FactoryProxy(url, classID) {
386 this.classID = Components.ID(classID);
388 FactoryProxy.prototype = {
389 QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
390 register: function () {
391 debug("bootstrap: register: " + this.classID + " " + this.contractID);
393 JSMLoader.registerFactory(this);
396 debug("bootstrap: create module: " + this.contractID);
398 Object.defineProperty(this, "module", { value: {}, enumerable: true });
399 JSMLoader.load(this.url, this.module);
402 createInstance: function (iids) {
403 return let (factory = this.module.NSGetFactory(this.classID))
404 factory.createInstance.apply(factory, arguments);
408 function shutdown(data, reason) {
409 let strReason = reasonToString(reason);
410 debug("bootstrap: shutdown " + strReason);
412 if (reason != APP_SHUTDOWN) {
414 //JSMLoader.load("disable-acr").cleanup(addon.id);
420 if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
421 Services.obs.notifyObservers(null, "dactyl-purge", null);
423 Services.obs.notifyObservers(null, "dactyl-cleanup", strReason);
424 Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
426 JSMLoader.atexit(strReason);
427 JSMLoader.cleanup(strReason);
429 if (JSMLoader.SANDBOX)
430 Cu.nukeSandbox(bootstrap);
431 bootstrap_jsm.require = null;
432 Cu.unload(BOOTSTRAP);
434 bootstrap_jsm = null;
436 for each (let [category, entry] in categories)
437 categoryManager.deleteCategoryEntry(category, entry, false);
438 for each (let resource in resources)
439 resourceProto.setSubstitution(resource, null);
443 function uninstall(data, reason) {
444 debug("bootstrap: uninstall " + reasonToString(reason));
445 if (reason == ADDON_UNINSTALL) {
446 Services.prefs.deleteBranch("extensions.dactyl.");
448 if (BOOTSTRAP_CONTRACT in Cc) {
449 let service = Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject;
450 manager.unregisterFactory(service.classID, service);
455 function reasonToString(reason) {
456 for each (let name in ["disable", "downgrade", "enable",
457 "install", "shutdown", "startup",
458 "uninstall", "upgrade"])
459 if (reason == global["ADDON_" + name.toUpperCase()] ||
460 reason == global["APP_" + name.toUpperCase()])
464 function install(data, reason) { debug("bootstrap: install " + reasonToString(reason)); }
466 // vim: set fdm=marker sw=4 ts=4 et: