1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
#pragma once
#include "content/public/browser/download_manager.h"
#include <map>
#include <set>
#include "base/hash_tables.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/synchronization/lock.h"
#include "content/browser/download/download_item_impl.h"
#include "content/browser/download/download_status_updater_delegate.h"
#include "content/common/content_export.h"
class DownloadIdFactory;
class DownloadStatusUpdater;
class CONTENT_EXPORT DownloadManagerImpl
: public content::DownloadManager,
public DownloadItemImpl::Delegate,
public DownloadStatusUpdaterDelegate {
public:
DownloadManagerImpl(content::DownloadManagerDelegate* delegate,
DownloadIdFactory* id_factory,
DownloadStatusUpdater* status_updater);
// content::DownloadManager functions.
virtual void Shutdown() OVERRIDE;
virtual void GetTemporaryDownloads(const FilePath& dir_path,
DownloadVector* result) OVERRIDE;
virtual void GetAllDownloads(const FilePath& dir_path,
DownloadVector* result) OVERRIDE;
virtual void SearchDownloads(const string16& query,
DownloadVector* result) OVERRIDE;
virtual bool Init(content::BrowserContext* browser_context) OVERRIDE;
virtual void StartDownload(int32 id) OVERRIDE;
virtual void UpdateDownload(int32 download_id,
int64 bytes_so_far,
int64 bytes_per_sec,
std::string hash_state) OVERRIDE;
virtual void OnResponseCompleted(int32 download_id, int64 size,
const std::string& hash) OVERRIDE;
virtual void CancelDownload(int32 download_id) OVERRIDE;
virtual void OnDownloadInterrupted(int32 download_id,
int64 size,
std::string hash_state,
InterruptReason reason) OVERRIDE;
virtual void OnDownloadRenamedToFinalName(int download_id,
const FilePath& full_path,
int uniquifier) OVERRIDE;
virtual int RemoveDownloadsBetween(const base::Time remove_begin,
const base::Time remove_end) OVERRIDE;
virtual int RemoveDownloads(const base::Time remove_begin) OVERRIDE;
virtual int RemoveAllDownloads() OVERRIDE;
virtual void DownloadUrl(const GURL& url,
const GURL& referrer,
const std::string& referrer_encoding,
TabContents* tab_contents) OVERRIDE;
virtual void DownloadUrlToFile(const GURL& url,
const GURL& referrer,
const std::string& referrer_encoding,
const DownloadSaveInfo& save_info,
content::WebContents* web_contents) OVERRIDE;
virtual void AddObserver(Observer* observer) OVERRIDE;
virtual void RemoveObserver(Observer* observer) OVERRIDE;
virtual void OnPersistentStoreQueryComplete(
std::vector<DownloadPersistentStoreInfo>* entries) OVERRIDE;
virtual void OnItemAddedToPersistentStore(int32 download_id,
int64 db_handle) OVERRIDE;
virtual int InProgressCount() const OVERRIDE;
virtual content::BrowserContext* GetBrowserContext() const OVERRIDE;
virtual FilePath LastDownloadPath() OVERRIDE;
virtual void CreateDownloadItem(
DownloadCreateInfo* info,
const DownloadRequestHandle& request_handle) OVERRIDE;
virtual content::DownloadItem* CreateSavePackageDownloadItem(
const FilePath& main_file_path,
const GURL& page_url,
bool is_otr,
content::DownloadItem::Observer* observer) OVERRIDE;
virtual void ClearLastDownloadPath() OVERRIDE;
virtual void FileSelected(const FilePath& path, void* params) OVERRIDE;
virtual void FileSelectionCanceled(void* params) OVERRIDE;
virtual void RestartDownload(int32 download_id) OVERRIDE;
virtual void CheckForHistoryFilesRemoval() OVERRIDE;
virtual content::DownloadItem* GetDownloadItem(int id) OVERRIDE;
virtual void SavePageDownloadFinished(
content::DownloadItem* download) OVERRIDE;
virtual content::DownloadItem* GetActiveDownloadItem(int id) OVERRIDE;
virtual content::DownloadManagerDelegate* delegate() const OVERRIDE;
virtual void SetDownloadManagerDelegate(
content::DownloadManagerDelegate* delegate) OVERRIDE;
// Overridden from DownloadItemImpl::Delegate
// (Note that |GetBrowserContext| are present in both interfaces.)
virtual bool ShouldOpenDownload(content::DownloadItem* item) OVERRIDE;
virtual bool ShouldOpenFileBasedOnExtension(
const FilePath& path) OVERRIDE;
virtual void CheckForFileRemoval(
content::DownloadItem* download_item) OVERRIDE;
virtual void MaybeCompleteDownload(
content::DownloadItem* download) OVERRIDE;
virtual void DownloadCancelled(
content::DownloadItem* download) OVERRIDE;
virtual void DownloadCompleted(
content::DownloadItem* download) OVERRIDE;
virtual void DownloadOpened(
content::DownloadItem* download) OVERRIDE;
virtual void DownloadRemoved(content::DownloadItem* download) OVERRIDE;
virtual void AssertStateConsistent(
content::DownloadItem* download) const OVERRIDE;
// Overridden from DownloadStatusUpdaterDelegate:
virtual bool IsDownloadProgressKnown() const OVERRIDE;
virtual int64 GetInProgressDownloadCount() const OVERRIDE;
virtual int64 GetReceivedDownloadBytes() const OVERRIDE;
virtual int64 GetTotalDownloadBytes() const OVERRIDE;
private:
typedef std::set<content::DownloadItem*> DownloadSet;
typedef base::hash_map<int64, content::DownloadItem*> DownloadMap;
// For testing.
friend class DownloadManagerTest;
friend class DownloadTest;
friend class base::RefCountedThreadSafe<
DownloadManagerImpl, content::BrowserThread::DeleteOnUIThread>;
friend struct content::BrowserThread::DeleteOnThread<
content::BrowserThread::UI>;
friend class DeleteTask<DownloadManagerImpl>;
virtual ~DownloadManagerImpl();
// Determine if the download is ready for completion, i.e. has had
// all data saved, and completed the filename determination and
// history insertion.
bool IsDownloadReadyForCompletion(content::DownloadItem* download);
// Show the download in the browser.
void ShowDownloadInBrowser(content::DownloadItem* download);
// Get next download id from factory.
DownloadId GetNextId();
// Called on the FILE thread to check the existence of a downloaded file.
void CheckForFileRemovalOnFileThread(int64 db_handle, const FilePath& path);
// Called on the UI thread if the FILE thread detects the removal of
// the downloaded file. The UI thread updates the state of the file
// and then notifies this update to the file's observer.
void OnFileRemovalDetected(int64 db_handle);
// Called back after a target path for the file to be downloaded to has been
// determined, either automatically based on the suggested file name, or by
// the user in a Save As dialog box.
virtual void ContinueDownloadWithPath(content::DownloadItem* download,
const FilePath& chosen_file) OVERRIDE;
// Retrieves the download from the |download_id|.
// Returns NULL if the download is not active.
virtual content::DownloadItem* GetActiveDownload(int32 download_id) OVERRIDE;
// Removes |download| from the active and in progress maps.
// Called when the download is cancelled or has an error.
// Does nothing if the download is not in the history DB.
void RemoveFromActiveList(content::DownloadItem* download);
// Updates the delegate about the overall download progress.
void UpdateDownloadProgress();
// Inform observers that the model has changed.
void NotifyModelChanged();
// Debugging routine to confirm relationship between below
// containers; no-op if NDEBUG.
void AssertContainersConsistent() const;
// Add a DownloadItem to history_downloads_.
void AddDownloadItemToHistory(content::DownloadItem* item, int64 db_handle);
// Remove from internal maps.
int RemoveDownloadItems(const DownloadVector& pending_deletes);
// Called when a download entry is committed to the persistent store.
void OnDownloadItemAddedToPersistentStore(int32 download_id, int64 db_handle);
// Called when Save Page As entry is committed to the persistent store.
void OnSavePageItemAddedToPersistentStore(int32 download_id, int64 db_handle);
// For unit tests only.
virtual void SetFileManager(DownloadFileManager* file_manager) OVERRIDE;
// |downloads_| is the owning set for all downloads known to the
// DownloadManager. This includes downloads started by the user in
// this session, downloads initialized from the history system, and
// "save page as" downloads. All other DownloadItem containers in
// the DownloadManager are maps; they do not own the DownloadItems.
// Note that this is the only place (with any functional implications;
// see save_page_downloads_ below) that "save page as" downloads are
// kept, as the DownloadManager's only job is to hold onto those
// until destruction.
//
// |history_downloads_| is map of all downloads in this browser context. The
// key is the handle returned by the history system, which is unique across
// sessions.
//
// |active_downloads_| is a map of all downloads that are currently being
// processed. The key is the ID assigned by the DownloadFileManager,
// which is unique for the current session.
//
// |in_progress_| is a map of all downloads that are in progress and that have
// not yet received a valid history handle. The key is the ID assigned by the
// DownloadFileManager, which is unique for the current session.
//
// |save_page_downloads_| (if defined) is a collection of all the
// downloads the "save page as" system has given to us to hold onto
// until we are destroyed. They key is DownloadFileManager, so it is unique
// compared to download item. It is only used for debugging.
//
// When a download is created through a user action, the corresponding
// DownloadItem* is placed in |active_downloads_| and remains there until the
// download is in a terminal state (COMPLETE or CANCELLED). It is also
// placed in |in_progress_| and remains there until it has received a
// valid handle from the history system. Once it has a valid handle, the
// DownloadItem* is placed in the |history_downloads_| map. When the
// download reaches a terminal state, it is removed from |in_progress_|.
// Downloads from past sessions read from a persisted state from the
// history system are placed directly into |history_downloads_| since
// they have valid handles in the history system.
DownloadSet downloads_;
DownloadMap history_downloads_;
DownloadMap in_progress_;
DownloadMap active_downloads_;
DownloadMap save_page_downloads_;
// True if the download manager has been initialized and requires a shutdown.
bool shutdown_needed_;
// Observers that want to be notified of changes to the set of downloads.
ObserverList<Observer> observers_;
// The current active browser context.
content::BrowserContext* browser_context_;
// Non-owning pointer for handling file writing on the download_thread_.
DownloadFileManager* file_manager_;
// Non-owning pointer for updating the download status.
base::WeakPtr<DownloadStatusUpdater> status_updater_;
// The user's last choice for download directory. This is only used when the
// user wants us to prompt for a save location for each download.
FilePath last_download_path_;
// Allows an embedder to control behavior. Guaranteed to outlive this object.
content::DownloadManagerDelegate* delegate_;
DownloadIdFactory* id_factory_;
// TODO(rdsmith): Remove when http://crbug.com/85408 is fixed.
// For debugging only.
int64 largest_db_handle_in_history_;
DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl);
};
#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
|