File: search_engine_preconnector.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (378 lines) | stat: -rw-r--r-- 14,170 bytes parent folder | download | duplicates (3)
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// 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/search_engine_preconnector.h"

#include <limits>

#include "base/functional/bind.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/numerics/safe_conversions.h"
#include "base/time/default_tick_clock.h"
#include "base/time/tick_clock.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/predictors/loading_predictor_config.h"
#include "chrome/browser/predictors/predictors_traffic_annotations.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_context.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "net/base/features.h"
#include "net/base/reconnect_notifier.h"

namespace {

#if BUILDFLAG(IS_ANDROID)
const int kDefaultStartupDelayMs = 0;
const bool kDefaultSkipInBackground = false;
#else
const int kDefaultStartupDelayMs = 5000;
const bool kDefaultSkipInBackground = true;
#endif

constexpr int kPreconnectIntervalSec = 60;
constexpr int kPreconnectRetryDelayMs = 50;

}  // namespace

namespace features {
// Feature to control preconnect to search.

BASE_FEATURE(kPreconnectFromKeyedService,
             "PreconnectFromKeyedService",
             base::FEATURE_DISABLED_BY_DEFAULT);
BASE_FEATURE(kPreconnectToSearch,
             "PreconnectToSearch",
             base::FEATURE_ENABLED_BY_DEFAULT);
}  // namespace features

WebContentVisibilityManager::WebContentVisibilityManager()
    : tick_clock_(base::DefaultTickClock::GetInstance()) {}

WebContentVisibilityManager::~WebContentVisibilityManager() = default;

void WebContentVisibilityManager::OnWebContentsVisibilityChanged(
    content::WebContents* web_contents,
    bool is_in_foreground) {
  visible_web_contents_.erase(web_contents);
  last_web_contents_state_change_time_ = tick_clock_->NowTicks();
  if (is_in_foreground) {
    visible_web_contents_.insert(web_contents);
  }
}

void WebContentVisibilityManager::OnWebContentsDestroyed(
    content::WebContents* web_contents) {
  visible_web_contents_.erase(web_contents);
  last_web_contents_state_change_time_ = tick_clock_->NowTicks();
}

bool WebContentVisibilityManager::IsBrowserAppLikelyInForeground() const {
  // If no web contents is in foreground, then allow a very short cool down
  // period before considering app in background. This cooldown period is
  // needed since when switching between the tabs, none of the web contents is
  // in foreground for a very short period.
  if (visible_web_contents_.empty() &&
      tick_clock_->NowTicks() - last_web_contents_state_change_time_ >
          base::Seconds(1)) {
    return false;
  }

  return tick_clock_->NowTicks() - last_web_contents_state_change_time_ <=
         base::Seconds(120);
}

void WebContentVisibilityManager::SetTickClockForTesting(
    const base::TickClock* tick_clock) {
  tick_clock_ = tick_clock;
}

bool SearchEnginePreconnector::ShouldBeEnabledAsKeyedService() {
  static bool preconnect_from_keyed_service =
      base::FeatureList::IsEnabled(features::kPreconnectFromKeyedService);
  return preconnect_from_keyed_service;
}

bool SearchEnginePreconnector::ShouldBeEnabledForOffTheRecord() {
  static bool enabled_for_otr = base::GetFieldTrialParamByFeatureAsBool(
      features::kPreconnectFromKeyedService, "run_on_otr", false);
  return enabled_for_otr;
}

bool SearchEnginePreconnector::SearchEnginePreconnect2Enabled() {
  static bool preconnect2_enabled =
      base::FeatureList::IsEnabled(net::features::kSearchEnginePreconnect2);
  return preconnect2_enabled;
}

SearchEnginePreconnector::SearchEnginePreconnector(
    content::BrowserContext* browser_context)
    : browser_context_(browser_context) {
  DCHECK(ShouldBeEnabledForOffTheRecord() ||
         !browser_context_->IsOffTheRecord());
}

SearchEnginePreconnector::~SearchEnginePreconnector() = default;

void SearchEnginePreconnector::StopPreconnecting() {
  preconnector_started_ = false;
  timer_.Stop();
}

void SearchEnginePreconnector::StartPreconnecting(bool with_startup_delay) {
  preconnector_started_ = true;
  timer_.Stop();
  if (with_startup_delay) {
    StartPreconnectWithDelay(
        base::Milliseconds(base::GetFieldTrialParamByFeatureAsInt(
            features::kPreconnectToSearch, "startup_delay_ms",
            kDefaultStartupDelayMs)),
        PreconnectTriggerEvent::kInitialPreconnect);
    return;
  }

  PreconnectDSE();
}

void SearchEnginePreconnector::PreconnectDSE() {
  DCHECK(ShouldBeEnabledForOffTheRecord() ||
         !browser_context_->IsOffTheRecord());
  DCHECK(!timer_.IsRunning());
  if (!base::FeatureList::IsEnabled(features::kPreconnectToSearch))
    return;

  // Don't preconnect unless the user allows search suggestions.
  if (!Profile::FromBrowserContext(browser_context_)
           ->GetPrefs()
           ->GetBoolean(prefs::kSearchSuggestEnabled))
    return;

  GURL preconnect_url = GetDefaultSearchEngineOriginURL();
  if (preconnect_url.scheme() != url::kHttpScheme &&
      preconnect_url.scheme() != url::kHttpsScheme) {
    return;
  }

  if (!preconnect_url.is_valid() || !preconnect_url.has_host()) {
    return;
  }

  if (!predictors::IsPreconnectAllowed(
          Profile::FromBrowserContext(browser_context_))) {
    return;
  }

  const bool is_browser_app_likely_in_foreground =
      IsBrowserAppLikelyInForeground();
  base::UmaHistogramBoolean(
      "NavigationPredictor.SearchEnginePreconnector."
      "IsBrowserAppLikelyInForeground",
      is_browser_app_likely_in_foreground);

  std::optional<net::ConnectionKeepAliveConfig> keepalive_config;
  mojo::PendingRemote<network::mojom::ConnectionChangeObserverClient> observer;
  if (SearchEnginePreconnect2Enabled()) {
    keepalive_config = net::ConnectionKeepAliveConfig();
    keepalive_config->idle_timeout_in_seconds =
        net::features::kIdleTimeoutInSeconds.Get();
    keepalive_config->ping_interval_in_seconds =
        net::features::kPingIntervalInSeconds.Get();
    keepalive_config->enable_connection_keep_alive = true;
    keepalive_config->quic_connection_options =
        net::features::kQuicConnectionOptions.Get();

    if (!receiver_.is_bound()) {
      observer = receiver_.BindNewPipeAndPassRemote();
      receiver_.set_disconnect_handler(base::BindOnce(
          &SearchEnginePreconnector::OnReconnectObserverPipeDisconnected,
          base::Unretained(this)));
    }
  }

  if (!base::GetFieldTrialParamByFeatureAsBool(features::kPreconnectToSearch,
                                               "skip_in_background",
                                               kDefaultSkipInBackground) ||
      is_browser_app_likely_in_foreground) {
    net::SchemefulSite schemeful_site(preconnect_url);
    auto network_anonymziation_key =
        net::NetworkAnonymizationKey::CreateSameSite(schemeful_site);
    GetPreconnectManager().StartPreconnectUrl(
        preconnect_url, /*allow_credentials=*/true, network_anonymziation_key,
        predictors::kSearchEnginePreconnectTrafficAnnotation,
        /*storage_partition_config=*/nullptr, std::move(keepalive_config),
        std::move(observer));
  }

  // Periodically preconnect to the DSE. If the browser app is likely in
  // background, we will reattempt preconnect later.
  if (!SearchEnginePreconnect2Enabled()) {
    StartPreconnectWithDelay(GetPreconnectInterval(),
                             PreconnectTriggerEvent::kPeriodicPreconnect);
  }

  last_preconnect_attempt_time_ = base::TimeTicks::Now();
}

GURL SearchEnginePreconnector::GetDefaultSearchEngineOriginURL() const {
  auto* template_service = TemplateURLServiceFactory::GetForProfile(
      Profile::FromBrowserContext(browser_context_));
  if (!template_service)
    return GURL();
  const auto* search_provider = template_service->GetDefaultSearchProvider();
  if (!search_provider || !search_provider->data().preconnect_to_search_url)
    return GURL();
  return search_provider->GenerateSearchURL({}).DeprecatedGetOriginAsURL();
}

base::TimeDelta SearchEnginePreconnector::GetPreconnectInterval() const {
  if (!SearchEnginePreconnect2Enabled()) {
    int preconnect_interval = base::GetFieldTrialParamByFeatureAsInt(
        net::features::kSearchEnginePreconnectInterval, "preconnect_interval",
        kPreconnectIntervalSec);

    // Add an extra delay to make sure the preconnect has expired if it wasn't
    // used.
    return base::Seconds(preconnect_interval) +
           base::Milliseconds(kPreconnectRetryDelayMs);
  }

  // If this is the first time failing, we should instantly retry, but we wait
  // a very small amount of time since a closed connection would likely mean
  // that there were something wrong in the connection.
  // Otherwise, we backoff `kPreconnectRetryDelayMs` (currently 50 ms) * 2^n for
  // the next preconnect attempt.
  return std::min(
      base::Milliseconds(kPreconnectRetryDelayMs) *
          CalculateBackoffMultiplier(),
      base::Seconds(net::features::kMaxPreconnectRetryInterval.Get()));
}

int32_t SearchEnginePreconnector::CalculateBackoffMultiplier() const {
  return 1 << std::min(static_cast<int>(consecutive_connection_failure_),
                       std::numeric_limits<int32_t>::digits - 1);
}

bool SearchEnginePreconnector::IsShortSession() const {
  CHECK(last_preconnect_attempt_time_.has_value());
  if (is_short_session_for_testing_.has_value()) {
    return is_short_session_for_testing_.value();
  }

  base::TimeDelta session_time =
      base::TimeTicks::Now() - last_preconnect_attempt_time_.value();

  // If the current session duration is shorter than the idle timeout, we
  // consider the session to be short.
  return session_time < net::features::kShortSessionThreshold.Get();
}

void SearchEnginePreconnector::StartPreconnectWithDelay(
    base::TimeDelta delay,
    PreconnectTriggerEvent event) {
  RecordPreconnectAttemptHistogram(delay, event);
  //  Set/Reset the timer to fire after the specified `delay`.
  timer_.Start(FROM_HERE, delay,
               base::BindOnce(&SearchEnginePreconnector::PreconnectDSE,
                              base::Unretained(this)));
}

predictors::PreconnectManager&
SearchEnginePreconnector::GetPreconnectManager() {
  if (!preconnect_manager_) {
    preconnect_manager_ = std::make_unique<predictors::PreconnectManager>(
        GetWeakPtr(), Profile::FromBrowserContext(browser_context_));
  }

  return *preconnect_manager_.get();
}

void SearchEnginePreconnector::OnWebContentsVisibilityChanged(
    content::WebContents* web_contents,
    bool is_in_foreground) {
  WebContentVisibilityManager::OnWebContentsVisibilityChanged(web_contents,
                                                              is_in_foreground);

  if (!SearchEnginePreconnect2Enabled()) {
    return;
  }

  // Early stop when we know that the visibility change did not trigger
  // foregrounding of the app and also when the preconnector is not started.
  if (!IsBrowserAppLikelyInForeground() || !preconnector_started_) {
    return;
  }

  // Stop the timer explicitly here so that we do not have any duplicate
  // attempts.
  timer_.Stop();

  // Attempt reconnect again in case the visibility has changed after the last
  // preconnect attempt so that we will preconnect sooner.
  PreconnectDSE();
}

void SearchEnginePreconnector::OnSessionClosed() {
  if (IsShortSession()) {
    // If we have a short session, we consider that the session was closed due
    // to an error, and will consider as a failed connection as well.
    consecutive_connection_failure_++;
  } else {
    // If the last session was not short, then it must mean that the connection
    // was successful. Reset the failure count.
    base::UmaHistogramCounts1000(
        "NavigationPredictor.SearchEnginePreconnector.ConsecutiveFailures",
        consecutive_connection_failure_);
    consecutive_connection_failure_ = 0;
  }
  StartPreconnectWithDelay(GetPreconnectInterval(),
                           PreconnectTriggerEvent::kSessionClosed);
}

void SearchEnginePreconnector::OnNetworkEvent(net::NetworkChangeEvent event) {
  // If the network event is `Connected`, we attempt preconnect. Otherwise,
  // we will ignore the events for now.
  if (event == net::NetworkChangeEvent::kConnected) {
    StartPreconnectWithDelay(base::Milliseconds(kPreconnectRetryDelayMs),
                             PreconnectTriggerEvent::kNetworkEvent);
  }
}

void SearchEnginePreconnector::OnConnectionFailed() {
  consecutive_connection_failure_++;
  StartPreconnectWithDelay(GetPreconnectInterval(),
                           PreconnectTriggerEvent::kConnectionFailed);
}

void SearchEnginePreconnector::OnReconnectObserverPipeDisconnected() {
  receiver_.reset();
  // Only call `OnConnectionFailed` when the `timer_` is not running since we
  // might already be waiting for reconnect attempt from other reasons.
  if (!timer_.IsRunning()) {
    OnConnectionFailed();
  }
}

void SearchEnginePreconnector::RecordPreconnectAttemptHistogram(
    base::TimeDelta delay,
    PreconnectTriggerEvent event) {
  base::UmaHistogramEnumeration(
      "NavigationPredictor.SearchEnginePreconnector.TriggerEvent", event);
  base::UmaHistogramLongTimes(
      "NavigationPredictor.SearchEnginePreconnector.PreconnectDelay", delay);
  if (last_preconnect_attempt_time_.has_value()) {
    base::UmaHistogramLongTimes(
        "NavigationPredictor.SearchEnginePreconnector."
        "PreconnectAttemptInterval",
        base::TimeTicks::Now() - last_preconnect_attempt_time_.value());
  }
}