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 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
86 | 86 |
87 // Called immediately after the DownloadManager puts up a select file | 87 // Called immediately after the DownloadManager puts up a select file |
88 // dialog. | 88 // dialog. |
89 // |id| indicates which download opened the dialog. | 89 // |id| indicates which download opened the dialog. |
90 virtual void SelectFileDialogDisplayed(int32 id) {} | 90 virtual void SelectFileDialogDisplayed(int32 id) {} |
91 | 91 |
92 protected: | 92 protected: |
93 virtual ~Observer() {} | 93 virtual ~Observer() {} |
94 }; | 94 }; |
95 | 95 |
96 typedef std::vector<DownloadItem*> DownloadVector; | |
97 | |
96 // Return all temporary downloads that reside in the specified directory. | 98 // Return all temporary downloads that reside in the specified directory. |
97 void GetTemporaryDownloads(const FilePath& dir_path, | 99 void GetTemporaryDownloads(const FilePath& dir_path, DownloadVector* result); |
98 std::vector<DownloadItem*>* result); | |
99 | 100 |
100 // Return all non-temporary downloads in the specified directory that are | 101 // Return all non-temporary downloads in the specified directory that are |
101 // are in progress or have completed. | 102 // are in progress or have completed. |
102 void GetAllDownloads(const FilePath& dir_path, | 103 void GetAllDownloads(const FilePath& dir_path, DownloadVector* result); |
103 std::vector<DownloadItem*>* result); | |
104 | 104 |
105 // Return all non-temporary downloads in the specified directory that are | 105 // Return all non-temporary downloads in the specified directory that are |
106 // in-progress (including dangerous downloads waiting for user confirmation). | 106 // in-progress (including dangerous downloads waiting for user confirmation). |
107 void GetCurrentDownloads(const FilePath& dir_path, | 107 void GetCurrentDownloads(const FilePath& dir_path, DownloadVector* result); |
108 std::vector<DownloadItem*>* result); | |
109 | 108 |
110 // Returns all non-temporary downloads matching |query|. Empty query matches | 109 // Returns all non-temporary downloads matching |query|. Empty query matches |
111 // everything. | 110 // everything. |
112 void SearchDownloads(const string16& query, | 111 void SearchDownloads(const string16& query, DownloadVector* result); |
113 std::vector<DownloadItem*>* result); | |
114 | 112 |
115 // Returns true if initialized properly. | 113 // Returns true if initialized properly. |
116 bool Init(Profile* profile); | 114 bool Init(Profile* profile); |
117 | 115 |
118 // Notifications sent from the download thread to the UI thread | 116 // Notifications sent from the download thread to the UI thread |
119 void StartDownload(int32 id); | 117 void StartDownload(int32 id); |
120 void UpdateDownload(int32 download_id, int64 size); | 118 void UpdateDownload(int32 download_id, int64 size); |
121 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 119 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
122 // is not available. | 120 // is not available. |
123 void OnResponseCompleted(int32 download_id, int64 size, int os_error, | 121 void OnResponseCompleted(int32 download_id, int64 size, int os_error, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
157 int RemoveDownloads(const base::Time remove_begin); | 155 int RemoveDownloads(const base::Time remove_begin); |
158 | 156 |
159 // Remove all downloads will delete all downloads. The number of downloads | 157 // Remove all downloads will delete all downloads. The number of downloads |
160 // deleted is returned back to the caller. | 158 // deleted is returned back to the caller. |
161 int RemoveAllDownloads(); | 159 int RemoveAllDownloads(); |
162 | 160 |
163 // Final download manager transition for download: Update the download | 161 // Final download manager transition for download: Update the download |
164 // history and remove the download from |active_downloads_|. | 162 // history and remove the download from |active_downloads_|. |
165 void DownloadCompleted(int32 download_id); | 163 void DownloadCompleted(int32 download_id); |
166 | 164 |
167 // Called when a Save Page As download is started. Transfers ownership | |
168 // of |download_item| to the DownloadManager. | |
169 void SavePageAsDownloadStarted(DownloadItem* download_item); | |
170 | |
171 // Download the object at the URL. Used in cases such as "Save Link As..." | 165 // Download the object at the URL. Used in cases such as "Save Link As..." |
172 void DownloadUrl(const GURL& url, | 166 void DownloadUrl(const GURL& url, |
173 const GURL& referrer, | 167 const GURL& referrer, |
174 const std::string& referrer_encoding, | 168 const std::string& referrer_encoding, |
175 TabContents* tab_contents); | 169 TabContents* tab_contents); |
176 | 170 |
177 // Download the object at the URL and save it to the specified path. The | 171 // Download the object at the URL and save it to the specified path. The |
178 // download is treated as the temporary download and thus will not appear | 172 // download is treated as the temporary download and thus will not appear |
179 // in the download history. Used in cases such as drag and drop. | 173 // in the download history. Used in cases such as drag and drop. |
180 void DownloadUrlToFile(const GURL& url, | 174 void DownloadUrlToFile(const GURL& url, |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
256 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); | 250 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); |
257 | 251 |
258 // Assert the named download item is on the correct queues | 252 // Assert the named download item is on the correct queues |
259 // in the DownloadManager. For debugging. | 253 // in the DownloadManager. For debugging. |
260 void AssertQueueStateConsistent(DownloadItem* download); | 254 void AssertQueueStateConsistent(DownloadItem* download); |
261 | 255 |
262 // Get the download item from the history map. Useful after the item has | 256 // Get the download item from the history map. Useful after the item has |
263 // been removed from the active map, or was retrieved from the history DB. | 257 // been removed from the active map, or was retrieved from the history DB. |
264 DownloadItem* GetDownloadItem(int id); | 258 DownloadItem* GetDownloadItem(int id); |
265 | 259 |
260 // Called when Save Page download starts. Transfers ownership of |download| | |
261 // to the DownloadManager. | |
262 void SavePageDownloadStarted(DownloadItem* download); | |
263 | |
264 // Callback when Save Page As entry is commited to the history system. | |
265 void OnSavePageDownloadEntryAdded(int32 download_id, int64 db_handle); | |
266 | |
267 // Called when Save Page download is done. | |
268 void SavePageDownloadFinished(DownloadItem* download); | |
269 | |
270 // Download Id for next Save Page. | |
271 int32 GetNextSavePageId(); | |
272 | |
266 DownloadManagerDelegate* delegate() const { return delegate_; } | 273 DownloadManagerDelegate* delegate() const { return delegate_; } |
267 | 274 |
268 private: | 275 private: |
276 typedef std::set<DownloadItem*> DownloadSet; | |
277 typedef base::hash_map<int64, DownloadItem*> DownloadMap; | |
278 | |
279 #if defined(UNIT_TEST) | |
Randy Smith (Not in Mondays)
2011/08/04 16:01:32
It's Pawel's suggestion, not mine, but I don't thi
Paweł Hajdan Jr.
2011/08/04 18:11:47
Well, putting friend declarations for test classes
achuithb
2011/08/04 18:16:11
Done.
| |
269 // For testing. | 280 // For testing. |
270 friend class DownloadManagerTest; | 281 friend class DownloadManagerTest; |
271 friend class MockDownloadManager; | 282 friend class MockDownloadManager; |
283 friend class SavePageBrowserTest; | |
284 #endif | |
272 | 285 |
273 // This class is used to let an incognito DownloadManager observe changes to | 286 // This class is used to let an incognito DownloadManager observe changes to |
274 // a normal DownloadManager, to propagate ModelChanged() calls from the parent | 287 // a normal DownloadManager, to propagate ModelChanged() calls from the parent |
275 // DownloadManager to the observers of the incognito DownloadManager. | 288 // DownloadManager to the observers of the incognito DownloadManager. |
276 class OtherDownloadManagerObserver : public Observer { | 289 class OtherDownloadManagerObserver : public Observer { |
277 public: | 290 public: |
278 explicit OtherDownloadManagerObserver( | 291 explicit OtherDownloadManagerObserver( |
279 DownloadManager* observing_download_manager); | 292 DownloadManager* observing_download_manager); |
280 virtual ~OtherDownloadManagerObserver(); | 293 virtual ~OtherDownloadManagerObserver(); |
281 | 294 |
282 // Observer interface. | 295 // Observer interface. |
283 virtual void ModelChanged(); | 296 virtual void ModelChanged(); |
284 virtual void ManagerGoingDown(); | 297 virtual void ManagerGoingDown(); |
285 | 298 |
286 private: | 299 private: |
287 // The incognito download manager. | 300 // The incognito download manager. |
288 DownloadManager* observing_download_manager_; | 301 DownloadManager* observing_download_manager_; |
289 | 302 |
290 // The original profile's download manager. | 303 // The original profile's download manager. |
291 DownloadManager* observed_download_manager_; | 304 DownloadManager* observed_download_manager_; |
292 }; | 305 }; |
293 | 306 |
294 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 307 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
295 friend class DeleteTask<DownloadManager>; | 308 friend class DeleteTask<DownloadManager>; |
296 friend class OtherDownloadManagerObserver; | 309 friend class OtherDownloadManagerObserver; |
297 | 310 |
298 virtual ~DownloadManager(); | 311 virtual ~DownloadManager(); |
299 | 312 |
313 DownloadHistory* download_history() { return download_history_.get(); } | |
314 | |
300 // Called on the FILE thread to check the existence of a downloaded file. | 315 // Called on the FILE thread to check the existence of a downloaded file. |
301 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); | 316 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); |
302 | 317 |
303 // Called on the UI thread if the FILE thread detects the removal of | 318 // Called on the UI thread if the FILE thread detects the removal of |
304 // the downloaded file. The UI thread updates the state of the file | 319 // the downloaded file. The UI thread updates the state of the file |
305 // and then notifies this update to the file's observer. | 320 // and then notifies this update to the file's observer. |
306 void OnFileRemovalDetected(int64 db_handle); | 321 void OnFileRemovalDetected(int64 db_handle); |
307 | 322 |
308 // Called on the download thread to check whether the suggested file path | 323 // Called on the download thread to check whether the suggested file path |
309 // exists. We don't check if the file exists on the UI thread to avoid UI | 324 // exists. We don't check if the file exists on the UI thread to avoid UI |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 // yet in the history map. | 360 // yet in the history map. |
346 DownloadItem* GetActiveDownloadItem(int id); | 361 DownloadItem* GetActiveDownloadItem(int id); |
347 | 362 |
348 // Debugging routine to confirm relationship between below | 363 // Debugging routine to confirm relationship between below |
349 // containers; no-op if NDEBUG. | 364 // containers; no-op if NDEBUG. |
350 void AssertContainersConsistent() const; | 365 void AssertContainersConsistent() const; |
351 | 366 |
352 // Add a DownloadItem to history_downloads_. | 367 // Add a DownloadItem to history_downloads_. |
353 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); | 368 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); |
354 | 369 |
370 // Remove from internal maps. | |
371 int RemoveDownloadItems(const DownloadVector& pending_deletes); | |
372 | |
355 // |downloads_| is the owning set for all downloads known to the | 373 // |downloads_| is the owning set for all downloads known to the |
356 // DownloadManager. This includes downloads started by the user in | 374 // DownloadManager. This includes downloads started by the user in |
357 // this session, downloads initialized from the history system, and | 375 // this session, downloads initialized from the history system, and |
358 // "save page as" downloads. All other DownloadItem containers in | 376 // "save page as" downloads. All other DownloadItem containers in |
359 // the DownloadManager are maps; they do not own the DownloadItems. | 377 // the DownloadManager are maps; they do not own the DownloadItems. |
360 // Note that this is the only place (with any functional implications; | 378 // Note that this is the only place (with any functional implications; |
361 // see save_page_as_downloads_ below) that "save page as" downloads are | 379 // see save_page_as_downloads_ below) that "save page as" downloads are |
362 // kept, as the DownloadManager's only job is to hold onto those | 380 // kept, as the DownloadManager's only job is to hold onto those |
363 // until destruction. | 381 // until destruction. |
364 // | 382 // |
(...skipping 16 matching lines...) Expand all Loading... | |
381 // When a download is created through a user action, the corresponding | 399 // When a download is created through a user action, the corresponding |
382 // DownloadItem* is placed in |active_downloads_| and remains there until the | 400 // DownloadItem* is placed in |active_downloads_| and remains there until the |
383 // download is in a terminal state (COMPLETE or CANCELLED). It is also | 401 // download is in a terminal state (COMPLETE or CANCELLED). It is also |
384 // placed in |in_progress_| and remains there until it has received a | 402 // placed in |in_progress_| and remains there until it has received a |
385 // valid handle from the history system. Once it has a valid handle, the | 403 // valid handle from the history system. Once it has a valid handle, the |
386 // DownloadItem* is placed in the |history_downloads_| map. When the | 404 // DownloadItem* is placed in the |history_downloads_| map. When the |
387 // download reaches a terminal state, it is removed from |in_progress_|. | 405 // download reaches a terminal state, it is removed from |in_progress_|. |
388 // Downloads from past sessions read from a persisted state from the | 406 // Downloads from past sessions read from a persisted state from the |
389 // history system are placed directly into |history_downloads_| since | 407 // history system are placed directly into |history_downloads_| since |
390 // they have valid handles in the history system. | 408 // they have valid handles in the history system. |
391 typedef std::set<DownloadItem*> DownloadSet; | |
392 typedef base::hash_map<int64, DownloadItem*> DownloadMap; | |
393 | 409 |
394 DownloadSet downloads_; | 410 DownloadSet downloads_; |
395 DownloadMap history_downloads_; | 411 DownloadMap history_downloads_; |
396 DownloadMap in_progress_; | 412 DownloadMap in_progress_; |
397 DownloadMap active_downloads_; | 413 DownloadMap active_downloads_; |
398 #if !defined(NDEBUG) | 414 DownloadMap save_page_downloads_; |
399 DownloadSet save_page_as_downloads_; | |
400 #endif | |
401 | 415 |
402 // True if the download manager has been initialized and requires a shutdown. | 416 // True if the download manager has been initialized and requires a shutdown. |
403 bool shutdown_needed_; | 417 bool shutdown_needed_; |
404 | 418 |
405 // Observers that want to be notified of changes to the set of downloads. | 419 // Observers that want to be notified of changes to the set of downloads. |
406 ObserverList<Observer> observers_; | 420 ObserverList<Observer> observers_; |
407 | 421 |
408 // The current active profile. | 422 // The current active profile. |
409 Profile* profile_; | 423 Profile* profile_; |
410 | 424 |
411 scoped_ptr<DownloadHistory> download_history_; | 425 scoped_ptr<DownloadHistory> download_history_; |
412 | 426 |
413 scoped_ptr<DownloadPrefs> download_prefs_; | 427 scoped_ptr<DownloadPrefs> download_prefs_; |
414 | 428 |
415 // Non-owning pointer for handling file writing on the download_thread_. | 429 // Non-owning pointer for handling file writing on the download_thread_. |
416 DownloadFileManager* file_manager_; | 430 DownloadFileManager* file_manager_; |
417 | 431 |
418 // Non-owning pointer for updating the download status. | 432 // Non-owning pointer for updating the download status. |
419 base::WeakPtr<DownloadStatusUpdater> status_updater_; | 433 base::WeakPtr<DownloadStatusUpdater> status_updater_; |
420 | 434 |
421 // The user's last choice for download directory. This is only used when the | 435 // The user's last choice for download directory. This is only used when the |
422 // user wants us to prompt for a save location for each download. | 436 // user wants us to prompt for a save location for each download. |
423 FilePath last_download_path_; | 437 FilePath last_download_path_; |
424 | 438 |
439 // Download Id for next Save Page. | |
440 int32 next_save_page_id_; | |
441 | |
425 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; | 442 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; |
426 | 443 |
427 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 444 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
428 DownloadManagerDelegate* delegate_; | 445 DownloadManagerDelegate* delegate_; |
429 | 446 |
430 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 447 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
431 }; | 448 }; |
432 | 449 |
433 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 450 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |