| 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::DownloadId StartDownload( | 59 virtual content::DownloadId 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 // Does nothing if |download_id| is not an active download. | 106 // Does nothing if |download_id| is not an active download. |
| 106 void MaybeCompleteDownloadById(int download_id); | 107 void MaybeCompleteDownloadById(int download_id); |
| 107 | 108 |
| 108 // Determine if the download is ready for completion, i.e. has had | 109 // Determine if the download is ready for completion, i.e. has had |
| 109 // all data saved, and completed the filename determination and | 110 // all data saved, and completed the filename determination and |
| 110 // history insertion. | 111 // history insertion. |
| 111 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); | 112 bool IsDownloadReadyForCompletion(DownloadItemImpl* download); |
| 112 | 113 |
| 113 // Show the download in the browser. | 114 // Show the download in the browser. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 135 // Debugging routine to confirm relationship between below | 136 // Debugging routine to confirm relationship between below |
| 136 // containers; no-op if NDEBUG. | 137 // containers; no-op if NDEBUG. |
| 137 void AssertContainersConsistent() const; | 138 void AssertContainersConsistent() const; |
| 138 | 139 |
| 139 // Add a DownloadItem to history_downloads_. | 140 // Add a DownloadItem to history_downloads_. |
| 140 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); | 141 void AddDownloadItemToHistory(DownloadItemImpl* item, int64 db_handle); |
| 141 | 142 |
| 142 // Remove from internal maps. | 143 // Remove from internal maps. |
| 143 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); | 144 int RemoveDownloadItems(const DownloadItemImplVector& pending_deletes); |
| 144 | 145 |
| 145 // Called in response to our request to the DownloadFileManager to | |
| 146 // create a DownloadFile. A |reason| of | |
| 147 // content::DOWNLOAD_INTERRUPT_REASON_NONE indicates success. | |
| 148 void OnDownloadFileCreated( | |
| 149 int32 download_id, content::DownloadInterruptReason reason); | |
| 150 | |
| 151 // Called when the delegate has completed determining the download target. | |
| 152 // Arguments following |download_id| are as per | |
| 153 // content::DownloadTargetCallback. | |
| 154 void OnDownloadTargetDetermined( | |
| 155 int32 download_id, | |
| 156 const FilePath& target_path, | |
| 157 content::DownloadItem::TargetDisposition disposition, | |
| 158 content::DownloadDangerType danger_type, | |
| 159 const FilePath& intermediate_path); | |
| 160 | |
| 161 // Called when a download entry is committed to the persistent store. | 146 // Called when a download entry is committed to the persistent store. |
| 162 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); | 147 void OnDownloadItemAddedToPersistentStore(DownloadItemImpl* item); |
| 163 | 148 |
| 164 // Called when Save Page As entry is committed to the persistent store. | 149 // Called when Save Page As entry is committed to the persistent store. |
| 165 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); | 150 void OnSavePageItemAddedToPersistentStore(DownloadItemImpl* item); |
| 166 | 151 |
| 167 // Overridden from DownloadItemImplDelegate | 152 // Overridden from DownloadItemImplDelegate |
| 168 // (Note that |GetBrowserContext| are present in both interfaces.) | 153 // (Note that |GetBrowserContext| are present in both interfaces.) |
| 169 virtual DownloadFileManager* GetDownloadFileManager() OVERRIDE; | 154 virtual void DetermineDownloadTarget( |
| 155 DownloadItemImpl* item, DownloadTargetCallback callback) OVERRIDE; |
| 170 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; | 156 virtual bool ShouldOpenDownload(DownloadItemImpl* item) OVERRIDE; |
| 171 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; | 157 virtual bool ShouldOpenFileBasedOnExtension(const FilePath& path) OVERRIDE; |
| 172 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; | 158 virtual void CheckForFileRemoval(DownloadItemImpl* download_item) OVERRIDE; |
| 173 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; | 159 virtual void MaybeCompleteDownload(DownloadItemImpl* download) OVERRIDE; |
| 174 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; | 160 virtual void DownloadStopped(DownloadItemImpl* download) OVERRIDE; |
| 175 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; | 161 virtual void DownloadCompleted(DownloadItemImpl* download) OVERRIDE; |
| 176 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; | 162 virtual void DownloadOpened(DownloadItemImpl* download) OVERRIDE; |
| 177 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; | 163 virtual void DownloadRemoved(DownloadItemImpl* download) OVERRIDE; |
| 178 virtual void DownloadRenamedToIntermediateName( | 164 virtual void DownloadRenamedToIntermediateName( |
| 179 DownloadItemImpl* download) OVERRIDE; | 165 DownloadItemImpl* download) OVERRIDE; |
| 180 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; | 166 virtual void DownloadRenamedToFinalName(DownloadItemImpl* download) OVERRIDE; |
| 181 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; | 167 virtual void AssertStateConsistent(DownloadItemImpl* download) const OVERRIDE; |
| 182 | 168 |
| 183 // Factory for creation of downloads items. | 169 // Factory for creation of downloads items. |
| 184 scoped_ptr<content::DownloadItemFactory> factory_; | 170 scoped_ptr<content::DownloadItemFactory> item_factory_; |
| 171 |
| 172 // Factory for the creation of download files. |
| 173 scoped_ptr<content::DownloadFileFactory> file_factory_; |
| 185 | 174 |
| 186 // |downloads_| is the owning set for all downloads known to the | 175 // |downloads_| is the owning set for all downloads known to the |
| 187 // DownloadManager. This includes downloads started by the user in | 176 // DownloadManager. This includes downloads started by the user in |
| 188 // this session, downloads initialized from the history system, and | 177 // this session, downloads initialized from the history system, and |
| 189 // "save page as" downloads. All other DownloadItem containers in | 178 // "save page as" downloads. All other DownloadItem containers in |
| 190 // the DownloadManager are maps; they do not own the DownloadItems. | 179 // the DownloadManager are maps; they do not own the DownloadItems. |
| 191 // Note that this is the only place (with any functional implications; | 180 // Note that this is the only place (with any functional implications; |
| 192 // see save_page_downloads_ below) that "save page as" downloads are | 181 // see save_page_downloads_ below) that "save page as" downloads are |
| 193 // kept, as the DownloadManager's only job is to hold onto those | 182 // kept, as the DownloadManager's only job is to hold onto those |
| 194 // until destruction. | 183 // until destruction. |
| 195 // | 184 // |
| 196 // |active_downloads_| is a map of all downloads that are currently being | 185 // |active_downloads_| is a map of all downloads that are currently being |
| 197 // processed. The key is the ID assigned by the DownloadFileManager, | 186 // processed. |
| 198 // which is unique for the current session. | |
| 199 // | 187 // |
| 200 // When a download is created through a user action, the corresponding | 188 // When a download is created through a user action, the corresponding |
| 201 // DownloadItem* is placed in |active_downloads_| and remains there until the | 189 // DownloadItem* is placed in |active_downloads_| and remains there until the |
| 202 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a | 190 // download is in a terminal state (COMPLETE or CANCELLED). Once it has a |
| 203 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. | 191 // valid handle, the DownloadItem* is placed in the |history_downloads_| map. |
| 204 // Downloads from past sessions read from a persisted state from the history | 192 // Downloads from past sessions read from a persisted state from the history |
| 205 // system are placed directly into |history_downloads_| since they have valid | 193 // system are placed directly into |history_downloads_| since they have valid |
| 206 // handles in the history system. | 194 // handles in the history system. |
| 207 | 195 |
| 208 DownloadMap downloads_; | 196 DownloadMap downloads_; |
| 209 DownloadMap active_downloads_; | 197 DownloadMap active_downloads_; |
| 210 | 198 |
| 211 int history_size_; | 199 int history_size_; |
| 212 | 200 |
| 213 // True if the download manager has been initialized and requires a shutdown. | 201 // True if the download manager has been initialized and requires a shutdown. |
| 214 bool shutdown_needed_; | 202 bool shutdown_needed_; |
| 215 | 203 |
| 216 // Observers that want to be notified of changes to the set of downloads. | 204 // Observers that want to be notified of changes to the set of downloads. |
| 217 ObserverList<Observer> observers_; | 205 ObserverList<Observer> observers_; |
| 218 | 206 |
| 219 // The current active browser context. | 207 // The current active browser context. |
| 220 content::BrowserContext* browser_context_; | 208 content::BrowserContext* browser_context_; |
| 221 | 209 |
| 222 // Non-owning pointer for handling file writing on the download_thread_. | |
| 223 DownloadFileManager* file_manager_; | |
| 224 | |
| 225 // Allows an embedder to control behavior. Guaranteed to outlive this object. | 210 // Allows an embedder to control behavior. Guaranteed to outlive this object. |
| 226 content::DownloadManagerDelegate* delegate_; | 211 content::DownloadManagerDelegate* delegate_; |
| 227 | 212 |
| 228 net::NetLog* net_log_; | 213 net::NetLog* net_log_; |
| 229 | 214 |
| 230 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); | 215 DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl); |
| 231 }; | 216 }; |
| 232 | 217 |
| 233 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ | 218 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_ |
| OLD | NEW |