OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // The DownloadManager object manages the process of downloading, including | 5 // The DownloadManager object manages the process of downloading, including |
6 // updates to the history system and providing the information for displaying | 6 // updates to the history system and providing the information for displaying |
7 // the downloads view in the Destinations tab. There is one DownloadManager per | 7 // the downloads view in the Destinations tab. There is one DownloadManager per |
8 // active profile in Chrome. | 8 // active profile in Chrome. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
(...skipping 23 matching lines...) Expand all Loading... |
34 #include <vector> | 34 #include <vector> |
35 | 35 |
36 #include "base/basictypes.h" | 36 #include "base/basictypes.h" |
37 #include "base/file_path.h" | 37 #include "base/file_path.h" |
38 #include "base/gtest_prod_util.h" | 38 #include "base/gtest_prod_util.h" |
39 #include "base/memory/ref_counted.h" | 39 #include "base/memory/ref_counted.h" |
40 #include "base/memory/weak_ptr.h" | 40 #include "base/memory/weak_ptr.h" |
41 #include "base/observer_list.h" | 41 #include "base/observer_list.h" |
42 #include "base/scoped_ptr.h" | 42 #include "base/scoped_ptr.h" |
43 #include "base/time.h" | 43 #include "base/time.h" |
| 44 #include "chrome/browser/download/download_item.h" |
44 #include "chrome/browser/download/download_status_updater_delegate.h" | 45 #include "chrome/browser/download/download_status_updater_delegate.h" |
45 #include "chrome/browser/download/download_process_handle.h" | 46 #include "chrome/browser/download/download_process_handle.h" |
46 #include "chrome/browser/ui/shell_dialogs.h" | 47 #include "chrome/browser/ui/shell_dialogs.h" |
47 #include "content/browser/browser_thread.h" | 48 #include "content/browser/browser_thread.h" |
48 | 49 |
49 class DownloadFileManager; | 50 class DownloadFileManager; |
50 class DownloadHistory; | 51 class DownloadHistory; |
51 class DownloadItem; | |
52 class DownloadPrefs; | 52 class DownloadPrefs; |
53 class DownloadStatusUpdater; | 53 class DownloadStatusUpdater; |
54 class GURL; | 54 class GURL; |
55 class Profile; | 55 class Profile; |
56 class ResourceDispatcherHost; | 56 class ResourceDispatcherHost; |
57 class TabContents; | 57 class TabContents; |
58 struct DownloadCreateInfo; | 58 struct DownloadCreateInfo; |
| 59 struct DownloadHistoryInfo; |
59 struct DownloadSaveInfo; | 60 struct DownloadSaveInfo; |
60 | 61 |
61 // Browser's download manager: manages all downloads and destination view. | 62 // Browser's download manager: manages all downloads and destination view. |
62 class DownloadManager | 63 class DownloadManager |
63 : public base::RefCountedThreadSafe<DownloadManager, | 64 : public base::RefCountedThreadSafe<DownloadManager, |
64 BrowserThread::DeleteOnUIThread>, | 65 BrowserThread::DeleteOnUIThread>, |
65 public DownloadStatusUpdaterDelegate, | 66 public DownloadStatusUpdaterDelegate, |
66 public SelectFileDialog::Listener { | 67 public SelectFileDialog::Listener { |
67 public: | 68 public: |
68 explicit DownloadManager(DownloadStatusUpdater* status_updater); | 69 explicit DownloadManager(DownloadStatusUpdater* status_updater); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 108 |
108 // Returns all non-temporary downloads matching |query|. Empty query matches | 109 // Returns all non-temporary downloads matching |query|. Empty query matches |
109 // everything. | 110 // everything. |
110 void SearchDownloads(const string16& query, | 111 void SearchDownloads(const string16& query, |
111 std::vector<DownloadItem*>* result); | 112 std::vector<DownloadItem*>* result); |
112 | 113 |
113 // Returns true if initialized properly. | 114 // Returns true if initialized properly. |
114 bool Init(Profile* profile); | 115 bool Init(Profile* profile); |
115 | 116 |
116 // Notifications sent from the download thread to the UI thread | 117 // Notifications sent from the download thread to the UI thread |
117 void StartDownload(DownloadCreateInfo* info); | 118 void StartDownload(int32 id); |
118 void UpdateDownload(int32 download_id, int64 size); | 119 void UpdateDownload(int32 download_id, int64 size); |
119 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 120 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
120 // is not available. | 121 // is not available. |
121 void OnResponseCompleted(int32 download_id, int64 size, int os_error, | 122 void OnResponseCompleted(int32 download_id, int64 size, int os_error, |
122 const std::string& hash); | 123 const std::string& hash); |
123 | 124 |
124 // Called from a view when a user clicks a UI button or link. | 125 // Called from a view when a user clicks a UI button or link. |
125 void DownloadCancelled(int32 download_id); | 126 void DownloadCancelled(int32 download_id); |
126 void PauseDownload(int32 download_id, bool pause); | 127 void PauseDownload(int32 download_id, bool pause); |
127 void RemoveDownload(int64 download_handle); | 128 void RemoveDownload(int64 download_handle); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 TabContents* tab_contents); | 184 TabContents* tab_contents); |
184 | 185 |
185 // Allow objects to observe the download creation process. | 186 // Allow objects to observe the download creation process. |
186 void AddObserver(Observer* observer); | 187 void AddObserver(Observer* observer); |
187 | 188 |
188 // Remove a download observer from ourself. | 189 // Remove a download observer from ourself. |
189 void RemoveObserver(Observer* observer); | 190 void RemoveObserver(Observer* observer); |
190 | 191 |
191 // Methods called on completion of a query sent to the history system. | 192 // Methods called on completion of a query sent to the history system. |
192 void OnQueryDownloadEntriesComplete( | 193 void OnQueryDownloadEntriesComplete( |
193 std::vector<DownloadCreateInfo>* entries); | 194 std::vector<DownloadHistoryInfo>* entries); |
194 void OnCreateDownloadEntryComplete( | 195 void OnCreateDownloadEntryComplete(int32 download_id, int64 db_handle); |
195 DownloadCreateInfo info, int64 db_handle); | |
196 | 196 |
197 // Display a new download in the appropriate browser UI. | 197 // Display a new download in the appropriate browser UI. |
198 void ShowDownloadInBrowser(DownloadProcessHandle* process_handle, | 198 void ShowDownloadInBrowser(DownloadItem* download); |
199 DownloadItem* download); | |
200 | 199 |
201 // The number of in progress (including paused) downloads. | 200 // The number of in progress (including paused) downloads. |
202 int in_progress_count() const { | 201 int in_progress_count() const { |
203 return static_cast<int>(in_progress_.size()); | 202 return static_cast<int>(in_progress_.size()); |
204 } | 203 } |
205 | 204 |
206 Profile* profile() { return profile_; } | 205 Profile* profile() { return profile_; } |
207 | 206 |
208 DownloadHistory* download_history() { return download_history_.get(); } | 207 DownloadHistory* download_history() { return download_history_.get(); } |
209 | 208 |
(...skipping 11 matching lines...) Expand all Loading... |
221 // Overridden from DownloadStatusUpdaterDelegate: | 220 // Overridden from DownloadStatusUpdaterDelegate: |
222 virtual bool IsDownloadProgressKnown(); | 221 virtual bool IsDownloadProgressKnown(); |
223 virtual int64 GetInProgressDownloadCount(); | 222 virtual int64 GetInProgressDownloadCount(); |
224 virtual int64 GetReceivedDownloadBytes(); | 223 virtual int64 GetReceivedDownloadBytes(); |
225 virtual int64 GetTotalDownloadBytes(); | 224 virtual int64 GetTotalDownloadBytes(); |
226 | 225 |
227 // Overridden from SelectFileDialog::Listener: | 226 // Overridden from SelectFileDialog::Listener: |
228 virtual void FileSelected(const FilePath& path, int index, void* params); | 227 virtual void FileSelected(const FilePath& path, int index, void* params); |
229 virtual void FileSelectionCanceled(void* params); | 228 virtual void FileSelectionCanceled(void* params); |
230 | 229 |
| 230 // Returns true if this download should show the "dangerous file" warning. |
| 231 // Various factors are considered, such as the type of the file, whether a |
| 232 // user action initiated the download, and whether the user has explicitly |
| 233 // marked the file type as "auto open". |
| 234 bool IsDangerous(const DownloadItem& download, |
| 235 const DownloadStateInfo& state); |
| 236 |
231 // Called when the user has validated the download of a dangerous file. | 237 // Called when the user has validated the download of a dangerous file. |
232 void DangerousDownloadValidated(DownloadItem* download); | 238 void DangerousDownloadValidated(DownloadItem* download); |
233 | 239 |
234 // Callback function after url is checked with safebrowsing service. | 240 // Callback function after url is checked with safebrowsing service. |
235 void CheckDownloadUrlDone(DownloadCreateInfo* info, bool is_dangerous_url); | 241 void CheckDownloadUrlDone(int32 download_id, bool is_dangerous_url); |
236 | 242 |
237 // Callback function after download file hash is checked with safebrowsing | 243 // Callback function after download file hash is checked with safebrowsing |
238 // service. | 244 // service. |
239 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); | 245 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); |
240 | 246 |
241 private: | 247 private: |
242 // For testing. | 248 // For testing. |
243 friend class DownloadManagerTest; | 249 friend class DownloadManagerTest; |
244 friend class MockDownloadManager; | 250 friend class MockDownloadManager; |
245 | 251 |
(...skipping 20 matching lines...) Expand all Loading... |
266 | 272 |
267 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 273 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
268 friend class DeleteTask<DownloadManager>; | 274 friend class DeleteTask<DownloadManager>; |
269 friend class OtherDownloadManagerObserver; | 275 friend class OtherDownloadManagerObserver; |
270 | 276 |
271 ~DownloadManager(); | 277 ~DownloadManager(); |
272 | 278 |
273 // Called on the download thread to check whether the suggested file path | 279 // Called on the download thread to check whether the suggested file path |
274 // exists. We don't check if the file exists on the UI thread to avoid UI | 280 // exists. We don't check if the file exists on the UI thread to avoid UI |
275 // stalls from interacting with the file system. | 281 // stalls from interacting with the file system. |
276 void CheckIfSuggestedPathExists(DownloadCreateInfo* info, | 282 void CheckIfSuggestedPathExists(int32 download_id, |
| 283 DownloadStateInfo state, |
277 const FilePath& default_path); | 284 const FilePath& default_path); |
278 | 285 |
279 // Called on the UI thread once the DownloadManager has determined whether the | 286 // Called on the UI thread once the DownloadManager has determined whether the |
280 // suggested file path exists. | 287 // suggested file path exists. |
281 void OnPathExistenceAvailable(DownloadCreateInfo* info); | 288 void OnPathExistenceAvailable(int32 download_id, |
| 289 DownloadStateInfo new_state); |
282 | 290 |
283 // Called back after a target path for the file to be downloaded to has been | 291 // Called back after a target path for the file to be downloaded to has been |
284 // determined, either automatically based on the suggested file name, or by | 292 // determined, either automatically based on the suggested file name, or by |
285 // the user in a Save As dialog box. | 293 // the user in a Save As dialog box. |
286 void AttachDownloadItem(DownloadCreateInfo* info); | 294 void ContinueDownloadWithPath(DownloadItem* download, |
| 295 const FilePath& chosen_file); |
287 | 296 |
288 // Download cancel helper function. | 297 // Download cancel helper function. |
289 // |process_handle| is passed by value because it is ultimately passed to | 298 // |process_handle| is passed by value because it is ultimately passed to |
290 // other threads, and this way we don't have to worry about object lifetimes. | 299 // other threads, and this way we don't have to worry about object lifetimes. |
291 void DownloadCancelledInternal(int download_id, | 300 void DownloadCancelledInternal(int download_id, |
292 DownloadProcessHandle process_handle); | 301 DownloadProcessHandle process_handle); |
293 | 302 |
294 // All data has been downloaded. | 303 // All data has been downloaded. |
295 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 304 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
296 // is not available. | 305 // is not available. |
297 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash); | 306 void OnAllDataSaved(int32 download_id, int64 size, const std::string& hash); |
298 | 307 |
299 // An error occurred in the download. | 308 // An error occurred in the download. |
300 void OnDownloadError(int32 download_id, int64 size, int os_error); | 309 void OnDownloadError(int32 download_id, int64 size, int os_error); |
301 | 310 |
302 // Updates the app icon about the overall download progress. | 311 // Updates the app icon about the overall download progress. |
303 void UpdateAppIcon(); | 312 void UpdateAppIcon(); |
304 | 313 |
305 // Makes the ResourceDispatcherHost pause/un-pause a download request. | 314 // Makes the ResourceDispatcherHost pause/un-pause a download request. |
306 // Called on the IO thread. | 315 // Called on the IO thread. |
307 // |process_handle| is passed by value because this is called from other | 316 // |process_handle| is passed by value because this is called from other |
308 // threads, and this way we don't have to worry about object lifetimes. | 317 // threads, and this way we don't have to worry about object lifetimes. |
309 void PauseDownloadRequest(ResourceDispatcherHost* rdh, | 318 void PauseDownloadRequest(ResourceDispatcherHost* rdh, |
310 DownloadProcessHandle process_handle, | 319 DownloadProcessHandle process_handle, |
311 bool pause); | 320 bool pause); |
312 | 321 |
313 // Inform observers that the model has changed. | 322 // Inform observers that the model has changed. |
314 void NotifyModelChanged(); | 323 void NotifyModelChanged(); |
315 | 324 |
| 325 // Get the download item from the history map. Useful after the item has |
| 326 // been removed from the active map, or was retrieved from the history DB. |
316 DownloadItem* GetDownloadItem(int id); | 327 DownloadItem* GetDownloadItem(int id); |
317 | 328 |
| 329 // Get the download item from the active map. Useful when the item is not |
| 330 // yet in the history map. |
| 331 DownloadItem* GetActiveDownloadItem(int id); |
| 332 |
318 // Debugging routine to confirm relationship between below | 333 // Debugging routine to confirm relationship between below |
319 // containers; no-op if NDEBUG. | 334 // containers; no-op if NDEBUG. |
320 void AssertContainersConsistent() const; | 335 void AssertContainersConsistent() const; |
321 | 336 |
322 // |downloads_| is the owning set for all downloads known to the | 337 // |downloads_| is the owning set for all downloads known to the |
323 // DownloadManager. This includes downloads started by the user in | 338 // DownloadManager. This includes downloads started by the user in |
324 // this session, downloads initialized from the history system, and | 339 // this session, downloads initialized from the history system, and |
325 // "save page as" downloads. All other DownloadItem containers in | 340 // "save page as" downloads. All other DownloadItem containers in |
326 // the DownloadManager are maps; they do not own the DownloadItems. | 341 // the DownloadManager are maps; they do not own the DownloadItems. |
327 // Note that this is the only place (with any functional implications; | 342 // Note that this is the only place (with any functional implications; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // The "Save As" dialog box used to ask the user where a file should be | 407 // The "Save As" dialog box used to ask the user where a file should be |
393 // saved. | 408 // saved. |
394 scoped_refptr<SelectFileDialog> select_file_dialog_; | 409 scoped_refptr<SelectFileDialog> select_file_dialog_; |
395 | 410 |
396 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; | 411 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; |
397 | 412 |
398 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 413 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
399 }; | 414 }; |
400 | 415 |
401 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 416 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |