56 #include <nsAutoLock.h>
76 static PRLogModuleInfo* gBaseMediaItemDownloadJobLog = nsnull;
77 #define TRACE(args) PR_LOG(gBaseMediaItemDownloadJobLog, PR_LOG_DEBUG, args)
78 #define LOG(args) PR_LOG(gBaseMediaItemDownloadJobLog, PR_LOG_WARN, args)
80 #define __FUNCTION__ __PRETTY_FUNCTION__
117 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
123 nsCOMPtr<nsIURI> downloadURI;
124 rv = GetDownloadURI(getter_AddRefs(downloadURI));
125 NS_ENSURE_SUCCESS(rv, rv);
128 rv = Start(downloadURI);
129 NS_ENSURE_SUCCESS(rv, rv);
147 sbBaseMediaItemDownloadJob::GetDownloadedFile(nsIFile** aDownloadedFile)
149 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
150 NS_ENSURE_ARG_POINTER(aDownloadedFile);
153 nsCOMPtr<sbIFileDownloader> fileDownloader;
155 nsAutoLock autoLock(
mLock);
158 NS_ENSURE_TRUE(fileDownloader, NS_ERROR_NOT_AVAILABLE);
161 nsresult rv = fileDownloader->GetDestinationFile(aDownloadedFile);
162 NS_ENSURE_SUCCESS(rv, rv);
178 NS_ENSURE_ARG_POINTER(aProperties);
181 nsAutoLock autoLock(
mLock);
184 return CallQueryInterface(properties, aProperties);
196 sbBaseMediaItemDownloadJob::GetTemporaryFileFactory
199 NS_ENSURE_ARG_POINTER(aTemporaryFileFactory);
200 nsAutoLock autoLock(
mLock);
206 sbBaseMediaItemDownloadJob::SetTemporaryFileFactory
209 nsAutoLock autoLock(
mLock);
229 sbBaseMediaItemDownloadJob::AddJobProgressListener
232 TRACE((
"%s[%.8x] 0x%08x", __FUNCTION__,
this, aListener));
233 NS_ENSURE_ARG_POINTER(aListener);
236 nsAutoLock autoLock(
mLock);
238 nsCOMPtr<sbIJobProgressListener>*
240 NS_ENSURE_TRUE(appendedElement, NS_ERROR_OUT_OF_MEMORY);
243 NS_WARNING(
"Listener already added");
258 sbBaseMediaItemDownloadJob::RemoveJobProgressListener
261 TRACE((
"%s[%.8x] 0x%08x", __FUNCTION__,
this, aListener));
262 NS_ENSURE_ARG_POINTER(aListener);
265 nsAutoLock autoLock(
mLock);
270 NS_WARNING(
"Listener not present");
289 sbBaseMediaItemDownloadJob::GetStatus(PRUint16* aStatus)
291 NS_ENSURE_ARG_POINTER(aStatus);
292 nsAutoLock autoLock(
mLock);
306 sbBaseMediaItemDownloadJob::GetBlocked(PRBool* aBlocked)
308 NS_ENSURE_ARG_POINTER(aBlocked);
309 *aBlocked = PR_FALSE;
322 sbBaseMediaItemDownloadJob::GetStatusText(nsAString& aStatusText)
326 "sbBaseMediaItemDownloadJob::GetStatusText is main thread only!");
328 (
"media_item_downloader.job.status.downloading"));
341 sbBaseMediaItemDownloadJob::GetTitleText(nsAString& aTitleText)
345 "sbBaseMediaItemDownloadJob::GetTitleText is main thread only!");
359 sbBaseMediaItemDownloadJob::GetProgress(PRUint32* aProgress)
362 NS_ENSURE_ARG_POINTER(aProgress);
371 NS_ENSURE_SUCCESS(rv, rv);
375 *aProgress =
static_cast<PRUint32
>(progress & 0xFFFFFFFF);
389 sbBaseMediaItemDownloadJob::GetTotal(PRUint32* aTotal)
392 NS_ENSURE_ARG_POINTER(aTotal);
398 PRUint64 bytesToDownload = 0;
401 NS_ENSURE_SUCCESS(rv, rv);
405 *aTotal =
static_cast<PRUint32
>(bytesToDownload & 0xFFFFFFFF);
419 sbBaseMediaItemDownloadJob::GetErrorCount(PRUint32* aErrorCount)
422 NS_ENSURE_ARG_POINTER(aErrorCount);
437 NS_ENSURE_SUCCESS(rv, rv);
443 NS_ENSURE_SUCCESS(rv, rv);
444 if (complete && !succeeded)
466 NS_ENSURE_ARG_POINTER(retval);
469 "sbBaseMediaItemDownloadJob::GetErrorMessages is main thread only!");
472 nsTArray<nsString> errorMessageList;
477 rv = GetErrorCount(&errorCount);
478 NS_ENSURE_SUCCESS(rv, rv);
482 errorMessageList.AppendElement
487 nsCOMPtr<nsIStringEnumerator>
489 NS_ENSURE_TRUE(errorMessageEnum, NS_ERROR_OUT_OF_MEMORY);
490 errorMessageEnum.forget(retval);
502 sbBaseMediaItemDownloadJob::GetCanCancel(PRBool *aCanCancel)
505 *aCanCancel = PR_TRUE;
517 sbBaseMediaItemDownloadJob::Cancel()
536 sbBaseMediaItemDownloadJob::OnProgress()
538 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
544 nsTArray< nsCOMPtr<sbIJobProgressListener> > listenerList;
546 nsAutoLock autoLock(
mLock);
551 PRInt32
const listenerCount = listenerList.Length();
552 for (PRInt32
i = 0;
i < listenerCount; ++
i) {
553 rv = listenerList[
i]->OnJobProgress(
this);
554 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Listener error");
569 sbBaseMediaItemDownloadJob::OnComplete()
571 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
577 nsCOMPtr<sbIFileDownloader> fileDownloader;
579 nsAutoLock autoLock(
mLock);
582 NS_ENSURE_TRUE(fileDownloader, NS_ERROR_NOT_AVAILABLE);
585 rv = fileDownloader->SetListener(nsnull);
586 NS_ENSURE_SUCCESS(rv, rv);
590 rv = fileDownloader->GetSucceeded(&succeeded);
591 NS_ENSURE_SUCCESS(rv, rv);
594 nsTArray< nsCOMPtr<sbIJobProgressListener> > listenerList;
596 nsAutoLock autoLock(
mLock);
609 PRInt32
const listenerCount = listenerList.Length();
610 for (PRInt32
i = 0;
i < listenerCount; ++
i) {
611 rv = listenerList[
i]->OnJobProgress(
this);
612 NS_WARN_IF_FALSE(NS_SUCCEEDED(rv),
"Listener error");
632 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
636 nsAutoLock::DestroyLock(
mLock);
656 NS_ENSURE_ARG_POINTER(aURI);
662 nsCOMPtr<sbIFileDownloader> fileDownloader =
665 NS_ENSURE_SUCCESS(rv, rv);
666 rv = fileDownloader->SetSourceURI(aURI);
667 NS_ENSURE_SUCCESS(rv, rv);
668 rv = fileDownloader->SetListener(
this);
669 NS_ENSURE_SUCCESS(rv, rv);
673 if (NS_SUCCEEDED(rv)) {
674 nsCAutoString fileExtension;
675 rv = url->GetFileExtension(fileExtension);
676 NS_ENSURE_SUCCESS(rv, rv);
677 rv = fileDownloader->SetDestinationFileExtension
678 (NS_ConvertUTF8toUTF16(fileExtension));
679 NS_ENSURE_SUCCESS(rv, rv);
684 rv = GetTemporaryFileFactory(getter_AddRefs(temporaryFileFactory));
685 NS_ENSURE_SUCCESS(rv, rv);
686 if (temporaryFileFactory) {
687 rv = fileDownloader->SetTemporaryFileFactory(temporaryFileFactory);
688 NS_ENSURE_SUCCESS(rv, rv);
693 nsAutoLock autoLock(
mLock);
698 rv = fileDownloader->Start();
699 NS_ENSURE_SUCCESS(rv, rv);
713 NS_ENSURE_SUCCESS(rv, rv);
728 NS_ENSURE_ARG_POINTER(aURI);
734 nsCOMPtr<sbIMediaItem> mediaItem;
736 nsAutoLock autoLock(
mLock);
741 rv = mediaItem->GetContentSrc(aURI);
742 NS_ENSURE_SUCCESS(rv, rv);
756 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
762 mLock = nsAutoLock::NewLock(
"sbBaseMediaItemDownloadJob::mLock");
763 NS_ENSURE_TRUE(
mLock, NS_ERROR_OUT_OF_MEMORY);
767 NS_ENSURE_SUCCESS(rv, rv);
781 mMediaItem(aMediaItem),
782 mTargetLibrary(aTargetLibrary),
787 if (!gBaseMediaItemDownloadJobLog) {
788 gBaseMediaItemDownloadJobLog =
789 PR_NewLogModule(
"sbBaseMediaItemDownloadJob");
793 TRACE((
"%s[%.8x]", __FUNCTION__,
this));
readonly attribute unsigned long errorCount
Number of errors that have been encountered.
function succeeded(ch, cx, status, data)
Generic interface for exposing long running jobs to the UI.
const unsigned short STATUS_SUCCEEDED
Constant indicating that the job has completed.
const sbCreateProxiedComponent do_ProxiedCreateInstance(const nsCID &aCID, nsresult *error=0)
const unsigned short STATUS_RUNNING
Constant indicating that the job is active.
sbMainThreadQueryInterface do_MainThreadQueryInterface(nsISupports *aSupports, nsresult *aResult=nsnull)
Media library abstraction.
readonly attribute unsigned long progress
Number of work units completed.
Implemented to receive notifications from sbIJobProgress interfaces.
const unsigned short STATUS_FAILED
Constant indicating that the job has completed with errors.
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
_getSelectedPageStyle s i