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