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