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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 #include "chrome/browser/download/download_request_handle.h" | 46 #include "chrome/browser/download/download_request_handle.h" |
47 #include "chrome/browser/download/download_status_updater_delegate.h" | 47 #include "chrome/browser/download/download_status_updater_delegate.h" |
48 #include "chrome/browser/ui/shell_dialogs.h" | 48 #include "chrome/browser/ui/shell_dialogs.h" |
49 #include "content/browser/browser_thread.h" | 49 #include "content/browser/browser_thread.h" |
50 | 50 |
51 class DownloadFileManager; | 51 class DownloadFileManager; |
52 class DownloadHistory; | 52 class DownloadHistory; |
53 class DownloadPrefs; | 53 class DownloadPrefs; |
54 class DownloadStatusUpdater; | 54 class DownloadStatusUpdater; |
55 class GURL; | 55 class GURL; |
| 56 class ListValue; |
56 class Profile; | 57 class Profile; |
57 class ResourceDispatcherHost; | 58 class ResourceDispatcherHost; |
58 class TabContents; | 59 class TabContents; |
59 struct DownloadCreateInfo; | 60 struct DownloadCreateInfo; |
60 struct DownloadHistoryInfo; | 61 struct DownloadHistoryInfo; |
61 struct DownloadSaveInfo; | 62 struct DownloadSaveInfo; |
| 63 namespace download_util { |
| 64 // Try to avoid including download_query.h because it defines some macros. |
| 65 class DownloadQuery; |
| 66 } // namespace download_util |
62 | 67 |
63 // Browser's download manager: manages all downloads and destination view. | 68 // Browser's download manager: manages all downloads and destination view. |
64 class DownloadManager | 69 class DownloadManager |
65 : public base::RefCountedThreadSafe<DownloadManager, | 70 : public base::RefCountedThreadSafe<DownloadManager, |
66 BrowserThread::DeleteOnUIThread>, | 71 BrowserThread::DeleteOnUIThread>, |
67 public DownloadStatusUpdaterDelegate, | 72 public DownloadStatusUpdaterDelegate, |
68 public SelectFileDialog::Listener { | 73 public SelectFileDialog::Listener { |
69 public: | 74 public: |
| 75 typedef std::vector<DownloadItem*> ItemVector; |
| 76 |
70 explicit DownloadManager(DownloadStatusUpdater* status_updater); | 77 explicit DownloadManager(DownloadStatusUpdater* status_updater); |
71 | 78 |
72 // Shutdown the download manager. Must be called before destruction. | 79 // Shutdown the download manager. Must be called before destruction. |
73 void Shutdown(); | 80 void Shutdown(); |
74 | 81 |
75 // Interface to implement for observers that wish to be informed of changes | 82 // Interface to implement for observers that wish to be informed of changes |
76 // to the DownloadManager's collection of downloads. | 83 // to the DownloadManager's collection of downloads. |
77 class Observer { | 84 class Observer { |
78 public: | 85 public: |
79 // New or deleted download, observers should query us for the current set | 86 // New or deleted download, observers should query us for the current set |
80 // of downloads. | 87 // of downloads. |
81 virtual void ModelChanged() = 0; | 88 virtual void ModelChanged() = 0; |
82 | 89 |
83 // Called when the DownloadManager is being destroyed to prevent Observers | 90 // Called when the DownloadManager is being destroyed to prevent Observers |
84 // from calling back to a stale pointer. | 91 // from calling back to a stale pointer. |
85 virtual void ManagerGoingDown() {} | 92 virtual void ManagerGoingDown() {} |
86 | 93 |
87 // Called immediately after the DownloadManager puts up a select file | 94 // Called immediately after the DownloadManager puts up a select file |
88 // dialog. | 95 // dialog. |
89 // |id| indicates which download opened the dialog. | 96 // |id| indicates which download opened the dialog. |
90 virtual void SelectFileDialogDisplayed(int32 id) {} | 97 virtual void SelectFileDialogDisplayed(int32 id) {} |
91 | 98 |
92 protected: | 99 protected: |
93 virtual ~Observer() {} | 100 virtual ~Observer() {} |
94 }; | 101 }; |
95 | 102 |
96 // Return all temporary downloads that reside in the specified directory. | 103 // Return all temporary downloads that reside in the specified directory. |
97 void GetTemporaryDownloads(const FilePath& dir_path, | 104 void GetTemporaryDownloads(const FilePath& dir_path, |
98 std::vector<DownloadItem*>* result); | 105 ItemVector* result); |
99 | 106 |
100 // Return all non-temporary downloads in the specified directory that are | 107 // Return all non-temporary downloads in the specified directory that are |
101 // are in progress or have completed. | 108 // are in progress or have completed. |
102 void GetAllDownloads(const FilePath& dir_path, | 109 void GetAllDownloads(const FilePath& dir_path, |
103 std::vector<DownloadItem*>* result); | 110 ItemVector* result); |
104 | 111 |
105 // Return all non-temporary downloads in the specified directory that are | 112 // Return all non-temporary downloads in the specified directory that are |
106 // in-progress (including dangerous downloads waiting for user confirmation). | 113 // in-progress (including dangerous downloads waiting for user confirmation). |
107 void GetCurrentDownloads(const FilePath& dir_path, | 114 void GetCurrentDownloads(const FilePath& dir_path, |
108 std::vector<DownloadItem*>* result); | 115 ItemVector* result); |
| 116 |
| 117 bool Search(const download_util::DownloadQuery& query, |
| 118 ItemVector* results = NULL, |
| 119 bool merge_parent_manager = true, |
| 120 std::string* error_msg = NULL, |
| 121 ListValue* json_results = NULL) const; |
109 | 122 |
110 // Returns all non-temporary downloads matching |query|. Empty query matches | 123 // Returns all non-temporary downloads matching |query|. Empty query matches |
111 // everything. | 124 // everything. |
112 void SearchDownloads(const string16& query, | 125 void SearchDownloads(const string16& query, |
113 std::vector<DownloadItem*>* result); | 126 ItemVector* result); |
114 | 127 |
115 // Returns true if initialized properly. | 128 // Returns true if initialized properly. |
116 bool Init(Profile* profile); | 129 bool Init(Profile* profile); |
117 | 130 |
118 // Notifications sent from the download thread to the UI thread | 131 // Notifications sent from the download thread to the UI thread |
119 void StartDownload(int32 id); | 132 void StartDownload(int32 id); |
120 void UpdateDownload(int32 download_id, int64 size); | 133 void UpdateDownload(int32 download_id, int64 size); |
121 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 134 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
122 // is not available. | 135 // is not available. |
123 void OnResponseCompleted(int32 download_id, int64 size, int os_error, | 136 void OnResponseCompleted(int32 download_id, int64 size, int os_error, |
124 const std::string& hash); | 137 const std::string& hash); |
125 | 138 |
126 // Called from a view when a user clicks a UI button or link. | 139 // Called from a view when a user clicks a UI button or link. |
127 void DownloadCancelled(int32 download_id); | 140 void DownloadCancelled(int32 download_id); |
128 void RemoveDownload(int64 download_handle); | 141 void RemoveDownload(int64 id); |
129 | 142 |
130 // Determine if the download is ready for completion, i.e. has had | 143 // Determine if the download is ready for completion, i.e. has had |
131 // all data saved, and completed the filename determination and | 144 // all data saved, and completed the filename determination and |
132 // history insertion. | 145 // history insertion. |
133 bool IsDownloadReadyForCompletion(DownloadItem* download); | 146 bool IsDownloadReadyForCompletion(DownloadItem* download); |
134 | 147 |
135 // If all pre-requisites have been met, complete download processing, i.e. | 148 // If all pre-requisites have been met, complete download processing, i.e. |
136 // do internal cleanup, file rename, and potentially auto-open. | 149 // do internal cleanup, file rename, and potentially auto-open. |
137 // (Dangerous downloads still may block on user acceptance after this | 150 // (Dangerous downloads still may block on user acceptance after this |
138 // point.) | 151 // point.) |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 198 |
186 // Allow objects to observe the download creation process. | 199 // Allow objects to observe the download creation process. |
187 void AddObserver(Observer* observer); | 200 void AddObserver(Observer* observer); |
188 | 201 |
189 // Remove a download observer from ourself. | 202 // Remove a download observer from ourself. |
190 void RemoveObserver(Observer* observer); | 203 void RemoveObserver(Observer* observer); |
191 | 204 |
192 // Methods called on completion of a query sent to the history system. | 205 // Methods called on completion of a query sent to the history system. |
193 void OnQueryDownloadEntriesComplete( | 206 void OnQueryDownloadEntriesComplete( |
194 std::vector<DownloadHistoryInfo>* entries); | 207 std::vector<DownloadHistoryInfo>* entries); |
195 void OnCreateDownloadEntryComplete(int32 download_id, int64 db_handle); | 208 void OnCreateDownloadEntryComplete(int32 download_id); |
196 | 209 |
197 // Display a new download in the appropriate browser UI. | 210 // Display a new download in the appropriate browser UI. |
198 void ShowDownloadInBrowser(DownloadItem* download); | 211 void ShowDownloadInBrowser(DownloadItem* download); |
199 | 212 |
200 // The number of in progress (including paused) downloads. | 213 // The number of in progress (including paused) downloads. |
201 int in_progress_count() const { | 214 int in_progress_count() const { |
202 return static_cast<int>(in_progress_.size()); | 215 return GetInProgressDownloadCount(); |
203 } | 216 } |
204 | 217 |
205 Profile* profile() { return profile_; } | 218 Profile* profile() { return profile_; } |
206 | 219 |
207 DownloadPrefs* download_prefs() { return download_prefs_.get(); } | 220 DownloadPrefs* download_prefs() { return download_prefs_.get(); } |
208 | 221 |
209 // Creates the download item. Must be called on the UI thread. | 222 // Creates the download item. Must be called on the UI thread. |
210 void CreateDownloadItem(DownloadCreateInfo* info); | 223 void CreateDownloadItem(DownloadCreateInfo* info); |
211 | 224 |
212 // Clears the last download path, used to initialize "save as" dialogs. | 225 // Clears the last download path, used to initialize "save as" dialogs. |
213 void ClearLastDownloadPath(); | 226 void ClearLastDownloadPath(); |
214 | 227 |
215 // Tests if a file type should be opened automatically. | 228 // Tests if a file type should be opened automatically. |
216 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; | 229 bool ShouldOpenFileBasedOnExtension(const FilePath& path) const; |
217 | 230 |
218 // Overridden from DownloadStatusUpdaterDelegate: | 231 // Overridden from DownloadStatusUpdaterDelegate: |
219 virtual bool IsDownloadProgressKnown(); | 232 virtual bool IsDownloadProgressKnown(); |
220 virtual int64 GetInProgressDownloadCount(); | 233 virtual int64 GetInProgressDownloadCount() const; |
221 virtual int64 GetReceivedDownloadBytes(); | 234 virtual int64 GetReceivedDownloadBytes(); |
222 virtual int64 GetTotalDownloadBytes(); | 235 virtual int64 GetTotalDownloadBytes(); |
223 | 236 |
224 // Overridden from SelectFileDialog::Listener: | 237 // Overridden from SelectFileDialog::Listener: |
225 virtual void FileSelected(const FilePath& path, int index, void* params); | 238 virtual void FileSelected(const FilePath& path, int index, void* params); |
226 virtual void FileSelectionCanceled(void* params); | 239 virtual void FileSelectionCanceled(void* params); |
227 | 240 |
228 // Returns true if this download should show the "dangerous file" warning. | 241 // Returns true if this download should show the "dangerous file" warning. |
229 // Various factors are considered, such as the type of the file, whether a | 242 // Various factors are considered, such as the type of the file, whether a |
230 // user action initiated the download, and whether the user has explicitly | 243 // user action initiated the download, and whether the user has explicitly |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 // The original profile's download manager. | 304 // The original profile's download manager. |
292 DownloadManager* observed_download_manager_; | 305 DownloadManager* observed_download_manager_; |
293 }; | 306 }; |
294 | 307 |
295 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; | 308 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>; |
296 friend class DeleteTask<DownloadManager>; | 309 friend class DeleteTask<DownloadManager>; |
297 friend class OtherDownloadManagerObserver; | 310 friend class OtherDownloadManagerObserver; |
298 | 311 |
299 virtual ~DownloadManager(); | 312 virtual ~DownloadManager(); |
300 | 313 |
| 314 void MergeItems(bool merge_parent_manager, |
| 315 ItemVector* items) const; |
| 316 |
301 // Called on the FILE thread to check the existence of a downloaded file. | 317 // Called on the FILE thread to check the existence of a downloaded file. |
302 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); | 318 void CheckForFileRemovalOnFileThread(int64 id, const FilePath& path); |
303 | 319 |
304 // Called on the UI thread if the FILE thread detects the removal of | 320 // Called on the UI thread if the FILE thread detects the removal of |
305 // the downloaded file. The UI thread updates the state of the file | 321 // the downloaded file. The UI thread updates the state of the file |
306 // and then notifies this update to the file's observer. | 322 // and then notifies this update to the file's observer. |
307 void OnFileRemovalDetected(int64 db_handle); | 323 void OnFileRemovalDetected(int64 id); |
308 | 324 |
309 // Called on the download thread to check whether the suggested file path | 325 // Called on the download thread to check whether the suggested file path |
310 // exists. We don't check if the file exists on the UI thread to avoid UI | 326 // exists. We don't check if the file exists on the UI thread to avoid UI |
311 // stalls from interacting with the file system. | 327 // stalls from interacting with the file system. |
312 void CheckIfSuggestedPathExists(int32 download_id, | 328 void CheckIfSuggestedPathExists(int32 download_id, |
313 DownloadStateInfo state, | 329 DownloadStateInfo state, |
314 const FilePath& default_path); | 330 const FilePath& default_path); |
315 | 331 |
316 // Called on the UI thread once the DownloadManager has determined whether the | 332 // Called on the UI thread once the DownloadManager has determined whether the |
317 // suggested file path exists. | 333 // suggested file path exists. |
(...skipping 26 matching lines...) Expand all Loading... |
344 | 360 |
345 // Get the download item from the active map. Useful when the item is not | 361 // Get the download item from the active map. Useful when the item is not |
346 // yet in the history map. | 362 // yet in the history map. |
347 DownloadItem* GetActiveDownloadItem(int id); | 363 DownloadItem* GetActiveDownloadItem(int id); |
348 | 364 |
349 // Debugging routine to confirm relationship between below | 365 // Debugging routine to confirm relationship between below |
350 // containers; no-op if NDEBUG. | 366 // containers; no-op if NDEBUG. |
351 void AssertContainersConsistent() const; | 367 void AssertContainersConsistent() const; |
352 | 368 |
353 // Add a DownloadItem to history_downloads_. | 369 // Add a DownloadItem to history_downloads_. |
354 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); | 370 void AddDownloadItemToHistory(DownloadItem* item); |
355 | 371 |
356 // |downloads_| is the owning set for all downloads known to the | |
357 // DownloadManager. This includes downloads started by the user in | |
358 // this session, downloads initialized from the history system, and | |
359 // "save page as" downloads. All other DownloadItem containers in | |
360 // the DownloadManager are maps; they do not own the DownloadItems. | |
361 // Note that this is the only place (with any functional implications; | |
362 // see save_page_as_downloads_ below) that "save page as" downloads are | |
363 // kept, as the DownloadManager's only job is to hold onto those | |
364 // until destruction. | |
365 // | |
366 // |history_downloads_| is map of all downloads in this profile. The key | |
367 // is the handle returned by the history system, which is unique | |
368 // across sessions. | |
369 // | |
370 // |active_downloads_| is a map of all downloads that are currently being | |
371 // processed. The key is the ID assigned by the ResourceDispatcherHost, | |
372 // which is unique for the current session. | |
373 // | |
374 // |in_progress_| is a map of all downloads that are in progress and that have | |
375 // not yet received a valid history handle. The key is the ID assigned by the | |
376 // ResourceDispatcherHost, which is unique for the current session. | |
377 // | |
378 // |save_page_as_downloads_| (if defined) is a collection of all the | |
379 // downloads the "save page as" system has given to us to hold onto | |
380 // until we are destroyed. It is only used for debugging. | |
381 // | |
382 // When a download is created through a user action, the corresponding | |
383 // DownloadItem* is placed in |active_downloads_| and remains there until the | |
384 // download is in a terminal state (COMPLETE or CANCELLED). It is also | |
385 // placed in |in_progress_| and remains there until it has received a | |
386 // valid handle from the history system. Once it has a valid handle, the | |
387 // DownloadItem* is placed in the |history_downloads_| map. When the | |
388 // download reaches a terminal state, it is removed from |in_progress_|. | |
389 // Downloads from past sessions read from a persisted state from the | |
390 // history system are placed directly into |history_downloads_| since | |
391 // they have valid handles in the history system. | |
392 typedef std::set<DownloadItem*> DownloadSet; | 372 typedef std::set<DownloadItem*> DownloadSet; |
393 typedef base::hash_map<int64, DownloadItem*> DownloadMap; | 373 typedef base::hash_map<int64, DownloadItem*> DownloadMap; |
394 | 374 |
395 DownloadSet downloads_; | |
396 DownloadMap history_downloads_; | |
397 DownloadMap in_progress_; | |
398 DownloadMap active_downloads_; | |
399 #if !defined(NDEBUG) | 375 #if !defined(NDEBUG) |
400 DownloadSet save_page_as_downloads_; | 376 DownloadSet save_page_as_downloads_; |
401 #endif | 377 #endif |
402 | 378 |
| 379 // Map from id to DownloadItem. Owns the DownloadItem. |
| 380 DownloadMap downloads_; |
| 381 |
403 // True if the download manager has been initialized and requires a shutdown. | 382 // True if the download manager has been initialized and requires a shutdown. |
404 bool shutdown_needed_; | 383 bool shutdown_needed_; |
405 | 384 |
406 // Observers that want to be notified of changes to the set of downloads. | 385 // Observers that want to be notified of changes to the set of downloads. |
407 ObserverList<Observer> observers_; | 386 ObserverList<Observer> observers_; |
408 | 387 |
409 // The current active profile. | 388 // The current active profile. |
410 Profile* profile_; | 389 Profile* profile_; |
411 | 390 |
412 scoped_ptr<DownloadHistory> download_history_; | 391 scoped_ptr<DownloadHistory> download_history_; |
(...skipping 13 matching lines...) Expand all Loading... |
426 // The "Save As" dialog box used to ask the user where a file should be | 405 // The "Save As" dialog box used to ask the user where a file should be |
427 // saved. | 406 // saved. |
428 scoped_refptr<SelectFileDialog> select_file_dialog_; | 407 scoped_refptr<SelectFileDialog> select_file_dialog_; |
429 | 408 |
430 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; | 409 scoped_ptr<OtherDownloadManagerObserver> other_download_manager_observer_; |
431 | 410 |
432 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 411 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
433 }; | 412 }; |
434 | 413 |
435 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 414 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |