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