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