Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 1 | // Copyright 2023 The Chromium Authors |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/browser/preloading/preloading_decider.h" |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 6 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <cmath> |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 9 | #include <string_view> |
Andrew Rayskiy | f6599036 | 2024-02-27 18:43:24 | [diff] [blame] | 10 | #include <vector> |
| 11 | |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 12 | #include "base/check_is_test.h" |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 13 | #include "base/check_op.h" |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 14 | #include "base/containers/enum_set.h" |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 15 | #include "base/feature_list.h" |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 16 | #include "base/numerics/safe_conversions.h" |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 17 | #include "base/strings/string_split.h" |
Adithya Srinivasan | 19b84bf | 2024-09-06 14:41:53 | [diff] [blame] | 18 | #include "content/browser/devtools/devtools_instrumentation.h" |
kenoss | ec07285 | 2025-02-19 07:07:55 | [diff] [blame] | 19 | #include "content/browser/devtools/devtools_preload_storage.h" |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 20 | #include "content/browser/preloading/prefetch/no_vary_search_helper.h" |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 21 | #include "content/browser/preloading/prefetch/prefetch_document_manager.h" |
| 22 | #include "content/browser/preloading/prefetch/prefetch_params.h" |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 23 | #include "content/browser/preloading/preloading.h" |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 24 | #include "content/browser/preloading/preloading_confidence.h" |
Iman Saboori | 31f0949 | 2023-06-26 20:15:20 | [diff] [blame] | 25 | #include "content/browser/preloading/preloading_data_impl.h" |
Kouhei Ueno | a234a8e | 2023-11-15 05:24:42 | [diff] [blame] | 26 | #include "content/browser/preloading/preloading_trigger_type_impl.h" |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 27 | #include "content/browser/preloading/prerender/prerender_features.h" |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 28 | #include "content/browser/preloading/prerenderer_impl.h" |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 29 | #include "content/browser/preloading/speculation_rules/speculation_rules_util.h" |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 30 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Iman Saboori | eda025c7 | 2023-05-12 18:55:29 | [diff] [blame] | 31 | #include "content/public/browser/navigation_handle.h" |
William Liu | 238a9e5 | 2023-01-23 20:32:40 | [diff] [blame] | 32 | #include "content/public/browser/preloading.h" |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 33 | #include "content/public/browser/weak_document_ptr.h" |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 34 | #include "content/public/browser/web_contents.h" |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 35 | #include "third_party/blink/public/common/features.h" |
Iman Saboori | 50d5109 | 2023-06-09 22:17:30 | [diff] [blame] | 36 | #include "third_party/blink/public/mojom/preloading/anchor_element_interaction_host.mojom.h" |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 37 | #include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom-data-view.h" |
| 38 | #include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom-forward.h" |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 39 | |
| 40 | namespace content { |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 41 | |
| 42 | namespace { |
| 43 | |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 44 | content::PreloadingDecider::EagernessSet EagernessSetFromFeatureParam( |
| 45 | std::string_view value) { |
| 46 | content::PreloadingDecider::EagernessSet set; |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 47 | for (std::string_view piece : base::SplitStringPiece( |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 48 | value, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY)) { |
| 49 | if (piece == "conservative") { |
| 50 | set.Put(blink::mojom::SpeculationEagerness::kConservative); |
| 51 | } else if (piece == "moderate") { |
| 52 | set.Put(blink::mojom::SpeculationEagerness::kModerate); |
| 53 | } |
| 54 | } |
| 55 | return set; |
| 56 | } |
| 57 | |
Taiyo Mizuhashi | 59355fc | 2023-10-10 19:05:39 | [diff] [blame] | 58 | void OnPrefetchDestroyed(WeakDocumentPtr document, const GURL& url) { |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 59 | PreloadingDecider* preloading_decider = |
| 60 | PreloadingDecider::GetForCurrentDocument( |
| 61 | document.AsRenderFrameHostIfValid()); |
| 62 | if (preloading_decider) { |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 63 | preloading_decider->OnPreloadDiscarded( |
| 64 | {url, blink::mojom::SpeculationAction::kPrefetch}); |
| 65 | } |
| 66 | } |
| 67 | |
Taiyo Mizuhashi | 59355fc | 2023-10-10 19:05:39 | [diff] [blame] | 68 | void OnPrerenderCanceled(WeakDocumentPtr document, const GURL& url) { |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 69 | PreloadingDecider* preloading_decider = |
| 70 | PreloadingDecider::GetForCurrentDocument( |
| 71 | document.AsRenderFrameHostIfValid()); |
| 72 | if (preloading_decider) { |
| 73 | preloading_decider->OnPreloadDiscarded( |
| 74 | {url, blink::mojom::SpeculationAction::kPrerender}); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 78 | bool PredictionOccursInOtherWebContents( |
| 79 | const blink::mojom::SpeculationCandidate& candidate) { |
| 80 | return base::FeatureList::IsEnabled(blink::features::kPrerender2InNewTab) && |
| 81 | candidate.action == blink::mojom::SpeculationAction::kPrerender && |
| 82 | candidate.target_browsing_context_name_hint == |
| 83 | blink::mojom::SpeculationTargetHint::kBlank; |
| 84 | } |
| 85 | |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 86 | } // namespace |
| 87 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 88 | class PreloadingDecider::BehaviorConfig { |
| 89 | public: |
| 90 | BehaviorConfig() |
| 91 | : ml_model_eagerness_{blink::mojom::SpeculationEagerness::kModerate}, |
| 92 | ml_model_enacts_candidates_( |
| 93 | blink::features::kPreloadingModelEnactCandidates.Get()), |
| 94 | ml_model_prefetch_moderate_threshold_{std::clamp( |
| 95 | blink::features::kPreloadingModelPrefetchModerateThreshold.Get(), |
| 96 | 0, |
| 97 | 100)}, |
| 98 | ml_model_prerender_moderate_threshold_{std::clamp( |
| 99 | blink::features::kPreloadingModelPrerenderModerateThreshold.Get(), |
| 100 | 0, |
| 101 | 100)} { |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 102 | pointer_down_eagerness_ = |
Domenic Denicola | d9b38f9 | 2025-06-06 06:53:53 | [diff] [blame] | 103 | EagernessSet{blink::mojom::SpeculationEagerness::kConservative, |
| 104 | blink::mojom::SpeculationEagerness::kModerate}; |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 105 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 106 | pointer_hover_eagerness_ = |
Domenic Denicola | 89af201 | 2025-06-06 06:58:33 | [diff] [blame] | 107 | EagernessSet{blink::mojom::SpeculationEagerness::kModerate}; |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 108 | |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 109 | if (base::FeatureList::IsEnabled( |
| 110 | blink::features::kPreloadingEagerHeuristics)) { |
| 111 | pointer_down_eagerness_.Put(blink::mojom::SpeculationEagerness::kEager); |
| 112 | pointer_hover_eagerness_.Put(blink::mojom::SpeculationEagerness::kEager); |
| 113 | } |
| 114 | |
| 115 | CHECK(pointer_down_eagerness_.HasAll(pointer_hover_eagerness_)); |
| 116 | |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 117 | static const base::FeatureParam<std::string> kViewportHeuristicEagerness{ |
| 118 | &blink::features::kPreloadingViewportHeuristics, |
| 119 | "viewport_heuristic_eagerness", "moderate"}; |
| 120 | viewport_heuristic_eagerness_ = |
| 121 | EagernessSetFromFeatureParam(kViewportHeuristicEagerness.Get()); |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | EagernessSet EagernessSetForPredictor( |
| 125 | const PreloadingPredictor& predictor) const { |
Kevin McNee | 031e28c | 2024-03-21 20:55:00 | [diff] [blame] | 126 | if (predictor == preloading_predictor::kUrlPointerDownOnAnchor) { |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 127 | return pointer_down_eagerness_; |
Kevin McNee | 031e28c | 2024-03-21 20:55:00 | [diff] [blame] | 128 | } else if (predictor == preloading_predictor::kUrlPointerHoverOnAnchor) { |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 129 | return pointer_hover_eagerness_; |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 130 | } else if (predictor == preloading_predictor::kViewportHeuristic) { |
| 131 | return viewport_heuristic_eagerness_; |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 132 | } else if (predictor == |
| 133 | preloading_predictor::kPreloadingHeuristicsMLModel) { |
| 134 | return ml_model_eagerness_; |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 135 | } else { |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 136 | NOTREACHED() << "unexpected predictor " << predictor.name() << "/" |
| 137 | << predictor.ukm_value(); |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 141 | PreloadingConfidence GetThreshold( |
| 142 | const PreloadingPredictor& predictor, |
| 143 | blink::mojom::SpeculationAction action) const { |
| 144 | if (predictor == preloading_predictor::kUrlPointerDownOnAnchor) { |
| 145 | return kNoThreshold; |
| 146 | } else if (predictor == preloading_predictor::kUrlPointerHoverOnAnchor) { |
| 147 | return kNoThreshold; |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 148 | } else if (predictor == preloading_predictor::kViewportHeuristic) { |
| 149 | return kNoThreshold; |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 150 | } else if (predictor == |
| 151 | preloading_predictor::kPreloadingHeuristicsMLModel) { |
| 152 | switch (action) { |
| 153 | case blink::mojom::SpeculationAction::kPrefetch: |
| 154 | case blink::mojom::SpeculationAction::kPrefetchWithSubresources: |
| 155 | return ml_model_prefetch_moderate_threshold_; |
Lingqi Chi | b19879f | 2025-07-16 05:48:13 | [diff] [blame] | 156 | // TODO(https://crbug.com/428500219): Revisit the threshold for |
| 157 | // prerender-until-script; it could be lower than the threshold for |
| 158 | // prerender. |
| 159 | case blink::mojom::SpeculationAction::kPrerenderUntilScript: |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 160 | case blink::mojom::SpeculationAction::kPrerender: |
| 161 | return ml_model_prerender_moderate_threshold_; |
| 162 | } |
| 163 | } else { |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 164 | NOTREACHED() << "unexpected predictor " << predictor.name() << "/" |
| 165 | << predictor.ukm_value(); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | bool ml_model_enacts_candidates() const { |
| 170 | return ml_model_enacts_candidates_; |
| 171 | } |
| 172 | |
| 173 | private: |
| 174 | // Any confidence value is >= kNoThreshold, so the associated action will |
| 175 | // happen regardless of the confidence value. |
| 176 | static constexpr PreloadingConfidence kNoThreshold{0}; |
| 177 | |
| 178 | EagernessSet pointer_down_eagerness_; |
| 179 | EagernessSet pointer_hover_eagerness_; |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 180 | EagernessSet viewport_heuristic_eagerness_; |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 181 | const EagernessSet ml_model_eagerness_; |
| 182 | const bool ml_model_enacts_candidates_ = false; |
| 183 | const PreloadingConfidence ml_model_prefetch_moderate_threshold_{ |
| 184 | kNoThreshold}; |
| 185 | const PreloadingConfidence ml_model_prerender_moderate_threshold_{ |
| 186 | kNoThreshold}; |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 187 | }; |
| 188 | |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 189 | DOCUMENT_USER_DATA_KEY_IMPL(PreloadingDecider); |
| 190 | |
Johann | a9fe85f | 2023-01-17 10:15:43 | [diff] [blame] | 191 | PreloadingDecider::PreloadingDecider(RenderFrameHost* rfh) |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 192 | : DocumentUserData<PreloadingDecider>(rfh), |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 193 | behavior_config_(std::make_unique<BehaviorConfig>()), |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 194 | observer_for_testing_(nullptr), |
| 195 | preconnector_(render_frame_host()), |
| 196 | prefetcher_(render_frame_host()), |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 197 | prerenderer_(std::make_unique<PrerendererImpl>(render_frame_host())) { |
Adithya Srinivasan | 9d1b6fa | 2024-08-29 15:04:35 | [diff] [blame] | 198 | PrefetchDocumentManager::GetOrCreateForCurrentDocument(rfh) |
| 199 | ->SetPrefetchDestructionCallback( |
| 200 | base::BindRepeating(&OnPrefetchDestroyed, rfh->GetWeakDocumentPtr())); |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 201 | |
Hiroki Nakagawa | 8821cd88 | 2024-08-01 08:19:46 | [diff] [blame] | 202 | prerenderer_->SetPrerenderCancellationCallback( |
| 203 | base::BindRepeating(&OnPrerenderCanceled, rfh->GetWeakDocumentPtr())); |
kenoss | ec07285 | 2025-02-19 07:07:55 | [diff] [blame] | 204 | |
| 205 | // Forcibly create `DevToolsPreloadStorage` before we use it in |
| 206 | // `devtools_instrumentation`. |
| 207 | // |
| 208 | // For more details, see |
| 209 | // https://docs.google.com/document/d/1ZP7lYrtqZL9jC2xXieNY_UBMJL1sCrfmzTB8K6v4sD4/edit?resourcekey=0-fkbeQhkT3PhBb9FnnPgnZA&tab=t.e4x3d1nxzmy3#heading=h.4lvl0yr9vmh7 |
| 210 | // |
| 211 | // We found that there is a case that |
| 212 | // `devtools_instrumentation::DidUpdatePrerenderStatus()` in the same stack |
| 213 | // that called `DocumentAssociatedData::dtor()`. The former needs an instance |
| 214 | // of `DevToolsPreloadStorage`. If we call |
| 215 | // `DevToolsPreloadStorage::GetOrCreateForCurrentDocument()` there, the call |
| 216 | // may try to create an instance, but it is forbidden as the holder |
| 217 | // `DocumentAssociatedData` is in dtor and will crash. |
| 218 | // |
| 219 | // To mitigate this crash, we'll call `GetOrCreateForCurrentDocument()` here |
| 220 | // and use `GetForCurrentDocument()` in |
| 221 | // `devtools_instrumentation::DidUpdatePrerenderStatus()`. |
| 222 | // |
| 223 | // This works because: |
| 224 | // |
| 225 | // - The issue happens only on speculation rules preload, not |
| 226 | // browser-initiated preloads. This is because browser-initiated preloads |
| 227 | // don't emit CDP events as they don't have an initiator document, thus |
| 228 | // don't use `DevToolsPreloadStorage`. This is guaranteed by |
| 229 | // `DevToolsPrerenderAttempt::SetFailureReason()`. Therefore, we do this |
| 230 | // workaround in `PreloadingDecider`, not the common layer. |
| 231 | // So, we can assume that the below |
| 232 | // `DevToolsPreloadStorage::GetOrCreateForCurrentDocument()` is called and |
| 233 | // an instance basically exists. |
| 234 | // - `SupportsUserData::ClearAllUserData()` (which is called from |
| 235 | // `DocumentAssociatedData::dtor()`) swaps user data with an empty map and |
| 236 | // then drops the swapped map at the end of scope, which calls each dtor. |
| 237 | // https://source.chromium.org/chromium/chromium/src/+/main:base/supports_user_data.cc;l=142;drc=5f14562c01775211a40ebc3056d0a773c3569008 |
| 238 | // So, `DevToolsPreloadStorage::GetForCurrentDocument()` returns non null |
| 239 | // pointer iff the call is before `DocumentAssociatedData::dtor()` call. We |
| 240 | // can branch by the condition. |
| 241 | // |
| 242 | // Note that this is just a short-term fix. We are planning to fix the root |
| 243 | // cause. |
| 244 | // |
| 245 | // TODO(crbug.com/394631076): Fix the root cause and revert this. |
| 246 | DevToolsPreloadStorage::GetOrCreateForCurrentDocument(rfh); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 247 | } |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 248 | |
| 249 | PreloadingDecider::~PreloadingDecider() = default; |
| 250 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 251 | void PreloadingDecider::AddPreloadingPrediction( |
| 252 | const GURL& url, |
| 253 | PreloadingPredictor predictor, |
| 254 | PreloadingConfidence confidence) { |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 255 | WebContents* web_contents = |
| 256 | WebContents::FromRenderFrameHost(&render_frame_host()); |
| 257 | auto* preloading_data = |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 258 | PreloadingDataImpl::GetOrCreateForWebContents(web_contents); |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 259 | ukm::SourceId triggered_primary_page_source_id = |
| 260 | web_contents->GetPrimaryMainFrame()->GetPageUkmSourceId(); |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 261 | preloading_data->AddPreloadingPrediction( |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 262 | predictor, confidence, PreloadingData::GetSameURLMatcher(url), |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 263 | triggered_primary_page_source_id); |
Iman Saboori | 9455dfa | 2022-11-18 19:43:43 | [diff] [blame] | 264 | } |
| 265 | |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 266 | void PreloadingDecider::OnPointerDown(const GURL& url) { |
Iman Saboori | 9455dfa | 2022-11-18 19:43:43 | [diff] [blame] | 267 | if (observer_for_testing_) { |
| 268 | observer_for_testing_->OnPointerDown(url); |
| 269 | } |
Adithya Srinivasan | ecb97f2 | 2024-09-25 19:02:26 | [diff] [blame] | 270 | MaybeEnactCandidate(url, preloading_predictor::kUrlPointerDownOnAnchor, |
| 271 | PreloadingConfidence{100}, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 272 | /*fallback_to_preconnect=*/true, |
| 273 | /*eagerness_to_exclude=*/{}); |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 274 | } |
| 275 | |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 276 | void PreloadingDecider::OnPreloadingHeuristicsModelDone(const GURL& url, |
| 277 | float score) { |
Kevin McNee | 031e28c | 2024-03-21 20:55:00 | [diff] [blame] | 278 | CHECK(base::FeatureList::IsEnabled( |
| 279 | blink::features::kPreloadingHeuristicsMLModel)); |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 280 | WebContents* web_contents = |
| 281 | WebContents::FromRenderFrameHost(&render_frame_host()); |
| 282 | auto* preloading_data = static_cast<PreloadingDataImpl*>( |
| 283 | PreloadingData::GetOrCreateForWebContents(web_contents)); |
| 284 | preloading_data->AddExperimentalPreloadingPrediction( |
| 285 | /*name=*/"OnPreloadingHeuristicsMLModel", |
| 286 | /*url_match_predicate=*/PreloadingData::GetSameURLMatcher(url), |
| 287 | /*score=*/score, |
| 288 | /*min_score=*/0.0, |
| 289 | /*max_score=*/1.0, |
| 290 | /*buckets=*/100); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 291 | |
| 292 | if (!behavior_config_->ml_model_enacts_candidates()) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | ml_model_available_ = true; |
| 297 | |
| 298 | const PreloadingConfidence confidence{std::clamp( |
| 299 | base::saturated_cast<int>(std::nearbyint(score * 100.f)), 0, 100)}; |
| 300 | |
| 301 | MaybeEnactCandidate(url, preloading_predictor::kPreloadingHeuristicsMLModel, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 302 | confidence, /*fallback_to_preconnect=*/false, |
| 303 | /*eagerness_to_exclude=*/{}); |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 304 | } |
| 305 | |
Iman Saboori | 50d5109 | 2023-06-09 22:17:30 | [diff] [blame] | 306 | void PreloadingDecider::OnPointerHover( |
| 307 | const GURL& url, |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 308 | blink::mojom::AnchorElementPointerDataPtr mouse_data, |
| 309 | blink::mojom::SpeculationEagerness target_eagerness) { |
| 310 | // In non-test code, target eagerness must be either "moderate" or "eager". |
| 311 | if (target_eagerness != blink::mojom::SpeculationEagerness::kModerate && |
| 312 | target_eagerness != blink::mojom::SpeculationEagerness::kEager) { |
| 313 | CHECK_IS_TEST(); |
| 314 | return; |
| 315 | } |
| 316 | |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 317 | if (observer_for_testing_) { |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 318 | observer_for_testing_->OnPointerHover(url, target_eagerness); |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 319 | } |
Iman Saboori | 31f0949 | 2023-06-26 20:15:20 | [diff] [blame] | 320 | |
| 321 | WebContents* web_contents = |
| 322 | WebContents::FromRenderFrameHost(&render_frame_host()); |
| 323 | auto* preloading_data = static_cast<PreloadingDataImpl*>( |
| 324 | PreloadingData::GetOrCreateForWebContents(web_contents)); |
| 325 | preloading_data->AddExperimentalPreloadingPrediction( |
| 326 | /*name=*/"OnPointerHoverWithMotionEstimator", |
| 327 | /*url_match_predicate=*/PreloadingData::GetSameURLMatcher(url), |
| 328 | /*score=*/std::clamp(mouse_data->mouse_velocity, 0.0, 500.0), |
| 329 | /*min_score=*/0, |
| 330 | /*max_score=*/500, |
| 331 | /*buckets=*/100); |
| 332 | |
Adithya Srinivasan | ecb97f2 | 2024-09-25 19:02:26 | [diff] [blame] | 333 | // Preconnecting on hover events should not be done if the link is not safe |
| 334 | // to prefetch or prerender. |
| 335 | constexpr bool fallback_to_preconnect = false; |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 336 | // Filter `kModerate` for the "eager" mouse hover to prevent false preloading. |
| 337 | EagernessSet eagerness_to_exclude; |
| 338 | if (base::FeatureList::IsEnabled( |
| 339 | blink::features::kPreloadingEagerHeuristics)) { |
| 340 | eagerness_to_exclude = EagernessSet::All(); |
| 341 | eagerness_to_exclude.Remove(target_eagerness); |
| 342 | } |
Adithya Srinivasan | ecb97f2 | 2024-09-25 19:02:26 | [diff] [blame] | 343 | MaybeEnactCandidate(url, preloading_predictor::kUrlPointerHoverOnAnchor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 344 | PreloadingConfidence{100}, fallback_to_preconnect, |
Takashi Nakayama | e234c49 | 2025-08-04 03:16:48 | [diff] [blame] | 345 | eagerness_to_exclude); |
Iman Saboori | 03a661d | 2022-11-17 04:37:59 | [diff] [blame] | 346 | } |
| 347 | |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 348 | void PreloadingDecider::OnViewportHeuristicTriggered(const GURL& url) { |
| 349 | CHECK(base::FeatureList::IsEnabled( |
| 350 | blink::features::kPreloadingViewportHeuristics)); |
| 351 | static const base::FeatureParam<bool> kShouldEnactCandidates{ |
| 352 | &blink::features::kPreloadingViewportHeuristics, "enact_candidates", |
| 353 | false}; |
| 354 | const bool should_enact_candidates = kShouldEnactCandidates.Get(); |
| 355 | if (!should_enact_candidates) { |
| 356 | AddPreloadingPrediction(url, preloading_predictor::kViewportHeuristic, |
| 357 | PreloadingConfidence(100)); |
| 358 | return; |
| 359 | } |
| 360 | |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 361 | MaybeEnactCandidate( |
| 362 | url, preloading_predictor::kViewportHeuristic, PreloadingConfidence{100}, |
| 363 | /*fallback_to_preconnect=*/false, /*eagerness_to_exclude=*/{}); |
Adithya Srinivasan | 7239d4f | 2024-11-05 15:35:53 | [diff] [blame] | 364 | } |
| 365 | |
Kevin McNee | 7413329 | 2024-03-08 22:17:33 | [diff] [blame] | 366 | void PreloadingDecider::MaybeEnactCandidate( |
| 367 | const GURL& url, |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 368 | const PreloadingPredictor& enacting_predictor, |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 369 | PreloadingConfidence confidence, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 370 | bool fallback_to_preconnect, |
| 371 | EagernessSet eagerness_to_exclude) { |
| 372 | if (const auto [found, added_prediction] = MaybePrerender( |
| 373 | url, enacting_predictor, confidence, eagerness_to_exclude); |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 374 | found) { |
| 375 | // If the prediction is associated with another WebContents, don't duplicate |
| 376 | // it here. |
| 377 | if (!added_prediction) { |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 378 | AddPreloadingPrediction(url, enacting_predictor, confidence); |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 379 | } |
Kevin McNee | 7413329 | 2024-03-08 22:17:33 | [diff] [blame] | 380 | return; |
| 381 | } |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 382 | |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 383 | AddPreloadingPrediction(url, enacting_predictor, confidence); |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 384 | |
Kevin McNee | 7413329 | 2024-03-08 22:17:33 | [diff] [blame] | 385 | if (ShouldWaitForPrerenderResult(url)) { |
| 386 | // If there is a prerender in progress already, don't attempt a prefetch. |
| 387 | return; |
| 388 | } |
| 389 | |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 390 | if (MaybePrefetch(url, enacting_predictor, confidence, |
| 391 | eagerness_to_exclude)) { |
Kevin McNee | 7413329 | 2024-03-08 22:17:33 | [diff] [blame] | 392 | return; |
| 393 | } |
| 394 | // Ideally it is preferred to fallback to preconnect asynchronously if a |
| 395 | // prefetch attempt fails. We should revisit it later perhaps after having |
| 396 | // data showing it is worth doing so. |
| 397 | if (!fallback_to_preconnect || ShouldWaitForPrefetchResult(url)) { |
| 398 | return; |
| 399 | } |
| 400 | preconnector_.MaybePreconnect(url); |
| 401 | } |
| 402 | |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 403 | void PreloadingDecider::AddStandbyCandidate( |
| 404 | const blink::mojom::SpeculationCandidatePtr& candidate) { |
| 405 | SpeculationCandidateKey key{candidate->url, candidate->action}; |
| 406 | on_standby_candidates_[key].push_back(candidate.Clone()); |
| 407 | |
| 408 | GURL::Replacements replacements; |
| 409 | replacements.ClearRef(); |
| 410 | replacements.ClearQuery(); |
| 411 | if (candidate->no_vary_search_hint) { |
| 412 | SpeculationCandidateKey key_no_vary_search{ |
| 413 | candidate->url.ReplaceComponents(replacements), candidate->action}; |
| 414 | no_vary_search_hint_on_standby_candidates_[key_no_vary_search].insert(key); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | void PreloadingDecider::RemoveStandbyCandidate( |
| 419 | const SpeculationCandidateKey key) { |
| 420 | GURL::Replacements replacements; |
| 421 | replacements.ClearRef(); |
| 422 | replacements.ClearQuery(); |
| 423 | SpeculationCandidateKey key_no_vary_search{ |
| 424 | key.first.ReplaceComponents(replacements), key.second}; |
| 425 | auto it = no_vary_search_hint_on_standby_candidates_.find(key_no_vary_search); |
| 426 | if (it != no_vary_search_hint_on_standby_candidates_.end()) { |
| 427 | it->second.erase(key); |
Liviu Tinta | d6bf506 | 2023-05-24 19:17:00 | [diff] [blame] | 428 | if (it->second.empty()) { |
| 429 | no_vary_search_hint_on_standby_candidates_.erase(it); |
| 430 | } |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 431 | } |
| 432 | on_standby_candidates_.erase(key); |
| 433 | } |
| 434 | |
| 435 | void PreloadingDecider::ClearStandbyCandidates() { |
| 436 | no_vary_search_hint_on_standby_candidates_.clear(); |
| 437 | on_standby_candidates_.clear(); |
| 438 | } |
| 439 | |
Iman Saboori | 03a661d | 2022-11-17 04:37:59 | [diff] [blame] | 440 | void PreloadingDecider::UpdateSpeculationCandidates( |
| 441 | std::vector<blink::mojom::SpeculationCandidatePtr>& candidates) { |
Johann | a9fe85f | 2023-01-17 10:15:43 | [diff] [blame] | 442 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
Iman Saboori | 03a661d | 2022-11-17 04:37:59 | [diff] [blame] | 443 | if (observer_for_testing_) { |
| 444 | observer_for_testing_->UpdateSpeculationCandidates(candidates); |
| 445 | } |
Adithya Srinivasan | 19b84bf | 2024-09-06 14:41:53 | [diff] [blame] | 446 | devtools_instrumentation::DidUpdateSpeculationCandidates(render_frame_host(), |
| 447 | candidates); |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 448 | |
Iman Saboori | eda025c7 | 2023-05-12 18:55:29 | [diff] [blame] | 449 | WebContents* web_contents = |
| 450 | WebContents::FromRenderFrameHost(&render_frame_host()); |
Iman Saboori | 31f0949 | 2023-06-26 20:15:20 | [diff] [blame] | 451 | auto* preloading_data = static_cast<PreloadingDataImpl*>( |
| 452 | PreloadingData::GetOrCreateForWebContents(web_contents)); |
Iman Saboori | eda025c7 | 2023-05-12 18:55:29 | [diff] [blame] | 453 | preloading_data->SetIsNavigationInDomainCallback( |
| 454 | content_preloading_predictor::kSpeculationRules, |
| 455 | base::BindRepeating([](NavigationHandle* navigation_handle) -> bool { |
| 456 | return ui::PageTransitionIsWebTriggerable( |
Hiroki Nakagawa | e8c3ae13 | 2024-08-26 10:12:54 | [diff] [blame] | 457 | navigation_handle->GetPageTransition()); |
Iman Saboori | eda025c7 | 2023-05-12 18:55:29 | [diff] [blame] | 458 | })); |
Kevin McNee | 031e28c | 2024-03-21 20:55:00 | [diff] [blame] | 459 | PredictorDomainCallback is_new_link_nav = |
Adithya Srinivasan | 9c5b28b | 2024-10-21 22:29:23 | [diff] [blame] | 460 | base::BindRepeating(&PreloadingDataImpl::IsLinkClickNavigation); |
Adithya Srinivasan | ecb97f2 | 2024-09-25 19:02:26 | [diff] [blame] | 461 | preloading_data->SetIsNavigationInDomainCallback( |
| 462 | preloading_predictor::kUrlPointerDownOnAnchor, is_new_link_nav); |
| 463 | preloading_data->SetIsNavigationInDomainCallback( |
| 464 | preloading_predictor::kUrlPointerHoverOnAnchor, is_new_link_nav); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 465 | if (base::FeatureList::IsEnabled( |
| 466 | blink::features::kPreloadingHeuristicsMLModel) && |
| 467 | behavior_config_->ml_model_enacts_candidates()) { |
| 468 | preloading_data->SetIsNavigationInDomainCallback( |
| 469 | preloading_predictor::kPreloadingHeuristicsMLModel, is_new_link_nav); |
| 470 | } |
Adithya Srinivasan | 3d9bf5b | 2024-10-31 20:44:54 | [diff] [blame] | 471 | if (base::FeatureList::IsEnabled( |
| 472 | blink::features::kPreloadingViewportHeuristics)) { |
| 473 | preloading_data->SetIsNavigationInDomainCallback( |
| 474 | preloading_predictor::kViewportHeuristic, is_new_link_nav); |
| 475 | } |
Iman Saboori | eda025c7 | 2023-05-12 18:55:29 | [diff] [blame] | 476 | |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 477 | // Here we look for all preloading candidates that are safe to perform, but |
| 478 | // their eagerness level is not high enough to perform without the trigger |
| 479 | // form link selection heuristics logic. We then remove them from the |
| 480 | // |candidates| list to prevent them from being initiated and will add them |
| 481 | // to |on_standby_candidates_| to be later considered by the heuristics logic. |
| 482 | auto should_mark_as_on_standby = [&](const auto& candidate) { |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 483 | SpeculationCandidateKey key{candidate->url, candidate->action}; |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 484 | if (!IsImmediateSpeculationEagerness(candidate->eagerness) && |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 485 | processed_candidates_.find(key) == processed_candidates_.end()) { |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 486 | // A PreloadingPrediction is intentionally not created for these |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 487 | // candidates. Non-immediate rules aren't predictions per se, but a |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 488 | // declaration to the browser that preloading would be safe. |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 489 | AddStandbyCandidate(candidate); |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 490 | // TODO(isaboori) In current implementation, after calling prefetcher |
| 491 | // ProcessCandidatesForPrefetch, the prefetch_service starts checking the |
| 492 | // eligibility of the candidates and it will add any eligible candidates |
| 493 | // to the prefetch_queue_starts and starts prefetching them as soon as |
| 494 | // possible. For that reason here we remove on-standby candidates from the |
| 495 | // list. The prefetch service should be updated to let us pass the |
| 496 | // on-standby candidates to prefetch_service from here to let it check |
| 497 | // their eligibility right away without starting to prefetch them. It |
| 498 | // should also be possible to trigger the start of the prefetch based on |
| 499 | // heuristics. |
| 500 | return true; |
| 501 | } |
| 502 | |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 503 | processed_candidates_[key].push_back(candidate.Clone()); |
| 504 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 505 | // TODO(crbug.com/40230530): Pass the action requested by speculation rules |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 506 | // to PreloadingPrediction. |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 507 | // A new web contents will be created for the case of prerendering into a |
| 508 | // new tab, so recording PreloadingPrediction is delayed until |
| 509 | // PrerenderNewTabHandle::StartPrerendering. |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 510 | bool add_preloading_prediction = |
| 511 | !PredictionOccursInOtherWebContents(*candidate); |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 512 | |
| 513 | if (add_preloading_prediction) { |
| 514 | PreloadingTriggerType trigger_type = |
| 515 | PreloadingTriggerTypeFromSpeculationInjectionType( |
| 516 | candidate->injection_type); |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 517 | // Immediate candidates are enacted by the same predictor that creates |
| 518 | // them. |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 519 | PreloadingPredictor enacting_predictor = |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 520 | GetPredictorForPreloadingTriggerType(trigger_type); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 521 | AddPreloadingPrediction(candidate->url, std::move(enacting_predictor), |
| 522 | PreloadingConfidence{100}); |
HuanPo Lin | 12ce3b5fb4 | 2024-03-04 09:01:14 | [diff] [blame] | 523 | } |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 524 | |
| 525 | return false; |
| 526 | }; |
| 527 | |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 528 | ClearStandbyCandidates(); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 529 | |
| 530 | // The lists of SpeculationCandidates cached in |processed_candidates_| will |
| 531 | // be stale now, so we clear the lists now and repopulate them below. |
| 532 | for (auto& entry : processed_candidates_) { |
| 533 | entry.second.clear(); |
| 534 | } |
| 535 | |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 536 | // Move immediage candidates to the front. This will avoid unnecessarily |
| 537 | // marking some non-immediate candidates as on-standby when there is an |
| 538 | // immediate candidate with the same URL that will be processed immediately. |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 539 | std::ranges::stable_partition(candidates, [](const auto& candidate) { |
| 540 | return IsImmediateSpeculationEagerness(candidate->eagerness); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 541 | }); |
| 542 | |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 543 | // The candidates remaining after this call will be all immediate candidates, |
| 544 | // and all non-immediate candidates whose (url, action) pair has already been |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 545 | // processed. |
Andrew Rayskiy | f6599036 | 2024-02-27 18:43:24 | [diff] [blame] | 546 | std::erase_if(candidates, should_mark_as_on_standby); |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 547 | |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 548 | // TODO(crbug.com/381687257): Combine all speculation rules tags merging logic |
| 549 | // in PreloadingDecider to reduce code redundancy. |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 550 | // Aggregate all tags for immediate candidates. |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 551 | std::map<SpeculationCandidateKey, std::vector<std::optional<std::string>>> |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 552 | tags_map_for_immediate_preloading; |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 553 | for (auto& candidate : candidates) { |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 554 | if (!IsImmediateSpeculationEagerness(candidate->eagerness)) { |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 555 | continue; |
| 556 | } |
| 557 | |
| 558 | SpeculationCandidateKey key{candidate->url, candidate->action}; |
| 559 | for (const auto& tag : candidate->tags) { |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 560 | tags_map_for_immediate_preloading[key].push_back(tag); |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 561 | } |
| 562 | } |
| 563 | |
| 564 | for (auto& candidate : candidates) { |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 565 | if (!IsImmediateSpeculationEagerness(candidate->eagerness)) { |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 566 | continue; |
| 567 | } |
| 568 | |
| 569 | SpeculationCandidateKey key{candidate->url, candidate->action}; |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 570 | if (tags_map_for_immediate_preloading.count(key) != 0) { |
| 571 | candidate->tags = tags_map_for_immediate_preloading[key]; |
HuanPo Lin | 5860e8d | 2025-03-31 00:52:36 | [diff] [blame] | 572 | } |
| 573 | } |
| 574 | |
Adithya Srinivasan | 94ec2fea | 2023-05-01 17:02:15 | [diff] [blame] | 575 | prefetcher_.ProcessCandidatesForPrefetch(candidates); |
Robert Lin | a5f5ca7 | 2023-03-23 02:44:48 | [diff] [blame] | 576 | |
Adithya Srinivasan | 94ec2fea | 2023-05-01 17:02:15 | [diff] [blame] | 577 | prerenderer_->ProcessCandidatesForPrerender(candidates); |
Iman Saboori | 03a661d | 2022-11-17 04:37:59 | [diff] [blame] | 578 | } |
| 579 | |
Yoichi Osato | fed56ff8 | 2024-02-29 01:13:45 | [diff] [blame] | 580 | void PreloadingDecider::OnLCPPredicted() { |
| 581 | prerenderer_->OnLCPPredicted(); |
| 582 | } |
| 583 | |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 584 | std::vector<std::optional<std::string>> |
| 585 | PreloadingDecider::GetMergedSpeculationTagsFromSuitableCandidates( |
| 586 | const PreloadingDecider::SpeculationCandidateKey& lookup_key, |
| 587 | const PreloadingPredictor& enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 588 | PreloadingConfidence confidence, |
| 589 | EagernessSet eagerness_to_exclude) { |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 590 | std::vector<std::optional<std::string>> merged_tags; |
| 591 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 592 | // Find all suitable candidates. |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 593 | auto suitable_candidates = FindSuitableCandidates( |
| 594 | lookup_key, enacting_predictor, confidence, eagerness_to_exclude); |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 595 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 596 | // Iterate through all suitable candidates and merge their tags. |
| 597 | for (const auto& candidate_pair : suitable_candidates) { |
| 598 | for (const auto& tag : candidate_pair.second->tags) { |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 599 | if (!base::Contains(merged_tags, tag)) { |
| 600 | merged_tags.push_back(tag); |
| 601 | } |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | return merged_tags; |
| 606 | } |
| 607 | |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 608 | bool PreloadingDecider::MaybePrefetch( |
| 609 | const GURL& url, |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 610 | const PreloadingPredictor& enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 611 | PreloadingConfidence confidence, |
| 612 | EagernessSet eagerness_to_exclude) { |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 613 | SpeculationCandidateKey key{url, blink::mojom::SpeculationAction::kPrefetch}; |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 614 | std::vector<std::optional<std::string>> merged_tags = |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 615 | GetMergedSpeculationTagsFromSuitableCandidates( |
| 616 | key, enacting_predictor, confidence, eagerness_to_exclude); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 617 | std::optional<std::pair<PreloadingDecider::SpeculationCandidateKey, |
| 618 | blink::mojom::SpeculationCandidatePtr>> |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 619 | matched_candidate_pair = GetMatchedPreloadingCandidate( |
| 620 | key, enacting_predictor, confidence, eagerness_to_exclude); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 621 | if (!matched_candidate_pair.has_value()) { |
| 622 | return false; |
| 623 | } |
| 624 | |
| 625 | key = matched_candidate_pair.value().first; |
HuanPo Lin | 9b73037 | 2025-03-28 05:50:45 | [diff] [blame] | 626 | matched_candidate_pair.value().second->tags = merged_tags; |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 627 | bool result = prefetcher_.MaybePrefetch( |
| 628 | std::move(matched_candidate_pair.value().second), enacting_predictor); |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 629 | |
| 630 | auto it = on_standby_candidates_.find(key); |
Alex Gough | 42aed90 | 2024-07-23 03:17:47 | [diff] [blame] | 631 | CHECK(it != on_standby_candidates_.end()); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 632 | std::vector<blink::mojom::SpeculationCandidatePtr> candidates_for_key = |
| 633 | std::move(it->second); |
| 634 | RemoveStandbyCandidate(key); |
| 635 | processed_candidates_[std::move(key)] = std::move(candidates_for_key); |
| 636 | return result; |
| 637 | } |
| 638 | |
| 639 | std::optional<std::pair<PreloadingDecider::SpeculationCandidateKey, |
| 640 | blink::mojom::SpeculationCandidatePtr>> |
| 641 | PreloadingDecider::GetMatchedPreloadingCandidate( |
| 642 | const PreloadingDecider::SpeculationCandidateKey& lookup_key, |
| 643 | const PreloadingPredictor& enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 644 | PreloadingConfidence confidence, |
| 645 | EagernessSet eagerness_to_exclude) const { |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 646 | // Find all suitable candidates. |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 647 | auto suitable_candidates = FindSuitableCandidates( |
| 648 | lookup_key, enacting_predictor, confidence, eagerness_to_exclude); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 649 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 650 | if (suitable_candidates.empty()) { |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 651 | return std::nullopt; |
| 652 | } |
| 653 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 654 | // Return the first suitable candidate if any are found. |
| 655 | return std::move(suitable_candidates[0]); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 656 | } |
| 657 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 658 | // Enumerates all NVS-matched candidates and invokes the visitor for each match. |
| 659 | // If the visitor returns true, enumeration stops early. |
| 660 | template <typename Visitor> |
| 661 | void PreloadingDecider::EnumerateNoVarySearchMatchedCandidates( |
| 662 | const SpeculationCandidateKey& lookup_key, |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 663 | const PreloadingPredictor& enacting_predictor, |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 664 | PreloadingConfidence confidence, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 665 | EagernessSet eagerness_to_exclude, |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 666 | Visitor&& visitor) const { |
| 667 | // Remove query and ref from the URL for NVS matching. |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 668 | GURL::Replacements replacements; |
| 669 | replacements.ClearRef(); |
| 670 | replacements.ClearQuery(); |
| 671 | const GURL url_without_query_and_ref = |
| 672 | lookup_key.first.ReplaceComponents(replacements); |
| 673 | auto nvs_it = no_vary_search_hint_on_standby_candidates_.find( |
| 674 | {url_without_query_and_ref, lookup_key.second}); |
| 675 | if (nvs_it == no_vary_search_hint_on_standby_candidates_.end()) { |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 676 | return; |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 677 | } |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 678 | |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 679 | for (const auto& standby_key : nvs_it->second) { |
| 680 | CHECK_EQ(standby_key.second, lookup_key.second); |
| 681 | const GURL& preload_url = standby_key.first; |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 682 | auto standby_it = on_standby_candidates_.find(standby_key); |
| 683 | CHECK(standby_it != on_standby_candidates_.end()); |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 684 | |
| 685 | for (const auto& on_standby_candidate : standby_it->second) { |
| 686 | if (on_standby_candidate->no_vary_search_hint && |
| 687 | no_vary_search::ParseHttpNoVarySearchDataFromMojom( |
| 688 | on_standby_candidate->no_vary_search_hint) |
| 689 | .AreEquivalent(lookup_key.first, preload_url) && |
| 690 | IsSuitableCandidate(on_standby_candidate, enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 691 | confidence, standby_key.second, |
| 692 | eagerness_to_exclude)) { |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 693 | // If visitor returns true, stop enumeration early. |
| 694 | if (visitor(standby_key, on_standby_candidate)) { |
| 695 | return; |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | std::vector<std::pair<PreloadingDecider::SpeculationCandidateKey, |
| 703 | blink::mojom::SpeculationCandidatePtr>> |
| 704 | PreloadingDecider::FindSuitableCandidates( |
| 705 | const PreloadingDecider::SpeculationCandidateKey& lookup_key, |
| 706 | const PreloadingPredictor& enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 707 | PreloadingConfidence confidence, |
| 708 | EagernessSet eagerness_to_exclude) const { |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 709 | std::vector< |
| 710 | std::pair<SpeculationCandidateKey, blink::mojom::SpeculationCandidatePtr>> |
| 711 | suitable_candidates; |
| 712 | |
| 713 | // First, attempt a direct lookup for the exact key. |
| 714 | auto it = on_standby_candidates_.find(lookup_key); |
| 715 | if (it != on_standby_candidates_.end()) { |
| 716 | for (const auto& candidate : it->second) { |
| 717 | if (IsSuitableCandidate(candidate, enacting_predictor, confidence, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 718 | lookup_key.second, eagerness_to_exclude)) { |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 719 | suitable_candidates.emplace_back(lookup_key, candidate.Clone()); |
| 720 | } |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 724 | // If a direct match is found, return early. |
| 725 | if (!suitable_candidates.empty()) { |
| 726 | return suitable_candidates; |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 727 | } |
| 728 | |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 729 | // Use NVS matching to collect all suitable candidates. |
| 730 | EnumerateNoVarySearchMatchedCandidates( |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 731 | lookup_key, enacting_predictor, confidence, eagerness_to_exclude, |
Rulong Chen(陈汝龙) | 88e0688 | 2025-07-14 12:57:24 | [diff] [blame] | 732 | [&](const SpeculationCandidateKey& standby_key, |
| 733 | const blink::mojom::SpeculationCandidatePtr& candidate) { |
| 734 | suitable_candidates.emplace_back(standby_key, candidate.Clone()); |
| 735 | return false; // Continue enumeration to collect all matches. |
| 736 | }); |
| 737 | |
| 738 | return suitable_candidates; |
| 739 | } |
| 740 | |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 741 | bool PreloadingDecider::ShouldWaitForPrefetchResult(const GURL& url) { |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 742 | // TODO(liviutinta): Don't implement any No-Vary-Search hint matching here |
| 743 | // for now. It is not clear how to match `url` with a `processed_candidate`. |
| 744 | // Also, for a No-Vary-Search hint matched candidate we might end up not |
| 745 | // using the processed_candidate at all. We will revisit this later. |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 746 | auto it = processed_candidates_.find( |
| 747 | {url, blink::mojom::SpeculationAction::kPrefetch}); |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 748 | if (it == processed_candidates_.end()) { |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 749 | return false; |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 750 | } |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 751 | return !prefetcher_.IsPrefetchAttemptFailedOrDiscarded(url); |
| 752 | } |
| 753 | |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 754 | std::pair<bool, bool> PreloadingDecider::MaybePrerender( |
| 755 | const GURL& url, |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 756 | const PreloadingPredictor& enacting_predictor, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 757 | PreloadingConfidence confidence, |
| 758 | EagernessSet eagerness_to_exclude) { |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 759 | std::pair<bool, bool> result{false, false}; |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 760 | SpeculationCandidateKey key{url, blink::mojom::SpeculationAction::kPrerender}; |
HuanPo Lin | 9262183f | 2025-03-31 11:33:23 | [diff] [blame] | 761 | std::vector<std::optional<std::string>> merged_tags = |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 762 | GetMergedSpeculationTagsFromSuitableCandidates( |
| 763 | key, enacting_predictor, confidence, eagerness_to_exclude); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 764 | std::optional<std::pair<PreloadingDecider::SpeculationCandidateKey, |
| 765 | blink::mojom::SpeculationCandidatePtr>> |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 766 | matched_candidate_pair = GetMatchedPreloadingCandidate( |
| 767 | key, enacting_predictor, confidence, eagerness_to_exclude); |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 768 | if (!matched_candidate_pair.has_value()) { |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 769 | return result; |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 770 | } |
| 771 | |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 772 | key = matched_candidate_pair.value().first; |
HuanPo Lin | 9262183f | 2025-03-31 11:33:23 | [diff] [blame] | 773 | matched_candidate_pair.value().second->tags = merged_tags; |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 774 | blink::mojom::SpeculationCandidatePtr candidate = |
| 775 | std::move(matched_candidate_pair.value().second); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 776 | result.first = |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 777 | prerenderer_->MaybePrerender(candidate, enacting_predictor, confidence); |
Kevin McNee | 98e068a | 2024-04-09 20:12:34 | [diff] [blame] | 778 | |
| 779 | result.second = |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 780 | result.first && PredictionOccursInOtherWebContents(*candidate); |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 781 | |
Liviu Tinta | 52efaa06 | 2024-06-14 22:56:42 | [diff] [blame] | 782 | auto it = on_standby_candidates_.find(key); |
Alex Gough | 42aed90 | 2024-07-23 03:17:47 | [diff] [blame] | 783 | CHECK(it != on_standby_candidates_.end()); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 784 | std::vector<blink::mojom::SpeculationCandidatePtr> processed = |
| 785 | std::move(it->second); |
Liviu Tinta | 8a3a7bc | 2023-05-23 00:44:34 | [diff] [blame] | 786 | RemoveStandbyCandidate(it->first); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 787 | processed_candidates_[std::move(key)] = std::move(processed); |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 788 | return result; |
| 789 | } |
| 790 | |
| 791 | bool PreloadingDecider::ShouldWaitForPrerenderResult(const GURL& url) { |
| 792 | auto it = processed_candidates_.find( |
| 793 | {url, blink::mojom::SpeculationAction::kPrerender}); |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 794 | if (it == processed_candidates_.end()) { |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 795 | return false; |
Iman Saboori | 86e1b6d | 2023-08-28 18:42:35 | [diff] [blame] | 796 | } |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 797 | return prerenderer_->ShouldWaitForPrerenderResult(url); |
| 798 | } |
| 799 | |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 800 | bool PreloadingDecider::IsSuitableCandidate( |
| 801 | const blink::mojom::SpeculationCandidatePtr& candidate, |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 802 | const PreloadingPredictor& predictor, |
| 803 | PreloadingConfidence confidence, |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 804 | blink::mojom::SpeculationAction action, |
| 805 | EagernessSet eagerness_to_exclude) const { |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 806 | EagernessSet eagerness_set_for_predictor = |
| 807 | behavior_config_->EagernessSetForPredictor(predictor); |
Takashi Nakayama | 80498ee | 2025-07-31 05:31:31 | [diff] [blame] | 808 | eagerness_set_for_predictor.RemoveAll(eagerness_to_exclude); |
Kevin McNee | 842eb0a | 2024-04-11 20:14:16 | [diff] [blame] | 809 | |
| 810 | // If the ML model is available, its decisions supersede the hover heuristic. |
| 811 | if (ml_model_available_ && |
| 812 | predictor == preloading_predictor::kUrlPointerHoverOnAnchor) { |
| 813 | eagerness_set_for_predictor.RemoveAll( |
| 814 | behavior_config_->EagernessSetForPredictor( |
| 815 | preloading_predictor::kPreloadingHeuristicsMLModel)); |
| 816 | } |
| 817 | |
| 818 | return eagerness_set_for_predictor.Has(candidate->eagerness) && |
| 819 | confidence >= behavior_config_->GetThreshold(predictor, action); |
Jeremy Roman | 2cf0e77 | 2023-03-13 23:20:00 | [diff] [blame] | 820 | } |
| 821 | |
Iman Saboori | 64dc4de | 2022-11-29 18:58:06 | [diff] [blame] | 822 | PreloadingDeciderObserverForTesting* PreloadingDecider::SetObserverForTesting( |
| 823 | PreloadingDeciderObserverForTesting* observer) { |
| 824 | return std::exchange(observer_for_testing_, observer); |
| 825 | } |
| 826 | |
Taiyo Mizuhashi | f3098ea | 2023-10-11 13:14:26 | [diff] [blame] | 827 | Prerenderer& PreloadingDecider::GetPrerendererForTesting() { |
| 828 | CHECK(prerenderer_); |
| 829 | return *prerenderer_; |
| 830 | } |
| 831 | |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 832 | std::unique_ptr<Prerenderer> PreloadingDecider::SetPrerendererForTesting( |
| 833 | std::unique_ptr<Prerenderer> prerenderer) { |
Taiyo Mizuhashi | f3098ea | 2023-10-11 13:14:26 | [diff] [blame] | 834 | prerenderer->SetPrerenderCancellationCallback(base::BindRepeating( |
| 835 | &OnPrerenderCanceled, render_frame_host().GetWeakDocumentPtr())); |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 836 | return std::exchange(prerenderer_, std::move(prerenderer)); |
| 837 | } |
| 838 | |
| 839 | bool PreloadingDecider::IsOnStandByForTesting( |
| 840 | const GURL& url, |
Kevin McNee | 031e28c | 2024-03-21 20:55:00 | [diff] [blame] | 841 | blink::mojom::SpeculationAction action) const { |
| 842 | return on_standby_candidates_.contains({url, action}); |
| 843 | } |
| 844 | |
| 845 | bool PreloadingDecider::HasCandidatesForTesting() const { |
| 846 | return !on_standby_candidates_.empty() || |
| 847 | !no_vary_search_hint_on_standby_candidates_.empty() || |
| 848 | !processed_candidates_.empty(); |
Iman Saboori | 31bed87 | 2022-12-09 15:41:31 | [diff] [blame] | 849 | } |
| 850 | |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 851 | void PreloadingDecider::OnPreloadDiscarded(SpeculationCandidateKey key) { |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 852 | auto it = processed_candidates_.find(key); |
kenoss | 18aa959 | 2024-06-24 09:26:16 | [diff] [blame] | 853 | // If the preload is triggered outside of `PreloadingDecider`, ignore it. |
| 854 | // Currently, `PrerendererImpl` triggers prefetch ahead of prerender. |
| 855 | if (it == processed_candidates_.end()) { |
| 856 | return; |
| 857 | } |
| 858 | |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 859 | std::vector<blink::mojom::SpeculationCandidatePtr> candidates = |
| 860 | std::move(it->second); |
| 861 | processed_candidates_.erase(it); |
| 862 | for (const auto& candidate : candidates) { |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 863 | if (!IsImmediateSpeculationEagerness(candidate->eagerness)) { |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 864 | AddStandbyCandidate(candidate); |
| 865 | } |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 866 | // TODO(crbug.com/40064525): Add support for the case where |candidate|'s |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 867 | // eagerness is immediate one like `kImmediate`. In a scenario where the |
| 868 | // prefetch evicted is a non-immediate prefetch, we could theoretically |
| 869 | // reprefetch using the immediate candidate (and have it use the immediate |
| 870 | // prefetch quota). In that scenario, perhaps not evicting and just making |
| 871 | // the prefetch use the immediate limit might be a better option too. In the |
| 872 | // case where an immediate prefetch is evicted, we don't want to immediately |
| 873 | // try and reprefetch the candidate; it would defeat the purpose of evicting |
| 874 | // in the first place, and due to a possible-rentrancy into |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 875 | // PrefetchService::Prefetch(), it could cause us to exceed the limit. |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 876 | |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 877 | // TODO(crbug.com/40275452): Add implementation for immediate cases for |
Taiyo Mizuhashi | 295ccbdf | 2023-09-13 09:17:02 | [diff] [blame] | 878 | // prerender. |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 879 | } |
| 880 | } |
| 881 | |
Iman Saboori | 1607172 | 2022-11-04 14:59:46 | [diff] [blame] | 882 | } // namespace content |