home *** CD-ROM | disk | FTP | other *** search
/ Freelog 112 / FreelogNo112-NovembreDecembre2012.iso / Multimedia / Songbird / Songbird_2.0.0-2311_windows-i686-msvc8.exe / components / sbFileDownloader.js < prev    next >
Text File  |  2012-06-08  |  17KB  |  505 lines

  1. /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim: set sw=2 :miv */
  3. /*
  4.  *=BEGIN SONGBIRD GPL
  5.  *
  6.  * This file is part of the Songbird web player.
  7.  *
  8.  * Copyright(c) 2005-2010 POTI, Inc.
  9.  * http://www.songbirdnest.com
  10.  *
  11.  * This file may be licensed under the terms of of the
  12.  * GNU General Public License Version 2 (the ``GPL'').
  13.  *
  14.  * Software distributed under the License is distributed
  15.  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
  16.  * express or implied. See the GPL for the specific language
  17.  * governing rights and limitations.
  18.  *
  19.  * You should have received a copy of the GPL along with this
  20.  * program. If not, go to http://www.gnu.org/licenses/gpl.html
  21.  * or write to the Free Software Foundation, Inc.,
  22.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  23.  *
  24.  *=END SONGBIRD GPL
  25.  */
  26.  
  27. /**
  28.  * \file  sbFileDownloader.js
  29.  * \brief Javascript source for the file downloader component.
  30.  */
  31.  
  32. //------------------------------------------------------------------------------
  33. //------------------------------------------------------------------------------
  34. //
  35. // File downloader component.
  36. //
  37. //   This component provides support for downloading files.
  38. //
  39. //------------------------------------------------------------------------------
  40. //------------------------------------------------------------------------------
  41.  
  42. //------------------------------------------------------------------------------
  43. //
  44. // File downloader imported services.
  45. //
  46. //------------------------------------------------------------------------------
  47.  
  48. // Component manager defs.
  49. if (typeof(Cc) == "undefined")
  50.   var Cc = Components.classes;
  51. if (typeof(Ci) == "undefined")
  52.   var Ci = Components.interfaces;
  53. if (typeof(Cr) == "undefined")
  54.   var Cr = Components.results;
  55. if (typeof(Cu) == "undefined")
  56.   var Cu = Components.utils;
  57.  
  58. // Songbird imports.
  59. Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
  60.  
  61.  
  62. //------------------------------------------------------------------------------
  63. //
  64. // File downloader configuration.
  65. //
  66. //------------------------------------------------------------------------------
  67.  
  68. //
  69. // classDescription             Description of component class.
  70. // classID                      Component class ID.
  71. // contractID                   Component contract ID.
  72. // ifList                       List of external component interfaces.
  73. // _xpcom_categories            List of component categories.
  74. //
  75.  
  76. var sbFileDownloaderCfg= {
  77.   classDescription: "File Downloader",
  78.   classID: Components.ID("{f93a67e4-d97d-4607-971e-00ae2bf8ef8f}"),
  79.   contractID: "@songbirdnest.com/Songbird/FileDownloader;1",
  80.   ifList: [ Ci.sbIFileDownloader, Ci.nsIWebProgressListener ],
  81.   _xpcom_categories: []
  82. };
  83.  
  84.  
  85. //------------------------------------------------------------------------------
  86. //
  87. // File downloader object.
  88. //
  89. //------------------------------------------------------------------------------
  90.  
  91. /**
  92.  * Construct a file downloader object.
  93.  */
  94.  
  95. function sbFileDownloader() {
  96. }
  97.  
  98. // Define the object.
  99. sbFileDownloader.prototype = {
  100.   // Set the constructor.
  101.   constructor: sbFileDownloader,
  102.  
  103.   //
  104.   // File downloader component configuration fields.
  105.   //
  106.   //   classDescription         Description of component class.
  107.   //   classID                  Component class ID.
  108.   //   contractID               Component contract ID.
  109.   //   _xpcom_categories        List of component categories.
  110.   //
  111.  
  112.   classDescription: sbFileDownloaderCfg.classDescription,
  113.   classID: sbFileDownloaderCfg.classID,
  114.   contractID: sbFileDownloaderCfg.contractID,
  115.   _xpcom_categories: sbFileDownloaderCfg._xpcom_categories,
  116.  
  117.  
  118.   //
  119.   // File downloader fields.
  120.   //
  121.   //   _cfg                     Configuration settings.
  122.   //   _webBrowserPersist       Web browser persist object.
  123.   //
  124.  
  125.   _cfg: sbFileDownloaderCfg,
  126.   _webBrowserPersist: null,
  127.  
  128.  
  129.   //----------------------------------------------------------------------------
  130.   //
  131.   // File downloader sbIFileDownloader services.
  132.   //
  133.   //----------------------------------------------------------------------------
  134.  
  135.   /**
  136.    * \brief Number of bytes in file being downloaded.
  137.    */
  138.  
  139.   bytesToDownload: 0,
  140.  
  141.  
  142.   /**
  143.    * \brief Number of bytes in file that have been downloaded.
  144.    */
  145.  
  146.   bytesDownloaded: 0,
  147.  
  148.  
  149.   /**
  150.    * \brief Percentage (0-100) of bytes of file that have been downloaded.
  151.    */
  152.  
  153.   percentComplete: 0,
  154.  
  155.  
  156.   /**
  157.    * \brief True if file download has completed, whether successful or not.
  158.    */
  159.  
  160.   complete: false,
  161.  
  162.  
  163.   /**
  164.    * \brief True if file downloaded successfully.  Will be false if download is
  165.    *        cancelled.
  166.    */
  167.  
  168.   succeeded: false,
  169.  
  170.  
  171.   /**
  172.    * \brief Listener for download events.
  173.    */
  174.  
  175.   listener: null,
  176.  
  177.  
  178.   /**
  179.    * \brief URI of source of file.
  180.    */
  181.  
  182.   sourceURI: null,
  183.  
  184.  
  185.   /**
  186.    * \brief URI spec of source of file.
  187.    */
  188.  
  189.   sourceURISpec: null,
  190.  
  191.  
  192.   /**
  193.    * \brief Destination file.  If not set when download is started, a temporary
  194.    *        file will be created and set in destinationFile.
  195.    */
  196.  
  197.   destinationFile: null,
  198.  
  199.  
  200.   /**
  201.    * \brief Destination file extension.  If a temporary file is created, set its
  202.    *        file extension to destinationFileExtension.
  203.    */
  204.  
  205.   destinationFileExtension: null,
  206.  
  207.  
  208.   /**
  209.    * \brief Temporary file factory to use for any temporary files.
  210.    */
  211.  
  212.   temporaryFileFactory: null,
  213.  
  214.  
  215.   /**
  216.    * \brief Start file download from source URI to destination file.  If source
  217.    *        URI is not specified, use source URI spec.  If destination file is
  218.    *        not specified, create a temporary one.
  219.    */
  220.  
  221.   start: function sbFileDownloader_start() {
  222.     // Create a web browser persist object.
  223.     this._webBrowserPersist =
  224.            Cc["@mozilla.org/embedding/browser/nsWebBrowserPersist;1"]
  225.              .createInstance(Ci.nsIWebBrowserPersist);
  226.     this._webBrowserPersist.progressListener = this;
  227.     this._webBrowserPersist.persistFlags =
  228.            Ci.nsIWebBrowserPersist.PERSIST_FLAGS_REPLACE_EXISTING_FILES |
  229.            Ci.nsIWebBrowserPersist.PERSIST_FLAGS_NO_CONVERSION |
  230.            Ci.nsIWebBrowserPersist.PERSIST_FLAGS_BYPASS_CACHE;
  231.  
  232.     // If source URI is not provided, use source URI spec.
  233.     if (!this.sourceURI && this.sourceURISpec) {
  234.       var ioService = Cc["@mozilla.org/network/io-service;1"]
  235.                         .getService(Ci.nsIIOService);
  236.       this.sourceURI = ioService.newURI(this.sourceURISpec, null, null);
  237.     }
  238.  
  239.     // Throw an exception if no source is specified.
  240.     if (!this.sourceURI)
  241.       throw Cr.NS_ERROR_INVALID_ARG;
  242.  
  243.     // If destination file is not provided, create a temporary one.
  244.     //XXXeps if destination file is provided, should create a temporary one and
  245.     //XXXeps move to destination when complete.
  246.     if (!this.destinationFile) {
  247.       if (this.temporaryFileFactory) {
  248.         this.destinationFile =
  249.           this.temporaryFileFactory.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
  250.                                                null,
  251.                                                this.destinationFileExtension);
  252.       }
  253.       else {
  254.         var temporaryFileService =
  255.               Cc["@songbirdnest.com/Songbird/TemporaryFileService;1"]
  256.                 .getService(Ci.sbITemporaryFileService);
  257.         this.destinationFile =
  258.                temporaryFileService.createFile(Ci.nsIFile.NORMAL_FILE_TYPE,
  259.                                                null,
  260.                                                this.destinationFileExtension);
  261.       }
  262.     }
  263.  
  264.     // Start the file download.
  265.     this._webBrowserPersist.saveURI(this.sourceURI,
  266.                                     null,
  267.                                     null,
  268.                                     null,
  269.                                     null,
  270.                                     this.destinationFile);
  271.   },
  272.  
  273.  
  274.   /**
  275.    * \brief Cancel file download.
  276.    */
  277.  
  278.   cancel: function sbFileDownloader_cancel() {
  279.     // Cancel the file download.
  280.     this._webBrowserPersist.cancelSave();
  281.     this.request = null;
  282.   },
  283.  
  284.   /**
  285.    * \brief The request used during the transfer.
  286.    */
  287.   request: null,
  288.  
  289.  
  290.   //----------------------------------------------------------------------------
  291.   //
  292.   // File downloader nsIWebProgressListener services.
  293.   //
  294.   //----------------------------------------------------------------------------
  295.  
  296.   /**
  297.    * Notification indicating the state has changed for one of the requests
  298.    * associated with aWebProgress.
  299.    *
  300.    * @param aWebProgress
  301.    *        The nsIWebProgress instance that fired the notification
  302.    * @param aRequest
  303.    *        The nsIRequest that has changed state.
  304.    * @param aStateFlags
  305.    *        Flags indicating the new state.  This value is a combination of one
  306.    *        of the State Transition Flags and one or more of the State Type
  307.    *        Flags defined above.  Any undefined bits are reserved for future
  308.    *        use.
  309.    * @param aStatus
  310.    *        Error status code associated with the state change.  This parameter
  311.    *        should be ignored unless aStateFlags includes the STATE_STOP bit.
  312.    *        The status code indicates success or failure of the request
  313.    *        associated with the state change.  NOTE: aStatus may be a success
  314.    *        code even for server generated errors, such as the HTTP 404 error.
  315.    *        In such cases, the request itself should be queried for extended
  316.    *        error information (e.g., for HTTP requests see nsIHttpChannel).
  317.    */
  318.  
  319.   onStateChange: function sbFileDownloader_onStateChange(aWebProgress,
  320.                                                          aRequest,
  321.                                                          aStateFlags,
  322.                                                          aStatus) {
  323.     if (!this.request) {
  324.       this.request = aRequest;
  325.     }
  326.  
  327.     // Check for completion.
  328.     if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
  329.       // Mark completion.
  330.       this.complete = true;
  331.  
  332.       // Check for HTTP channel failure.
  333.       var httpChannelFailed = false;
  334.       try {
  335.         var httpChannel = aRequest.QueryInterface(Ci.nsIHttpChannel);
  336.         httpChannelFailed = (httpChannel.requestSucceeded ? false : true);
  337.       }
  338.       catch (ex) {}
  339.  
  340.       // Mark success if aStatus is NS_OK and the HTTP channel did not fail.
  341.       if ((aStatus == Cr.NS_OK) && !httpChannelFailed)
  342.         this.succeeded = true;
  343.  
  344.       // The destination file may have been overwritten.  This can cause the
  345.       // destination file object to be invalid.  Clone it to get a usable
  346.       // object.
  347.       if (this.destinationFile)
  348.         this.destinationFile = this.destinationFile.clone();
  349.  
  350.       // Delete destination file on failure.
  351.       if (!this.succeeded) {
  352.         this.destinationFile.remove(false);
  353.         this.destinationFile = null;
  354.       }
  355.  
  356.       // Remove web browser persist listener reference to avoid a cycle.
  357.       this._webBrowserPersist.progressListener = null;
  358.  
  359.       // Call file download listener.
  360.       if (this.listener)
  361.         this.listener.onComplete();
  362.     }
  363.   },
  364.  
  365.  
  366.   /**
  367.    * Notification that the progress has changed for one of the requests
  368.    * associated with aWebProgress.  Progress totals are reset to zero when all
  369.    * requests in aWebProgress complete (corresponding to onStateChange being
  370.    * called with aStateFlags including the STATE_STOP and STATE_IS_WINDOW
  371.    * flags).
  372.    *
  373.    * @param aWebProgress
  374.    *        The nsIWebProgress instance that fired the notification.
  375.    * @param aRequest
  376.    *        The nsIRequest that has new progress.
  377.    * @param aCurSelfProgress
  378.    *        The current progress for aRequest.
  379.    * @param aMaxSelfProgress
  380.    *        The maximum progress for aRequest.
  381.    * @param aCurTotalProgress
  382.    *        The current progress for all requests associated with aWebProgress.
  383.    * @param aMaxTotalProgress
  384.    *        The total progress for all requests associated with aWebProgress.
  385.    *
  386.    * NOTE: If any progress value is unknown, or if its value would exceed the
  387.    * maximum value of type long, then its value is replaced with -1.
  388.    *
  389.    * NOTE: If the object also implements nsIWebProgressListener2 and the caller
  390.    * knows about that interface, this function will not be called. Instead,
  391.    * nsIWebProgressListener2::onProgressChange64 will be called.
  392.    */
  393.  
  394.   onProgressChange:
  395.     function sbFileDownloader_onProgressChange(aWebProgress,
  396.                                                aRequest,
  397.                                                aCurSelfProgress,
  398.                                                aMaxSelfProgress,
  399.                                                aCurTotalProgress,
  400.                                                aMaxTotalProgress) {
  401.     // Update the file download statistics.
  402.     this.bytesToDownload = aMaxSelfProgress;
  403.     this.bytesDownloaded = aCurSelfProgress;
  404.     this.percentComplete =
  405.            Math.floor((this.bytesDownloaded / this.bytesToDownload) * 100);
  406.  
  407.     // Call file download listener.
  408.     if (this.listener)
  409.       this.listener.onProgress();
  410.   },
  411.  
  412.  
  413.   /**
  414.    * Called when the location of the window being watched changes.  This is not
  415.    * when a load is requested, but rather once it is verified that the load is
  416.    * going to occur in the given window.  For instance, a load that starts in a
  417.    * window might send progress and status messages for the new site, but it
  418.    * will not send the onLocationChange until we are sure that we are loading
  419.    * this new page here.
  420.    *
  421.    * @param aWebProgress
  422.    *        The nsIWebProgress instance that fired the notification.
  423.    * @param aRequest
  424.    *        The associated nsIRequest.  This may be null in some cases.
  425.    * @param aLocation
  426.    *        The URI of the location that is being loaded.
  427.    */
  428.  
  429.   onLocationChange: function sbFileDownloader_onLocationChange(aWebProgress,
  430.                                                                aRequest,
  431.                                                                aLocation) {
  432.   },
  433.  
  434.  
  435.   /**
  436.    * Notification that the status of a request has changed.  The status message
  437.    * is intended to be displayed to the user (e.g., in the status bar of the
  438.    * browser).
  439.    *
  440.    * @param aWebProgress
  441.    *        The nsIWebProgress instance that fired the notification.
  442.    * @param aRequest
  443.    *        The nsIRequest that has new status.
  444.    * @param aStatus
  445.    *        This value is not an error code.  Instead, it is a numeric value
  446.    *        that indicates the current status of the request.  This interface
  447.    *        does not define the set of possible status codes.  NOTE: Some
  448.    *        status values are defined by nsITransport and nsISocketTransport.
  449.    * @param aMessage
  450.    *        Localized text corresponding to aStatus.
  451.    */
  452.  
  453.   onStatusChange: function sbFileDownloader_onStatusChange(aWebProgress,
  454.                                                            aRequest,
  455.                                                            aStatus,
  456.                                                            aMessage) {
  457.   },
  458.  
  459.  
  460.   /**
  461.    * Notification called for security progress.  This method will be called on
  462.    * security transitions (eg HTTP -> HTTPS, HTTPS -> HTTP, FOO -> HTTPS) and
  463.    * after document load completion.  It might also be called if an error
  464.    * occurs during network loading.
  465.    *
  466.    * @param aWebProgress
  467.    *        The nsIWebProgress instance that fired the notification.
  468.    * @param aRequest
  469.    *        The nsIRequest that has new security state.
  470.    * @param aState
  471.    *        A value composed of the Security State Flags and the Security
  472.    *        Strength Flags listed above.  Any undefined bits are reserved for
  473.    *        future use.
  474.    *
  475.    * NOTE: These notifications will only occur if a security package is
  476.    * installed.
  477.    */
  478.  
  479.   onSecurityChange: function sbFileDownloader_onSecurityChange(aWebProgress,
  480.                                                                aRequest,
  481.                                                                aState) {
  482.   },
  483.  
  484.  
  485.   //----------------------------------------------------------------------------
  486.   //
  487.   // File downloader nsISupports services.
  488.   //
  489.   //----------------------------------------------------------------------------
  490.  
  491.   QueryInterface: XPCOMUtils.generateQI(sbFileDownloaderCfg.ifList)
  492. };
  493.  
  494.  
  495. //------------------------------------------------------------------------------
  496. //
  497. // File downloader component services.
  498. //
  499. //------------------------------------------------------------------------------
  500.  
  501. function NSGetModule(compMgr, fileSpec) {
  502.   return XPCOMUtils.generateModule([sbFileDownloader]);
  503. }
  504.  
  505.