OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
16 #include "base/sequenced_task_runner_helpers.h" | 16 #include "base/sequenced_task_runner_helpers.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "content/browser/download/download_item_factory.h" | |
19 #include "content/browser/download/download_item_impl_delegate.h" | 18 #include "content/browser/download/download_item_impl_delegate.h" |
20 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
21 #include "content/public/browser/download_manager.h" | 20 #include "content/public/browser/download_manager.h" |
22 | 21 |
23 class DownloadFileManager; | 22 class DownloadFileManager; |
24 class DownloadItemImpl; | 23 class DownloadItemImpl; |
25 | 24 |
| 25 namespace content { |
| 26 class DownloadItemFactory; |
| 27 class DownloadFileFactory; |
| 28 } |
| 29 |
| 30 namespace net { |
| 31 class BoundNetLog; |
| 32 } |
| 33 |
26 class CONTENT_EXPORT DownloadManagerImpl | 34 class CONTENT_EXPORT DownloadManagerImpl |
27 : public content::DownloadManager, | 35 : public content::DownloadManager, |
28 private DownloadItemImplDelegate { | 36 private DownloadItemImplDelegate { |
29 public: | 37 public: |
30 // Caller guarantees that |file_manager| and |net_log| will remain valid | 38 // Caller guarantees that |net_log| will remain valid |
31 // for the lifetime of DownloadManagerImpl (until Shutdown() is called). | 39 // for the lifetime of DownloadManagerImpl (until Shutdown() is called). |
32 // |factory| may be a default constructed (null) scoped_ptr; if so, | 40 // |factory| may be a default constructed (null) scoped_ptr; if so, |
33 // the DownloadManagerImpl creates and takes ownership of the | 41 // the DownloadManagerImpl creates and takes ownership of the |
34 // default DownloadItemFactory. | 42 // default DownloadItemFactory. |
35 DownloadManagerImpl(DownloadFileManager* file_manager, | 43 DownloadManagerImpl(scoped_ptr<content::DownloadItemFactory> item_factory, |
36 scoped_ptr<content::DownloadItemFactory> factory, | 44 scoped_ptr<content::DownloadFileFactory> file_factory, |
37 net::NetLog* net_log); | 45 net::NetLog* net_log); |
38 | 46 |
39 // Implementation functions (not part of the DownloadManager interface). | 47 // Implementation functions (not part of the DownloadManager interface). |
40 | 48 |
41 // Creates a download item for the SavePackage system. | 49 // Creates a download item for the SavePackage system. |
42 // Must be called on the UI thread. Note that the DownloadManager | 50 // Must be called on the UI thread. Note that the DownloadManager |
43 // retains ownership. | 51 // retains ownership. |
44 virtual DownloadItemImpl* CreateSavePackageDownloadItem( | 52 virtual DownloadItemImpl* CreateSavePackageDownloadItem( |
45 const FilePath& main_file_path, | 53 const FilePath& main_file_path, |
46 const GURL& page_url, | 54 const GURL& page_url, |
47 const std::string& mime_type, | 55 const std::string& mime_type, |
48 content::DownloadItem::Observer* observer); | 56 content::DownloadItem::Observer* observer); |
49 | 57 |
50 // content::DownloadManager functions. | 58 // content::DownloadManager functions. |
51 virtual void SetDelegate(content::DownloadManagerDelegate* delegate) OVERRIDE; | 59 virtual void SetDelegate(content::DownloadManagerDelegate* delegate) OVERRIDE; |
52 virtual content::DownloadManagerDelegate* GetDelegate() const OVERRIDE; | 60 virtual content::DownloadManagerDelegate* GetDelegate() const OVERRIDE; |
53 virtual void Shutdown() OVERRIDE; | 61 virtual void Shutdown() OVERRIDE; |
54 virtual void GetTemporaryDownloads(const FilePath& dir_path, | 62 virtual void GetTemporaryDownloads(const FilePath& dir_path, |
55 DownloadVector* result) OVERRIDE; | 63 DownloadVector* result) OVERRIDE; |
56 virtual void GetAllDownloads(const FilePath& dir_path, | 64 virtual void GetAllDownloads(const FilePath& dir_path, |
57 DownloadVector* result) OVERRIDE; | 65 DownloadVector* result) OVERRIDE; |
58 virtual void SearchDownloads(const string16& query, | 66 virtual void SearchDownloads(const string16& query, |
59 DownloadVector* result) OVERRIDE; | 67 DownloadVector* result) OVERRIDE; |
60 virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; | 68 virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; |
61 virtual content::DownloadId StartDownload( | 69 virtual content::DownloadId StartDownload( |
62 scoped_ptr<DownloadCreateInfo> info, | 70 scoped_ptr<DownloadCreateInfo> info, |
63 scoped_ptr<content::ByteStreamReader> stream) OVERRIDE; | 71 scoped_ptr<content::ByteStreamReader> stream) OVERRIDE; |
64 virtual void UpdateDownload(int32 download_id, | |
65 int64 bytes_so_far, | |
66 int64 bytes_per_sec, | |
67 const std::string& hash_state) OVERRIDE; | |
68 virtual void OnResponseCompleted(int32 download_id, int64 size, | |
69 const std::string& hash) OVERRIDE; | |
70 virtual void CancelDownload(int32 download_id) OVERRIDE; | 72 virtual void CancelDownload(int32 download_id) OVERRIDE; |
71 virtual void OnDownloadInterrupted( | |
72 int32 download_id, | |
73 content::DownloadInterruptReason reason) OVERRIDE; | |
74 virtual int RemoveDownloadsBetween(base::Time remove_begin, | 73 virtual int RemoveDownloadsBetween(base::Time remove_begin, |
75 base::Time remove_end) OVERRIDE; | 74 base::Time remove_end) OVERRIDE; |
76 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; | 75 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; |
77 virtual int RemoveAllDownloads() OVERRIDE; | 76 virtual int RemoveAllDownloads() OVERRIDE; |
78 virtual void DownloadUrl( | 77 virtual void DownloadUrl( |
79 scoped_ptr<content::DownloadUrlParameters> params) OVERRIDE; | 78 scoped_ptr<content::DownloadUrlParameters> params) OVERRIDE; |
80 virtual void AddObserver(Observer* observer) OVERRIDE; | 79 virtual void AddObserver(Observer* observer) OVERRIDE; |
81 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 80 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
82 virtual void OnPersistentStoreQueryComplete( | 81 virtual void OnPersistentStoreQueryComplete( |
83 std::vector<content::DownloadPersistentStoreInfo>* entries) OVERRIDE; | 82 std::vector<content::DownloadPersistentStoreInfo>* entries) OVERRIDE; |
84 virtual void OnItemAddedToPersistentStore(int32 download_id, | 83 virtual void OnItemAddedToPersistentStore(int32 download_id, |
85 int64 db_handle) OVERRIDE; | 84 int64 db_handle) OVERRIDE; |
86 virtual int InProgressCount() const OVERRIDE; | 85 virtual int InProgressCount() const OVERRIDE; |
87 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; | 86 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; |
88 virtual void CheckForHistoryFilesRemoval() OVERRIDE; | 87 virtual void CheckForHistoryFilesRemoval() OVERRIDE; |
89 virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE; | 88 virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE; |
90 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; | 89 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; |
91 virtual void SavePageDownloadFinished( | 90 virtual void SavePageDownloadFinished( |
92 content::DownloadItem* download) OVERRIDE; | 91 content::DownloadItem* download) OVERRIDE; |
93 virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; | 92 virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; |
94 virtual bool GenerateFileHash() OVERRIDE; | 93 virtual bool GenerateFileHash() OVERRIDE; |
95 | 94 |
| 95 // For testing; specifically, accessed from TestFileErrorInjector. |
| 96 virtual void SetDownloadFileFactoryForTesting( |
| 97 scoped_ptr<content::DownloadFileFactory> file_factory); |
| 98 virtual content::DownloadFileFactory* GetDownloadFileFactoryForTesting(); |
| 99 |
96 private: | 100 private: |
97 typedef std::set<content::DownloadItem*> DownloadSet; | 101 typedef std::set<content::DownloadItem*> DownloadSet; |
98 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; | 102 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; |
99 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; | 103 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; |
100 | 104 |
101 // For testing. | 105 // For testing. |
102 friend class DownloadManagerTest; | 106 friend class DownloadManagerTest; |
103 friend class DownloadTest; | 107 friend class DownloadTest; |
104 | 108 |
105 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; | 109 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; |
106 | 110 |
107 virtual ~DownloadManagerImpl(); | 111 virtual ~DownloadManagerImpl(); |
108 | 112 |
109 // Creates the download item. Must be called on the UI thread. | 113 // Creates the download item. Must be called on the UI thread. |
110 // Returns the |BoundNetLog| used by the |DownloadItem|. | 114 virtual DownloadItemImpl* CreateDownloadItem( |
111 virtual net::BoundNetLog CreateDownloadItem(DownloadCreateInfo* info); | 115 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log); |
112 | 116 |
113 // Does nothing if |download_id| is not an active download. | 117 // Does nothing if |download_id| is not an active download. |
114 void MaybeCompleteDownloadById(int download_id); | 118 void MaybeCompleteDownloadById(int download_id); |
115 | 119 |
116 // Determine if the download is ready for completion, i.e. has had | 120 // Determine if the download is ready for completion, i.e. has had |
117 // all data saved, and completed the filename determination and | 121 // all data saved, and completed the filename determination and |
118 // history insertion. | 122 // history insertion. |
119 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); | 123 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); |
120 | 124 |
121 // Show the download in the browser. | 125 // Show the download in the browser. |
(...skipping 21 matching lines...) Expand all Loading... |
143 // Debugging routine to confirm relationship between below | 147 // Debugging routine to confirm relationship between below |
144 // containers; no-op if NDEBUG. | 148 // containers; no-op if NDEBUG. |
145 void AssertContainersConsistent() const; | 149 void AssertContainersConsistent() const; |
146 | 150 |
147 // Add a DownloadItem to history_downloads_. | 151 // Add a DownloadItem to history_downloads_. |
148 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); | 152 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); |
149 | 153 |
150 // Remove from internal maps. | 154 // Remove from internal maps. |
151 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); | 155 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); |
152 | 156 |
153 // Called in response to our request to the DownloadFileManager to | |
154 // create a DownloadFile. A |reason| of | |
155 // content::DOWNLOAD_INTERRUPT_REASON_NONE indicates success. | |
156 void OnDownloadFileCreated( | |
157 int32 download_id, content::DownloadInterruptReason reason); | |
158 | |
159 // Called when the delegate has completed determining the download target. | 157 // Called when the delegate has completed determining the download target. |
160 // Arguments following |download_id| are as per | 158 // Arguments following |download_id| are as per |
161 // content::DownloadTargetCallback. | 159 // content::DownloadTargetCallback. |
162 void OnDownloadTargetDetermined( | 160 void OnDownloadTargetDetermined( |
163 int32 download_id, | 161 int32 download_id, |
164 const FilePath& target_path, | 162 const FilePath& target_path, |
165 content::DownloadItem::TargetDisposition disposition, | 163 content::DownloadItem::TargetDisposition disposition, |
166 content::DownloadDangerType danger_type, | 164 content::DownloadDangerType danger_type, |
167 const FilePath& intermediate_path); | 165 const FilePath& intermediate_path); |
168 | 166 |
169 // Called when a download entry is committed to the persistent store. | 167 // Called when a download entry is committed to the persistent store. |
170 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); | 168 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); |
171 | 169 |
172 // Called when Save Page As entry is committed to the persistent store. | 170 // Called when Save Page As entry is committed to the persistent store. |
173 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); | 171 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); |
174 | 172 |
175 // Overridden from DownloadItemImplDelegate | 173 // Overridden from DownloadItemImplDelegate |
176 // (Note that |GetBrowserContext| are present in both interfaces.) | 174 // (Note that |GetBrowserContext| are present in both interfaces.) |
177 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE; | 175 virtual void DelegateStart(DownloadItemImpl* item) OVERRIDE; |
178 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; | 176 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; |
179 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; | 177 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; |
180 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; | 178 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; |
181 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; | 179 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; |
182 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; | 180 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; |
183 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; | 181 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; |
184 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; | 182 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; |
185 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; | 183 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; |
186 virtual void DownloadRenamedToIntermediateName( | 184 virtual void DownloadRenamedToIntermediateName( |
187 DownloadItemImpl* download) OVERRIDE; | 185 DownloadItemImpl* download) OVERRIDE; |
188 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; | 186 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; |
189 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; | 187 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; |
190 | 188 |
191 // Factory for creation of downloads items. | 189 // Factory for creation of downloads items. |
192 scoped_ptr<content::DownloadItemFactory> factory_; | 190 scoped_ptr<content::DownloadItemFactory> item_factory_; |
| 191 |
| 192 // Factory for the creation of download files. |
| 193 scoped_ptr<content::DownloadFileFactory> file_factory_; |
193 | 194 |
194 // |downloads_| is the owning set for all downloads known to the | 195 // |downloads_| is the owning set for all downloads known to the |
195 // DownloadManager. This includes downloads started by the user in | 196 // DownloadManager. This includes downloads started by the user in |
196 // this session, downloads initialized from the history system, and | 197 // this session, downloads initialized from the history system, and |
197 // "save page as" downloads. All other DownloadItem containers in | 198 // "save page as" downloads. All other DownloadItem containers in |
198 // the DownloadManager are maps; they do not own the DownloadItems. | 199 // the DownloadManager are maps; they do not own the DownloadItems. |
199 // Note that this is the only place (with any functional implications; | 200 // Note that this is the only place (with any functional implications; |
200 // see save_page_downloads_ below) that "save page as" downloads are | 201 // see save_page_downloads_ below) that "save page as" downloads are |
201 // kept, as the DownloadManager's only job is to hold onto those | 202 // kept, as the DownloadManager's only job is to hold onto those |
202 // until destruction. | 203 // until destruction. |
203 // | 204 // |
204 // |active_downloads_| is a map of all downloads that are currently being | 205 // |active_downloads_| is a map of all downloads that are currently being |
205 // processed. The key is the ID assigned by the DownloadFileManager, | 206 // processed. |
206 // which is unique for the current session. | |
207 // | 207 // |
208 // When a download is created through a user action, the corresponding | 208 // When a download is created through a user action, the corresponding |
209 // DownloadItem* is placed in |active_downloads_| and remains there until the | 209 // DownloadItem* is placed in |active_downloads_| and remains there until the |
210 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a | 210 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a |
211 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. | 211 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. |
212 // Downloads from past sessions read from a persisted state from the history | 212 // Downloads from past sessions read from a persisted state from the history |
213 // system are placed directly into |history_downloads_| since they have valid | 213 // system are placed directly into |history_downloads_| since they have valid |
214 // handles in the history system. | 214 // handles in the history system. |
215 | 215 |
216 DownloadMap downloads_; | 216 DownloadMap downloads_; |
217 DownloadMap active_downloads_; | 217 DownloadMap active_downloads_; |
218 | 218 |
219 int history_size_; | 219 int history_size_; |
220 | 220 |
221 // True if the download manager has been initialized and requires a shutdown. | 221 // True if the download manager has been initialized and requires a shutdown. |
222 bool shutdown_needed_; | 222 bool shutdown_needed_; |
223 | 223 |
224 // Observers that want to be notified of changes to the set of downloads. | 224 // Observers that want to be notified of changes to the set of downloads. |
225 ObserverList<Observer> observers_; | 225 ObserverList<Observer> observers_; |
226 | 226 |
227 // The current active browser context. | 227 // The current active browser context. |
228 content::BrowserContext* browser_context_; | 228 content::BrowserContext* browser_context_; |
229 | 229 |
230 // Non-owning pointer for handling file writing on the download_thread_. | |
231 DownloadFileManager* file_manager_; | |
232 | |
233 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 230 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
234 content::DownloadManagerDelegate* delegate_; | 231 content::DownloadManagerDelegate* delegate_; |
235 | 232 |
236 net::NetLog* net_log_; | 233 net::NetLog* net_log_; |
237 | 234 |
238 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); | 235 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); |
239 }; | 236 }; |
240 | 237 |
241 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 238 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
OLD | NEW |