This repository has been archived by the owner on Feb 26, 2022. It is now read-only.
/
installer.js
176 lines (145 loc) · 5.28 KB
/
installer.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* 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/. */
/* Promise-style installer for a addons */
/* references:
aInstall: https://developer.mozilla.org/en-US/docs/Addons/Add-on_Manager/AddonInstall
aAddon: https://developer.mozilla.org/en-US/docs/Addons/Add-on_Manager/Addon
Listener: https://developer.mozilla.org/en-US/docs/Addons/Add-on_Manager/InstallListener
*/
const { Cc, Ci, Cu } = require("chrome");
const { AddonManager } = Cu.import("resource://gre/modules/AddonManager.jsm");
const { defer } = require("api-utils/promise");
const { setTimeout } = require("api-utils/timer");
const { Class, mix } = require('api-utils/heritage');
/**
* `install` method error codes:
*
* https://developer.mozilla.org/en/Addons/Add-on_Manager/AddonManager#AddonInstall_errors
*/
exports.ERROR_NETWORK_FAILURE = AddonManager.ERROR_NETWORK_FAILURE;
exports.ERROR_INCORRECT_HASH = AddonManager.ERROR_INCORRECT_HASH;
exports.ERROR_CORRUPT_FILE = AddonManager.ERROR_CORRUPT_FILE;
exports.ERROR_FILE_ACCESS = AddonManager.ERROR_FILE_ACCESS;
/**
* `install` listener events
*
* https://developer.mozilla.org/en-US/docs/Addons/Add-on_Manager/InstallListener
*/
// Listen for various install events. Using a class here is possibly overkill.
// see git history for when this was a simpler object.
let Listener = Class({
initialize: function initialize(promise,resolve,reject) {
// TODO, what should the args here be? we need to bind to the promise.
this.promise = promise;
this.reject = reject;
this.resolve = resolve;
},
//extends: EventTarget, // ?
type: 'InstallEventListener',
myself: this, // are we dancing too much here?
onInstallEnded: function(aInstall, aAddon) {
console.log('hooray, we finished the install!');
aInstall.removeListener(this.myself);
// Bug 749745: on FF14+, onInstallEnded is called just before `startup()`
// is called, but we expect to resolve the promise only after it.
// As startup is called synchronously just after onInstallEnded,
// a simple setTimeout(0) is enough
setTimeout(this.resolve, 0, aAddon); // return the whole addon
//
},
onInstallFailed: function (aInstall) {
console.log("failed");
aInstall.removeListener(this.myself);
console.log('I really truly failed');
this.reject(aInstall.error);
},
onDownloadFailed: function(aInstall) {
this.onInstallFailed(aInstall);
},
onNewInstall: function(aInstall){ console.log('NewInstall')},
onDownloadStarted: function(aInstall){},
onDownloadProgress: function(aInstall){},
onDownloadEnded: function(aInstall){},
onDownloadCancelled: function(aInstall){},
onInstallStarted: function(aInstall){console.log('Install starting in listener')},
onInstallCancelled: function(aInstall){},
onExternalInstall: function(aInstall,aAddon,needsRestart){}
// TODO, add 'hash failure?'
});
/**
* Immediatly install an addon.
*
* @param {String} xpiPath
* file path to an xpi file to install
* @return {Promise}
* A promise resolved when the addon is finally installed.
* Resolved with addon id as value or rejected with an error code.
*/
exports.installFile = function installFile(xpiPath) {
console.log('into the install');
let { promise, resolve, reject } = defer();
// Create nsIFile for the xpi file
let file = Cc['@mozilla.org/file/local;1'].createInstance(Ci.nsILocalFile);
try {
file.initWithPath(xpiPath);
}
catch(e) {
reject(exports.ERROR_FILE_ACCESS);
return promise;
}
// Listen for installation end
let listener = new Listener(promise,resolve,reject);
console.log("made a listener");
listener.onNewInstall(); // test some pipes
// Order AddonManager to install the addon
AddonManager.getInstallForFile(file, function(install) {
install.addListener(listener);
install.install();
});
return promise;
};
exports.install = exports.installFile;
exports.installUrl = function installUrl(url,hash,mimetype){
let { promise, resolve, reject } = defer();
if (mimetype === undefined) {mimetype = "application/x-xpinstall"};
if (hash === undefined) {hash = ""};
// TODO, should we download the file ourselves, then go with file?
let listener = new Listener(promise,resolve,reject);
console.log("made a listener");
listener.onNewInstall();
AddonManager.getInstallForURL(url, function(install) {
install.addListener(listener);
install.install();
},
mimetype,
hash
);
return promise;
};
exports.uninstall = function uninstall(addonId) {
let { promise, resolve, reject } = defer();
// Listen for uninstallation end
let listener = {
onUninstalled: function onUninstalled(aAddon) {
if (aAddon.id != addonId)
return;
AddonManager.removeAddonListener(listener);
resolve(aAddon);
}
};
AddonManager.addAddonListener(listener);
// Order Addonmanager to uninstall the addon
AddonManager.getAddonByID(addonId, function (aAddon) {
aAddon.uninstall();
});
return promise;
};
exports.disable = function disable(addonId) {
let { promise, resolve, reject } = defer();
AddonManager.getAddonByID(addonId, function (aAddon) {
aAddon.userDisabled = true;
resolve(aAddon);
});
return promise;
};