blob: 0ed44a7db53d0d8d383dd0febbbf1d87a301765d [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2018 The Chromium Authors
Alexandr Ilin5b417db2018-08-17 18:34:002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Peter E Conn2352ee62025-07-30 17:12:385#include "content/browser/preloading/resolve_host_client_impl.h"
Alexandr Ilin5b417db2018-08-17 18:34:006
7#include <utility>
8
Minoru Chikamuneb7e2ea8e2025-02-18 04:21:009#include "base/check_is_test.h"
Avi Drissman9269d4ed2023-01-07 01:38:0610#include "base/functional/bind.h"
Colin Blundelle7e33112022-03-16 10:18:3811#include "base/metrics/histogram_macros.h"
Colin Blundell85157ec2022-03-21 17:31:4512#include "base/task/common/task_annotator.h"
Alex Clarkef9f39aee2019-03-20 13:17:2613#include "content/public/browser/browser_task_traits.h"
14#include "content/public/browser/browser_thread.h"
Alexandr Ilin5b417db2018-08-17 18:34:0015#include "net/base/host_port_pair.h"
16#include "net/base/net_errors.h"
Matt Menke23a960092019-12-05 18:35:4017#include "net/base/network_isolation_key.h"
Alexandr Ilinbf274332018-08-21 08:34:3318#include "net/base/request_priority.h"
Katharine Daly0b58af02019-11-22 14:59:4919#include "net/dns/public/resolve_error_info.h"
Alexandr Ilin5b417db2018-08-17 18:34:0020#include "services/network/public/mojom/network_context.mojom.h"
21#include "url/gurl.h"
Tsuyoshi Horo4746c142022-08-29 22:42:1522#include "url/scheme_host_port.h"
Alexandr Ilin5b417db2018-08-17 18:34:0023
Peter E Conn2352ee62025-07-30 17:12:3824namespace content {
Alexandr Ilin5b417db2018-08-17 18:34:0025
26ResolveHostClientImpl::ResolveHostClientImpl(
27 const GURL& url,
Brianna Goldstein581e2af82022-10-20 13:56:3028 const net::NetworkAnonymizationKey& network_anonymization_key,
Alexandr Ilin5b417db2018-08-17 18:34:0029 ResolveHostCallback callback,
30 network::mojom::NetworkContext* network_context)
Miyoung Shin5e36fee2019-10-15 01:59:5331 : callback_(std::move(callback)) {
Alex Clarkef9f39aee2019-03-20 13:17:2632 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
Miyoung Shin5e36fee2019-10-15 01:59:5333
Alexandr Ilinbf274332018-08-21 08:34:3334 network::mojom::ResolveHostParametersPtr parameters =
35 network::mojom::ResolveHostParameters::New();
36 parameters->initial_priority = net::RequestPriority::IDLE;
37 parameters->is_speculative = true;
Kenichi Ishibashi5adcf4c2021-08-06 10:46:5238 parameters->purpose =
39 network::mojom::ResolveHostParameters::Purpose::kPreconnect;
Colin Blundelle7e33112022-03-16 10:18:3840 resolve_host_start_time_ = base::TimeTicks::Now();
Tsuyoshi Horo4746c142022-08-29 22:42:1541 // 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 Goldstein581e2af82022-10-20 13:56:3047 network_anonymization_key, std::move(parameters),
Tsuyoshi Horo4746c142022-08-29 22:42:1548 receiver_.BindNewPipeAndPassRemote());
Miyoung Shin5e36fee2019-10-15 01:59:5349 receiver_.set_disconnect_handler(base::BindOnce(
Alexandr Ilin5b417db2018-08-17 18:34:0050 &ResolveHostClientImpl::OnConnectionError, base::Unretained(this)));
51}
52
53ResolveHostClientImpl::~ResolveHostClientImpl() = default;
54
Alexandr Ilin5b417db2018-08-17 18:34:0055void ResolveHostClientImpl::OnComplete(
56 int result,
Katharine Daly238cb7c2019-11-22 08:10:3157 const net::ResolveErrorInfo& resolve_error_info,
David Benjamin1627af92025-07-22 11:13:3858 const net::AddressList& resolved_addresses,
59 const net::HostResolverEndpointResults& alternative_endpoints) {
Colin Blundelle7e33112022-03-16 10:18:3860 UMA_HISTOGRAM_TIMES("Navigation.Preconnect.ResolveHostLatency",
61 base::TimeTicks::Now() - resolve_host_start_time_);
Colin Blundell85157ec2022-03-21 17:31:4562
63 auto* task = base::TaskAnnotator::CurrentTaskForThread();
Minoru Chikamuneb7e2ea8e2025-02-18 04:21:0064 if (!task) {
65 // The `task` can be null in base::ScopedMockTimeMessageLoopTaskRunner scope
66 // in test.
67 CHECK_IS_TEST();
68 }
Colin Blundell85157ec2022-03-21 17:31:4569 // 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 Chikamuneb7e2ea8e2025-02-18 04:21:0072 DCHECK(!task || task->delayed_run_time.is_null());
Colin Blundell85157ec2022-03-21 17:31:4573
74 // The task will have a null |queue_time| if run synchronously (this happens
75 // in unit tests, for example).
Minoru Chikamuneb7e2ea8e2025-02-18 04:21:0076 base::TimeTicks queue_time = (task && !task->queue_time.is_null())
77 ? task->queue_time
78 : base::TimeTicks::Now();
Colin Blundell85157ec2022-03-21 17:31:4579 UMA_HISTOGRAM_TIMES("Navigation.Preconnect.ResolveHostCallbackQueueingTime",
80 base::TimeTicks::Now() - queue_time);
81
Alexandr Ilin5b417db2018-08-17 18:34:0082 std::move(callback_).Run(result == net::OK);
83}
84
85void ResolveHostClientImpl::OnConnectionError() {
86 std::move(callback_).Run(false);
87}
88
Peter E Conn2352ee62025-07-30 17:12:3889} // namespace content