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. 4 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ 7 8 #include <map> 9 #include <set> 10 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" 23 24 namespace net { 25 class BoundNetLog; 26 } 27 28 namespace content { 29 class DownloadFileFactory; 30 class DownloadItemFactory; 31 class DownloadItemImpl; 32 class DownloadRequestHandleInterface; 33 34 class CONTENT_EXPORT DownloadManagerImpl : public DownloadManager, 35 private DownloadItemImplDelegate { 36 public: 37 typedef base::Callback<void(DownloadItemImpl*)> DownloadItemImplCreated; 38 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(); 43 44 // 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( 50 const base::FilePath& main_file_path, 51 const GURL& page_url, 52 const std::string& mime_type, 53 scoped_ptr<DownloadRequestHandleInterface> request_handle, 54 const DownloadItemImplCreated& item_created); 55 56 // Notifies DownloadManager about a successful completion of |download_item|. 57 void OnSavePackageSuccessfullyFinished(DownloadItem* download_item); 58 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( 76 uint32 id, 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, 85 int64 received_bytes, 86 int64 total_bytes, 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 int NonMaliciousInProgressCount() const OVERRIDE; 93 virtual BrowserContext* GetBrowserContext() const OVERRIDE; 94 virtual void CheckForHistoryFilesRemoval() OVERRIDE; 95 virtual DownloadItem* GetDownload(uint32 id) OVERRIDE; 96 97 // For testing; specifically, accessed from TestFileErrorInjector. 98 void SetDownloadItemFactoryForTesting( 99 scoped_ptr<DownloadItemFactory> item_factory); 100 void SetDownloadFileFactoryForTesting( 101 scoped_ptr<DownloadFileFactory> file_factory); 102 virtual DownloadFileFactory* GetDownloadFileFactoryForTesting(); 103 104 private: 105 typedef std::set<DownloadItem*> DownloadSet; 106 typedef base::hash_map<uint32, DownloadItemImpl*> DownloadMap; 107 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; 108 109 // For testing. 110 friend class DownloadManagerTest; 111 friend class DownloadTest; 112 113 void StartDownloadWithId( 114 scoped_ptr<DownloadCreateInfo> info, 115 scoped_ptr<ByteStreamReader> stream, 116 const DownloadUrlParameters::OnStartedCallback& on_started, 117 bool new_download, 118 uint32 id); 119 120 void CreateSavePackageDownloadItemWithId( 121 const base::FilePath& main_file_path, 122 const GURL& page_url, 123 const std::string& mime_type, 124 scoped_ptr<DownloadRequestHandleInterface> request_handle, 125 const DownloadItemImplCreated& on_started, 126 uint32 id); 127 128 // Create a new active item based on the info. Separate from 129 // StartDownload() for testing. 130 DownloadItemImpl* CreateActiveItem(uint32 id, 131 const DownloadCreateInfo& info); 132 133 // Get next download id. |callback| is called on the UI thread and may 134 // be called synchronously. 135 void GetNextId(const DownloadIdCallback& callback); 136 137 // Called with the result of DownloadManagerDelegate::CheckForFileExistence. 138 // Updates the state of the file and then notifies this update to the file's 139 // observer. 140 void OnFileExistenceChecked(uint32 download_id, bool result); 141 142 // Overridden from DownloadItemImplDelegate 143 // (Note that |GetBrowserContext| are present in both interfaces.) 144 virtual void DetermineDownloadTarget( 145 DownloadItemImpl* item, const DownloadTargetCallback& callback) OVERRIDE; 146 virtual bool ShouldCompleteDownload( 147 DownloadItemImpl* item, const base::Closure& complete_callback) OVERRIDE; 148 virtual bool ShouldOpenFileBasedOnExtension( 149 const base::FilePath& path) OVERRIDE; 150 virtual bool ShouldOpenDownload( 151 DownloadItemImpl* item, 152 const ShouldOpenDownloadCallback& callback) OVERRIDE; 153 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; 154 virtual void ResumeInterruptedDownload( 155 scoped_ptr<content::DownloadUrlParameters> params, 156 uint32 id) OVERRIDE; 157 virtual void OpenDownload(DownloadItemImpl* download) OVERRIDE; 158 virtual void ShowDownloadInShell(DownloadItemImpl* download) OVERRIDE; 159 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; 160 161 // Factory for creation of downloads items. 162 scoped_ptr<DownloadItemFactory> item_factory_; 163 164 // Factory for the creation of download files. 165 scoped_ptr<DownloadFileFactory> file_factory_; 166 167 // |downloads_| is the owning set for all downloads known to the 168 // DownloadManager. This includes downloads started by the user in 169 // this session, downloads initialized from the history system, and 170 // "save page as" downloads. 171 DownloadMap downloads_; 172 173 int history_size_; 174 175 // True if the download manager has been initialized and requires a shutdown. 176 bool shutdown_needed_; 177 178 // Observers that want to be notified of changes to the set of downloads. 179 ObserverList<Observer> observers_; 180 181 // The current active browser context. 182 BrowserContext* browser_context_; 183 184 // Allows an embedder to control behavior. Guaranteed to outlive this object. 185 DownloadManagerDelegate* delegate_; 186 187 net::NetLog* net_log_; 188 189 base::WeakPtrFactory<DownloadManagerImpl> weak_factory_; 190 191 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); 192 }; 193 194 } // namespace content 195 196 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ 197