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