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_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
33 JSMLoader = JSMLoader || BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
35 var JSMLoader = BOOTSTRAP_CONTRACT in Components.classes &&
36 Components.classes[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
38 // Temporary migration code.
39 if (!JSMLoader && "@mozilla.org/fuel/application;1" in Components.classes)
40 JSMLoader = Components.classes["@mozilla.org/fuel/application;1"]
41 .getService(Components.interfaces.extIApplication)
42 .storage.get("dactyl.JSMLoader", null);
45 function reportError(e) {
46 dump("\ndactyl: bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
50 function httpGet(url) {
51 let xmlhttp = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest);
52 xmlhttp.overrideMimeType("text/plain");
53 xmlhttp.open("GET", url, false);
58 let initialized = false;
67 function updateVersion() {
69 function isDev(ver) /^hg|pre$/.test(ver);
70 if (typeof require === "undefined" || addon === addonData)
73 require(global, "config");
74 require(global, "prefs");
75 config.lastVersion = localPrefs.get("lastVersion", null);
77 localPrefs.set("lastVersion", addon.version);
79 if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
80 addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
87 function startup(data, reason) {
88 dump("dactyl: bootstrap: startup " + reasonToString(reason) + "\n");
89 basePath = data.installPath;
94 dump("dactyl: bootstrap: init" + " " + data.id + "\n");
98 AddonManager.getAddonByID(addon.id, function (a) {
103 if (basePath.isDirectory())
104 getURI = function getURI(path) {
105 let uri = Services.io.newFileURI(basePath);
107 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
110 getURI = function getURI(path)
111 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec + "!/" + path, null, null);
122 function FactoryProxy(url, classID) {
124 this.classID = Components.ID(classID);
126 FactoryProxy.prototype = {
127 QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
128 register: function () {
129 dump("dactyl: bootstrap: register: " + this.classID + " " + this.contractID + "\n");
131 JSMLoader.registerFactory(this);
134 dump("dactyl: bootstrap: create module: " + this.contractID + "\n");
136 Object.defineProperty(this, "module", { value: {}, enumerable: true });
137 JSMLoader.load(this.url, this.module);
140 createInstance: function (iids) {
141 return let (factory = this.module.NSGetFactory(this.classID))
142 factory.createInstance.apply(factory, arguments);
147 dump("dactyl: bootstrap: init\n");
149 let manifestURI = getURI("chrome.manifest");
150 let manifest = httpGet(manifestURI.spec)
152 .replace(/^\s*|\s*$|#.*/g, "")
153 .replace(/^\s*\n/gm, "");
156 let chars = "0123456789abcdefghijklmnopqrstuv";
157 for (let n = Date.now(); n; n = Math.round(n / chars.length))
158 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 resources.push(fields[1], fields[1] + suffix);
177 resourceProto.setSubstitution(fields[1], getURI(fields[2]));
178 resourceProto.setSubstitution(fields[1] + suffix, getURI(fields[2]));
183 module("resource://dactyl-content/disable-acr.jsm").init(addon.id);
189 if (JSMLoader && JSMLoader.bump !== 4) // Temporary hack
190 Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
191 Cu.import("resource://dactyl/bootstrap.jsm", global));
193 if (!JSMLoader || JSMLoader.bump !== 4)
194 Cu.import("resource://dactyl/bootstrap.jsm", global);
196 JSMLoader.bootstrap = this;
198 JSMLoader.load("resource://dactyl/bootstrap.jsm", global);
200 JSMLoader.init(suffix);
201 JSMLoader.load("base.jsm", global);
203 if (!(BOOTSTRAP_CONTRACT in Cc))
204 manager.registerFactory(Components.ID("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
205 String("{f541c8b0-fe26-4621-a30b-e77d21721fb5}"),
206 BOOTSTRAP_CONTRACT, {
207 QueryInterface: XPCOMUtils.generateQI([Ci.nsIFactory]),
209 QueryInterface: XPCOMUtils.generateQI([]),
210 contractID: BOOTSTRAP_CONTRACT,
213 createInstance: function () this.instance
216 Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = JSMLoader;
218 for each (let component in components)
219 component.register();
221 Services.obs.notifyObservers(null, "dactyl-rehash", null);
223 require(global, "overlay");
226 function shutdown(data, reason) {
227 dump("dactyl: bootstrap: shutdown " + reasonToString(reason) + "\n");
228 if (reason != APP_SHUTDOWN) {
230 module("resource://dactyl-content/disable-acr.jsm").cleanup();
236 if ([ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason) >= 0)
237 Services.obs.notifyObservers(null, "dactyl-purge", null);
239 Services.obs.notifyObservers(null, "dactyl-cleanup", null);
240 Services.obs.notifyObservers(null, "dactyl-cleanup-modules", null);
243 for each (let [category, entry] in categories)
244 categoryManager.deleteCategoryEntry(category, entry, false);
245 for each (let resource in resources)
246 resourceProto.setSubstitution(resource, null);
250 function reasonToString(reason) {
251 for each (let name in ["disable", "downgrade", "enable",
252 "install", "shutdown", "startup",
253 "uninstall", "upgrade"])
254 if (reason == global["ADDON_" + name.toUpperCase()] ||
255 reason == global["APP_" + name.toUpperCase()])
259 function install(data, reason) { dump("dactyl: bootstrap: install " + reasonToString(reason) + "\n"); }
260 function uninstall(data, reason) { dump("dactyl: bootstrap: uninstall " + reasonToString(reason) + "\n"); }
262 // vim: set fdm=marker sw=4 ts=4 et: