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 <stdint.h>
9 
10 #include <memory>
11 #include <set>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 
16 #include "base/callback_forward.h"
17 #include "base/macros.h"
18 #include "base/memory/ref_counted.h"
19 #include "base/memory/weak_ptr.h"
20 #include "base/observer_list.h"
21 #include "base/optional.h"
22 #include "base/sequenced_task_runner_helpers.h"
23 #include "base/synchronization/lock.h"
24 #include "build/build_config.h"
25 #include "components/download/public/common/download_item_impl_delegate.h"
26 #include "components/download/public/common/download_job.h"
27 #include "components/download/public/common/download_url_parameters.h"
28 #include "components/download/public/common/in_progress_download_manager.h"
29 #include "components/download/public/common/url_download_handler.h"
30 #include "content/browser/loader/navigation_url_loader.h"
31 #include "content/common/content_export.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/download_manager.h"
34 #include "content/public/browser/download_manager_delegate.h"
35 #include "content/public/browser/ssl_status.h"
36 #include "mojo/public/cpp/system/data_pipe.h"
37 #include "services/metrics/public/cpp/ukm_source_id.h"
38 #include "services/network/public/mojom/url_loader.mojom.h"
39 #include "url/origin.h"
40 
41 namespace download {
42 class DownloadFileFactory;
43 class DownloadItemFactory;
44 class DownloadItemImpl;
45 }
46 
47 // These values are persisted to logs (Download.InitiatedByWindowOpener).
48 // Entries should not be renumbered and numeric values should never be reused.
49 // Openee is the tab over which the download is initiated.
50 // Opener is the tab that opened the openee tab and initiated a download on it.
51 enum class InitiatedByWindowOpenerType {
52   kSameOrigin = 0,
53   // Openee and opener are cross origin.
54   kCrossOrigin = 1,
55   // Openee and opener are cross origin but same site (i.e. same eTLD+1).
56   kSameSite = 2,
57   // Either the openee or the opener is not HTTP or HTTPS, e.g. about:blank.
58   kNonHTTPOrHTTPS = 3,
59   kMaxValue = kNonHTTPOrHTTPS
60 };
61 
62 namespace content {
63 class CONTENT_EXPORT DownloadManagerImpl
64     : public DownloadManager,
65       public download::InProgressDownloadManager::Delegate,
66       private download::DownloadItemImplDelegate {
67  public:
68   using DownloadItemImplCreated =
69       base::OnceCallback<void(download::DownloadItemImpl*)>;
70 
71   // Caller guarantees that |net_log| will remain valid
72   // for the lifetime of DownloadManagerImpl (until Shutdown() is called).
73   explicit DownloadManagerImpl(BrowserContext* browser_context);
74   ~DownloadManagerImpl() override;
75 
76   // Implementation functions (not part of the DownloadManager interface).
77 
78   // Creates a download item for the SavePackage system.
79   // Must be called on the UI thread.  Note that the DownloadManager
80   // retains ownership.
81   void CreateSavePackageDownloadItem(
82       const base::FilePath& main_file_path,
83       const GURL& page_url,
84       const std::string& mime_type,
85       int render_process_id,
86       int render_frame_id,
87       download::DownloadJob::CancelRequestCallback cancel_request_callback,
88       DownloadItemImplCreated item_created);
89 
90   // DownloadManager functions.
91   void SetDelegate(DownloadManagerDelegate* delegate) override;
92   DownloadManagerDelegate* GetDelegate() override;
93   void Shutdown() override;
94   void GetAllDownloads(
95       download::SimpleDownloadManager::DownloadVector* result) override;
96   void GetUninitializedActiveDownloadsIfAny(
97       download::SimpleDownloadManager::DownloadVector* result) override;
98   int RemoveDownloadsByURLAndTime(
99       const base::RepeatingCallback<bool(const GURL&)>& url_filter,
100       base::Time remove_begin,
101       base::Time remove_end) override;
102   bool CanDownload(download::DownloadUrlParameters* parameters) override;
103   void DownloadUrl(
104       std::unique_ptr<download::DownloadUrlParameters> parameters) override;
105   void DownloadUrl(std::unique_ptr<download::DownloadUrlParameters> params,
106                    scoped_refptr<network::SharedURLLoaderFactory>
107                        blob_url_loader_factory) override;
108   void AddObserver(Observer* observer) override;
109   void RemoveObserver(Observer* observer) override;
110   download::DownloadItem* CreateDownloadItem(
111       const std::string& guid,
112       uint32_t id,
113       const base::FilePath& current_path,
114       const base::FilePath& target_path,
115       const std::vector<GURL>& url_chain,
116       const GURL& referrer_url,
117       const GURL& site_url,
118       const GURL& tab_url,
119       const GURL& tab_refererr_url,
120       const base::Optional<url::Origin>& request_initiator,
121       const std::string& mime_type,
122       const std::string& original_mime_type,
123       base::Time start_time,
124       base::Time end_time,
125       const std::string& etag,
126       const std::string& last_modified,
127       int64_t received_bytes,
128       int64_t total_bytes,
129       const std::string& hash,
130       download::DownloadItem::DownloadState state,
131       download::DownloadDangerType danger_type,
132       download::DownloadInterruptReason interrupt_reason,
133       bool opened,
134       base::Time last_access_time,
135       bool transient,
136       const std::vector<download::DownloadItem::ReceivedSlice>& received_slices)
137       override;
138   void PostInitialization(DownloadInitializationDependency dependency) override;
139   bool IsManagerInitialized() override;
140   int InProgressCount() override;
141   int NonMaliciousInProgressCount() override;
142   BrowserContext* GetBrowserContext() override;
143   void CheckForHistoryFilesRemoval() override;
144   void OnHistoryQueryComplete(
145       base::OnceClosure load_history_downloads_cb) override;
146   download::DownloadItem* GetDownload(uint32_t id) override;
147   download::DownloadItem* GetDownloadByGuid(const std::string& guid) override;
148   void GetNextId(GetNextIdCallback callback) override;
149 
150   void StartDownload(
151       std::unique_ptr<download::DownloadCreateInfo> info,
152       std::unique_ptr<download::InputStream> stream,
153       download::DownloadUrlParameters::OnStartedCallback on_started);
154 
155   // For testing; specifically, accessed from TestFileErrorInjector.
156   void SetDownloadItemFactoryForTesting(
157       std::unique_ptr<download::DownloadItemFactory> item_factory);
158   void SetDownloadFileFactoryForTesting(
159       std::unique_ptr<download::DownloadFileFactory> file_factory);
160   virtual download::DownloadFileFactory* GetDownloadFileFactoryForTesting();
161 
162   // Continue a navigation that ends up to be a download after it reaches the
163   // OnResponseStarted() step. It has to be called on the UI thread.
164   void InterceptNavigation(
165       std::unique_ptr<network::ResourceRequest> resource_request,
166       std::vector<GURL> url_chain,
167       network::mojom::URLResponseHeadPtr response_head,
168       mojo::ScopedDataPipeConsumerHandle response_body,
169       network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
170       net::CertStatus cert_status,
171       int frame_tree_node_id,
172       bool from_download_cross_origin_redirect);
173 
174  private:
175   using DownloadSet = std::set<download::DownloadItem*>;
176   using DownloadGuidMap =
177       std::unordered_map<std::string, download::DownloadItemImpl*>;
178   using DownloadItemImplVector = std::vector<download::DownloadItemImpl*>;
179 
180   // For testing.
181   friend class DownloadManagerTest;
182   friend class DownloadTest;
183 
184   void CreateSavePackageDownloadItemWithId(
185       const base::FilePath& main_file_path,
186       const GURL& page_url,
187       const std::string& mime_type,
188       int render_process_id,
189       int render_frame_id,
190       download::DownloadJob::CancelRequestCallback cancel_request_callback,
191       DownloadItemImplCreated on_started,
192       uint32_t id);
193 
194   // InProgressDownloadManager::Delegate implementations.
195   void OnDownloadsInitialized() override;
196   bool InterceptDownload(const download::DownloadCreateInfo& info) override;
197   base::FilePath GetDefaultDownloadDirectory() override;
198   void StartDownloadItem(
199       std::unique_ptr<download::DownloadCreateInfo> info,
200       download::DownloadUrlParameters::OnStartedCallback on_started,
201       download::InProgressDownloadManager::StartDownloadItemCallback callback)
202       override;
203 
204   // Creates a new download item and call |callback|.
205   void CreateNewDownloadItemToStart(
206       std::unique_ptr<download::DownloadCreateInfo> info,
207       download::DownloadUrlParameters::OnStartedCallback on_started,
208       download::InProgressDownloadManager::StartDownloadItemCallback callback,
209       uint32_t id);
210 
211   // Sets the |next_download_id_| if the |next_id| is larger. Runs all the
212   // |id_callbacks_| if both the ID from both history db and in-progress db
213   // are retrieved.
214   void SetNextId(uint32_t next_id);
215 
216   // Called when the next ID from history db is retrieved.
217   void OnHistoryNextIdRetrived(uint32_t next_id);
218 
219   // Create a new active item based on the info.  Separate from
220   // StartDownload() for testing.
221   download::DownloadItemImpl* CreateActiveItem(
222       uint32_t id,
223       const download::DownloadCreateInfo& info);
224 
225   // Called with the result of CheckForFileExistence. Updates the state of the
226   // file and then notifies this update to the file's observer.
227   void OnFileExistenceChecked(uint32_t download_id, bool result);
228 
229   // Overridden from DownloadItemImplDelegate
230   void DetermineDownloadTarget(download::DownloadItemImpl* item,
231                                DownloadTargetCallback callback) override;
232   bool ShouldCompleteDownload(download::DownloadItemImpl* item,
233                               base::OnceClosure complete_callback) override;
234   bool ShouldOpenFileBasedOnExtension(const base::FilePath& path) override;
235   bool ShouldOpenDownload(download::DownloadItemImpl* item,
236                           ShouldOpenDownloadCallback callback) override;
237   void CheckForFileRemoval(download::DownloadItemImpl* download_item) override;
238   std::string GetApplicationClientIdForFileScanning() const override;
239   void ResumeInterruptedDownload(
240       std::unique_ptr<download::DownloadUrlParameters> params,
241       const GURL& site_url) override;
242   void OpenDownload(download::DownloadItemImpl* download) override;
243   void ShowDownloadInShell(download::DownloadItemImpl* download) override;
244   void DownloadRemoved(download::DownloadItemImpl* download) override;
245   void DownloadInterrupted(download::DownloadItemImpl* download) override;
246   bool IsOffTheRecord() const override;
247   void ReportBytesWasted(download::DownloadItemImpl* download) override;
248   void BindWakeLockProvider(
249       mojo::PendingReceiver<device::mojom::WakeLockProvider> receiver) override;
250   download::QuarantineConnectionCallback GetQuarantineConnectionCallback()
251       override;
252 
253   // Drops a download before it is created.
254   void DropDownload();
255 
256   // Helper method to start or resume a download.
257   void BeginDownloadInternal(
258       std::unique_ptr<download::DownloadUrlParameters> params,
259       scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
260       bool is_new_download,
261       const GURL& site_url);
262 
263   void InterceptNavigationOnChecksComplete(
264       WebContents::Getter web_contents_getter,
265       std::unique_ptr<network::ResourceRequest> resource_request,
266       std::vector<GURL> url_chain,
267       net::CertStatus cert_status,
268       network::mojom::URLResponseHeadPtr response_head,
269       mojo::ScopedDataPipeConsumerHandle response_body,
270       network::mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints,
271       bool is_download_allowed);
272   void BeginResourceDownloadOnChecksComplete(
273       std::unique_ptr<download::DownloadUrlParameters> params,
274       scoped_refptr<network::SharedURLLoaderFactory> blob_url_loader_factory,
275       bool is_new_download,
276       const GURL& site_url,
277       bool is_download_allowed);
278 
279   // Whether |next_download_id_| is initialized.
280   bool IsNextIdInitialized() const;
281 
282   // Called when a new download is created.
283   void OnDownloadCreated(std::unique_ptr<download::DownloadItemImpl> download);
284 
285   // Retrieves a download from |in_progress_downloads_|.
286   std::unique_ptr<download::DownloadItemImpl> RetrieveInProgressDownload(
287       uint32_t id);
288 
289   // Import downloads from |in_progress_downloads_| into |downloads_|, resolve
290   // missing download IDs.
291   void ImportInProgressDownloads(uint32_t next_id);
292 
293   // Called when this object is considered initialized.
294   void OnDownloadManagerInitialized();
295 
296   // Check whether a download should be cleared from history. Cancelled and
297   // non-resumable interrupted download will be cleaned up to save memory.
298   bool ShouldClearDownloadFromDB(const GURL& url,
299                                  download::DownloadItem::DownloadState state,
300                                  download::DownloadInterruptReason reason,
301                                  const base::Time& start_time);
302 
303   // Factory for creation of downloads items.
304   std::unique_ptr<download::DownloadItemFactory> item_factory_;
305 
306   // |downloads_| is the owning set for all downloads known to the
307   // DownloadManager.  This includes downloads started by the user in
308   // this session, downloads initialized from the history system, and
309   // "save page as" downloads.
310   // TODO(asanka): Remove this container in favor of downloads_by_guid_ as a
311   // part of http://crbug.com/593020.
312   std::unordered_map<uint32_t, std::unique_ptr<download::DownloadItemImpl>>
313       downloads_;
314 
315   // Same as the above, but maps from GUID to download item. Note that the
316   // container is case sensitive. Hence the key needs to be normalized to
317   // upper-case when inserting new elements here. Fortunately for us,
318   // DownloadItemImpl already normalizes the string GUID.
319   DownloadGuidMap downloads_by_guid_;
320 
321   // True if the download manager has been initialized and requires a shutdown.
322   bool shutdown_needed_;
323 
324   // Whether the history db and/or in progress cache are initialized.
325   bool history_db_initialized_;
326   bool in_progress_cache_initialized_;
327 
328   // Observers that want to be notified of changes to the set of downloads.
329   base::ObserverList<Observer>::Unchecked observers_;
330 
331   // Stores information about in-progress download items.
332   std::unique_ptr<download::DownloadItem::Observer>
333       in_progress_download_observer_;
334 
335   // The current active browser context.
336   BrowserContext* browser_context_;
337 
338   // Allows an embedder to control behavior. Guaranteed to outlive this object.
339   DownloadManagerDelegate* delegate_;
340 
341   std::unique_ptr<download::InProgressDownloadManager> in_progress_manager_;
342 
343   // Callback to run to load all history downloads.
344   base::OnceClosure load_history_downloads_cb_;
345 
346   // The next download id to issue to new downloads. The |next_download_id_| can
347   // only be used when both history and in-progress db have provided their
348   // values.
349   uint32_t next_download_id_;
350 
351   // Whether next download ID from history DB is being retrieved.
352   bool is_history_download_id_retrieved_;
353 
354   // Whether new download should be persisted to the in progress download
355   // database.
356   bool should_persist_new_download_;
357 
358   // The download GUIDs that are cleared up on startup.
359   std::set<std::string> cleared_download_guids_on_startup_;
360 
361   // In progress downloads returned by |in_progress_manager_| that are not yet
362   // added to |downloads_|. If a download was started without launching full
363   // browser process, its ID will be invalid. DownloadManager will assign new
364   // ID to it when importing all downloads.
365   std::vector<std::unique_ptr<download::DownloadItemImpl>>
366       in_progress_downloads_;
367 
368   // Callbacks to run once download ID is determined.
369   using IdCallbackVector = std::vector<std::unique_ptr<GetNextIdCallback>>;
370   IdCallbackVector id_callbacks_;
371 
372   // SequencedTaskRunner to check for file existence. A sequence is used so
373   // that a large download history doesn't cause a large number of concurrent
374   // disk operations.
375   const scoped_refptr<base::SequencedTaskRunner> disk_access_task_runner_;
376 
377   // DownloadItem for which a query is queued in the |disk_access_task_runner_|.
378   std::set<uint32_t> pending_disk_access_query_;
379 
380   base::WeakPtrFactory<DownloadManagerImpl> weak_factory_{this};
381 
382   DISALLOW_COPY_AND_ASSIGN(DownloadManagerImpl);
383 };
384 
385 }  // namespace content
386 
387 #endif  // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_MANAGER_IMPL_H_
388