home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / xulrunner-1.9.0.14 / chrome / toolkit.jar / content / mozapps / plugins / pluginInstallerService.js < prev    next >
Encoding:
Text File  |  2009-04-21  |  9.5 KB  |  287 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  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/
  8.  *
  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
  12.  * License.
  13.  *
  14.  * The Original Code is Plugin Finder Service.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * IBM Corporation.
  18.  * Portions created by the IBM Corporation are Copyright (C) 2004
  19.  * IBM Corporation. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Doron Rosenberg <doronr@us.ibm.com>
  23.  *
  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.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsIXPIProgressDialog = Components.interfaces.nsIXPIProgressDialog;
  39.  
  40. function getLocalizedError(key)
  41. {
  42.   return document.getElementById("xpinstallStrings").getString(key);
  43. }
  44.  
  45. function binaryToHex(input)
  46. {
  47.   return [('0' + input.charCodeAt(i).toString(16)).slice(-2)
  48.           for (i in input)].join('');
  49. }
  50.  
  51. function verifyHash(aFile, aHash)
  52. {
  53.   try {
  54.     var [, method, hash] = /^([A-Za-z0-9]+):(.*)$/.exec(aHash);
  55.  
  56.     var fis = Components.classes['@mozilla.org/network/file-input-stream;1'].
  57.       createInstance(Components.interfaces.nsIFileInputStream);
  58.     fis.init(aFile, -1, -1, 0);
  59.  
  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;
  66.   }
  67.   catch (e) {
  68.     Components.utils.reportError(e);
  69.     return false;
  70.   }
  71. }
  72.  
  73. function InstallerObserver(aPlugin)
  74. {
  75.   this._plugin = aPlugin;
  76.   this._init();
  77. }
  78.  
  79. InstallerObserver.prototype = {
  80.   _init: function()
  81.   {
  82.     try {
  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);
  87.  
  88.       // Use a local filename appropriate for the OS
  89.       var leafName = uri.fileName;
  90.       var os = Components.classes["@mozilla.org/xre/app-info;1"]
  91.                          .getService(Components.interfaces.nsIXULRuntime)
  92.                          .OS;
  93.       if (os == "WINNT" && leafName.indexOf(".") < 0)
  94.         leafName += ".exe";
  95.  
  96.       var dirs = Components.classes["@mozilla.org/file/directory_service;1"].
  97.         getService(Components.interfaces.nsIProperties);
  98.  
  99.       var resultFile = dirs.get("TmpD", Components.interfaces.nsIFile);
  100.       resultFile.append(leafName);
  101.       resultFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,
  102.                               0770);
  103.  
  104.       var channel = ios.newChannelFromURI(uri);
  105.       this._downloader =
  106.         Components.classes["@mozilla.org/network/downloader;1"].
  107.           createInstance(Components.interfaces.nsIDownloader);
  108.       this._downloader.init(this, resultFile);
  109.       channel.notificationCallbacks = this;
  110.  
  111.       this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_START, null);
  112.  
  113.       channel.asyncOpen(this._downloader, null);
  114.     }
  115.     catch (e) {
  116.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  117.                              getLocalizedError("error-228"));
  118.       if (resultFile && resultFile.exists())
  119.         resultfile.remove(false);
  120.     }
  121.   },
  122.  
  123.   /**
  124.    * Inform the gPluginInstaller about what's going on.
  125.    */
  126.   _fireNotification: function(aStatus, aErrorMsg)
  127.   {
  128.     gPluginInstaller.pluginInstallationProgress(this._plugin.pid,
  129.                                                 aStatus, aErrorMsg);
  130.  
  131.     if (aStatus == nsIXPIProgressDialog.INSTALL_DONE) {
  132.       --PluginInstallService._installersPending;
  133.       PluginInstallService._fireFinishedNotification();
  134.     }
  135.   },
  136.  
  137.   QueryInterface: function(iid)
  138.   {
  139.     if (iid.equals(Components.interfaces.nsISupports) ||
  140.         iid.equals(Components.interfaces.nsIInterfaceRequestor) ||
  141.         iid.equals(Components.interfaces.nsIDownloadObserver) ||
  142.         iid.equals(Components.interfaces.nsIProgressEventSink))
  143.       return this;
  144.  
  145.     throw Components.results.NS_ERROR_NO_INTERFACE;
  146.   },
  147.  
  148.   getInterface: function(iid)
  149.   {
  150.     if (iid.equals(Components.interfaces.nsIProgressEventSink))
  151.       return this;
  152.  
  153.     return null;
  154.   },
  155.  
  156.   onDownloadComplete: function(downloader, request, ctxt, status, result)
  157.   {
  158.     if (!Components.isSuccessCode(status)) {
  159.       // xpinstall error 228 is "Download Error"
  160.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  161.                              getLocalizedError("error-228"));
  162.       result.remove(false);
  163.       return;
  164.     }
  165.  
  166.     this._fireNotification(nsIXPIProgressDialog.DOWNLOAD_DONE);
  167.  
  168.     if (this._plugin.InstallerHash &&
  169.         !verifyHash(result, this._plugin.InstallerHash)) {
  170.       // xpinstall error 261 is "Invalid file hash..."
  171.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  172.                              getLocalizedError("error-261"));
  173.       result.remove(false);
  174.       return;
  175.     }
  176.  
  177.     this._fireNotification(nsIXPIProgressDialog.INSTALL_START);
  178.  
  179.     result.QueryInterface(Components.interfaces.nsILocalFile);
  180.     try {
  181.       // Make sure the file is executable
  182.       result.permissions = 0770;
  183.       result.launch();
  184.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE, null);
  185.       // It would be nice to remove the tempfile, but we don't have
  186.       // any way to know when it will stop being used :-(
  187.     }
  188.     catch (e) {
  189.       Components.utils.reportError(e);
  190.       this._fireNotification(nsIXPIProgressDialog.INSTALL_DONE,
  191.                              getLocalizedError("error-207"));
  192.       result.remove(false);
  193.     }
  194.   },
  195.  
  196.   onProgress: function(aRequest, aContext, aProgress, aProgressMax)
  197.   {
  198.     gPluginInstaller.pluginInstallationProgressMeter(this._plugin.pid,
  199.                                                      aProgress,
  200.                                                      aProgressMax);
  201.   },
  202.  
  203.   onStatus: function(aRequest, aContext, aStatus, aStatusArg)
  204.   {
  205.     /* pass */
  206.   }
  207. };
  208.  
  209. var PluginInstallService = {
  210.  
  211.   /**
  212.    * Start installation of installers and XPI plugins.
  213.    * @param aInstallerPlugins An array of objects which should have the
  214.    *                          properties "pid", "InstallerLocation",
  215.    *                          and "InstallerHash"
  216.    * @param aXPIPlugins       An array of objects which should have the
  217.    *                          properties "pid", "XPILocation",
  218.    *                          and "XPIHash"
  219.    */
  220.   startPluginInstallation: function (aInstallerPlugins,
  221.                                      aXPIPlugins)
  222.   {
  223.     this._installerPlugins = [new InstallerObserver(plugin)
  224.                               for each (plugin in aInstallerPlugins)];
  225.     this._installersPending = this._installerPlugins.length;
  226.  
  227.     this._xpiPlugins = aXPIPlugins;
  228.  
  229.     if (this._xpiPlugins.length > 0) {
  230.       this._xpisDone = false;
  231.  
  232.       var xpiManager = Components.classes["@mozilla.org/xpinstall/install-manager;1"]
  233.                                  .createInstance(Components.interfaces.nsIXPInstallManager);
  234.       xpiManager.initManagerWithHashes(
  235.         [plugin.XPILocation for each (plugin in this._xpiPlugins)],
  236.         [plugin.XPIHash for each (plugin in this._xpiPlugins)],
  237.         this._xpiPlugins.length, this);
  238.     }
  239.     else {
  240.       this._xpisDone = true;
  241.     }
  242.   },
  243.  
  244.   _fireFinishedNotification: function()
  245.   {
  246.     if (this._installersPending == 0 && this._xpisDone)
  247.       gPluginInstaller.
  248.         pluginInstallationProgress(null, nsIXPIProgressDialog.DIALOG_CLOSE,
  249.                                    null);
  250.   },
  251.  
  252.   // XPI progress listener stuff
  253.   onStateChange: function (aIndex, aState, aValue)
  254.   {
  255.     // get the pid to return to the wizard
  256.     var pid = this._xpiPlugins[aIndex].pid;
  257.     var errorMsg;
  258.  
  259.     if (aState == nsIXPIProgressDialog.INSTALL_DONE) {
  260.       if (aValue != 0) {
  261.         var xpinstallStrings = document.getElementById("xpinstallStrings");
  262.         try {
  263.           errorMsg = xpinstallStrings.getString("error" + aValue);
  264.         }
  265.         catch (e) {
  266.           errorMsg = xpinstallStrings.getFormattedString("unknown.error", [aValue]);
  267.         }
  268.       }
  269.     }
  270.  
  271.     if (aState == nsIXPIProgressDialog.DIALOG_CLOSE) {
  272.       this._xpisDone = true;
  273.       this._fireFinishedNotification();
  274.     }
  275.     else {
  276.       gPluginInstaller.pluginInstallationProgress(pid, aState, errorMsg);
  277.     }
  278.   },
  279.  
  280.   onProgress: function (aIndex, aValue, aMaxValue)
  281.   {
  282.     // get the pid to return to the wizard
  283.     var pid = this._xpiPlugins[aIndex].pid;
  284.     gPluginInstaller.pluginInstallationProgressMeter(pid, aValue, aMaxValue);
  285.   }
  286. }
  287.