2010-04-29 20:11:23 +00:00
|
|
|
/*
|
2012-05-21 11:12:37 +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";
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
const PREF_EM_UPDATE_INTERVAL = "extensions.update.interval";
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
const SUCCESS = 0;
|
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
const MSG_INSTALL_ENABLED = "WebInstallerIsInstallEnabled";
|
|
|
|
const MSG_INSTALL_ADDONS = "WebInstallerInstallAddonsFromWebpage";
|
|
|
|
const MSG_INSTALL_CALLBACK = "WebInstallerInstallCallback";
|
|
|
|
|
|
|
|
const CHILD_SCRIPT =
|
|
|
|
"chrome://mozapps/content/extensions/extensions-content.js";
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
|
2010-08-16 20:41:46 +00:00
|
|
|
Components.utils.import("resource://gre/modules/Services.jsm");
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
var gSingleton = null;
|
|
|
|
|
|
|
|
function amManager() {
|
|
|
|
Components.utils.import("resource://gre/modules/AddonManager.jsm");
|
2010-08-16 20:41:46 +00:00
|
|
|
|
|
|
|
var messageManager = Cc["@mozilla.org/globalmessagemanager;1"].
|
|
|
|
getService(Ci.nsIChromeFrameMessageManager);
|
|
|
|
|
|
|
|
messageManager.addMessageListener(MSG_INSTALL_ENABLED, this);
|
|
|
|
messageManager.addMessageListener(MSG_INSTALL_ADDONS, this);
|
|
|
|
messageManager.loadFrameScript(CHILD_SCRIPT, true, true);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
amManager.prototype = {
|
2010-04-28 16:42:07 +00:00
|
|
|
observe: function AMC_observe(aSubject, aTopic, aData) {
|
2010-04-29 20:11:23 +00:00
|
|
|
let os = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
|
2010-04-28 16:42:07 +00:00
|
|
|
switch (aTopic) {
|
2010-06-11 19:02:44 +00:00
|
|
|
case "addons-startup":
|
2010-04-29 20:11:23 +00:00
|
|
|
os.addObserver(this, "xpcom-shutdown", false);
|
|
|
|
AddonManagerPrivate.startup();
|
|
|
|
break;
|
|
|
|
case "xpcom-shutdown":
|
|
|
|
os.removeObserver(this, "xpcom-shutdown");
|
|
|
|
AddonManagerPrivate.shutdown();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see amIWebInstaller.idl
|
|
|
|
*/
|
2010-04-28 16:42:07 +00:00
|
|
|
isInstallEnabled: function AMC_isInstallEnabled(aMimetype, aReferer) {
|
|
|
|
return AddonManager.isInstallEnabled(aMimetype);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see amIWebInstaller.idl
|
|
|
|
*/
|
2010-04-28 16:42:07 +00:00
|
|
|
installAddonsFromWebpage: function AMC_installAddonsFromWebpage(aMimetype,
|
|
|
|
aWindow,
|
|
|
|
aReferer, aUris,
|
|
|
|
aHashes, aNames,
|
|
|
|
aIcons, aCallback) {
|
|
|
|
if (aUris.length == 0)
|
2010-04-29 20:11:23 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
let retval = true;
|
2010-04-28 16:42:07 +00:00
|
|
|
if (!AddonManager.isInstallAllowed(aMimetype, aReferer)) {
|
|
|
|
aCallback = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
retval = false;
|
|
|
|
}
|
|
|
|
|
2010-04-28 16:33:43 +00:00
|
|
|
let loadGroup = null;
|
2010-04-29 20:11:23 +00:00
|
|
|
|
|
|
|
try {
|
2010-04-28 16:42:07 +00:00
|
|
|
loadGroup = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
|
|
|
|
.getInterface(Ci.nsIWebNavigation)
|
|
|
|
.QueryInterface(Ci.nsIDocumentLoader).loadGroup;
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
|
|
|
|
let installs = [];
|
|
|
|
function buildNextInstall() {
|
2010-04-28 16:42:07 +00:00
|
|
|
if (aUris.length == 0) {
|
|
|
|
AddonManager.installAddonsFromWebpage(aMimetype, aWindow, aReferer, installs);
|
2010-04-29 20:11:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2010-04-28 16:42:07 +00:00
|
|
|
let uri = aUris.shift();
|
|
|
|
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({
|
|
|
|
onDownloadCancelled: function(aInstall) {
|
2010-08-20 22:48:05 +00:00
|
|
|
callCallback(uri, USER_CANCELLED);
|
2010-04-29 20:11:23 +00:00
|
|
|
},
|
|
|
|
|
2010-06-10 23:39:47 +00:00
|
|
|
onDownloadFailed: function(aInstall) {
|
|
|
|
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
|
|
|
},
|
|
|
|
|
2010-06-10 23:39:47 +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
|
|
|
},
|
|
|
|
|
2010-04-28 16:42:07 +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();
|
2010-04-28 16:42:07 +00:00
|
|
|
}, aMimetype, aHashes.shift(), aNames.shift(), aIcons.shift(), null, loadGroup);
|
2010-04-29 20:11:23 +00:00
|
|
|
}
|
|
|
|
buildNextInstall();
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
},
|
|
|
|
|
2010-04-28 16:42:07 +00:00
|
|
|
notify: function AMC_notify(aTimer) {
|
2010-04-29 20:11:23 +00:00
|
|
|
AddonManagerPrivate.backgroundUpdateCheck();
|
|
|
|
},
|
|
|
|
|
2010-08-16 20:41:46 +00:00
|
|
|
/**
|
|
|
|
* messageManager callback function.
|
|
|
|
*
|
|
|
|
* Listens to requests from child processes for InstallTrigger
|
|
|
|
* activity, and sends back callbacks.
|
|
|
|
*/
|
|
|
|
receiveMessage: function(aMessage) {
|
|
|
|
var payload = aMessage.json;
|
|
|
|
var referer = Services.io.newURI(payload.referer, null, null);
|
|
|
|
switch (aMessage.name) {
|
|
|
|
case MSG_INSTALL_ENABLED:
|
|
|
|
return this.isInstallEnabled(payload.mimetype, referer);
|
|
|
|
|
|
|
|
case MSG_INSTALL_ADDONS:
|
|
|
|
var callback = null;
|
|
|
|
if (payload.callbackId != -1) {
|
|
|
|
callback = {
|
|
|
|
onInstallEnded: function ITP_callback(url, status) {
|
|
|
|
// Doing it this way, instead of aMessage.target.messageManager,
|
|
|
|
// ensures it works in Firefox and not only Fennec. See bug
|
|
|
|
// 578172. TODO: Clean up this code once that bug is fixed
|
|
|
|
var flo = aMessage.target.QueryInterface(Ci.nsIFrameLoaderOwner);
|
|
|
|
var returnMessageManager = flo.frameLoader.messageManager;
|
|
|
|
returnMessageManager.sendAsyncMessage(MSG_INSTALL_CALLBACK,
|
|
|
|
{ installerId: payload.installerId,
|
|
|
|
callbackId: payload.callbackId, url: url, status: status }
|
|
|
|
);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2010-09-15 17:46:55 +00:00
|
|
|
var window = null;
|
2010-08-16 20:41:46 +00:00
|
|
|
try {
|
|
|
|
// Normal approach for single-process mode
|
2010-09-15 17:46:55 +00:00
|
|
|
window = aMessage.target.contentWindow;
|
2010-08-16 20:41:46 +00:00
|
|
|
} catch (e) {
|
2010-09-15 17:46:55 +00:00
|
|
|
// Fallback for multiprocess (e10s) mode. Should reimplement this
|
|
|
|
// properly with Window IDs when possible, see bug 596109.
|
2010-08-16 20:41:46 +00:00
|
|
|
window = aMessage.target.ownerDocument.defaultView;
|
|
|
|
}
|
|
|
|
return this.installAddonsFromWebpage(payload.mimetype,
|
|
|
|
window, referer, payload.uris, payload.hashes, payload.names,
|
|
|
|
payload.icons, callback, payload.uris.length);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2010-04-29 20:11:23 +00:00
|
|
|
classID: Components.ID("{4399533d-08d1-458c-a87a-235f74451cfa}"),
|
|
|
|
_xpcom_factory: {
|
2010-04-28 16:42:07 +00:00
|
|
|
createInstance: function(aOuter, aIid) {
|
|
|
|
if (aOuter != null)
|
2012-07-30 10:46:45 +00:00
|
|
|
throw Components.Exception("Component does not support aggregation",
|
|
|
|
Cr.NS_ERROR_NO_AGGREGATION);
|
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
|
|
|
}
|
|
|
|
},
|
|
|
|
QueryInterface: XPCOMUtils.generateQI([Ci.amIWebInstaller,
|
|
|
|
Ci.nsITimerCallback,
|
|
|
|
Ci.nsIObserver])
|
|
|
|
};
|
|
|
|
|
2010-06-22 16:59:15 +00:00
|
|
|
var NSGetFactory = XPCOMUtils.generateNSGetFactory([amManager]);
|