| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_DOWNLOAD_TARGET_DETERMINER_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> |
| 9 | 10 |
| 10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/task/cancelable_task_tracker.h" | 15 #include "base/task/cancelable_task_tracker.h" |
| 15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
| 16 #include "chrome/browser/download/download_path_reservation_tracker.h" | 17 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 17 #include "chrome/browser/download/download_target_determiner_delegate.h" | 18 #include "chrome/browser/download/download_target_determiner_delegate.h" |
| 18 #include "chrome/browser/download/download_target_info.h" | 19 #include "chrome/browser/download/download_target_info.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 48 // determining the download target. It observes the DownloadItem and aborts the | 49 // determining the download target. It observes the DownloadItem and aborts the |
| 49 // process if the download is removed. DownloadTargetDeterminerDelegate is | 50 // process if the download is removed. DownloadTargetDeterminerDelegate is |
| 50 // responsible for providing external dependencies and prompting the user if | 51 // responsible for providing external dependencies and prompting the user if |
| 51 // necessary. | 52 // necessary. |
| 52 // | 53 // |
| 53 // The only public entrypoint is the static Start() method which creates an | 54 // The only public entrypoint is the static Start() method which creates an |
| 54 // instance of DownloadTargetDeterminer. | 55 // instance of DownloadTargetDeterminer. |
| 55 class DownloadTargetDeterminer | 56 class DownloadTargetDeterminer |
| 56 : public content::DownloadItem::Observer { | 57 : public content::DownloadItem::Observer { |
| 57 public: | 58 public: |
| 58 typedef base::Callback<void(std::unique_ptr<DownloadTargetInfo>)> | 59 using CompletionCallback = |
| 59 CompletionCallback; | 60 base::Callback<void(std::unique_ptr<DownloadTargetInfo>)>; |
| 60 | 61 |
| 61 // Start the process of determing the target of |download|. | 62 // Start the process of determing the target of |download|. |
| 62 // | 63 // |
| 63 // |initial_virtual_path| if non-empty, defines the initial virtual path for | 64 // |initial_virtual_path| if non-empty, defines the initial virtual path for |
| 64 // the target determination process. If one isn't specified, one will be | 65 // the target determination process. If one isn't specified, one will be |
| 65 // generated based on the response data specified in |download| and the | 66 // generated based on the response data specified in |download| and the |
| 66 // users' downloads directory. | 67 // users' downloads directory. |
| 67 // Note: |initial_virtual_path| is only used if download has prompted the | 68 // Note: |initial_virtual_path| is only used if download has prompted the |
| 68 // user before and doesn't have a forced path. | 69 // user before and doesn't have a forced path. |
| 69 // |download_prefs| is required and must outlive |download|. It is used for | 70 // |download_prefs| is required and must outlive |download|. It is used for |
| 70 // determining the user's preferences regarding the default downloads | 71 // determining the user's preferences regarding the default downloads |
| 71 // directory, prompting and auto-open behavior. | 72 // directory, prompting and auto-open behavior. |
| 72 // |delegate| is required and must live until |callback| is invoked. | 73 // |delegate| is required and must live until |callback| is invoked. |
| 73 // |callback| will be scheduled asynchronously on the UI thread after download | 74 // |callback| will be scheduled asynchronously on the UI thread after download |
| 74 // determination is complete or after |download| is destroyed. | 75 // determination is complete or after |download| is destroyed. |
| 75 // | 76 // |
| 76 // Start() should be called on the UI thread. | 77 // Start() should be called on the UI thread. |
| 77 static void Start(content::DownloadItem* download, | 78 static void Start( |
| 78 const base::FilePath& initial_virtual_path, | 79 content::DownloadItem* download, |
| 79 DownloadPrefs* download_prefs, | 80 const base::FilePath& initial_virtual_path, |
| 80 DownloadTargetDeterminerDelegate* delegate, | 81 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 81 const CompletionCallback& callback); | 82 DownloadPrefs* download_prefs, |
| 83 DownloadTargetDeterminerDelegate* delegate, |
| 84 const CompletionCallback& callback); |
| 82 | 85 |
| 83 // Returns a .crdownload intermediate path for the |suggested_path|. | 86 // Returns a .crdownload intermediate path for the |suggested_path|. |
| 84 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); | 87 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); |
| 85 | 88 |
| 86 #if defined(OS_WIN) | 89 #if defined(OS_WIN) |
| 87 // Returns true if Adobe Reader is up to date. This information refreshed | 90 // Returns true if Adobe Reader is up to date. This information refreshed |
| 88 // only when Start() gets called for a PDF and Adobe Reader is the default | 91 // only when Start() gets called for a PDF and Adobe Reader is the default |
| 89 // System PDF viewer. | 92 // System PDF viewer. |
| 90 static bool IsAdobeReaderUpToDate(); | 93 static bool IsAdobeReaderUpToDate(); |
| 91 #endif | 94 #endif |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 | 133 |
| 131 // Used with GetDangerLevel to indicate whether the user has visited the | 134 // Used with GetDangerLevel to indicate whether the user has visited the |
| 132 // referrer URL for the download prior to today. | 135 // referrer URL for the download prior to today. |
| 133 enum PriorVisitsToReferrer { | 136 enum PriorVisitsToReferrer { |
| 134 NO_VISITS_TO_REFERRER, | 137 NO_VISITS_TO_REFERRER, |
| 135 VISITED_REFERRER, | 138 VISITED_REFERRER, |
| 136 }; | 139 }; |
| 137 | 140 |
| 138 // Construct a DownloadTargetDeterminer object. Constraints on the arguments | 141 // Construct a DownloadTargetDeterminer object. Constraints on the arguments |
| 139 // are as per Start() above. | 142 // are as per Start() above. |
| 140 DownloadTargetDeterminer(content::DownloadItem* download, | 143 DownloadTargetDeterminer( |
| 141 const base::FilePath& initial_virtual_path, | 144 content::DownloadItem* download, |
| 142 DownloadPrefs* download_prefs, | 145 const base::FilePath& initial_virtual_path, |
| 143 DownloadTargetDeterminerDelegate* delegate, | 146 DownloadPathReservationTracker::FilenameConflictAction conflict_action, |
| 144 const CompletionCallback& callback); | 147 DownloadPrefs* download_prefs, |
| 148 DownloadTargetDeterminerDelegate* delegate, |
| 149 const CompletionCallback& callback); |
| 145 | 150 |
| 146 ~DownloadTargetDeterminer() override; | 151 ~DownloadTargetDeterminer() override; |
| 147 | 152 |
| 148 // Invoke each successive handler until a handler returns QUIT_DOLOOP or | 153 // Invoke each successive handler until a handler returns QUIT_DOLOOP or |
| 149 // COMPLETE. Note that as a result, this object might be deleted. So |this| | 154 // COMPLETE. Note that as a result, this object might be deleted. So |this| |
| 150 // should not be accessed after calling DoLoop(). | 155 // should not be accessed after calling DoLoop(). |
| 151 void DoLoop(); | 156 void DoLoop(); |
| 152 | 157 |
| 153 // === Main workflow === | 158 // === Main workflow === |
| 154 | 159 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 172 const base::FilePath& new_path, | 177 const base::FilePath& new_path, |
| 173 DownloadPathReservationTracker::FilenameConflictAction conflict_action); | 178 DownloadPathReservationTracker::FilenameConflictAction conflict_action); |
| 174 | 179 |
| 175 // Invokes ReserveVirtualPath() on the delegate to acquire a reservation for | 180 // Invokes ReserveVirtualPath() on the delegate to acquire a reservation for |
| 176 // the path. See DownloadPathReservationTracker. | 181 // the path. See DownloadPathReservationTracker. |
| 177 // Next state: | 182 // Next state: |
| 178 // - STATE_PROMPT_USER_FOR_DOWNLOAD_PATH. | 183 // - STATE_PROMPT_USER_FOR_DOWNLOAD_PATH. |
| 179 Result DoReserveVirtualPath(); | 184 Result DoReserveVirtualPath(); |
| 180 | 185 |
| 181 // Callback invoked after the delegate aquires a path reservation. | 186 // Callback invoked after the delegate aquires a path reservation. |
| 182 void ReserveVirtualPathDone(const base::FilePath& path, bool verified); | 187 void ReserveVirtualPathDone(PathValidationResult result, |
| 188 const base::FilePath& path); |
| 183 | 189 |
| 184 // Presents a file picker to the user if necessary. | 190 // Presents a file picker to the user if necessary. |
| 185 // Next state: | 191 // Next state: |
| 186 // - STATE_DETERMINE_LOCAL_PATH. | 192 // - STATE_DETERMINE_LOCAL_PATH. |
| 187 Result DoPromptUserForDownloadPath(); | 193 Result DoRequestConfirmation(); |
| 188 | 194 |
| 189 // Callback invoked after the file picker completes. Cancels the download if | 195 // Callback invoked after the file picker completes. Cancels the download if |
| 190 // the user cancels the file picker. | 196 // the user cancels the file picker. |
| 191 void PromptUserForDownloadPathDone(const base::FilePath& virtual_path); | 197 void RequestConfirmationDone(DownloadConfirmationResult result, |
| 198 const base::FilePath& virtual_path); |
| 192 | 199 |
| 193 // Up until this point, the path that was used is considered to be a virtual | 200 // Up until this point, the path that was used is considered to be a virtual |
| 194 // path. This step determines the local file system path corresponding to this | 201 // path. This step determines the local file system path corresponding to this |
| 195 // virtual path. The translation is done by invoking the DetermineLocalPath() | 202 // virtual path. The translation is done by invoking the DetermineLocalPath() |
| 196 // method on the delegate. | 203 // method on the delegate. |
| 197 // Next state: | 204 // Next state: |
| 198 // - STATE_DETERMINE_MIME_TYPE. | 205 // - STATE_DETERMINE_MIME_TYPE. |
| 199 Result DoDetermineLocalPath(); | 206 Result DoDetermineLocalPath(); |
| 200 | 207 |
| 201 // Callback invoked when the delegate has determined local path. | 208 // Callback invoked when the delegate has determined local path. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // target determination is complete. The determination assumes that the | 270 // target determination is complete. The determination assumes that the |
| 264 // intermediate file will never be overwritten (always uniquified if needed). | 271 // intermediate file will never be overwritten (always uniquified if needed). |
| 265 // Next state: | 272 // Next state: |
| 266 // - STATE_NONE: Returns COMPLETE. | 273 // - STATE_NONE: Returns COMPLETE. |
| 267 Result DoDetermineIntermediatePath(); | 274 Result DoDetermineIntermediatePath(); |
| 268 | 275 |
| 269 // === End of main workflow === | 276 // === End of main workflow === |
| 270 | 277 |
| 271 // Utilities: | 278 // Utilities: |
| 272 | 279 |
| 273 void ScheduleCallbackAndDeleteSelf(); | 280 // Schedules the completion callback to be run on the UI thread and deletes |
| 274 | 281 // this object. The determined target info will be passed into the callback |
| 275 void CancelOnFailureAndDeleteSelf(); | 282 // if |interrupt_reason| is NONE. Otherwise, only the interrupt reason will be |
| 283 // passed on. |
| 284 void ScheduleCallbackAndDeleteSelf(content::DownloadInterruptReason result); |
| 276 | 285 |
| 277 Profile* GetProfile() const; | 286 Profile* GetProfile() const; |
| 278 | 287 |
| 279 // Determine whether to prompt the user for the download location. For regular | 288 // Determine whether to prompt the user for the download location. For regular |
| 280 // downloads, this determination is based on the target disposition, auto-open | 289 // downloads, this determination is based on the target disposition, auto-open |
| 281 // behavior, among other factors. For an interrupted download, this | 290 // behavior, among other factors. For an interrupted download, this |
| 282 // determination will be based on the interrupt reason. It is assumed that | 291 // determination will be based on the interrupt reason. It is assumed that |
| 283 // download interruptions always occur after the first round of download | 292 // download interruptions always occur after the first round of download |
| 284 // target determination is complete. | 293 // target determination is complete. |
| 285 bool ShouldPromptForDownload(const base::FilePath& filename) const; | 294 DownloadConfirmationReason ShouldPromptForDownload( |
| 295 const base::FilePath& filename) const; |
| 286 | 296 |
| 287 // Returns true if the user has been prompted for this download at least once | 297 // Returns true if the user has been prompted for this download at least once |
| 288 // prior to this target determination operation. This method is only expected | 298 // prior to this target determination operation. This method is only expected |
| 289 // to return true for a resuming interrupted download that has prompted the | 299 // to return true for a resuming interrupted download that has prompted the |
| 290 // user before interruption. The return value does not depend on whether the | 300 // user before interruption. The return value does not depend on whether the |
| 291 // user will be or has been prompted during the current target determination | 301 // user will be or has been prompted during the current target determination |
| 292 // operation. | 302 // operation. |
| 293 bool HasPromptedForPath() const; | 303 bool HasPromptedForPath() const; |
| 294 | 304 |
| 295 // Returns true if this download should show the "dangerous file" warning. | 305 // Returns true if this download should show the "dangerous file" warning. |
| 296 // Various factors are considered, such as the type of the file, whether a | 306 // Various factors are considered, such as the type of the file, whether a |
| 297 // user action initiated the download, and whether the user has explicitly | 307 // user action initiated the download, and whether the user has explicitly |
| 298 // marked the file type as "auto open". Protected virtual for testing. | 308 // marked the file type as "auto open". Protected virtual for testing. |
| 299 // | 309 // |
| 300 // If |require_explicit_consent| is non-null then the pointed bool will be set | 310 // If |require_explicit_consent| is non-null then the pointed bool will be set |
| 301 // to true if the download requires explicit user consent. | 311 // to true if the download requires explicit user consent. |
| 302 safe_browsing::DownloadFileType::DangerLevel GetDangerLevel( | 312 safe_browsing::DownloadFileType::DangerLevel GetDangerLevel( |
| 303 PriorVisitsToReferrer visits) const; | 313 PriorVisitsToReferrer visits) const; |
| 304 | 314 |
| 305 // content::DownloadItem::Observer | 315 // content::DownloadItem::Observer |
| 306 void OnDownloadDestroyed(content::DownloadItem* download) override; | 316 void OnDownloadDestroyed(content::DownloadItem* download) override; |
| 307 | 317 |
| 308 // state | 318 // state |
| 309 State next_state_; | 319 State next_state_; |
| 310 bool should_prompt_; | 320 DownloadConfirmationReason confirmation_reason_; |
| 311 bool should_notify_extensions_; | 321 bool should_notify_extensions_; |
| 312 bool create_target_directory_; | 322 bool create_target_directory_; |
| 313 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; | 323 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; |
| 314 content::DownloadDangerType danger_type_; | 324 content::DownloadDangerType danger_type_; |
| 315 safe_browsing::DownloadFileType::DangerLevel danger_level_; | 325 safe_browsing::DownloadFileType::DangerLevel danger_level_; |
| 316 base::FilePath virtual_path_; | 326 base::FilePath virtual_path_; |
| 317 base::FilePath local_path_; | 327 base::FilePath local_path_; |
| 318 base::FilePath intermediate_path_; | 328 base::FilePath intermediate_path_; |
| 319 std::string mime_type_; | 329 std::string mime_type_; |
| 320 bool is_filetype_handled_safely_; | 330 bool is_filetype_handled_safely_; |
| 321 | 331 |
| 322 content::DownloadItem* download_; | 332 content::DownloadItem* download_; |
| 323 const bool is_resumption_; | 333 const bool is_resumption_; |
| 324 DownloadPrefs* download_prefs_; | 334 DownloadPrefs* download_prefs_; |
| 325 DownloadTargetDeterminerDelegate* delegate_; | 335 DownloadTargetDeterminerDelegate* delegate_; |
| 326 CompletionCallback completion_callback_; | 336 CompletionCallback completion_callback_; |
| 327 base::CancelableTaskTracker history_tracker_; | 337 base::CancelableTaskTracker history_tracker_; |
| 328 | 338 |
| 329 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; | 339 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; |
| 330 | 340 |
| 331 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); | 341 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); |
| 332 }; | 342 }; |
| 333 | 343 |
| 334 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ | 344 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| OLD | NEW |