OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 // |
| 5 |
| 6 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| 7 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| 8 #pragma once |
| 9 |
| 10 #include "content/browser/download/download_manager.h" |
| 11 |
| 12 #include <map> |
| 13 #include <set> |
| 14 |
| 15 #include "base/hash_tables.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/observer_list.h" |
| 20 #include "base/synchronization/lock.h" |
| 21 #include "content/browser/download/download_status_updater_delegate.h" |
| 22 #include "content/common/content_export.h" |
| 23 |
| 24 class DownloadIdFactory; |
| 25 class DownloadStatusUpdater; |
| 26 |
| 27 class CONTENT_EXPORT DownloadManagerImpl |
| 28 : public DownloadManager, |
| 29 public DownloadStatusUpdaterDelegate { |
| 30 public: |
| 31 DownloadManagerImpl(content::DownloadManagerDelegate* delegate, |
| 32 DownloadIdFactory* id_factory, |
| 33 DownloadStatusUpdater* status_updater); |
| 34 |
| 35 // DownloadManager functions. |
| 36 virtual void Shutdown() OVERRIDE; |
| 37 virtual void GetTemporaryDownloads(const FilePath& dir_path, |
| 38 DownloadVector* result) OVERRIDE; |
| 39 virtual void GetAllDownloads(const FilePath& dir_path, |
| 40 DownloadVector* result) OVERRIDE; |
| 41 virtual void SearchDownloads(const string16& query, |
| 42 DownloadVector* result) OVERRIDE; |
| 43 virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; |
| 44 virtual void StartDownload(int32 id) OVERRIDE; |
| 45 virtual void UpdateDownload(int32 download_id, int64 size) OVERRIDE; |
| 46 virtual void OnResponseCompleted(int32 download_id, int64 size, |
| 47 const std::string& hash) OVERRIDE; |
| 48 virtual void CancelDownload(int32 download_id) OVERRIDE; |
| 49 virtual void OnDownloadInterrupted(int32 download_id, int64 size, |
| 50 InterruptReason reason) OVERRIDE; |
| 51 virtual void DownloadCancelledInternal(DownloadItem* download) OVERRIDE; |
| 52 virtual void RemoveDownload(int64 download_handle) OVERRIDE; |
| 53 virtual bool IsDownloadReadyForCompletion(DownloadItem* download) OVERRIDE; |
| 54 virtual void MaybeCompleteDownload(DownloadItem* download) OVERRIDE; |
| 55 virtual void OnDownloadRenamedToFinalName(int download_id, |
| 56 const FilePath& full_path, |
| 57 int uniquifier) OVERRIDE; |
| 58 virtual int RemoveDownloadsBetween(const base::Time remove_begin, |
| 59 const base::Time remove_end) OVERRIDE; |
| 60 virtual int RemoveDownloads(const base::Time remove_begin) OVERRIDE; |
| 61 virtual int RemoveAllDownloads() OVERRIDE; |
| 62 virtual void DownloadCompleted(int32 download_id) OVERRIDE; |
| 63 virtual void DownloadUrl(const GURL& url, |
| 64 const GURL& referrer, |
| 65 const std::string& referrer_encoding, |
| 66 TabContents* tab_contents) OVERRIDE; |
| 67 virtual void DownloadUrlToFile(const GURL& url, |
| 68 const GURL& referrer, |
| 69 const std::string& referrer_encoding, |
| 70 const DownloadSaveInfo& save_info, |
| 71 TabContents* tab_contents) OVERRIDE; |
| 72 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 73 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 74 virtual void OnPersistentStoreQueryComplete( |
| 75 std::vector<DownloadPersistentStoreInfo>* entries) OVERRIDE; |
| 76 virtual void OnItemAddedToPersistentStore(int32 download_id, |
| 77 int64 db_handle) OVERRIDE; |
| 78 virtual void ShowDownloadInBrowser(DownloadItem* download) OVERRIDE; |
| 79 virtual int InProgressCount() const OVERRIDE; |
| 80 virtual content::BrowserContext* BrowserContext() OVERRIDE; |
| 81 virtual FilePath LastDownloadPath() OVERRIDE; |
| 82 virtual void CreateDownloadItem( |
| 83 DownloadCreateInfo* info, |
| 84 const DownloadRequestHandle& request_handle) OVERRIDE; |
| 85 virtual void ClearLastDownloadPath() OVERRIDE; |
| 86 virtual void FileSelected(const FilePath& path, void* params) OVERRIDE; |
| 87 virtual void FileSelectionCanceled(void* params) OVERRIDE; |
| 88 virtual void RestartDownload(int32 download_id) OVERRIDE; |
| 89 virtual void MarkDownloadOpened(DownloadItem* download) OVERRIDE; |
| 90 virtual void CheckForHistoryFilesRemoval() OVERRIDE; |
| 91 virtual void CheckForFileRemoval(DownloadItem* download_item) OVERRIDE; |
| 92 virtual void AssertQueueStateConsistent(DownloadItem* download) OVERRIDE; |
| 93 virtual DownloadItem* GetDownloadItem(int id) OVERRIDE; |
| 94 virtual void SavePageDownloadStarted(DownloadItem* download) OVERRIDE; |
| 95 virtual void SavePageDownloadFinished(DownloadItem* download) OVERRIDE; |
| 96 virtual DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; |
| 97 virtual content::DownloadManagerDelegate* delegate() const OVERRIDE; |
| 98 virtual void SetDownloadManagerDelegate( |
| 99 content::DownloadManagerDelegate* delegate) OVERRIDE; |
| 100 virtual DownloadId GetNextId() OVERRIDE; |
| 101 |
| 102 // Overridden from DownloadStatusUpdaterDelegate: |
| 103 virtual bool IsDownloadProgressKnown() const OVERRIDE; |
| 104 virtual int64 GetInProgressDownloadCount() const OVERRIDE; |
| 105 virtual int64 GetReceivedDownloadBytes() const OVERRIDE; |
| 106 virtual int64 GetTotalDownloadBytes() const OVERRIDE; |
| 107 |
| 108 private: |
| 109 typedef std::set<DownloadItem*> DownloadSet; |
| 110 typedef base::hash_map<int64, DownloadItem*> DownloadMap; |
| 111 |
| 112 // For testing. |
| 113 friend class DownloadManagerTest; |
| 114 friend class DownloadTest; |
| 115 friend class MockDownloadManager; |
| 116 |
| 117 friend class base::RefCountedThreadSafe< |
| 118 DownloadManagerImpl, content::BrowserThread::DeleteOnUIThread>; |
| 119 friend struct content::BrowserThread::DeleteOnThread< |
| 120 content::BrowserThread::UI>; |
| 121 friend class DeleteTask<DownloadManagerImpl>; |
| 122 |
| 123 virtual ~DownloadManagerImpl(); |
| 124 |
| 125 // Called on the FILE thread to check the existence of a downloaded file. |
| 126 void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path); |
| 127 |
| 128 // Called on the UI thread if the FILE thread detects the removal of |
| 129 // the downloaded file. The UI thread updates the state of the file |
| 130 // and then notifies this update to the file's observer. |
| 131 void OnFileRemovalDetected(int64 db_handle); |
| 132 |
| 133 // Called back after a target path for the file to be downloaded to has been |
| 134 // determined, either automatically based on the suggested file name, or by |
| 135 // the user in a Save As dialog box. |
| 136 virtual void ContinueDownloadWithPath(DownloadItem* download, |
| 137 const FilePath& chosen_file) OVERRIDE; |
| 138 |
| 139 // Retrieves the download from the |download_id|. |
| 140 // Returns NULL if the download is not active. |
| 141 virtual DownloadItem* GetActiveDownload(int32 download_id) OVERRIDE; |
| 142 |
| 143 // Removes |download| from the active and in progress maps. |
| 144 // Called when the download is cancelled or has an error. |
| 145 // Does nothing if the download is not in the history DB. |
| 146 void RemoveFromActiveList(DownloadItem* download); |
| 147 |
| 148 // Updates the delegate about the overall download progress. |
| 149 void UpdateDownloadProgress(); |
| 150 |
| 151 // Inform observers that the model has changed. |
| 152 void NotifyModelChanged(); |
| 153 |
| 154 // Debugging routine to confirm relationship between below |
| 155 // containers; no-op if NDEBUG. |
| 156 void AssertContainersConsistent() const; |
| 157 |
| 158 // Add a DownloadItem to history_downloads_. |
| 159 void AddDownloadItemToHistory(DownloadItem* item, int64 db_handle); |
| 160 |
| 161 // Remove from internal maps. |
| 162 int RemoveDownloadItems(const DownloadVector& pending_deletes); |
| 163 |
| 164 // Called when a download entry is committed to the persistent store. |
| 165 void OnDownloadItemAddedToPersistentStore(int32 download_id, int64 db_handle); |
| 166 |
| 167 // Called when Save Page As entry is committed to the persistent store. |
| 168 void OnSavePageItemAddedToPersistentStore(int32 download_id, int64 db_handle); |
| 169 |
| 170 // For unit tests only. |
| 171 virtual void SetFileManager(DownloadFileManager* file_manager) OVERRIDE; |
| 172 |
| 173 // |downloads_| is the owning set for all downloads known to the |
| 174 // DownloadManager. This includes downloads started by the user in |
| 175 // this session, downloads initialized from the history system, and |
| 176 // "save page as" downloads. All other DownloadItem containers in |
| 177 // the DownloadManager are maps; they do not own the DownloadItems. |
| 178 // Note that this is the only place (with any functional implications; |
| 179 // see save_page_downloads_ below) that "save page as" downloads are |
| 180 // kept, as the DownloadManager's only job is to hold onto those |
| 181 // until destruction. |
| 182 // |
| 183 // |history_downloads_| is map of all downloads in this browser context. The |
| 184 // key is the handle returned by the history system, which is unique across |
| 185 // sessions. |
| 186 // |
| 187 // |active_downloads_| is a map of all downloads that are currently being |
| 188 // processed. The key is the ID assigned by the DownloadFileManager, |
| 189 // which is unique for the current session. |
| 190 // |
| 191 // |in_progress_| is a map of all downloads that are in progress and that have |
| 192 // not yet received a valid history handle. The key is the ID assigned by the |
| 193 // DownloadFileManager, which is unique for the current session. |
| 194 // |
| 195 // |save_page_downloads_| (if defined) is a collection of all the |
| 196 // downloads the "save page as" system has given to us to hold onto |
| 197 // until we are destroyed. They key is DownloadFileManager, so it is unique |
| 198 // compared to download item. It is only used for debugging. |
| 199 // |
| 200 // When a download is created through a user action, the corresponding |
| 201 // DownloadItem* is placed in |active_downloads_| and remains there until the |
| 202 // download is in a terminal state (COMPLETE or CANCELLED). It is also |
| 203 // placed in |in_progress_| and remains there until it has received a |
| 204 // valid handle from the history system. Once it has a valid handle, the |
| 205 // DownloadItem* is placed in the |history_downloads_| map. When the |
| 206 // download reaches a terminal state, it is removed from |in_progress_|. |
| 207 // Downloads from past sessions read from a persisted state from the |
| 208 // history system are placed directly into |history_downloads_| since |
| 209 // they have valid handles in the history system. |
| 210 |
| 211 DownloadSet downloads_; |
| 212 DownloadMap history_downloads_; |
| 213 DownloadMap in_progress_; |
| 214 DownloadMap active_downloads_; |
| 215 DownloadMap save_page_downloads_; |
| 216 |
| 217 // True if the download manager has been initialized and requires a shutdown. |
| 218 bool shutdown_needed_; |
| 219 |
| 220 // Observers that want to be notified of changes to the set of downloads. |
| 221 ObserverList<Observer> observers_; |
| 222 |
| 223 // The current active browser context. |
| 224 content::BrowserContext* browser_context_; |
| 225 |
| 226 // Non-owning pointer for handling file writing on the download_thread_. |
| 227 DownloadFileManager* file_manager_; |
| 228 |
| 229 // Non-owning pointer for updating the download status. |
| 230 base::WeakPtr<DownloadStatusUpdater> status_updater_; |
| 231 |
| 232 // The user's last choice for download directory. This is only used when the |
| 233 // user wants us to prompt for a save location for each download. |
| 234 FilePath last_download_path_; |
| 235 |
| 236 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
| 237 content::DownloadManagerDelegate* delegate_; |
| 238 |
| 239 DownloadIdFactory* id_factory_; |
| 240 |
| 241 // TODO(rdsmith): Remove when http://crbug.com/85408 is fixed. |
| 242 // For debugging only. |
| 243 int64 largest_db_handle_in_history_; |
| 244 |
| 245 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); |
| 246 }; |
| 247 |
| 248 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
OLD | NEW |