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