| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| 6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| 7 |
| 8 #include "base/files/file_path.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "chrome/browser/common/cancelable_request.h" |
| 13 #include "chrome/browser/download/download_path_reservation_tracker.h" |
| 14 #include "chrome/browser/download/download_target_determiner_delegate.h" |
| 15 #include "content/public/browser/download_danger_type.h" |
| 16 #include "content/public/browser/download_item.h" |
| 17 #include "content/public/browser/download_manager_delegate.h" |
| 18 |
| 19 class ChromeDownloadManagerDelegate; |
| 20 class Profile; |
| 21 class DownloadPrefs; |
| 22 |
| 23 namespace content { |
| 24 enum DownloadDangerType; |
| 25 } |
| 26 |
| 27 // Determines the target of the download. |
| 28 // |
| 29 // Terminology: |
| 30 // Virtual Path: A path representing the target of the download that may or |
| 31 // may not be a physical file path. E.g. if the target of the download is in |
| 32 // cloud storage, then the virtual path may be relative to a logical mount |
| 33 // point. |
| 34 // |
| 35 // Local Path: A local file system path where the downloads system should |
| 36 // write the file to. |
| 37 // |
| 38 // Intermediate Path: Where the data should be written to during the course of |
| 39 // the download. Once the download completes, the file could be renamed to |
| 40 // Local Path. |
| 41 // |
| 42 // DownloadTargetDeterminer is a self owned object that performs the work of |
| 43 // determining the download target. It observes the DownloadItem and aborts the |
| 44 // process if the download is removed. DownloadTargetDeterminerDelegate is |
| 45 // responsible for providing external dependencies and prompting the user if |
| 46 // necessary. |
| 47 // |
| 48 // The only public entrypoint is the static Start() method which creates an |
| 49 // instance of DownloadTargetDeterminer. |
| 50 class DownloadTargetDeterminer |
| 51 : public content::DownloadItem::Observer { |
| 52 public: |
| 53 // Start the process of determing the target of |download|. |
| 54 // |
| 55 // |download_prefs| is required and must outlive |download|. It is used for |
| 56 // determining the user's preferences regarding the default downloads |
| 57 // directory, prompting and auto-open behavior. |
| 58 // |last_selected_directory| is the most recent directory that was chosen by |
| 59 // the user. If the user needs to be prompted, then this directory will be |
| 60 // used as the directory for the download instead of the user's default |
| 61 // downloads directory. |
| 62 // |delegate| is required and must live until |callback| is invoked. |
| 63 // |callback| will be scheduled asynchronously on the UI thread after download |
| 64 // determination is complete or after |download| is destroyed. |
| 65 // |
| 66 // Start() should be called on the UI thread. |
| 67 static void Start(content::DownloadItem* download, |
| 68 DownloadPrefs* download_prefs, |
| 69 const base::FilePath& last_selected_directory, |
| 70 DownloadTargetDeterminerDelegate* delegate, |
| 71 const content::DownloadTargetCallback& callback); |
| 72 |
| 73 private: |
| 74 // The main workflow is controlled via a set of state transitions. Each state |
| 75 // has an associated handler. The handler for STATE_FOO is DoFoo. Each handler |
| 76 // performs work, determines the next state to transition to and returns a |
| 77 // Result indicating how the workflow should proceed. The loop ends when a |
| 78 // handler returns COMPLETE. |
| 79 enum State { |
| 80 STATE_GENERATE_TARGET_PATH, |
| 81 STATE_NOTIFY_EXTENSIONS, |
| 82 STATE_RESERVE_VIRTUAL_PATH, |
| 83 STATE_PROMPT_USER_FOR_DOWNLOAD_PATH, |
| 84 STATE_DETERMINE_LOCAL_PATH, |
| 85 STATE_CHECK_DOWNLOAD_URL, |
| 86 STATE_CHECK_VISITED_REFERRER_BEFORE, |
| 87 STATE_DETERMINE_INTERMEDIATE_PATH, |
| 88 STATE_NONE, |
| 89 }; |
| 90 |
| 91 // Result code returned by each step of the workflow below. Controls execution |
| 92 // of DoLoop(). |
| 93 enum Result { |
| 94 // Continue processing. next_state_ is required to not be STATE_NONE. |
| 95 CONTINUE, |
| 96 |
| 97 // The DoLoop() that invoked the handler should exit. This value is |
| 98 // typically returned when the handler has invoked an asynchronous operation |
| 99 // and is expecting a callback. If a handler returns this value, it has |
| 100 // taken responsibility for ensuring that DoLoop() is invoked. It is |
| 101 // possible that the handler has invoked another DoLoop() already. |
| 102 QUIT_DOLOOP, |
| 103 |
| 104 // Target determination is complete. |
| 105 COMPLETE |
| 106 }; |
| 107 |
| 108 // Used with IsDangerousFile to indicate whether the user has visited the |
| 109 // referrer URL for the download prior to today. |
| 110 enum PriorVisitsToReferrer { |
| 111 NO_VISITS_TO_REFERRER, |
| 112 VISITED_REFERRER, |
| 113 }; |
| 114 |
| 115 // Construct a DownloadTargetDeterminer object. Constraints on the arguments |
| 116 // are as per Start() above. |
| 117 DownloadTargetDeterminer( |
| 118 content::DownloadItem* download, |
| 119 DownloadPrefs* download_prefs, |
| 120 const base::FilePath& last_selected_directory, |
| 121 DownloadTargetDeterminerDelegate* delegate, |
| 122 const content::DownloadTargetCallback& callback); |
| 123 |
| 124 virtual ~DownloadTargetDeterminer(); |
| 125 |
| 126 // Invoke each successive handler until a handler returns QUIT_DOLOOP or |
| 127 // COMPLETE. Note that as a result, this object might be deleted. So |this| |
| 128 // should not be accessed after calling DoLoop(). |
| 129 void DoLoop(); |
| 130 |
| 131 // === Main workflow === |
| 132 |
| 133 // Generates an initial target path. This target is based only on the state of |
| 134 // the download item. |
| 135 // Next state: |
| 136 // - STATE_NONE : If the download is not in progress, returns COMPLETE. |
| 137 // - STATE_NOTIFY_EXTENSIONS : All other downloads. |
| 138 Result DoGenerateTargetPath(); |
| 139 |
| 140 // Notifies downloads extensions. If any extension wishes to override the |
| 141 // download filename, it will respond to the OnDeterminingFilename() |
| 142 // notification. |
| 143 // Next state: |
| 144 // - STATE_RESERVE_VIRTUAL_PATH. |
| 145 Result DoNotifyExtensions(); |
| 146 |
| 147 // Callback invoked after extensions are notified. Updates |virtual_path_| and |
| 148 // |conflict_action_|. |
| 149 void NotifyExtensionsDone( |
| 150 const base::FilePath& new_path, |
| 151 DownloadPathReservationTracker::FilenameConflictAction conflict_action); |
| 152 |
| 153 // Invokes ReserveVirtualPath() on the delegate to acquire a reservation for |
| 154 // the path. See DownloadPathReservationTracker. |
| 155 // Next state: |
| 156 // - STATE_PROMPT_USER_FOR_DOWNLOAD_PATH. |
| 157 Result DoReserveVirtualPath(); |
| 158 |
| 159 // Callback invoked after the delegate aquires a path reservation. |
| 160 void ReserveVirtualPathDone(const base::FilePath& path, bool verified); |
| 161 |
| 162 // Presents a file picker to the user if necessary. |
| 163 // Next state: |
| 164 // - STATE_DETERMINE_LOCAL_PATH. |
| 165 Result DoPromptUserForDownloadPath(); |
| 166 |
| 167 // Callback invoked after the file picker completes. Cancels the download if |
| 168 // the user cancels the file picker. |
| 169 void PromptUserForDownloadPathDone(const base::FilePath& virtual_path); |
| 170 |
| 171 // Up until this point, the path that was used is considered to be a virtual |
| 172 // path. This step determines the local file system path corresponding to this |
| 173 // virtual path. The translation is done by invoking the DetermineLocalPath() |
| 174 // method on the delegate. |
| 175 // Next state: |
| 176 // - STATE_CHECK_DOWNLOAD_URL. |
| 177 Result DoDetermineLocalPath(); |
| 178 |
| 179 // Callback invoked when the delegate has determined local path. |
| 180 void DetermineLocalPathDone(const base::FilePath& local_path); |
| 181 |
| 182 // Checks whether the downloaded URL is malicious. Invokes the |
| 183 // DownloadProtectionService via the delegate. |
| 184 // Next state: |
| 185 // - STATE_CHECK_VISITED_REFERRER_BEFORE. |
| 186 Result DoCheckDownloadUrl(); |
| 187 |
| 188 // Callback invoked after the delegate has checked the download URL. Sets the |
| 189 // danger type of the download to |danger_type|. |
| 190 void CheckDownloadUrlDone(content::DownloadDangerType danger_type); |
| 191 |
| 192 // Checks if the user has visited the referrer URL of the download prior to |
| 193 // today. The actual check is only performed if it would be needed to |
| 194 // determine the danger type of the download. |
| 195 // Next state: |
| 196 // - STATE_DETERMINE_INTERMEDIATE_PATH. |
| 197 Result DoCheckVisitedReferrerBefore(); |
| 198 |
| 199 // Callback invoked after completion of history check for prior visits to |
| 200 // referrer URL. |
| 201 void CheckVisitedReferrerBeforeDone(bool visited_referrer_before); |
| 202 |
| 203 // Determines the intermediate path. Once this step completes, downloads |
| 204 // target determination is complete. The determination assumes that the |
| 205 // intermediate file will never be overwritten (always uniquified if needed). |
| 206 // Next state: |
| 207 // - STATE_NONE: Returns COMPLETE. |
| 208 Result DoDetermineIntermediatePath(); |
| 209 |
| 210 // === End of main workflow === |
| 211 |
| 212 // Utilities: |
| 213 |
| 214 void ScheduleCallbackAndDeleteSelf(); |
| 215 |
| 216 void CancelOnFailureAndDeleteSelf(); |
| 217 |
| 218 Profile* GetProfile(); |
| 219 |
| 220 bool ShouldPromptForDownload(const base::FilePath& filename); |
| 221 |
| 222 // Returns true if this download should show the "dangerous file" warning. |
| 223 // Various factors are considered, such as the type of the file, whether a |
| 224 // user action initiated the download, and whether the user has explicitly |
| 225 // marked the file type as "auto open". Protected virtual for testing. |
| 226 bool IsDangerousFile(PriorVisitsToReferrer visits); |
| 227 |
| 228 // content::DownloadItem::Observer |
| 229 virtual void OnDownloadDestroyed(content::DownloadItem* download) OVERRIDE; |
| 230 |
| 231 // state |
| 232 State next_state_; |
| 233 bool should_prompt_; |
| 234 DownloadPathReservationTracker::FilenameConflictAction conflict_action_; |
| 235 content::DownloadDangerType danger_type_; |
| 236 base::FilePath virtual_path_; |
| 237 base::FilePath local_path_; |
| 238 base::FilePath intermediate_path_; |
| 239 |
| 240 content::DownloadItem* download_; |
| 241 DownloadPrefs* download_prefs_; |
| 242 DownloadTargetDeterminerDelegate* delegate_; |
| 243 base::FilePath last_selected_directory_; |
| 244 content::DownloadTargetCallback completion_callback_; |
| 245 CancelableRequestConsumer history_consumer_; |
| 246 |
| 247 base::WeakPtrFactory<DownloadTargetDeterminer> weak_ptr_factory_; |
| 248 |
| 249 DISALLOW_COPY_AND_ASSIGN(DownloadTargetDeterminer); |
| 250 }; |
| 251 |
| 252 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_TARGET_DETERMINER_H_ |
| OLD | NEW |