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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
content / browser / preloading / preloading_data_impl.cc [blame]
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/preloading/preloading_data_impl.h"
#include <limits>
#include <string_view>
#include <utility>
#include <vector>
#include "base/hash/hash.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/rand_util.h"
#include "base/strings/strcat.h"
#include "content/browser/preloading/prefetch/prefetch_container.h"
#include "content/browser/preloading/prefetch/prefetch_service.h"
#include "content/browser/preloading/preloading.h"
#include "content/browser/preloading/preloading_attempt_impl.h"
#include "content/browser/preloading/preloading_config.h"
#include "content/browser/preloading/preloading_prediction.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"
#if DCHECK_IS_ON()
#include "base/no_destructor.h"
#endif // DCHECK_IS_ON()
namespace content {
namespace {
// Called by `AddPreloadingAttempt` and `AddPreloadingPrediction`. Fails
// the callers if the predictor is redefined. This method can be racy due to the
// static variable.
static void CheckPreloadingPredictorValidity(PreloadingPredictor predictor) {
#if DCHECK_IS_ON()
// Use `std::string` because we can't guarantee std::string_view has a static
// lifetime.
static base::NoDestructor<std::vector<std::pair<int64_t, std::string>>>
seen_predictors;
std::pair<int64_t, std::string> new_predictor(predictor.ukm_value(),
predictor.name());
bool found = false;
for (const auto& seen : *seen_predictors) {
if ((seen.first == new_predictor.first) ^
(seen.second == new_predictor.second)) {
// We cannot have two `PreloadingPredictor`s that only differ in either
// the ukm int value or the string description - each new
// `PreloadingPredictor` needs to be unique in both.
DCHECK(false) << new_predictor.second << "/" << new_predictor.first
<< " vs " << seen.second << "/" << seen.first;
} else if (seen == new_predictor) {
found = true;
break;
}
}
if (!found) {
seen_predictors->push_back(new_predictor);
}
#endif // DCHECK_IS_ON()
}
size_t GetMaxPredictions(bool max_predictions_is_ten_for_testing) {
// The limit is somewhat arbitrary. It should be large enough that most pages
// won't reach it, but small enough to keep memory usage reasonable for those
// that do.
constexpr size_t kMaxPredictions = 10000;
return max_predictions_is_ten_for_testing ? 10 : kMaxPredictions;
}
std::optional<double> GetSamplingLikelihood(
bool max_predictions_is_ten_for_testing,
size_t total_seen_predictions) {
const size_t max_predictions =
GetMaxPredictions(max_predictions_is_ten_for_testing);
return (total_seen_predictions <= max_predictions)
? std::nullopt
: std::optional<double>{static_cast<double>(max_predictions) /
total_seen_predictions};
}
// We may produce a large number of predictions over the lifetime of a long
// lived page. After the number of predictions grows sufficiently large, we'll
// start randomly sampling and replacing existing predictions in order to limit
// memory usage.
// See https://en.wikipedia.org/wiki/Reservoir_sampling#Simple:_Algorithm_R
// We don't report anything from the predictions that aren't sampled in here
// when the page navigates/unloads. When we record UKMs, we include the
// sampling factor which indicates how much downsampling happened here.
template <typename PredictionType>
void PredictionReservoirSample(std::vector<PredictionType>& predictions,
size_t& items_seen,
bool max_predictions_is_ten_for_testing,
PredictionType new_prediction) {
CHECK_LE(predictions.size(), items_seen);
const size_t max_predictions =
GetMaxPredictions(max_predictions_is_ten_for_testing);
if (items_seen < max_predictions) {
predictions.push_back(std::move(new_prediction));
++items_seen;
return;
}
size_t replace_idx = static_cast<size_t>(base::RandGenerator(items_seen + 1));
if (replace_idx < predictions.size()) {
predictions[replace_idx] = std::move(new_prediction);
}
++items_seen;
}
} // namespace
// static
PreloadingURLMatchCallback PreloadingData::GetSameURLMatcher(
const GURL& destination_url) {
// To save memory, only bind a hash of the URL for comparison.
return base::BindRepeating(
[](size_t predicted_url_hash, const GURL& navigated_url) {
return predicted_url_hash == base::FastHash(navigated_url.spec());
},
base::FastHash(destination_url.spec()));
}
// static
PreloadingURLMatchCallback PreloadingDataImpl::GetPrefetchServiceMatcher(
PrefetchService& prefetch_service,
const PrefetchContainer::Key& predicted) {
return base::BindRepeating(
[](base::WeakPtr<PrefetchService> prefetch_service,
const PrefetchContainer::Key& predicted, const GURL& navigated_url) {
if (!prefetch_service) {
return predicted.url() == navigated_url;
}
if (predicted.url() == navigated_url) {
return true;
}
base::WeakPtr<PrefetchContainer> prefetch_container =
prefetch_service->MatchUrl(predicted.WithNewUrl(navigated_url));
return prefetch_container && prefetch_container->key() == predicted;
},
prefetch_service.GetWeakPtr(), predicted);
}
// static
PreloadingData* PreloadingData::GetOrCreateForWebContents(
WebContents* web_contents) {
return PreloadingDataImpl::GetOrCreateForWebContents(web_contents);
}
// static
PreloadingData* PreloadingData::GetForWebContents(WebContents* web_contents) {
return PreloadingDataImpl::FromWebContents(web_contents);
}
// static
PreloadingDataImpl* PreloadingDataImpl::GetOrCreateForWebContents(
WebContents* web_contents) {
auto* preloading_impl = PreloadingDataImpl::FromWebContents(web_contents);
if (!preloading_impl)
PreloadingDataImpl::CreateForWebContents(web_contents);
return PreloadingDataImpl::FromWebContents(web_contents);
}
PreloadingAttempt* PreloadingDataImpl::AddPreloadingAttempt(
PreloadingPredictor predictor,
PreloadingType preloading_type,
PreloadingURLMatchCallback url_match_predicate,
std::optional<PreloadingType> planned_max_preloading_type,
ukm::SourceId triggering_primary_page_source_id) {
// The same `predictor` created and enacted the candidate associated with this
// attempt.
return AddPreloadingAttempt(predictor, predictor, preloading_type,
std::move(url_match_predicate),
std::move(planned_max_preloading_type),
triggering_primary_page_source_id);
}
PreloadingAttemptImpl* PreloadingDataImpl::AddPreloadingAttempt(
const PreloadingPredictor& creating_predictor,
const PreloadingPredictor& enacting_predictor,
PreloadingType preloading_type,
PreloadingURLMatchCallback url_match_predicate,
std::optional<PreloadingType> planned_max_preloading_type,
ukm::SourceId triggering_primary_page_source_id) {
auto attempt = std::make_unique<PreloadingAttemptImpl>(
creating_predictor, enacting_predictor, preloading_type,
triggering_primary_page_source_id, std::move(url_match_predicate),
std::move(planned_max_preloading_type), sampling_seed_);
preloading_attempts_.push_back(std::move(attempt));
return preloading_attempts_.back().get();
}
void PreloadingDataImpl::AddPreloadingPrediction(
PreloadingPredictor predictor,
int confidence,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id) {
AddPreloadingPrediction(predictor, PreloadingConfidence{confidence},
std::move(url_match_predicate),
triggering_primary_page_source_id);
}
void PreloadingDataImpl::AddPreloadingPrediction(
const PreloadingPredictor& predictor,
PreloadingConfidence confidence,
PreloadingURLMatchCallback url_match_predicate,
ukm::SourceId triggering_primary_page_source_id) {
// We want to log the metrics for user visible primary pages to measure the
// impact of PreloadingPredictions on the page user is viewing.
// TODO(crbug.com/40227283): Extend this for non-primary page and inner
// WebContents preloading predictions.
PredictionReservoirSample(
preloading_predictions_, total_seen_preloading_predictions_,
max_predictions_is_ten_for_testing_,
PreloadingPrediction{predictor, confidence,
triggering_primary_page_source_id,
std::move(url_match_predicate)});
}
void PreloadingDataImpl::AddExperimentalPreloadingPrediction(
std::string_view name,
PreloadingURLMatchCallback url_match_predicate,
float score,
float min_score,
float max_score,
size_t buckets) {
PredictionReservoirSample(
experimental_predictions_, total_seen_experimental_predictions_,
max_predictions_is_ten_for_testing_,
ExperimentalPreloadingPrediction{name, std::move(url_match_predicate),
score, min_score, max_score, buckets});
}
void PreloadingDataImpl::SetIsNavigationInDomainCallback(
PreloadingPredictor predictor,
PredictorDomainCallback is_navigation_in_domain_callback) {
is_navigation_in_predictor_domain_callbacks_.insert(
{predictor, std::move(is_navigation_in_domain_callback)});
}
void PreloadingDataImpl::CopyPredictorDomains(
const PreloadingDataImpl& other,
const std::vector<PreloadingPredictor>& predictors) {
for (const auto& predictor : predictors) {
if (const auto it =
other.is_navigation_in_predictor_domain_callbacks_.find(predictor);
it != other.is_navigation_in_predictor_domain_callbacks_.end()) {
SetIsNavigationInDomainCallback(predictor, it->second);
}
}
}
PreloadingDataImpl::PreloadingDataImpl(WebContents* web_contents)
: WebContentsUserData<PreloadingDataImpl>(*web_contents),
WebContentsObserver(web_contents),
sampling_seed_(static_cast<uint32_t>(base::RandUint64())) {}
PreloadingDataImpl::~PreloadingDataImpl() = default;
void PreloadingDataImpl::DidFinishNavigation(
NavigationHandle* navigation_handle) {
// Record UKMs for primary page navigations only. The reason we don't use
// WebContentsObserver::PrimaryPageChanged is because we want to get the
// navigation UkmSourceId which is different from
// RenderFrameHost::GetPageUkmSourceId for prerender activation.
// TODO(crbug.com/40215894): Switch to PrimaryPageChanged once we align
// RenderFrameHost::GetPageUkmSourceId with
// PageLoadTracker::GetPageUKMSourceId.
if (!navigation_handle->IsInPrimaryMainFrame() ||
navigation_handle->IsSameDocument() ||
!navigation_handle->HasCommitted()) {
return;
}
ukm::SourceId navigated_page_source_id =
navigation_handle->GetNextPageUkmSourceId();
// Log the metrics also on navigation when the user ends up navigating. Please
// note that we currently log the metrics on the primary page to analyze
// preloading impact on user-visible primary pages.
RecordMetricsForPreloadingAttempts(navigated_page_source_id);
RecordUKMForPreloadingPredictions(navigated_page_source_id);
// Delete the user data after logging.
web_contents()->RemoveUserData(UserDataKey());
}
void PreloadingDataImpl::DidStartNavigation(
NavigationHandle* navigation_handle) {
// Only observe for the navigation in the primary frame tree to log the
// metrics after which this class will be deleted.
if (!navigation_handle->IsInPrimaryMainFrame()) {
return;
}
// Ignore same-document navigations as preloading is not served for these
// cases.
if (navigation_handle->IsSameDocument()) {
return;
}
// Match the preloading based on the URL the frame is navigating to rather
// than the committed URL as they could be different because of redirects. We
// set accurate triggering and prediction bits in DidStartNavigation before
// PrimaryPageChanged is invoked where the metrics are logged to capture if
// the prediction/triggering was accurate. This doesn't imply that the user
// navigated to the predicted URL.
ResetRecallStats();
SetIsAccurateTriggeringAndPrediction(navigation_handle->GetURL());
RecordRecallStatsToUMA(navigation_handle);
}
void PreloadingDataImpl::WebContentsDestroyed() {
// Log the metrics also on WebContentsDestroyed event to avoid losing the data
// in case the user doesn't end up navigating. When the WebContents is
// destroyed before navigation, we pass ukm::kInvalidSourceId and empty URL to
// avoid the UKM associated to wrong page.
RecordMetricsForPreloadingAttempts(ukm::kInvalidSourceId);
RecordUKMForPreloadingPredictions(ukm::kInvalidSourceId);
for (const auto& experimental_prediction : experimental_predictions_) {
experimental_prediction.RecordToUMA();
}
experimental_predictions_.clear();
total_seen_experimental_predictions_ = 0;
const std::optional<double> sampling_likelihood = GetSamplingLikelihood(
max_predictions_is_ten_for_testing_, total_seen_ml_predictions_);
for (auto& ml_prediction : ml_predictions_) {
ml_prediction.Record(sampling_likelihood);
}
ml_predictions_.clear();
total_seen_ml_predictions_ = 0;
// Delete the user data after logging.
web_contents()->RemoveUserData(UserDataKey());
}
void PreloadingDataImpl::RecordPreloadingAttemptPrecisionToUMA(
const PreloadingAttemptImpl& attempt) {
bool is_true_positive = attempt.IsAccurateTriggering();
for (const auto& predictor : attempt.GetPredictors()) {
const auto uma_attempt_precision = base::StrCat(
{"Preloading.", PreloadingTypeToString(attempt.preloading_type()),
".Attempt.", predictor.name(), ".Precision"});
base::UmaHistogramEnumeration(
uma_attempt_precision, is_true_positive
? PredictorConfusionMatrix::kTruePositive
: PredictorConfusionMatrix::kFalsePositive);
}
}
// static
bool PreloadingDataImpl::IsLinkClickNavigation(
NavigationHandle* navigation_handle) {
auto page_transition = navigation_handle->GetPageTransition();
return ui::PageTransitionCoreTypeIs(
page_transition, ui::PageTransition::PAGE_TRANSITION_LINK) &&
(page_transition & ui::PAGE_TRANSITION_CLIENT_REDIRECT) == 0 &&
ui::PageTransitionIsNewNavigation(page_transition);
}
void PreloadingDataImpl::RecordPredictionPrecisionToUMA(
const PreloadingPrediction& prediction) {
bool is_true_positive = prediction.IsAccuratePrediction();
const auto uma_predictor_precision =
base::StrCat({"Preloading.Predictor.", prediction.predictor_type().name(),
".Precision"});
base::UmaHistogramEnumeration(uma_predictor_precision,
is_true_positive
? PredictorConfusionMatrix::kTruePositive
: PredictorConfusionMatrix::kFalsePositive);
}
void PreloadingDataImpl::UpdatePreloadingAttemptRecallStats(
const PreloadingAttemptImpl& attempt) {
bool is_true_positive = attempt.IsAccurateTriggering();
if (is_true_positive) {
for (const auto& predictor : attempt.GetPredictors()) {
preloading_attempt_recall_stats_.insert(
{predictor, attempt.preloading_type()});
}
}
}
void PreloadingDataImpl::UpdatePredictionRecallStats(
const PreloadingPrediction& prediction) {
bool is_true_positive = prediction.IsAccuratePrediction();
if (is_true_positive) {
predictions_recall_stats_.insert(prediction.predictor_type());
}
}
void PreloadingDataImpl::ResetRecallStats() {
predictions_recall_stats_.clear();
preloading_attempt_recall_stats_.clear();
}
void PreloadingDataImpl::RecordRecallStatsToUMA(
NavigationHandle* navigation_handle) {
constexpr PreloadingType kPreloadingTypes[] = {PreloadingType::kPreconnect,
PreloadingType::kPrefetch,
PreloadingType::kPrerender};
for (const auto& [predictor_type, is_navigation_in_domain_callback] :
is_navigation_in_predictor_domain_callbacks_) {
if (!is_navigation_in_domain_callback.Run(navigation_handle)) {
continue;
}
const auto uma_predictor_recall = base::StrCat(
{"Preloading.Predictor.", predictor_type.name(), ".Recall"});
base::UmaHistogramEnumeration(
uma_predictor_recall, predictions_recall_stats_.contains(predictor_type)
? PredictorConfusionMatrix::kTruePositive
: PredictorConfusionMatrix::kFalseNegative);
for (const auto& preloading_type : kPreloadingTypes) {
const auto uma_attemp_recall =
base::StrCat({"Preloading.", PreloadingTypeToString(preloading_type),
".Attempt.", predictor_type.name(), ".Recall"});
base::UmaHistogramEnumeration(
uma_attemp_recall, preloading_attempt_recall_stats_.contains(
{predictor_type, preloading_type})
? PredictorConfusionMatrix::kTruePositive
: PredictorConfusionMatrix::kFalseNegative);
}
}
// Clear registered predictor domain callbacks and get ready for the next
// navigation.
is_navigation_in_predictor_domain_callbacks_.clear();
}
void PreloadingDataImpl::SetIsAccurateTriggeringAndPrediction(
const GURL& navigated_url) {
for (auto& experimental_prediction : experimental_predictions_) {
experimental_prediction.SetIsAccuratePrediction(navigated_url);
experimental_prediction.RecordToUMA();
}
experimental_predictions_.clear();
total_seen_experimental_predictions_ = 0;
const std::optional<double> sampling_likelihood = GetSamplingLikelihood(
max_predictions_is_ten_for_testing_, total_seen_ml_predictions_);
for (auto& ml_prediction : ml_predictions_) {
ml_prediction.SetIsAccuratePrediction(navigated_url);
ml_prediction.Record(sampling_likelihood);
}
ml_predictions_.clear();
total_seen_ml_predictions_ = 0;
for (auto& attempt : preloading_attempts_) {
attempt->SetIsAccurateTriggering(navigated_url);
RecordPreloadingAttemptPrecisionToUMA(*attempt);
UpdatePreloadingAttemptRecallStats(*attempt);
}
for (auto& prediction : preloading_predictions_) {
prediction.SetIsAccuratePrediction(navigated_url);
RecordPredictionPrecisionToUMA(prediction);
UpdatePredictionRecallStats(prediction);
}
}
void PreloadingDataImpl::SetHasSpeculationRulesPrerender() {
has_speculation_rules_prerender_ = true;
}
bool PreloadingDataImpl::HasSpeculationRulesPrerender() {
return has_speculation_rules_prerender_;
}
void PreloadingDataImpl::OnPreloadingHeuristicsModelInput(
const GURL& url,
ModelPredictionTrainingData::OutcomeCallback on_record_outcome) {
PredictionReservoirSample(
ml_predictions_, total_seen_ml_predictions_,
max_predictions_is_ten_for_testing_,
ModelPredictionTrainingData{std::move(on_record_outcome),
GetSameURLMatcher(url)});
}
void PreloadingDataImpl::RecordMetricsForPreloadingAttempts(
ukm::SourceId navigated_page_source_id) {
for (auto& attempt : preloading_attempts_) {
// Check the validity at the time of UKMs reporting, as the UKMs are
// reported from the same thread (whichever thread calls
// `PreloadingDataImpl::WebContentsDestroyed` or
// `PreloadingDataImpl::DidFinishNavigation`).
for (const auto& predictor : attempt->GetPredictors()) {
CheckPreloadingPredictorValidity(predictor);
}
attempt->RecordPreloadingAttemptMetrics(navigated_page_source_id);
}
// Clear all records once we record the UKMs.
preloading_attempts_.clear();
}
void PreloadingDataImpl::RecordUKMForPreloadingPredictions(
ukm::SourceId navigated_page_source_id) {
const std::optional<double> sampling_likelihood = GetSamplingLikelihood(
max_predictions_is_ten_for_testing_, total_seen_preloading_predictions_);
for (auto& prediction : preloading_predictions_) {
// Check the validity at the time of UKMs reporting, as the UKMs are
// reported from the same thread (whichever thread calls
// `PreloadingDataImpl::WebContentsDestroyed` or
// `PreloadingDataImpl::DidFinishNavigation`).
CheckPreloadingPredictorValidity(prediction.predictor_type());
prediction.RecordPreloadingPredictionUKMs(navigated_page_source_id,
sampling_likelihood);
}
// Clear all records once we record the UKMs.
preloading_predictions_.clear();
total_seen_preloading_predictions_ = 0;
}
size_t PreloadingDataImpl::GetPredictionsSizeForTesting() const {
return preloading_predictions_.size();
}
void PreloadingDataImpl::SetMaxPredictionsToTenForTesting() {
max_predictions_is_ten_for_testing_ = true;
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(PreloadingDataImpl);
} // namespace content