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