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 "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 // responsible for providing external dependencies and prompting the user if | 45 // responsible for providing external dependencies and prompting the user if |
46 // necessary. | 46 // necessary. |
47 // | 47 // |
48 // The only public entrypoint is the static Start() method which creates an | 48 // The only public entrypoint is the static Start() method which creates an |
49 // instance of DownloadTargetDeterminer. | 49 // instance of DownloadTargetDeterminer. |
50 class DownloadTargetDeterminer | 50 class DownloadTargetDeterminer |
51 : public content::DownloadItem::Observer { | 51 : public content::DownloadItem::Observer { |
52 public: | 52 public: |
53 // Start the process of determing the target of |download|. | 53 // Start the process of determing the target of |download|. |
54 // | 54 // |
| 55 // |initial_virtual_path| if non-empty, defines the initial virtual path for |
| 56 // the target determination process. If one isn't specified, one will be |
| 57 // generated based on the response data specified in |download| and the |
| 58 // users' downloads directory. |
| 59 // Note: |initial_virtual_path| is only used if download has prompted the |
| 60 // user before and doesn't have a forced path. |
55 // |download_prefs| is required and must outlive |download|. It is used for | 61 // |download_prefs| is required and must outlive |download|. It is used for |
56 // determining the user's preferences regarding the default downloads | 62 // determining the user's preferences regarding the default downloads |
57 // directory, prompting and auto-open behavior. | 63 // directory, prompting and auto-open behavior. |
58 // |delegate| is required and must live until |callback| is invoked. | 64 // |delegate| is required and must live until |callback| is invoked. |
59 // |callback| will be scheduled asynchronously on the UI thread after download | 65 // |callback| will be scheduled asynchronously on the UI thread after download |
60 // determination is complete or after |download| is destroyed. | 66 // determination is complete or after |download| is destroyed. |
61 // | 67 // |
62 // Start() should be called on the UI thread. | 68 // Start() should be called on the UI thread. |
63 static void Start(content::DownloadItem* download, | 69 static void Start(content::DownloadItem* download, |
| 70 const base::FilePath& initial_virtual_path, |
64 DownloadPrefs* download_prefs, | 71 DownloadPrefs* download_prefs, |
65 DownloadTargetDeterminerDelegate* delegate, | 72 DownloadTargetDeterminerDelegate* delegate, |
66 const content::DownloadTargetCallback& callback); | 73 const content::DownloadTargetCallback& callback); |
67 | 74 |
68 // Returns a .crdownload intermediate path for the |suggested_path|. | 75 // Returns a .crdownload intermediate path for the |suggested_path|. |
69 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); | 76 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); |
70 | 77 |
71 private: | 78 private: |
72 // The main workflow is controlled via a set of state transitions. Each state | 79 // The main workflow is controlled via a set of state transitions. Each state |
73 // has an associated handler. The handler for STATE_FOO is DoFoo. Each handler | 80 // has an associated handler. The handler for STATE_FOO is DoFoo. Each handler |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // referrer URL for the download prior to today. | 114 // referrer URL for the download prior to today. |
108 enum PriorVisitsToReferrer { | 115 enum PriorVisitsToReferrer { |
109 NO_VISITS_TO_REFERRER, | 116 NO_VISITS_TO_REFERRER, |
110 VISITED_REFERRER, | 117 VISITED_REFERRER, |
111 }; | 118 }; |
112 | 119 |
113 // Construct a DownloadTargetDeterminer object. Constraints on the arguments | 120 // Construct a DownloadTargetDeterminer object. Constraints on the arguments |
114 // are as per Start() above. | 121 // are as per Start() above. |
115 DownloadTargetDeterminer( | 122 DownloadTargetDeterminer( |
116 content::DownloadItem* download, | 123 content::DownloadItem* download, |
| 124 const base::FilePath& initial_virtual_path, |
117 DownloadPrefs* download_prefs, | 125 DownloadPrefs* download_prefs, |
118 DownloadTargetDeterminerDelegate* delegate, | 126 DownloadTargetDeterminerDelegate* delegate, |
119 const content::DownloadTargetCallback& callback); | 127 const content::DownloadTargetCallback& callback); |
120 | 128 |
121 virtual ~DownloadTargetDeterminer(); | 129 virtual ~DownloadTargetDeterminer(); |
122 | 130 |
123 // Invoke each successive handler until a handler returns QUIT_DOLOOP or | 131 // Invoke each successive handler until a handler returns QUIT_DOLOOP or |
124 // COMPLETE. Note that as a result, this object might be deleted. So |this| | 132 // COMPLETE. Note that as a result, this object might be deleted. So |this| |
125 // should not be accessed after calling DoLoop(). | 133 // should not be accessed after calling DoLoop(). |
126 void DoLoop(); | 134 void DoLoop(); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 // === End of main workflow === | 215 // === End of main workflow === |
208 | 216 |
209 // Utilities: | 217 // Utilities: |
210 | 218 |
211 void ScheduleCallbackAndDeleteSelf(); | 219 void ScheduleCallbackAndDeleteSelf(); |
212 | 220 |
213 void CancelOnFailureAndDeleteSelf(); | 221 void CancelOnFailureAndDeleteSelf(); |
214 | 222 |
215 Profile* GetProfile(); | 223 Profile* GetProfile(); |
216 | 224 |
217 bool ShouldPromptForDownload(const base::FilePath& filename); | 225 // Determine whether to prompt the user for the download location. For regular |
| 226 // downloads, this determination is based on the target disposition, auto-open |
| 227 // behavior, among other factors. For an interrupted download, this |
| 228 // determination will be based on the interrupt reason. It is assumed that |
| 229 // download interruptions always occur after the first round of download |
| 230 // target determination is complete. |
| 231 bool ShouldPromptForDownload(const base::FilePath& filename) const; |
| 232 |
| 233 // Returns true if the user has been prompted for this download at least once |
| 234 // prior to this target determination operation. This method is only expected |
| 235 // to return true for a resuming interrupted download that has prompted the |
| 236 // user before interruption. The return value does not depend on whether the |
| 237 // user will be or has been prompted during the current target determination |
| 238 // operation. |
| 239 bool HasPromptedForPath() const; |
218 | 240 |
219 // Returns true if this download should show the "dangerous file" warning. | 241 // Returns true if this download should show the "dangerous file" warning. |
220 // Various factors are considered, such as the type of the file, whether a | 242 // Various factors are considered, such as the type of the file, whether a |
221 // user action initiated the download, and whether the user has explicitly | 243 // user action initiated the download, and whether the user has explicitly |
222 // marked the file type as "auto open". Protected virtual for testing. | 244 // marked the file type as "auto open". Protected virtual for testing. |
223 bool IsDangerousFile(PriorVisitsToReferrer visits); | 245 bool IsDangerousFile(PriorVisitsToReferrer visits); |
224 | 246 |
225 // content::DownloadItem::Observer | 247 // content::DownloadItem::Observer |
226 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; | 248 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; |
227 | 249 |
228 // state | 250 // state |
229 State next_state_; | 251 State next_state_; |
230 bool should_prompt_; | 252 bool should_prompt_; |
231 bool create_directory_; | 253 bool should_notify_extensions_; |
| 254 bool create_target_directory_; |
232 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; | 255 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; |
233 content::DownloadDangerType danger_type_; | 256 content::DownloadDangerType danger_type_; |
234 base::FilePath virtual_path_; | 257 base::FilePath virtual_path_; |
235 base::FilePath local_path_; | 258 base::FilePath local_path_; |
236 base::FilePath intermediate_path_; | 259 base::FilePath intermediate_path_; |
237 | 260 |
238 content::DownloadItem* download_; | 261 content::DownloadItem* download_; |
| 262 const bool is_resumption_; |
239 DownloadPrefs* download_prefs_; | 263 DownloadPrefs* download_prefs_; |
240 DownloadTargetDeterminerDelegate* delegate_; | 264 DownloadTargetDeterminerDelegate* delegate_; |
241 content::DownloadTargetCallback completion_callback_; | 265 content::DownloadTargetCallback completion_callback_; |
242 CancelableRequestConsumer history_consumer_; | 266 CancelableRequestConsumer history_consumer_; |
243 | 267 |
244 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; | 268 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; |
245 | 269 |
246 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); | 270 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); |
247 }; | 271 }; |
248 | 272 |
249 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ | 273 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
OLD | NEW |