1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Plugin Finder Service.
16 * The Initial Developer of the Original Code is
18 * Portions created by the IBM Corporation are Copyright (C) 2004
19 * IBM Corporation. All Rights Reserved.
22 * Doron Rosenberg <doronr@us.ibm.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 const nsIXPIProgressDialog = Components.interfaces.nsIXPIProgressDialog;
40 function getLocalizedError(key)
42 return document.getElementById("xpinstallStrings").getString(key);
45 function binaryToHex(input)
47 return [('0' + input.charCodeAt(i).toString(16)).slice(-2)
48 for (i in input)].join('');
51 function verifyHash(aFile, aHash)
54 var [, method, hash] = /^([A-Za-z0-9]+):(.*)$/.exec(aHash);
56 var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
57 createInstance(Components.interfaces.nsIFileInputStream);
58 fis.init(aFile, -1, -1, 0);
60 var hasher = Components.classes['@mozilla.org/security/hash;1'].
61 createInstance(Components.interfaces.nsICryptoHash);
62 hasher.initWithString(method);
63 hasher.updateFromStream(fis, -1);
64 dlhash = binaryToHex(hasher.finish(false));
65 return dlhash == hash;
68 Components.utils.reportError(e);
73 function InstallerObserver(aPlugin)
75 this._plugin = aPlugin;
79 InstallerObserver.prototype = {
83 var ios = Components.classes["@mozilla.org/network/io-service;1"].
84 getService(Components.interfaces.nsIIOService);
85 var uri = ios.newURI(this._plugin.InstallerLocation, null, null);
86 uri.QueryInterface(Components.interfaces.nsIURL);
88 var leafName = uri.fileName;
89 if (leafName.indexOf('.') == -1)
90 throw "Filename needs to contain a dot for platform-native launching to work correctly.";
92 var dirs = Components.classes["@mozilla.org/file/directory_service;1"].
93 getService(Components.interfaces.nsIProperties);
95 var resultFile = dirs.get("TmpD", Components.interfaces.nsIFile);
96 resultFile.append(leafName);
97 resultFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
100 var channel = ios.newChannelFromURI(uri);
102 Components.classes["@mozilla.org/network/downloader;1"].
103 createInstance(Components.interfaces.nsIDownloader);
104 this._downloader.init(this, resultFile);
105 channel.notificationCallbacks = this;
107 this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_START, null);
109 channel.asyncOpen(this._downloader, null);
112 this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
113 getLocalizedError("error-228"));
114 if (resultFile && resultFile.exists())
115 resultfile.remove(false);
120 * Inform the gPluginInstaller about what's going on.
122 _fireNotification: function(aStatus, aErrorMsg)
124 gPluginInstaller.pluginInstallationProgress(this._plugin.pid,
127 if (aStatus == nsIXPIProgressDialog.INSTALL_DONE) {
128 --PluginInstallService._installersPending;
129 PluginInstallService._fireFinishedNotification();
133 QueryInterface: function(iid)
135 if (iid.equals(Components.interfaces.nsISupports) ||
136 iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
137 iid.equals(Components.interfaces.nsIDownloadObserver) ||
138 iid.equals(Components.interfaces.nsIProgressEventSink))
141 throw Components.results.NS_ERROR_NO_INTERFACE;
144 getInterface: function(iid)
146 if (iid.equals(Components.interfaces.nsIProgressEventSink))
152 onDownloadComplete: function(downloader, request, ctxt, status, result)
154 if (!Components.isSuccessCode(status)) {
155 // xpinstall error 228 is "Download Error"
156 this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
157 getLocalizedError("error-228"));
158 result.remove(false);
162 this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_DONE);
164 if (this._plugin.InstallerHash &&
165 !verifyHash(result, this._plugin.InstallerHash)) {
166 // xpinstall error 261 is "Invalid file hash..."
167 this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
168 getLocalizedError("error-261"));
169 result.remove(false);
173 this._fireNotification(nsIXPIProgressDialog.INSTALL_START);
175 result.QueryInterface(Components.interfaces.nsILocalFile);
178 this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE, null);
179 // It would be nice to remove the tempfile, but we don't have
180 // any way to know when it will stop being used :-(
183 Components.utils.reportError(e);
184 this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
185 getLocalizedError("error-207"));
186 result.remove(false);
190 onProgress: function(aRequest, aContext, aProgress, aProgressMax)
192 gPluginInstaller.pluginInstallationProgressMeter(this._plugin.pid,
197 onStatus: function(aRequest, aContext, aStatus, aStatusArg)
203 var PluginInstallService = {
206 * Start installation of installers and XPI plugins.
207 * @param aInstallerPlugins An array of objects which should have the
208 * properties "pid", "InstallerLocation",
209 * and "InstallerHash"
210 * @param aXPIPlugins An array of objects which should have the
211 * properties "pid", "XPILocation",
214 startPluginInstallation: function (aInstallerPlugins,
217 this._installerPlugins = [new InstallerObserver(plugin)
218 for each (plugin in aInstallerPlugins)];
219 this._installersPending = this._installerPlugins.length;
221 this._xpiPlugins = aXPIPlugins;
223 if (this._xpiPlugins.length > 0) {
224 this._xpisDone = false;
226 var xpiManager = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
227 .createInstance(Components.interfaces.nsIXPInstallManager);
228 xpiManager.initManagerWithHashes(
229 [plugin.XPILocation for each (plugin in this._xpiPlugins)],
230 [plugin.XPIHash for each (plugin in this._xpiPlugins)],
231 this._xpiPlugins.length, this);
234 this._xpisDone = true;
238 _fireFinishedNotification: function()
240 if (this._installersPending == 0 && this._xpisDone)
242 pluginInstallationProgress(null, nsIXPIProgressDialog.DIALOG_CLOSE,
246 // XPI progress listener stuff
247 onStateChange: function (aIndex, aState, aValue)
249 // get the pid to return to the wizard
250 var pid = this._xpiPlugins[aIndex].pid;
253 if (aState == nsIXPIProgressDialog.INSTALL_DONE) {
255 var xpinstallStrings = document.getElementById("xpinstallStrings");
257 errorMsg = xpinstallStrings.getString("error" + aValue);
260 errorMsg = xpinstallStrings.getFormattedString("unknown.error", [aValue]);
265 if (aState == nsIXPIProgressDialog.DIALOG_CLOSE) {
266 this._xpisDone = true;
267 this._fireFinishedNotification();
270 gPluginInstaller.pluginInstallationProgress(pid, aState, errorMsg);
274 onProgress: function (aIndex, aValue, aMaxValue)
276 // get the pid to return to the wizard
277 var pid = this._xpiPlugins[aIndex].pid;
278 gPluginInstaller.pluginInstallationProgressMeter(pid, aValue, aMaxValue);