[email protected] | 0afff03 | 2012-01-06 20:55:00 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | // |
| 5 | // The DownloadManager object manages the process of downloading, including |
| 6 | // updates to the history system and providing the information for displaying |
| 7 | // the downloads view in the Destinations tab. There is one DownloadManager per |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 8 | // active browser context in Chrome. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 9 | // |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | // Download observers: |
| 11 | // Objects that are interested in notifications about new downloads, or progress |
| 12 | // updates for a given download must implement one of the download observer |
| 13 | // interfaces: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | // DownloadManager::Observer: |
| 15 | // - allows observers, primarily views, to be notified when changes to the |
| 16 | // set of all downloads (such as new downloads, or deletes) occur |
| 17 | // Use AddObserver() / RemoveObserver() on the appropriate download object to |
| 18 | // receive state updates. |
| 19 | // |
| 20 | // Download state persistence: |
| 21 | // The DownloadManager uses the history service for storing persistent |
| 22 | // information about the state of all downloads. The history system maintains a |
| 23 | // separate table for this called 'downloads'. At the point that the |
| 24 | // DownloadManager is constructed, we query the history service for the state of |
| 25 | // all persisted downloads. |
| 26 | |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 27 | #ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ |
| 28 | #define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
avi | 652869c | 2015-12-25 01:48:45 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | |
Lukasz Anforowicz | 76fc3578 | 2019-09-27 19:29:40 | [diff] [blame] | 32 | #include <memory> |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 33 | #include <string> |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 34 | #include <vector> |
| 35 | |
[email protected] | 89e6aa7 | 2012-03-12 22:51:33 | [diff] [blame] | 36 | #include "base/callback.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 37 | #include "base/files/file_path.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 38 | #include "base/task/sequenced_task_runner.h" |
[email protected] | abb52216 | 2013-06-28 01:54:16 | [diff] [blame] | 39 | #include "base/time/time.h" |
Min Qin | eb78b7a | 2018-02-03 00:43:16 | [diff] [blame] | 40 | #include "components/download/public/common/download_interrupt_reasons.h" |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 41 | #include "components/download/public/common/download_item.h" |
Takuto Ikuta | aa3b796c | 2019-02-06 02:54:56 | [diff] [blame] | 42 | #include "components/download/public/common/download_stream.mojom-forward.h" |
Min Qin | a904f330 | 2018-02-13 23:33:34 | [diff] [blame] | 43 | #include "components/download/public/common/download_url_parameters.h" |
Min Qin | 745839d | 2018-03-09 00:09:40 | [diff] [blame] | 44 | #include "components/download/public/common/input_stream.h" |
Min Qin | 9c436ab | 2019-03-23 07:24:51 | [diff] [blame] | 45 | #include "components/download/public/common/simple_download_manager.h" |
Min Qin | fc77ad6 | 2018-02-15 18:09:50 | [diff] [blame] | 46 | #include "content/common/content_export.h" |
[email protected] | 33c6d3f1 | 2011-09-04 00:00:54 | [diff] [blame] | 47 | #include "net/base/net_errors.h" |
Marijn Kruisselbrink | 4d4aa99 | 2018-04-27 18:03:27 | [diff] [blame] | 48 | #include "services/network/public/cpp/shared_url_loader_factory.h" |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 49 | #include "third_party/abseil-cpp/absl/types/optional.h" |
Joe DeBlasio | a9356359 | 2019-05-10 15:17:13 | [diff] [blame] | 50 | #include "url/origin.h" |
[email protected] | b7f0588 | 2009-02-22 01:21:56 | [diff] [blame] | 51 | |
[email protected] | 46072d4 | 2008-07-28 14:49:35 | [diff] [blame] | 52 | class GURL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 54 | namespace content { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 55 | |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 56 | class BrowserContext; |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 57 | class DownloadManagerDelegate; |
Ovidio Ruiz-HenrĂquez | 814407a | 2022-02-10 21:55:31 | [diff] [blame] | 58 | class StoragePartitionConfig; |
[email protected] | 6d0c9fb | 2011-08-22 19:26:03 | [diff] [blame] | 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | // Browser's download manager: manages all downloads and destination view. |
Min Qin | 9c436ab | 2019-03-23 07:24:51 | [diff] [blame] | 61 | class CONTENT_EXPORT DownloadManager : public base::SupportsUserData::Data, |
| 62 | public download::SimpleDownloadManager { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | public: |
dcheng | e933b3eb | 2014-10-21 11:44:09 | [diff] [blame] | 64 | ~DownloadManager() override {} |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 65 | |
siggi | 2b05ad10 | 2017-07-21 18:57:56 | [diff] [blame] | 66 | // Returns the task runner that's used for all download-related blocking |
| 67 | // tasks, such as file IO. |
| 68 | static scoped_refptr<base::SequencedTaskRunner> GetTaskRunner(); |
| 69 | |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 70 | // Sets/Gets the delegate for this DownloadManager. The delegate has to live |
| 71 | // past its Shutdown method being called (by the DownloadManager). |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 72 | virtual void SetDelegate(DownloadManagerDelegate* delegate) = 0; |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 73 | virtual DownloadManagerDelegate* GetDelegate() = 0; |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 74 | |
[email protected] | 3f789eaf | 2012-06-11 16:48:02 | [diff] [blame] | 75 | // Shutdown the download manager. Content calls this when BrowserContext is |
| 76 | // being destructed. If the embedder needs this to be called earlier, it can |
| 77 | // call it. In that case, the delegate's Shutdown() method will only be called |
| 78 | // once. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 79 | virtual void Shutdown() = 0; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 80 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 81 | // Interface to implement for observers that wish to be informed of changes |
| 82 | // to the DownloadManager's collection of downloads. |
[email protected] | 8d128d6 | 2011-09-13 22:11:57 | [diff] [blame] | 83 | class CONTENT_EXPORT Observer { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | public: |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 85 | // A download::DownloadItem was created. This item may be visible before the |
| 86 | // filename is determined; in this case the return value of |
| 87 | // GetTargetFileName() will be null. This method may be called an arbitrary |
| 88 | // number of times, e.g. when loading history on startup. As a result, |
| 89 | // consumers should avoid doing large amounts of work in |
| 90 | // OnDownloadCreated(). TODO(<whoever>): When we've fully specified the |
| 91 | // possible states of the download::DownloadItem in download_item.h, we |
| 92 | // should remove the caveat above. |
| 93 | virtual void OnDownloadCreated(DownloadManager* manager, |
| 94 | download::DownloadItem* item) {} |
[email protected] | 6a1a59a1 | 2012-07-26 17:26:40 | [diff] [blame] | 95 | |
Chong Zhang | cbbd7543 | 2018-03-09 18:32:14 | [diff] [blame] | 96 | // Called when the download manager intercepted a download navigation but |
| 97 | // didn't create the download item. Possible reasons: |
| 98 | // 1. |delegate| is null. |
| 99 | // 2. |delegate| doesn't allow the download. |
| 100 | virtual void OnDownloadDropped(DownloadManager* manager) {} |
| 101 | |
xingliu | d64fde65 | 2017-05-24 07:04:33 | [diff] [blame] | 102 | // Called when the download manager has finished loading the data. |
| 103 | virtual void OnManagerInitialized() {} |
| 104 | |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 105 | // Called when the DownloadManager is being destroyed to prevent Observers |
| 106 | // from calling back to a stale pointer. |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 107 | virtual void ManagerGoingDown(DownloadManager* manager) {} |
[email protected] | b0ab1d4 | 2010-02-24 19:29:28 | [diff] [blame] | 108 | |
[email protected] | 135fd3b6 | 2009-12-16 01:07:08 | [diff] [blame] | 109 | protected: |
| 110 | virtual ~Observer() {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 111 | }; |
| 112 | |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 113 | // Remove downloads whose URLs match the |url_filter| and are within |
| 114 | // the given time constraints - after remove_begin (inclusive) and before |
| 115 | // remove_end (exclusive). You may pass in null Time values to do an unbounded |
| 116 | // delete in either direction. |
| 117 | virtual int RemoveDownloadsByURLAndTime( |
danakj | 710b4c0 | 2019-11-28 16:08:45 | [diff] [blame] | 118 | const base::RepeatingCallback<bool(const GURL&)>& url_filter, |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 119 | base::Time remove_begin, |
| 120 | base::Time remove_end) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 121 | |
Min Qin | 9c436ab | 2019-03-23 07:24:51 | [diff] [blame] | 122 | using SimpleDownloadManager::DownloadUrl; |
Marijn Kruisselbrink | 929ce7df | 2019-09-27 23:57:36 | [diff] [blame] | 123 | // For downloads of blob URLs, the caller can pass a URLLoaderFactory to |
| 124 | // use to load the Blob URL. If none is specified and the blob URL cannot be |
| 125 | // mapped to a blob by the time the download request starts, then the download |
| 126 | // will fail. |
Min Qin | 93bed6ae | 2018-02-14 21:53:22 | [diff] [blame] | 127 | virtual void DownloadUrl( |
| 128 | std::unique_ptr<download::DownloadUrlParameters> parameters, |
Marijn Kruisselbrink | 4d4aa99 | 2018-04-27 18:03:27 | [diff] [blame] | 129 | scoped_refptr<network::SharedURLLoaderFactory> |
| 130 | blob_url_loader_factory) = 0; |
Min Qin | 93bed6ae | 2018-02-14 21:53:22 | [diff] [blame] | 131 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 132 | // Allow objects to observe the download creation process. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 133 | virtual void AddObserver(Observer* observer) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | |
| 135 | // Remove a download observer from ourself. |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 136 | virtual void RemoveObserver(Observer* observer) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 137 | |
[email protected] | 2588ea9d | 2011-08-22 20:59:53 | [diff] [blame] | 138 | // Called by the embedder, after creating the download manager, to let it know |
| 139 | // about downloads from previous runs of the browser. |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 140 | virtual download::DownloadItem* CreateDownloadItem( |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 141 | const std::string& guid, |
avi | 652869c | 2015-12-25 01:48:45 | [diff] [blame] | 142 | uint32_t id, |
[email protected] | c42de73 | 2013-02-16 06:26:31 | [diff] [blame] | 143 | const base::FilePath& current_path, |
| 144 | const base::FilePath& target_path, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 145 | const std::vector<GURL>& url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 146 | const GURL& referrer_url, |
Ovidio Henriquez | 86dd0e4 | 2022-02-11 14:42:15 | [diff] [blame] | 147 | const StoragePartitionConfig& storage_partition_config, |
asanka | 038d58d | 2016-04-14 00:29:28 | [diff] [blame] | 148 | const GURL& tab_url, |
| 149 | const GURL& tab_referrer_url, |
Anton Bikineev | f62d1bf | 2021-05-15 17:56:07 | [diff] [blame] | 150 | const absl::optional<url::Origin>& request_initiator, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 151 | const std::string& mime_type, |
| 152 | const std::string& original_mime_type, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 153 | base::Time start_time, |
| 154 | base::Time end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 155 | const std::string& etag, |
| 156 | const std::string& last_modified, |
avi | 652869c | 2015-12-25 01:48:45 | [diff] [blame] | 157 | int64_t received_bytes, |
| 158 | int64_t total_bytes, |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 159 | const std::string& hash, |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 160 | download::DownloadItem::DownloadState state, |
Min Qin | 0ca8e1ee | 2018-01-31 00:49:35 | [diff] [blame] | 161 | download::DownloadDangerType danger_type, |
Min Qin | eb78b7a | 2018-02-03 00:43:16 | [diff] [blame] | 162 | download::DownloadInterruptReason interrupt_reason, |
qinmin | 23b2857f | 2017-02-25 00:24:28 | [diff] [blame] | 163 | bool opened, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 164 | base::Time last_access_time, |
shaktisahu | fd49a3e | 2017-03-30 18:35:10 | [diff] [blame] | 165 | bool transient, |
Alice Gong | 909513f6 | 2021-08-05 20:39:26 | [diff] [blame] | 166 | const std::vector<download::DownloadItem::ReceivedSlice>& received_slices, |
| 167 | const download::DownloadItemRerouteInfo& reroute_info) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 168 | |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 169 | // Enum to describe which dependency was initialized in PostInitialization. |
| 170 | enum DownloadInitializationDependency { |
| 171 | DOWNLOAD_INITIALIZATION_DEPENDENCY_NONE, |
| 172 | DOWNLOAD_INITIALIZATION_DEPENDENCY_HISTORY_DB, |
| 173 | DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE, |
| 174 | }; |
| 175 | |
| 176 | // Called when download manager has loaded all the data, once when the history |
| 177 | // db is initialized and once when the in-progress cache is initialized. |
| 178 | virtual void PostInitialization( |
| 179 | DownloadInitializationDependency dependency) = 0; |
xingliu | d64fde65 | 2017-05-24 07:04:33 | [diff] [blame] | 180 | |
| 181 | // Returns if the manager has been initialized and loaded all the data. |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 182 | virtual bool IsManagerInitialized() = 0; |
xingliu | d64fde65 | 2017-05-24 07:04:33 | [diff] [blame] | 183 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | // The number of in progress (including paused) downloads. |
[email protected] | fd6ddb8 | 2012-11-27 00:38:43 | [diff] [blame] | 185 | // Performance note: this loops over all items. If profiling finds that this |
| 186 | // is too slow, use an AllDownloadItemNotifier to count in-progress items. |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 187 | virtual int InProgressCount() = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 188 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 189 | // The number of in progress (including paused) downloads. |
| 190 | // Performance note: this loops over all items. If profiling finds that this |
| 191 | // is too slow, use an AllDownloadItemNotifier to count in-progress items. |
| 192 | // This excludes downloads that are marked as malicious. |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 193 | virtual int NonMaliciousInProgressCount() = 0; |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 194 | |
Lucas Furukawa Gadani | d726e1e | 2019-05-08 16:20:03 | [diff] [blame] | 195 | virtual BrowserContext* GetBrowserContext() = 0; |
[email protected] | d3b1290 | 2010-08-16 23:39:42 | [diff] [blame] | 196 | |
Min Qin | 5ca2c5e | 2018-06-01 19:39:24 | [diff] [blame] | 197 | // Called when download history query completes. Call |
| 198 | // |load_history_downloads_cb| to load all the history downloads. |
| 199 | virtual void OnHistoryQueryComplete( |
| 200 | base::OnceClosure load_history_downloads_cb) = 0; |
| 201 | |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 202 | // Get the download item for |id| if present, no matter what type of download |
| 203 | // it is or state it's in. |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 204 | // DEPRECATED: Don't add new callers for GetDownload(uint32_t). Instead keep |
Min Qin | a9f48787 | 2018-02-09 20:43:23 | [diff] [blame] | 205 | // track of the GUID and use GetDownloadByGuid(), or observe the |
| 206 | // download::DownloadItem if you need to keep track of a specific download. |
| 207 | // (http://crbug.com/593020) |
| 208 | virtual download::DownloadItem* GetDownload(uint32_t id) = 0; |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 209 | |
Min Qin | 75d9ad1 | 2018-11-10 01:02:49 | [diff] [blame] | 210 | using GetNextIdCallback = base::OnceCallback<void(uint32_t)>; |
| 211 | // Called to get an ID for a new download. |callback| may be called |
| 212 | // synchronously. |
| 213 | virtual void GetNextId(GetNextIdCallback callback) = 0; |
Ovidio Ruiz-HenrĂquez | 814407a | 2022-02-10 21:55:31 | [diff] [blame] | 214 | |
| 215 | // Called to convert between a StoragePartitionConfig and a serialized |
| 216 | // proto::EmbedderDownloadData. The serialized proto::EmbedderDownloadData is |
| 217 | // written to the downloads database. |
| 218 | virtual std::string StoragePartitionConfigToSerializedEmbedderDownloadData( |
| 219 | const StoragePartitionConfig& storage_partition_config) = 0; |
| 220 | virtual StoragePartitionConfig |
| 221 | SerializedEmbedderDownloadDataToStoragePartitionConfig( |
| 222 | const std::string& serialized_embedder_download_data) = 0; |
Ovidio Henriquez | 86dd0e4 | 2022-02-11 14:42:15 | [diff] [blame] | 223 | |
| 224 | // Called to get the proper StoragePartitionConfig that corresponds to the |
| 225 | // given site URL. This method is used in DownloadHistory to convert download |
| 226 | // history entries containing just site URLs to DownloadItem objects that no |
| 227 | // longer use site URL. The download history database is not able to migrate |
| 228 | // away from site URL because it is shared by all platforms, therefore it |
| 229 | // cannot reference StoragePartitionConfig since it is a content class. |
| 230 | // See https://crbug.com/1258193 for more details. |
| 231 | virtual StoragePartitionConfig GetStoragePartitionConfigForSiteUrl( |
| 232 | const GURL& site_url) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 233 | }; |
| 234 | |
[email protected] | e582fdd | 2011-12-20 16:48:17 | [diff] [blame] | 235 | } // namespace content |
| 236 | |
| 237 | #endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_ |