Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / download / download_manager_impl.h
blob0c9c33bbbad5a3b078c1b33363e610cdf1f8c908
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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/sequenced_task_runner_helpers.h"
18 #include "base/synchronization/lock.h"
19 #include "content/browser/download/download_item_impl_delegate.h"
20 #include "content/common/content_export.h"
21 #include "content/public/browser/download_manager.h"
22 #include "content/public/browser/download_manager_delegate.h"
23 #include "content/public/browser/download_url_parameters.h"
25 namespace net {
26 class BoundNetLog;
29 namespace content {
30 class DownloadFileFactory;
31 class DownloadItemFactory;
32 class DownloadItemImpl;
33 class DownloadRequestHandleInterface;
35 class CONTENT_EXPORT DownloadManagerImpl : public DownloadManager,
36 private DownloadItemImplDelegate {
37 public:
38 typedef base::Callback<void(DownloadItemImpl*)> DownloadItemImplCreated;
40 // Caller guarantees that |net_log| will remain valid
41 // for the lifetime of DownloadManagerImpl (until Shutdown() is called).
42 DownloadManagerImpl(net::NetLog* net_log, BrowserContext* browser_context);
43 ~DownloadManagerImpl() override;
45 // Implementation functions (not part of the DownloadManager interface).
47 // Creates a download item for the SavePackage system.
48 // Must be called on the UI thread. Note that the DownloadManager
49 // retains ownership.
50 virtual void CreateSavePackageDownloadItem(
51 const base::FilePath& main_file_path,
52 const GURL& page_url,
53 const std::string& mime_type,
54 scoped_ptr<DownloadRequestHandleInterface> request_handle,
55 const DownloadItemImplCreated& item_created);
57 // Notifies DownloadManager about a successful completion of |download_item|.
58 void OnSavePackageSuccessfullyFinished(DownloadItem* download_item);
60 // DownloadManager functions.
61 void SetDelegate(DownloadManagerDelegate* delegate) override;
62 DownloadManagerDelegate* GetDelegate() const override;
63 void Shutdown() override;
64 void GetAllDownloads(DownloadVector* result) override;
65 void StartDownload(
66 scoped_ptr<DownloadCreateInfo> info,
67 scoped_ptr<ByteStreamReader> stream,
68 const DownloadUrlParameters::OnStartedCallback& on_started) override;
69 int RemoveDownloadsBetween(base::Time remove_begin,
70 base::Time remove_end) override;
71 int RemoveDownloads(base::Time remove_begin) override;
72 int RemoveAllDownloads() override;
73 void DownloadUrl(scoped_ptr<DownloadUrlParameters> params) override;
74 void AddObserver(Observer* observer) override;
75 void RemoveObserver(Observer* observer) override;
76 content::DownloadItem* CreateDownloadItem(
77 uint32 id,
78 const base::FilePath& current_path,
79 const base::FilePath& target_path,
80 const std::vector<GURL>& url_chain,
81 const GURL& referrer_url,
82 const std::string& mime_type,
83 const std::string& original_mime_type,
84 const base::Time& start_time,
85 const base::Time& end_time,
86 const std::string& etag,
87 const std::string& last_modified,
88 int64 received_bytes,
89 int64 total_bytes,
90 content::DownloadItem::DownloadState state,
91 DownloadDangerType danger_type,
92 DownloadInterruptReason interrupt_reason,
93 bool opened) override;
94 int InProgressCount() const override;
95 int NonMaliciousInProgressCount() const override;
96 BrowserContext* GetBrowserContext() const override;
97 void CheckForHistoryFilesRemoval() override;
98 DownloadItem* GetDownload(uint32 id) override;
100 // For testing; specifically, accessed from TestFileErrorInjector.
101 void SetDownloadItemFactoryForTesting(
102 scoped_ptr<DownloadItemFactory> item_factory);
103 void SetDownloadFileFactoryForTesting(
104 scoped_ptr<DownloadFileFactory> file_factory);
105 virtual DownloadFileFactory* GetDownloadFileFactoryForTesting();
107 private:
108 typedef std::set<DownloadItem*> DownloadSet;
109 typedef base::hash_map<uint32, DownloadItemImpl*> DownloadMap;
110 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector;
112 // For testing.
113 friend class DownloadManagerTest;
114 friend class DownloadTest;
116 void StartDownloadWithId(
117 scoped_ptr<DownloadCreateInfo> info,
118 scoped_ptr<ByteStreamReader> stream,
119 const DownloadUrlParameters::OnStartedCallback& on_started,
120 bool new_download,
121 uint32 id);
123 void CreateSavePackageDownloadItemWithId(
124 const base::FilePath& main_file_path,
125 const GURL& page_url,
126 const std::string& mime_type,
127 scoped_ptr<DownloadRequestHandleInterface> request_handle,
128 const DownloadItemImplCreated& on_started,
129 uint32 id);
131 // Create a new active item based on the info. Separate from
132 // StartDownload() for testing.
133 DownloadItemImpl* CreateActiveItem(uint32 id,
134 const DownloadCreateInfo& info);
136 // Get next download id. |callback| is called on the UI thread and may
137 // be called synchronously.
138 void GetNextId(const DownloadIdCallback& callback);
140 // Called with the result of DownloadManagerDelegate::CheckForFileExistence.
141 // Updates the state of the file and then notifies this update to the file's
142 // observer.
143 void OnFileExistenceChecked(uint32 download_id, bool result);
145 // Overridden from DownloadItemImplDelegate
146 // (Note that |GetBrowserContext| are present in both interfaces.)
147 void DetermineDownloadTarget(DownloadItemImpl* item,
148 const DownloadTargetCallback& callback) override;
149 bool ShouldCompleteDownload(DownloadItemImpl* item,
150 const base::Closure& complete_callback) override;
151 bool ShouldOpenFileBasedOnExtension(const base::FilePath& path) override;
152 bool ShouldOpenDownload(DownloadItemImpl* item,
153 const ShouldOpenDownloadCallback& callback) override;
154 void CheckForFileRemoval(DownloadItemImpl* download_item) override;
155 void ResumeInterruptedDownload(
156 scoped_ptr<content::DownloadUrlParameters> params,
157 uint32 id) override;
158 void OpenDownload(DownloadItemImpl* download) override;
159 void ShowDownloadInShell(DownloadItemImpl* download) override;
160 void DownloadRemoved(DownloadItemImpl* download) override;
162 // Factory for creation of downloads items.
163 scoped_ptr<DownloadItemFactory> item_factory_;
165 // Factory for the creation of download files.
166 scoped_ptr<DownloadFileFactory> file_factory_;
168 // |downloads_| is the owning set for all downloads known to the
169 // DownloadManager. This includes downloads started by the user in
170 // this session, downloads initialized from the history system, and
171 // "save page as" downloads.
172 DownloadMap downloads_;
174 int history_size_;
176 // True if the download manager has been initialized and requires a shutdown.
177 bool shutdown_needed_;
179 // Observers that want to be notified of changes to the set of downloads.
180 ObserverList<Observer> observers_;
182 // The current active browser context.
183 BrowserContext* browser_context_;
185 // Allows an embedder to control behavior. Guaranteed to outlive this object.
186 DownloadManagerDelegate* delegate_;
188 net::NetLog* net_log_;
190 base::WeakPtrFactory<DownloadManagerImpl> weak_factory_;
192 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl);
195 } // namespace content
197 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_