]> git.donarmstrong.com Git - dactyl.git/blob - common/bootstrap.js
Import 1.0b7.1 supporting Firefox up to 8.*
[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 const Cc = Components.classes;
13 const Ci = Components.interfaces;
14 const Cu = Components.utils;
15 const Cr = Components.results;
16
17 function module(uri) {
18     let obj = {};
19     Cu.import(uri, obj);
20     return obj;
21 }
22
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");
26
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);
31
32 const BOOTSTRAP_JSM = "resource://dactyl/bootstrap.jsm";
33
34 const BOOTSTRAP_CONTRACT = "@dactyl.googlecode.com/base/bootstrap";
35 JSMLoader = JSMLoader || BOOTSTRAP_CONTRACT in Cc && Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
36
37 var JSMLoader = BOOTSTRAP_CONTRACT in Components.classes &&
38     Components.classes[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader;
39
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);
45
46 function reportError(e) {
47     dump("\ndactyl: bootstrap: " + e + "\n" + (e.stack || Error().stack) + "\n");
48     Cu.reportError(e);
49 }
50
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);
55     xmlhttp.send(null);
56     return xmlhttp;
57 }
58
59 let initialized = false;
60 let addon = null;
61 let addonData = null;
62 let basePath = null;
63 let categories = [];
64 let components = {};
65 let resources = [];
66 let getURI = null;
67
68 function updateVersion() {
69     try {
70         function isDev(ver) /^hg|pre$/.test(ver);
71         if (typeof require === "undefined" || addon === addonData)
72             return;
73
74         require(global, "config");
75         require(global, "prefs");
76         config.lastVersion = localPrefs.get("lastVersion", null);
77
78         localPrefs.set("lastVersion", addon.version);
79
80         if (!config.lastVersion || isDev(config.lastVersion) != isDev(addon.version))
81             addon.applyBackgroundUpdates = AddonManager[isDev(addon.version) ? "AUTOUPDATE_DISABLE" : "AUTOUPDATE_DEFAULT"];
82     }
83     catch (e) {
84         reportError(e);
85     }
86 }
87
88 function startup(data, reason) {
89     dump("dactyl: bootstrap: startup " + reasonToString(reason) + "\n");
90     basePath = data.installPath;
91
92     if (!initialized) {
93         initialized = true;
94
95         dump("dactyl: bootstrap: init" + " " + data.id + "\n");
96
97         addonData = data;
98         addon = data;
99         AddonManager.getAddonByID(addon.id, function (a) {
100             addon = a;
101             updateVersion();
102         });
103
104         if (basePath.isDirectory())
105             getURI = function getURI(path) {
106                 let uri = Services.io.newFileURI(basePath);
107                 uri.path += path;
108                 return Services.io.newFileURI(uri.QueryInterface(Ci.nsIFileURL).file);
109             };
110         else
111             getURI = function getURI(path)
112                 Services.io.newURI("jar:" + Services.io.newFileURI(basePath).spec + "!/" + path, null, null);
113
114         try {
115             init();
116         }
117         catch (e) {
118             reportError(e);
119         }
120     }
121 }
122
123 function FactoryProxy(url, classID) {
124     this.url = url;
125     this.classID = Components.ID(classID);
126 }
127 FactoryProxy.prototype = {
128     QueryInterface: XPCOMUtils.generateQI(Ci.nsIFactory),
129     register: function () {
130         dump("dactyl: bootstrap: register: " + this.classID + " " + this.contractID + "\n");
131
132         JSMLoader.registerFactory(this);
133     },
134     get module() {
135         dump("dactyl: bootstrap: create module: " + this.contractID + "\n");
136
137         Object.defineProperty(this, "module", { value: {}, enumerable: true });
138         JSMLoader.load(this.url, this.module);
139         return this.module;
140     },
141     createInstance: function (iids) {
142         return let (factory = this.module.NSGetFactory(this.classID))
143             factory.createInstance.apply(factory, arguments);
144     }
145 }
146
147 function init() {
148     dump("dactyl: bootstrap: init\n");
149
150     let manifestURI = getURI("chrome.manifest");
151     let manifest = httpGet(manifestURI.spec)
152             .responseText
153             .replace(/^\s*|\s*$|#.*/g, "")
154             .replace(/^\s*\n/gm, "");
155
156     let suffix = "-";
157     let chars = "0123456789abcdefghijklmnopqrstuv";
158     for (let n = Date.now(); n; n = Math.round(n / chars.length))
159         suffix += chars[n % chars.length];
160
161     for each (let line in manifest.split("\n")) {
162         let fields = line.split(/\s+/);
163         switch(fields[0]) {
164         case "category":
165             categoryManager.addCategoryEntry(fields[1], fields[2], fields[3], false, true);
166             categories.push([fields[1], fields[2]]);
167             break;
168         case "component":
169             components[fields[1]] = new FactoryProxy(getURI(fields[2]).spec, fields[1]);
170             break;
171         case "contract":
172             components[fields[2]].contractID = fields[1];
173             break;
174
175         case "resource":
176             var hardSuffix = /^[^\/]*/.exec(fields[2])[0];
177
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]));
181         }
182     }
183
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);
190     }
191
192     try {
193         module("resource://dactyl-content/disable-acr.jsm").init(addon.id);
194     }
195     catch (e) {
196         reportError(e);
197     }
198
199     if (JSMLoader) {
200         if (Cu.unload) {
201             Cu.unload(BOOTSTRAP_JSM);
202             for (let [name] in Iterator(JSMLoader.globals))
203                 Cu.unload(~name.indexOf(":") ? name : "resource://dactyl" + JSMLoader.suffix + "/" + name);
204         }
205         else if (JSMLoader.bump != 5) // Temporary hack
206             Services.scriptloader.loadSubScript("resource://dactyl" + suffix + "/bootstrap.jsm",
207                 Cu.import(BOOTSTRAP_JSM, global));
208     }
209
210     if (!JSMLoader || JSMLoader.bump !== 5 || Cu.unload)
211         Cu.import(BOOTSTRAP_JSM, global);
212
213     JSMLoader.bootstrap = this;
214
215     JSMLoader.load(BOOTSTRAP_JSM, global);
216
217     JSMLoader.init(suffix);
218     JSMLoader.load("base.jsm", global);
219
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]),
225             instance: {
226                 QueryInterface: XPCOMUtils.generateQI([]),
227                 contractID: BOOTSTRAP_CONTRACT,
228                 wrappedJSObject: {}
229             },
230             createInstance: function () this.instance
231         });
232
233     Cc[BOOTSTRAP_CONTRACT].getService().wrappedJSObject.loader = !Cu.unload && JSMLoader;
234
235     for each (let component in components)
236         component.register();
237
238     Services.obs.notifyObservers(null, "dactyl-rehash", null);
239     updateVersion();
240     require(global, "overlay");
241 }
242
243 function shutdown(data, reason) {
244     dump("dactyl: bootstrap: shutdown " + reasonToString(reason) + "\n");
245     if (reason != APP_SHUTDOWN) {
246         try {
247             module("resource://dactyl-content/disable-acr.jsm").cleanup();
248         }
249         catch (e) {
250             reportError(e);
251         }
252
253         if (~[ADDON_UPGRADE, ADDON_DOWNGRADE, ADDON_UNINSTALL].indexOf(reason))
254             Services.obs.notifyObservers(null, "dactyl-purge", null);
255
256         Services.obs.notifyObservers(null, "dactyl-cleanup", reasonToString(reason));
257         Services.obs.notifyObservers(null, "dactyl-cleanup-modules", reasonToString(reason));
258
259         JSMLoader.purge();
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);
264     }
265 }
266
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()])
273             return name;
274 }
275
276 function install(data, reason) { dump("dactyl: bootstrap: install " + reasonToString(reason) + "\n"); }
277 function uninstall(data, reason) { dump("dactyl: bootstrap: uninstall " + reasonToString(reason) + "\n"); }
278
279 // vim: set fdm=marker sw=4 ts=4 et: