[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 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 5 | #include "content/browser/download/download_manager_impl.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 7 | #include <iterator> |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 8 | #include <utility> |
[email protected] | e7557f17 | 2011-08-19 23:42:01 | [diff] [blame] | 9 | |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 10 | #include "base/bind.h" |
[email protected] | 2041cf34 | 2010-02-19 03:15:59 | [diff] [blame] | 11 | #include "base/callback.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 12 | #include "base/debug/alias.h" |
[email protected] | 503d0387 | 2011-05-06 08:36:26 | [diff] [blame] | 13 | #include "base/i18n/case_conversion.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "base/logging.h" |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 16 | #include "base/memory/weak_ptr.h" |
[email protected] | 95f861e | 2013-07-18 02:07:17 | [diff] [blame] | 17 | #include "base/message_loop/message_loop.h" |
jialiul | 82b8e50 | 2016-09-30 17:42:42 | [diff] [blame] | 18 | #include "base/metrics/histogram_macros.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 19 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 20 | #include "base/strings/stringprintf.h" |
[email protected] | 40d11e0 | 2013-03-28 17:43:14 | [diff] [blame] | 21 | #include "base/strings/sys_string_conversions.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 22 | #include "base/supports_user_data.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 23 | #include "base/synchronization/lock.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 24 | #include "build/build_config.h" |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 25 | #include "components/download/downloader/in_progress/download_entry.h" |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 26 | #include "components/download/downloader/in_progress/in_progress_cache_impl.h" |
[email protected] | f8e92b5d | 2013-03-21 18:35:46 | [diff] [blame] | 27 | #include "content/browser/byte_stream.h" |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 28 | #include "content/browser/child_process_security_policy_impl.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 29 | #include "content/browser/download/download_create_info.h" |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 30 | #include "content/browser/download/download_file_factory.h" |
| 31 | #include "content/browser/download/download_item_factory.h" |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 32 | #include "content/browser/download/download_item_impl.h" |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 33 | #include "content/browser/download/download_stats.h" |
siggi | 2b05ad10 | 2017-07-21 18:57:56 | [diff] [blame] | 34 | #include "content/browser/download/download_task_runner.h" |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 35 | #include "content/browser/download/download_utils.h" |
| 36 | #include "content/browser/download/resource_downloader.h" |
| 37 | #include "content/browser/download/url_downloader.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 38 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 39 | #include "content/browser/loader/resource_request_info_impl.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 40 | #include "content/browser/renderer_host/render_view_host_impl.h" |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 41 | #include "content/browser/storage_partition_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 42 | #include "content/browser/web_contents/web_contents_impl.h" |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 43 | #include "content/common/throttling_url_loader.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 44 | #include "content/public/browser/browser_context.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 45 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 46 | #include "content/public/browser/download_interrupt_reasons.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 47 | #include "content/public/browser/download_manager_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 48 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 49 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 50 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 51 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 52 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 53 | #include "content/public/browser/web_contents_delegate.h" |
Min Qin | c3c19465 | 2017-11-28 00:02:57 | [diff] [blame] | 54 | #include "content/public/common/browser_side_navigation_policy.h" |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 55 | #include "content/public/common/content_features.h" |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 56 | #include "content/public/common/previews_state.h" |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 57 | #include "content/public/common/referrer.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 58 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 59 | #include "net/base/load_flags.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 60 | #include "net/base/request_priority.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 61 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 62 | #include "net/url_request/url_request_context.h" |
asanka | 6150c52 | 2016-03-25 21:40:57 | [diff] [blame] | 63 | #include "storage/browser/blob/blob_url_request_job_factory.h" |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 64 | #include "url/origin.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | |
Antonio Gomes | 5986d9bca | 2017-08-30 22:15:56 | [diff] [blame] | 66 | #if defined(USE_X11) |
thomasanderson | 1799a122 | 2017-06-14 00:02:02 | [diff] [blame] | 67 | #include "base/nix/xdg_util.h" |
| 68 | #endif |
| 69 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 70 | namespace content { |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 71 | namespace { |
| 72 | |
Min Qin | c3c19465 | 2017-11-28 00:02:57 | [diff] [blame] | 73 | WebContents* GetWebContents(int render_process_id, |
| 74 | int render_frame_id, |
| 75 | int frame_tree_node_id) { |
| 76 | DCHECK(IsBrowserSideNavigationEnabled()); |
| 77 | |
| 78 | WebContents* web_contents = WebContents::FromRenderFrameHost( |
| 79 | RenderFrameHost::FromID(render_process_id, render_frame_id)); |
| 80 | if (web_contents) |
| 81 | return web_contents; |
| 82 | |
| 83 | return WebContents::FromFrameTreeNodeId(frame_tree_node_id); |
| 84 | } |
| 85 | |
Min Qin | 99b2c57e | 2017-09-28 05:04:37 | [diff] [blame] | 86 | StoragePartitionImpl* GetStoragePartition(BrowserContext* context, |
| 87 | int render_process_id, |
| 88 | int render_frame_id) { |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 89 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 90 | |
| 91 | SiteInstance* site_instance = nullptr; |
| 92 | if (render_process_id >= 0) { |
| 93 | RenderFrameHost* render_frame_host_ = |
| 94 | RenderFrameHost::FromID(render_process_id, render_frame_id); |
| 95 | if (render_frame_host_) |
| 96 | site_instance = render_frame_host_->GetSiteInstance(); |
| 97 | } |
Min Qin | 99b2c57e | 2017-09-28 05:04:37 | [diff] [blame] | 98 | return static_cast<StoragePartitionImpl*>( |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 99 | BrowserContext::GetStoragePartition(context, site_instance)); |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 100 | } |
| 101 | |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 102 | bool CanRequestURLFromRenderer(int render_process_id, GURL url) { |
| 103 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 104 | // Check if the renderer is permitted to request the requested URL. |
| 105 | if (!ChildProcessSecurityPolicyImpl::GetInstance()->CanRequestURL( |
| 106 | render_process_id, url)) { |
| 107 | DVLOG(1) << "Denied unauthorized download request for " |
| 108 | << url.possibly_invalid_spec(); |
| 109 | return false; |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | void CreateInterruptedDownload( |
| 115 | DownloadUrlParameters* params, |
| 116 | DownloadInterruptReason reason, |
| 117 | base::WeakPtr<DownloadManagerImpl> download_manager) { |
| 118 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 | std::unique_ptr<DownloadCreateInfo> failed_created_info( |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 120 | new DownloadCreateInfo(base::Time::Now(), |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 121 | base::WrapUnique(new DownloadSaveInfo))); |
| 122 | failed_created_info->url_chain.push_back(params->url()); |
| 123 | failed_created_info->result = reason; |
| 124 | std::unique_ptr<ByteStreamReader> empty_byte_stream; |
| 125 | BrowserThread::PostTask( |
| 126 | BrowserThread::UI, FROM_HERE, |
| 127 | base::BindOnce(&DownloadManager::StartDownload, download_manager, |
| 128 | std::move(failed_created_info), |
Jeremy Roman | 04f27c37 | 2017-10-27 15:20:55 | [diff] [blame] | 129 | std::make_unique<DownloadManager::InputStream>( |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 130 | std::move(empty_byte_stream)), |
| 131 | params->callback())); |
| 132 | } |
| 133 | |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 134 | DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginDownload( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 135 | std::unique_ptr<DownloadUrlParameters> params, |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 136 | content::ResourceContext* resource_context, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 137 | uint32_t download_id, |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 138 | base::WeakPtr<DownloadManagerImpl> download_manager) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 139 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 140 | |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 141 | std::unique_ptr<net::URLRequest> url_request = |
Ramin Halavati | 03efa73 | 2017-06-12 09:28:20 | [diff] [blame] | 142 | DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 143 | std::unique_ptr<storage::BlobDataHandle> blob_data_handle = |
asanka | 6150c52 | 2016-03-25 21:40:57 | [diff] [blame] | 144 | params->GetBlobDataHandle(); |
| 145 | if (blob_data_handle) { |
| 146 | storage::BlobProtocolHandler::SetRequestedBlobDataHandle( |
| 147 | url_request.get(), std::move(blob_data_handle)); |
| 148 | } |
[email protected] | fc179dd1 | 2013-03-16 09:35:19 | [diff] [blame] | 149 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 150 | // If there's a valid renderer process associated with the request, then the |
| 151 | // request should be driven by the ResourceLoader. Pass it over to the |
| 152 | // ResourceDispatcherHostImpl which will in turn pass it along to the |
| 153 | // ResourceLoader. |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 154 | if (params->render_process_host_id() >= 0) { |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 155 | DownloadInterruptReason reason = DownloadManagerImpl::BeginDownloadRequest( |
| 156 | std::move(url_request), params->referrer(), resource_context, |
| 157 | params->content_initiated(), params->render_process_host_id(), |
| 158 | params->render_view_host_routing_id(), |
| 159 | params->render_frame_host_routing_id(), |
| 160 | params->do_not_prompt_for_login()); |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 161 | |
| 162 | // If the download was accepted, the DownloadResourceHandler is now |
| 163 | // responsible for driving the request to completion. |
| 164 | if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 165 | return nullptr; |
| 166 | |
| 167 | // Otherwise, create an interrupted download. |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 168 | CreateInterruptedDownload(params.get(), reason, download_manager); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 169 | return nullptr; |
| 170 | } |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 171 | |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 172 | return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr( |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 173 | UrlDownloader::BeginDownload(download_manager, std::move(url_request), |
qinmin | 462abbb | 2017-05-03 21:55:42 | [diff] [blame] | 174 | params->referrer(), false) |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 175 | .release()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 176 | } |
| 177 | |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 178 | DownloadManagerImpl::UniqueUrlDownloadHandlerPtr BeginResourceDownload( |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 179 | std::unique_ptr<DownloadUrlParameters> params, |
| 180 | std::unique_ptr<ResourceRequest> request, |
| 181 | scoped_refptr<URLLoaderFactoryGetter> url_loader_factory_getter, |
Min Qin | 99b2c57e | 2017-09-28 05:04:37 | [diff] [blame] | 182 | scoped_refptr<storage::FileSystemContext> file_system_context, |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 183 | uint32_t download_id, |
| 184 | base::WeakPtr<DownloadManagerImpl> download_manager) { |
| 185 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 186 | |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 187 | // Check if the renderer is permitted to request the requested URL. |
| 188 | if (params->render_process_host_id() >= 0 && |
| 189 | !CanRequestURLFromRenderer(params->render_process_host_id(), |
| 190 | params->url())) { |
| 191 | CreateInterruptedDownload(params.get(), |
| 192 | DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST, |
| 193 | download_manager); |
| 194 | return nullptr; |
| 195 | } |
| 196 | |
Nick Carter | 62855fe8 | 2017-11-29 00:17:17 | [diff] [blame] | 197 | ResourceRequestInfo::WebContentsGetter getter = |
| 198 | base::Bind(&GetWebContents, params->render_process_host_id(), |
| 199 | params->render_frame_host_routing_id(), -1); |
Min Qin | c3c19465 | 2017-11-28 00:02:57 | [diff] [blame] | 200 | // TODO(qinmin): Check the storage permission before creating the URLLoader. |
| 201 | // This is already done for context menu download, but it is missing for |
| 202 | // download service and download resumption. |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 203 | return DownloadManagerImpl::UniqueUrlDownloadHandlerPtr( |
| 204 | ResourceDownloader::BeginDownload( |
| 205 | download_manager, std::move(params), std::move(request), |
Min Qin | 4a56735 | 2017-11-28 19:14:48 | [diff] [blame] | 206 | url_loader_factory_getter, file_system_context, getter, download_id, |
| 207 | false) |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 208 | .release()); |
| 209 | } |
| 210 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 211 | class DownloadItemFactoryImpl : public DownloadItemFactory { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 212 | public: |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 213 | DownloadItemFactoryImpl() {} |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 214 | ~DownloadItemFactoryImpl() override {} |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 215 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 216 | DownloadItemImpl* CreatePersistedItem( |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 217 | DownloadItemImplDelegate* delegate, |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 218 | const std::string& guid, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 219 | uint32_t download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 220 | const base::FilePath& current_path, |
| 221 | const base::FilePath& target_path, |
| 222 | const std::vector<GURL>& url_chain, |
| 223 | const GURL& referrer_url, |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 224 | const GURL& site_url, |
asanka | 038d58d | 2016-04-14 00:29:28 | [diff] [blame] | 225 | const GURL& tab_url, |
| 226 | const GURL& tab_refererr_url, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 227 | const std::string& mime_type, |
| 228 | const std::string& original_mime_type, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 229 | base::Time start_time, |
| 230 | base::Time end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 231 | const std::string& etag, |
| 232 | const std::string& last_modified, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 233 | int64_t received_bytes, |
| 234 | int64_t total_bytes, |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 235 | const std::string& hash, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 236 | DownloadItem::DownloadState state, |
| 237 | DownloadDangerType danger_type, |
| 238 | DownloadInterruptReason interrupt_reason, |
| 239 | bool opened, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 240 | base::Time last_access_time, |
shaktisahu | fd49a3e | 2017-03-30 18:35:10 | [diff] [blame] | 241 | bool transient, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 242 | const std::vector<DownloadItem::ReceivedSlice>& received_slices) |
| 243 | override { |
asanka | 038d58d | 2016-04-14 00:29:28 | [diff] [blame] | 244 | return new DownloadItemImpl( |
| 245 | delegate, guid, download_id, current_path, target_path, url_chain, |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 246 | referrer_url, site_url, tab_url, tab_refererr_url, mime_type, |
| 247 | original_mime_type, start_time, end_time, etag, last_modified, |
| 248 | received_bytes, total_bytes, hash, state, danger_type, interrupt_reason, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 249 | opened, last_access_time, transient, received_slices); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 250 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 251 | |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 252 | DownloadItemImpl* CreateActiveItem(DownloadItemImplDelegate* delegate, |
| 253 | uint32_t download_id, |
| 254 | const DownloadCreateInfo& info) override { |
| 255 | return new DownloadItemImpl(delegate, download_id, info); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 256 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 257 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 258 | DownloadItemImpl* CreateSavePageItem( |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 259 | DownloadItemImplDelegate* delegate, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 260 | uint32_t download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 261 | const base::FilePath& path, |
| 262 | const GURL& url, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 263 | const std::string& mime_type, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 264 | std::unique_ptr<DownloadRequestHandleInterface> request_handle) override { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 265 | return new DownloadItemImpl(delegate, download_id, path, url, mime_type, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 266 | std::move(request_handle)); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 267 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 268 | }; |
| 269 | |
Antonio Gomes | 5986d9bca | 2017-08-30 22:15:56 | [diff] [blame] | 270 | #if defined(USE_X11) |
thomasanderson | 1799a122 | 2017-06-14 00:02:02 | [diff] [blame] | 271 | base::FilePath GetTemporaryDownloadDirectory() { |
| 272 | std::unique_ptr<base::Environment> env(base::Environment::Create()); |
| 273 | return base::nix::GetXDGDirectory(env.get(), "XDG_DATA_HOME", ".local/share"); |
| 274 | } |
| 275 | #endif |
| 276 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 277 | } // namespace |
| 278 | |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 279 | // Responsible for persisting the in-progress metadata associated with a |
| 280 | // download. |
| 281 | class InProgressDownloadObserver : public DownloadItem::Observer { |
| 282 | public: |
| 283 | explicit InProgressDownloadObserver( |
| 284 | download::InProgressCache* in_progress_cache); |
| 285 | ~InProgressDownloadObserver() override; |
| 286 | |
| 287 | private: |
| 288 | // DownloadItem::Observer |
| 289 | void OnDownloadUpdated(DownloadItem* download) override; |
| 290 | void OnDownloadRemoved(DownloadItem* download) override; |
| 291 | |
| 292 | // The persistent cache to store in-progress metadata. |
| 293 | download::InProgressCache* in_progress_cache_; |
| 294 | |
| 295 | DISALLOW_COPY_AND_ASSIGN(InProgressDownloadObserver); |
| 296 | }; |
| 297 | |
| 298 | InProgressDownloadObserver::InProgressDownloadObserver( |
| 299 | download::InProgressCache* in_progress_cache) |
| 300 | : in_progress_cache_(in_progress_cache) {} |
| 301 | |
| 302 | InProgressDownloadObserver::~InProgressDownloadObserver() = default; |
| 303 | |
| 304 | void InProgressDownloadObserver::OnDownloadUpdated(DownloadItem* download) { |
| 305 | // TODO(crbug.com/778425): Properly handle fail/resume/retry for downloads |
| 306 | // that are in the INTERRUPTED state for a long time. |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 307 | if (!in_progress_cache_) |
| 308 | return; |
| 309 | |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 310 | switch (download->GetState()) { |
| 311 | case DownloadItem::DownloadState::COMPLETE: |
| 312 | case DownloadItem::DownloadState::CANCELLED: |
| 313 | if (in_progress_cache_) |
| 314 | in_progress_cache_->RemoveEntry(download->GetGuid()); |
| 315 | break; |
| 316 | case DownloadItem::DownloadState::IN_PROGRESS: |
| 317 | // TODO(crbug.com/778425): After RetrieveEntry has been implemented, do a |
| 318 | // check to make sure the entry exists in the cache. |
| 319 | break; |
| 320 | default: |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | void InProgressDownloadObserver::OnDownloadRemoved(DownloadItem* download) { |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 326 | if (!in_progress_cache_) |
| 327 | return; |
| 328 | |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 329 | in_progress_cache_->RemoveEntry(download->GetGuid()); |
| 330 | } |
| 331 | |
Min Qin | ddb40ce | 2017-10-28 03:38:26 | [diff] [blame] | 332 | DownloadManagerImpl::DownloadManagerImpl(BrowserContext* browser_context) |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 333 | : item_factory_(new DownloadItemFactoryImpl()), |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 334 | file_factory_(new DownloadFileFactory()), |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 335 | shutdown_needed_(true), |
xingliu | d64fde65 | 2017-05-24 07:04:33 | [diff] [blame] | 336 | initialized_(false), |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 337 | history_db_initialized_(false), |
| 338 | in_progress_cache_initialized_(false), |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 339 | browser_context_(browser_context), |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 340 | delegate_(nullptr), |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 341 | weak_factory_(this) { |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 342 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 343 | } |
| 344 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 345 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 346 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 347 | } |
| 348 | |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 349 | DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 350 | uint32_t id, |
| 351 | const DownloadCreateInfo& info) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 352 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 353 | DCHECK(!base::ContainsKey(downloads_, id)); |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 354 | DownloadItemImpl* download = item_factory_->CreateActiveItem(this, id, info); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 355 | downloads_[id] = base::WrapUnique(download); |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 356 | downloads_by_guid_[download->GetGuid()] = download; |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 357 | return download; |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 358 | } |
| 359 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 360 | void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 361 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 362 | if (delegate_) { |
| 363 | delegate_->GetNextId(callback); |
| 364 | return; |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 365 | } |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 366 | static uint32_t next_id = content::DownloadItem::kInvalidId + 1; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 367 | callback.Run(next_id++); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 368 | } |
| 369 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 370 | void DownloadManagerImpl::DetermineDownloadTarget( |
| 371 | DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 372 | // Note that this next call relies on |
| 373 | // DownloadItemImplDelegate::DownloadTargetCallback and |
| 374 | // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 375 | // type. If the types ever diverge, gasket code will need to |
| 376 | // be written here. |
| 377 | if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 378 | base::FilePath target_path = item->GetForcedFilePath(); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 379 | // TODO(asanka): Determine a useful path if |target_path| is empty. |
Asanka Herath | 1ba0e9f | 2017-04-03 18:48:53 | [diff] [blame] | 380 | callback.Run(target_path, DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 381 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, target_path, |
| 382 | DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 383 | } |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 384 | } |
| 385 | |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 386 | bool DownloadManagerImpl::ShouldCompleteDownload( |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 387 | DownloadItemImpl* item, const base::Closure& complete_callback) { |
| 388 | if (!delegate_ || |
| 389 | delegate_->ShouldCompleteDownload(item, complete_callback)) { |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 390 | return true; |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 391 | } |
| 392 | // Otherwise, the delegate has accepted responsibility to run the |
| 393 | // callback when the download is ready for completion. |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 394 | return false; |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 395 | } |
| 396 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 397 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension( |
| 398 | const base::FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 399 | if (!delegate_) |
| 400 | return false; |
| 401 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 402 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 403 | } |
| 404 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 405 | bool DownloadManagerImpl::ShouldOpenDownload( |
| 406 | DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) { |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 407 | if (!delegate_) |
| 408 | return true; |
| 409 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 410 | // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and |
| 411 | // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening" |
| 412 | // to have the same type :-}. |
| 413 | return delegate_->ShouldOpenDownload(item, callback); |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 414 | } |
| 415 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 416 | void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) { |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 417 | delegate_ = delegate; |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 418 | |
| 419 | // If initialization has not occurred and there's a delegate and cache, |
| 420 | // initialize and indicate initialization is complete. Else, just indicate it |
| 421 | // is ok to continue. |
| 422 | if (initialized_ || in_progress_cache_initialized_) |
| 423 | return; |
| 424 | |
| 425 | base::RepeatingClosure post_init_callback = base::BindRepeating( |
| 426 | &DownloadManagerImpl::PostInitialization, weak_factory_.GetWeakPtr(), |
| 427 | DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE); |
| 428 | |
| 429 | if (delegate_) { |
| 430 | download::InProgressCache* in_progress_cache = |
| 431 | delegate_->GetInProgressCache(); |
| 432 | if (in_progress_cache) { |
| 433 | in_progress_cache->Initialize(post_init_callback); |
| 434 | return; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, post_init_callback); |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 439 | } |
| 440 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 441 | DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 442 | return delegate_; |
| 443 | } |
| 444 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 445 | void DownloadManagerImpl::Shutdown() { |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 446 | DVLOG(20) << __func__ << "() shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 447 | if (!shutdown_needed_) |
| 448 | return; |
| 449 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 451 | for (auto& observer : observers_) |
| 452 | observer.ManagerGoingDown(this); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 453 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 454 | |
[email protected] | fe75227 | 2013-05-29 18:57:45 | [diff] [blame] | 455 | // If there are in-progress downloads, cancel them. This also goes for |
| 456 | // dangerous downloads which will remain in history if they aren't explicitly |
| 457 | // accepted or discarded. Canceling will remove the intermediate download |
| 458 | // file. |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 459 | for (const auto& it : downloads_) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 460 | DownloadItemImpl* download = it.second.get(); |
[email protected] | fe75227 | 2013-05-29 18:57:45 | [diff] [blame] | 461 | if (download->GetState() == DownloadItem::IN_PROGRESS) |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 462 | download->Cancel(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 463 | } |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 464 | downloads_.clear(); |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 465 | downloads_by_guid_.clear(); |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 466 | url_download_handlers_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 467 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 468 | // We'll have nothing more to report to the observers after this point. |
| 469 | observers_.Clear(); |
| 470 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 471 | if (delegate_) |
| 472 | delegate_->Shutdown(); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 473 | delegate_ = nullptr; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | } |
| 475 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 476 | void DownloadManagerImpl::StartDownload( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 477 | std::unique_ptr<DownloadCreateInfo> info, |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 478 | std::unique_ptr<DownloadManager::InputStream> stream, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 479 | const DownloadUrlParameters::OnStartedCallback& on_started) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 480 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 481 | DCHECK(info); |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 482 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 483 | // |stream| is only non-nil if the download request was successful. |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 484 | DCHECK( |
| 485 | (info->result == DOWNLOAD_INTERRUPT_REASON_NONE && !stream->IsEmpty()) || |
| 486 | (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && stream->IsEmpty())); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 487 | DVLOG(20) << __func__ |
| 488 | << "() result=" << DownloadInterruptReasonToString(info->result); |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 489 | uint32_t download_id = info->download_id; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 490 | const bool new_download = (download_id == content::DownloadItem::kInvalidId); |
jialiul | 82b8e50 | 2016-09-30 17:42:42 | [diff] [blame] | 491 | if (new_download) |
| 492 | RecordDownloadConnectionSecurity(info->url(), info->url_chain); |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 493 | base::Callback<void(uint32_t)> got_id(base::Bind( |
| 494 | &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(), |
| 495 | base::Passed(&info), base::Passed(&stream), on_started, new_download)); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 496 | if (new_download) { |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 497 | GetNextId(got_id); |
| 498 | } else { |
| 499 | got_id.Run(download_id); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | void DownloadManagerImpl::StartDownloadWithId( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 504 | std::unique_ptr<DownloadCreateInfo> info, |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 505 | std::unique_ptr<DownloadManager::InputStream> stream, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 506 | const DownloadUrlParameters::OnStartedCallback& on_started, |
| 507 | bool new_download, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 508 | uint32_t id) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 509 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 510 | DCHECK_NE(content::DownloadItem::kInvalidId, id); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 511 | DownloadItemImpl* download = nullptr; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 512 | if (new_download) { |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 513 | download = CreateActiveItem(id, *info); |
| 514 | } else { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 515 | auto item_iterator = downloads_.find(id); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 516 | // Trying to resume an interrupted download. |
[email protected] | 7b3eeca4 | 2013-05-23 20:56:37 | [diff] [blame] | 517 | if (item_iterator == downloads_.end() || |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 518 | (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 519 | // If the download is no longer known to the DownloadManager, then it was |
[email protected] | 7b3eeca4 | 2013-05-23 20:56:37 | [diff] [blame] | 520 | // removed after it was resumed. Ignore. If the download is cancelled |
| 521 | // while resuming, then also ignore the request. |
Min Qin | 5b912dc4 | 2017-06-07 23:10:18 | [diff] [blame] | 522 | info->request_handle->CancelRequest(true); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 523 | if (!on_started.is_null()) |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 524 | on_started.Run(nullptr, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
siggi | 2b05ad10 | 2017-07-21 18:57:56 | [diff] [blame] | 525 | // The ByteStreamReader lives and dies on the download sequence. |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 526 | if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) |
siggi | 2b05ad10 | 2017-07-21 18:57:56 | [diff] [blame] | 527 | GetDownloadTaskRunner()->DeleteSoon(FROM_HERE, stream.release()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 528 | return; |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 529 | } |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 530 | download = item_iterator->second.get(); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 531 | } |
| 532 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 533 | base::FilePath default_download_directory; |
Antonio Gomes | 5986d9bca | 2017-08-30 22:15:56 | [diff] [blame] | 534 | #if defined(USE_X11) |
Tom Anderson | e9b9ba0e | 2017-11-14 21:28:06 | [diff] [blame] | 535 | // TODO(thomasanderson,crbug.com/784010): Remove this when all Linux |
| 536 | // distros with versions of GTK lower than 3.14.7 are no longer |
| 537 | // supported. This should happen when support for Ubuntu Trusty and |
| 538 | // Debian Jessie are removed. |
thomasanderson | 1799a122 | 2017-06-14 00:02:02 | [diff] [blame] | 539 | default_download_directory = GetTemporaryDownloadDirectory(); |
| 540 | #else |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 541 | if (delegate_) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 542 | base::FilePath website_save_directory; // Unused |
| 543 | bool skip_dir_check = false; // Unused |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 544 | delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 545 | &default_download_directory, &skip_dir_check); |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 546 | } |
thomasanderson | 1799a122 | 2017-06-14 00:02:02 | [diff] [blame] | 547 | #endif |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 548 | |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 549 | if (delegate_) { |
| 550 | if (!in_progress_download_observer_) { |
| 551 | in_progress_download_observer_.reset( |
| 552 | new InProgressDownloadObserver(delegate_->GetInProgressCache())); |
| 553 | } |
| 554 | // May already observe this item, remove observer first. |
| 555 | download->RemoveObserver(in_progress_download_observer_.get()); |
| 556 | download->AddObserver(in_progress_download_observer_.get()); |
| 557 | } |
| 558 | |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 559 | std::unique_ptr<DownloadFile> download_file; |
[email protected] | 492c009 | 2013-08-10 13:49:48 | [diff] [blame] | 560 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 561 | if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 562 | DCHECK(stream.get()); |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 563 | download_file.reset(file_factory_->CreateFile( |
| 564 | std::move(info->save_info), default_download_directory, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 565 | std::move(stream), id, download->DestinationObserverAsWeakPtr())); |
[email protected] | 492c009 | 2013-08-10 13:49:48 | [diff] [blame] | 566 | } |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 567 | // It is important to leave info->save_info intact in the case of an interrupt |
| 568 | // so that the DownloadItem can salvage what it can out of a failed resumption |
| 569 | // attempt. |
[email protected] | 492c009 | 2013-08-10 13:49:48 | [diff] [blame] | 570 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 571 | download->Start(std::move(download_file), std::move(info->request_handle), |
| 572 | *info); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 573 | |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 574 | // For interrupted downloads, Start() will transition the state to |
| 575 | // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). |
| 576 | // For new downloads, we notify here, rather than earlier, so that |
| 577 | // the download_file is bound to download and all the usual |
| 578 | // setters (e.g. Cancel) work. |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 579 | if (new_download) { |
| 580 | for (auto& observer : observers_) |
| 581 | observer.OnDownloadCreated(this, download); |
| 582 | } |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 583 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 584 | if (!on_started.is_null()) |
[email protected] | 7f391888 | 2014-01-14 06:14:56 | [diff] [blame] | 585 | on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 586 | } |
| 587 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 588 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 589 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 590 | for (const auto& it : downloads_) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 591 | DownloadItemImpl* item = it.second.get(); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 592 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 596 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 597 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 598 | if ((download_item->GetState() == DownloadItem::COMPLETE) && |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 599 | !download_item->GetFileExternallyRemoved() && |
| 600 | delegate_) { |
| 601 | delegate_->CheckForFileExistence( |
| 602 | download_item, |
tzik | 5b521ad0 | 2017-08-18 07:49:19 | [diff] [blame] | 603 | base::BindOnce(&DownloadManagerImpl::OnFileExistenceChecked, |
| 604 | weak_factory_.GetWeakPtr(), download_item->GetId())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 608 | void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id, |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 609 | bool result) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 610 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 611 | if (!result) { // File does not exist. |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 612 | if (base::ContainsKey(downloads_, download_id)) |
[email protected] | 1c63da0 | 2014-01-17 17:09:51 | [diff] [blame] | 613 | downloads_[download_id]->OnDownloadedFileRemoved(); |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 614 | } |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 615 | } |
| 616 | |
asanka | db9e50ad | 2016-10-03 15:00:29 | [diff] [blame] | 617 | std::string DownloadManagerImpl::GetApplicationClientIdForFileScanning() const { |
| 618 | if (delegate_) |
| 619 | return delegate_->ApplicationClientIdForFileScanning(); |
| 620 | return std::string(); |
| 621 | } |
| 622 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 623 | BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 624 | return browser_context_; |
| 625 | } |
| 626 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 627 | void DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 628 | const base::FilePath& main_file_path, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 629 | const GURL& page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 630 | const std::string& mime_type, |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 631 | std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 632 | const DownloadItemImplCreated& item_created) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 633 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 634 | GetNextId(base::Bind( |
| 635 | &DownloadManagerImpl::CreateSavePackageDownloadItemWithId, |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 636 | weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type, |
| 637 | base::Passed(std::move(request_handle)), item_created)); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( |
| 641 | const base::FilePath& main_file_path, |
| 642 | const GURL& page_url, |
| 643 | const std::string& mime_type, |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 644 | std::unique_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 645 | const DownloadItemImplCreated& item_created, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 646 | uint32_t id) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 647 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 648 | DCHECK_NE(content::DownloadItem::kInvalidId, id); |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 649 | DCHECK(!base::ContainsKey(downloads_, id)); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 650 | DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 651 | this, id, main_file_path, page_url, mime_type, std::move(request_handle)); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 652 | downloads_[download_item->GetId()] = base::WrapUnique(download_item); |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 653 | DCHECK(!base::ContainsKey(downloads_by_guid_, download_item->GetGuid())); |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 654 | downloads_by_guid_[download_item->GetGuid()] = download_item; |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 655 | for (auto& observer : observers_) |
| 656 | observer.OnDownloadCreated(this, download_item); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 657 | if (!item_created.is_null()) |
| 658 | item_created.Run(download_item); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 659 | } |
| 660 | |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 661 | // Resume a download of a specific URL. We send the request to the |
| 662 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 663 | // download. |
| 664 | void DownloadManagerImpl::ResumeInterruptedDownload( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 665 | std::unique_ptr<content::DownloadUrlParameters> params, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 666 | uint32_t id) { |
Min Qin | c60045c | 2017-10-17 03:02:22 | [diff] [blame] | 667 | BeginDownloadInternal(std::move(params), id); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 668 | } |
| 669 | |
Min Qin | c60045c | 2017-10-17 03:02:22 | [diff] [blame] | 670 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 671 | void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 672 | std::unique_ptr<DownloadItemFactory> item_factory) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 673 | item_factory_ = std::move(item_factory); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
dcheng | f6c4058 | 2016-04-09 00:05:13 | [diff] [blame] | 677 | std::unique_ptr<DownloadFileFactory> file_factory) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 678 | file_factory_ = std::move(file_factory); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 679 | } |
| 680 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 681 | DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 682 | return file_factory_.get(); |
| 683 | } |
| 684 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 685 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 686 | if (!download) |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 687 | return; |
| 688 | |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 689 | downloads_by_guid_.erase(download->GetGuid()); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 690 | downloads_.erase(download->GetId()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 691 | } |
| 692 | |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 693 | void DownloadManagerImpl::AddUrlDownloadHandler( |
Min Qin | 40b7217 | 2017-09-27 00:19:17 | [diff] [blame] | 694 | UniqueUrlDownloadHandlerPtr downloader) { |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 695 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 696 | if (downloader) |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 697 | url_download_handlers_.push_back(std::move(downloader)); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 698 | } |
| 699 | |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 700 | // static |
| 701 | DownloadInterruptReason DownloadManagerImpl::BeginDownloadRequest( |
| 702 | std::unique_ptr<net::URLRequest> url_request, |
| 703 | const Referrer& referrer, |
| 704 | ResourceContext* resource_context, |
| 705 | bool is_content_initiated, |
| 706 | int render_process_id, |
| 707 | int render_view_route_id, |
| 708 | int render_frame_route_id, |
| 709 | bool do_not_prompt_for_login) { |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 710 | if (ResourceDispatcherHostImpl::Get()->is_shutdown()) |
| 711 | return DOWNLOAD_INTERRUPT_REASON_USER_SHUTDOWN; |
| 712 | |
| 713 | // The URLRequest needs to be initialized with the referrer and other |
| 714 | // information prior to issuing it. |
| 715 | ResourceDispatcherHostImpl::Get()->InitializeURLRequest( |
| 716 | url_request.get(), referrer, |
| 717 | true, // download. |
| 718 | render_process_id, render_view_route_id, render_frame_route_id, |
megjablon | caf312f | 2017-01-12 18:47:49 | [diff] [blame] | 719 | PREVIEWS_OFF, resource_context); |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 720 | |
| 721 | // We treat a download as a main frame load, and thus update the policy URL on |
| 722 | // redirects. |
| 723 | // |
| 724 | // TODO(davidben): Is this correct? If this came from a |
| 725 | // ViewHostMsg_DownloadUrl in a frame, should it have first-party URL set |
| 726 | // appropriately? |
| 727 | url_request->set_first_party_url_policy( |
| 728 | net::URLRequest::UPDATE_FIRST_PARTY_URL_ON_REDIRECT); |
| 729 | |
| 730 | const GURL& url = url_request->original_url(); |
| 731 | |
| 732 | // Check if the renderer is permitted to request the requested URL. |
Min Qin | 4eb4ec43 | 2017-09-22 20:13:44 | [diff] [blame] | 733 | if (!CanRequestURLFromRenderer(render_process_id, url)) |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 734 | return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 735 | |
| 736 | const net::URLRequestContext* request_context = url_request->context(); |
mmenke | 1918ae78 | 2017-04-03 20:10:20 | [diff] [blame] | 737 | if (!request_context->job_factory()->IsHandledProtocol(url.scheme())) { |
ananta | c82dd5be | 2016-08-26 01:21:53 | [diff] [blame] | 738 | DVLOG(1) << "Download request for unsupported protocol: " |
| 739 | << url.possibly_invalid_spec(); |
| 740 | return DOWNLOAD_INTERRUPT_REASON_NETWORK_INVALID_REQUEST; |
| 741 | } |
| 742 | |
| 743 | // From this point forward, the |DownloadResourceHandler| is responsible for |
| 744 | // |started_callback|. |
| 745 | // TODO(ananta) |
| 746 | // Find a better way to create the DownloadResourceHandler instance. |
| 747 | std::unique_ptr<ResourceHandler> handler( |
| 748 | DownloadResourceHandler::Create(url_request.get())); |
| 749 | |
| 750 | ResourceDispatcherHostImpl::Get()->BeginURLRequest( |
| 751 | std::move(url_request), std::move(handler), true, // download |
| 752 | is_content_initiated, do_not_prompt_for_login, resource_context); |
| 753 | return DOWNLOAD_INTERRUPT_REASON_NONE; |
| 754 | } |
| 755 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 756 | void DownloadManagerImpl::InterceptNavigation( |
| 757 | std::unique_ptr<ResourceRequest> resource_request, |
| 758 | std::vector<GURL> url_chain, |
| 759 | scoped_refptr<ResourceResponse> response, |
| 760 | mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints, |
John Abd-El-Malek | f36e05f | 2017-11-30 16:17:52 | [diff] [blame] | 761 | net::CertStatus cert_status, |
Xing Liu | 18a8e089 | 2017-11-15 06:16:13 | [diff] [blame] | 762 | int frame_tree_node_id) { |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 763 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 764 | if (!delegate_) |
Min Qin | c3c19465 | 2017-11-28 00:02:57 | [diff] [blame] | 765 | return; |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 766 | |
| 767 | const GURL& url = resource_request->url; |
| 768 | const std::string& method = resource_request->method; |
| 769 | ResourceRequestInfo::WebContentsGetter web_contents_getter = |
| 770 | base::BindRepeating(&GetWebContents, ChildProcessHost::kInvalidUniqueID, |
| 771 | MSG_ROUTING_NONE, frame_tree_node_id); |
| 772 | |
| 773 | base::OnceCallback<void(bool /* download allowed */)> |
| 774 | on_download_checks_done = base::BindOnce( |
| 775 | &DownloadManagerImpl::InterceptNavigationOnChecksComplete, |
| 776 | weak_factory_.GetWeakPtr(), web_contents_getter, |
| 777 | std::move(resource_request), std::move(url_chain), |
| 778 | std::move(response), cert_status, |
| 779 | std::move(url_loader_client_endpoints)); |
| 780 | |
| 781 | delegate_->CheckDownloadAllowed(web_contents_getter, url, method, |
| 782 | std::move(on_download_checks_done)); |
Min Qin | c3c19465 | 2017-11-28 00:02:57 | [diff] [blame] | 783 | } |
| 784 | |
asanka | 6381bd4e | 2017-01-05 22:08:37 | [diff] [blame] | 785 | int DownloadManagerImpl::RemoveDownloadsByURLAndTime( |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 786 | const base::Callback<bool(const GURL&)>& url_filter, |
asanka | 6381bd4e | 2017-01-05 22:08:37 | [diff] [blame] | 787 | base::Time remove_begin, |
| 788 | base::Time remove_end) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 789 | int count = 0; |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 790 | auto it = downloads_.begin(); |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 791 | while (it != downloads_.end()) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 792 | DownloadItemImpl* download = it->second.get(); |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 793 | |
| 794 | // Increment done here to protect against invalidation below. |
| 795 | ++it; |
| 796 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 797 | if (download->GetState() != DownloadItem::IN_PROGRESS && |
asanka | 6381bd4e | 2017-01-05 22:08:37 | [diff] [blame] | 798 | url_filter.Run(download->GetURL()) && |
| 799 | download->GetStartTime() >= remove_begin && |
| 800 | (remove_end.is_null() || download->GetStartTime() < remove_end)) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 801 | download->Remove(); |
| 802 | count++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 803 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 804 | } |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 805 | return count; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 806 | } |
| 807 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 808 | void DownloadManagerImpl::DownloadUrl( |
Ramin Halavati | 03efa73 | 2017-06-12 09:28:20 | [diff] [blame] | 809 | std::unique_ptr<DownloadUrlParameters> params) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 810 | if (params->post_id() >= 0) { |
| 811 | // Check this here so that the traceback is more useful. |
| 812 | DCHECK(params->prefer_cache()); |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 813 | DCHECK_EQ("POST", params->method()); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 814 | } |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 815 | |
Min Qin | c60045c | 2017-10-17 03:02:22 | [diff] [blame] | 816 | BeginDownloadInternal(std::move(params), content::DownloadItem::kInvalidId); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 817 | } |
| 818 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 819 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 820 | observers_.AddObserver(observer); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 821 | } |
| 822 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 823 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 824 | observers_.RemoveObserver(observer); |
| 825 | } |
| 826 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 827 | DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 828 | const std::string& guid, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 829 | uint32_t id, |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 830 | const base::FilePath& current_path, |
| 831 | const base::FilePath& target_path, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 832 | const std::vector<GURL>& url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 833 | const GURL& referrer_url, |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 834 | const GURL& site_url, |
asanka | 038d58d | 2016-04-14 00:29:28 | [diff] [blame] | 835 | const GURL& tab_url, |
| 836 | const GURL& tab_refererr_url, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 837 | const std::string& mime_type, |
| 838 | const std::string& original_mime_type, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 839 | base::Time start_time, |
| 840 | base::Time end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 841 | const std::string& etag, |
| 842 | const std::string& last_modified, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 843 | int64_t received_bytes, |
| 844 | int64_t total_bytes, |
asanka | 4350f6a | 2016-03-15 02:40:57 | [diff] [blame] | 845 | const std::string& hash, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 846 | DownloadItem::DownloadState state, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 847 | DownloadDangerType danger_type, |
| 848 | DownloadInterruptReason interrupt_reason, |
qinmin | 23b2857 | 2017-02-25 00:24:28 | [diff] [blame] | 849 | bool opened, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 850 | base::Time last_access_time, |
shaktisahu | fd49a3e | 2017-03-30 18:35:10 | [diff] [blame] | 851 | bool transient, |
qinmin | 23b2857 | 2017-02-25 00:24:28 | [diff] [blame] | 852 | const std::vector<DownloadItem::ReceivedSlice>& received_slices) { |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 853 | if (base::ContainsKey(downloads_, id)) { |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 854 | NOTREACHED(); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 855 | return nullptr; |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 856 | } |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 857 | DCHECK(!base::ContainsKey(downloads_by_guid_, guid)); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 858 | DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
asanka | 038d58d | 2016-04-14 00:29:28 | [diff] [blame] | 859 | this, guid, id, current_path, target_path, url_chain, referrer_url, |
asanka | 3b4be12 | 2016-05-11 04:27:57 | [diff] [blame] | 860 | site_url, tab_url, tab_refererr_url, mime_type, original_mime_type, |
| 861 | start_time, end_time, etag, last_modified, received_bytes, total_bytes, |
shaktisahu | 60eb97f | 2017-03-03 08:53:23 | [diff] [blame] | 862 | hash, state, danger_type, interrupt_reason, opened, last_access_time, |
Min Qin | 218f1a53 | 2017-11-09 01:35:01 | [diff] [blame] | 863 | transient, received_slices); |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 864 | downloads_[id] = base::WrapUnique(item); |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 865 | downloads_by_guid_[guid] = item; |
ericwilligers | 254597b | 2016-10-17 10:32:31 | [diff] [blame] | 866 | for (auto& observer : observers_) |
| 867 | observer.OnDownloadCreated(this, item); |
pkasting | f527948 | 2016-07-27 02:18:20 | [diff] [blame] | 868 | DVLOG(20) << __func__ << "() download = " << item->DebugString(true); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 869 | return item; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 870 | } |
| 871 | |
Joy Ming | 13a8456 | 2017-12-02 00:42:17 | [diff] [blame] | 872 | void DownloadManagerImpl::PostInitialization( |
| 873 | DownloadInitializationDependency dependency) { |
| 874 | // If initialization has occurred (ie. in tests), skip post init steps. |
| 875 | if (initialized_) |
| 876 | return; |
| 877 | |
| 878 | switch (dependency) { |
| 879 | case DOWNLOAD_INITIALIZATION_DEPENDENCY_HISTORY_DB: |
| 880 | history_db_initialized_ = true; |
| 881 | break; |
| 882 | case DOWNLOAD_INITIALIZATION_DEPENDENCY_IN_PROGRESS_CACHE: |
| 883 | in_progress_cache_initialized_ = true; |
| 884 | break; |
| 885 | case DOWNLOAD_INITIALIZATION_DEPENDENCY_NONE: |
| 886 | default: |
| 887 | NOTREACHED(); |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | // Download manager is only initialized if both history db and in progress |
| 892 | // cache are initialized. |
| 893 | initialized_ = history_db_initialized_ && in_progress_cache_initialized_; |
| 894 | |
| 895 | if (initialized_) { |
| 896 | for (auto& observer : observers_) |
| 897 | observer.OnManagerInitialized(); |
| 898 | } |
xingliu | d64fde65 | 2017-05-24 07:04:33 | [diff] [blame] | 899 | } |
| 900 | |
| 901 | bool DownloadManagerImpl::IsManagerInitialized() const { |
| 902 | return initialized_; |
| 903 | } |
| 904 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 905 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 906 | int count = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 907 | for (const auto& it : downloads_) { |
| 908 | if (it.second->GetState() == DownloadItem::IN_PROGRESS) |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 909 | ++count; |
| 910 | } |
| 911 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 912 | } |
| 913 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 914 | int DownloadManagerImpl::NonMaliciousInProgressCount() const { |
| 915 | int count = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 916 | for (const auto& it : downloads_) { |
| 917 | if (it.second->GetState() == DownloadItem::IN_PROGRESS && |
| 918 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL && |
| 919 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT && |
| 920 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST && |
| 921 | it.second->GetDangerType() != |
[email protected] | 24eb517 | 2013-10-27 02:03:59 | [diff] [blame] | 922 | DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 923 | ++count; |
| 924 | } |
| 925 | } |
| 926 | return count; |
| 927 | } |
| 928 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 929 | DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 930 | return base::ContainsKey(downloads_, download_id) |
| 931 | ? downloads_[download_id].get() |
| 932 | : nullptr; |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) { |
skyostil | 66bd6791 | 2016-08-12 12:33:11 | [diff] [blame] | 936 | return base::ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid] |
| 937 | : nullptr; |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 938 | } |
| 939 | |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 940 | void DownloadManagerImpl::OnUrlDownloadStarted( |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 941 | std::unique_ptr<DownloadCreateInfo> download_create_info, |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 942 | std::unique_ptr<DownloadManager::InputStream> stream, |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 943 | const DownloadUrlParameters::OnStartedCallback& callback) { |
Min Qin | 0e4d82a | 2017-09-01 20:40:31 | [diff] [blame] | 944 | StartDownload(std::move(download_create_info), std::move(stream), callback); |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 945 | } |
| 946 | |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 947 | void DownloadManagerImpl::OnUrlDownloadStopped(UrlDownloadHandler* downloader) { |
| 948 | for (auto ptr = url_download_handlers_.begin(); |
| 949 | ptr != url_download_handlers_.end(); ++ptr) { |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 950 | if (ptr->get() == downloader) { |
Min Qin | 3cbdb8d | 2017-08-25 06:23:09 | [diff] [blame] | 951 | url_download_handlers_.erase(ptr); |
xingliu | 468824d | 2017-02-28 02:59:25 | [diff] [blame] | 952 | return; |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 957 | void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 958 | for (const auto& it : downloads_) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 959 | downloads->push_back(it.second.get()); |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 963 | void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 964 | int num_unopened = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 965 | for (const auto& it : downloads_) { |
avi | cebda792 | 2016-10-21 17:37:54 | [diff] [blame] | 966 | DownloadItemImpl* item = it.second.get(); |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 967 | if ((item->GetState() == DownloadItem::COMPLETE) && |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 968 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 969 | ++num_unopened; |
| 970 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 971 | RecordOpensOutstanding(num_unopened); |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 972 | |
[email protected] | 0baf592 | 2013-01-30 13:55:18 | [diff] [blame] | 973 | if (delegate_) |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 974 | delegate_->OpenDownload(download); |
| 975 | } |
| 976 | |
| 977 | void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 978 | if (delegate_) |
| 979 | delegate_->ShowDownloadInShell(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 980 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 981 | |
arthursonzogni | 3a4ca9f | 2017-12-07 17:58:34 | [diff] [blame^] | 982 | void DownloadManagerImpl::InterceptNavigationOnChecksComplete( |
| 983 | ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 984 | std::unique_ptr<ResourceRequest> resource_request, |
| 985 | std::vector<GURL> url_chain, |
| 986 | scoped_refptr<ResourceResponse> response, |
| 987 | net::CertStatus cert_status, |
| 988 | mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints, |
| 989 | bool is_download_allowed) { |
| 990 | if (!is_download_allowed) |
| 991 | return; |
| 992 | |
| 993 | BrowserThread::PostTask( |
| 994 | BrowserThread::IO, FROM_HERE, |
| 995 | base::BindOnce(&DownloadManagerImpl::CreateDownloadHandlerForNavigation, |
| 996 | weak_factory_.GetWeakPtr(), web_contents_getter, |
| 997 | std::move(resource_request), std::move(url_chain), |
| 998 | std::move(response), std::move(cert_status), |
| 999 | std::move(url_loader_client_endpoints))); |
| 1000 | } |
| 1001 | |
| 1002 | // static |
| 1003 | void DownloadManagerImpl::CreateDownloadHandlerForNavigation( |
| 1004 | base::WeakPtr<DownloadManagerImpl> download_manager, |
| 1005 | ResourceRequestInfo::WebContentsGetter web_contents_getter, |
| 1006 | std::unique_ptr<ResourceRequest> resource_request, |
| 1007 | std::vector<GURL> url_chain, |
| 1008 | scoped_refptr<ResourceResponse> response, |
| 1009 | net::CertStatus cert_status, |
| 1010 | mojom::URLLoaderClientEndpointsPtr url_loader_client_endpoints) { |
| 1011 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1012 | |
| 1013 | std::unique_ptr<ResourceDownloader> resource_downloader = |
| 1014 | ResourceDownloader::InterceptNavigationResponse( |
| 1015 | download_manager, std::move(resource_request), |
| 1016 | std::move(web_contents_getter), std::move(url_chain), |
| 1017 | std::move(response), std::move(cert_status), |
| 1018 | std::move(url_loader_client_endpoints)); |
| 1019 | |
| 1020 | BrowserThread::PostTask( |
| 1021 | BrowserThread::UI, FROM_HERE, |
| 1022 | base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler, |
| 1023 | download_manager, |
| 1024 | DownloadManagerImpl::UniqueUrlDownloadHandlerPtr( |
| 1025 | resource_downloader.release()))); |
| 1026 | } |
| 1027 | |
Min Qin | c60045c | 2017-10-17 03:02:22 | [diff] [blame] | 1028 | void DownloadManagerImpl::BeginDownloadInternal( |
| 1029 | std::unique_ptr<content::DownloadUrlParameters> params, |
| 1030 | uint32_t id) { |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 1031 | download::InProgressCache* in_progress_cache = |
| 1032 | GetBrowserContext()->GetDownloadManagerDelegate()->GetInProgressCache(); |
| 1033 | if (in_progress_cache) { |
Xing Liu | 3a08c5c | 2017-11-29 00:39:55 | [diff] [blame] | 1034 | in_progress_cache->AddOrReplaceEntry( |
| 1035 | download::DownloadEntry(params->guid(), params->request_origin(), |
| 1036 | ToDownloadSource(params->download_source()))); |
Joy Ming | da69d9a | 2017-11-13 20:22:22 | [diff] [blame] | 1037 | } |
| 1038 | |
Min Qin | c60045c | 2017-10-17 03:02:22 | [diff] [blame] | 1039 | if (base::FeatureList::IsEnabled(features::kNetworkService)) { |
| 1040 | std::unique_ptr<ResourceRequest> request = CreateResourceRequest( |
| 1041 | params.get()); |
| 1042 | StoragePartitionImpl* storage_partition = |
| 1043 | GetStoragePartition(browser_context_, params->render_process_host_id(), |
| 1044 | params->render_frame_host_routing_id()); |
| 1045 | BrowserThread::PostTaskAndReplyWithResult( |
| 1046 | BrowserThread::IO, FROM_HERE, |
| 1047 | base::BindOnce( |
| 1048 | &BeginResourceDownload, std::move(params), std::move(request), |
| 1049 | storage_partition->url_loader_factory_getter(), |
| 1050 | base::WrapRefCounted(storage_partition->GetFileSystemContext()), |
| 1051 | id, weak_factory_.GetWeakPtr()), |
| 1052 | base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler, |
| 1053 | weak_factory_.GetWeakPtr())); |
| 1054 | } else { |
| 1055 | BrowserThread::PostTaskAndReplyWithResult( |
| 1056 | BrowserThread::IO, FROM_HERE, |
| 1057 | base::BindOnce(&BeginDownload, std::move(params), |
| 1058 | browser_context_->GetResourceContext(), id, |
| 1059 | weak_factory_.GetWeakPtr()), |
| 1060 | base::BindOnce(&DownloadManagerImpl::AddUrlDownloadHandler, |
| 1061 | weak_factory_.GetWeakPtr())); |
| 1062 | } |
| 1063 | } |
| 1064 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 1065 | } // namespace content |