| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 5 #ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "base/hash_tables.h" | 11 #include "base/hash_tables.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/sequenced_task_runner_helpers.h" | 16 #include "base/sequenced_task_runner_helpers.h" |
| 17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 18 #include "content/browser/download/download_item_factory.h" | |
| 19 #include "content/browser/download/download_item_impl_delegate.h" | 18 #include "content/browser/download/download_item_impl_delegate.h" |
| 20 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
| 21 #include "content/public/browser/download_manager.h" | 20 #include "content/public/browser/download_manager.h" |
| 22 | 21 |
| 23 class DownloadFileManager; | 22 class DownloadFileManager; |
| 24 class DownloadItemImpl; | 23 class DownloadItemImpl; |
| 25 | 24 |
| 25 namespace content { |
| 26 class DownloadItemFactory; |
| 27 class DownloadFileFactory; |
| 28 } |
| 29 |
| 30 namespace net { |
| 31 class BoundNetLog; |
| 32 } |
| 33 |
| 26 class CONTENT_EXPORT DownloadManagerImpl | 34 class CONTENT_EXPORT DownloadManagerImpl |
| 27 : public content::DownloadManager, | 35 : public content::DownloadManager, |
| 28 private DownloadItemImplDelegate { | 36 private DownloadItemImplDelegate { |
| 29 public: | 37 public: |
| 30 // Caller guarantees that |file_manager| and |net_log| will remain valid | 38 // Caller guarantees that |net_log| will remain valid |
| 31 // for the lifetime of DownloadManagerImpl (until Shutdown() is called). | 39 // for the lifetime of DownloadManagerImpl (until Shutdown() is called). |
| 32 // |factory| may be a default constructed (null) scoped_ptr; if so, | 40 DownloadManagerImpl(net::NetLog* net_log); |
| 33 // the DownloadManagerImpl creates and takes ownership of the | |
| 34 // default DownloadItemFactory. | |
| 35 DownloadManagerImpl(DownloadFileManager* file_manager, | |
| 36 scoped_ptr<content::DownloadItemFactory> factory, | |
| 37 net::NetLog* net_log); | |
| 38 | 41 |
| 39 // Implementation functions (not part of the DownloadManager interface). | 42 // Implementation functions (not part of the DownloadManager interface). |
| 40 | 43 |
| 41 // Creates a download item for the SavePackage system. | 44 // Creates a download item for the SavePackage system. |
| 42 // Must be called on the UI thread. Note that the DownloadManager | 45 // Must be called on the UI thread. Note that the DownloadManager |
| 43 // retains ownership. | 46 // retains ownership. |
| 44 virtual DownloadItemImpl* CreateSavePackageDownloadItem( | 47 virtual DownloadItemImpl* CreateSavePackageDownloadItem( |
| 45 const FilePath& main_file_path, | 48 const FilePath& main_file_path, |
| 46 const GURL& page_url, | 49 const GURL& page_url, |
| 47 const std::string& mime_type, | 50 const std::string& mime_type, |
| 48 content::DownloadItem::Observer* observer); | 51 content::DownloadItem::Observer* observer); |
| 49 | 52 |
| 50 // content::DownloadManager functions. | 53 // content::DownloadManager functions. |
| 51 virtual void SetDelegate(content::DownloadManagerDelegate* delegate) OVERRIDE; | 54 virtual void SetDelegate(content::DownloadManagerDelegate* delegate) OVERRIDE; |
| 52 virtual content::DownloadManagerDelegate* GetDelegate() const OVERRIDE; | 55 virtual content::DownloadManagerDelegate* GetDelegate() const OVERRIDE; |
| 53 virtual void Shutdown() OVERRIDE; | 56 virtual void Shutdown() OVERRIDE; |
| 54 virtual void GetAllDownloads(DownloadVector* result) OVERRIDE; | 57 virtual void GetAllDownloads(DownloadVector* result) OVERRIDE; |
| 55 virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; | 58 virtual bool Init(content::BrowserContext* browser_context) OVERRIDE; |
| 56 virtual content::DownloadItem* StartDownload( | 59 virtual content::DownloadItem* StartDownload( |
| 57 scoped_ptr<DownloadCreateInfo> info, | 60 scoped_ptr<DownloadCreateInfo> info, |
| 58 scoped_ptr<content::ByteStreamReader> stream) OVERRIDE; | 61 scoped_ptr<content::ByteStreamReader> stream) OVERRIDE; |
| 59 virtual void UpdateDownload(int32 download_id, | |
| 60 int64 bytes_so_far, | |
| 61 int64 bytes_per_sec, | |
| 62 const std::string& hash_state) OVERRIDE; | |
| 63 virtual void OnResponseCompleted(int32 download_id, int64 size, | |
| 64 const std::string& hash) OVERRIDE; | |
| 65 virtual void CancelDownload(int32 download_id) OVERRIDE; | 62 virtual void CancelDownload(int32 download_id) OVERRIDE; |
| 66 virtual void OnDownloadInterrupted( | |
| 67 int32 download_id, | |
| 68 content::DownloadInterruptReason reason) OVERRIDE; | |
| 69 virtual int RemoveDownloadsBetween(base::Time remove_begin, | 63 virtual int RemoveDownloadsBetween(base::Time remove_begin, |
| 70 base::Time remove_end) OVERRIDE; | 64 base::Time remove_end) OVERRIDE; |
| 71 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; | 65 virtual int RemoveDownloads(base::Time remove_begin) OVERRIDE; |
| 72 virtual int RemoveAllDownloads() OVERRIDE; | 66 virtual int RemoveAllDownloads() OVERRIDE; |
| 73 virtual void DownloadUrl( | 67 virtual void DownloadUrl( |
| 74 scoped_ptr<content::DownloadUrlParameters> params) OVERRIDE; | 68 scoped_ptr<content::DownloadUrlParameters> params) OVERRIDE; |
| 75 virtual void AddObserver(Observer* observer) OVERRIDE; | 69 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 76 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 70 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 77 virtual void OnPersistentStoreQueryComplete( | 71 virtual void OnPersistentStoreQueryComplete( |
| 78 std::vector<content::DownloadPersistentStoreInfo>* entries) OVERRIDE; | 72 std::vector<content::DownloadPersistentStoreInfo>* entries) OVERRIDE; |
| 79 virtual void OnItemAddedToPersistentStore(int32 download_id, | 73 virtual void OnItemAddedToPersistentStore(int32 download_id, |
| 80 int64 db_handle) OVERRIDE; | 74 int64 db_handle) OVERRIDE; |
| 81 virtual int InProgressCount() const OVERRIDE; | 75 virtual int InProgressCount() const OVERRIDE; |
| 82 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; | 76 virtual content::BrowserContext* GetBrowserContext() const OVERRIDE; |
| 83 virtual void CheckForHistoryFilesRemoval() OVERRIDE; | 77 virtual void CheckForHistoryFilesRemoval() OVERRIDE; |
| 84 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; | 78 virtual content::DownloadItem* GetDownload(int id) OVERRIDE; |
| 85 virtual void SavePageDownloadFinished( | 79 virtual void SavePageDownloadFinished( |
| 86 content::DownloadItem* download) OVERRIDE; | 80 content::DownloadItem* download) OVERRIDE; |
| 87 | 81 |
| 82 // For testing; specifically, accessed from TestFileErrorInjector. |
| 83 void SetDownloadItemFactoryForTesting( |
| 84 scoped_ptr<content::DownloadItemFactory> item_factory); |
| 85 void SetDownloadFileFactoryForTesting( |
| 86 scoped_ptr<content::DownloadFileFactory> file_factory); |
| 87 virtual content::DownloadFileFactory* GetDownloadFileFactoryForTesting(); |
| 88 |
| 88 private: | 89 private: |
| 89 typedef std::set<content::DownloadItem*> DownloadSet; | 90 typedef std::set<content::DownloadItem*> DownloadSet; |
| 90 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; | 91 typedef base::hash_map<int32, DownloadItemImpl*> DownloadMap; |
| 91 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; | 92 typedef std::vector<DownloadItemImpl*> DownloadItemImplVector; |
| 92 | 93 |
| 93 // For testing. | 94 // For testing. |
| 94 friend class DownloadManagerTest; | 95 friend class DownloadManagerTest; |
| 95 friend class DownloadTest; | 96 friend class DownloadTest; |
| 96 | 97 |
| 97 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; | 98 friend class base::RefCountedThreadSafe<DownloadManagerImpl>; |
| 98 | 99 |
| 99 virtual ~DownloadManagerImpl(); | 100 virtual ~DownloadManagerImpl(); |
| 100 | 101 |
| 101 // Creates the download item. Must be called on the UI thread. | 102 // Creates the download item. Must be called on the UI thread. |
| 102 // Returns the |BoundNetLog| used by the |DownloadItem|. | 103 virtual DownloadItemImpl* CreateDownloadItem( |
| 103 virtual net::BoundNetLog CreateDownloadItem(DownloadCreateInfo* info); | 104 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log); |
| 104 | 105 |
| 105 // Show the download in the browser. | 106 // Show the download in the browser. |
| 106 void ShowDownloadInBrowser(DownloadItemImpl* download); | 107 void ShowDownloadInBrowser(DownloadItemImpl* download); |
| 107 | 108 |
| 108 // Get next download id. | 109 // Get next download id. |
| 109 content::DownloadId GetNextId(); | 110 content::DownloadId GetNextId(); |
| 110 | 111 |
| 111 // Called on the FILE thread to check the existence of a downloaded file. | 112 // Called on the FILE thread to check the existence of a downloaded file. |
| 112 void CheckForFileRemovalOnFileThread(int32 download_id, const FilePath& path); | 113 void CheckForFileRemovalOnFileThread(int32 download_id, const FilePath& path); |
| 113 | 114 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 127 // Debugging routine to confirm relationship between below | 128 // Debugging routine to confirm relationship between below |
| 128 // containers; no-op if NDEBUG. | 129 // containers; no-op if NDEBUG. |
| 129 void AssertContainersConsistent() const; | 130 void AssertContainersConsistent() const; |
| 130 | 131 |
| 131 // Add a DownloadItem to history_downloads_. | 132 // Add a DownloadItem to history_downloads_. |
| 132 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); | 133 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); |
| 133 | 134 |
| 134 // Remove from internal maps. | 135 // Remove from internal maps. |
| 135 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); | 136 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); |
| 136 | 137 |
| 137 // Called in response to our request to the DownloadFileManager to | |
| 138 // create a DownloadFile. A |reason| of | |
| 139 // content::DOWNLOAD_INTERRUPT_REASON_NONE indicates success. | |
| 140 void OnDownloadFileCreated( | |
| 141 int32 download_id, content::DownloadInterruptReason reason); | |
| 142 | |
| 143 // Called when the delegate has completed determining the download target. | |
| 144 // Arguments following |download_id| are as per | |
| 145 // content::DownloadTargetCallback. | |
| 146 void OnDownloadTargetDetermined( | |
| 147 int32 download_id, | |
| 148 const FilePath& target_path, | |
| 149 content::DownloadItem::TargetDisposition disposition, | |
| 150 content::DownloadDangerType danger_type, | |
| 151 const FilePath& intermediate_path); | |
| 152 | |
| 153 // Called when a download entry is committed to the persistent store. | 138 // Called when a download entry is committed to the persistent store. |
| 154 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); | 139 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); |
| 155 | 140 |
| 156 // Called when Save Page As entry is committed to the persistent store. | 141 // Called when Save Page As entry is committed to the persistent store. |
| 157 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); | 142 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); |
| 158 | 143 |
| 159 // Overridden from DownloadItemImplDelegate | 144 // Overridden from DownloadItemImplDelegate |
| 160 // (Note that |GetBrowserContext| are present in both interfaces.) | 145 // (Note that |GetBrowserContext| are present in both interfaces.) |
| 161 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE; | 146 virtual void DetermineDownloadTarget( |
| 147 DownloadItemImpl* item, const DownloadTargetCallback& callback) OVERRIDE; |
| 162 virtual void ReadyForDownloadCompletion( | 148 virtual void ReadyForDownloadCompletion( |
| 163 DownloadItemImpl* item, const base::Closure& complete_callback) OVERRIDE; | 149 DownloadItemImpl* item, const base::Closure& complete_callback) OVERRIDE; |
| 164 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; | 150 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; |
| 165 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; | 151 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; |
| 166 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; | 152 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; |
| 167 virtual void UpdatePersistence(DownloadItemImpl* download) OVERRIDE; | 153 virtual void UpdatePersistence(DownloadItemImpl* download) OVERRIDE; |
| 168 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; | 154 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; |
| 169 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; | 155 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; |
| 170 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; | 156 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; |
| 171 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; | 157 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; |
| 172 virtual void DownloadRenamedToIntermediateName( | 158 virtual void DownloadRenamedToIntermediateName( |
| 173 DownloadItemImpl* download) OVERRIDE; | 159 DownloadItemImpl* download) OVERRIDE; |
| 174 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; | 160 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; |
| 175 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; | 161 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; |
| 176 | 162 |
| 177 // Factory for creation of downloads items. | 163 // Factory for creation of downloads items. |
| 178 scoped_ptr<content::DownloadItemFactory> factory_; | 164 scoped_ptr<content::DownloadItemFactory> item_factory_; |
| 165 |
| 166 // Factory for the creation of download files. |
| 167 scoped_ptr<content::DownloadFileFactory> file_factory_; |
| 179 | 168 |
| 180 // |downloads_| is the owning set for all downloads known to the | 169 // |downloads_| is the owning set for all downloads known to the |
| 181 // DownloadManager. This includes downloads started by the user in | 170 // DownloadManager. This includes downloads started by the user in |
| 182 // this session, downloads initialized from the history system, and | 171 // this session, downloads initialized from the history system, and |
| 183 // "save page as" downloads. All other DownloadItem containers in | 172 // "save page as" downloads. All other DownloadItem containers in |
| 184 // the DownloadManager are maps; they do not own the DownloadItems. | 173 // the DownloadManager are maps; they do not own the DownloadItems. |
| 185 // Note that this is the only place (with any functional implications; | 174 // Note that this is the only place (with any functional implications; |
| 186 // see save_page_downloads_ below) that "save page as" downloads are | 175 // see save_page_downloads_ below) that "save page as" downloads are |
| 187 // kept, as the DownloadManager's only job is to hold onto those | 176 // kept, as the DownloadManager's only job is to hold onto those |
| 188 // until destruction. | 177 // until destruction. |
| 189 // | 178 // |
| 190 // |active_downloads_| is a map of all downloads that are currently being | 179 // |active_downloads_| is a map of all downloads that are currently being |
| 191 // processed. The key is the ID assigned by the DownloadFileManager, | 180 // processed. |
| 192 // which is unique for the current session. | |
| 193 // | 181 // |
| 194 // When a download is created through a user action, the corresponding | 182 // When a download is created through a user action, the corresponding |
| 195 // DownloadItem* is placed in |active_downloads_| and remains there until the | 183 // DownloadItem* is placed in |active_downloads_| and remains there until the |
| 196 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a | 184 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a |
| 197 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. | 185 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. |
| 198 // Downloads from past sessions read from a persisted state from the history | 186 // Downloads from past sessions read from a persisted state from the history |
| 199 // system are placed directly into |history_downloads_| since they have valid | 187 // system are placed directly into |history_downloads_| since they have valid |
| 200 // handles in the history system. | 188 // handles in the history system. |
| 201 | 189 |
| 202 DownloadMap downloads_; | 190 DownloadMap downloads_; |
| 203 DownloadMap active_downloads_; | 191 DownloadMap active_downloads_; |
| 204 | 192 |
| 205 int history_size_; | 193 int history_size_; |
| 206 | 194 |
| 207 // True if the download manager has been initialized and requires a shutdown. | 195 // True if the download manager has been initialized and requires a shutdown. |
| 208 bool shutdown_needed_; | 196 bool shutdown_needed_; |
| 209 | 197 |
| 210 // Observers that want to be notified of changes to the set of downloads. | 198 // Observers that want to be notified of changes to the set of downloads. |
| 211 ObserverList<Observer> observers_; | 199 ObserverList<Observer> observers_; |
| 212 | 200 |
| 213 // The current active browser context. | 201 // The current active browser context. |
| 214 content::BrowserContext* browser_context_; | 202 content::BrowserContext* browser_context_; |
| 215 | 203 |
| 216 // Non-owning pointer for handling file writing on the download_thread_. | |
| 217 DownloadFileManager* file_manager_; | |
| 218 | |
| 219 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 204 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
| 220 content::DownloadManagerDelegate* delegate_; | 205 content::DownloadManagerDelegate* delegate_; |
| 221 | 206 |
| 222 net::NetLog* net_log_; | 207 net::NetLog* net_log_; |
| 223 | 208 |
| 224 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); | 209 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); |
| 225 }; | 210 }; |
| 226 | 211 |
| 227 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 212 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| OLD | NEW |