blob: ee60a2c4549dbc7dea2021a49073991f0ab424ea [file] [log] [blame]
[email protected]0afff032012-01-06 20:55:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294//
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]6d0c9fb2011-08-22 19:26:038// active browser context in Chrome.
initial.commit09911bf2008-07-26 23:55:299//
initial.commit09911bf2008-07-26 23:55:2910// 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.commit09911bf2008-07-26 23:55:2914// 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]e582fdd2011-12-20 16:48:1727#ifndef CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
28#define CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_
initial.commit09911bf2008-07-26 23:55:2929
avi652869c2015-12-25 01:48:4530#include <stdint.h>
31
Lukasz Anforowicz76fc35782019-09-27 19:29:4032#include <memory>
[email protected]d2a8fb72010-01-21 05:31:4233#include <string>
initial.commit09911bf2008-07-26 23:55:2934#include <vector>
35
[email protected]89e6aa72012-03-12 22:51:3336#include "base/callback.h"
[email protected]57999812013-02-24 05:40:5237#include "base/files/file_path.h"
Patrick Monette643cdf62021-10-15 19:13:4238#include "base/task/sequenced_task_runner.h"
[email protected]abb522162013-06-28 01:54:1639#include "base/time/time.h"
Min Qineb78b7a2018-02-03 00:43:1640#include "components/download/public/common/download_interrupt_reasons.h"
Min Qina9f487872018-02-09 20:43:2341#include "components/download/public/common/download_item.h"
Takuto Ikutaaa3b796c2019-02-06 02:54:5642#include "components/download/public/common/download_stream.mojom-forward.h"
Min Qina904f3302018-02-13 23:33:3443#include "components/download/public/common/download_url_parameters.h"
Min Qin745839d2018-03-09 00:09:4044#include "components/download/public/common/input_stream.h"
Min Qin9c436ab2019-03-23 07:24:5145#include "components/download/public/common/simple_download_manager.h"
Min Qinfc77ad62018-02-15 18:09:5046#include "content/common/content_export.h"
[email protected]33c6d3f12011-09-04 00:00:5447#include "net/base/net_errors.h"
Marijn Kruisselbrink4d4aa992018-04-27 18:03:2748#include "services/network/public/cpp/shared_url_loader_factory.h"
Anton Bikineevf62d1bf2021-05-15 17:56:0749#include "third_party/abseil-cpp/absl/types/optional.h"
Joe DeBlasioa93563592019-05-10 15:17:1350#include "url/origin.h"
[email protected]b7f05882009-02-22 01:21:5651
[email protected]46072d42008-07-28 14:49:3552class GURL;
initial.commit09911bf2008-07-26 23:55:2953
[email protected]6d0c9fb2011-08-22 19:26:0354namespace content {
[email protected]c5a5c0842012-05-04 20:05:1455
[email protected]6d0c9fb2011-08-22 19:26:0356class BrowserContext;
[email protected]1bd0ef12011-10-20 05:24:1757class DownloadManagerDelegate;
Ovidio Ruiz-HenrĂ­quez814407a2022-02-10 21:55:3158class StoragePartitionConfig;
[email protected]6d0c9fb2011-08-22 19:26:0359
initial.commit09911bf2008-07-26 23:55:2960// Browser's download manager: manages all downloads and destination view.
Min Qin9c436ab2019-03-23 07:24:5161class CONTENT_EXPORT DownloadManager : public base::SupportsUserData::Data,
62 public download::SimpleDownloadManager {
initial.commit09911bf2008-07-26 23:55:2963 public:
dchenge933b3eb2014-10-21 11:44:0964 ~DownloadManager() override {}
[email protected]eba4a4d2013-05-29 02:18:0665
siggi2b05ad102017-07-21 18:57:5666 // 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]b488b5a52012-06-06 17:01:2870 // Sets/Gets the delegate for this DownloadManager. The delegate has to live
71 // past its Shutdown method being called (by the DownloadManager).
[email protected]b441a8492012-06-06 14:55:5772 virtual void SetDelegate(DownloadManagerDelegate* delegate) = 0;
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:0373 virtual DownloadManagerDelegate* GetDelegate() = 0;
[email protected]b441a8492012-06-06 14:55:5774
[email protected]3f789eaf2012-06-11 16:48:0275 // 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]5656f8a2011-11-17 16:12:5879 virtual void Shutdown() = 0;
[email protected]326a6a92010-09-10 20:21:1380
initial.commit09911bf2008-07-26 23:55:2981 // Interface to implement for observers that wish to be informed of changes
82 // to the DownloadManager's collection of downloads.
[email protected]8d128d62011-09-13 22:11:5783 class CONTENT_EXPORT Observer {
initial.commit09911bf2008-07-26 23:55:2984 public:
Min Qina9f487872018-02-09 20:43:2385 // 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]6a1a59a12012-07-26 17:26:4095
Chong Zhangcbbd75432018-03-09 18:32:1496 // 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
xingliud64fde652017-05-24 07:04:33102 // Called when the download manager has finished loading the data.
103 virtual void OnManagerInitialized() {}
104
[email protected]b0ab1d42010-02-24 19:29:28105 // Called when the DownloadManager is being destroyed to prevent Observers
106 // from calling back to a stale pointer.
[email protected]75e51b52012-02-04 16:57:54107 virtual void ManagerGoingDown(DownloadManager* manager) {}
[email protected]b0ab1d42010-02-24 19:29:28108
[email protected]135fd3b62009-12-16 01:07:08109 protected:
110 virtual ~Observer() {}
initial.commit09911bf2008-07-26 23:55:29111 };
112
msramekd9c162a2016-02-23 11:17:21113 // 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(
danakj710b4c02019-11-28 16:08:45118 const base::RepeatingCallback<bool(const GURL&)>& url_filter,
msramekd9c162a2016-02-23 11:17:21119 base::Time remove_begin,
120 base::Time remove_end) = 0;
initial.commit09911bf2008-07-26 23:55:29121
Min Qin9c436ab2019-03-23 07:24:51122 using SimpleDownloadManager::DownloadUrl;
Marijn Kruisselbrink929ce7df2019-09-27 23:57:36123 // 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 Qin93bed6ae2018-02-14 21:53:22127 virtual void DownloadUrl(
128 std::unique_ptr<download::DownloadUrlParameters> parameters,
Marijn Kruisselbrink4d4aa992018-04-27 18:03:27129 scoped_refptr<network::SharedURLLoaderFactory>
130 blob_url_loader_factory) = 0;
Min Qin93bed6ae2018-02-14 21:53:22131
initial.commit09911bf2008-07-26 23:55:29132 // Allow objects to observe the download creation process.
[email protected]5656f8a2011-11-17 16:12:58133 virtual void AddObserver(Observer* observer) = 0;
initial.commit09911bf2008-07-26 23:55:29134
135 // Remove a download observer from ourself.
[email protected]5656f8a2011-11-17 16:12:58136 virtual void RemoveObserver(Observer* observer) = 0;
initial.commit09911bf2008-07-26 23:55:29137
[email protected]2588ea9d2011-08-22 20:59:53138 // Called by the embedder, after creating the download manager, to let it know
139 // about downloads from previous runs of the browser.
Min Qina9f487872018-02-09 20:43:23140 virtual download::DownloadItem* CreateDownloadItem(
asankaeef62b02016-03-14 21:23:11141 const std::string& guid,
avi652869c2015-12-25 01:48:45142 uint32_t id,
[email protected]c42de732013-02-16 06:26:31143 const base::FilePath& current_path,
144 const base::FilePath& target_path,
[email protected]876774692013-02-03 17:20:15145 const std::vector<GURL>& url_chain,
[email protected]3d95e542012-11-20 00:52:08146 const GURL& referrer_url,
Ovidio Henriquez86dd0e42022-02-11 14:42:15147 const StoragePartitionConfig& storage_partition_config,
asanka038d58d2016-04-14 00:29:28148 const GURL& tab_url,
149 const GURL& tab_referrer_url,
Anton Bikineevf62d1bf2021-05-15 17:56:07150 const absl::optional<url::Origin>& request_initiator,
[email protected]0e648562014-06-12 19:39:45151 const std::string& mime_type,
152 const std::string& original_mime_type,
shaktisahu60eb97f2017-03-03 08:53:23153 base::Time start_time,
154 base::Time end_time,
[email protected]56cd5942013-08-08 23:53:21155 const std::string& etag,
156 const std::string& last_modified,
avi652869c2015-12-25 01:48:45157 int64_t received_bytes,
158 int64_t total_bytes,
asanka4350f6a2016-03-15 02:40:57159 const std::string& hash,
Min Qina9f487872018-02-09 20:43:23160 download::DownloadItem::DownloadState state,
Min Qin0ca8e1ee2018-01-31 00:49:35161 download::DownloadDangerType danger_type,
Min Qineb78b7a2018-02-03 00:43:16162 download::DownloadInterruptReason interrupt_reason,
qinmin23b2857f2017-02-25 00:24:28163 bool opened,
shaktisahu60eb97f2017-03-03 08:53:23164 base::Time last_access_time,
shaktisahufd49a3e2017-03-30 18:35:10165 bool transient,
Alice Gong909513f62021-08-05 20:39:26166 const std::vector<download::DownloadItem::ReceivedSlice>& received_slices,
167 const download::DownloadItemRerouteInfo& reroute_info) = 0;
initial.commit09911bf2008-07-26 23:55:29168
Joy Ming13a84562017-12-02 00:42:17169 // 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;
xingliud64fde652017-05-24 07:04:33180
181 // Returns if the manager has been initialized and loaded all the data.
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:03182 virtual bool IsManagerInitialized() = 0;
xingliud64fde652017-05-24 07:04:33183
initial.commit09911bf2008-07-26 23:55:29184 // The number of in progress (including paused) downloads.
[email protected]fd6ddb82012-11-27 00:38:43185 // 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 Gadanid726e1e2019-05-08 16:20:03187 virtual int InProgressCount() = 0;
initial.commit09911bf2008-07-26 23:55:29188
[email protected]422a7d12013-10-21 12:10:42189 // 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 Gadanid726e1e2019-05-08 16:20:03193 virtual int NonMaliciousInProgressCount() = 0;
[email protected]422a7d12013-10-21 12:10:42194
Lucas Furukawa Gadanid726e1e2019-05-08 16:20:03195 virtual BrowserContext* GetBrowserContext() = 0;
[email protected]d3b12902010-08-16 23:39:42196
Min Qin5ca2c5e2018-06-01 19:39:24197 // 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]ba7895d2012-06-22 19:02:52202 // Get the download item for |id| if present, no matter what type of download
203 // it is or state it's in.
asankaeef62b02016-03-14 21:23:11204 // DEPRECATED: Don't add new callers for GetDownload(uint32_t). Instead keep
Min Qina9f487872018-02-09 20:43:23205 // 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;
asankaeef62b02016-03-14 21:23:11209
Min Qin75d9ad12018-11-10 01:02:49210 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Ă­quez814407a2022-02-10 21:55:31214
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 Henriquez86dd0e42022-02-11 14:42:15223
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.commit09911bf2008-07-26 23:55:29233};
234
[email protected]e582fdd2011-12-20 16:48:17235} // namespace content
236
237#endif // CONTENT_PUBLIC_BROWSER_DOWNLOAD_MANAGER_H_