This repository has been archived by the owner on Nov 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbootstrap.js
127 lines (103 loc) · 3.42 KB
/
bootstrap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
const Ci = Components.interfaces;
const Cu = Components.utils;
const Cm = Components.manager.QueryInterface(Ci.nsIComponentRegistrar);
Cu.import("resource://gre/modules/Services.jsm");
const REASON = [ "unknown", "startup", "shutdown", "enable", "disable",
"install", "uninstall", "upgrade", "downgrade" ];
// Usefull piece of code from :bent
// http://mxr.mozilla.org/mozilla-central/source/dom/workers/test/extensions/bootstrap/bootstrap.js
function registerAddonResourceHandler(data) {
let file = data.installPath;
dump("IN registerAddonResourceHandler WITH file=" + file.path);
let fileuri = file.isDirectory() ?
Services.io.newFileURI(file) :
Services.io.newURI("jar:" + file.path + "!/", null, null);
let resourceName = encodeURIComponent(data.id.replace("@", "at"));
Services.io.getProtocolHandler("resource").
QueryInterface(Ci.nsIResProtocolHandler).
setSubstitution(resourceName, fileuri);
return "resource://" + resourceName + "/";
}
let mainModule;
let loader;
let unload;
function install(data, reason) {}
function startup(data, reason) {
for (var p in data) {
let value = null;
if (data[p] && data[p].toString) {
value = "=" + data[p].toString();
} else {
value = " can not be dumped as a string\n";
}
dump("data." + p + value + "\n");
}
let uri = registerAddonResourceHandler(data);
let loaderModule =
Cu.import("resource://gre/modules/commonjs/toolkit/loader.js").Loader;
let { Loader, Require, Main } = loaderModule;
unload = loaderModule.unload;
let loaderOptions = {
paths: {
"./": uri,
"": "resource://gre/modules/commonjs/"
},
modules: {
"toolkit/loader": loaderModule
}
};
/**
* setup a console object that only dumps messages if
* LOGPREF is true
*/
const LOGPREF = "[email protected]";
const LOGPREFIX = "B2G Installer:";
try {
Services.prefs.getBoolPref(LOGPREF);
} catch(e) {
// Doesn't exist yet
Services.prefs.setBoolPref(LOGPREF, false);
}
function canLog() {
return Services.prefs.getBoolPref(LOGPREF);
}
// In Firefox 44 and later, many DevTools modules were relocated.
// See https://bugzil.la/912121
const { ConsoleAPI } = Cu.import("resource://gre/modules/Console.jsm");
let _console = new ConsoleAPI();
loaderOptions.globals = {
console: {
log: function(...args) {
canLog() && _console.log(LOGPREFIX, ...args);
},
warn: function(...args) {
canLog() && _console.warn(LOGPREFIX, ...args);
},
error: function(...args) {
canLog() && _console.error(LOGPREFIX, ...args);
},
debug: function(...args) {
canLog() && _console.debug(LOGPREFIX, ...args);
}
}
}
loader = Loader(loaderOptions);
let require_ = Require(loader, { id: "./addon" });
mainModule = require_("chrome://b2g-installer/content/main.js");
}
function shutdown(data, reasonCode) {
let reason = REASON[reasonCode];
if (loader) {
unload(loader, reason);
unload = null;
}
if (mainModule && mainModule.shutdown) {
mainModule.shutdown();
}
}
function uninstall(data, reason) {}
/* vim: set et ts=2 sw=2 : */