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> 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 FilePath LastDownloadPath() OVERRIDE; | 87 virtual FilePath LastDownloadPath() OVERRIDE; |
90 virtual void ClearLastDownloadPath() OVERRIDE; | 88 virtual void ClearLastDownloadPath() OVERRIDE; |
91 virtual void FileSelected(const FilePath& path, int32 download_id) OVERRIDE; | 89 virtual void FileSelected(const FilePath& path, int32 download_id) OVERRIDE; |
92 virtual void FileSelectionCanceled(int32 download_id) OVERRIDE; | 90 virtual void FileSelectionCanceled(int32 download_id) OVERRIDE; |
93 virtual void RestartDownload(int32 download_id) OVERRIDE; | 91 virtual void RestartDownload(int32 download_id) OVERRIDE; |
94 virtual void CheckForHistoryFilesRemoval() OVERRIDE; | 92 virtual void CheckForHistoryFilesRemoval() OVERRIDE; |
95 virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE; | 93 virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE; |
96 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; | 94 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; |
97 virtual void SavePageDownloadFinished( | 95 virtual void SavePageDownloadFinished( |
98 content::DownloadItem* download) OVERRIDE; | 96 content::DownloadItem* download) OVERRIDE; |
99 virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; | 97 virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE; |
100 virtual bool GenerateFileHash() OVERRIDE; | 98 virtual bool GenerateFileHash() OVERRIDE; |
101 | 99 |
| 100 // For testing; specifically, accessed from TestFileErrorInjector. |
| 101 virtual void SetDownloadFileFactoryForTesting( |
| 102 scoped_ptr<content::DownloadFileFactory> file_factory); |
| 103 virtual content::DownloadFileFactory* GetDownloadFileFactory(); |
| 104 |
102 private: | 105 private: |
103 typedef std::set<content::DownloadItem*> DownloadSet; | 106 typedef std::set<content::DownloadItem*> DownloadSet; |
104 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; | 107 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; |
105 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; | 108 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; |
106 | 109 |
107 // For testing. | 110 // For testing. |
108 friend class DownloadManagerTest; | 111 friend class DownloadManagerTest; |
109 friend class DownloadTest; | 112 friend class DownloadTest; |
110 | 113 |
111 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; | 114 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; |
112 | 115 |
113 virtual ~DownloadManagerImpl(); | 116 virtual ~DownloadManagerImpl(); |
114 | 117 |
115 // Creates the download item. Must be called on the UI thread. | 118 // Creates the download item. Must be called on the UI thread. |
116 // Returns the |BoundNetLog| used by the |DownloadItem|. | 119 virtual DownloadItemImpl* CreateDownloadItem( |
117 virtual net::BoundNetLog CreateDownloadItem(DownloadCreateInfo* info); | 120 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log); |
118 | 121 |
119 // Does nothing if |download_id| is not an active download. | 122 // Does nothing if |download_id| is not an active download. |
120 void MaybeCompleteDownloadById(int download_id); | 123 void MaybeCompleteDownloadById(int download_id); |
121 | 124 |
122 // Determine if the download is ready for completion, i.e. has had | 125 // Determine if the download is ready for completion, i.e. has had |
123 // all data saved, and completed the filename determination and | 126 // all data saved, and completed the filename determination and |
124 // history insertion. | 127 // history insertion. |
125 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); | 128 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); |
126 | 129 |
127 // Show the download in the browser. | 130 // Show the download in the browser. |
(...skipping 26 matching lines...) Expand all Loading... |
154 // Debugging routine to confirm relationship between below | 157 // Debugging routine to confirm relationship between below |
155 // containers; no-op if NDEBUG. | 158 // containers; no-op if NDEBUG. |
156 void AssertContainersConsistent() const; | 159 void AssertContainersConsistent() const; |
157 | 160 |
158 // Add a DownloadItem to history_downloads_. | 161 // Add a DownloadItem to history_downloads_. |
159 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); | 162 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); |
160 | 163 |
161 // Remove from internal maps. | 164 // Remove from internal maps. |
162 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); | 165 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); |
163 | 166 |
164 // Called in response to our request to the DownloadFileManager to | |
165 // create a DownloadFile. A |reason| of | |
166 // content::DOWNLOAD_INTERRUPT_REASON_NONE indicates success. | |
167 void OnDownloadFileCreated( | |
168 int32 download_id, content::DownloadInterruptReason reason); | |
169 | |
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.) |
| 175 virtual void DelegateStart(DownloadItemImpl* item) OVERRIDE; |
178 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; | 176 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; |
179 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; | 177 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; |
180 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; | 178 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; |
181 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; | 179 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; |
182 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; | 180 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; |
183 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; | 181 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; |
184 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; | 182 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; |
185 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; | 183 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; |
186 virtual void DownloadRenamedToIntermediateName( | 184 virtual void DownloadRenamedToIntermediateName( |
187 DownloadItemImpl* download) OVERRIDE; | 185 DownloadItemImpl* download) OVERRIDE; |
188 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; | 186 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; |
189 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; | 187 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; |
190 | 188 |
191 // Factory for creation of downloads items. | 189 // Factory for creation of downloads items. |
192 scoped_ptr<content::DownloadItemFactory> factory_; | 190 scoped_ptr<content::DownloadItemFactory> factory_; |
193 | 191 |
| 192 // Factory for the creation of download files. |
| 193 scoped_ptr<content::DownloadFileFactory> file_factory_; |
| 194 |
194 // |downloads_| is the owning set for all downloads known to the | 195 // |downloads_| is the owning set for all downloads known to the |
195 // DownloadManager. This includes downloads started by the user in | 196 // DownloadManager. This includes downloads started by the user in |
196 // this session, downloads initialized from the history system, and | 197 // this session, downloads initialized from the history system, and |
197 // "save page as" downloads. All other DownloadItem containers in | 198 // "save page as" downloads. All other DownloadItem containers in |
198 // the DownloadManager are maps; they do not own the DownloadItems. | 199 // the DownloadManager are maps; they do not own the DownloadItems. |
199 // Note that this is the only place (with any functional implications; | 200 // Note that this is the only place (with any functional implications; |
200 // see save_page_downloads_ below) that "save page as" downloads are | 201 // see save_page_downloads_ below) that "save page as" downloads are |
201 // 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 |
202 // until destruction. | 203 // until destruction. |
203 // | 204 // |
204 // |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 |
205 // processed. The key is the ID assigned by the DownloadFileManager, | 206 // processed. |
206 // which is unique for the current session. | |
207 // | 207 // |
208 // When a download is created through a user action, the corresponding | 208 // When a download is created through a user action, the corresponding |
209 // DownloadItem* is placed in |active_downloads_| and remains there until the | 209 // 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 | 210 // 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. | 211 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. |
212 // 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 |
213 // system are placed directly into |history_downloads_| since they have valid | 213 // system are placed directly into |history_downloads_| since they have valid |
214 // handles in the history system. | 214 // handles in the history system. |
215 | 215 |
216 DownloadMap downloads_; | 216 DownloadMap downloads_; |
217 DownloadMap active_downloads_; | 217 DownloadMap active_downloads_; |
218 | 218 |
219 int history_size_; | 219 int history_size_; |
220 | 220 |
221 // 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. |
222 bool shutdown_needed_; | 222 bool shutdown_needed_; |
223 | 223 |
224 // 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. |
225 ObserverList<Observer> observers_; | 225 ObserverList<Observer> observers_; |
226 | 226 |
227 // The current active browser context. | 227 // The current active browser context. |
228 content::BrowserContext* browser_context_; | 228 content::BrowserContext* browser_context_; |
229 | 229 |
230 // Non-owning pointer for handling file writing on the download_thread_. | |
231 DownloadFileManager* file_manager_; | |
232 | |
233 // The user's last choice for download directory. This is only used when the | 230 // The user's last choice for download directory. This is only used when the |
234 // user wants us to prompt for a save location for each download. | 231 // user wants us to prompt for a save location for each download. |
235 FilePath last_download_path_; | 232 FilePath last_download_path_; |
236 | 233 |
237 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 234 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
238 content::DownloadManagerDelegate* delegate_; | 235 content::DownloadManagerDelegate* delegate_; |
239 | 236 |
240 net::NetLog* net_log_; | 237 net::NetLog* net_log_; |
241 | 238 |
242 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); | 239 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); |
243 }; | 240 }; |
244 | 241 |
245 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 242 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
OLD | NEW |