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
9 const NAME = "bootstrap";
12 const Cc = Components.classes;
13 const Ci = Components.interfaces;
14 const Cu = Components.utils;
15 const Cr = Components.results;
17 function module(uri) {
23 const { AddonManager } = module("resource://gre/modules/AddonManager.jsm");
24 const { XPCOMUtils } = module("resource://gre/modules/XPCOMUtils.jsm");
25 const { Services } = module("resource://gre/modules/Services.jsm");
27 const resourceProto = Services.io.getProtocolHandler("resource")
28 .QueryInterface(Ci.nsIResProtocolHandler);
29 const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
30 const manager = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
32 const BOOTSTRAP_JSM = "resource://dactyl/bootstrap.jsm";
34 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
35 JSMLoader = JSMLoader || BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
37 var JSMLoader = BOOTSTRAP_CONTRACT in Components.classes &&
38 Components.classes[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
40 // Temporary migration code.
41 if (!JSMLoader && "@mozilla.org/fuel/application;1" in Components.classes)
42 JSMLoader = Components.classes["@mozilla.org/fuel/application;1"]
43 .getService(Components.interfaces.extIApplication)
44 .storage.get("dactyl.JSMLoader", null);
46 function reportError(e) {
47 dump("\ndactyl: bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
51 function httpGet(url) {
52 let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
53 xmlhttp.overrideMimeType("text/plain");
54 xmlhttp.open("GET", url, false);
59 let initialized = false;
68 function updateVersion() {
70 function isDev(ver) /^hg|pre$/.test(ver);
71 if (typeof require === "undefined" || addon === addonData)
74 require(global, "config");
75 require(global, "prefs");
76 config.lastVersion = localPrefs.get("lastVersion", null);
78 localPrefs.set("lastVersion", addon.version);
80 if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
81 addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
88 function startup(data, reason) {
89 dump("dactyl: bootstrap: startup " + reasonToString(reason) + "\n");
90 basePath = data.installPath;
95 dump("dactyl: bootstrap: init" + " " + data.id + "\n");
99 AddonManager.getAddonByID(addon.id, function (a) {
104 if (basePath.isDirectory())
105 getURI = function getURI(path) {
106 let uri = Services.io.newFileURI(basePath);
108 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
111 getURI = function getURI(path)
112 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec + "!/" + path, null, null);
123 function FactoryProxy(url, classID) {
125 this.classID = Components.ID(classID);
127 FactoryProxy.prototype = {
128 QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
129 register: function () {
130 dump("dactyl: bootstrap: register: " + this.classID + " " + this.contractID + "\n");
132 JSMLoader.registerFactory(this);
135 dump("dactyl: bootstrap: create module: " + this.contractID + "\n");
137 Object.defineProperty(this, "module", { value: {}, enumerable: true });
138 JSMLoader.load(this.url, this.module);
141 createInstance: function (iids) {
142 return let (factory = this.module.NSGetFactory(this.classID))
143 factory.createInstance.apply(factory, arguments);
148 dump("dactyl: bootstrap: init\n");
150 let manifestURI = getURI("chrome.manifest");
151 let manifest = httpGet(manifestURI.spec)
153 .replace(/^\s*|\s*$|#.*/g, "")
154 .replace(/^\s*\n/gm, "");
157 let chars = "0123456789abcdefghijklmnopqrstuv";
158 for (let n = Date.now(); n; n = Math.round(n / chars.length))
159 suffix += chars[n % chars.length];
161 for each (let line in manifest.split("\n")) {
162 let fields = line.split(/\s+/);
165 categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
166 categories.push([fields[1], fields[2]]);
169 components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
172 components[fields[2]].contractID = fields[1];
176 var hardSuffix = /^[^\/]*/.exec(fields[2])[0];
178 resources.push(fields[1], fields[1] + suffix);
179 resourceProto.setSubstitution(fields[1], getURI(fields[2]));
180 resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
184 // Flush the cache if necessary, just to be paranoid
185 let pref = "extensions.dactyl.cacheFlushCheck";
186 let val = addon.version + "-" + hardSuffix;
187 if (!Services.prefs.prefHasUserValue(pref) || Services.prefs.getCharPref(pref) != val) {
188 Services.obs.notifyObservers(null, "startupcache-invalidate", "");
189 Services.prefs.setCharPref(pref, val);
193 module("resource://dactyl-content/disable-acr.jsm").init(addon.id);
201 Cu.unload(BOOTSTRAP_JSM);
202 for (let [name] in Iterator(JSMLoader.globals))
203 Cu.unload(~name.indexOf(":") ? name : "resource://dactyl" + JSMLoader.suffix + "/" + name);
205 else if (JSMLoader.bump != 5) // Temporary hack
206 Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
207 Cu.import(BOOTSTRAP_JSM, global));
210 if (!JSMLoader || JSMLoader.bump !== 5 || Cu.unload)
211 Cu.import(BOOTSTRAP_JSM, global);
213 JSMLoader.bootstrap = this;
215 JSMLoader.load(BOOTSTRAP_JSM, global);
217 JSMLoader.init(suffix);
218 JSMLoader.load("base.jsm", global);
220 if (!(BOOTSTRAP_CONTRACT in Cc))
221 manager.registerFactory(Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
222 String("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
223 BOOTSTRAP_CONTRACT, {
224 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
226 QueryInterface: XPCOMUtils.generateQI([]),
227 contractID: BOOTSTRAP_CONTRACT,
230 createInstance: function () this.instance
233 Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
235 for each (let component in components)
236 component.register();
238 Services.obs.notifyObservers(null, "dactyl-rehash", null);
240 require(global, "overlay");
243 function shutdown(data, reason) {
244 dump("dactyl: bootstrap: shutdown " + reasonToString(reason) + "\n");
245 if (reason != APP_SHUTDOWN) {
247 module("resource://dactyl-content/disable-acr.jsm").cleanup();
253 if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
254 Services.obs.notifyObservers(null, "dactyl-purge", null);
256 Services.obs.notifyObservers(null, "dactyl-cleanup", reasonToString(reason));
257 Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
260 for each (let [category, entry] in categories)
261 categoryManager.deleteCategoryEntry(category, entry, false);
262 for each (let resource in resources)
263 resourceProto.setSubstitution(resource, null);
267 function reasonToString(reason) {
268 for each (let name in ["disable", "downgrade", "enable",
269 "install", "shutdown", "startup",
270 "uninstall", "upgrade"])
271 if (reason == global["ADDON_" + name.toUpperCase()] ||
272 reason == global["APP_" + name.toUpperCase()])
276 function install(data, reason) { dump("dactyl: bootstrap: install " + reasonToString(reason) + "\n"); }
277 function uninstall(data, reason) { dump("dactyl: bootstrap: uninstall " + reasonToString(reason) + "\n"); }
279 // vim: set fdm=marker sw=4 ts=4 et: