Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Peter E Conn | 2352ee6 | 2025-07-30 17:12:38 | [diff] [blame] | 5 | #include "content/browser/preloading/resolve_host_client_impl.h" |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 6 | |
| 7 | #include <utility> |
| 8 | |
Minoru Chikamune | b7e2ea8e | 2025-02-18 04:21:00 | [diff] [blame] | 9 | #include "base/check_is_test.h" |
Avi Drissman | 9269d4ed | 2023-01-07 01:38:06 | [diff] [blame] | 10 | #include "base/functional/bind.h" |
Colin Blundell | e7e3311 | 2022-03-16 10:18:38 | [diff] [blame] | 11 | #include "base/metrics/histogram_macros.h" |
Colin Blundell | 85157ec | 2022-03-21 17:31:45 | [diff] [blame] | 12 | #include "base/task/common/task_annotator.h" |
Alex Clarke | f9f39aee | 2019-03-20 13:17:26 | [diff] [blame] | 13 | #include "content/public/browser/browser_task_traits.h" |
| 14 | #include "content/public/browser/browser_thread.h" |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 15 | #include "net/base/host_port_pair.h" |
| 16 | #include "net/base/net_errors.h" |
Matt Menke | 23a96009 | 2019-12-05 18:35:40 | [diff] [blame] | 17 | #include "net/base/network_isolation_key.h" |
Alexandr Ilin | bf27433 | 2018-08-21 08:34:33 | [diff] [blame] | 18 | #include "net/base/request_priority.h" |
Katharine Daly | 0b58af0 | 2019-11-22 14:59:49 | [diff] [blame] | 19 | #include "net/dns/public/resolve_error_info.h" |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 20 | #include "services/network/public/mojom/network_context.mojom.h" |
| 21 | #include "url/gurl.h" |
Tsuyoshi Horo | 4746c14 | 2022-08-29 22:42:15 | [diff] [blame] | 22 | #include "url/scheme_host_port.h" |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 23 | |
Peter E Conn | 2352ee6 | 2025-07-30 17:12:38 | [diff] [blame] | 24 | namespace content { |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 25 | |
| 26 | ResolveHostClientImpl::ResolveHostClientImpl( |
| 27 | const GURL& url, |
Brianna Goldstein | 581e2af8 | 2022-10-20 13:56:30 | [diff] [blame] | 28 | const net::NetworkAnonymizationKey& network_anonymization_key, |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 29 | ResolveHostCallback callback, |
| 30 | network::mojom::NetworkContext* network_context) |
Miyoung Shin | 5e36fee | 2019-10-15 01:59:53 | [diff] [blame] | 31 | : callback_(std::move(callback)) { |
Alex Clarke | f9f39aee | 2019-03-20 13:17:26 | [diff] [blame] | 32 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
Miyoung Shin | 5e36fee | 2019-10-15 01:59:53 | [diff] [blame] | 33 | |
Alexandr Ilin | bf27433 | 2018-08-21 08:34:33 | [diff] [blame] | 34 | network::mojom::ResolveHostParametersPtr parameters = |
| 35 | network::mojom::ResolveHostParameters::New(); |
| 36 | parameters->initial_priority = net::RequestPriority::IDLE; |
| 37 | parameters->is_speculative = true; |
Kenichi Ishibashi | 5adcf4c | 2021-08-06 10:46:52 | [diff] [blame] | 38 | parameters->purpose = |
| 39 | network::mojom::ResolveHostParameters::Purpose::kPreconnect; |
Colin Blundell | e7e3311 | 2022-03-16 10:18:38 | [diff] [blame] | 40 | resolve_host_start_time_ = base::TimeTicks::Now(); |
Tsuyoshi Horo | 4746c14 | 2022-08-29 22:42:15 | [diff] [blame] | 41 | // Intentionally using a SchemeHostPort. Resolving http:// scheme host will |
| 42 | // fail when a HTTPS resource record exists due to DNS-based scheme upgrade |
| 43 | // functionality. |
| 44 | network_context->ResolveHost( |
| 45 | network::mojom::HostResolverHost::NewSchemeHostPort( |
| 46 | url::SchemeHostPort(url)), |
Brianna Goldstein | 581e2af8 | 2022-10-20 13:56:30 | [diff] [blame] | 47 | network_anonymization_key, std::move(parameters), |
Tsuyoshi Horo | 4746c14 | 2022-08-29 22:42:15 | [diff] [blame] | 48 | receiver_.BindNewPipeAndPassRemote()); |
Miyoung Shin | 5e36fee | 2019-10-15 01:59:53 | [diff] [blame] | 49 | receiver_.set_disconnect_handler(base::BindOnce( |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 50 | &ResolveHostClientImpl::OnConnectionError, base::Unretained(this))); |
| 51 | } |
| 52 | |
| 53 | ResolveHostClientImpl::~ResolveHostClientImpl() = default; |
| 54 | |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 55 | void ResolveHostClientImpl::OnComplete( |
| 56 | int result, |
Katharine Daly | 238cb7c | 2019-11-22 08:10:31 | [diff] [blame] | 57 | const net::ResolveErrorInfo& resolve_error_info, |
David Benjamin | 1627af9 | 2025-07-22 11:13:38 | [diff] [blame] | 58 | const net::AddressList& resolved_addresses, |
| 59 | const net::HostResolverEndpointResults& alternative_endpoints) { |
Colin Blundell | e7e3311 | 2022-03-16 10:18:38 | [diff] [blame] | 60 | UMA_HISTOGRAM_TIMES("Navigation.Preconnect.ResolveHostLatency", |
| 61 | base::TimeTicks::Now() - resolve_host_start_time_); |
Colin Blundell | 85157ec | 2022-03-21 17:31:45 | [diff] [blame] | 62 | |
| 63 | auto* task = base::TaskAnnotator::CurrentTaskForThread(); |
Minoru Chikamune | b7e2ea8e | 2025-02-18 04:21:00 | [diff] [blame] | 64 | if (!task) { |
| 65 | // The `task` can be null in base::ScopedMockTimeMessageLoopTaskRunner scope |
| 66 | // in test. |
| 67 | CHECK_IS_TEST(); |
| 68 | } |
Colin Blundell | 85157ec | 2022-03-21 17:31:45 | [diff] [blame] | 69 | // As this method is executed as a callback from a Mojo call, it should be |
| 70 | // executed via RunTask() and thus have a non-delayed PendingTask associated |
| 71 | // with it. |
Minoru Chikamune | b7e2ea8e | 2025-02-18 04:21:00 | [diff] [blame] | 72 | DCHECK(!task || task->delayed_run_time.is_null()); |
Colin Blundell | 85157ec | 2022-03-21 17:31:45 | [diff] [blame] | 73 | |
| 74 | // The task will have a null |queue_time| if run synchronously (this happens |
| 75 | // in unit tests, for example). |
Minoru Chikamune | b7e2ea8e | 2025-02-18 04:21:00 | [diff] [blame] | 76 | base::TimeTicks queue_time = (task && !task->queue_time.is_null()) |
| 77 | ? task->queue_time |
| 78 | : base::TimeTicks::Now(); |
Colin Blundell | 85157ec | 2022-03-21 17:31:45 | [diff] [blame] | 79 | UMA_HISTOGRAM_TIMES("Navigation.Preconnect.ResolveHostCallbackQueueingTime", |
| 80 | base::TimeTicks::Now() - queue_time); |
| 81 | |
Alexandr Ilin | 5b417db | 2018-08-17 18:34:00 | [diff] [blame] | 82 | std::move(callback_).Run(result == net::OK); |
| 83 | } |
| 84 | |
| 85 | void ResolveHostClientImpl::OnConnectionError() { |
| 86 | std::move(callback_).Run(false); |
| 87 | } |
| 88 | |
Peter E Conn | 2352ee6 | 2025-07-30 17:12:38 | [diff] [blame] | 89 | } // namespace content |