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