toolkit/mozapps/extensions/addonManager.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 /**
michael@0 6 * This component serves as integration between the platform and AddonManager.
michael@0 7 * It is responsible for initializing and shutting down the AddonManager as well
michael@0 8 * as passing new installs from webpages to the AddonManager.
michael@0 9 */
michael@0 10
michael@0 11 "use strict";
michael@0 12
michael@0 13 const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
michael@0 14
michael@0 15 const PREF_EM_UPDATE_INTERVAL = "extensions.update.interval";
michael@0 16
michael@0 17 // The old XPInstall error codes
michael@0 18 const EXECUTION_ERROR = -203;
michael@0 19 const CANT_READ_ARCHIVE = -207;
michael@0 20 const USER_CANCELLED = -210;
michael@0 21 const DOWNLOAD_ERROR = -228;
michael@0 22 const UNSUPPORTED_TYPE = -244;
michael@0 23 const SUCCESS = 0;
michael@0 24
michael@0 25 const MSG_INSTALL_ENABLED = "WebInstallerIsInstallEnabled";
michael@0 26 const MSG_INSTALL_ADDONS = "WebInstallerInstallAddonsFromWebpage";
michael@0 27 const MSG_INSTALL_CALLBACK = "WebInstallerInstallCallback";
michael@0 28
michael@0 29 const CHILD_SCRIPT = "resource://gre/modules/addons/Content.js";
michael@0 30
michael@0 31 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
michael@0 32 Cu.import("resource://gre/modules/Services.jsm");
michael@0 33
michael@0 34 let gSingleton = null;
michael@0 35
michael@0 36 let gParentMM = null;
michael@0 37
michael@0 38
michael@0 39 function amManager() {
michael@0 40 Cu.import("resource://gre/modules/AddonManager.jsm");
michael@0 41
michael@0 42 let globalMM = Cc["@mozilla.org/globalmessagemanager;1"]
michael@0 43 .getService(Ci.nsIMessageListenerManager);
michael@0 44 globalMM.loadFrameScript(CHILD_SCRIPT, true);
michael@0 45
michael@0 46 gParentMM = Cc["@mozilla.org/parentprocessmessagemanager;1"]
michael@0 47 .getService(Ci.nsIMessageListenerManager);
michael@0 48 gParentMM.addMessageListener(MSG_INSTALL_ENABLED, this);
michael@0 49 gParentMM.addMessageListener(MSG_INSTALL_ADDONS, this);
michael@0 50 }
michael@0 51
michael@0 52 amManager.prototype = {
michael@0 53 observe: function AMC_observe(aSubject, aTopic, aData) {
michael@0 54 if (aTopic == "addons-startup")
michael@0 55 AddonManagerPrivate.startup();
michael@0 56 },
michael@0 57
michael@0 58 /**
michael@0 59 * @see amIAddonManager.idl
michael@0 60 */
michael@0 61 mapURIToAddonID: function AMC_mapURIToAddonID(uri, id) {
michael@0 62 id.value = AddonManager.mapURIToAddonID(uri);
michael@0 63 return !!id.value;
michael@0 64 },
michael@0 65
michael@0 66 /**
michael@0 67 * @see amIWebInstaller.idl
michael@0 68 */
michael@0 69 isInstallEnabled: function AMC_isInstallEnabled(aMimetype, aReferer) {
michael@0 70 return AddonManager.isInstallEnabled(aMimetype);
michael@0 71 },
michael@0 72
michael@0 73 /**
michael@0 74 * @see amIWebInstaller.idl
michael@0 75 */
michael@0 76 installAddonsFromWebpage: function AMC_installAddonsFromWebpage(aMimetype,
michael@0 77 aWindow,
michael@0 78 aReferer, aUris,
michael@0 79 aHashes, aNames,
michael@0 80 aIcons, aCallback) {
michael@0 81 if (aUris.length == 0)
michael@0 82 return false;
michael@0 83
michael@0 84 let retval = true;
michael@0 85 if (!AddonManager.isInstallAllowed(aMimetype, aReferer)) {
michael@0 86 aCallback = null;
michael@0 87 retval = false;
michael@0 88 }
michael@0 89
michael@0 90 let loadGroup = null;
michael@0 91
michael@0 92 try {
michael@0 93 loadGroup = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
michael@0 94 .getInterface(Ci.nsIWebNavigation)
michael@0 95 .QueryInterface(Ci.nsIDocumentLoader).loadGroup;
michael@0 96 }
michael@0 97 catch (e) {
michael@0 98 }
michael@0 99
michael@0 100 let installs = [];
michael@0 101 function buildNextInstall() {
michael@0 102 if (aUris.length == 0) {
michael@0 103 AddonManager.installAddonsFromWebpage(aMimetype, aWindow, aReferer, installs);
michael@0 104 return;
michael@0 105 }
michael@0 106 let uri = aUris.shift();
michael@0 107 AddonManager.getInstallForURL(uri, function buildNextInstall_getInstallForURL(aInstall) {
michael@0 108 function callCallback(aUri, aStatus) {
michael@0 109 try {
michael@0 110 aCallback.onInstallEnded(aUri, aStatus);
michael@0 111 }
michael@0 112 catch (e) {
michael@0 113 Components.utils.reportError(e);
michael@0 114 }
michael@0 115 }
michael@0 116
michael@0 117 if (aInstall) {
michael@0 118 installs.push(aInstall);
michael@0 119 if (aCallback) {
michael@0 120 aInstall.addListener({
michael@0 121 onDownloadCancelled: function buildNextInstall_onDownloadCancelled(aInstall) {
michael@0 122 callCallback(uri, USER_CANCELLED);
michael@0 123 },
michael@0 124
michael@0 125 onDownloadFailed: function buildNextInstall_onDownloadFailed(aInstall) {
michael@0 126 if (aInstall.error == AddonManager.ERROR_CORRUPT_FILE)
michael@0 127 callCallback(uri, CANT_READ_ARCHIVE);
michael@0 128 else
michael@0 129 callCallback(uri, DOWNLOAD_ERROR);
michael@0 130 },
michael@0 131
michael@0 132 onInstallFailed: function buildNextInstall_onInstallFailed(aInstall) {
michael@0 133 callCallback(uri, EXECUTION_ERROR);
michael@0 134 },
michael@0 135
michael@0 136 onInstallEnded: function buildNextInstall_onInstallEnded(aInstall, aStatus) {
michael@0 137 callCallback(uri, SUCCESS);
michael@0 138 }
michael@0 139 });
michael@0 140 }
michael@0 141 }
michael@0 142 else if (aCallback) {
michael@0 143 aCallback.onInstallEnded(uri, UNSUPPORTED_TYPE);
michael@0 144 }
michael@0 145 buildNextInstall();
michael@0 146 }, aMimetype, aHashes.shift(), aNames.shift(), aIcons.shift(), null, loadGroup);
michael@0 147 }
michael@0 148 buildNextInstall();
michael@0 149
michael@0 150 return retval;
michael@0 151 },
michael@0 152
michael@0 153 notify: function AMC_notify(aTimer) {
michael@0 154 AddonManagerPrivate.backgroundUpdateCheck();
michael@0 155 },
michael@0 156
michael@0 157 /**
michael@0 158 * messageManager callback function.
michael@0 159 *
michael@0 160 * Listens to requests from child processes for InstallTrigger
michael@0 161 * activity, and sends back callbacks.
michael@0 162 */
michael@0 163 receiveMessage: function AMC_receiveMessage(aMessage) {
michael@0 164 let payload = aMessage.data;
michael@0 165 let referer = Services.io.newURI(payload.referer, null, null);
michael@0 166
michael@0 167 switch (aMessage.name) {
michael@0 168 case MSG_INSTALL_ENABLED:
michael@0 169 return this.isInstallEnabled(payload.mimetype, referer);
michael@0 170
michael@0 171 case MSG_INSTALL_ADDONS:
michael@0 172 let callback = null;
michael@0 173 if (payload.callbackID != -1) {
michael@0 174 callback = {
michael@0 175 onInstallEnded: function ITP_callback(url, status) {
michael@0 176 gParentMM.broadcastAsyncMessage(MSG_INSTALL_CALLBACK, {
michael@0 177 callbackID: payload.callbackID,
michael@0 178 url: url,
michael@0 179 status: status
michael@0 180 });
michael@0 181 },
michael@0 182 };
michael@0 183 }
michael@0 184
michael@0 185 // Should reimplement this properly with Window IDs when possible,
michael@0 186 // see bug 596109.
michael@0 187 let window = aMessage.objects.win;
michael@0 188
michael@0 189 return this.installAddonsFromWebpage(payload.mimetype,
michael@0 190 window, referer, payload.uris, payload.hashes, payload.names,
michael@0 191 payload.icons, callback);
michael@0 192 }
michael@0 193 },
michael@0 194
michael@0 195 classID: Components.ID("{4399533d-08d1-458c-a87a-235f74451cfa}"),
michael@0 196 _xpcom_factory: {
michael@0 197 createInstance: function AMC_createInstance(aOuter, aIid) {
michael@0 198 if (aOuter != null)
michael@0 199 throw Components.Exception("Component does not support aggregation",
michael@0 200 Cr.NS_ERROR_NO_AGGREGATION);
michael@0 201
michael@0 202 if (!gSingleton)
michael@0 203 gSingleton = new amManager();
michael@0 204 return gSingleton.QueryInterface(aIid);
michael@0 205 }
michael@0 206 },
michael@0 207 QueryInterface: XPCOMUtils.generateQI([Ci.amIAddonManager,
michael@0 208 Ci.amIWebInstaller,
michael@0 209 Ci.nsITimerCallback,
michael@0 210 Ci.nsIObserver,
michael@0 211 Ci.nsIMessageListener])
michael@0 212 };
michael@0 213
michael@0 214 this.NSGetFactory = XPCOMUtils.generateNSGetFactory([amManager]);

mercurial