| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/common/cancelable_request.h" | |
| 13 #include "chrome/browser/download/download_path_reservation_tracker.h" | 12 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 13 #include "chrome/browser/download/download_target_determiner_delegate.h" |
| 14 #include "chrome/browser/safe_browsing/download_protection_service.h" | 14 #include "chrome/browser/safe_browsing/download_protection_service.h" |
| 15 #include "content/public/browser/download_danger_type.h" | 15 #include "content/public/browser/download_danger_type.h" |
| 16 #include "content/public/browser/download_item.h" | 16 #include "content/public/browser/download_item.h" |
| 17 #include "content/public/browser/download_manager_delegate.h" | 17 #include "content/public/browser/download_manager_delegate.h" |
| 18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
| 20 | 20 |
| 21 class DownloadPrefs; | 21 class DownloadPrefs; |
| 22 class PrefRegistrySyncable; | 22 class PrefRegistrySyncable; |
| 23 class Profile; | 23 class Profile; |
| (...skipping 14 matching lines...) Expand all Loading... |
| 38 return reinterpret_cast<std::size_t>(p); | 38 return reinterpret_cast<std::size_t>(p); |
| 39 } | 39 } |
| 40 }; | 40 }; |
| 41 } // namespace BASE_HASH_NAMESPACE | 41 } // namespace BASE_HASH_NAMESPACE |
| 42 #endif | 42 #endif |
| 43 | 43 |
| 44 // This is the Chrome side helper for the download system. | 44 // This is the Chrome side helper for the download system. |
| 45 class ChromeDownloadManagerDelegate | 45 class ChromeDownloadManagerDelegate |
| 46 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>, | 46 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>, |
| 47 public content::DownloadManagerDelegate, | 47 public content::DownloadManagerDelegate, |
| 48 public content::NotificationObserver { | 48 public content::NotificationObserver, |
| 49 public DownloadTargetDeterminerDelegate { |
| 49 public: | 50 public: |
| 50 // Callback type used with ChooseDownloadPath(). The callback should be | |
| 51 // invoked with the user-selected path as the argument. If the file selection | |
| 52 // was canceled, the argument should be the empty path. | |
| 53 typedef base::Callback<void(const base::FilePath&)> FileSelectedCallback; | |
| 54 | |
| 55 explicit ChromeDownloadManagerDelegate(Profile* profile); | 51 explicit ChromeDownloadManagerDelegate(Profile* profile); |
| 56 | 52 |
| 57 static void RegisterUserPrefs(PrefRegistrySyncable* registry); | 53 static void RegisterUserPrefs(PrefRegistrySyncable* registry); |
| 58 | 54 |
| 59 // Should be called before the first call to ShouldCompleteDownload() to | 55 // Should be called before the first call to ShouldCompleteDownload() to |
| 60 // disable SafeBrowsing checks for |item|. | 56 // disable SafeBrowsing checks for |item|. |
| 61 static void DisableSafeBrowsing(content::DownloadItem* item); | 57 static void DisableSafeBrowsing(content::DownloadItem* item); |
| 62 | 58 |
| 63 void SetDownloadManager(content::DownloadManager* dm); | 59 void SetDownloadManager(content::DownloadManager* dm); |
| 64 | 60 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // Clears the last directory chosen by the user in response to a file chooser | 92 // Clears the last directory chosen by the user in response to a file chooser |
| 97 // prompt. Called when clearing recent history. | 93 // prompt. Called when clearing recent history. |
| 98 void ClearLastDownloadPath(); | 94 void ClearLastDownloadPath(); |
| 99 | 95 |
| 100 DownloadPrefs* download_prefs() { return download_prefs_.get(); } | 96 DownloadPrefs* download_prefs() { return download_prefs_.get(); } |
| 101 | 97 |
| 102 protected: | 98 protected: |
| 103 // So that test classes can inherit from this for override purposes. | 99 // So that test classes can inherit from this for override purposes. |
| 104 virtual ~ChromeDownloadManagerDelegate(); | 100 virtual ~ChromeDownloadManagerDelegate(); |
| 105 | 101 |
| 106 // Returns the SafeBrowsing download protection service if it's | |
| 107 // enabled. Returns NULL otherwise. Protected virtual for testing. | |
| 108 virtual safe_browsing::DownloadProtectionService* | |
| 109 GetDownloadProtectionService(); | |
| 110 | |
| 111 // Returns true if this download should show the "dangerous file" warning. | |
| 112 // Various factors are considered, such as the type of the file, whether a | |
| 113 // user action initiated the download, and whether the user has explicitly | |
| 114 // marked the file type as "auto open". Protected virtual for testing. | |
| 115 virtual bool IsDangerousFile(const content::DownloadItem& download, | |
| 116 const base::FilePath& suggested_path, | |
| 117 bool visited_referrer_before); | |
| 118 | |
| 119 // Obtains a path reservation by calling | |
| 120 // DownloadPathReservationTracker::GetReservedPath(). Protected virtual for | |
| 121 // testing. | |
| 122 virtual void GetReservedPath( | |
| 123 content::DownloadItem& download, | |
| 124 const base::FilePath& target_path, | |
| 125 const base::FilePath& default_download_path, | |
| 126 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | |
| 127 const DownloadPathReservationTracker::ReservedPathCallback& callback); | |
| 128 | |
| 129 // Displays the file chooser dialog to prompt the user for the download | |
| 130 // location for |item|. |suggested_path| will be used as the initial download | |
| 131 // path. Once a location is available |callback| will be invoked with the | |
| 132 // selected full path. If the user cancels the dialog, then an empty FilePath | |
| 133 // will be passed into |callback|. Protected virtual for testing. | |
| 134 virtual void ChooseDownloadPath(content::DownloadItem* item, | |
| 135 const base::FilePath& suggested_path, | |
| 136 const FileSelectedCallback& callback); | |
| 137 | |
| 138 // So that test classes that inherit from this for override purposes | 102 // So that test classes that inherit from this for override purposes |
| 139 // can call back into the DownloadManager. | 103 // can call back into the DownloadManager. |
| 140 scoped_refptr<content::DownloadManager> download_manager_; | 104 scoped_refptr<content::DownloadManager> download_manager_; |
| 141 | 105 |
| 106 virtual safe_browsing::DownloadProtectionService* |
| 107 GetDownloadProtectionService(); |
| 108 |
| 109 // DownloadTargetDeterminerDelegate. Protected for testing. |
| 110 virtual void NotifyExtensions( |
| 111 content::DownloadItem* download, |
| 112 const base::FilePath& suggested_virtual_path, |
| 113 const NotifyExtensionsCallback& callback) OVERRIDE; |
| 114 virtual void ReserveVirtualPath( |
| 115 content::DownloadItem* download, |
| 116 const base::FilePath& virtual_path, |
| 117 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 118 const ReservedPathCallback& callback) OVERRIDE; |
| 119 virtual void PromptUserForDownloadPath( |
| 120 content::DownloadItem* download, |
| 121 const base::FilePath& suggested_virtual_path, |
| 122 const FileSelectedCallback& callback) OVERRIDE; |
| 123 virtual void DetermineLocalPath( |
| 124 content::DownloadItem* download, |
| 125 const base::FilePath& virtual_path, |
| 126 const LocalPathCallback& callback) OVERRIDE; |
| 127 virtual void CheckDownloadUrl( |
| 128 content::DownloadItem* download, |
| 129 const base::FilePath& suggested_virtual_path, |
| 130 const CheckDownloadUrlCallback& callback) OVERRIDE; |
| 131 |
| 132 // Callback invoked by the DownloadFilePicker after PromptUserForDownloadPath. |
| 133 void OnDownloadPathSelected( |
| 134 const DownloadTargetDeterminerDelegate::FileSelectedCallback& callback, |
| 135 const base::FilePath& virtual_path); |
| 136 |
| 142 private: | 137 private: |
| 143 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>; | 138 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>; |
| 144 | 139 |
| 145 struct ContinueFilenameDeterminationInfo; | |
| 146 | |
| 147 // content::NotificationObserver implementation. | 140 // content::NotificationObserver implementation. |
| 148 virtual void Observe(int type, | 141 virtual void Observe(int type, |
| 149 const content::NotificationSource& source, | 142 const content::NotificationSource& source, |
| 150 const content::NotificationDetails& details) OVERRIDE; | 143 const content::NotificationDetails& details) OVERRIDE; |
| 151 | 144 |
| 152 // Callback function after url is checked with safebrowsing service. | |
| 153 void CheckDownloadUrlDone( | |
| 154 int32 download_id, | |
| 155 const content::DownloadTargetCallback& callback, | |
| 156 safe_browsing::DownloadProtectionService::DownloadCheckResult result); | |
| 157 | |
| 158 // Callback function after the DownloadProtectionService completes. | 145 // Callback function after the DownloadProtectionService completes. |
| 159 void CheckClientDownloadDone( | 146 void CheckClientDownloadDone( |
| 160 int32 download_id, | 147 int32 download_id, |
| 161 safe_browsing::DownloadProtectionService::DownloadCheckResult result); | 148 safe_browsing::DownloadProtectionService::DownloadCheckResult result); |
| 162 | 149 |
| 163 // Callback function after we check whether the referrer URL has been visited | |
| 164 // before today. Determines the danger state of the download based on the file | |
| 165 // type and |visited_referrer_before|. Generates a target path for the | |
| 166 // download. Invokes |DownloadPathReservationTracker::GetReservedPath| to get | |
| 167 // a reserved path for the download. The path is then passed into | |
| 168 // OnPathReservationAvailable(). | |
| 169 void CheckVisitedReferrerBeforeDone( | |
| 170 int32 download_id, | |
| 171 const content::DownloadTargetCallback& callback, | |
| 172 content::DownloadDangerType danger_type, | |
| 173 bool visited_referrer_before); | |
| 174 | |
| 175 #if defined (OS_CHROMEOS) | |
| 176 // DriveDownloadObserver::SubstituteDriveDownloadPath callback. Calls | |
| 177 // |DownloadPathReservationTracker::GetReservedPath| to get a reserved path | |
| 178 // for the download. The path is then passed into | |
| 179 // OnPathReservationAvailable(). | |
| 180 void SubstituteDriveDownloadPathCallback( | |
| 181 int32 download_id, | |
| 182 const content::DownloadTargetCallback& callback, | |
| 183 bool should_prompt, | |
| 184 DownloadPathReservationTracker::FilenameConflictAction conflict_action, | |
| 185 content::DownloadDangerType danger_type, | |
| 186 const base::FilePath& unverified_path); | |
| 187 #endif | |
| 188 | |
| 189 // Called on the UI thread once a reserved path is available. Updates the | |
| 190 // download identified by |download_id| with the |target_path|, target | |
| 191 // disposition and |danger_type|. | |
| 192 void OnPathReservationAvailable( | |
| 193 int32 download_id, | |
| 194 const content::DownloadTargetCallback& callback, | |
| 195 bool should_prompt, | |
| 196 content::DownloadDangerType danger_type, | |
| 197 const base::FilePath& reserved_path, | |
| 198 bool reserved_path_verified); | |
| 199 | |
| 200 // When an extension opts to change a download's target filename, this | |
| 201 // sanitizes it before continuing with the filename determination process. | |
| 202 void OnExtensionOverridingFilename( | |
| 203 const ContinueFilenameDeterminationInfo& continue_info, | |
| 204 const base::FilePath& changed_filename, | |
| 205 DownloadPathReservationTracker::FilenameConflictAction conflict_action); | |
| 206 | |
| 207 // When extensions either opt not to change a download's target filename, or | |
| 208 // the changed filename has been sanitized, this method continues with the | |
| 209 // filename determination process, optionally prompting the user to manually | |
| 210 // set the filename. | |
| 211 void ContinueDeterminingFilename( | |
| 212 const ContinueFilenameDeterminationInfo& continue_info, | |
| 213 const base::FilePath& suggested_path, | |
| 214 DownloadPathReservationTracker::FilenameConflictAction conflict_action); | |
| 215 | |
| 216 // Called on the UI thread once the final target path is available. | |
| 217 void OnTargetPathDetermined( | |
| 218 int32 download_id, | |
| 219 const content::DownloadTargetCallback& callback, | |
| 220 content::DownloadItem::TargetDisposition disposition, | |
| 221 content::DownloadDangerType danger_type, | |
| 222 const base::FilePath& target_path); | |
| 223 | |
| 224 // Internal gateways for ShouldCompleteDownload(). | 150 // Internal gateways for ShouldCompleteDownload(). |
| 225 bool IsDownloadReadyForCompletion( | 151 bool IsDownloadReadyForCompletion( |
| 226 content::DownloadItem* item, | 152 content::DownloadItem* item, |
| 227 const base::Closure& internal_complete_callback); | 153 const base::Closure& internal_complete_callback); |
| 228 void ShouldCompleteDownloadInternal( | 154 void ShouldCompleteDownloadInternal( |
| 229 int download_id, | 155 int download_id, |
| 230 const base::Closure& user_complete_callback); | 156 const base::Closure& user_complete_callback); |
| 231 | 157 |
| 232 Profile* profile_; | 158 Profile* profile_; |
| 233 int next_download_id_; | 159 int next_download_id_; |
| 234 scoped_ptr<DownloadPrefs> download_prefs_; | 160 scoped_ptr<DownloadPrefs> download_prefs_; |
| 235 | 161 |
| 236 // Maps from pending extension installations to DownloadItem IDs. | 162 // Maps from pending extension installations to DownloadItem IDs. |
| 237 typedef base::hash_map<extensions::CrxInstaller*, | 163 typedef base::hash_map<extensions::CrxInstaller*, |
| 238 content::DownloadOpenDelayedCallback> CrxInstallerMap; | 164 content::DownloadOpenDelayedCallback> CrxInstallerMap; |
| 239 CrxInstallerMap crx_installers_; | 165 CrxInstallerMap crx_installers_; |
| 240 | 166 |
| 241 CancelableRequestConsumer history_consumer_; | |
| 242 | |
| 243 content::NotificationRegistrar registrar_; | 167 content::NotificationRegistrar registrar_; |
| 244 | 168 |
| 245 // The directory most recently chosen by the user in response to a Save As | 169 // The directory most recently chosen by the user in response to a Save As |
| 246 // dialog for a regular download. | 170 // dialog for a regular download. |
| 247 base::FilePath last_download_path_; | 171 base::FilePath last_download_path_; |
| 248 | 172 |
| 249 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); | 173 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); |
| 250 }; | 174 }; |
| 251 | 175 |
| 252 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 176 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
| OLD | NEW |