2012-08-04 01:22:55 +00:00
|
|
|
/* 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/. */
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This component serves as integration between the platform and AddonManager.
|
|
|
|
* It is responsible for initializing and shutting down the AddonManager as well
|
|
|
|
* as passing new installs from webpages to the AddonManager.
|
|
|
|
*/
|
|
|
|
|
2011-07-19 20:16:35 +00:00
|
|
|
"use strict";
|
|
|
|
|
2013-12-12 02:08:00 +00:00
|
|
|
const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
// The old XPInstall error codes
|
|
|
|
const EXECUTION_ERROR = -203;
|
|
|
|
const CANT_READ_ARCHIVE = -207;
|
|
|
|
const USER_CANCELLED = -210;
|
|
|
|
const DOWNLOAD_ERROR = -228;
|
|
|
|
const UNSUPPORTED_TYPE = -244;
|
2013-12-12 02:08:00 +00:00
|
|
|
const SUCCESS = 0;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
const MSG_INSTALL_ENABLED = "WebInstallerIsInstallEnabled";
|
|
|
|
const MSG_INSTALL_ADDONS = "WebInstallerInstallAddonsFromWebpage";
|
|
|
|
const MSG_INSTALL_CALLBACK = "WebInstallerInstallCallback";
|
|
|
|
|
2013-12-12 02:08:00 +00:00
|
|
|
const CHILD_SCRIPT = "resource://gre/modules/addons/Content.js";
|
2010-08-16 20:41:46 +00:00
|
|
|
|
2013-12-12 02:08:00 +00:00
|
|
|
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
|
|
|
|
Cu.import("resource://gre/modules/Services.jsm");
|
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var gSingleton = null;
|
2013-12-12 02:08:00 +00:00
|
|
|
|
2015-09-15 18:19:45 +00:00
|
|
|
var gParentMM = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
function amManager() {
|
2013-12-12 02:08:00 +00:00
|
|
|
Cu.import("resource://gre/modules/AddonManager.jsm");
|
2016-02-04 06:48:48 +00:00
|
|
|
/*globals AddonManagerPrivate*/
|
2010-08-16 20:41:46 +00:00
|
|
|
|
2013-12-12 02:08:00 +00:00
|
|
|
let globalMM = Cc["@mozilla.org/globalmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageListenerManager);
|
|
|
|
globalMM.loadFrameScript(CHILD_SCRIPT, true);
|
2014-07-18 16:27:29 +00:00
|
|
|
globalMM.addMessageListener(MSG_INSTALL_ADDONS, this);
|
2010-08-16 20:41:46 +00:00
|
|
|
|
2013-12-12 02:08:00 +00:00
|
|
|
gParentMM = Cc["@mozilla.org/parentprocessmessagemanager;1"]
|
|
|
|
.getService(Ci.nsIMessageListenerManager);
|
|
|
|
gParentMM.addMessageListener(MSG_INSTALL_ENABLED, this);
|
2015-08-31 18:23:39 +00:00
|
|
|
|
|
|
|
// Needed so receiveMessage can be called directly by JS callers
|
|
|
|
this.wrappedJSObject = this;
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amManager.prototype = {
|
2015-11-20 00:35:41 +00:00
|
|
|
observe: function(aSubject, aTopic, aData) {
|
2012-05-10 18:33:02 +00:00
|
|
|
if (aTopic == "addons-startup")
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.startup();
|
|
|
|
},
|
|
|
|
|
2013-06-15 02:48:06 +00:00
|
|
|
/**
|
|
|
|
* @see amIAddonManager.idl
|
|
|
|
*/
|
2015-11-20 00:35:41 +00:00
|
|
|
mapURIToAddonID: function(uri, id) {
|
2013-06-15 02:48:06 +00:00
|
|
|
id.value = AddonManager.mapURIToAddonID(uri);
|
|
|
|
return !!id.value;
|
|
|
|
},
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
/**
|
|
|
|
* @see amIWebInstaller.idl
|
|
|
|
*/
|
2015-11-20 00:35:41 +00:00
|
|
|
isInstallEnabled: function(aMimetype, aReferer) {
|
2010-04-28 16:42:07 +00:00
|
|
|
return AddonManager.isInstallEnabled(aMimetype);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see amIWebInstaller.idl
|
|
|
|
*/
|
2015-11-20 00:35:41 +00:00
|
|
|
installAddonsFromWebpage: function(aMimetype, aBrowser, aInstallingPrincipal,
|
|
|
|
aUris, aHashes, aNames, aIcons, aCallback) {
|
2010-04-28 16:42:07 +00:00
|
|
|
if (aUris.length == 0)
|
2010-04-29 20:11:23 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
let retval = true;
|
2015-08-19 00:21:05 +00:00
|
|
|
if (!AddonManager.isInstallAllowed(aMimetype, aInstallingPrincipal)) {
|
2010-04-28 16:42:07 +00:00
|
|
|
aCallback = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
let installs = [];
|
|
|
|
function buildNextInstall() {
|
2010-04-28 16:42:07 +00:00
|
|
|
if (aUris.length == 0) {
|
2015-08-19 00:21:05 +00:00
|
|
|
AddonManager.installAddonsFromWebpage(aMimetype, aBrowser, aInstallingPrincipal, installs);
|
2010-04-29 20:11:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-28 16:42:07 +00:00
|
|
|
let uri = aUris.shift();
|
2015-11-20 00:35:41 +00:00
|
|
|
AddonManager.getInstallForURL(uri, function(aInstall) {
|
2011-07-19 20:16:35 +00:00
|
|
|
function callCallback(aUri, aStatus) {
|
|
|
|
try {
|
|
|
|
aCallback.onInstallEnded(aUri, aStatus);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
Components.utils.reportError(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-28 16:42:07 +00:00
|
|
|
if (aInstall) {
|
|
|
|
installs.push(aInstall);
|
|
|
|
if (aCallback) {
|
|
|
|
aInstall.addListener({
|
2015-11-20 00:35:41 +00:00
|
|
|
onDownloadCancelled: function(aInstall) {
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, USER_CANCELLED);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
2015-11-20 00:35:41 +00:00
|
|
|
onDownloadFailed: function(aInstall) {
|
2010-06-10 23:39:47 +00:00
|
|
|
if (aInstall.error == AddonManager.ERROR_CORRUPT_FILE)
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, CANT_READ_ARCHIVE);
|
2010-04-29 20:11:23 +00:00
|
|
|
else
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, DOWNLOAD_ERROR);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
2015-11-20 00:35:41 +00:00
|
|
|
onInstallFailed: function(aInstall) {
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, EXECUTION_ERROR);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
2015-11-20 00:35:41 +00:00
|
|
|
onInstallEnded: function(aInstall, aStatus) {
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, SUCCESS);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2010-04-28 16:42:07 +00:00
|
|
|
else if (aCallback) {
|
|
|
|
aCallback.onInstallEnded(uri, UNSUPPORTED_TYPE);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
buildNextInstall();
|
2014-11-05 03:20:52 +00:00
|
|
|
}, aMimetype, aHashes.shift(), aNames.shift(), aIcons.shift(), null, aBrowser);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
buildNextInstall();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
},
|
|
|
|
|
2015-11-20 00:35:41 +00:00
|
|
|
notify: function(aTimer) {
|
2014-09-02 15:47:11 +00:00
|
|
|
AddonManagerPrivate.backgroundUpdateTimerHandler();
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
/**
|
|
|
|
* messageManager callback function.
|
|
|
|
*
|
|
|
|
* Listens to requests from child processes for InstallTrigger
|
|
|
|
* activity, and sends back callbacks.
|
|
|
|
*/
|
2015-11-20 00:35:41 +00:00
|
|
|
receiveMessage: function(aMessage) {
|
2013-12-12 02:08:00 +00:00
|
|
|
let payload = aMessage.data;
|
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
switch (aMessage.name) {
|
|
|
|
case MSG_INSTALL_ENABLED:
|
2015-08-19 00:21:05 +00:00
|
|
|
return AddonManager.isInstallEnabled(payload.mimetype);
|
2010-08-16 20:41:46 +00:00
|
|
|
|
2014-07-18 16:27:29 +00:00
|
|
|
case MSG_INSTALL_ADDONS: {
|
2013-12-12 02:08:00 +00:00
|
|
|
let callback = null;
|
|
|
|
if (payload.callbackID != -1) {
|
2010-08-16 20:41:46 +00:00
|
|
|
callback = {
|
2015-11-20 00:35:41 +00:00
|
|
|
onInstallEnded: function(url, status) {
|
2013-12-12 02:08:00 +00:00
|
|
|
gParentMM.broadcastAsyncMessage(MSG_INSTALL_CALLBACK, {
|
|
|
|
callbackID: payload.callbackID,
|
|
|
|
url: url,
|
|
|
|
status: status
|
|
|
|
});
|
2010-08-16 20:41:46 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2013-12-12 02:08:00 +00:00
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
return this.installAddonsFromWebpage(payload.mimetype,
|
2015-08-19 00:21:05 +00:00
|
|
|
aMessage.target, payload.triggeringPrincipal, payload.uris,
|
|
|
|
payload.hashes, payload.names, payload.icons, callback);
|
2014-07-18 16:27:29 +00:00
|
|
|
}
|
2010-08-16 20:41:46 +00:00
|
|
|
}
|
2016-04-05 18:32:12 +00:00
|
|
|
return undefined;
|
2010-08-16 20:41:46 +00:00
|
|
|
},
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
classID: Components.ID("{4399533d-08d1-458c-a87a-235f74451cfa}"),
|
|
|
|
_xpcom_factory: {
|
2015-11-20 00:35:41 +00:00
|
|
|
createInstance: function(aOuter, aIid) {
|
2010-04-28 16:42:07 +00:00
|
|
|
if (aOuter != null)
|
2012-07-30 10:46:45 +00:00
|
|
|
throw Components.Exception("Component does not support aggregation",
|
|
|
|
Cr.NS_ERROR_NO_AGGREGATION);
|
2013-12-12 02:08:00 +00:00
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
if (!gSingleton)
|
|
|
|
gSingleton = new amManager();
|
2010-04-28 16:42:07 +00:00
|
|
|
return gSingleton.QueryInterface(aIid);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
},
|
2013-06-15 02:48:06 +00:00
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.amIAddonManager,
|
|
|
|
Ci.amIWebInstaller,
|
2010-04-29 20:11:23 +00:00
|
|
|
Ci.nsITimerCallback,
|
2013-12-12 02:08:00 +00:00
|
|
|
Ci.nsIObserver,
|
|
|
|
Ci.nsIMessageListener])
|
2010-04-29 20:11:23 +00:00
|
|
|
};
|
|
|
|
|
2012-10-31 16:13:28 +00:00
|
|
|
this.NSGetFactory = XPCOMUtils.generateNSGetFactory([amManager]);
|