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

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

Issue 23496076: WIP - Refactor programmatic downloads Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 2 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/containers/hash_tables.h" 11 #include "base/containers/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/scoped_vector.h"
14 #include "base/memory/weak_ptr.h" 15 #include "base/memory/weak_ptr.h"
15 #include "base/observer_list.h" 16 #include "base/observer_list.h"
16 #include "base/sequenced_task_runner_helpers.h" 17 #include "base/sequenced_task_runner_helpers.h"
17 #include "base/synchronization/lock.h" 18 #include "base/synchronization/lock.h"
18 #include "content/browser/download/download_item_impl_delegate.h" 19 #include "content/browser/download/download_item_impl_delegate.h"
19 #include "content/common/content_export.h" 20 #include "content/common/content_export.h"
20 #include "content/public/browser/download_manager.h" 21 #include "content/public/browser/download_manager.h"
21 #include "content/public/browser/download_manager_delegate.h" 22 #include "content/public/browser/download_manager_delegate.h"
22 #include "content/public/browser/download_url_parameters.h" 23 #include "content/public/browser/download_url_parameters.h"
23 24
24 namespace net { 25 namespace net {
25 class BoundNetLog; 26 class BoundNetLog;
26 } 27 }
27 28
28 namespace content { 29 namespace content {
29 class DownloadFileFactory; 30 class DownloadFileFactory;
30 class DownloadItemFactory; 31 class DownloadItemFactory;
31 class DownloadItemImpl; 32 class DownloadItemImpl;
32 class DownloadRequestHandleInterface; 33 class DownloadRequestHandle;
33 34
34 class CONTENT_EXPORT DownloadManagerImpl : public DownloadManager, 35 class CONTENT_EXPORT DownloadManagerImpl : public DownloadManager,
35 private DownloadItemImplDelegate { 36 private DownloadItemImplDelegate {
36 public: 37 public:
37 typedef base::Callback<void(DownloadItemImpl*)> DownloadItemImplCreated; 38 typedef base::Callback<void(DownloadItemImpl*)> DownloadItemImplCreated;
38 39
39 // Caller guarantees that |net_log| will remain valid 40 // Caller guarantees that |net_log| will remain valid
40 // for the lifetime of DownloadManagerImpl (until Shutdown() is called). 41 // for the lifetime of DownloadManagerImpl (until Shutdown() is called).
41 DownloadManagerImpl(net::NetLog* net_log, BrowserContext* browser_context); 42 DownloadManagerImpl(net::NetLog* net_log, BrowserContext* browser_context);
42 virtual ~DownloadManagerImpl(); 43 virtual ~DownloadManagerImpl();
43 44
44 // Implementation functions (not part of the DownloadManager interface). 45 // Implementation functions (not part of the DownloadManager interface).
45
46 // Creates a download item for the SavePackage system.
47 // Must be called on the UI thread. Note that the DownloadManager
48 // retains ownership.
49 virtual void CreateSavePackageDownloadItem( 46 virtual void CreateSavePackageDownloadItem(
50 const base::FilePath& main_file_path, 47 scoped_ptr<DownloadCreateInfo> info,
51 const GURL& page_url, 48 const DownloadItemImplCreated& created_callback);
52 const std::string& mime_type,
53 scoped_ptr<DownloadRequestHandleInterface> request_handle,
54 const DownloadItemImplCreated& item_created);
55 49
56 // Notifies DownloadManager about a successful completion of |download_item|. 50 // Notifies DownloadManager about a successful completion of |download_item|.
57 void OnSavePackageSuccessfullyFinished(DownloadItem* download_item); 51 void OnSavePackageSuccessfullyFinished(DownloadItem* download_item);
58 52
59 // DownloadManager functions. 53 // DownloadManager functions.
60 virtual void SetDelegate(DownloadManagerDelegate* delegate) OVERRIDE; 54 virtual void SetDelegate(DownloadManagerDelegate* delegate) OVERRIDE;
61 virtual DownloadManagerDelegate* GetDelegate() const OVERRIDE; 55 virtual DownloadManagerDelegate* GetDelegate() const OVERRIDE;
62 virtual void Shutdown() OVERRIDE; 56 virtual void Shutdown() OVERRIDE;
63 virtual void GetAllDownloads(DownloadVector* result) OVERRIDE; 57 virtual void GetAllDownloads(DownloadVector* result) OVERRIDE;
64 virtual void StartDownload( 58 virtual void StartDownloadWithActiveRequest(
65 scoped_ptr<DownloadCreateInfo> info, 59 scoped_ptr<DownloadRequestHandle> request_handle,
66 scoped_ptr<ByteStreamReader> stream, 60 scoped_ptr<DownloadCreateInfo> info) OVERRIDE;
67 const DownloadUrlParameters::OnStartedCallback& on_started) OVERRIDE;
68 virtual int RemoveDownloadsBetween(base::Time remove_begin, 61 virtual int RemoveDownloadsBetween(base::Time remove_begin,
69 base::Time remove_end) OVERRIDE; 62 base::Time remove_end) OVERRIDE;
70 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; 63 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE;
71 virtual int RemoveAllDownloads() OVERRIDE; 64 virtual int RemoveAllDownloads() OVERRIDE;
72 virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> params) OVERRIDE; 65 virtual void DownloadUrl(scoped_ptr<DownloadUrlParameters> params) OVERRIDE;
73 virtual void AddObserver(Observer* observer) OVERRIDE; 66 virtual void AddObserver(Observer* observer) OVERRIDE;
74 virtual void RemoveObserver(Observer* observer) OVERRIDE; 67 virtual void RemoveObserver(Observer* observer) OVERRIDE;
75 virtual content::DownloadItem* CreateDownloadItem( 68 virtual content::DownloadItem* CreateDownloadItem(
76 uint32 id, 69 uint32 id,
77 const base::FilePath& current_path, 70 const base::FilePath& current_path,
(...skipping 24 matching lines...) Expand all
102 95
103 private: 96 private:
104 typedef std::set<DownloadItem*> DownloadSet; 97 typedef std::set<DownloadItem*> DownloadSet;
105 typedef base::hash_map<uint32, DownloadItemImpl*> DownloadMap; 98 typedef base::hash_map<uint32, DownloadItemImpl*> DownloadMap;
106 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; 99 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector;
107 100
108 // For testing. 101 // For testing.
109 friend class DownloadManagerTest; 102 friend class DownloadManagerTest;
110 friend class DownloadTest; 103 friend class DownloadTest;
111 104
112 void StartDownloadWithId( 105 DownloadItemImpl* UpdateAndGetResumedDownloadItem(
113 scoped_ptr<DownloadCreateInfo> info, 106 uint32 download_id,
114 scoped_ptr<ByteStreamReader> stream, 107 DownloadRequestHandle* request_handle,
108 const DownloadCreateInfo& new_create_info);
109
110 void DownloadUrlWithId(scoped_ptr<DownloadUrlParameters> params,
111 bool new_download,
112 uint32 download_id);
113
114 void OnDownloadRequestStarted(
115 uint32 download_id,
116 bool new_download,
115 const DownloadUrlParameters::OnStartedCallback& on_started, 117 const DownloadUrlParameters::OnStartedCallback& on_started,
116 bool new_download, 118 DownloadRequestHandle* request_handle,
117 uint32 id); 119 DownloadInterruptReason interrupt_reason,
120 scoped_ptr<DownloadCreateInfo> create_info);
121
122 void StartDownloadWithId(scoped_ptr<DownloadRequestHandle> request_handle,
123 scoped_ptr<DownloadCreateInfo> info,
124 uint32 id);
118 125
119 void CreateSavePackageDownloadItemWithId( 126 void CreateSavePackageDownloadItemWithId(
120 const base::FilePath& main_file_path, 127 scoped_ptr<DownloadCreateInfo> info,
121 const GURL& page_url, 128 const DownloadItemImplCreated& download_created_callback,
122 const std::string& mime_type,
123 scoped_ptr<DownloadRequestHandleInterface> request_handle,
124 const DownloadItemImplCreated& on_started,
125 uint32 id); 129 uint32 id);
126 130
127 // Create a new active item based on the info. Separate from 131 // Create a new active item based on the info. Separate from
128 // StartDownload() for testing. 132 // StartDownload() for testing.
129 DownloadItemImpl* CreateActiveItem(uint32 id, 133 DownloadItemImpl* CreateActiveItem(uint32 id, const DownloadCreateInfo& info);
130 const DownloadCreateInfo& info);
131 134
132 // Get next download id. |callback| is called on the UI thread and may 135 // Get next download id. |callback| is called on the UI thread and may
133 // be called synchronously. 136 // be called synchronously.
134 void GetNextId(const DownloadIdCallback& callback); 137 void GetNextId(const DownloadIdCallback& callback);
135 138
136 // Called with the result of DownloadManagerDelegate::CheckForFileExistence. 139 // Called with the result of DownloadManagerDelegate::CheckForFileExistence.
137 // Updates the state of the file and then notifies this update to the file's 140 // Updates the state of the file and then notifies this update to the file's
138 // observer. 141 // observer.
139 void OnFileExistenceChecked(uint32 download_id, bool result); 142 void OnFileExistenceChecked(uint32 download_id, bool result);
140 143
(...skipping 21 matching lines...) Expand all
162 165
163 // Factory for the creation of download files. 166 // Factory for the creation of download files.
164 scoped_ptr<DownloadFileFactory> file_factory_; 167 scoped_ptr<DownloadFileFactory> file_factory_;
165 168
166 // |downloads_| is the owning set for all downloads known to the 169 // |downloads_| is the owning set for all downloads known to the
167 // DownloadManager. This includes downloads started by the user in 170 // DownloadManager. This includes downloads started by the user in
168 // this session, downloads initialized from the history system, and 171 // this session, downloads initialized from the history system, and
169 // "save page as" downloads. 172 // "save page as" downloads.
170 DownloadMap downloads_; 173 DownloadMap downloads_;
171 174
175 // Handles to programmatic download requests that are still pending. These
176 // are kept here so that if the DownloadManager were to be destroyed while the
177 // requests are still pending, the associated handles and, by extension, the
178 // URLRequest will also be destroyed.
179 //
180 // TODO(asanka): Move towards model of constructing the DownloadItem early and
181 // then having the DownloadItem own the request handle.
182 ScopedVector<DownloadRequestHandle> pending_requests_;
183
172 int history_size_; 184 int history_size_;
173 185
174 // True if the download manager has been initialized and requires a shutdown. 186 // True if the download manager has been initialized and requires a shutdown.
175 bool shutdown_needed_; 187 bool shutdown_needed_;
176 188
177 // Observers that want to be notified of changes to the set of downloads. 189 // Observers that want to be notified of changes to the set of downloads.
178 ObserverList<Observer> observers_; 190 ObserverList<Observer> observers_;
179 191
180 // The current active browser context. 192 // The current active browser context.
181 BrowserContext* browser_context_; 193 BrowserContext* browser_context_;
182 194
183 // Allows an embedder to control behavior. Guaranteed to outlive this object. 195 // Allows an embedder to control behavior. Guaranteed to outlive this object.
184 DownloadManagerDelegate* delegate_; 196 DownloadManagerDelegate* delegate_;
185 197
186 net::NetLog* net_log_; 198 net::NetLog* net_log_;
187 199
188 base::WeakPtrFactory<DownloadManagerImpl> weak_factory_; 200 base::WeakPtrFactory<DownloadManagerImpl> weak_factory_;
189 201
190 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); 202 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl);
191 }; 203 };
192 204
193 } // namespace content 205 } // namespace content
194 206
195 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ 207 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
OLDNEW
« no previous file with comments | « content/browser/download/download_item_impl_unittest.cc ('k') | content/browser/download/download_manager_impl.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698