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 browser context in Chrome. | 8 // active browser context in Chrome. |
9 // | 9 // |
10 // Download observers: | 10 // Download observers: |
(...skipping 26 matching lines...) Expand all Loading... |
37 #include "base/callback.h" | 37 #include "base/callback.h" |
38 #include "base/file_path.h" | 38 #include "base/file_path.h" |
39 #include "base/gtest_prod_util.h" | 39 #include "base/gtest_prod_util.h" |
40 #include "base/hash_tables.h" | 40 #include "base/hash_tables.h" |
41 #include "base/memory/ref_counted.h" | 41 #include "base/memory/ref_counted.h" |
42 #include "base/memory/scoped_ptr.h" | 42 #include "base/memory/scoped_ptr.h" |
43 #include "base/memory/weak_ptr.h" | 43 #include "base/memory/weak_ptr.h" |
44 #include "base/observer_list.h" | 44 #include "base/observer_list.h" |
45 #include "base/synchronization/lock.h" | 45 #include "base/synchronization/lock.h" |
46 #include "base/time.h" | 46 #include "base/time.h" |
| 47 #include "content/browser/download/download_id.h" |
47 #include "content/browser/download/download_item.h" | 48 #include "content/browser/download/download_item.h" |
48 #include "content/browser/download/download_status_updater_delegate.h" | 49 #include "content/browser/download/download_status_updater_delegate.h" |
49 #include "content/browser/download/interrupt_reasons.h" | 50 #include "content/browser/download/interrupt_reasons.h" |
50 #include "content/common/content_export.h" | 51 #include "content/common/content_export.h" |
51 #include "content/public/browser/browser_thread.h" | 52 #include "content/public/browser/browser_thread.h" |
52 #include "net/base/net_errors.h" | 53 #include "net/base/net_errors.h" |
53 | 54 |
54 class DownloadFileManager; | 55 class DownloadFileManager; |
| 56 class DownloadIdFactory; |
55 class DownloadRequestHandle; | 57 class DownloadRequestHandle; |
56 class DownloadStatusUpdater; | 58 class DownloadStatusUpdater; |
57 class GURL; | 59 class GURL; |
58 class ResourceDispatcherHost; | 60 class ResourceDispatcherHost; |
59 class TabContents; | 61 class TabContents; |
60 struct DownloadCreateInfo; | 62 struct DownloadCreateInfo; |
61 struct DownloadSaveInfo; | 63 struct DownloadSaveInfo; |
62 | 64 |
63 namespace content { | 65 namespace content { |
64 class BrowserContext; | 66 class BrowserContext; |
65 class DownloadManagerDelegate; | 67 class DownloadManagerDelegate; |
66 } | 68 } |
67 | 69 |
68 // Browser's download manager: manages all downloads and destination view. | 70 // Browser's download manager: manages all downloads and destination view. |
69 class CONTENT_EXPORT DownloadManager | 71 class CONTENT_EXPORT DownloadManager |
70 : public base::RefCountedThreadSafe<DownloadManager, | 72 : public base::RefCountedThreadSafe<DownloadManager, |
71 BrowserThread::DeleteOnUIThread>, | 73 BrowserThread::DeleteOnUIThread>, |
72 public DownloadStatusUpdaterDelegate { | 74 public DownloadStatusUpdaterDelegate { |
73 public: | 75 public: |
74 DownloadManager(content::DownloadManagerDelegate* delegate, | 76 DownloadManager(content::DownloadManagerDelegate* delegate, |
| 77 DownloadIdFactory* id_factory, |
75 DownloadStatusUpdater* status_updater); | 78 DownloadStatusUpdater* status_updater); |
76 | 79 |
77 // Shutdown the download manager. Must be called before destruction. | 80 // Shutdown the download manager. Must be called before destruction. |
78 void Shutdown(); | 81 void Shutdown(); |
79 | 82 |
80 // Interface to implement for observers that wish to be informed of changes | 83 // Interface to implement for observers that wish to be informed of changes |
81 // to the DownloadManager's collection of downloads. | 84 // to the DownloadManager's collection of downloads. |
82 class CONTENT_EXPORT Observer { | 85 class CONTENT_EXPORT Observer { |
83 public: | 86 public: |
84 // New or deleted download, observers should query us for the current set | 87 // New or deleted download, observers should query us for the current set |
(...skipping 19 matching lines...) Expand all Loading... |
104 void GetTemporaryDownloads(const FilePath& dir_path, DownloadVector* result); | 107 void GetTemporaryDownloads(const FilePath& dir_path, DownloadVector* result); |
105 | 108 |
106 // Return all non-temporary downloads in the specified directory that are | 109 // Return all non-temporary downloads in the specified directory that are |
107 // are in progress or have completed. | 110 // are in progress or have completed. |
108 void GetAllDownloads(const FilePath& dir_path, DownloadVector* result); | 111 void GetAllDownloads(const FilePath& dir_path, DownloadVector* result); |
109 | 112 |
110 // Returns all non-temporary downloads matching |query|. Empty query matches | 113 // Returns all non-temporary downloads matching |query|. Empty query matches |
111 // everything. | 114 // everything. |
112 void SearchDownloads(const string16& query, DownloadVector* result); | 115 void SearchDownloads(const string16& query, DownloadVector* result); |
113 | 116 |
114 // Returns the next download id in a DownloadId and increments the counter. | |
115 // May be called on any thread. The incremented counter is not persisted, but | |
116 // the base counter for this accessor is initialized from the largest id | |
117 // actually saved to the download history database. | |
118 DownloadId GetNextId(); | |
119 | |
120 // Instead of passing a DownloadManager* between threads and hoping users only | |
121 // call GetNextId(), you can pass this thunk around instead. Pass the thunk | |
122 // around by const ref and store it by copy per the base::Callback interface. | |
123 // The thunk may be copied, including between threads. If you change | |
124 // GetNextIdThunkType from base::Callback, then you should think about how | |
125 // you're changing the ref-count of DownloadManager. Use it like: | |
126 // const DownloadManager::GetNextIdThunkType& next_id_thunk = | |
127 // download_manager->GetNextIdThunk(); | |
128 // id = next_id_thunk.Run(); | |
129 typedef base::Callback<DownloadId(void)> GetNextIdThunkType; | |
130 GetNextIdThunkType GetNextIdThunk(); | |
131 | |
132 // Returns true if initialized properly. | 117 // Returns true if initialized properly. |
133 bool Init(content::BrowserContext* browser_context); | 118 bool Init(content::BrowserContext* browser_context); |
134 | 119 |
135 // Notifications sent from the download thread to the UI thread | 120 // Notifications sent from the download thread to the UI thread |
136 void StartDownload(int32 id); | 121 void StartDownload(int32 id); |
137 void UpdateDownload(int32 download_id, int64 size); | 122 void UpdateDownload(int32 download_id, int64 size); |
138 | 123 |
139 // |download_id| is the ID of the download. | 124 // |download_id| is the ID of the download. |
140 // |size| is the number of bytes that have been downloaded. | 125 // |size| is the number of bytes that have been downloaded. |
141 // |hash| is sha256 hash for the downloaded file. It is empty when the hash | 126 // |hash| is sha256 hash for the downloaded file. It is empty when the hash |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 const std::string& referrer_encoding, | 197 const std::string& referrer_encoding, |
213 const DownloadSaveInfo& save_info, | 198 const DownloadSaveInfo& save_info, |
214 TabContents* tab_contents); | 199 TabContents* tab_contents); |
215 | 200 |
216 // Allow objects to observe the download creation process. | 201 // Allow objects to observe the download creation process. |
217 void AddObserver(Observer* observer); | 202 void AddObserver(Observer* observer); |
218 | 203 |
219 // Remove a download observer from ourself. | 204 // Remove a download observer from ourself. |
220 void RemoveObserver(Observer* observer); | 205 void RemoveObserver(Observer* observer); |
221 | 206 |
222 // Called by the embedder after creating the download manager to inform it of | |
223 // the next available download id. | |
224 // TODO(benjhayden): Separate this functionality out into a separate object. | |
225 void OnPersistentStoreGetNextId(int next_id); | |
226 | |
227 // Called by the embedder, after creating the download manager, to let it know | 207 // Called by the embedder, after creating the download manager, to let it know |
228 // about downloads from previous runs of the browser. | 208 // about downloads from previous runs of the browser. |
229 void OnPersistentStoreQueryComplete( | 209 void OnPersistentStoreQueryComplete( |
230 std::vector<DownloadPersistentStoreInfo>* entries); | 210 std::vector<DownloadPersistentStoreInfo>* entries); |
231 | 211 |
232 // Called by the embedder, in response to | 212 // Called by the embedder, in response to |
233 // DownloadManagerDelegate::AddItemToPersistentStore. | 213 // DownloadManagerDelegate::AddItemToPersistentStore. |
234 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); | 214 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); |
235 | 215 |
236 // Display a new download in the appropriate browser UI. | 216 // Display a new download in the appropriate browser UI. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 // Get the download item from the active map. Useful when the item is not | 277 // Get the download item from the active map. Useful when the item is not |
298 // yet in the history map. | 278 // yet in the history map. |
299 DownloadItem* GetActiveDownloadItem(int id); | 279 DownloadItem* GetActiveDownloadItem(int id); |
300 | 280 |
301 content::DownloadManagerDelegate* delegate() const { return delegate_; } | 281 content::DownloadManagerDelegate* delegate() const { return delegate_; } |
302 | 282 |
303 // For testing only. May be called from tests indirectly (through | 283 // For testing only. May be called from tests indirectly (through |
304 // other for testing only methods). | 284 // other for testing only methods). |
305 void SetDownloadManagerDelegate(content::DownloadManagerDelegate* delegate); | 285 void SetDownloadManagerDelegate(content::DownloadManagerDelegate* delegate); |
306 | 286 |
| 287 DownloadId GetNextId(); |
| 288 |
307 private: | 289 private: |
308 typedef std::set<DownloadItem*> DownloadSet; | 290 typedef std::set<DownloadItem*> DownloadSet; |
309 typedef base::hash_map<int64, DownloadItem*> DownloadMap; | 291 typedef base::hash_map<int64, DownloadItem*> DownloadMap; |
310 | 292 |
311 // For testing. | 293 // For testing. |
312 friend class DownloadManagerTest; | 294 friend class DownloadManagerTest; |
313 friend class DownloadTest; | 295 friend class DownloadTest; |
314 friend class MockDownloadManager; | 296 friend class MockDownloadManager; |
315 | 297 |
316 friend class base::RefCountedThreadSafe<DownloadManager, | 298 friend class base::RefCountedThreadSafe<DownloadManager, |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 | 393 |
412 // True if the download manager has been initialized and requires a shutdown. | 394 // True if the download manager has been initialized and requires a shutdown. |
413 bool shutdown_needed_; | 395 bool shutdown_needed_; |
414 | 396 |
415 // Observers that want to be notified of changes to the set of downloads. | 397 // Observers that want to be notified of changes to the set of downloads. |
416 ObserverList<Observer> observers_; | 398 ObserverList<Observer> observers_; |
417 | 399 |
418 // The current active browser context. | 400 // The current active browser context. |
419 content::BrowserContext* browser_context_; | 401 content::BrowserContext* browser_context_; |
420 | 402 |
421 base::Lock next_id_lock_; | |
422 int next_id_; | |
423 | |
424 // Non-owning pointer for handling file writing on the download_thread_. | 403 // Non-owning pointer for handling file writing on the download_thread_. |
425 DownloadFileManager* file_manager_; | 404 DownloadFileManager* file_manager_; |
426 | 405 |
427 // Non-owning pointer for updating the download status. | 406 // Non-owning pointer for updating the download status. |
428 base::WeakPtr<DownloadStatusUpdater> status_updater_; | 407 base::WeakPtr<DownloadStatusUpdater> status_updater_; |
429 | 408 |
430 // The user's last choice for download directory. This is only used when the | 409 // The user's last choice for download directory. This is only used when the |
431 // user wants us to prompt for a save location for each download. | 410 // user wants us to prompt for a save location for each download. |
432 FilePath last_download_path_; | 411 FilePath last_download_path_; |
433 | 412 |
434 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 413 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
435 content::DownloadManagerDelegate* delegate_; | 414 content::DownloadManagerDelegate* delegate_; |
436 | 415 |
| 416 DownloadIdFactory* id_factory_; |
| 417 |
437 // TODO(rdsmith): Remove when http://crbug.com/85408 is fixed. | 418 // TODO(rdsmith): Remove when http://crbug.com/85408 is fixed. |
438 // For debugging only. | 419 // For debugging only. |
439 int64 largest_db_handle_in_history_; | 420 int64 largest_db_handle_in_history_; |
440 | 421 |
441 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 422 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
442 }; | 423 }; |
443 | 424 |
444 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 425 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
OLD | NEW |