Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(316)

Side by Side Diff: content/browser/download/download_manager_impl.h

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

Powered by Google App Engine
This is Rietveld 408576698