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