[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" |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 15 | #include "base/memory/weak_ptr.h" |
[email protected] | 95f861e | 2013-07-18 02:07:17 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 17 | #include "base/stl_util.h" |
[email protected] | 10994d13 | 2013-06-11 07:16:18 | [diff] [blame] | 18 | #include "base/strings/stringprintf.h" |
[email protected] | 40d11e0 | 2013-03-28 17:43:14 | [diff] [blame] | 19 | #include "base/strings/sys_string_conversions.h" |
[email protected] | d59d40c | 2012-08-08 18:18:37 | [diff] [blame] | 20 | #include "base/supports_user_data.h" |
[email protected] | eda5840 | 2011-09-21 19:32:02 | [diff] [blame] | 21 | #include "base/synchronization/lock.h" |
[email protected] | d2a8fb7 | 2010-01-21 05:31:42 | [diff] [blame] | 22 | #include "build/build_config.h" |
[email protected] | f8e92b5d | 2013-03-21 18:35:46 | [diff] [blame] | 23 | #include "content/browser/byte_stream.h" |
[email protected] | 71bf3f5e | 2011-08-15 21:05:22 | [diff] [blame] | 24 | #include "content/browser/download/download_create_info.h" |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 25 | #include "content/browser/download/download_file_factory.h" |
| 26 | #include "content/browser/download/download_item_factory.h" |
[email protected] | c09a8fd | 2011-11-21 19:54:50 | [diff] [blame] | 27 | #include "content/browser/download/download_item_impl.h" |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 28 | #include "content/browser/download/download_stats.h" |
[email protected] | 678c036 | 2012-12-05 08:02:44 | [diff] [blame] | 29 | #include "content/browser/loader/resource_dispatcher_host_impl.h" |
[email protected] | b3c41c0b | 2012-03-06 15:48:32 | [diff] [blame] | 30 | #include "content/browser/renderer_host/render_view_host_impl.h" |
[email protected] | 93ddb3c | 2012-04-11 21:44:29 | [diff] [blame] | 31 | #include "content/browser/web_contents/web_contents_impl.h" |
[email protected] | ccb79730 | 2011-12-15 16:55:11 | [diff] [blame] | 32 | #include "content/public/browser/browser_context.h" |
[email protected] | 87f3c08 | 2011-10-19 18:07:44 | [diff] [blame] | 33 | #include "content/public/browser/content_browser_client.h" |
[email protected] | bf3b08a | 2012-03-08 01:52:34 | [diff] [blame] | 34 | #include "content/public/browser/download_interrupt_reasons.h" |
[email protected] | 1bd0ef1 | 2011-10-20 05:24:17 | [diff] [blame] | 35 | #include "content/public/browser/download_manager_delegate.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 36 | #include "content/public/browser/download_url_parameters.h" |
[email protected] | ad50def5 | 2011-10-19 23:17:07 | [diff] [blame] | 37 | #include "content/public/browser/notification_service.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame] | 38 | #include "content/public/browser/notification_types.h" |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 39 | #include "content/public/browser/render_process_host.h" |
[email protected] | 94e2bbe | 2012-06-22 15:26:13 | [diff] [blame] | 40 | #include "content/public/browser/resource_context.h" |
[email protected] | 0bfbf88 | 2011-12-22 18:19:27 | [diff] [blame] | 41 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 42 | #include "content/public/common/referrer.h" |
mmenke | cbc2b71 | 2014-10-09 20:29:07 | [diff] [blame] | 43 | #include "net/base/elements_upload_data_stream.h" |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 44 | #include "net/base/load_flags.h" |
[email protected] | 2ca01e5 | 2013-10-31 22:05:19 | [diff] [blame] | 45 | #include "net/base/request_priority.h" |
[email protected] | f288ef0 | 2012-12-15 20:28:28 | [diff] [blame] | 46 | #include "net/base/upload_bytes_element_reader.h" |
[email protected] | eb79563 | 2012-09-01 00:19:31 | [diff] [blame] | 47 | #include "net/url_request/url_request_context.h" |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 48 | #include "url/origin.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 49 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 50 | namespace content { |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 51 | namespace { |
| 52 | |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 53 | scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> BeginDownload( |
| 54 | scoped_ptr<DownloadUrlParameters> params, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 55 | uint32_t download_id, |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 56 | base::WeakPtr<DownloadManagerImpl> download_manager) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 57 | DCHECK_CURRENTLY_ON(BrowserThread::IO); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 58 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 59 | scoped_ptr<net::URLRequest> url_request = |
| 60 | DownloadRequestCore::CreateRequestOnIOThread(download_id, params.get()); |
[email protected] | fc179dd1 | 2013-03-16 09:35:19 | [diff] [blame] | 61 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 62 | // If there's a valid renderer process associated with the request, then the |
| 63 | // request should be driven by the ResourceLoader. Pass it over to the |
| 64 | // ResourceDispatcherHostImpl which will in turn pass it along to the |
| 65 | // ResourceLoader. |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 66 | if (params->render_process_host_id() != -1) { |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 67 | DownloadInterruptReason reason = |
| 68 | ResourceDispatcherHostImpl::Get()->BeginDownload( |
| 69 | std::move(url_request), params->referrer(), |
| 70 | params->content_initiated(), params->resource_context(), |
| 71 | params->render_process_host_id(), |
| 72 | params->render_view_host_routing_id(), |
| 73 | params->render_frame_host_routing_id(), |
| 74 | params->do_not_prompt_for_login()); |
| 75 | |
| 76 | // If the download was accepted, the DownloadResourceHandler is now |
| 77 | // responsible for driving the request to completion. |
| 78 | if (reason == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 79 | return nullptr; |
| 80 | |
| 81 | // Otherwise, create an interrupted download. |
| 82 | scoped_ptr<DownloadCreateInfo> failed_created_info( |
| 83 | new DownloadCreateInfo(base::Time::Now(), net::BoundNetLog(), |
| 84 | make_scoped_ptr(new DownloadSaveInfo))); |
| 85 | failed_created_info->url_chain.push_back(params->url()); |
| 86 | failed_created_info->result = reason; |
| 87 | scoped_ptr<ByteStreamReader> empty_byte_stream; |
| 88 | BrowserThread::PostTask( |
| 89 | BrowserThread::UI, FROM_HERE, |
| 90 | base::Bind(&DownloadManager::StartDownload, download_manager, |
| 91 | base::Passed(&failed_created_info), |
| 92 | base::Passed(&empty_byte_stream), params->callback())); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 93 | return nullptr; |
| 94 | } |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 95 | |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 96 | return scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread>( |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 97 | UrlDownloader::BeginDownload(download_manager, std::move(url_request), |
| 98 | params->referrer()) |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 99 | .release()); |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 102 | class DownloadItemFactoryImpl : public DownloadItemFactory { |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 103 | public: |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 104 | DownloadItemFactoryImpl() {} |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 105 | ~DownloadItemFactoryImpl() override {} |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 106 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 107 | DownloadItemImpl* CreatePersistedItem( |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 108 | DownloadItemImplDelegate* delegate, |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 109 | const std::string& guid, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 110 | uint32_t download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 111 | const base::FilePath& current_path, |
| 112 | const base::FilePath& target_path, |
| 113 | const std::vector<GURL>& url_chain, |
| 114 | const GURL& referrer_url, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 115 | const std::string& mime_type, |
| 116 | const std::string& original_mime_type, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 117 | const base::Time& start_time, |
| 118 | const base::Time& end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 119 | const std::string& etag, |
| 120 | const std::string& last_modified, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 121 | int64_t received_bytes, |
| 122 | int64_t total_bytes, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 123 | DownloadItem::DownloadState state, |
| 124 | DownloadDangerType danger_type, |
| 125 | DownloadInterruptReason interrupt_reason, |
| 126 | bool opened, |
mohan.reddy | 0673e93 | 2014-10-07 16:17:38 | [diff] [blame] | 127 | const net::BoundNetLog& bound_net_log) override { |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 128 | return new DownloadItemImpl(delegate, |
| 129 | guid, |
| 130 | download_id, |
| 131 | current_path, |
| 132 | target_path, |
| 133 | url_chain, |
| 134 | referrer_url, |
| 135 | mime_type, |
| 136 | original_mime_type, |
| 137 | start_time, |
| 138 | end_time, |
| 139 | etag, |
| 140 | last_modified, |
| 141 | received_bytes, |
| 142 | total_bytes, |
| 143 | state, |
| 144 | danger_type, |
| 145 | interrupt_reason, |
| 146 | opened, |
| 147 | bound_net_log); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 148 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 149 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 150 | DownloadItemImpl* CreateActiveItem( |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 151 | DownloadItemImplDelegate* delegate, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 152 | uint32_t download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 153 | const DownloadCreateInfo& info, |
mohan.reddy | 0673e93 | 2014-10-07 16:17:38 | [diff] [blame] | 154 | const net::BoundNetLog& bound_net_log) override { |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 155 | return new DownloadItemImpl(delegate, download_id, info, bound_net_log); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 156 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 157 | |
dcheng | c2282aa | 2014-10-21 12:07:58 | [diff] [blame] | 158 | DownloadItemImpl* CreateSavePageItem( |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 159 | DownloadItemImplDelegate* delegate, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 160 | uint32_t download_id, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 161 | const base::FilePath& path, |
| 162 | const GURL& url, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 163 | const std::string& mime_type, |
| 164 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
mohan.reddy | 0673e93 | 2014-10-07 16:17:38 | [diff] [blame] | 165 | const net::BoundNetLog& bound_net_log) override { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 166 | return new DownloadItemImpl(delegate, download_id, path, url, mime_type, |
| 167 | std::move(request_handle), bound_net_log); |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 168 | } |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 169 | }; |
| 170 | |
[email protected] | a0ce328 | 2011-08-19 20:49:52 | [diff] [blame] | 171 | } // namespace |
| 172 | |
[email protected] | d25fda1 | 2012-06-12 17:05:03 | [diff] [blame] | 173 | DownloadManagerImpl::DownloadManagerImpl( |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 174 | net::NetLog* net_log, |
| 175 | BrowserContext* browser_context) |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 176 | : item_factory_(new DownloadItemFactoryImpl()), |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 177 | file_factory_(new DownloadFileFactory()), |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 178 | history_size_(0), |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 179 | shutdown_needed_(true), |
| 180 | browser_context_(browser_context), |
[email protected] | 33c20cd9 | 2012-06-14 22:02:50 | [diff] [blame] | 181 | delegate_(NULL), |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 182 | net_log_(net_log), |
| 183 | weak_factory_(this) { |
[email protected] | 1679869 | 2013-04-23 18:08:38 | [diff] [blame] | 184 | DCHECK(browser_context); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 185 | } |
| 186 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 187 | DownloadManagerImpl::~DownloadManagerImpl() { |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 188 | DCHECK(!shutdown_needed_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | } |
| 190 | |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 191 | DownloadItemImpl* DownloadManagerImpl::CreateActiveItem( |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 192 | uint32_t id, |
| 193 | const DownloadCreateInfo& info) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 194 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 195 | DCHECK(!ContainsKey(downloads_, id)); |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 196 | net::BoundNetLog bound_net_log = |
| 197 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 198 | DownloadItemImpl* download = |
[email protected] | 8f29835 | 2013-03-28 22:18:19 | [diff] [blame] | 199 | item_factory_->CreateActiveItem(this, id, info, bound_net_log); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 200 | downloads_[id] = download; |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 201 | downloads_by_guid_[download->GetGuid()] = download; |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 202 | return download; |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 205 | void DownloadManagerImpl::GetNextId(const DownloadIdCallback& callback) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 206 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 207 | if (delegate_) { |
| 208 | delegate_->GetNextId(callback); |
| 209 | return; |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 210 | } |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 211 | static uint32_t next_id = content::DownloadItem::kInvalidId + 1; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 212 | callback.Run(next_id++); |
[email protected] | 2909e34 | 2011-10-29 00:46:53 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 215 | void DownloadManagerImpl::DetermineDownloadTarget( |
| 216 | DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 217 | // Note that this next call relies on |
| 218 | // DownloadItemImplDelegate::DownloadTargetCallback and |
| 219 | // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 220 | // type. If the types ever diverge, gasket code will need to |
| 221 | // be written here. |
| 222 | if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 223 | base::FilePath target_path = item->GetForcedFilePath(); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 224 | // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 225 | callback.Run(target_path, |
| 226 | DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 227 | DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 228 | target_path); |
| 229 | } |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 232 | bool DownloadManagerImpl::ShouldCompleteDownload( |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 233 | DownloadItemImpl* item, const base::Closure& complete_callback) { |
| 234 | if (!delegate_ || |
| 235 | delegate_->ShouldCompleteDownload(item, complete_callback)) { |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 236 | return true; |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 237 | } |
| 238 | // Otherwise, the delegate has accepted responsibility to run the |
| 239 | // callback when the download is ready for completion. |
[email protected] | 9829962 | 2013-01-03 22:26:50 | [diff] [blame] | 240 | return false; |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 243 | bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension( |
| 244 | const base::FilePath& path) { |
[email protected] | 491aaa6 | 2012-06-07 03:50:18 | [diff] [blame] | 245 | if (!delegate_) |
| 246 | return false; |
| 247 | |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 248 | return delegate_->ShouldOpenFileBasedOnExtension(path); |
| 249 | } |
| 250 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 251 | bool DownloadManagerImpl::ShouldOpenDownload( |
| 252 | DownloadItemImpl* item, const ShouldOpenDownloadCallback& callback) { |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 253 | if (!delegate_) |
| 254 | return true; |
| 255 | |
[email protected] | bde0e4f | 2012-11-08 22:49:59 | [diff] [blame] | 256 | // Relies on DownloadItemImplDelegate::ShouldOpenDownloadCallback and |
| 257 | // DownloadManagerDelegate::DownloadOpenDelayedCallback "just happening" |
| 258 | // to have the same type :-}. |
| 259 | return delegate_->ShouldOpenDownload(item, callback); |
[email protected] | 18710a42a | 2012-10-17 19:50:43 | [diff] [blame] | 260 | } |
| 261 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 262 | void DownloadManagerImpl::SetDelegate(DownloadManagerDelegate* delegate) { |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 263 | delegate_ = delegate; |
| 264 | } |
| 265 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 266 | DownloadManagerDelegate* DownloadManagerImpl::GetDelegate() const { |
[email protected] | b488b5a5 | 2012-06-06 17:01:28 | [diff] [blame] | 267 | return delegate_; |
| 268 | } |
| 269 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 270 | void DownloadManagerImpl::Shutdown() { |
anujk.sharma | d9a2874 | 2014-11-12 18:27:14 | [diff] [blame] | 271 | DVLOG(20) << __FUNCTION__ << "()" |
| 272 | << " shutdown_needed_ = " << shutdown_needed_; |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 273 | if (!shutdown_needed_) |
| 274 | return; |
| 275 | shutdown_needed_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | |
[email protected] | 75e51b5 | 2012-02-04 16:57:54 | [diff] [blame] | 277 | FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
[email protected] | fb4f8d90 | 2011-09-16 06:07:08 | [diff] [blame] | 278 | // TODO(benjhayden): Consider clearing observers_. |
[email protected] | 326a6a9 | 2010-09-10 20:21:13 | [diff] [blame] | 279 | |
[email protected] | fe75227 | 2013-05-29 18:57:45 | [diff] [blame] | 280 | // If there are in-progress downloads, cancel them. This also goes for |
| 281 | // dangerous downloads which will remain in history if they aren't explicitly |
| 282 | // accepted or discarded. Canceling will remove the intermediate download |
| 283 | // file. |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 284 | for (const auto& it : downloads_) { |
| 285 | DownloadItemImpl* download = it.second; |
[email protected] | fe75227 | 2013-05-29 18:57:45 | [diff] [blame] | 286 | if (download->GetState() == DownloadItem::IN_PROGRESS) |
[email protected] | 93af227 | 2011-09-21 18:29:17 | [diff] [blame] | 287 | download->Cancel(false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | } |
[email protected] | 7745ff0 | 2012-10-01 00:09:24 | [diff] [blame] | 289 | STLDeleteValues(&downloads_); |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 290 | downloads_by_guid_.clear(); |
svaldez | 9a07ab33 | 2016-01-12 18:29:01 | [diff] [blame] | 291 | url_downloaders_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | |
[email protected] | 41f558fb | 2012-01-09 22:59:58 | [diff] [blame] | 293 | // We'll have nothing more to report to the observers after this point. |
| 294 | observers_.Clear(); |
| 295 | |
[email protected] | b441a849 | 2012-06-06 14:55:57 | [diff] [blame] | 296 | if (delegate_) |
| 297 | delegate_->Shutdown(); |
[email protected] | 4766544 | 2012-07-27 02:31:22 | [diff] [blame] | 298 | delegate_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | } |
| 300 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 301 | void DownloadManagerImpl::StartDownload( |
[email protected] | 2bddd207 | 2012-06-18 16:16:51 | [diff] [blame] | 302 | scoped_ptr<DownloadCreateInfo> info, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 303 | scoped_ptr<ByteStreamReader> stream, |
| 304 | const DownloadUrlParameters::OnStartedCallback& on_started) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 305 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 306 | DCHECK(info); |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 307 | // |stream| is only non-nil if the download request was successful. |
| 308 | DCHECK((info->result == DOWNLOAD_INTERRUPT_REASON_NONE && stream.get()) || |
| 309 | (info->result != DOWNLOAD_INTERRUPT_REASON_NONE && !stream.get())); |
| 310 | DVLOG(20) << __FUNCTION__ << "()" |
| 311 | << " result=" << DownloadInterruptReasonToString(info->result); |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 312 | uint32_t download_id = info->download_id; |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 313 | const bool new_download = (download_id == content::DownloadItem::kInvalidId); |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 314 | base::Callback<void(uint32_t)> got_id(base::Bind( |
| 315 | &DownloadManagerImpl::StartDownloadWithId, weak_factory_.GetWeakPtr(), |
| 316 | base::Passed(&info), base::Passed(&stream), on_started, new_download)); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 317 | if (new_download) { |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 318 | GetNextId(got_id); |
| 319 | } else { |
| 320 | got_id.Run(download_id); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | void DownloadManagerImpl::StartDownloadWithId( |
| 325 | scoped_ptr<DownloadCreateInfo> info, |
| 326 | scoped_ptr<ByteStreamReader> stream, |
| 327 | const DownloadUrlParameters::OnStartedCallback& on_started, |
| 328 | bool new_download, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 329 | uint32_t id) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 330 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 331 | DCHECK_NE(content::DownloadItem::kInvalidId, id); |
| 332 | DownloadItemImpl* download = NULL; |
| 333 | if (new_download) { |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 334 | download = CreateActiveItem(id, *info); |
| 335 | } else { |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 336 | DownloadMap::iterator item_iterator = downloads_.find(id); |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 337 | // Trying to resume an interrupted download. |
[email protected] | 7b3eeca4 | 2013-05-23 20:56:37 | [diff] [blame] | 338 | if (item_iterator == downloads_.end() || |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 339 | (item_iterator->second->GetState() == DownloadItem::CANCELLED)) { |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 340 | // If the download is no longer known to the DownloadManager, then it was |
[email protected] | 7b3eeca4 | 2013-05-23 20:56:37 | [diff] [blame] | 341 | // removed after it was resumed. Ignore. If the download is cancelled |
| 342 | // while resuming, then also ignore the request. |
asanka | 6411415 | 2015-12-21 21:28:49 | [diff] [blame] | 343 | info->request_handle->CancelRequest(); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 344 | if (!on_started.is_null()) |
[email protected] | 7f391888 | 2014-01-14 06:14:56 | [diff] [blame] | 345 | on_started.Run(NULL, DOWNLOAD_INTERRUPT_REASON_USER_CANCELED); |
asanka | 81c2214f | 2016-01-13 20:05:54 | [diff] [blame] | 346 | // The ByteStreamReader lives and dies on the FILE thread. |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 347 | if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) |
| 348 | BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, |
| 349 | stream.release()); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 350 | return; |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 351 | } |
| 352 | download = item_iterator->second; |
[email protected] | 89ec81f | 2013-05-15 19:20:02 | [diff] [blame] | 353 | } |
| 354 | |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 355 | base::FilePath default_download_directory; |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 356 | if (delegate_) { |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 357 | base::FilePath website_save_directory; // Unused |
| 358 | bool skip_dir_check = false; // Unused |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 359 | delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 360 | &default_download_directory, &skip_dir_check); |
[email protected] | 96bfed05 | 2012-09-15 22:21:01 | [diff] [blame] | 361 | } |
| 362 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 363 | scoped_ptr<DownloadFile> download_file; |
[email protected] | 492c009 | 2013-08-10 13:49:48 | [diff] [blame] | 364 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 365 | if (info->result == DOWNLOAD_INTERRUPT_REASON_NONE) { |
| 366 | DCHECK(stream.get()); |
| 367 | download_file.reset(file_factory_->CreateFile( |
| 368 | *info->save_info, default_download_directory, info->url(), |
| 369 | info->referrer_url, delegate_ && delegate_->GenerateFileHash(), |
| 370 | std::move(info->save_info->file), std::move(stream), |
| 371 | download->GetBoundNetLog(), download->DestinationObserverAsWeakPtr())); |
| 372 | |
| 373 | if (download_file.get() && delegate_) { |
| 374 | download_file->SetClientGuid( |
| 375 | delegate_->ApplicationClientIdForFileScanning()); |
| 376 | } |
[email protected] | 492c009 | 2013-08-10 13:49:48 | [diff] [blame] | 377 | } |
| 378 | |
asanka | 00b621f | 2016-02-19 18:09:23 | [diff] [blame] | 379 | download->Start(std::move(download_file), std::move(info->request_handle), |
| 380 | *info); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 381 | |
[email protected] | 381af49 | 2013-03-28 01:56:22 | [diff] [blame] | 382 | // For interrupted downloads, Start() will transition the state to |
| 383 | // IN_PROGRESS and consumers will be notified via OnDownloadUpdated(). |
| 384 | // For new downloads, we notify here, rather than earlier, so that |
| 385 | // the download_file is bound to download and all the usual |
| 386 | // setters (e.g. Cancel) work. |
| 387 | if (new_download) |
| 388 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
[email protected] | 96c3bdbaa | 2012-08-31 16:39:54 | [diff] [blame] | 389 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 390 | if (!on_started.is_null()) |
[email protected] | 7f391888 | 2014-01-14 06:14:56 | [diff] [blame] | 391 | on_started.Run(download, DOWNLOAD_INTERRUPT_REASON_NONE); |
[email protected] | 287b86b | 2011-02-26 00:11:35 | [diff] [blame] | 392 | } |
| 393 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 394 | void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 395 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 396 | for (const auto& it : downloads_) { |
| 397 | DownloadItemImpl* item = it.second; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 398 | CheckForFileRemoval(item); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 399 | } |
| 400 | } |
| 401 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 402 | void DownloadManagerImpl::CheckForFileRemoval(DownloadItemImpl* download_item) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 403 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 404 | if ((download_item->GetState() == DownloadItem::COMPLETE) && |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 405 | !download_item->GetFileExternallyRemoved() && |
| 406 | delegate_) { |
| 407 | delegate_->CheckForFileExistence( |
| 408 | download_item, |
| 409 | base::Bind(&DownloadManagerImpl::OnFileExistenceChecked, |
[email protected] | eba4a4d | 2013-05-29 02:18:06 | [diff] [blame] | 410 | weak_factory_.GetWeakPtr(), download_item->GetId())); |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 414 | void DownloadManagerImpl::OnFileExistenceChecked(uint32_t download_id, |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 415 | bool result) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 416 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 417 | if (!result) { // File does not exist. |
| 418 | if (ContainsKey(downloads_, download_id)) |
[email protected] | 1c63da0 | 2014-01-17 17:09:51 | [diff] [blame] | 419 | downloads_[download_id]->OnDownloadedFileRemoved(); |
[email protected] | 4c544cd | 2013-01-25 07:54:16 | [diff] [blame] | 420 | } |
[email protected] | 9fc11467 | 2011-06-15 08:17:48 | [diff] [blame] | 421 | } |
| 422 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 423 | BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 424 | return browser_context_; |
| 425 | } |
| 426 | |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 427 | void DownloadManagerImpl::CreateSavePackageDownloadItem( |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 428 | const base::FilePath& main_file_path, |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 429 | const GURL& page_url, |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 430 | const std::string& mime_type, |
[email protected] | b375601 | 2013-03-06 17:43:02 | [diff] [blame] | 431 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 432 | const DownloadItemImplCreated& item_created) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 433 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 434 | GetNextId(base::Bind( |
| 435 | &DownloadManagerImpl::CreateSavePackageDownloadItemWithId, |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 436 | weak_factory_.GetWeakPtr(), main_file_path, page_url, mime_type, |
| 437 | base::Passed(std::move(request_handle)), item_created)); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void DownloadManagerImpl::CreateSavePackageDownloadItemWithId( |
| 441 | const base::FilePath& main_file_path, |
| 442 | const GURL& page_url, |
| 443 | const std::string& mime_type, |
| 444 | scoped_ptr<DownloadRequestHandleInterface> request_handle, |
| 445 | const DownloadItemImplCreated& item_created, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 446 | uint32_t id) { |
mostynb | fbcdc27a | 2015-03-13 17:58:52 | [diff] [blame] | 447 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 448 | DCHECK_NE(content::DownloadItem::kInvalidId, id); |
| 449 | DCHECK(!ContainsKey(downloads_, id)); |
[email protected] | ef17c9a | 2012-02-09 05:08:09 | [diff] [blame] | 450 | net::BoundNetLog bound_net_log = |
| 451 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 452 | DownloadItemImpl* download_item = item_factory_->CreateSavePageItem( |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 453 | this, id, main_file_path, page_url, mime_type, std::move(request_handle), |
[email protected] | 6474b11 | 2012-05-04 19:35:27 | [diff] [blame] | 454 | bound_net_log); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 455 | downloads_[download_item->GetId()] = download_item; |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 456 | DCHECK(!ContainsKey(downloads_by_guid_, download_item->GetGuid())); |
| 457 | downloads_by_guid_[download_item->GetGuid()] = download_item; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 458 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated( |
| 459 | this, download_item)); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 460 | if (!item_created.is_null()) |
| 461 | item_created.Run(download_item); |
[email protected] | fc03de2 | 2011-12-06 23:28:12 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | df58aa8 | 2013-01-24 21:54:00 | [diff] [blame] | 464 | void DownloadManagerImpl::OnSavePackageSuccessfullyFinished( |
| 465 | DownloadItem* download_item) { |
| 466 | FOR_EACH_OBSERVER(Observer, observers_, |
| 467 | OnSavePackageSuccessfullyFinished(this, download_item)); |
| 468 | } |
| 469 | |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 470 | // Resume a download of a specific URL. We send the request to the |
| 471 | // ResourceDispatcherHost, and let it send us responses like a regular |
| 472 | // download. |
| 473 | void DownloadManagerImpl::ResumeInterruptedDownload( |
| 474 | scoped_ptr<content::DownloadUrlParameters> params, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 475 | uint32_t id) { |
[email protected] | 11325a2 | 2013-07-30 23:02:26 | [diff] [blame] | 476 | RecordDownloadSource(INITIATED_BY_RESUMPTION); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 477 | BrowserThread::PostTaskAndReplyWithResult( |
| 478 | BrowserThread::IO, FROM_HERE, |
| 479 | base::Bind(&BeginDownload, base::Passed(¶ms), id, |
| 480 | weak_factory_.GetWeakPtr()), |
| 481 | base::Bind(&DownloadManagerImpl::AddUrlDownloader, |
| 482 | weak_factory_.GetWeakPtr())); |
[email protected] | 8d68a3e0 | 2013-01-12 15:57:10 | [diff] [blame] | 483 | } |
| 484 | |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 485 | void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 486 | scoped_ptr<DownloadItemFactory> item_factory) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 487 | item_factory_ = std::move(item_factory); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 491 | scoped_ptr<DownloadFileFactory> file_factory) { |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 492 | file_factory_ = std::move(file_factory); |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 493 | } |
| 494 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 495 | DownloadFileFactory* DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
[email protected] | 53ac00e8 | 2012-10-18 20:59:20 | [diff] [blame] | 496 | return file_factory_.get(); |
| 497 | } |
| 498 | |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 499 | void DownloadManagerImpl::DownloadRemoved(DownloadItemImpl* download) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 500 | if (!download) |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 501 | return; |
| 502 | |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 503 | downloads_by_guid_.erase(download->GetGuid()); |
| 504 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 505 | uint32_t download_id = download->GetId(); |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 506 | if (downloads_.erase(download_id) == 0) |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 507 | return; |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 508 | delete download; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 509 | } |
| 510 | |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 511 | void DownloadManagerImpl::AddUrlDownloader( |
| 512 | scoped_ptr<UrlDownloader, BrowserThread::DeleteOnIOThread> downloader) { |
| 513 | if (downloader) |
dcheng | 36b6aec9 | 2015-12-26 06:16:36 | [diff] [blame] | 514 | url_downloaders_.push_back(std::move(downloader)); |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | void DownloadManagerImpl::RemoveUrlDownloader(UrlDownloader* downloader) { |
| 518 | for (auto ptr = url_downloaders_.begin(); ptr != url_downloaders_.end(); |
| 519 | ++ptr) { |
| 520 | if (ptr->get() == downloader) { |
| 521 | url_downloaders_.erase(ptr); |
| 522 | return; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 527 | namespace { |
| 528 | |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 529 | bool EmptyFilter(const GURL& url) { |
| 530 | return true; |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 531 | } |
| 532 | |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 533 | bool RemoveDownloadByURLAndTime( |
| 534 | const base::Callback<bool(const GURL&)>& url_filter, |
| 535 | base::Time remove_begin, |
| 536 | base::Time remove_end, |
| 537 | const DownloadItemImpl* download_item) { |
| 538 | return url_filter.Run(download_item->GetURL()) && |
| 539 | download_item->GetStartTime() >= remove_begin && |
| 540 | (remove_end.is_null() || download_item->GetStartTime() < remove_end); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | } // namespace |
| 544 | |
| 545 | int DownloadManagerImpl::RemoveDownloads(const DownloadRemover& remover) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 546 | int count = 0; |
| 547 | DownloadMap::const_iterator it = downloads_.begin(); |
| 548 | while (it != downloads_.end()) { |
[email protected] | db1d8f7 | 2012-07-13 19:23:16 | [diff] [blame] | 549 | DownloadItemImpl* download = it->second; |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 550 | |
| 551 | // Increment done here to protect against invalidation below. |
| 552 | ++it; |
| 553 | |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 554 | if (download->GetState() != DownloadItem::IN_PROGRESS && |
| 555 | remover.Run(download)) { |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 556 | download->Remove(); |
| 557 | count++; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 558 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 559 | } |
[email protected] | 237af6d | 2013-03-15 23:45:28 | [diff] [blame] | 560 | return count; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 561 | } |
| 562 | |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 563 | int DownloadManagerImpl::RemoveDownloadsByURLAndTime( |
| 564 | const base::Callback<bool(const GURL&)>& url_filter, |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 565 | base::Time remove_begin, |
| 566 | base::Time remove_end) { |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 567 | return RemoveDownloads(base::Bind(&RemoveDownloadByURLAndTime, |
| 568 | url_filter, |
| 569 | remove_begin, remove_end)); |
ttr314 | 81dc54b | 2015-08-06 20:11:26 | [diff] [blame] | 570 | } |
| 571 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 572 | int DownloadManagerImpl::RemoveAllDownloads() { |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 573 | const base::Callback<bool(const GURL&)> empty_filter = |
| 574 | base::Bind(&EmptyFilter); |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 575 | // The null times make the date range unbounded. |
msramek | d9c162a | 2016-02-23 11:17:21 | [diff] [blame] | 576 | int num_deleted = RemoveDownloadsByURLAndTime( |
| 577 | empty_filter, base::Time(), base::Time()); |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 578 | RecordClearAllSize(num_deleted); |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 579 | return num_deleted; |
[email protected] | d41355e6f | 2009-04-07 21:21:12 | [diff] [blame] | 580 | } |
| 581 | |
[email protected] | 0d4e30c | 2012-01-28 00:47:53 | [diff] [blame] | 582 | void DownloadManagerImpl::DownloadUrl( |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 583 | scoped_ptr<DownloadUrlParameters> params) { |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 584 | if (params->post_id() >= 0) { |
| 585 | // Check this here so that the traceback is more useful. |
| 586 | DCHECK(params->prefer_cache()); |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 587 | DCHECK_EQ("POST", params->method()); |
[email protected] | c5a5c084 | 2012-05-04 20:05:14 | [diff] [blame] | 588 | } |
svaldez | 6901c90 | 2015-12-10 18:57:33 | [diff] [blame] | 589 | BrowserThread::PostTaskAndReplyWithResult( |
| 590 | BrowserThread::IO, FROM_HERE, |
| 591 | base::Bind(&BeginDownload, base::Passed(¶ms), |
| 592 | content::DownloadItem::kInvalidId, weak_factory_.GetWeakPtr()), |
| 593 | base::Bind(&DownloadManagerImpl::AddUrlDownloader, |
| 594 | weak_factory_.GetWeakPtr())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 595 | } |
| 596 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 597 | void DownloadManagerImpl::AddObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | observers_.AddObserver(observer); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 599 | } |
| 600 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 601 | void DownloadManagerImpl::RemoveObserver(Observer* observer) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 602 | observers_.RemoveObserver(observer); |
| 603 | } |
| 604 | |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 605 | DownloadItem* DownloadManagerImpl::CreateDownloadItem( |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 606 | const std::string& guid, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 607 | uint32_t id, |
[email protected] | 2dec8ec | 2013-02-07 19:20:34 | [diff] [blame] | 608 | const base::FilePath& current_path, |
| 609 | const base::FilePath& target_path, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 610 | const std::vector<GURL>& url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 611 | const GURL& referrer_url, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 612 | const std::string& mime_type, |
| 613 | const std::string& original_mime_type, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 614 | const base::Time& start_time, |
| 615 | const base::Time& end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 616 | const std::string& etag, |
| 617 | const std::string& last_modified, |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 618 | int64_t received_bytes, |
| 619 | int64_t total_bytes, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 620 | DownloadItem::DownloadState state, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 621 | DownloadDangerType danger_type, |
| 622 | DownloadInterruptReason interrupt_reason, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 623 | bool opened) { |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 624 | if (ContainsKey(downloads_, id)) { |
| 625 | NOTREACHED(); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 626 | return NULL; |
[email protected] | 6065748f | 2013-10-29 01:07:43 | [diff] [blame] | 627 | } |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 628 | DCHECK(!ContainsKey(downloads_by_guid_, guid)); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 629 | DownloadItemImpl* item = item_factory_->CreatePersistedItem( |
| 630 | this, |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 631 | guid, |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 632 | id, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 633 | current_path, |
| 634 | target_path, |
| 635 | url_chain, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 636 | referrer_url, |
[email protected] | 0e64856 | 2014-06-12 19:39:45 | [diff] [blame] | 637 | mime_type, |
| 638 | original_mime_type, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 639 | start_time, |
| 640 | end_time, |
[email protected] | 56cd594 | 2013-08-08 23:53:21 | [diff] [blame] | 641 | etag, |
| 642 | last_modified, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 643 | received_bytes, |
| 644 | total_bytes, |
| 645 | state, |
[email protected] | 87677469 | 2013-02-03 17:20:15 | [diff] [blame] | 646 | danger_type, |
| 647 | interrupt_reason, |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 648 | opened, |
| 649 | net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD)); |
[email protected] | 530047e | 2013-07-12 17:02:25 | [diff] [blame] | 650 | downloads_[id] = item; |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 651 | downloads_by_guid_[guid] = item; |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 652 | FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, item)); |
anujk.sharma | d9a2874 | 2014-11-12 18:27:14 | [diff] [blame] | 653 | DVLOG(20) << __FUNCTION__ << "() download = " << item->DebugString(true); |
[email protected] | 3d95e54 | 2012-11-20 00:52:08 | [diff] [blame] | 654 | return item; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 655 | } |
| 656 | |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 657 | int DownloadManagerImpl::InProgressCount() const { |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 658 | int count = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 659 | for (const auto& it : downloads_) { |
| 660 | if (it.second->GetState() == DownloadItem::IN_PROGRESS) |
[email protected] | 007e741 | 2012-03-13 20:10:56 | [diff] [blame] | 661 | ++count; |
| 662 | } |
| 663 | return count; |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 664 | } |
| 665 | |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 666 | int DownloadManagerImpl::NonMaliciousInProgressCount() const { |
| 667 | int count = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 668 | for (const auto& it : downloads_) { |
| 669 | if (it.second->GetState() == DownloadItem::IN_PROGRESS && |
| 670 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_URL && |
| 671 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT && |
| 672 | it.second->GetDangerType() != DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST && |
| 673 | it.second->GetDangerType() != |
[email protected] | 24eb517 | 2013-10-27 02:03:59 | [diff] [blame] | 674 | DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED) { |
[email protected] | 422a7d1 | 2013-10-21 12:10:42 | [diff] [blame] | 675 | ++count; |
| 676 | } |
| 677 | } |
| 678 | return count; |
| 679 | } |
| 680 | |
avi | b734894 | 2015-12-25 20:57:10 | [diff] [blame] | 681 | DownloadItem* DownloadManagerImpl::GetDownload(uint32_t download_id) { |
asanka | eef62b0 | 2016-03-14 21:23:11 | [diff] [blame] | 682 | return ContainsKey(downloads_, download_id) ? downloads_[download_id] |
| 683 | : nullptr; |
| 684 | } |
| 685 | |
| 686 | DownloadItem* DownloadManagerImpl::GetDownloadByGuid(const std::string& guid) { |
| 687 | DCHECK(guid == base::ToUpperASCII(guid)); |
| 688 | return ContainsKey(downloads_by_guid_, guid) ? downloads_by_guid_[guid] |
| 689 | : nullptr; |
[email protected] | ba7895d | 2012-06-22 19:02:52 | [diff] [blame] | 690 | } |
| 691 | |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 692 | void DownloadManagerImpl::GetAllDownloads(DownloadVector* downloads) { |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 693 | for (const auto& it : downloads_) { |
| 694 | downloads->push_back(it.second); |
[email protected] | 9f1f409 | 2012-09-10 21:18:04 | [diff] [blame] | 695 | } |
| 696 | } |
| 697 | |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 698 | void DownloadManagerImpl::OpenDownload(DownloadItemImpl* download) { |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 699 | int num_unopened = 0; |
lukasza | 921fabfa | 2016-02-05 19:51:48 | [diff] [blame] | 700 | for (const auto& it : downloads_) { |
| 701 | DownloadItemImpl* item = it.second; |
[email protected] | 803036e0 | 2013-06-15 17:08:47 | [diff] [blame] | 702 | if ((item->GetState() == DownloadItem::COMPLETE) && |
[email protected] | 09ea72c742 | 2012-07-02 20:35:40 | [diff] [blame] | 703 | !item->GetOpened()) |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 704 | ++num_unopened; |
| 705 | } |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 706 | RecordOpensOutstanding(num_unopened); |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 707 | |
[email protected] | 0baf592 | 2013-01-30 13:55:18 | [diff] [blame] | 708 | if (delegate_) |
[email protected] | ffc0c2d | 2013-01-21 15:32:12 | [diff] [blame] | 709 | delegate_->OpenDownload(download); |
| 710 | } |
| 711 | |
| 712 | void DownloadManagerImpl::ShowDownloadInShell(DownloadItemImpl* download) { |
| 713 | if (delegate_) |
| 714 | delegate_->ShowDownloadInShell(download); |
[email protected] | da4a558 | 2011-10-17 19:08:06 | [diff] [blame] | 715 | } |
[email protected] | 5656f8a | 2011-11-17 16:12:58 | [diff] [blame] | 716 | |
[email protected] | 3586962 | 2012-10-26 23:23:55 | [diff] [blame] | 717 | } // namespace content |