OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 6 #define CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
7 | 7 |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/hash_tables.h" | 9 #include "base/hash_tables.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 }; | 38 }; |
39 } // namespace BASE_HASH_NAMESPACE | 39 } // namespace BASE_HASH_NAMESPACE |
40 #endif | 40 #endif |
41 | 41 |
42 // This is the Chrome side helper for the download system. | 42 // This is the Chrome side helper for the download system. |
43 class ChromeDownloadManagerDelegate | 43 class ChromeDownloadManagerDelegate |
44 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>, | 44 : public base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>, |
45 public content::DownloadManagerDelegate, | 45 public content::DownloadManagerDelegate, |
46 public content::NotificationObserver { | 46 public content::NotificationObserver { |
47 public: | 47 public: |
| 48 // Callback type used with ChooseDownloadPath(). The callback should be |
| 49 // invoked with the user-selected path as the argument. If the file selection |
| 50 // was canceled, the argument should be the empty path. |
| 51 typedef base::Callback<void(const FilePath&)> FileSelectedCallback; |
| 52 |
48 explicit ChromeDownloadManagerDelegate(Profile* profile); | 53 explicit ChromeDownloadManagerDelegate(Profile* profile); |
49 | 54 |
50 void SetDownloadManager(content::DownloadManager* dm); | 55 void SetDownloadManager(content::DownloadManager* dm); |
51 | 56 |
52 // Should be called before the first call to ShouldCompleteDownload() to | 57 // Should be called before the first call to ShouldCompleteDownload() to |
53 // disable SafeBrowsing checks for |item|. | 58 // disable SafeBrowsing checks for |item|. |
54 static void DisableSafeBrowsing(content::DownloadItem* item); | 59 static void DisableSafeBrowsing(content::DownloadItem* item); |
55 | 60 |
| 61 // content::DownloadManagerDelegate |
56 virtual void Shutdown() OVERRIDE; | 62 virtual void Shutdown() OVERRIDE; |
57 virtual content::DownloadId GetNextId() OVERRIDE; | 63 virtual content::DownloadId GetNextId() OVERRIDE; |
58 virtual bool ShouldStartDownload(int32 download_id) OVERRIDE; | 64 virtual bool DetermineDownloadTarget( |
59 virtual void ChooseDownloadPath(content::DownloadItem* item) OVERRIDE; | 65 content::DownloadItem* item, |
60 virtual FilePath GetIntermediatePath( | 66 const content::DownloadTargetCallback& callback) OVERRIDE; |
61 const content::DownloadItem& item) OVERRIDE; | |
62 virtual content::WebContents* | 67 virtual content::WebContents* |
63 GetAlternativeWebContentsToNotifyForDownload() OVERRIDE; | 68 GetAlternativeWebContentsToNotifyForDownload() OVERRIDE; |
64 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; | 69 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; |
65 virtual bool ShouldCompleteDownload( | 70 virtual bool ShouldCompleteDownload( |
66 content::DownloadItem* item, | 71 content::DownloadItem* item, |
67 const base::Closure& complete_callback) OVERRIDE; | 72 const base::Closure& complete_callback) OVERRIDE; |
68 virtual bool ShouldOpenDownload(content::DownloadItem* item) OVERRIDE; | 73 virtual bool ShouldOpenDownload(content::DownloadItem* item) OVERRIDE; |
69 virtual bool GenerateFileHash() OVERRIDE; | 74 virtual bool GenerateFileHash() OVERRIDE; |
70 virtual void AddItemToPersistentStore(content::DownloadItem* item) OVERRIDE; | 75 virtual void AddItemToPersistentStore(content::DownloadItem* item) OVERRIDE; |
71 virtual void UpdateItemInPersistentStore( | 76 virtual void UpdateItemInPersistentStore( |
(...skipping 10 matching lines...) Expand all Loading... |
82 FilePath* website_save_dir, | 87 FilePath* website_save_dir, |
83 FilePath* download_save_dir, | 88 FilePath* download_save_dir, |
84 bool* skip_dir_check) OVERRIDE; | 89 bool* skip_dir_check) OVERRIDE; |
85 virtual void ChooseSavePath( | 90 virtual void ChooseSavePath( |
86 content::WebContents* web_contents, | 91 content::WebContents* web_contents, |
87 const FilePath& suggested_path, | 92 const FilePath& suggested_path, |
88 const FilePath::StringType& default_extension, | 93 const FilePath::StringType& default_extension, |
89 bool can_save_as_complete, | 94 bool can_save_as_complete, |
90 const content::SavePackagePathPickedCallback& callback) OVERRIDE; | 95 const content::SavePackagePathPickedCallback& callback) OVERRIDE; |
91 | 96 |
| 97 // Clears the last directory chosen by the user in response to a file chooser |
| 98 // prompt. Called when clearing recent history. |
| 99 void ClearLastDownloadPath(); |
| 100 |
92 DownloadPrefs* download_prefs() { return download_prefs_.get(); } | 101 DownloadPrefs* download_prefs() { return download_prefs_.get(); } |
93 DownloadHistory* download_history() { return download_history_.get(); } | 102 DownloadHistory* download_history() { return download_history_.get(); } |
94 | 103 |
95 protected: | 104 protected: |
96 // So that test classes can inherit from this for override purposes. | 105 // So that test classes can inherit from this for override purposes. |
97 virtual ~ChromeDownloadManagerDelegate(); | 106 virtual ~ChromeDownloadManagerDelegate(); |
98 | 107 |
99 // Returns the SafeBrowsing download protection service if it's | 108 // Returns the SafeBrowsing download protection service if it's |
100 // enabled. Returns NULL otherwise. Protected virtual for testing. | 109 // enabled. Returns NULL otherwise. Protected virtual for testing. |
101 virtual safe_browsing::DownloadProtectionService* | 110 virtual safe_browsing::DownloadProtectionService* |
102 GetDownloadProtectionService(); | 111 GetDownloadProtectionService(); |
103 | 112 |
104 // Returns true if this download should show the "dangerous file" warning. | 113 // Returns true if this download should show the "dangerous file" warning. |
105 // Various factors are considered, such as the type of the file, whether a | 114 // Various factors are considered, such as the type of the file, whether a |
106 // user action initiated the download, and whether the user has explicitly | 115 // user action initiated the download, and whether the user has explicitly |
107 // marked the file type as "auto open". Protected virtual for testing. | 116 // marked the file type as "auto open". Protected virtual for testing. |
108 virtual bool IsDangerousFile(const content::DownloadItem& download, | 117 virtual bool IsDangerousFile(const content::DownloadItem& download, |
109 const FilePath& suggested_path, | 118 const FilePath& suggested_path, |
110 bool visited_referrer_before); | 119 bool visited_referrer_before); |
111 | 120 |
112 // Obtains a path reservation by calling | 121 // Obtains a path reservation by calling |
113 // DownloadPathReservationTracker::GetReservedPath(). Protected virtual for | 122 // DownloadPathReservationTracker::GetReservedPath(). Protected virtual for |
114 // testing. | 123 // testing. |
115 virtual void GetReservedPath( | 124 virtual void GetReservedPath( |
116 content::DownloadItem& download, | 125 content::DownloadItem& download, |
117 const FilePath& target_path, | 126 const FilePath& target_path, |
118 const FilePath& default_download_path, | 127 const FilePath& default_download_path, |
119 bool should_uniquify_path, | 128 bool should_uniquify_path, |
120 const DownloadPathReservationTracker::ReservedPathCallback callback); | 129 const DownloadPathReservationTracker::ReservedPathCallback& callback); |
| 130 |
| 131 // Displays the file chooser dialog to prompt the user for the download |
| 132 // location for |item|. |suggested_path| will be used as the initial download |
| 133 // path. Once a location is available |callback| will be invoked with the |
| 134 // selected full path. If the user cancels the dialog, then an empty FilePath |
| 135 // will be passed into |callback|. Protected virtual for testing. |
| 136 virtual void ChooseDownloadPath(content::DownloadItem* item, |
| 137 const FilePath& suggested_path, |
| 138 const FileSelectedCallback& callback); |
121 | 139 |
122 // So that test classes that inherit from this for override purposes | 140 // So that test classes that inherit from this for override purposes |
123 // can call back into the DownloadManager. | 141 // can call back into the DownloadManager. |
124 scoped_refptr<content::DownloadManager> download_manager_; | 142 scoped_refptr<content::DownloadManager> download_manager_; |
125 | 143 |
126 private: | 144 private: |
127 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>; | 145 friend class base::RefCountedThreadSafe<ChromeDownloadManagerDelegate>; |
128 | 146 |
129 // content::NotificationObserver implementation. | 147 // content::NotificationObserver implementation. |
130 virtual void Observe(int type, | 148 virtual void Observe(int type, |
131 const content::NotificationSource& source, | 149 const content::NotificationSource& source, |
132 const content::NotificationDetails& details) OVERRIDE; | 150 const content::NotificationDetails& details) OVERRIDE; |
133 | 151 |
134 // Callback function after url is checked with safebrowsing service. | 152 // Callback function after url is checked with safebrowsing service. |
135 void CheckDownloadUrlDone( | 153 void CheckDownloadUrlDone( |
136 int32 download_id, | 154 int32 download_id, |
| 155 const content::DownloadTargetCallback& callback, |
137 safe_browsing::DownloadProtectionService::DownloadCheckResult result); | 156 safe_browsing::DownloadProtectionService::DownloadCheckResult result); |
138 | 157 |
139 // Callback function after the DownloadProtectionService completes. | 158 // Callback function after the DownloadProtectionService completes. |
140 void CheckClientDownloadDone( | 159 void CheckClientDownloadDone( |
141 int32 download_id, | 160 int32 download_id, |
142 safe_browsing::DownloadProtectionService::DownloadCheckResult result); | 161 safe_browsing::DownloadProtectionService::DownloadCheckResult result); |
143 | 162 |
144 // Callback function after we check whether the referrer URL has been visited | 163 // Callback function after we check whether the referrer URL has been visited |
145 // before today. Determines the danger state of the download based on the file | 164 // before today. Determines the danger state of the download based on the file |
146 // type and |visited_referrer_before|. Generates a target path for the | 165 // type and |visited_referrer_before|. Generates a target path for the |
147 // download. Invokes |DownloadPathReservationTracker::GetReservedPath| to get | 166 // download. Invokes |DownloadPathReservationTracker::GetReservedPath| to get |
148 // a reserved path for the download. The path is then passed into | 167 // a reserved path for the download. The path is then passed into |
149 // OnPathReservationAvailable(). | 168 // OnPathReservationAvailable(). |
150 void CheckVisitedReferrerBeforeDone(int32 download_id, | 169 void CheckVisitedReferrerBeforeDone( |
151 content::DownloadDangerType danger_type, | 170 int32 download_id, |
152 bool visited_referrer_before); | 171 const content::DownloadTargetCallback& callback, |
| 172 content::DownloadDangerType danger_type, |
| 173 bool visited_referrer_before); |
153 | 174 |
154 #if defined (OS_CHROMEOS) | 175 #if defined (OS_CHROMEOS) |
155 // GDataDownloadObserver::SubstituteGDataDownloadPath callback. Calls | 176 // GDataDownloadObserver::SubstituteGDataDownloadPath callback. Calls |
156 // |DownloadPathReservationTracker::GetReservedPath| to get a reserved path | 177 // |DownloadPathReservationTracker::GetReservedPath| to get a reserved path |
157 // for the download. The path is then passed into | 178 // for the download. The path is then passed into |
158 // OnPathReservationAvailable(). | 179 // OnPathReservationAvailable(). |
159 void SubstituteGDataDownloadPathCallback( | 180 void SubstituteGDataDownloadPathCallback( |
160 int32 download_id, | 181 int32 download_id, |
| 182 const content::DownloadTargetCallback& callback, |
161 bool should_prompt, | 183 bool should_prompt, |
162 bool is_forced_path, | 184 bool is_forced_path, |
163 content::DownloadDangerType danger_type, | 185 content::DownloadDangerType danger_type, |
164 const FilePath& unverified_path); | 186 const FilePath& unverified_path); |
165 #endif | 187 #endif |
166 | 188 |
| 189 // Determine the intermediate path to use for |target_path|. |danger_type| |
| 190 // specifies the danger level of the download. |
| 191 FilePath GetIntermediatePath(const FilePath& target_path, |
| 192 content::DownloadDangerType danger_type); |
| 193 |
167 // Called on the UI thread once a reserved path is available. Updates the | 194 // Called on the UI thread once a reserved path is available. Updates the |
168 // download identified by |download_id| with the |target_path|, target | 195 // download identified by |download_id| with the |target_path|, target |
169 // disposition and |danger_type|. | 196 // disposition and |danger_type|. |
170 void OnPathReservationAvailable( | 197 void OnPathReservationAvailable( |
171 int32 download_id, | 198 int32 download_id, |
| 199 const content::DownloadTargetCallback& callback, |
172 bool should_prompt, | 200 bool should_prompt, |
173 content::DownloadDangerType danger_type, | 201 content::DownloadDangerType danger_type, |
174 const FilePath& target_path, | 202 const FilePath& reserved_path, |
175 bool target_path_verified); | 203 bool reserved_path_verified); |
| 204 |
| 205 // Called on the UI thread once the final target path is available. |
| 206 void OnTargetPathDetermined( |
| 207 int32 download_id, |
| 208 const content::DownloadTargetCallback& callback, |
| 209 content::DownloadItem::TargetDisposition disposition, |
| 210 content::DownloadDangerType danger_type, |
| 211 const FilePath& target_path); |
176 | 212 |
177 // Callback from history system. | 213 // Callback from history system. |
178 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); | 214 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); |
179 | 215 |
180 // Check policy of whether we should open this download with a web intents | 216 // Check policy of whether we should open this download with a web intents |
181 // dispatch. | 217 // dispatch. |
182 bool ShouldOpenWithWebIntents(const content::DownloadItem* item); | 218 bool ShouldOpenWithWebIntents(const content::DownloadItem* item); |
183 | 219 |
184 // Open the given item with a web intent dispatch. | 220 // Open the given item with a web intent dispatch. |
185 void OpenWithWebIntent(const content::DownloadItem* item); | 221 void OpenWithWebIntent(const content::DownloadItem* item); |
(...skipping 24 matching lines...) Expand all Loading... |
210 #if !defined(OS_ANDROID) | 246 #if !defined(OS_ANDROID) |
211 // The ExtensionDownloadsEventRouter dispatches download creation, change, and | 247 // The ExtensionDownloadsEventRouter dispatches download creation, change, and |
212 // erase events to extensions. Like ChromeDownloadManagerDelegate, it's a | 248 // erase events to extensions. Like ChromeDownloadManagerDelegate, it's a |
213 // chrome-level concept and its lifetime should match DownloadManager. There | 249 // chrome-level concept and its lifetime should match DownloadManager. There |
214 // should be a separate EDER for on-record and off-record managers. | 250 // should be a separate EDER for on-record and off-record managers. |
215 // There does not appear to be a separate ExtensionSystem for on-record and | 251 // There does not appear to be a separate ExtensionSystem for on-record and |
216 // off-record profiles, so ExtensionSystem cannot own the EDER. | 252 // off-record profiles, so ExtensionSystem cannot own the EDER. |
217 scoped_ptr<ExtensionDownloadsEventRouter> extension_event_router_; | 253 scoped_ptr<ExtensionDownloadsEventRouter> extension_event_router_; |
218 #endif | 254 #endif |
219 | 255 |
| 256 // The directory most recently chosen by the user in response to a Save As |
| 257 // dialog for a regular download. |
| 258 FilePath last_download_path_; |
| 259 |
220 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); | 260 DISALLOW_COPY_AND_ASSIGN(ChromeDownloadManagerDelegate); |
221 }; | 261 }; |
222 | 262 |
223 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ | 263 #endif // CHROME_BROWSER_DOWNLOAD_CHROME_DOWNLOAD_MANAGER_DELEGATE_H_ |
OLD | NEW |