sbFileDownloader.js
Go to the documentation of this file.
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 
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 
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 ],
82 };
83 
84 
85 //------------------------------------------------------------------------------
86 //
87 // File downloader object.
88 //
89 //------------------------------------------------------------------------------
90 
95 function sbFileDownloader() {
96 }
97 
98 // Define the object.
99 sbFileDownloader.prototype = {
100  // Set the constructor.
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 
139  bytesToDownload: 0,
140 
141 
146  bytesDownloaded: 0,
147 
148 
153  percentComplete: 0,
154 
155 
160  complete: false,
161 
162 
168  succeeded: false,
169 
170 
175  listener: null,
176 
177 
182  sourceURI: null,
183 
184 
189  sourceURISpec: null,
190 
191 
197  destinationFile: null,
198 
199 
205  destinationFileExtension: null,
206 
207 
212  temporaryFileFactory: null,
213 
214 
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 
278  cancel: function sbFileDownloader_cancel() {
279  // Cancel the file download.
280  this._webBrowserPersist.cancelSave();
281  this.request = null;
282  },
283 
287  request: null,
288 
289 
290  //----------------------------------------------------------------------------
291  //
292  // File downloader nsIWebProgressListener services.
293  //
294  //----------------------------------------------------------------------------
295 
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 
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 
429  onLocationChange: function sbFileDownloader_onLocationChange(aWebProgress,
430  aRequest,
431  aLocation) {
432  },
433 
434 
453  onStatusChange: function sbFileDownloader_onStatusChange(aWebProgress,
454  aRequest,
455  aStatus,
456  aMessage) {
457  },
458 
459 
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 
function start(ch)
const Cu
const Cc
var sbFileDownloaderCfg
function succeeded(ch, cx, status, data)
sbDeviceFirmwareAutoCheckForUpdate prototype contractID
sbOSDControlService prototype QueryInterface
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
var ioService
function NSGetModule(compMgr, fileSpec)
DataRemote prototype constructor
return null
Definition: FeedWriter.js:1143
const Cr
function sbFileDownloader()
const Ci
sbDeviceFirmwareAutoCheckForUpdate prototype classID
ContinuingWebProgressListener prototype onStateChange
sbWindowsAutoPlayServiceCfg _xpcom_categories
restoreWindow aState