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" |
11 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
12 #include "chrome/browser/common/cancelable_request.h" | 12 #include "chrome/browser/common/cancelable_request.h" |
13 #include "chrome/browser/download/download_path_reservation_tracker.h" | 13 #include "chrome/browser/download/download_path_reservation_tracker.h" |
14 #include "chrome/browser/download/download_target_determiner_delegate.h" | 14 #include "chrome/browser/download/download_target_determiner_delegate.h" |
15 #include "chrome/browser/download/download_target_info.h" | |
16 #include "content/public/browser/download_danger_type.h" | 15 #include "content/public/browser/download_danger_type.h" |
17 #include "content/public/browser/download_item.h" | 16 #include "content/public/browser/download_item.h" |
18 #include "content/public/browser/download_manager_delegate.h" | 17 #include "content/public/browser/download_manager_delegate.h" |
19 | 18 |
20 class ChromeDownloadManagerDelegate; | 19 class ChromeDownloadManagerDelegate; |
21 class Profile; | 20 class Profile; |
22 class DownloadPrefs; | 21 class DownloadPrefs; |
23 | 22 |
24 namespace content { | 23 namespace content { |
25 enum DownloadDangerType; | 24 enum DownloadDangerType; |
(...skipping 18 matching lines...) Expand all Loading... |
44 // determining the download target. It observes the DownloadItem and aborts the | 43 // determining the download target. It observes the DownloadItem and aborts the |
45 // process if the download is removed. DownloadTargetDeterminerDelegate is | 44 // process if the download is removed. DownloadTargetDeterminerDelegate is |
46 // responsible for providing external dependencies and prompting the user if | 45 // responsible for providing external dependencies and prompting the user if |
47 // necessary. | 46 // necessary. |
48 // | 47 // |
49 // 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 |
50 // instance of DownloadTargetDeterminer. | 49 // instance of DownloadTargetDeterminer. |
51 class DownloadTargetDeterminer | 50 class DownloadTargetDeterminer |
52 : public content::DownloadItem::Observer { | 51 : public content::DownloadItem::Observer { |
53 public: | 52 public: |
54 typedef base::Callback<void(scoped_ptr<DownloadTargetInfo>)> | |
55 CompletionCallback; | |
56 | |
57 // Start the process of determing the target of |download|. | 53 // Start the process of determing the target of |download|. |
58 // | 54 // |
59 // |initial_virtual_path| if non-empty, defines the initial virtual path for | 55 // |initial_virtual_path| if non-empty, defines the initial virtual path for |
60 // the target determination process. If one isn't specified, one will be | 56 // the target determination process. If one isn't specified, one will be |
61 // generated based on the response data specified in |download| and the | 57 // generated based on the response data specified in |download| and the |
62 // users' downloads directory. | 58 // users' downloads directory. |
63 // Note: |initial_virtual_path| is only used if download has prompted the | 59 // Note: |initial_virtual_path| is only used if download has prompted the |
64 // user before and doesn't have a forced path. | 60 // user before and doesn't have a forced path. |
65 // |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 |
66 // determining the user's preferences regarding the default downloads | 62 // determining the user's preferences regarding the default downloads |
67 // directory, prompting and auto-open behavior. | 63 // directory, prompting and auto-open behavior. |
68 // |delegate| is required and must live until |callback| is invoked. | 64 // |delegate| is required and must live until |callback| is invoked. |
69 // |callback| will be scheduled asynchronously on the UI thread after download | 65 // |callback| will be scheduled asynchronously on the UI thread after download |
70 // determination is complete or after |download| is destroyed. | 66 // determination is complete or after |download| is destroyed. |
71 // | 67 // |
72 // Start() should be called on the UI thread. | 68 // Start() should be called on the UI thread. |
73 static void Start(content::DownloadItem* download, | 69 static void Start(content::DownloadItem* download, |
74 const base::FilePath& initial_virtual_path, | 70 const base::FilePath& initial_virtual_path, |
75 DownloadPrefs* download_prefs, | 71 DownloadPrefs* download_prefs, |
76 DownloadTargetDeterminerDelegate* delegate, | 72 DownloadTargetDeterminerDelegate* delegate, |
77 const CompletionCallback& callback); | 73 const content::DownloadTargetCallback& callback); |
78 | 74 |
79 // Returns a .crdownload intermediate path for the |suggested_path|. | 75 // Returns a .crdownload intermediate path for the |suggested_path|. |
80 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); | 76 static base::FilePath GetCrDownloadPath(const base::FilePath& suggested_path); |
81 | 77 |
82 private: | 78 private: |
83 // 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 |
84 // 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 |
85 // performs work, determines the next state to transition to and returns a | 81 // performs work, determines the next state to transition to and returns a |
86 // Result indicating how the workflow should proceed. The loop ends when a | 82 // Result indicating how the workflow should proceed. The loop ends when a |
87 // handler returns COMPLETE. | 83 // handler returns COMPLETE. |
88 enum State { | 84 enum State { |
89 STATE_GENERATE_TARGET_PATH, | 85 STATE_GENERATE_TARGET_PATH, |
90 STATE_NOTIFY_EXTENSIONS, | 86 STATE_NOTIFY_EXTENSIONS, |
91 STATE_RESERVE_VIRTUAL_PATH, | 87 STATE_RESERVE_VIRTUAL_PATH, |
92 STATE_PROMPT_USER_FOR_DOWNLOAD_PATH, | 88 STATE_PROMPT_USER_FOR_DOWNLOAD_PATH, |
93 STATE_DETERMINE_LOCAL_PATH, | 89 STATE_DETERMINE_LOCAL_PATH, |
94 STATE_DETERMINE_MIME_TYPE, | |
95 STATE_DETERMINE_IF_HANDLED_BY_BROWSER, | |
96 STATE_CHECK_DOWNLOAD_URL, | 90 STATE_CHECK_DOWNLOAD_URL, |
97 STATE_CHECK_VISITED_REFERRER_BEFORE, | 91 STATE_CHECK_VISITED_REFERRER_BEFORE, |
98 STATE_DETERMINE_INTERMEDIATE_PATH, | 92 STATE_DETERMINE_INTERMEDIATE_PATH, |
99 STATE_NONE, | 93 STATE_NONE, |
100 }; | 94 }; |
101 | 95 |
102 // Result code returned by each step of the workflow below. Controls execution | 96 // Result code returned by each step of the workflow below. Controls execution |
103 // of DoLoop(). | 97 // of DoLoop(). |
104 enum Result { | 98 enum Result { |
105 // Continue processing. next_state_ is required to not be STATE_NONE. | 99 // Continue processing. next_state_ is required to not be STATE_NONE. |
(...skipping 12 matching lines...) Expand all Loading... |
118 | 112 |
119 // Used with IsDangerousFile to indicate whether the user has visited the | 113 // Used with IsDangerousFile to indicate whether the user has visited the |
120 // referrer URL for the download prior to today. | 114 // referrer URL for the download prior to today. |
121 enum PriorVisitsToReferrer { | 115 enum PriorVisitsToReferrer { |
122 NO_VISITS_TO_REFERRER, | 116 NO_VISITS_TO_REFERRER, |
123 VISITED_REFERRER, | 117 VISITED_REFERRER, |
124 }; | 118 }; |
125 | 119 |
126 // Construct a DownloadTargetDeterminer object. Constraints on the arguments | 120 // Construct a DownloadTargetDeterminer object. Constraints on the arguments |
127 // are as per Start() above. | 121 // are as per Start() above. |
128 DownloadTargetDeterminer(content::DownloadItem* download, | 122 DownloadTargetDeterminer( |
129 const base::FilePath& initial_virtual_path, | 123 content::DownloadItem* download, |
130 DownloadPrefs* download_prefs, | 124 const base::FilePath& initial_virtual_path, |
131 DownloadTargetDeterminerDelegate* delegate, | 125 DownloadPrefs* download_prefs, |
132 const CompletionCallback& callback); | 126 DownloadTargetDeterminerDelegate* delegate, |
| 127 const content::DownloadTargetCallback& callback); |
133 | 128 |
134 virtual ~DownloadTargetDeterminer(); | 129 virtual ~DownloadTargetDeterminer(); |
135 | 130 |
136 // Invoke each successive handler until a handler returns QUIT_DOLOOP or | 131 // Invoke each successive handler until a handler returns QUIT_DOLOOP or |
137 // 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| |
138 // should not be accessed after calling DoLoop(). | 133 // should not be accessed after calling DoLoop(). |
139 void DoLoop(); | 134 void DoLoop(); |
140 | 135 |
141 // === Main workflow === | 136 // === Main workflow === |
142 | 137 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
176 | 171 |
177 // Callback invoked after the file picker completes. Cancels the download if | 172 // Callback invoked after the file picker completes. Cancels the download if |
178 // the user cancels the file picker. | 173 // the user cancels the file picker. |
179 void PromptUserForDownloadPathDone(const base::FilePath& virtual_path); | 174 void PromptUserForDownloadPathDone(const base::FilePath& virtual_path); |
180 | 175 |
181 // Up until this point, the path that was used is considered to be a virtual | 176 // Up until this point, the path that was used is considered to be a virtual |
182 // path. This step determines the local file system path corresponding to this | 177 // path. This step determines the local file system path corresponding to this |
183 // virtual path. The translation is done by invoking the DetermineLocalPath() | 178 // virtual path. The translation is done by invoking the DetermineLocalPath() |
184 // method on the delegate. | 179 // method on the delegate. |
185 // Next state: | 180 // Next state: |
186 // - STATE_DETERMINE_MIME_TYPE. | 181 // - STATE_CHECK_DOWNLOAD_URL. |
187 Result DoDetermineLocalPath(); | 182 Result DoDetermineLocalPath(); |
188 | 183 |
189 // Callback invoked when the delegate has determined local path. | 184 // Callback invoked when the delegate has determined local path. |
190 void DetermineLocalPathDone(const base::FilePath& local_path); | 185 void DetermineLocalPathDone(const base::FilePath& local_path); |
191 | 186 |
192 // Determine the MIME type corresponding to the local file path. This is only | |
193 // done if the local path and the virtual path was the same. I.e. The file is | |
194 // intended for the local file system. This restriction is there because the | |
195 // resulting MIME type is only valid for determining whether the browser can | |
196 // handle the download if it were opened via a file:// URL. | |
197 // Next state: | |
198 // - STATE_DETERMINE_IF_HANDLED_BY_BROWSER. | |
199 Result DoDetermineMimeType(); | |
200 | |
201 // Callback invoked when the MIME type is available. Since determination of | |
202 // the MIME type can involve disk access, it is done in the blocking pool. | |
203 void DetermineMimeTypeDone(const std::string& mime_type); | |
204 | |
205 // Determine if the file type can be handled by the browser if it were to be | |
206 // opened via a file:// URL. | |
207 // Next state: | |
208 // - STATE_CHECK_DOWNLOAD_URL. | |
209 Result DoDetermineIfHandledByBrowser(); | |
210 | |
211 // Callback invoked when a decision is available about whether the file type | |
212 // can be handled by the browser. The actual decision depends on the profile | |
213 // and has to be made on the UI thread. Therefore this method receives a | |
214 // callback that can determine whether the download is handled by the browser | |
215 // based on a passed-in Profile* parameter. | |
216 void DetermineIfHandledByBrowserDone( | |
217 const base::Callback<bool(Profile*)>& per_profile_handler_checker); | |
218 | |
219 // Checks whether the downloaded URL is malicious. Invokes the | 187 // Checks whether the downloaded URL is malicious. Invokes the |
220 // DownloadProtectionService via the delegate. | 188 // DownloadProtectionService via the delegate. |
221 // Next state: | 189 // Next state: |
222 // - STATE_CHECK_VISITED_REFERRER_BEFORE. | 190 // - STATE_CHECK_VISITED_REFERRER_BEFORE. |
223 Result DoCheckDownloadUrl(); | 191 Result DoCheckDownloadUrl(); |
224 | 192 |
225 // Callback invoked after the delegate has checked the download URL. Sets the | 193 // Callback invoked after the delegate has checked the download URL. Sets the |
226 // danger type of the download to |danger_type|. | 194 // danger type of the download to |danger_type|. |
227 void CheckDownloadUrlDone(content::DownloadDangerType danger_type); | 195 void CheckDownloadUrlDone(content::DownloadDangerType danger_type); |
228 | 196 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 // state | 250 // state |
283 State next_state_; | 251 State next_state_; |
284 bool should_prompt_; | 252 bool should_prompt_; |
285 bool should_notify_extensions_; | 253 bool should_notify_extensions_; |
286 bool create_target_directory_; | 254 bool create_target_directory_; |
287 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; | 255 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; |
288 content::DownloadDangerType danger_type_; | 256 content::DownloadDangerType danger_type_; |
289 base::FilePath virtual_path_; | 257 base::FilePath virtual_path_; |
290 base::FilePath local_path_; | 258 base::FilePath local_path_; |
291 base::FilePath intermediate_path_; | 259 base::FilePath intermediate_path_; |
292 std::string mime_type_; | |
293 bool is_filetype_handled_securely_; | |
294 | 260 |
295 content::DownloadItem* download_; | 261 content::DownloadItem* download_; |
296 const bool is_resumption_; | 262 const bool is_resumption_; |
297 DownloadPrefs* download_prefs_; | 263 DownloadPrefs* download_prefs_; |
298 DownloadTargetDeterminerDelegate* delegate_; | 264 DownloadTargetDeterminerDelegate* delegate_; |
299 CompletionCallback completion_callback_; | 265 content::DownloadTargetCallback completion_callback_; |
300 CancelableRequestConsumer history_consumer_; | 266 CancelableRequestConsumer history_consumer_; |
301 | 267 |
302 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; | 268 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; |
303 | 269 |
304 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); | 270 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); |
305 }; | 271 }; |
306 | 272 |
307 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ | 273 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
OLD | NEW |