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_
12 #include "base/callback_forward.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/observer_list.h"
18 #include "base/sequenced_task_runner_helpers.h"
19 #include "base/synchronization/lock.h"
20 #include "content/browser/download/download_item_impl_delegate.h"
21 #include "content/common/content_export.h"
22 #include "content/public/browser/download_manager.h"
23 #include "content/public/browser/download_manager_delegate.h"
24 #include "content/public/browser/download_url_parameters.h"
31 class DownloadFileFactory
;
32 class DownloadItemFactory
;
33 class DownloadItemImpl
;
34 class DownloadRequestHandleInterface
;
36 class CONTENT_EXPORT DownloadManagerImpl
: public DownloadManager
,
37 private DownloadItemImplDelegate
{
39 typedef base::Callback
<void(DownloadItemImpl
*)> DownloadItemImplCreated
;
41 // Caller guarantees that |net_log| will remain valid
42 // for the lifetime of DownloadManagerImpl (until Shutdown() is called).
43 DownloadManagerImpl(net::NetLog
* net_log
, BrowserContext
* browser_context
);
44 ~DownloadManagerImpl() override
;
46 // Implementation functions (not part of the DownloadManager interface).
48 // Creates a download item for the SavePackage system.
49 // Must be called on the UI thread. Note that the DownloadManager
51 virtual void CreateSavePackageDownloadItem(
52 const base::FilePath
& main_file_path
,
54 const std::string
& mime_type
,
55 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
56 const DownloadItemImplCreated
& item_created
);
58 // Notifies DownloadManager about a successful completion of |download_item|.
59 void OnSavePackageSuccessfullyFinished(DownloadItem
* download_item
);
61 // DownloadManager functions.
62 void SetDelegate(DownloadManagerDelegate
* delegate
) override
;
63 DownloadManagerDelegate
* GetDelegate() const override
;
64 void Shutdown() override
;
65 void GetAllDownloads(DownloadVector
* result
) override
;
67 scoped_ptr
<DownloadCreateInfo
> info
,
68 scoped_ptr
<ByteStreamReader
> stream
,
69 const DownloadUrlParameters::OnStartedCallback
& on_started
) override
;
71 int RemoveDownloadsByOriginAndTime(const url::Origin
& origin
,
72 base::Time remove_begin
,
73 base::Time remove_end
) override
;
74 int RemoveDownloadsBetween(base::Time remove_begin
,
75 base::Time remove_end
) override
;
76 int RemoveDownloads(base::Time remove_begin
) override
;
77 int RemoveAllDownloads() override
;
78 void DownloadUrl(scoped_ptr
<DownloadUrlParameters
> params
) override
;
79 void AddObserver(Observer
* observer
) override
;
80 void RemoveObserver(Observer
* observer
) override
;
81 content::DownloadItem
* CreateDownloadItem(
83 const base::FilePath
& current_path
,
84 const base::FilePath
& target_path
,
85 const std::vector
<GURL
>& url_chain
,
86 const GURL
& referrer_url
,
87 const std::string
& mime_type
,
88 const std::string
& original_mime_type
,
89 const base::Time
& start_time
,
90 const base::Time
& end_time
,
91 const std::string
& etag
,
92 const std::string
& last_modified
,
95 content::DownloadItem::DownloadState state
,
96 DownloadDangerType danger_type
,
97 DownloadInterruptReason interrupt_reason
,
98 bool opened
) override
;
99 int InProgressCount() const override
;
100 int NonMaliciousInProgressCount() const override
;
101 BrowserContext
* GetBrowserContext() const override
;
102 void CheckForHistoryFilesRemoval() override
;
103 DownloadItem
* GetDownload(uint32 id
) override
;
105 // For testing; specifically, accessed from TestFileErrorInjector.
106 void SetDownloadItemFactoryForTesting(
107 scoped_ptr
<DownloadItemFactory
> item_factory
);
108 void SetDownloadFileFactoryForTesting(
109 scoped_ptr
<DownloadFileFactory
> file_factory
);
110 virtual DownloadFileFactory
* GetDownloadFileFactoryForTesting();
113 typedef std::set
<DownloadItem
*> DownloadSet
;
114 typedef base::hash_map
<uint32
, DownloadItemImpl
*> DownloadMap
;
115 typedef std::vector
<DownloadItemImpl
*> DownloadItemImplVector
;
116 typedef base::Callback
<bool(const DownloadItemImpl
*)> DownloadRemover
;
119 friend class DownloadManagerTest
;
120 friend class DownloadTest
;
122 void StartDownloadWithId(
123 scoped_ptr
<DownloadCreateInfo
> info
,
124 scoped_ptr
<ByteStreamReader
> stream
,
125 const DownloadUrlParameters::OnStartedCallback
& on_started
,
129 void CreateSavePackageDownloadItemWithId(
130 const base::FilePath
& main_file_path
,
131 const GURL
& page_url
,
132 const std::string
& mime_type
,
133 scoped_ptr
<DownloadRequestHandleInterface
> request_handle
,
134 const DownloadItemImplCreated
& on_started
,
137 // Create a new active item based on the info. Separate from
138 // StartDownload() for testing.
139 DownloadItemImpl
* CreateActiveItem(uint32 id
,
140 const DownloadCreateInfo
& info
);
142 // Get next download id. |callback| is called on the UI thread and may
143 // be called synchronously.
144 void GetNextId(const DownloadIdCallback
& callback
);
146 // Called with the result of DownloadManagerDelegate::CheckForFileExistence.
147 // Updates the state of the file and then notifies this update to the file's
149 void OnFileExistenceChecked(uint32 download_id
, bool result
);
151 // Remove all downloads for which |remover| returns true.
152 int RemoveDownloads(const DownloadRemover
& remover
);
154 // Overridden from DownloadItemImplDelegate
155 // (Note that |GetBrowserContext| are present in both interfaces.)
156 void DetermineDownloadTarget(DownloadItemImpl
* item
,
157 const DownloadTargetCallback
& callback
) override
;
158 bool ShouldCompleteDownload(DownloadItemImpl
* item
,
159 const base::Closure
& complete_callback
) override
;
160 bool ShouldOpenFileBasedOnExtension(const base::FilePath
& path
) override
;
161 bool ShouldOpenDownload(DownloadItemImpl
* item
,
162 const ShouldOpenDownloadCallback
& callback
) override
;
163 void CheckForFileRemoval(DownloadItemImpl
* download_item
) override
;
164 void ResumeInterruptedDownload(
165 scoped_ptr
<content::DownloadUrlParameters
> params
,
167 void OpenDownload(DownloadItemImpl
* download
) override
;
168 void ShowDownloadInShell(DownloadItemImpl
* download
) override
;
169 void DownloadRemoved(DownloadItemImpl
* download
) override
;
171 // Factory for creation of downloads items.
172 scoped_ptr
<DownloadItemFactory
> item_factory_
;
174 // Factory for the creation of download files.
175 scoped_ptr
<DownloadFileFactory
> file_factory_
;
177 // |downloads_| is the owning set for all downloads known to the
178 // DownloadManager. This includes downloads started by the user in
179 // this session, downloads initialized from the history system, and
180 // "save page as" downloads.
181 DownloadMap downloads_
;
185 // True if the download manager has been initialized and requires a shutdown.
186 bool shutdown_needed_
;
188 // Observers that want to be notified of changes to the set of downloads.
189 base::ObserverList
<Observer
> observers_
;
191 // The current active browser context.
192 BrowserContext
* browser_context_
;
194 // Allows an embedder to control behavior. Guaranteed to outlive this object.
195 DownloadManagerDelegate
* delegate_
;
197 net::NetLog
* net_log_
;
199 base::WeakPtrFactory
<DownloadManagerImpl
> weak_factory_
;
201 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl
);
204 } // namespace content
206 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_