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