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