Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef downloadproxy___h___ |
michael@0 | 7 | #define downloadproxy___h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIDownloadManager.h" |
michael@0 | 10 | #include "nsIPrefBranch.h" |
michael@0 | 11 | #include "nsIPrefService.h" |
michael@0 | 12 | #include "nsIMIMEInfo.h" |
michael@0 | 13 | #include "nsIFileURL.h" |
michael@0 | 14 | #include "nsIDownloadManagerUI.h" |
michael@0 | 15 | |
michael@0 | 16 | #define PREF_BDM_SHOWWHENSTARTING "browser.download.manager.showWhenStarting" |
michael@0 | 17 | #define PREF_BDM_FOCUSWHENSTARTING "browser.download.manager.focusWhenStarting" |
michael@0 | 18 | |
michael@0 | 19 | // This class only exists because nsDownload cannot inherit from nsITransfer |
michael@0 | 20 | // directly. The reason for this is that nsDownloadManager (incorrectly) keeps |
michael@0 | 21 | // an nsCOMArray of nsDownloads, and nsCOMArray is only intended for use with |
michael@0 | 22 | // abstract classes. Using a concrete class that multiply inherits from classes |
michael@0 | 23 | // deriving from nsISupports will throw ambiguous base class errors. |
michael@0 | 24 | class nsDownloadProxy : public nsITransfer |
michael@0 | 25 | { |
michael@0 | 26 | public: |
michael@0 | 27 | |
michael@0 | 28 | nsDownloadProxy() { } |
michael@0 | 29 | virtual ~nsDownloadProxy() { } |
michael@0 | 30 | |
michael@0 | 31 | NS_DECL_ISUPPORTS |
michael@0 | 32 | |
michael@0 | 33 | NS_IMETHODIMP Init(nsIURI* aSource, |
michael@0 | 34 | nsIURI* aTarget, |
michael@0 | 35 | const nsAString& aDisplayName, |
michael@0 | 36 | nsIMIMEInfo *aMIMEInfo, |
michael@0 | 37 | PRTime aStartTime, |
michael@0 | 38 | nsIFile* aTempFile, |
michael@0 | 39 | nsICancelable* aCancelable, |
michael@0 | 40 | bool aIsPrivate) { |
michael@0 | 41 | nsresult rv; |
michael@0 | 42 | nsCOMPtr<nsIDownloadManager> dm = do_GetService("@mozilla.org/download-manager;1", &rv); |
michael@0 | 43 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 44 | |
michael@0 | 45 | rv = dm->AddDownload(nsIDownloadManager::DOWNLOAD_TYPE_DOWNLOAD, aSource, |
michael@0 | 46 | aTarget, aDisplayName, aMIMEInfo, aStartTime, |
michael@0 | 47 | aTempFile, aCancelable, aIsPrivate, |
michael@0 | 48 | getter_AddRefs(mInner)); |
michael@0 | 49 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 50 | |
michael@0 | 51 | nsCOMPtr<nsIPrefService> prefs = do_GetService("@mozilla.org/preferences-service;1", &rv); |
michael@0 | 52 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 53 | nsCOMPtr<nsIPrefBranch> branch = do_QueryInterface(prefs); |
michael@0 | 54 | |
michael@0 | 55 | bool showDM = true; |
michael@0 | 56 | if (branch) |
michael@0 | 57 | branch->GetBoolPref(PREF_BDM_SHOWWHENSTARTING, &showDM); |
michael@0 | 58 | |
michael@0 | 59 | if (showDM) { |
michael@0 | 60 | nsCOMPtr<nsIDownloadManagerUI> dmui = |
michael@0 | 61 | do_GetService("@mozilla.org/download-manager-ui;1", &rv); |
michael@0 | 62 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 63 | |
michael@0 | 64 | bool visible; |
michael@0 | 65 | rv = dmui->GetVisible(&visible); |
michael@0 | 66 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 67 | |
michael@0 | 68 | bool focusWhenStarting = true; |
michael@0 | 69 | if (branch) |
michael@0 | 70 | (void)branch->GetBoolPref(PREF_BDM_FOCUSWHENSTARTING, &focusWhenStarting); |
michael@0 | 71 | |
michael@0 | 72 | if (visible && !focusWhenStarting) |
michael@0 | 73 | return NS_OK; |
michael@0 | 74 | |
michael@0 | 75 | return dmui->Show(nullptr, mInner, nsIDownloadManagerUI::REASON_NEW_DOWNLOAD, aIsPrivate); |
michael@0 | 76 | } |
michael@0 | 77 | return rv; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | NS_IMETHODIMP OnStateChange(nsIWebProgress* aWebProgress, |
michael@0 | 81 | nsIRequest* aRequest, uint32_t aStateFlags, |
michael@0 | 82 | nsresult aStatus) |
michael@0 | 83 | { |
michael@0 | 84 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 85 | return mInner->OnStateChange(aWebProgress, aRequest, aStateFlags, aStatus); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | NS_IMETHODIMP OnStatusChange(nsIWebProgress *aWebProgress, |
michael@0 | 89 | nsIRequest *aRequest, nsresult aStatus, |
michael@0 | 90 | const char16_t *aMessage) |
michael@0 | 91 | { |
michael@0 | 92 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 93 | return mInner->OnStatusChange(aWebProgress, aRequest, aStatus, aMessage); |
michael@0 | 94 | } |
michael@0 | 95 | |
michael@0 | 96 | NS_IMETHODIMP OnLocationChange(nsIWebProgress *aWebProgress, |
michael@0 | 97 | nsIRequest *aRequest, nsIURI *aLocation, |
michael@0 | 98 | uint32_t aFlags) |
michael@0 | 99 | { |
michael@0 | 100 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 101 | return mInner->OnLocationChange(aWebProgress, aRequest, aLocation, aFlags); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | NS_IMETHODIMP OnProgressChange(nsIWebProgress *aWebProgress, |
michael@0 | 105 | nsIRequest *aRequest, |
michael@0 | 106 | int32_t aCurSelfProgress, |
michael@0 | 107 | int32_t aMaxSelfProgress, |
michael@0 | 108 | int32_t aCurTotalProgress, |
michael@0 | 109 | int32_t aMaxTotalProgress) |
michael@0 | 110 | { |
michael@0 | 111 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 112 | return mInner->OnProgressChange(aWebProgress, aRequest, |
michael@0 | 113 | aCurSelfProgress, |
michael@0 | 114 | aMaxSelfProgress, |
michael@0 | 115 | aCurTotalProgress, |
michael@0 | 116 | aMaxTotalProgress); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | NS_IMETHODIMP OnProgressChange64(nsIWebProgress *aWebProgress, |
michael@0 | 120 | nsIRequest *aRequest, |
michael@0 | 121 | int64_t aCurSelfProgress, |
michael@0 | 122 | int64_t aMaxSelfProgress, |
michael@0 | 123 | int64_t aCurTotalProgress, |
michael@0 | 124 | int64_t aMaxTotalProgress) |
michael@0 | 125 | { |
michael@0 | 126 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 127 | return mInner->OnProgressChange64(aWebProgress, aRequest, |
michael@0 | 128 | aCurSelfProgress, |
michael@0 | 129 | aMaxSelfProgress, |
michael@0 | 130 | aCurTotalProgress, |
michael@0 | 131 | aMaxTotalProgress); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP OnRefreshAttempted(nsIWebProgress *aWebProgress, |
michael@0 | 135 | nsIURI *aUri, |
michael@0 | 136 | int32_t aDelay, |
michael@0 | 137 | bool aSameUri, |
michael@0 | 138 | bool *allowRefresh) |
michael@0 | 139 | { |
michael@0 | 140 | *allowRefresh = true; |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | NS_IMETHODIMP OnSecurityChange(nsIWebProgress *aWebProgress, |
michael@0 | 145 | nsIRequest *aRequest, uint32_t aState) |
michael@0 | 146 | { |
michael@0 | 147 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 148 | return mInner->OnSecurityChange(aWebProgress, aRequest, aState); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | NS_IMETHODIMP SetSha256Hash(const nsACString& aHash) |
michael@0 | 152 | { |
michael@0 | 153 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 154 | return mInner->SetSha256Hash(aHash); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | NS_IMETHODIMP SetSignatureInfo(nsIArray* aSignatureInfo) |
michael@0 | 158 | { |
michael@0 | 159 | NS_ENSURE_TRUE(mInner, NS_ERROR_NOT_INITIALIZED); |
michael@0 | 160 | return mInner->SetSignatureInfo(aSignatureInfo); |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | private: |
michael@0 | 164 | nsCOMPtr<nsIDownload> mInner; |
michael@0 | 165 | }; |
michael@0 | 166 | |
michael@0 | 167 | NS_IMPL_ISUPPORTS(nsDownloadProxy, nsITransfer, |
michael@0 | 168 | nsIWebProgressListener, nsIWebProgressListener2) |
michael@0 | 169 | |
michael@0 | 170 | #endif |