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