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 18 matching lines...) Expand all Loading... |
199 | 193 |
200 // The number of in progress (including paused) downloads. | 194 // The number of in progress (including paused) downloads. |
201 int in_progress_count() const { | 195 int in_progress_count() const { |
202 return static_cast<int>(in_progress_.size()); | 196 return static_cast<int>(in_progress_.size()); |
203 } | 197 } |
204 | 198 |
205 Profile* profile() { return profile_; } | 199 Profile* profile() { return profile_; } |
206 | 200 |
207 DownloadPrefs* download_prefs() { return download_prefs_.get(); } | 201 DownloadPrefs* download_prefs() { return download_prefs_.get(); } |
208 | 202 |
| 203 #if defined(UNIT_TEST) |
| 204 DownloadHistory* download_history() { return download_history_.get(); } |
| 205 #endif |
| 206 |
209 // Creates the download item. Must be called on the UI thread. | 207 // Creates the download item. Must be called on the UI thread. |
210 void CreateDownloadItem(DownloadCreateInfo* info); | 208 void CreateDownloadItem(DownloadCreateInfo* info); |
211 | 209 |
212 // Clears the last download path, used to initialize "save as" dialogs. | 210 // Clears the last download path, used to initialize "save as" dialogs. |
213 void ClearLastDownloadPath(); | 211 void ClearLastDownloadPath(); |
214 | 212 |
215 // Tests if a file type should be opened automatically. | 213 // Tests if a file type should be opened automatically. |
216 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; | 214 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; |
217 | 215 |
218 // Overridden from DownloadStatusUpdaterDelegate: | 216 // Overridden from DownloadStatusUpdaterDelegate: |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
256 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); | 254 void CheckDownloadHashDone(int32 download_id, bool is_dangerous_hash); |
257 | 255 |
258 // Assert the named download item is on the correct queues | 256 // Assert the named download item is on the correct queues |
259 // in the DownloadManager. For debugging. | 257 // in the DownloadManager. For debugging. |
260 void AssertQueueStateConsistent(DownloadItem* download); | 258 void AssertQueueStateConsistent(DownloadItem* download); |
261 | 259 |
262 // Get the download item from the history map. Useful after the item has | 260 // 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. | 261 // been removed from the active map, or was retrieved from the history DB. |
264 DownloadItem* GetDownloadItem(int id); | 262 DownloadItem* GetDownloadItem(int id); |
265 | 263 |
| 264 // Called when Save Page download starts. Transfers ownership of |download| |
| 265 // to the DownloadManager. |
| 266 void SavePageDownloadStarted(DownloadItem* download); |
| 267 |
| 268 // Callback when Save Page As entry is commited to the history system. |
| 269 void OnSavePageDownloadEntryAdded(int32 download_id, int64 db_handle); |
| 270 |
| 271 // Called when Save Page download is done. |
| 272 void SavePageDownloadFinished(DownloadItem* download); |
| 273 |
| 274 // Download Id for next Save Page. |
| 275 int32 GetNextSavePageId(); |
| 276 |
266 DownloadManagerDelegate* delegate() const { return delegate_; } | 277 DownloadManagerDelegate* delegate() const { return delegate_; } |
267 | 278 |
268 private: | 279 private: |
| 280 typedef std::set<DownloadItem*> DownloadSet; |
| 281 typedef base::hash_map<int64, DownloadItem*> DownloadMap; |
| 282 |
269 // For testing. | 283 // For testing. |
270 friend class DownloadManagerTest; | 284 friend class DownloadManagerTest; |
271 friend class MockDownloadManager; | 285 friend class MockDownloadManager; |
272 | 286 |
273 // This class is used to let an incognito DownloadManager observe changes to | 287 // This class is used to let an incognito DownloadManager observe changes to |
274 // a normal DownloadManager, to propagate ModelChanged() calls from the parent | 288 // a normal DownloadManager, to propagate ModelChanged() calls from the parent |
275 // DownloadManager to the observers of the incognito DownloadManager. | 289 // DownloadManager to the observers of the incognito DownloadManager. |
276 class OtherDownloadManagerObserver : public Observer { | 290 class OtherDownloadManagerObserver : public Observer { |
277 public: | 291 public: |
278 explicit OtherDownloadManagerObserver( | 292 explicit OtherDownloadManagerObserver( |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 // yet in the history map. | 359 // yet in the history map. |
346 DownloadItem* GetActiveDownloadItem(int id); | 360 DownloadItem* GetActiveDownloadItem(int id); |
347 | 361 |
348 // Debugging routine to confirm relationship between below | 362 // Debugging routine to confirm relationship between below |
349 // containers; no-op if NDEBUG. | 363 // containers; no-op if NDEBUG. |
350 void AssertContainersConsistent() const; | 364 void AssertContainersConsistent() const; |
351 | 365 |
352 // Add a DownloadItem to history_downloads_. | 366 // Add a DownloadItem to history_downloads_. |
353 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); | 367 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); |
354 | 368 |
| 369 // Remove from internal maps. |
| 370 int RemoveDownloadItems(const DownloadVector& pending_deletes); |
| 371 |
355 // |downloads_| is the owning set for all downloads known to the | 372 // |downloads_| is the owning set for all downloads known to the |
356 // DownloadManager. This includes downloads started by the user in | 373 // DownloadManager. This includes downloads started by the user in |
357 // this session, downloads initialized from the history system, and | 374 // this session, downloads initialized from the history system, and |
358 // "save page as" downloads. All other DownloadItem containers in | 375 // "save page as" downloads. All other DownloadItem containers in |
359 // the DownloadManager are maps; they do not own the DownloadItems. | 376 // the DownloadManager are maps; they do not own the DownloadItems. |
360 // Note that this is the only place (with any functional implications; | 377 // Note that this is the only place (with any functional implications; |
361 // see save_page_as_downloads_ below) that "save page as" downloads are | 378 // 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 | 379 // kept, as the DownloadManager's only job is to hold onto those |
363 // until destruction. | 380 // until destruction. |
364 // | 381 // |
(...skipping 16 matching lines...) Expand all Loading... |
381 // When a download is created through a user action, the corresponding | 398 // When a download is created through a user action, the corresponding |
382 // DownloadItem* is placed in |active_downloads_| and remains there until the | 399 // DownloadItem* is placed in |active_downloads_| and remains there until the |
383 // download is in a terminal state (COMPLETE or CANCELLED). It is also | 400 // 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 | 401 // 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 | 402 // valid handle from the history system. Once it has a valid handle, the |
386 // DownloadItem* is placed in the |history_downloads_| map. When the | 403 // DownloadItem* is placed in the |history_downloads_| map. When the |
387 // download reaches a terminal state, it is removed from |in_progress_|. | 404 // download reaches a terminal state, it is removed from |in_progress_|. |
388 // Downloads from past sessions read from a persisted state from the | 405 // Downloads from past sessions read from a persisted state from the |
389 // history system are placed directly into |history_downloads_| since | 406 // history system are placed directly into |history_downloads_| since |
390 // they have valid handles in the history system. | 407 // they have valid handles in the history system. |
391 typedef std::set<DownloadItem*> DownloadSet; | |
392 typedef base::hash_map<int64, DownloadItem*> DownloadMap; | |
393 | 408 |
394 DownloadSet downloads_; | 409 DownloadSet downloads_; |
395 DownloadMap history_downloads_; | 410 DownloadMap history_downloads_; |
396 DownloadMap in_progress_; | 411 DownloadMap in_progress_; |
397 DownloadMap active_downloads_; | 412 DownloadMap active_downloads_; |
398 #if !defined(NDEBUG) | 413 DownloadMap save_page_downloads_; |
399 DownloadSet save_page_as_downloads_; | |
400 #endif | |
401 | 414 |
402 // True if the download manager has been initialized and requires a shutdown. | 415 // True if the download manager has been initialized and requires a shutdown. |
403 bool shutdown_needed_; | 416 bool shutdown_needed_; |
404 | 417 |
405 // Observers that want to be notified of changes to the set of downloads. | 418 // Observers that want to be notified of changes to the set of downloads. |
406 ObserverList<Observer> observers_; | 419 ObserverList<Observer> observers_; |
407 | 420 |
408 // The current active profile. | 421 // The current active profile. |
409 Profile* profile_; | 422 Profile* profile_; |
410 | 423 |
411 scoped_ptr<DownloadHistory> download_history_; | 424 scoped_ptr<DownloadHistory> download_history_; |
412 | 425 |
413 scoped_ptr<DownloadPrefs> download_prefs_; | 426 scoped_ptr<DownloadPrefs> download_prefs_; |
414 | 427 |
415 // Non-owning pointer for handling file writing on the download_thread_. | 428 // Non-owning pointer for handling file writing on the download_thread_. |
416 DownloadFileManager* file_manager_; | 429 DownloadFileManager* file_manager_; |
417 | 430 |
418 // Non-owning pointer for updating the download status. | 431 // Non-owning pointer for updating the download status. |
419 base::WeakPtr<DownloadStatusUpdater> status_updater_; | 432 base::WeakPtr<DownloadStatusUpdater> status_updater_; |
420 | 433 |
421 // The user's last choice for download directory. This is only used when the | 434 // 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. | 435 // user wants us to prompt for a save location for each download. |
423 FilePath last_download_path_; | 436 FilePath last_download_path_; |
424 | 437 |
| 438 // Download Id for next Save Page. |
| 439 int32 next_save_page_id_; |
| 440 |
425 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; | 441 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; |
426 | 442 |
427 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 443 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
428 DownloadManagerDelegate* delegate_; | 444 DownloadManagerDelegate* delegate_; |
429 | 445 |
430 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 446 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
431 }; | 447 }; |
432 | 448 |
433 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 449 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |