| 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 27 matching lines...) Expand all Loading... |
| 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/browser_thread.h" | 47 #include "content/browser/browser_thread.h" |
| 48 #include "content/browser/download/download_id.h" |
| 48 #include "content/browser/download/download_item.h" | 49 #include "content/browser/download/download_item.h" |
| 49 #include "content/browser/download/download_request_handle.h" | 50 #include "content/browser/download/download_request_handle.h" |
| 50 #include "content/browser/download/download_status_updater_delegate.h" | 51 #include "content/browser/download/download_status_updater_delegate.h" |
| 51 #include "content/browser/download/interrupt_reasons.h" | 52 #include "content/browser/download/interrupt_reasons.h" |
| 52 #include "content/common/content_export.h" | 53 #include "content/common/content_export.h" |
| 53 #include "net/base/net_errors.h" | 54 #include "net/base/net_errors.h" |
| 54 | 55 |
| 55 class DownloadFileManager; | 56 class DownloadFileManager; |
| 57 class DownloadIdFactory; |
| 56 class DownloadManagerDelegate; | 58 class DownloadManagerDelegate; |
| 57 class DownloadStatusUpdater; | 59 class DownloadStatusUpdater; |
| 58 class GURL; | 60 class GURL; |
| 59 class ResourceDispatcherHost; | 61 class ResourceDispatcherHost; |
| 60 class TabContents; | 62 class TabContents; |
| 61 struct DownloadCreateInfo; | 63 struct DownloadCreateInfo; |
| 62 struct DownloadSaveInfo; | 64 struct DownloadSaveInfo; |
| 63 | 65 |
| 64 namespace content { | 66 namespace content { |
| 65 class BrowserContext; | 67 class BrowserContext; |
| 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(DownloadManagerDelegate* delegate, | 76 DownloadManager(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(); | 117 DownloadId GetNextId(); |
| 119 | 118 |
| 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. | 119 // Returns true if initialized properly. |
| 133 bool Init(content::BrowserContext* browser_context); | 120 bool Init(content::BrowserContext* browser_context); |
| 134 | 121 |
| 135 // Notifications sent from the download thread to the UI thread | 122 // Notifications sent from the download thread to the UI thread |
| 136 void StartDownload(int32 id); | 123 void StartDownload(int32 id); |
| 137 void UpdateDownload(int32 download_id, int64 size); | 124 void UpdateDownload(int32 download_id, int64 size); |
| 138 | 125 |
| 139 // |download_id| is the ID of the download. | 126 // |download_id| is the ID of the download. |
| 140 // |size| is the number of bytes that have been downloaded. | 127 // |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 | 128 // |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, | 199 const std::string& referrer_encoding, |
| 213 const DownloadSaveInfo& save_info, | 200 const DownloadSaveInfo& save_info, |
| 214 TabContents* tab_contents); | 201 TabContents* tab_contents); |
| 215 | 202 |
| 216 // Allow objects to observe the download creation process. | 203 // Allow objects to observe the download creation process. |
| 217 void AddObserver(Observer* observer); | 204 void AddObserver(Observer* observer); |
| 218 | 205 |
| 219 // Remove a download observer from ourself. | 206 // Remove a download observer from ourself. |
| 220 void RemoveObserver(Observer* observer); | 207 void RemoveObserver(Observer* observer); |
| 221 | 208 |
| 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 | 209 // Called by the embedder, after creating the download manager, to let it know |
| 228 // about downloads from previous runs of the browser. | 210 // about downloads from previous runs of the browser. |
| 229 void OnPersistentStoreQueryComplete( | 211 void OnPersistentStoreQueryComplete( |
| 230 std::vector<DownloadPersistentStoreInfo>* entries); | 212 std::vector<DownloadPersistentStoreInfo>* entries); |
| 231 | 213 |
| 232 // Called by the embedder, in response to | 214 // Called by the embedder, in response to |
| 233 // DownloadManagerDelegate::AddItemToPersistentStore. | 215 // DownloadManagerDelegate::AddItemToPersistentStore. |
| 234 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); | 216 void OnItemAddedToPersistentStore(int32 download_id, int64 db_handle); |
| 235 | 217 |
| 236 // Display a new download in the appropriate browser UI. | 218 // Display a new download in the appropriate browser UI. |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 | 392 |
| 411 // True if the download manager has been initialized and requires a shutdown. | 393 // True if the download manager has been initialized and requires a shutdown. |
| 412 bool shutdown_needed_; | 394 bool shutdown_needed_; |
| 413 | 395 |
| 414 // Observers that want to be notified of changes to the set of downloads. | 396 // Observers that want to be notified of changes to the set of downloads. |
| 415 ObserverList<Observer> observers_; | 397 ObserverList<Observer> observers_; |
| 416 | 398 |
| 417 // The current active browser context. | 399 // The current active browser context. |
| 418 content::BrowserContext* browser_context_; | 400 content::BrowserContext* browser_context_; |
| 419 | 401 |
| 420 base::Lock next_id_lock_; | |
| 421 int next_id_; | |
| 422 | |
| 423 // Non-owning pointer for handling file writing on the download_thread_. | 402 // Non-owning pointer for handling file writing on the download_thread_. |
| 424 DownloadFileManager* file_manager_; | 403 DownloadFileManager* file_manager_; |
| 425 | 404 |
| 426 // Non-owning pointer for updating the download status. | 405 // Non-owning pointer for updating the download status. |
| 427 base::WeakPtr<DownloadStatusUpdater> status_updater_; | 406 base::WeakPtr<DownloadStatusUpdater> status_updater_; |
| 428 | 407 |
| 429 // The user's last choice for download directory. This is only used when the | 408 // The user's last choice for download directory. This is only used when the |
| 430 // user wants us to prompt for a save location for each download. | 409 // user wants us to prompt for a save location for each download. |
| 431 FilePath last_download_path_; | 410 FilePath last_download_path_; |
| 432 | 411 |
| 433 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 412 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
| 434 DownloadManagerDelegate* delegate_; | 413 DownloadManagerDelegate* delegate_; |
| 435 | 414 |
| 415 DownloadIdFactory* id_factory_; |
| 416 |
| 436 // TODO(rdsmith): Remove when http://crbug.com/85408 is fixed. | 417 // TODO(rdsmith): Remove when http://crbug.com/85408 is fixed. |
| 437 // For debugging only. | 418 // For debugging only. |
| 438 int64 largest_db_handle_in_history_; | 419 int64 largest_db_handle_in_history_; |
| 439 | 420 |
| 440 DISALLOW_COPY_AND_ASSIGN(DownloadManager); | 421 DISALLOW_COPY_AND_ASSIGN(DownloadManager); |
| 441 }; | 422 }; |
| 442 | 423 |
| 443 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ | 424 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_H_ |
| OLD | NEW |