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
|
// Copyright 2025 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/loader/from_gws_navigation_and_keep_alive_request_tracker.h"
#include <tuple>
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/time/time.h"
#include "components/page_load_metrics/browser/features.h"
#include "content/public/browser/browser_context.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"
#include "services/network/public/cpp/resource_request.h"
namespace {
BASE_FEATURE_PARAM(base::TimeDelta,
kBeaconLeakageLoggingCleanupTimeout,
&page_load_metrics::features::kBeaconLeakageLogging,
"cleanup_timeout",
base::Seconds(180));
} // namespace
FromGWSNavigationAndKeepAliveRequestTracker::
FromGWSNavigationAndKeepAliveRequestTracker(
content::BrowserContext* browser_context)
: browser_context_(browser_context) {}
FromGWSNavigationAndKeepAliveRequestTracker::
~FromGWSNavigationAndKeepAliveRequestTracker() = default;
void FromGWSNavigationAndKeepAliveRequestTracker::TrackNavigation(
content::GlobalRenderFrameHostId initator_rfh_id,
int64_t category_id,
ukm::SourceId ukm_source_id,
int64_t navigation_id) {
FrameCategory frame_category{
initator_rfh_id,
category_id,
ukm_source_id,
};
if (per_frame_category_navigation_and_keepalive_requests_.find(
frame_category) ==
per_frame_category_navigation_and_keepalive_requests_.end()) {
per_frame_category_navigation_and_keepalive_requests_[frame_category] = {};
} else {
auto& requests =
per_frame_category_navigation_and_keepalive_requests_[frame_category];
for (size_t i = 0; i < requests.size(); ++i) {
if (requests[i].keepalive_token.has_value() &&
!requests[i].navigation_id.has_value()) {
// Found a keepalive request token without a navigation ID.
// Logs a UKM event and removes the request from the list.
requests[i].navigation_id = navigation_id;
LogUkmEvent(frame_category, requests[i]);
requests.erase(requests.begin() + i);
return;
}
}
// No keepalive request token without a navigation ID.
}
per_frame_category_navigation_and_keepalive_requests_[frame_category]
.push_back({.navigation_id = navigation_id});
StartCleanupTimer();
}
void FromGWSNavigationAndKeepAliveRequestTracker::TrackKeepAliveRequest(
content::GlobalRenderFrameHostId initator_rfh_id,
int64_t category_id,
ukm::SourceId ukm_source_id,
base::UnguessableToken keepalive_token) {
FrameCategory frame_category{
initator_rfh_id,
category_id,
ukm_source_id,
};
if (per_frame_category_navigation_and_keepalive_requests_.find(
frame_category) ==
per_frame_category_navigation_and_keepalive_requests_.end()) {
per_frame_category_navigation_and_keepalive_requests_[frame_category] = {};
} else {
auto& requests =
per_frame_category_navigation_and_keepalive_requests_[frame_category];
for (size_t i = 0; i < requests.size(); ++i) {
if (!requests[i].keepalive_token.has_value() &&
requests[i].navigation_id.has_value()) {
// Found a navigation without a keepalive request token.
// Logs a UKM event and removes the request from the list.
requests[i].keepalive_token = keepalive_token;
LogUkmEvent(frame_category, requests[i]);
requests.erase(requests.begin() + i);
return;
}
}
}
per_frame_category_navigation_and_keepalive_requests_[frame_category]
.push_back({.keepalive_token = keepalive_token});
StartCleanupTimer();
}
void FromGWSNavigationAndKeepAliveRequestTracker::LogUkmEvent(
const FrameCategory& frame_category,
const NavigationAndKeepAliveRequest& request) {
CHECK(request.navigation_id.has_value());
CHECK(request.keepalive_token.has_value());
ukm::builders::FetchKeepAliveRequest_WithCategory_Navigation ukm_builder(
frame_category.ukm_source_id);
ukm_builder.SetCategory(frame_category.category_id);
ukm_builder.SetNavigationId(*request.navigation_id);
ukm_builder.SetId_Low(request.keepalive_token->GetLowForSerialization());
ukm_builder.SetId_High(request.keepalive_token->GetHighForSerialization());
auto* ukm_recorder = ukm::UkmRecorder::Get();
ukm_builder.Record(ukm_recorder->Get());
}
void FromGWSNavigationAndKeepAliveRequestTracker::StartCleanupTimer() {
if (cleanup_timer_.IsRunning()) {
return;
}
cleanup_timer_.Start(
FROM_HERE, kBeaconLeakageLoggingCleanupTimeout.Get(),
base::BindRepeating(
&FromGWSNavigationAndKeepAliveRequestTracker::OnCleanupTimeout,
weak_ptr_factory_.GetWeakPtr()));
}
void FromGWSNavigationAndKeepAliveRequestTracker::OnCleanupTimeout() {
per_frame_category_navigation_and_keepalive_requests_.clear();
}
bool FromGWSNavigationAndKeepAliveRequestTracker::FrameCategory::operator<(
const FrameCategory& rhs) const {
return std::tie(initator_rfh_id, category_id, ukm_source_id) <
std::tie(rhs.initator_rfh_id, rhs.category_id, rhs.ukm_source_id);
}
bool FromGWSNavigationAndKeepAliveRequestTracker::FrameCategory::operator==(
const FrameCategory& rhs) const {
return std::tie(initator_rfh_id, category_id, ukm_source_id) ==
std::tie(rhs.initator_rfh_id, rhs.category_id, rhs.ukm_source_id);
}
|