DownloadDevice.h
Go to the documentation of this file.
1 /*
2 //
3 // BEGIN SONGBIRD GPL
4 //
5 // This file is part of the Songbird web player.
6 //
7 // Copyright(c) 2005-2008 POTI, Inc.
8 // http://songbirdnest.com
9 //
10 // This file may be licensed under the terms of of the
11 // GNU General Public License Version 2 (the "GPL").
12 //
13 // Software distributed under the License is distributed
14 // on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either
15 // express or implied. See the GPL for the specific language
16 // governing rights and limitations.
17 //
18 // You should have received a copy of the GPL along with this
19 // program. If not, go to http://www.gnu.org/licenses/gpl.html
20 // or write to the Free Software Foundation, Inc.,
21 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 //
23 // END SONGBIRD GPL
24 //
25 */
26 
27 #ifndef __DOWNLOAD_DEVICE_H__
28 #define __DOWNLOAD_DEVICE_H__
29 
30 /* *****************************************************************************
31  *******************************************************************************
32  *
33  * Download device.
34  *
35  *******************************************************************************
36  ******************************************************************************/
37 
43 /* *****************************************************************************
44  *
45  * Download device configuration.
46  *
47  ******************************************************************************/
48 
49 /*
50  * Download device XPCOM component definitions.
51  */
52 
53 #define SONGBIRD_DownloadDevice_CONTRACTID \
54  "@songbirdnest.com/Songbird/OldDeviceImpl/DownloadDevice;1"
55 #define SONGBIRD_DownloadDevice_CLASSNAME "Songbird Download Device"
56 #define SONGBIRD_DownloadDevice_CID \
57 { \
58  0x961DA3F4, \
59  0x5EF1, \
60  0x4AD0, \
61  { 0x81, 0x8d, 0x62, 0x2C, 0x7B, 0xD1, 0x74, 0x47} \
62 }
63 
64 
65 /* *****************************************************************************
66  *
67  * Download device imported services.
68  *
69  ******************************************************************************/
70 
71 /* Local imports. */
72 #include <DeviceBase.h>
73 #include <sbIDownloadDevice.h>
74 
75 /* Mozilla imports. */
76 #include <nsAutoPtr.h>
77 #include <nsCOMPtr.h>
78 #include <nsIDialogParamBlock.h>
79 #include <nsIFile.h>
80 #include <nsIIOService.h>
81 #include <nsIObserverService.h>
82 #include <nsIPrefService.h>
83 #include <nsIRunnable.h>
84 #include <nsIStringBundle.h>
85 #include <nsIThreadPool.h>
86 #include <prmon.h>
87 
88 /* Songbird imports. */
90 #include <sbIDataRemote.h>
91 #include <sbILibrary.h>
92 #include <sbILibraryUtils.h>
93 
94 
95 
96 nsCString GetContentDispositionFilename(const nsACString &contentDisposition);
97 
98 /* *****************************************************************************
99  *
100  * Download device classes.
101  *
102  ******************************************************************************/
103 
104 /*
105  * sbDownloadDevice class.
106  */
107 
108 class sbDownloadSession;
109 
111  public sbIDownloadDevice,
112  public sbDeviceBase,
113  public sbIMediaListListener
114 {
115  /* *************************************************************************
116  *
117  * Friends.
118  *
119  **************************************************************************/
120 
121  friend class sbDownloadSession;
122 
123 
124  /* *************************************************************************
125  *
126  * Public interface.
127  *
128  **************************************************************************/
129 
130  public:
131 
132  /*
133  * Inherited interfaces.
134  */
135 
137  NS_DECL_NSIOBSERVER
138  NS_DECL_SBIDEVICEBASE
139  NS_DECL_SBIDOWNLOADDEVICE
140  NS_DECL_SBIMEDIALISTLISTENER
141 
142 
143  /*
144  * Public download device services.
145  */
146 
148 
149  virtual ~sbDownloadDevice();
150 
151 
152  /* *************************************************************************
153  *
154  * Private interface.
155  *
156  **************************************************************************/
157 
158  private:
159 
160  /*
161  * mpDownloadMediaList Download device medialist.
162  * mpDeviceLibraryListener Songbird device library listener.
163  * mpMainLibrary Main library.
164  * mpWebLibrary Web library.
165  * mpPrefBranch Preference branch.
166  * mpIOService I/O service.
167  * mpStringBundle Download device string bundle.
168  * mQueuedStr Download queued string.
169  * mpTmpDownloadDir Temporary download directory.
170  * mpDownloadSession Current download session.
171  * mpDeviceLock Lock for download device access.
172  * mDeviceIdentifier Download device identifier.
173  * mFileMoveThreadPool Thread pool to move files on completion
174  */
175 
176  nsCOMPtr<sbIMediaList> mpDownloadMediaList;
177  nsRefPtr<sbDeviceBaseLibraryListener>
178  mpDeviceLibraryListener;
179  nsCOMPtr<sbILibrary> mpMainLibrary;
180  nsCOMPtr<sbILibrary> mpWebLibrary;
181  nsCOMPtr<nsIPrefBranch> mpPrefBranch;
182  nsCOMPtr<nsIIOService> mpIOService;
183  nsCOMPtr<nsIStringBundle> mpStringBundle;
184  nsString mQueuedStr;
185  nsCOMPtr<nsIFile> mpTmpDownloadDir;
186  nsRefPtr<sbDownloadSession> mpDownloadSession;
187  PRMonitor *mpDeviceMonitor;
188  nsString mDeviceIdentifier;
189 
190  nsCOMPtr<nsIThreadPool> mFileMoveThreadPool;
191 
192  /*
193  * Private media list services.
194  */
195 
196  nsresult InitializeDownloadMediaList();
197 
198  void FinalizeDownloadMediaList();
199 
200  nsresult CreateDownloadMediaList();
201 
202  void GetDownloadMediaList();
203 
204  nsresult UpdateDownloadMediaList();
205 
206 
207  /*
208  * Private transfer services.
209  */
210 
211  nsresult EnqueueItem(
212  sbIMediaItem *apMediaItem);
213 
214  nsresult RunTransferQueue();
215 
216  PRBool GetNextTransferItem(
217  sbIMediaItem **appMediaItem);
218 
219  nsresult ResumeTransfers();
220 
221  nsresult SetTransferDestination(
222  nsCOMPtr<sbIMediaItem> pMediaItem);
223 
224  nsresult CancelSession();
225 
226  void SessionCompleted(
227  sbDownloadSession *apDownloadSession,
228  PRInt32 aStatus);
229 
230 
231  /*
232  * Private services.
233  */
234 
235  nsresult GetTmpFile(
236  nsIFile **ppTmpFile);
237 
238  static nsresult MakeFileUnique(
239  nsIFile *apFile);
240 
241  nsresult OpenDialog(
242  char *aChromeURL,
243  nsIDialogParamBlock *apDialogPB);
244 
245  static nsresult GetStatusTarget(
246  sbIMediaItem *apMediaItem,
247  sbIMediaItem **apStatusTarget);
248 };
249 
250 
251 /* *****************************************************************************
252  *******************************************************************************
253  *
254  * Download session.
255  *
256  *******************************************************************************
257  ******************************************************************************/
258 
259 /* *****************************************************************************
260  *
261  * Download session imported services.
262  *
263  ******************************************************************************/
264 
265 /* Mozilla imports. */
266 #include <nsIFileProtocolHandler.h>
267 #include <nsIHttpChannel.h>
268 #include <nsIHttpEventSink.h>
269 #include <nsIInterfaceRequestor.h>
270 #include <nsIProgressEventSink.h>
271 #include <nsIWebBrowserPersist.h>
272 #include <nsIWebProgressListener.h>
273 #include <nsITimer.h>
274 
275 
276 /* *****************************************************************************
277  *
278  * Download session classes.
279  *
280  ******************************************************************************/
281 
282 /*
283  * sbDownloadSession class.
284  */
285 
286 class sbDownloadSession : public nsIWebProgressListener, nsITimerCallback
287 {
288  /* *************************************************************************
289  *
290  * Public interface.
291  *
292  **************************************************************************/
293 
294  public:
295 
296  /*
297  * mpMediaItem Media item being downloaded.
298  * mSrcURISpec Source URI spec string.
299  * mDstURISpec Destination URI spec string.
300  */
301 
302  nsCOMPtr<sbIMediaItem> mpMediaItem;
303  nsString mSrcURISpec;
304  nsString mDstURISpec;
305 
306 
307  /*
308  * Inherited interfaces.
309  */
310 
312  NS_DECL_NSIWEBPROGRESSLISTENER
313  NS_DECL_NSITIMERCALLBACK
314 
315  /*
316  * Public download session services.
317  */
318 
320  sbDownloadDevice *pDownloadDevice,
321  sbIMediaItem *pMediaItem);
322 
323  virtual ~sbDownloadSession();
324 
325  nsresult Initiate();
326 
327  nsresult Suspend();
328 
329  nsresult Resume();
330 
331  void Shutdown();
332 
333  PRBool IsSuspended();
334 
335 
336  /* *************************************************************************
337  *
338  * Private interface.
339  *
340  **************************************************************************/
341 
342  private:
343 
344  /*
345  * mpSessionLock Lock for session.
346  * mpDownloadDevice Managing download device.
347  * mpStringBundle Download device string bundle.
348  * mCompleteStr Download complete string.
349  * mErrorStr Download error string.
350  * mpLibraryUtils Library utilities service.
351  * mpFileProtocolHandler File protocol handler.
352  * mpWebBrowser Web browser used for download.
353  * mpChannel Download channel.
354  * mpTmpFile Temporary download file.
355  * mpSrcURI Source download URI.
356  * mpDstLibrary Destination library.
357  * mpDstFile Destination download file.
358  * mpDstURI Destination download URI.
359  * mEntityID Entity ID of the download (for resuming).
360  * mShutdown True if session has been shut down.
361  * mSuspended True if session is suspended.
362  * mLastUpdate Last time progress was updated.
363  * mInitialProgressBytes Number of bytes already downloaded when a new channel was created.
364  * mLastProgressBytes Number of progress bytes on last update.
365  * mRate Download rate.
366  * mIdleTimer Idle timer cancels downloads that aren't
367  * seeing progress.
368  */
369 
370  PRLock *mpSessionLock;
371  sbDownloadDevice *mpDownloadDevice;
372  nsCOMPtr<nsIStringBundle> mpStringBundle;
373  nsString mCompleteStr;
374  nsString mErrorStr;
375  nsCOMPtr<sbILibraryUtils> mpLibraryUtils;
376  nsCOMPtr<nsIWebBrowserPersist>
377  mpWebBrowser;
378  nsCOMPtr<nsIChannel> mpRequest;
379  nsCOMPtr<nsIFile> mpTmpFile;
380  nsCOMPtr<nsIURI> mpSrcURI;
381  nsCOMPtr<sbILibrary> mpDstLibrary;
382  nsCOMPtr<nsIFile> mpDstFile;
383  nsCOMPtr<nsIURI> mpDstURI;
384  nsCOMPtr<sbIMediaItem> mpStatusTarget;
385  nsCString mEntityID;
386  PRBool mShutdown;
387  PRBool mSuspended;
388  PRTime mLastUpdate;
389  PRInt64 mInitialProgressBytes;
390  PRUint64 mLastProgressBytes;
391  PRUint64 mLastProgressBytesMax;
392  double mRate;
393  nsCOMPtr<nsITimer> mIdleTimer;
394  nsCOMPtr<nsITimer> mProgressTimer;
395 
396 
397  /*
398  * Private download session services.
399  */
400 
401  nsresult SetUpRequest();
402 
403  nsresult CompleteTransfer(nsIRequest* aRequest);
404 
405  nsresult UpdateDstLibraryMetadata();
406 
407  void UpdateProgress(
408  PRUint64 aProgress,
409  PRUint64 aProgressMax);
410 
411  void UpdateDownloadDetails(
412  PRUint64 aProgress,
413  PRUint64 aProgressMax);
414 
415  void UpdateDownloadRate(
416  PRUint64 aProgress,
417  PRUint64 aElapsedUSecs);
418 
419  nsresult FormatProgress(
420  nsString &aProgressStr,
421  PRUint64 aProgress,
422  PRUint64 aProgressMax,
423  double aRate,
424  PRUint32 aRemSeconds);
425 
426  nsresult FormatRate(
427  nsString &aRateStr,
428  double aRate);
429 
430  nsresult FormatByteProgress(
431  nsString &aByteProgressStr,
432  PRUint64 aBytes,
433  PRUint64 aBytesMax);
434 
435  nsresult FormatTime(
436  nsString &aTimeStr,
437  PRUint32 aSeconds);
438 
439  nsresult StartTimers();
440  nsresult StopTimers();
441  nsresult ResetTimers();
442 
443 
444  /* *************************************************************************
445  *
446  * Library metadata updater class.
447  *
448  **************************************************************************/
449 
450  class LibraryMetadataUpdater : public sbIMediaListEnumerationListener
451  {
452  /*
453  * Public interface.
454  */
455 
456  public:
457 
459  NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
460 
461 
462  /*
463  * Private interface.
464  */
465 
466  private:
467 
468  nsCOMPtr<nsIMutableArray> mpMediaItemArray;
469  };
470 
471 
472  /* *************************************************************************
473  *
474  * Web library updater class.
475  *
476  **************************************************************************/
477 
478  class WebLibraryUpdater : public sbIMediaListEnumerationListener
479  {
480  /*
481  * Public interface.
482  */
483 
484  public:
485 
487  NS_DECL_SBIMEDIALISTENUMERATIONLISTENER
488 
489  WebLibraryUpdater(
490  sbDownloadSession *pDownloadSession)
491  :
492  mpDownloadSession(pDownloadSession)
493  {
494  }
495 
496 
497  /*
498  * Private interface.
499  */
500 
501  private:
502 
503  sbDownloadSession *mpDownloadSession;
504  };
505 };
506 
508 {
509 public:
511  sbIMediaItem* aStatusTarget = nsnull,
512  PRBool aReadOnly = PR_FALSE);
514 
515  nsAutoPtr<sbDownloadButtonPropertyValue> value;
516 
517 private:
518  nsCOMPtr<sbIMediaItem> mMediaItem;
519  nsCOMPtr<sbIMediaItem> mStatusTarget;
520  PRBool mReadOnly;
521 };
522 
523 class sbDownloadSessionMoveHandler : public nsIRunnable
524 {
525 public:
526  explicit sbDownloadSessionMoveHandler(nsIFile *aSourceFile,
527  nsIFile *aDestinationPath,
528  const nsAString &aDestinationFileName,
529  sbIMediaItem *aDestinationItem) {
530  NS_ASSERTION(aSourceFile, "aSourceFile is null!");
531  NS_ASSERTION(aDestinationPath, "aDestinationPath is null!");
532  NS_ASSERTION(aDestinationItem, "aDestinationItem is null!");
533 
534  mSourceFile = aSourceFile;
535  mDestinationPath = aDestinationPath;
536  mDestinationFileName = aDestinationFileName;
537  mDestinationItem = aDestinationItem;
538  }
539 
541  NS_DECL_NSIRUNNABLE
542 
543 private:
544  nsCOMPtr<nsIFile> mSourceFile;
545  nsCOMPtr<nsIFile> mDestinationPath;
546  nsString mDestinationFileName;
547 
548  nsCOMPtr<sbIMediaItem> mDestinationItem;
549 };
550 
551 #endif // __DOWNLOAD_DEVICE_H__
nsCOMPtr< sbIMediaItem > mpMediaItem
sbAutoDownloadButtonPropertyValue(sbIMediaItem *aMediaItem, sbIMediaItem *aStatusTarget=nsnull, PRBool aReadOnly=PR_FALSE)
Interface used to enumerate the items in a media list.
nsCString GetContentDispositionFilename(const nsACString &contentDisposition)
sbDownloadSessionMoveHandler(nsIFile *aSourceFile, nsIFile *aDestinationPath, const nsAString &aDestinationFileName, sbIMediaItem *aDestinationItem)
Interface used to listen to changes to a media list.
virtual ~sbDownloadDevice()
NS_DECL_ISUPPORTS NS_DECL_NSIOBSERVER NS_DECL_SBIDEVICEBASE NS_DECL_SBIDOWNLOADDEVICE NS_DECL_SBIMEDIALISTLISTENER sbDownloadDevice()
NS_DECL_ISUPPORTS NS_DECL_NSIWEBPROGRESSLISTENER NS_DECL_NSITIMERCALLBACK sbDownloadSession(sbDownloadDevice *pDownloadDevice, sbIMediaItem *pMediaItem)
Songbird DownloadDevice Interface.
Interface that defines a single item of media in the system.
virtual ~sbDownloadSession()
nsAutoPtr< sbDownloadButtonPropertyValue > value
Songbird DeviceBase Component Definition.
nsITimerCallback