michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef downloadproxy___h___ michael@0: #define downloadproxy___h___ michael@0: michael@0: #include "nsIDownloadManager.h" michael@0: #include "nsIPrefBranch.h" michael@0: #include "nsIPrefService.h" michael@0: #include "nsIMIMEInfo.h" michael@0: #include "nsIFileURL.h" michael@0: #include "nsIDownloadManagerUI.h" michael@0: michael@0: #define PREF_BDM_SHOWWHENSTARTING "browser.download.manager.showWhenStarting" michael@0: #define PREF_BDM_FOCUSWHENSTARTING "browser.download.manager.focusWhenStarting" michael@0: michael@0: // This class only exists because nsDownload cannot inherit from nsITransfer michael@0: // directly. The reason for this is that nsDownloadManager (incorrectly) keeps michael@0: // an nsCOMArray of nsDownloads, and nsCOMArray is only intended for use with michael@0: // abstract classes. Using a concrete class that multiply inherits from classes michael@0: // deriving from nsISupports will throw ambiguous base class errors. michael@0: class nsDownloadProxy : public nsITransfer michael@0: { michael@0: public: michael@0: michael@0: nsDownloadProxy() { } michael@0: virtual ~nsDownloadProxy() { } michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: NS_IMETHODIMP Init(nsIURI* aSource, michael@0: nsIURI* aTarget, michael@0: const nsAString& aDisplayName, michael@0: nsIMIMEInfo *aMIMEInfo, michael@0: PRTime aStartTime, michael@0: nsIFile* aTempFile, michael@0: nsICancelable* aCancelable, michael@0: bool aIsPrivate) { michael@0: nsresult rv; michael@0: nsCOMPtr dm = do_GetService("@mozilla.org/download-manager;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: rv = dm->AddDownload(nsIDownloadManager::DOWNLOAD_TYPE_DOWNLOAD, aSource, michael@0: aTarget, aDisplayName, aMIMEInfo, aStartTime, michael@0: aTempFile, aCancelable, aIsPrivate, michael@0: getter_AddRefs(mInner)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr prefs = do_GetService("@mozilla.org/preferences-service;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsCOMPtr branch = do_QueryInterface(prefs); michael@0: michael@0: bool showDM = true; michael@0: if (branch) michael@0: branch->GetBoolPref(PREF_BDM_SHOWWHENSTARTING, &showDM); michael@0: michael@0: if (showDM) { michael@0: nsCOMPtr dmui = michael@0: do_GetService("@mozilla.org/download-manager-ui;1", &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool visible; michael@0: rv = dmui->GetVisible(&visible); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: bool focusWhenStarting = true; michael@0: if (branch) michael@0: (void)branch->GetBoolPref(PREF_BDM_FOCUSWHENSTARTING, &focusWhenStarting); michael@0: michael@0: if (visible && !focusWhenStarting) michael@0: return NS_OK; michael@0: michael@0: return dmui->Show(nullptr, mInner, nsIDownloadManagerUI::REASON_NEW_DOWNLOAD, aIsPrivate); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP OnStateChange(nsIWebProgress* aWebProgress, michael@0: nsIRequest* aRequest, uint32_t aStateFlags, michael@0: nsresult aStatus) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); michael@0: } michael@0: michael@0: NS_IMETHODIMP OnStatusChange(nsIWebProgress *aWebProgress, michael@0: nsIRequest *aRequest, nsresult aStatus, michael@0: const char16_t *aMessage) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); michael@0: } michael@0: michael@0: NS_IMETHODIMP OnLocationChange(nsIWebProgress *aWebProgress, michael@0: nsIRequest *aRequest, nsIURI *aLocation, michael@0: uint32_t aFlags) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnLocationChange(aWebProgress, aRequest, aLocation, aFlags); michael@0: } michael@0: michael@0: NS_IMETHODIMP OnProgressChange(nsIWebProgress *aWebProgress, michael@0: nsIRequest *aRequest, michael@0: int32_t aCurSelfProgress, michael@0: int32_t aMaxSelfProgress, michael@0: int32_t aCurTotalProgress, michael@0: int32_t aMaxTotalProgress) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnProgressChange(aWebProgress, aRequest, michael@0: aCurSelfProgress, michael@0: aMaxSelfProgress, michael@0: aCurTotalProgress, michael@0: aMaxTotalProgress); michael@0: } michael@0: michael@0: NS_IMETHODIMP OnProgressChange64(nsIWebProgress *aWebProgress, michael@0: nsIRequest *aRequest, michael@0: int64_t aCurSelfProgress, michael@0: int64_t aMaxSelfProgress, michael@0: int64_t aCurTotalProgress, michael@0: int64_t aMaxTotalProgress) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnProgressChange64(aWebProgress, aRequest, michael@0: aCurSelfProgress, michael@0: aMaxSelfProgress, michael@0: aCurTotalProgress, michael@0: aMaxTotalProgress); michael@0: } michael@0: michael@0: NS_IMETHODIMP OnRefreshAttempted(nsIWebProgress *aWebProgress, michael@0: nsIURI *aUri, michael@0: int32_t aDelay, michael@0: bool aSameUri, michael@0: bool *allowRefresh) michael@0: { michael@0: *allowRefresh = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP OnSecurityChange(nsIWebProgress *aWebProgress, michael@0: nsIRequest *aRequest, uint32_t aState) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->OnSecurityChange(aWebProgress, aRequest, aState); michael@0: } michael@0: michael@0: NS_IMETHODIMP SetSha256Hash(const nsACString& aHash) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->SetSha256Hash(aHash); michael@0: } michael@0: michael@0: NS_IMETHODIMP SetSignatureInfo(nsIArray* aSignatureInfo) michael@0: { michael@0: NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); michael@0: return mInner->SetSignatureInfo(aSignatureInfo); michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mInner; michael@0: }; michael@0: michael@0: NS_IMPL_ISUPPORTS(nsDownloadProxy, nsITransfer, michael@0: nsIWebProgressListener, nsIWebProgressListener2) michael@0: michael@0: #endif