File: navigation_predictor_preconnect_client.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (263 lines) | stat: -rw-r--r-- 9,711 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/navigation_predictor/navigation_predictor_preconnect_client.h"

#include <memory>

#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_macros.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_features.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service.h"
#include "chrome/browser/navigation_predictor/navigation_predictor_keyed_service_factory.h"
#include "chrome/browser/navigation_predictor/search_engine_preconnector.h"
#include "chrome/browser/navigation_predictor/search_engine_preconnector_keyed_service_factory.h"
#include "chrome/browser/predictors/loading_predictor.h"
#include "chrome/browser/predictors/loading_predictor_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "net/base/features.h"
#include "net/base/ip_address.h"

NavigationPredictorPreconnectClient::NavigationPredictorPreconnectClient(
    content::WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      content::WebContentsUserData<NavigationPredictorPreconnectClient>(
          *web_contents),
      browser_context_(web_contents->GetBrowserContext()),
      current_visibility_(web_contents->GetVisibility()) {}

NavigationPredictorPreconnectClient::~NavigationPredictorPreconnectClient() {
  auto* search_engine_preconnector = GetSearchEnginePreconnector();
  if (search_engine_preconnector) {
    search_engine_preconnector->OnWebContentsDestroyed(web_contents());
  }
}

NavigationPredictorKeyedService*
NavigationPredictorPreconnectClient::GetNavigationPredictorKeyedService()
    const {
  return NavigationPredictorKeyedServiceFactory::GetForProfile(
      Profile::FromBrowserContext(browser_context_));
}

SearchEnginePreconnector*
NavigationPredictorPreconnectClient::GetSearchEnginePreconnector() {
  if (SearchEnginePreconnector::ShouldBeEnabledAsKeyedService()) {
    // If we have `SearchEnginePreconnectorKeyedService` enabled, the
    // `SearchEnginePreconnector` should be fetched directly from the
    // `KeyedService`.
    return SearchEnginePreconnectorKeyedServiceFactory::GetForProfile(
        Profile::FromBrowserContext(browser_context_));
  }

  auto* navigation_predictor_service = GetNavigationPredictorKeyedService();
  if (navigation_predictor_service) {
    return navigation_predictor_service->search_engine_preconnector();
  }
  return nullptr;
}

void NavigationPredictorPreconnectClient::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  // Notify search engine preconnector of any same-document navigations that
  // may be captured here. Same-document navigations imply that user is
  // interacting with the browser app.
  auto* search_engine_preconnector = GetSearchEnginePreconnector();
  if (search_engine_preconnector) {
    search_engine_preconnector->OnWebContentsVisibilityChanged(
        web_contents(), current_visibility_ == content::Visibility::VISIBLE);
  }

  if (!navigation_handle->IsInPrimaryMainFrame() ||
      !navigation_handle->HasCommitted()) {
    return;
  }

  if (!navigation_handle->IsSameDocument()) {
    is_publicly_routable_ = false;

    std::optional<bool> is_publicly_routable =
        IsPubliclyRoutable(navigation_handle);

    if (is_publicly_routable) {
      is_publicly_routable_ = is_publicly_routable.value();
    }
  }

  if (navigation_handle->IsSameDocument()) {
    return;
  }

  if (!navigation_handle->GetURL().SchemeIsHTTPOrHTTPS())
    return;

  // New page, so stop the preconnect timer.
  timer_.Stop();

  if (navigation_handle->IsSameDocument()) {
    constexpr int delay_ms = 3000;
    timer_.Start(
        FROM_HERE, base::Milliseconds(delay_ms),
        base::BindOnce(&NavigationPredictorPreconnectClient::MaybePreconnectNow,
                       base::Unretained(this), /*preconnects_attempted=*/0u));
  }
}

void NavigationPredictorPreconnectClient::OnVisibilityChanged(
    content::Visibility visibility) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  auto* search_engine_preconnector = GetSearchEnginePreconnector();
  if (search_engine_preconnector) {
    search_engine_preconnector->OnWebContentsVisibilityChanged(
        web_contents(), current_visibility_ == content::Visibility::VISIBLE);
  }

  // Check for same state.
  if (current_visibility_ == visibility)
    return;

  // Check if the visibility is now visible, if not, cancel future preconnects.
  // If visible, we can begin preconnecting.
  if (visibility != content::Visibility::VISIBLE) {
    current_visibility_ = visibility;

    // Stop any future preconnects while hidden.
    timer_.Stop();
    return;
  }

  current_visibility_ = visibility;

  // Previously, the visibility was HIDDEN, and now it is VISIBLE implying that
  // the web contents that was fully hidden is now fully visible.
  MaybePreconnectNow(/*preconnects_attempted=*/0u);
}

void NavigationPredictorPreconnectClient::DidFinishLoad(
    content::RenderFrameHost* render_frame_host,
    const GURL& validated_url) {
  // Ignore sub-frames and non-primary mainframes load.
  if (!render_frame_host->IsInPrimaryMainFrame())
    return;

  MaybePreconnectNow(/*preconnects_attempted=*/0u);
}

void NavigationPredictorPreconnectClient::MaybePreconnectNow(
    size_t preconnects_attempted) {
  if (base::FeatureList::IsEnabled(
          features::kNavigationPredictorPreconnectHoldback))
    return;

  if (browser_context_->IsOffTheRecord())
    return;

  // Only preconnect foreground tab.
  if (current_visibility_ != content::Visibility::VISIBLE)
    return;

  // Only allow 5 preconnects per foreground/load.
  if (preconnects_attempted >= 5u)
    return;

  // On search engine results page, next navigation is likely to be a
  // different origin. Currently, the preconnect is only allowed for same
  // origins. Hence, preconnect is currently disabled on search engine results
  // page. If preconnect to DSE is enabled, skip this check.
  if (!base::FeatureList::IsEnabled(features::kPreconnectToSearch) &&
      IsSearchEnginePage())
    return;

  url::Origin preconnect_origin =
      web_contents()->GetPrimaryMainFrame()->GetLastCommittedOrigin();
  if (preconnect_origin.scheme() != url::kHttpScheme &&
      preconnect_origin.scheme() != url::kHttpsScheme) {
    return;
  }

  UMA_HISTOGRAM_BOOLEAN("NavigationPredictor.IsPubliclyRoutable",
                        is_publicly_routable_);

  // Disable preconnecting to servers that are not publicly routable. These
  // could likely be small IoT servers that may not support extra traffic.
  if (!is_publicly_routable_)
    return;

  auto* loading_predictor = predictors::LoadingPredictorFactory::GetForProfile(
      Profile::FromBrowserContext(browser_context_));
  GURL preconnect_url_serialized(preconnect_origin.Serialize());
  DCHECK(preconnect_url_serialized.is_valid());

  if (!loading_predictor)
    return;

  loading_predictor->PrepareForPageLoad(
      preconnect_origin, preconnect_url_serialized,
      predictors::HintOrigin::NAVIGATION_PREDICTOR, true);

  // The delay beyond the idle socket timeout that net uses when
  // re-preconnecting. If negative, no retries occur.
  const base::TimeDelta retry_delay = base::Milliseconds(50);

  // Set/Reset the timer to fire after the preconnect times out. Add an extra
  // delay to make sure the preconnect has expired if it wasn't used.
  timer_.Start(
      FROM_HERE, base::Seconds(GetPreconnectInterval()) + retry_delay,
      base::BindOnce(&NavigationPredictorPreconnectClient::MaybePreconnectNow,
                     base::Unretained(this), preconnects_attempted + 1));
}

int NavigationPredictorPreconnectClient::GetPreconnectInterval() const {
  constexpr int kPreconnectIntervalSec = 60;
  return preconnect_interval_for_testing_.value_or(kPreconnectIntervalSec);
}

bool NavigationPredictorPreconnectClient::IsSearchEnginePage() const {
  auto* template_service = TemplateURLServiceFactory::GetForProfile(
      Profile::FromBrowserContext(browser_context_));
  if (!template_service)
    return false;
  return template_service->IsSearchResultsPageFromDefaultSearchProvider(
      web_contents()->GetLastCommittedURL());
}

std::optional<bool> NavigationPredictorPreconnectClient::IsPubliclyRoutable(
    content::NavigationHandle* navigation_handle) const {
  net::IPEndPoint remote_endpoint = navigation_handle->GetSocketAddress();
  net::IPAddress page_ip_address_ = remote_endpoint.address();

  // Sometimes the IP address may not be set (e.g., if the socket is being
  // reused).
  if (!page_ip_address_.IsValid()) {
    return std::nullopt;
  }

  if (!enable_preconnects_for_local_ips_for_testing_) {
    if (page_ip_address_.IsLoopback() ||
        !page_ip_address_.IsPubliclyRoutable()) {
      return false;
    }
  }
  return true;
}

bool NavigationPredictorPreconnectClient::
    enable_preconnects_for_local_ips_for_testing_ = false;

std::optional<int>
    NavigationPredictorPreconnectClient::preconnect_interval_for_testing_ =
        std::nullopt;

WEB_CONTENTS_USER_DATA_KEY_IMPL(NavigationPredictorPreconnectClient);