Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [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 | |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 5 | #include "content/browser/preloading/prefetch/prefetch_container.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 6 | |
kenoss | 081c9d9 | 2024-11-13 10:21:57 | [diff] [blame] | 7 | #include "base/debug/crash_logging.h" |
David Sanders | 1b28c24 | 2025-01-27 00:46:53 | [diff] [blame] | 8 | #include "base/debug/dump_without_crashing.h" |
Max Curran | 983f21a5 | 2023-03-23 23:52:58 | [diff] [blame] | 9 | #include "base/metrics/histogram_functions.h" |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Daniel Cheng | 4f1c533 | 2025-06-19 19:37:21 | [diff] [blame] | 11 | #include "base/notimplemented.h" |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 12 | #include "base/notreached.h" |
kenoss | cc25ed1 | 2024-11-14 09:20:37 | [diff] [blame] | 13 | #include "base/strings/strcat.h" |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 14 | #include "base/time/time.h" |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 15 | #include "components/variations/net/variations_http_headers.h" |
Robert Lin | 233948df | 2023-02-10 01:37:39 | [diff] [blame] | 16 | #include "content/browser/devtools/devtools_instrumentation.h" |
Hiroshige Hayashizaki | 583b496 | 2025-08-19 18:58:26 | [diff] [blame] | 17 | #include "content/browser/devtools/network_service_devtools_observer.h" |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 18 | #include "content/browser/loader/navigation_url_loader_impl.h" |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 19 | #include "content/browser/preloading/prefetch/no_vary_search_helper.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 20 | #include "content/browser/preloading/prefetch/prefetch_cookie_listener.h" |
| 21 | #include "content/browser/preloading/prefetch/prefetch_document_manager.h" |
Hiroshige Hayashizaki | 1919c7c | 2023-10-13 21:46:42 | [diff] [blame] | 22 | #include "content/browser/preloading/prefetch/prefetch_features.h" |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 23 | #include "content/browser/preloading/prefetch/prefetch_match_resolver.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 24 | #include "content/browser/preloading/prefetch/prefetch_network_context.h" |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 25 | #include "content/browser/preloading/prefetch/prefetch_params.h" |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 26 | #include "content/browser/preloading/prefetch/prefetch_request.h" |
Hiroshige Hayashizaki | bf37abc3 | 2023-09-13 10:02:36 | [diff] [blame] | 27 | #include "content/browser/preloading/prefetch/prefetch_response_reader.h" |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 28 | #include "content/browser/preloading/prefetch/prefetch_servable_state.h" |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 29 | #include "content/browser/preloading/prefetch/prefetch_serving_handle.h" |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 30 | #include "content/browser/preloading/prefetch/prefetch_serving_page_metrics_container.h" |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 31 | #include "content/browser/preloading/prefetch/prefetch_single_redirect_hop.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 32 | #include "content/browser/preloading/prefetch/prefetch_status.h" |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 33 | #include "content/browser/preloading/prefetch/prefetch_streaming_url_loader.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 34 | #include "content/browser/preloading/prefetch/prefetch_type.h" |
Adithya Srinivasan | 38e0c40 | 2023-07-19 16:12:58 | [diff] [blame] | 35 | #include "content/browser/preloading/preloading_attempt_impl.h" |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 36 | #include "content/browser/preloading/preloading_trigger_type_impl.h" |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 37 | #include "content/browser/preloading/prerender/prerender_features.h" |
Peter Conn | 3816460 | 2025-08-06 14:41:19 | [diff] [blame] | 38 | #include "content/browser/preloading/proxy_lookup_client_impl.h" |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 39 | #include "content/browser/preloading/speculation_rules/speculation_rules_tags.h" |
Robert Lin | 233948df | 2023-02-10 01:37:39 | [diff] [blame] | 40 | #include "content/browser/renderer_host/frame_tree_node.h" |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 41 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
| 42 | #include "content/browser/web_contents/web_contents_impl.h" |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 43 | #include "content/public/browser/client_hints.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 44 | #include "content/public/browser/global_routing_id.h" |
Wayne Jackson Jr. | b22d53b2 | 2024-11-15 11:40:06 | [diff] [blame] | 45 | #include "content/public/browser/prefetch_request_status_listener.h" |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 46 | #include "content/public/browser/preloading.h" |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 47 | #include "content/public/browser/web_contents.h" |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 48 | #include "net/base/load_flags.h" |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 49 | #include "net/http/http_request_headers.h" |
| 50 | #include "net/url_request/redirect_util.h" |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 51 | #include "services/metrics/public/cpp/ukm_builders.h" |
| 52 | #include "services/metrics/public/cpp/ukm_recorder.h" |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 53 | #include "services/network/public/cpp/client_hints.h" |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 54 | #include "services/network/public/cpp/devtools_observer_util.h" |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 55 | #include "third_party/blink/public/common/client_hints/client_hints.h" |
David Risney | a1dd2bf | 2025-03-06 03:40:42 | [diff] [blame] | 56 | #include "third_party/blink/public/common/navigation/preloading_headers.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 57 | #include "url/gurl.h" |
| 58 | |
| 59 | namespace content { |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 60 | namespace { |
| 61 | |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 62 | PrefetchStatus PrefetchStatusFromIneligibleReason( |
| 63 | PreloadingEligibility eligibility) { |
| 64 | switch (eligibility) { |
| 65 | case PreloadingEligibility::kBatterySaverEnabled: |
| 66 | return PrefetchStatus::kPrefetchIneligibleBatterySaverEnabled; |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 67 | case PreloadingEligibility::kDataSaverEnabled: |
| 68 | return PrefetchStatus::kPrefetchIneligibleDataSaverEnabled; |
| 69 | case PreloadingEligibility::kExistingProxy: |
| 70 | return PrefetchStatus::kPrefetchIneligibleExistingProxy; |
| 71 | case PreloadingEligibility::kHostIsNonUnique: |
| 72 | return PrefetchStatus::kPrefetchIneligibleHostIsNonUnique; |
| 73 | case PreloadingEligibility::kNonDefaultStoragePartition: |
| 74 | return PrefetchStatus::kPrefetchIneligibleNonDefaultStoragePartition; |
| 75 | case PreloadingEligibility::kPrefetchProxyNotAvailable: |
| 76 | return PrefetchStatus::kPrefetchIneligiblePrefetchProxyNotAvailable; |
| 77 | case PreloadingEligibility::kPreloadingDisabled: |
| 78 | return PrefetchStatus::kPrefetchIneligiblePreloadingDisabled; |
| 79 | case PreloadingEligibility::kRetryAfter: |
| 80 | return PrefetchStatus::kPrefetchIneligibleRetryAfter; |
| 81 | case PreloadingEligibility::kSameSiteCrossOriginPrefetchRequiredProxy: |
| 82 | return PrefetchStatus:: |
| 83 | kPrefetchIneligibleSameSiteCrossOriginPrefetchRequiredProxy; |
| 84 | case PreloadingEligibility::kSchemeIsNotHttps: |
| 85 | return PrefetchStatus::kPrefetchIneligibleSchemeIsNotHttps; |
| 86 | case PreloadingEligibility::kUserHasCookies: |
| 87 | return PrefetchStatus::kPrefetchIneligibleUserHasCookies; |
| 88 | case PreloadingEligibility::kUserHasServiceWorker: |
| 89 | return PrefetchStatus::kPrefetchIneligibleUserHasServiceWorker; |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 90 | case PreloadingEligibility::kUserHasServiceWorkerNoFetchHandler: |
| 91 | return PrefetchStatus:: |
| 92 | kPrefetchIneligibleUserHasServiceWorkerNoFetchHandler; |
| 93 | case PreloadingEligibility::kRedirectFromServiceWorker: |
| 94 | return PrefetchStatus::kPrefetchIneligibleRedirectFromServiceWorker; |
| 95 | case PreloadingEligibility::kRedirectToServiceWorker: |
| 96 | return PrefetchStatus::kPrefetchIneligibleRedirectToServiceWorker; |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 97 | |
| 98 | case PreloadingEligibility::kEligible: |
| 99 | default: |
| 100 | // Other ineligible cases are not used in `PrefetchService`. |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 101 | NOTREACHED(); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 105 | std::optional<PreloadingTriggeringOutcome> TriggeringOutcomeFromStatus( |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 106 | PrefetchStatus prefetch_status) { |
| 107 | switch (prefetch_status) { |
| 108 | case PrefetchStatus::kPrefetchNotFinishedInTime: |
| 109 | return PreloadingTriggeringOutcome::kRunning; |
| 110 | case PrefetchStatus::kPrefetchSuccessful: |
| 111 | return PreloadingTriggeringOutcome::kReady; |
| 112 | case PrefetchStatus::kPrefetchResponseUsed: |
| 113 | return PreloadingTriggeringOutcome::kSuccess; |
| 114 | case PrefetchStatus::kPrefetchIsPrivacyDecoy: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 115 | case PrefetchStatus::kPrefetchIsStale: |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 116 | case PrefetchStatus::kPrefetchFailedNetError: |
| 117 | case PrefetchStatus::kPrefetchFailedNon2XX: |
| 118 | case PrefetchStatus::kPrefetchFailedMIMENotSupported: |
| 119 | case PrefetchStatus::kPrefetchFailedInvalidRedirect: |
| 120 | case PrefetchStatus::kPrefetchFailedIneligibleRedirect: |
Adithya Srinivasan | b7f08b4 | 2023-12-05 15:49:36 | [diff] [blame] | 121 | case PrefetchStatus::kPrefetchEvictedAfterCandidateRemoved: |
| 122 | case PrefetchStatus::kPrefetchEvictedForNewerPrefetch: |
Hiroshige Hayashizaki | 238198d8 | 2023-10-23 23:04:12 | [diff] [blame] | 123 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorker: |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 124 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorkerNoFetchHandler: |
| 125 | case PrefetchStatus::kPrefetchIneligibleRedirectFromServiceWorker: |
| 126 | case PrefetchStatus::kPrefetchIneligibleRedirectToServiceWorker: |
Hiroshige Hayashizaki | 238198d8 | 2023-10-23 23:04:12 | [diff] [blame] | 127 | case PrefetchStatus::kPrefetchIneligibleSchemeIsNotHttps: |
| 128 | case PrefetchStatus::kPrefetchIneligibleNonDefaultStoragePartition: |
| 129 | case PrefetchStatus::kPrefetchIneligibleHostIsNonUnique: |
| 130 | case PrefetchStatus::kPrefetchIneligibleDataSaverEnabled: |
| 131 | case PrefetchStatus::kPrefetchIneligibleBatterySaverEnabled: |
| 132 | case PrefetchStatus::kPrefetchIneligiblePreloadingDisabled: |
| 133 | case PrefetchStatus::kPrefetchIneligibleExistingProxy: |
| 134 | case PrefetchStatus::kPrefetchIneligibleUserHasCookies: |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 135 | case PrefetchStatus::kPrefetchIneligibleRetryAfter: |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 136 | case PrefetchStatus::kPrefetchNotUsedCookiesChanged: |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 137 | case PrefetchStatus::kPrefetchNotUsedProbeFailed: |
| 138 | case PrefetchStatus:: |
Hiroshige Hayashizaki | 238198d8 | 2023-10-23 23:04:12 | [diff] [blame] | 139 | kPrefetchIneligibleSameSiteCrossOriginPrefetchRequiredProxy: |
Adithya Srinivasan | 0aab970 | 2024-08-01 13:25:42 | [diff] [blame] | 140 | case PrefetchStatus::kPrefetchIneligiblePrefetchProxyNotAvailable: |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 141 | case PrefetchStatus::kPrefetchEvictedAfterBrowsingDataRemoved: |
Robert Lin | 07e5257 | 2023-05-22 07:10:18 | [diff] [blame] | 142 | return PreloadingTriggeringOutcome::kFailure; |
| 143 | case PrefetchStatus::kPrefetchHeldback: |
Robert Lin | 07e5257 | 2023-05-22 07:10:18 | [diff] [blame] | 144 | case PrefetchStatus::kPrefetchNotStarted: |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 145 | return std::nullopt; |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 146 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 147 | return std::nullopt; |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 148 | } |
| 149 | |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 150 | // Returns true if SetPrefetchStatus(|status|) can be called after a prefetch |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 151 | // has already been marked as failed. We ignore such status updates |
Taiyo Mizuhashi | 4e69317 | 2025-03-26 12:07:50 | [diff] [blame] | 152 | // as they may end up overwriting the initial failure reason. |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 153 | bool StatusUpdateIsPossibleAfterFailure(PrefetchStatus status) { |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 154 | switch (status) { |
| 155 | case PrefetchStatus::kPrefetchEvictedAfterCandidateRemoved: |
| 156 | case PrefetchStatus::kPrefetchIsStale: |
Steven Wei | cbbda74f | 2025-03-19 02:12:34 | [diff] [blame] | 157 | case PrefetchStatus::kPrefetchEvictedForNewerPrefetch: |
| 158 | case PrefetchStatus::kPrefetchEvictedAfterBrowsingDataRemoved: { |
kenoss | 081c9d9 | 2024-11-13 10:21:57 | [diff] [blame] | 159 | CHECK(TriggeringOutcomeFromStatus(status) == |
| 160 | PreloadingTriggeringOutcome::kFailure); |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 161 | return true; |
kenoss | 081c9d9 | 2024-11-13 10:21:57 | [diff] [blame] | 162 | } |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 163 | case PrefetchStatus::kPrefetchNotFinishedInTime: |
| 164 | case PrefetchStatus::kPrefetchSuccessful: |
| 165 | case PrefetchStatus::kPrefetchResponseUsed: |
| 166 | case PrefetchStatus::kPrefetchIsPrivacyDecoy: |
| 167 | case PrefetchStatus::kPrefetchFailedNetError: |
| 168 | case PrefetchStatus::kPrefetchFailedNon2XX: |
| 169 | case PrefetchStatus::kPrefetchFailedMIMENotSupported: |
| 170 | case PrefetchStatus::kPrefetchFailedInvalidRedirect: |
| 171 | case PrefetchStatus::kPrefetchFailedIneligibleRedirect: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 172 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorker: |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 173 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorkerNoFetchHandler: |
| 174 | case PrefetchStatus::kPrefetchIneligibleRedirectFromServiceWorker: |
| 175 | case PrefetchStatus::kPrefetchIneligibleRedirectToServiceWorker: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 176 | case PrefetchStatus::kPrefetchIneligibleSchemeIsNotHttps: |
| 177 | case PrefetchStatus::kPrefetchIneligibleNonDefaultStoragePartition: |
| 178 | case PrefetchStatus::kPrefetchIneligibleHostIsNonUnique: |
| 179 | case PrefetchStatus::kPrefetchIneligibleDataSaverEnabled: |
| 180 | case PrefetchStatus::kPrefetchIneligibleBatterySaverEnabled: |
| 181 | case PrefetchStatus::kPrefetchIneligiblePreloadingDisabled: |
| 182 | case PrefetchStatus::kPrefetchIneligibleExistingProxy: |
| 183 | case PrefetchStatus::kPrefetchIneligibleUserHasCookies: |
| 184 | case PrefetchStatus::kPrefetchIneligibleRetryAfter: |
| 185 | case PrefetchStatus::kPrefetchNotUsedCookiesChanged: |
| 186 | case PrefetchStatus::kPrefetchNotUsedProbeFailed: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 187 | case PrefetchStatus:: |
| 188 | kPrefetchIneligibleSameSiteCrossOriginPrefetchRequiredProxy: |
| 189 | case PrefetchStatus::kPrefetchHeldback: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 190 | case PrefetchStatus::kPrefetchNotStarted: |
| 191 | case PrefetchStatus::kPrefetchIneligiblePrefetchProxyNotAvailable: |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 192 | return false; |
| 193 | } |
| 194 | } |
| 195 | |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 196 | void RecordPrefetchProxyPrefetchMainframeNetError(int net_error) { |
| 197 | base::UmaHistogramSparse("PrefetchProxy.Prefetch.Mainframe.NetError", |
| 198 | std::abs(net_error)); |
| 199 | } |
| 200 | |
| 201 | void RecordPrefetchProxyPrefetchMainframeBodyLength(int64_t body_length) { |
| 202 | UMA_HISTOGRAM_COUNTS_10M("PrefetchProxy.Prefetch.Mainframe.BodyLength", |
| 203 | body_length); |
| 204 | } |
| 205 | |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 206 | bool CalculateIsLikelyAheadOfPrerender( |
kenoss | c3f9a2c | 2025-03-06 15:35:51 | [diff] [blame] | 207 | const PreloadPipelineInfoImpl& preload_pipeline_info) { |
kenoss | c649294 | 2025-04-10 19:36:32 | [diff] [blame] | 208 | if (!features::UsePrefetchPrerenderIntegration()) { |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 212 | switch (preload_pipeline_info.planned_max_preloading_type()) { |
| 213 | case PreloadingType::kPrefetch: |
| 214 | return false; |
| 215 | case PreloadingType::kPrerender: |
Lingqi Chi | 844e05d4 | 2025-07-23 02:50:50 | [diff] [blame] | 216 | case PreloadingType::kPrerenderUntilScript: |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 217 | return true; |
| 218 | case PreloadingType::kUnspecified: |
| 219 | case PreloadingType::kPreconnect: |
| 220 | case PreloadingType::kNoStatePrefetch: |
| 221 | case PreloadingType::kLinkPreview: |
| 222 | NOTREACHED(); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 223 | } |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 224 | } |
| 225 | |
Hiroshige Hayashizaki | 29e14dd | 2025-03-14 16:03:07 | [diff] [blame] | 226 | PrefetchContainer::PrefetchResponseCompletedCallbackForTesting& |
| 227 | GetPrefetchResponseCompletedCallbackForTesting() { |
| 228 | static base::NoDestructor< |
| 229 | PrefetchContainer::PrefetchResponseCompletedCallbackForTesting> |
| 230 | prefetch_response_completed_callback_for_testing; |
| 231 | return *prefetch_response_completed_callback_for_testing; |
| 232 | } |
| 233 | |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 234 | } // namespace |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 235 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 236 | PrefetchContainer::PrefetchContainer( |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 237 | RenderFrameHostImpl& referring_render_frame_host, |
Hiroshige Hayashizaki | 2df4529 | 2023-10-10 22:59:03 | [diff] [blame] | 238 | const blink::DocumentToken& referring_document_token, |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 239 | const GURL& url, |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 240 | const PrefetchType& prefetch_type, |
Kevin McNee | e6ca089 | 2022-09-28 15:36:22 | [diff] [blame] | 241 | const blink::mojom::Referrer& referrer, |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 242 | std::optional<SpeculationRulesTags> speculation_rules_tags, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 243 | std::optional<net::HttpNoVarySearchData> no_vary_search_hint, |
Taiyo Mizuhashi | d7c85699 | 2025-06-23 08:56:02 | [diff] [blame] | 244 | std::optional<PrefetchPriority> priority, |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 245 | base::WeakPtr<PrefetchDocumentManager> prefetch_document_manager, |
kenoss | 3bd73b8 | 2024-10-10 20:33:49 | [diff] [blame] | 246 | scoped_refptr<PreloadPipelineInfo> preload_pipeline_info, |
Taiyo Mizuhashi | 0192d3f | 2024-02-15 05:09:43 | [diff] [blame] | 247 | base::WeakPtr<PreloadingAttempt> attempt) |
Hiroshige Hayashizaki | ce441325 | 2025-08-19 19:05:45 | [diff] [blame] | 248 | : PrefetchContainer(std::make_unique<PrefetchRequest>( |
| 249 | prefetch_type, |
| 250 | PrefetchKey(referring_document_token, url), |
| 251 | std::move(no_vary_search_hint), |
| 252 | std::move(priority), |
| 253 | std::move(preload_pipeline_info), |
| 254 | std::move(attempt), |
| 255 | WebContentsImpl::FromRenderFrameHostImpl(&referring_render_frame_host) |
| 256 | ->GetOrCreateWebPreferences() |
| 257 | .javascript_enabled, |
| 258 | referrer, |
| 259 | referring_render_frame_host.GetLastCommittedOrigin(), |
| 260 | referring_render_frame_host.GetBrowserContext()->GetWeakPtr(), |
| 261 | std::move(speculation_rules_tags), |
| 262 | /*Must be empty: additional_headers=*/net::HttpRequestHeaders(), |
| 263 | PrefetchContainerDefaultTtlInPrefetchService(), |
| 264 | /*holdback_status_override=*/std::nullopt, |
| 265 | /*should_append_variations_header=*/true, |
| 266 | /*should_disable_block_until_head_timeout=*/false, |
| 267 | PrefetchRendererInitiatorInfo( |
| 268 | referring_render_frame_host, |
| 269 | std::move(prefetch_document_manager)))) {} |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 270 | |
| 271 | PrefetchContainer::PrefetchContainer( |
| 272 | WebContents& referring_web_contents, |
| 273 | const GURL& url, |
| 274 | const PrefetchType& prefetch_type, |
Taiyo Mizuhashi | 49959d0 | 2025-04-22 16:07:54 | [diff] [blame] | 275 | const std::string& embedder_histogram_suffix, |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 276 | const blink::mojom::Referrer& referrer, |
| 277 | const std::optional<url::Origin>& referring_origin, |
| 278 | std::optional<net::HttpNoVarySearchData> no_vary_search_hint, |
Taiyo Mizuhashi | d7c85699 | 2025-06-23 08:56:02 | [diff] [blame] | 279 | std::optional<PrefetchPriority> priority, |
kenoss | a1af66f1 | 2025-03-07 06:10:55 | [diff] [blame] | 280 | scoped_refptr<PreloadPipelineInfo> preload_pipeline_info, |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 281 | base::WeakPtr<PreloadingAttempt> attempt, |
kenoss | 6938554c | 2025-06-10 10:18:17 | [diff] [blame] | 282 | std::optional<PreloadingHoldbackStatus> holdback_status_override, |
| 283 | std::optional<base::TimeDelta> ttl) |
Hiroshige Hayashizaki | ce441325 | 2025-08-19 19:05:45 | [diff] [blame] | 284 | : PrefetchContainer(std::make_unique<PrefetchRequest>( |
| 285 | prefetch_type, |
| 286 | PrefetchKey(std::optional<blink::DocumentToken>(std::nullopt), url), |
| 287 | std::move(no_vary_search_hint), |
| 288 | std::move(priority), |
| 289 | std::move(preload_pipeline_info), |
| 290 | std::move(attempt), |
| 291 | referring_web_contents.GetOrCreateWebPreferences().javascript_enabled, |
| 292 | referrer, |
| 293 | referring_origin, |
| 294 | referring_web_contents.GetBrowserContext()->GetWeakPtr(), |
| 295 | /*speculation_rules_tags=*/std::nullopt, |
| 296 | /*Must be empty: additional_headers=*/net::HttpRequestHeaders(), |
| 297 | ttl.has_value() ? ttl.value() |
| 298 | : PrefetchContainerDefaultTtlInPrefetchService(), |
| 299 | std::move(holdback_status_override), |
| 300 | /*should_append_variations_header=*/true, |
| 301 | /*should_disable_block_until_head_timeout=*/false, |
| 302 | PrefetchBrowserInitiatorInfo(embedder_histogram_suffix, |
| 303 | /*request_status_listener=*/nullptr))) {} |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 304 | |
| 305 | PrefetchContainer::PrefetchContainer( |
Wayne Jackson Jr. | 5b1dc04 | 2024-09-09 12:53:10 | [diff] [blame] | 306 | BrowserContext* browser_context, |
| 307 | const GURL& url, |
| 308 | const PrefetchType& prefetch_type, |
Taiyo Mizuhashi | 49959d0 | 2025-04-22 16:07:54 | [diff] [blame] | 309 | const std::string& embedder_histogram_suffix, |
Wayne Jackson Jr. | 5b1dc04 | 2024-09-09 12:53:10 | [diff] [blame] | 310 | const blink::mojom::Referrer& referrer, |
| 311 | bool javascript_enabled, |
| 312 | const std::optional<url::Origin>& referring_origin, |
| 313 | std::optional<net::HttpNoVarySearchData> no_vary_search_hint, |
Taiyo Mizuhashi | d7c85699 | 2025-06-23 08:56:02 | [diff] [blame] | 314 | std::optional<PrefetchPriority> priority, |
Wayne Jackson Jr. | 03a15fa6 | 2024-09-16 14:42:15 | [diff] [blame] | 315 | base::WeakPtr<PreloadingAttempt> attempt, |
Wayne Jackson Jr. | 1b732247 | 2024-10-29 13:08:51 | [diff] [blame] | 316 | const net::HttpRequestHeaders& additional_headers, |
elabadysayed | f7d6b00f | 2025-02-05 11:27:00 | [diff] [blame] | 317 | std::unique_ptr<PrefetchRequestStatusListener> request_status_listener, |
kenoss | 70bfbfe | 2025-06-10 08:04:42 | [diff] [blame] | 318 | base::TimeDelta ttl, |
Taiyo Mizuhashi | cd08a8f | 2025-06-10 17:27:32 | [diff] [blame] | 319 | bool should_append_variations_header, |
| 320 | bool should_disable_block_until_head_timeout) |
Hiroshige Hayashizaki | ce441325 | 2025-08-19 19:05:45 | [diff] [blame] | 321 | : PrefetchContainer(std::make_unique<PrefetchRequest>( |
| 322 | prefetch_type, |
| 323 | PrefetchKey(std::optional<blink::DocumentToken>(std::nullopt), url), |
| 324 | std::move(no_vary_search_hint), |
| 325 | std::move(priority), |
| 326 | PreloadPipelineInfo::Create( |
| 327 | /*planned_max_preloading_type=*/PreloadingType::kPrefetch), |
| 328 | std::move(attempt), |
| 329 | javascript_enabled, |
| 330 | referrer, |
| 331 | referring_origin, |
| 332 | browser_context->GetWeakPtr(), |
| 333 | /*speculation_rules_tags=*/std::nullopt, |
| 334 | additional_headers, |
| 335 | ttl, |
| 336 | /*holdback_status_override=*/std::nullopt, |
| 337 | should_append_variations_header, |
| 338 | should_disable_block_until_head_timeout, |
| 339 | PrefetchBrowserInitiatorInfo(embedder_histogram_suffix, |
| 340 | std::move(request_status_listener)))) {} |
Wayne Jackson Jr. | 5b1dc04 | 2024-09-09 12:53:10 | [diff] [blame] | 341 | |
Hiroshige Hayashizaki | ce441325 | 2025-08-19 19:05:45 | [diff] [blame] | 342 | PrefetchContainer::PrefetchContainer(std::unique_ptr<PrefetchRequest> request) |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 343 | : request_(std::move(request)), |
Hiroshige Hayashizaki | ff0133db | 2025-08-19 19:03:10 | [diff] [blame] | 344 | referrer_(request_->initial_referrer()), |
Hiroshige Hayashizaki | ce441325 | 2025-08-19 19:05:45 | [diff] [blame] | 345 | request_id_(base::UnguessableToken::Create().ToString()) { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 346 | CHECK(request_); |
| 347 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 348 | is_likely_ahead_of_prerender_ = |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 349 | CalculateIsLikelyAheadOfPrerender(request_->preload_pipeline_info()); |
kenoss | aa7dbe7 | 2024-10-17 11:20:03 | [diff] [blame] | 350 | |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 351 | redirect_chain_.push_back(std::make_unique<PrefetchSingleRedirectHop>( |
Hiroshige Hayashizaki | 40270b7 | 2025-07-23 01:52:01 | [diff] [blame] | 352 | *this, GetURL(), IsCrossSiteRequest(url::Origin::Create(GetURL())))); |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 353 | |
| 354 | // Disallow prefetching ServiceWorker-controlled responses for isolated |
| 355 | // network contexts. |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 356 | if (!features::IsPrefetchServiceWorkerEnabled(request_->browser_context()) || |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 357 | IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
| 358 | service_worker_state_ = PrefetchServiceWorkerState::kDisallowed; |
| 359 | } |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 360 | } |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 361 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 362 | PrefetchContainer::~PrefetchContainer() { |
kenoss | 6c5fb09 | 2024-07-05 05:42:14 | [diff] [blame] | 363 | is_in_dtor_ = true; |
| 364 | |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 365 | // Ideally, this method should be called just before dtor. |
| 366 | // https://chromium-review.googlesource.com/c/chromium/src/+/5657659/comments/0cfb14c0_3050963e |
| 367 | // |
| 368 | // TODO(crbug.com/356314759): Do it. |
kenoss | c6c712a | 2025-03-10 23:17:02 | [diff] [blame] | 369 | OnWillBeDestroyed(); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 370 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 371 | CancelStreamingURLLoaderIfNotServing(); |
| 372 | |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 373 | MaybeRecordPrefetchStatusToUMA( |
| 374 | prefetch_status_.value_or(PrefetchStatus::kPrefetchNotStarted)); |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 375 | RecordPrefetchDurationHistogram(); |
Taiyo Mizuhashi | a009caf | 2025-08-05 07:34:22 | [diff] [blame] | 376 | RecordPrefetchContainerServedCountHistogram(); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 377 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 378 | ukm::SourceId ukm_source_id = ukm::kInvalidSourceId; |
| 379 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 380 | ukm_source_id = renderer_initiator_info->ukm_source_id(); |
| 381 | } |
| 382 | ukm::builders::PrefetchProxy_PrefetchedResource builder(ukm_source_id); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 383 | builder.SetResourceType(/*mainframe*/ 1); |
| 384 | builder.SetStatus(static_cast<int>( |
| 385 | prefetch_status_.value_or(PrefetchStatus::kPrefetchNotStarted))); |
Taiyo Mizuhashi | a009caf | 2025-08-05 07:34:22 | [diff] [blame] | 386 | builder.SetLinkClicked(served_count_ > 0); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 387 | |
Hiroshige Hayashizaki | 57872f3 | 2025-07-09 11:52:02 | [diff] [blame] | 388 | if (GetNonRedirectResponseReader()) { |
| 389 | GetNonRedirectResponseReader()->RecordOnPrefetchContainerDestroyed( |
| 390 | base::PassKey<PrefetchContainer>(), builder); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | if (probe_result_) { |
| 394 | builder.SetISPFilteringStatus(static_cast<int>(probe_result_.value())); |
| 395 | } |
| 396 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 397 | // TODO(crbug.com/40215782): Get the navigation start time and set the |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 398 | // NavigationStartToFetchStartMs field of the PrefetchProxy.PrefetchedResource |
| 399 | // UKM event. |
| 400 | |
| 401 | builder.Record(ukm::UkmRecorder::Get()); |
Adithya Srinivasan | d476f4f8 | 2023-06-20 15:55:13 | [diff] [blame] | 402 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 403 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 404 | if (renderer_initiator_info->prefetch_document_manager()) { |
| 405 | renderer_initiator_info->prefetch_document_manager() |
| 406 | ->PrefetchWillBeDestroyed(this); |
| 407 | } |
Adithya Srinivasan | d476f4f8 | 2023-06-20 15:55:13 | [diff] [blame] | 408 | } |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | void PrefetchContainer::OnWillBeDestroyed() { |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 412 | for (auto& observer : observers_) { |
| 413 | observer.OnWillBeDestroyed(*this); |
| 414 | } |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 415 | } |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 416 | |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 417 | PrefetchServingHandle PrefetchContainer::CreateServingHandle() { |
| 418 | return PrefetchServingHandle(GetWeakPtr(), 0); |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 419 | } |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 420 | |
Hiroshige Hayashizaki | 386c502 | 2025-08-12 14:43:37 | [diff] [blame] | 421 | const std::vector<std::unique_ptr<PrefetchSingleRedirectHop>>& |
| 422 | PrefetchContainer::redirect_chain(base::PassKey<PrefetchServingHandle>) const { |
| 423 | return redirect_chain_; |
| 424 | } |
| 425 | |
| 426 | void PrefetchContainer::SetProbeResult(base::PassKey<PrefetchServingHandle>, |
| 427 | PrefetchProbeResult probe_result) { |
| 428 | probe_result_ = probe_result; |
| 429 | } |
| 430 | |
| 431 | std::optional<PreloadingTriggeringOutcome> |
| 432 | PrefetchContainer::TriggeringOutcomeFromStatusForServingHandle( |
| 433 | base::PassKey<PrefetchServingHandle>, |
| 434 | PrefetchStatus prefetch_status) { |
| 435 | return TriggeringOutcomeFromStatus(prefetch_status); |
| 436 | } |
| 437 | |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 438 | // Please follow go/preloading-dashboard-updates if a new outcome enum or a |
| 439 | // failure reason enum is added. |
| 440 | void PrefetchContainer::SetTriggeringOutcomeAndFailureReasonFromStatus( |
| 441 | PrefetchStatus new_prefetch_status) { |
| 442 | std::optional<PrefetchStatus> old_prefetch_status = prefetch_status_; |
| 443 | if (old_prefetch_status && |
| 444 | old_prefetch_status.value() == PrefetchStatus::kPrefetchResponseUsed) { |
| 445 | // Skip this update if the triggering outcome has already been updated |
| 446 | // to kSuccess. |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | if (old_prefetch_status && |
Taiyo Mizuhashi | 4e69317 | 2025-03-26 12:07:50 | [diff] [blame] | 451 | (TriggeringOutcomeFromStatus(old_prefetch_status.value()) == |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 452 | PreloadingTriggeringOutcome::kFailure)) { |
| 453 | if (StatusUpdateIsPossibleAfterFailure(new_prefetch_status)) { |
| 454 | // Note that `StatusUpdateIsPossibleAfterFailure()` implies that |
Taiyo Mizuhashi | 4e69317 | 2025-03-26 12:07:50 | [diff] [blame] | 455 | // the new status is a failure. |
kenoss | 081c9d9 | 2024-11-13 10:21:57 | [diff] [blame] | 456 | CHECK(TriggeringOutcomeFromStatus(new_prefetch_status) == |
| 457 | PreloadingTriggeringOutcome::kFailure); |
Taiyo Mizuhashi | 4e69317 | 2025-03-26 12:07:50 | [diff] [blame] | 458 | |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 459 | // Skip this update since if the triggering outcome has already been |
| 460 | // updated to kFailure, we don't need to overwrite it. |
kenoss | 081c9d9 | 2024-11-13 10:21:57 | [diff] [blame] | 461 | return; |
| 462 | } else { |
| 463 | SCOPED_CRASH_KEY_NUMBER("PrefetchContainer", "prefetch_status_from", |
| 464 | static_cast<int>(old_prefetch_status.value())); |
| 465 | SCOPED_CRASH_KEY_NUMBER("PrefetchContainer", "prefetch_status_to", |
| 466 | static_cast<int>(new_prefetch_status)); |
| 467 | NOTREACHED() |
| 468 | << "PrefetchStatus illegal transition: (old_prefetch_status, " |
| 469 | "new_prefetch_status) = (" |
| 470 | << static_cast<int>(old_prefetch_status.value()) << ", " |
| 471 | << static_cast<int>(new_prefetch_status) << ")"; |
| 472 | } |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | // We record the prefetch status to UMA if it's a failure, or if the prefetch |
| 476 | // response is being used. For other statuses, there may be more updates in |
| 477 | // the future, so we only record them in the destructor. |
| 478 | // Note: The prefetch may have an updated failure status in the future |
| 479 | // (for example: if the triggering speculation candidate for a failed prefetch |
| 480 | // is removed), but the original failure is more pertinent for metrics |
| 481 | // purposes. |
| 482 | if (TriggeringOutcomeFromStatus(new_prefetch_status) == |
| 483 | PreloadingTriggeringOutcome::kFailure || |
| 484 | new_prefetch_status == PrefetchStatus::kPrefetchResponseUsed) { |
| 485 | MaybeRecordPrefetchStatusToUMA(new_prefetch_status); |
| 486 | } |
| 487 | |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 488 | if (request().attempt()) { |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 489 | switch (new_prefetch_status) { |
| 490 | case PrefetchStatus::kPrefetchNotFinishedInTime: |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 491 | request().attempt()->SetTriggeringOutcome( |
| 492 | PreloadingTriggeringOutcome::kRunning); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 493 | break; |
| 494 | case PrefetchStatus::kPrefetchSuccessful: |
| 495 | // A successful prefetch means the response is ready to be used for the |
| 496 | // next navigation. |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 497 | request().attempt()->SetTriggeringOutcome( |
| 498 | PreloadingTriggeringOutcome::kReady); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 499 | break; |
| 500 | case PrefetchStatus::kPrefetchResponseUsed: |
| 501 | if (old_prefetch_status && old_prefetch_status.value() != |
| 502 | PrefetchStatus::kPrefetchSuccessful) { |
| 503 | // If the new prefetch status is |kPrefetchResponseUsed| or |
| 504 | // |kPrefetchUsedNoProbe| but the previous status is not |
| 505 | // |kPrefetchSuccessful|, then temporarily update the triggering |
| 506 | // outcome to |kReady| to ensure valid triggering outcome state |
| 507 | // transitions. This can occur in cases where the prefetch is served |
| 508 | // before the body is fully received. |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 509 | request().attempt()->SetTriggeringOutcome( |
| 510 | PreloadingTriggeringOutcome::kReady); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 511 | } |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 512 | request().attempt()->SetTriggeringOutcome( |
| 513 | PreloadingTriggeringOutcome::kSuccess); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 514 | break; |
| 515 | // A decoy is considered eligible because a network request is made for |
| 516 | // it. It is considered as a failure as the final response is never |
| 517 | // served. |
| 518 | case PrefetchStatus::kPrefetchIsPrivacyDecoy: |
| 519 | case PrefetchStatus::kPrefetchFailedNetError: |
| 520 | case PrefetchStatus::kPrefetchFailedNon2XX: |
| 521 | case PrefetchStatus::kPrefetchFailedMIMENotSupported: |
| 522 | case PrefetchStatus::kPrefetchFailedInvalidRedirect: |
| 523 | case PrefetchStatus::kPrefetchFailedIneligibleRedirect: |
| 524 | case PrefetchStatus::kPrefetchNotUsedProbeFailed: |
| 525 | case PrefetchStatus::kPrefetchNotUsedCookiesChanged: |
| 526 | // TODO(adithyas): This would report 'eviction' as a failure even though |
| 527 | // the initial prefetch succeeded, consider introducing a different |
| 528 | // PreloadingTriggerOutcome for eviction. |
| 529 | case PrefetchStatus::kPrefetchEvictedAfterCandidateRemoved: |
| 530 | case PrefetchStatus::kPrefetchEvictedForNewerPrefetch: |
| 531 | case PrefetchStatus::kPrefetchIsStale: |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 532 | case PrefetchStatus::kPrefetchEvictedAfterBrowsingDataRemoved: |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 533 | request().attempt()->SetFailureReason( |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 534 | ToPreloadingFailureReason(new_prefetch_status)); |
| 535 | break; |
| 536 | case PrefetchStatus::kPrefetchHeldback: |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 537 | case PrefetchStatus::kPrefetchNotStarted: |
| 538 | // `kPrefetchNotStarted` is set in |
Hiroshige Hayashizaki | f1f8b360 | 2025-01-15 11:50:49 | [diff] [blame] | 539 | // `PrefetchService::OnGotEligibilityForNonRedirect()` when the |
| 540 | // container is pushed onto the prefetch queue, which occurs before the |
| 541 | // holdback status is determined in |
| 542 | // `PrefetchService::StartSinglePrefetch`. After the container is queued |
| 543 | // and before it is sent for prefetch, the only status change is when |
| 544 | // the container is popped from the queue but heldback. This is covered |
| 545 | // by attempt's holdback status. For these two reasons this |
| 546 | // PrefetchStatus does not fire a `SetTriggeringOutcome`. |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 547 | break; |
| 548 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorker: |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 549 | case PrefetchStatus:: |
| 550 | kPrefetchIneligibleUserHasServiceWorkerNoFetchHandler: |
| 551 | case PrefetchStatus::kPrefetchIneligibleRedirectFromServiceWorker: |
| 552 | case PrefetchStatus::kPrefetchIneligibleRedirectToServiceWorker: |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 553 | case PrefetchStatus::kPrefetchIneligibleSchemeIsNotHttps: |
| 554 | case PrefetchStatus::kPrefetchIneligibleNonDefaultStoragePartition: |
| 555 | case PrefetchStatus::kPrefetchIneligibleHostIsNonUnique: |
| 556 | case PrefetchStatus::kPrefetchIneligibleDataSaverEnabled: |
| 557 | case PrefetchStatus::kPrefetchIneligibleBatterySaverEnabled: |
| 558 | case PrefetchStatus::kPrefetchIneligiblePreloadingDisabled: |
| 559 | case PrefetchStatus::kPrefetchIneligibleExistingProxy: |
| 560 | case PrefetchStatus::kPrefetchIneligibleUserHasCookies: |
| 561 | case PrefetchStatus::kPrefetchIneligibleRetryAfter: |
| 562 | case PrefetchStatus::kPrefetchIneligiblePrefetchProxyNotAvailable: |
| 563 | case PrefetchStatus:: |
| 564 | kPrefetchIneligibleSameSiteCrossOriginPrefetchRequiredProxy: |
| 565 | NOTIMPLEMENTED(); |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 570 | void PrefetchContainer::SetPrefetchStatusWithoutUpdatingTriggeringOutcome( |
| 571 | PrefetchStatus prefetch_status) { |
| 572 | prefetch_status_ = prefetch_status; |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 573 | request().preload_pipeline_info().SetPrefetchStatus(prefetch_status); |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 574 | for (auto& preload_pipeline_info : inherited_preload_pipeline_infos_) { |
| 575 | preload_pipeline_info->SetPrefetchStatus(prefetch_status); |
| 576 | } |
Robert Lin | 1f778f89 | 2023-05-12 13:51:44 | [diff] [blame] | 577 | |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 578 | // Currently DevTools only supports when the prefetch is initiated by |
| 579 | // renderer. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 580 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 581 | std::optional<PreloadingTriggeringOutcome> preloading_trigger_outcome = |
| 582 | TriggeringOutcomeFromStatus(prefetch_status); |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 583 | if (renderer_initiator_info->devtools_navigation_token().has_value() && |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 584 | preloading_trigger_outcome.has_value()) { |
| 585 | devtools_instrumentation::DidUpdatePrefetchStatus( |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 586 | FrameTreeNode::From(renderer_initiator_info->GetRenderFrameHost()), |
| 587 | renderer_initiator_info->devtools_navigation_token().value(), |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 588 | GetURL(), request().preload_pipeline_info().id(), |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 589 | preloading_trigger_outcome.value(), prefetch_status, RequestId()); |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 590 | } |
Robert Lin | 1f778f89 | 2023-05-12 13:51:44 | [diff] [blame] | 591 | } |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 592 | } |
| 593 | |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 594 | void PrefetchContainer::SetPrefetchStatus(PrefetchStatus prefetch_status) { |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 595 | // The concept of `PreloadingAttempt`'s `PreloadingTriggeringOutcome` is to |
| 596 | // record the outcomes of started triggers. Therefore, this should |
| 597 | // only be called once prefetching has actually started, and not for |
| 598 | // ineligible or eligibled but not started triggers (e.g., holdback triggers, |
| 599 | // triggers waiting on a queue). |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 600 | switch (GetLoadState()) { |
| 601 | case LoadState::kStarted: |
| 602 | case LoadState::kDeterminedHead: |
| 603 | case LoadState::kCompletedOrFailed: |
| 604 | SetTriggeringOutcomeAndFailureReasonFromStatus(prefetch_status); |
| 605 | break; |
| 606 | case LoadState::kNotStarted: |
| 607 | case LoadState::kEligible: |
| 608 | case LoadState::kFailedIneligible: |
| 609 | case LoadState::kFailedHeldback: |
| 610 | break; |
Taiyo Mizuhashi | 070baec | 2025-03-28 15:37:00 | [diff] [blame] | 611 | } |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 612 | SetPrefetchStatusWithoutUpdatingTriggeringOutcome(prefetch_status); |
| 613 | } |
| 614 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 615 | PrefetchStatus PrefetchContainer::GetPrefetchStatus() const { |
| 616 | DCHECK(prefetch_status_); |
| 617 | return prefetch_status_.value(); |
| 618 | } |
| 619 | |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 620 | void PrefetchContainer::TakeProxyLookupClient( |
| 621 | std::unique_ptr<ProxyLookupClientImpl> proxy_lookup_client) { |
| 622 | DCHECK(!proxy_lookup_client_); |
| 623 | proxy_lookup_client_ = std::move(proxy_lookup_client); |
| 624 | } |
| 625 | |
| 626 | std::unique_ptr<ProxyLookupClientImpl> |
| 627 | PrefetchContainer::ReleaseProxyLookupClient() { |
| 628 | DCHECK(proxy_lookup_client_); |
| 629 | return std::move(proxy_lookup_client_); |
| 630 | } |
| 631 | |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 632 | PrefetchNetworkContext* |
Kouhei Ueno | 4442db9 | 2023-11-13 06:38:13 | [diff] [blame] | 633 | PrefetchContainer::GetOrCreateNetworkContextForCurrentPrefetch() { |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 634 | bool is_isolated_network_context_required = |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 635 | IsIsolatedNetworkContextRequiredForCurrentPrefetch(); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 636 | |
Hiroshige Hayashizaki | d44edd4e | 2025-08-12 07:35:46 | [diff] [blame] | 637 | PrefetchNetworkContext* network_context = |
| 638 | GetNetworkContext(is_isolated_network_context_required); |
| 639 | if (network_context) { |
| 640 | return network_context; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 641 | } |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 642 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 643 | GlobalRenderFrameHostId referring_render_frame_host_id; |
| 644 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 645 | referring_render_frame_host_id = |
| 646 | renderer_initiator_info->GetRenderFrameHostId(); |
| 647 | } |
| 648 | |
Hiroshige Hayashizaki | d44edd4e | 2025-08-12 07:35:46 | [diff] [blame] | 649 | auto owned_network_context = std::make_unique<PrefetchNetworkContext>( |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 650 | is_isolated_network_context_required, request().prefetch_type(), |
Hiroshige Hayashizaki | 65285e3 | 2025-08-19 17:24:20 | [diff] [blame] | 651 | referring_render_frame_host_id, request().referring_origin()); |
Hiroshige Hayashizaki | d44edd4e | 2025-08-12 07:35:46 | [diff] [blame] | 652 | network_context = owned_network_context.get(); |
| 653 | network_contexts_.emplace(is_isolated_network_context_required, |
| 654 | std::move(owned_network_context)); |
| 655 | |
| 656 | return network_context; |
| 657 | } |
| 658 | |
| 659 | PrefetchNetworkContext* PrefetchContainer::GetNetworkContext( |
| 660 | bool is_isolated_network_context_required) const { |
| 661 | const auto network_context_itr = |
| 662 | network_contexts_.find(is_isolated_network_context_required); |
| 663 | if (network_context_itr == network_contexts_.end()) { |
| 664 | return nullptr; |
| 665 | } |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 666 | return network_context_itr->second.get(); |
| 667 | } |
| 668 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 669 | void PrefetchContainer::CloseIdleConnections() { |
| 670 | for (const auto& network_context_itr : network_contexts_) { |
| 671 | CHECK(network_context_itr.second); |
| 672 | network_context_itr.second->CloseIdleConnections(); |
| 673 | } |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 674 | } |
| 675 | |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 676 | void PrefetchContainer::SetLoadState(LoadState new_load_state) { |
| 677 | switch (new_load_state) { |
| 678 | case LoadState::kNotStarted: |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 679 | NOTREACHED(); |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 680 | |
| 681 | case LoadState::kEligible: |
| 682 | case LoadState::kFailedIneligible: |
| 683 | CHECK_EQ(load_state_, LoadState::kNotStarted); |
| 684 | break; |
| 685 | |
| 686 | case LoadState::kStarted: |
| 687 | case LoadState::kFailedHeldback: |
| 688 | CHECK_EQ(load_state_, LoadState::kEligible); |
| 689 | break; |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 690 | |
| 691 | case LoadState::kDeterminedHead: |
| 692 | CHECK_EQ(load_state_, LoadState::kStarted); |
| 693 | break; |
| 694 | |
| 695 | case LoadState::kCompletedOrFailed: |
| 696 | CHECK_EQ(load_state_, LoadState::kDeterminedHead); |
| 697 | break; |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 698 | } |
Kouhei Ueno | 929ee8e1 | 2024-07-05 03:09:56 | [diff] [blame] | 699 | DVLOG(1) << (*this) << " LoadState " << load_state_ << " -> " |
| 700 | << new_load_state; |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 701 | load_state_ = new_load_state; |
| 702 | } |
| 703 | |
| 704 | PrefetchContainer::LoadState PrefetchContainer::GetLoadState() const { |
| 705 | return load_state_; |
| 706 | } |
| 707 | |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 708 | void PrefetchContainer::OnAddedToPrefetchService() { |
| 709 | time_added_to_prefetch_service_ = base::TimeTicks::Now(); |
| 710 | } |
| 711 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 712 | void PrefetchContainer::OnEligibilityCheckComplete( |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 713 | PreloadingEligibility eligibility) { |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 714 | request().preload_pipeline_info().SetPrefetchEligibility(eligibility); |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 715 | for (auto& preload_pipeline_info : inherited_preload_pipeline_infos_) { |
| 716 | preload_pipeline_info->SetPrefetchEligibility(eligibility); |
| 717 | } |
| 718 | |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 719 | bool is_eligible = eligibility == PreloadingEligibility::kEligible; |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 720 | |
Hiroshige Hayashizaki | 079949e | 2023-05-26 01:05:23 | [diff] [blame] | 721 | if (redirect_chain_.size() == 1) { |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 722 | // This case is for just the URL that was originally requested to be |
| 723 | // prefetched. |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 724 | if (is_eligible) { |
| 725 | SetLoadState(LoadState::kEligible); |
| 726 | } else { |
| 727 | SetLoadState(LoadState::kFailedIneligible); |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 728 | PrefetchStatus new_prefetch_status = |
| 729 | PrefetchStatusFromIneligibleReason(eligibility); |
| 730 | MaybeRecordPrefetchStatusToUMA(new_prefetch_status); |
| 731 | SetPrefetchStatusWithoutUpdatingTriggeringOutcome(new_prefetch_status); |
Wayne Jackson Jr. | 00e3e8b | 2024-09-25 12:35:56 | [diff] [blame] | 732 | OnInitialPrefetchFailedIneligible(eligibility); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 733 | } |
| 734 | |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 735 | if (request().attempt()) { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 736 | // Please follow go/preloading-dashboard-updates if a new eligibility is |
| 737 | // added. |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 738 | request().attempt()->SetEligibility(eligibility); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 739 | } |
| 740 | |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 741 | time_initial_eligibility_got_ = base::TimeTicks::Now(); |
| 742 | |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 743 | // Recording an eligiblity for PrefetchReferringPageMetrics. |
| 744 | // TODO(crbug.com/40946257): Current code doesn't support |
| 745 | // PrefetchReferringPageMetrics when the prefetch is initiated by browser. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 746 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 747 | if (renderer_initiator_info->prefetch_document_manager()) { |
| 748 | renderer_initiator_info->prefetch_document_manager() |
| 749 | ->OnEligibilityCheckComplete(is_eligible); |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 750 | } |
Max Curran | b9c7bba | 2023-03-28 19:31:23 | [diff] [blame] | 751 | } |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 752 | |
| 753 | for (auto& observer : observers_) { |
| 754 | observer.OnGotInitialEligibility(*this, eligibility); |
| 755 | } |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 756 | } else { |
| 757 | // This case is for any URLs from redirects. |
| 758 | if (!is_eligible) { |
| 759 | SetPrefetchStatus(PrefetchStatus::kPrefetchFailedIneligibleRedirect); |
| 760 | } |
| 761 | } |
| 762 | } |
| 763 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 764 | void PrefetchContainer::AddRedirectHop(const net::RedirectInfo& redirect_info) { |
| 765 | CHECK(resource_request_); |
| 766 | |
| 767 | // There are sometimes other headers that are modified during navigation |
| 768 | // redirects; see |NavigationRequest::OnRedirectChecksComplete| (including |
| 769 | // some which are added by throttles). These aren't yet supported for |
| 770 | // prefetch, including browsing topics and client hints. |
| 771 | net::HttpRequestHeaders updated_headers; |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 772 | std::vector<std::string> headers_to_remove = {variations::kClientDataHeader}; |
David Risney | a1dd2bf | 2025-03-06 03:40:42 | [diff] [blame] | 773 | updated_headers.SetHeader(blink::kSecPurposeHeaderName, |
kenoss | 5df53e05 | 2024-06-17 06:59:52 | [diff] [blame] | 774 | GetSecPurposeHeaderValue(redirect_info.new_url)); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 775 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 776 | // Remove any existing client hints headers (except, below, if we still want |
| 777 | // to send this particular hint). |
| 778 | if (base::FeatureList::IsEnabled(features::kPrefetchClientHints)) { |
| 779 | const auto& client_hints = network::GetClientHintToNameMap(); |
| 780 | headers_to_remove.reserve(headers_to_remove.size() + client_hints.size()); |
| 781 | for (const auto& [_, header] : client_hints) { |
| 782 | headers_to_remove.push_back(header); |
| 783 | } |
| 784 | } |
| 785 | |
HuanPo Lin | d8887e3 | 2025-04-17 04:08:14 | [diff] [blame] | 786 | // Sec-Speculation-Tags is set only when the prefetch is triggered |
| 787 | // by speculation rules and it is not cross-site prefetch redirection. |
| 788 | // To see more details: |
| 789 | // https://github.com/WICG/nav-speculation/blob/main/speculation-rules-tags.md#the-cross-site-case |
| 790 | headers_to_remove.push_back(blink::kSecSpeculationTagsHeaderName); |
Hiroshige Hayashizaki | 995741b | 2025-08-19 18:41:13 | [diff] [blame] | 791 | if (request().speculation_rules_tags().has_value() && |
HuanPo Lin | d8887e3 | 2025-04-17 04:08:14 | [diff] [blame] | 792 | !IsCrossSiteRequest(url::Origin::Create(redirect_info.new_url))) { |
HuanPo Lin | d8887e3 | 2025-04-17 04:08:14 | [diff] [blame] | 793 | std::optional<std::string> serialized_list = |
Hiroshige Hayashizaki | 995741b | 2025-08-19 18:41:13 | [diff] [blame] | 794 | request().speculation_rules_tags()->ConvertStringToHeaderString(); |
HuanPo Lin | d8887e3 | 2025-04-17 04:08:14 | [diff] [blame] | 795 | CHECK(serialized_list.has_value()); |
| 796 | updated_headers.SetHeader(blink::kSecSpeculationTagsHeaderName, |
| 797 | serialized_list.value()); |
| 798 | } |
| 799 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 800 | // Then add the client hints that are appropriate for the redirect. |
| 801 | AddClientHintsHeaders(url::Origin::Create(redirect_info.new_url), |
| 802 | &updated_headers); |
| 803 | |
| 804 | // To avoid spurious reordering, don't remove headers that will be updated |
| 805 | // anyway. |
Andrew Rayskiy | f6599036 | 2024-02-27 18:43:24 | [diff] [blame] | 806 | std::erase_if(headers_to_remove, [&](const std::string& header) { |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 807 | return updated_headers.HasHeader(header); |
| 808 | }); |
| 809 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 810 | // TODO(jbroman): We have several places that invoke |
| 811 | // `net::RedirectUtil::UpdateHttpRequest` and then need to do very similar |
| 812 | // work afterward. Ideally we would deduplicate these more. |
| 813 | bool should_clear_upload = false; |
| 814 | net::RedirectUtil::UpdateHttpRequest( |
| 815 | resource_request_->url, resource_request_->method, redirect_info, |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 816 | std::move(headers_to_remove), std::move(updated_headers), |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 817 | &resource_request_->headers, &should_clear_upload); |
| 818 | CHECK(!should_clear_upload); |
| 819 | |
| 820 | resource_request_->url = redirect_info.new_url; |
| 821 | resource_request_->method = redirect_info.new_method; |
| 822 | resource_request_->site_for_cookies = redirect_info.new_site_for_cookies; |
| 823 | |
| 824 | resource_request_->trusted_params->isolation_info = |
| 825 | resource_request_->trusted_params->isolation_info.CreateForRedirect( |
| 826 | url::Origin::Create(resource_request_->url)); |
| 827 | |
| 828 | // TODO(jbroman): This somewhat duplicates |referrer_|. Revisit usage of that |
| 829 | // (and related data members) to see if they can/should use this data instead. |
| 830 | resource_request_->referrer = GURL(redirect_info.new_referrer); |
| 831 | resource_request_->referrer_policy = redirect_info.new_referrer_policy; |
| 832 | |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 833 | AddXClientDataHeader(*resource_request_.get()); |
| 834 | |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 835 | redirect_chain_.push_back(std::make_unique<PrefetchSingleRedirectHop>( |
Hiroshige Hayashizaki | 40270b7 | 2025-07-23 01:52:01 | [diff] [blame] | 836 | *this, redirect_info.new_url, |
kenoss | dc902477 | 2025-07-01 10:28:38 | [diff] [blame] | 837 | IsCrossSiteRequest(url::Origin::Create(redirect_info.new_url)))); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 838 | } |
| 839 | |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 840 | bool PrefetchContainer::IsCrossSiteRequest(const url::Origin& origin) const { |
Hiroshige Hayashizaki | 65285e3 | 2025-08-19 17:24:20 | [diff] [blame] | 841 | return request().referring_origin().has_value() && |
| 842 | !net::SchemefulSite::IsSameSite(request().referring_origin().value(), |
| 843 | origin); |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | bool PrefetchContainer::IsCrossOriginRequest(const url::Origin& origin) const { |
Hiroshige Hayashizaki | 65285e3 | 2025-08-19 17:24:20 | [diff] [blame] | 847 | return request().referring_origin().has_value() && |
| 848 | !request().referring_origin().value().IsSameOriginWith(origin); |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 849 | } |
| 850 | |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 851 | void PrefetchContainer::MarkCrossSiteContaminated() { |
| 852 | is_cross_site_contaminated_ = true; |
| 853 | } |
| 854 | |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 855 | void PrefetchContainer::AddXClientDataHeader( |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 856 | network::ResourceRequest& resource_request) { |
| 857 | if (request().browser_context()) { |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 858 | // Add X-Client-Data header with experiment IDs from field trials. |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 859 | variations::AppendVariationsHeader( |
| 860 | resource_request.url, |
| 861 | request().browser_context()->IsOffTheRecord() |
| 862 | ? variations::InIncognito::kYes |
| 863 | : variations::InIncognito::kNo, |
| 864 | variations::SignedIn::kNo, &resource_request); |
Liviu Tinta | 2e1ffe2 | 2024-06-21 21:01:33 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 868 | void PrefetchContainer::RegisterCookieListener( |
| 869 | network::mojom::CookieManager* cookie_manager) { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 870 | PrefetchSingleRedirectHop& this_prefetch = |
| 871 | GetCurrentSingleRedirectHopToPrefetch(); |
Hiroshige Hayashizaki | eec97ed1 | 2023-05-26 06:38:32 | [diff] [blame] | 872 | this_prefetch.cookie_listener_ = PrefetchCookieListener::MakeAndRegister( |
| 873 | this_prefetch.url_, cookie_manager); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 874 | } |
| 875 | |
Taiyo Mizuhashi | 8b5ddcc | 2025-02-21 22:12:12 | [diff] [blame] | 876 | void PrefetchContainer::PauseAllCookieListeners() { |
| 877 | // TODO(crbug.com/377440445): Consider whether we actually need to |
| 878 | // pause/resume all single prefetch's cookie listener during each single |
| 879 | // prefetch's isolated cookie copy. |
Max Curran | 7dbdea4d | 2023-03-21 23:47:37 | [diff] [blame] | 880 | for (const auto& single_prefetch : redirect_chain_) { |
| 881 | if (single_prefetch->cookie_listener_) { |
Taiyo Mizuhashi | 8b5ddcc | 2025-02-21 22:12:12 | [diff] [blame] | 882 | single_prefetch->cookie_listener_->PauseListening(); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | void PrefetchContainer::ResumeAllCookieListeners() { |
| 888 | // TODO(crbug.com/377440445): Consider whether we actually need to |
| 889 | // pause/resume all single prefetch's cookie listener during each single |
| 890 | // prefetch's isolated cookie copy. |
| 891 | for (const auto& single_prefetch : redirect_chain_) { |
| 892 | if (single_prefetch->cookie_listener_) { |
| 893 | single_prefetch->cookie_listener_->ResumeListening(); |
Max Curran | 7dbdea4d | 2023-03-21 23:47:37 | [diff] [blame] | 894 | } |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 895 | } |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 896 | } |
| 897 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 898 | void PrefetchContainer::SetStreamingURLLoader( |
| 899 | base::WeakPtr<PrefetchStreamingURLLoader> streaming_loader) { |
| 900 | // The previous streaming loader (if any) should be already deleted or to be |
| 901 | // deleted soon when the new `streaming_loader` is set here. |
| 902 | CHECK(!streaming_loader_ || streaming_loader_->IsDeletionScheduledForCHECK()); |
| 903 | |
| 904 | streaming_loader_ = std::move(streaming_loader); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 905 | } |
| 906 | |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 907 | base::WeakPtr<PrefetchStreamingURLLoader> |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 908 | PrefetchContainer::GetStreamingURLLoader() const { |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 909 | // Streaming loaders already deleted or scheduled to be deleted shouldn't be |
| 910 | // used. |
| 911 | if (!streaming_loader_ || streaming_loader_->IsDeletionScheduledForCHECK()) { |
| 912 | return nullptr; |
| 913 | } |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 914 | return streaming_loader_; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 915 | } |
| 916 | |
Hiroshige Hayashizaki | 5c50ec5 | 2023-09-13 00:35:49 | [diff] [blame] | 917 | bool PrefetchContainer::IsStreamingURLLoaderDeletionScheduledForTesting() |
| 918 | const { |
| 919 | return streaming_loader_ && streaming_loader_->IsDeletionScheduledForCHECK(); |
| 920 | } |
| 921 | |
Hiroshige Hayashizaki | 334d0fa | 2023-08-16 23:33:27 | [diff] [blame] | 922 | const PrefetchResponseReader* PrefetchContainer::GetNonRedirectResponseReader() |
| 923 | const { |
Hiroshige Hayashizaki | b3e51a5 | 2025-01-18 00:17:21 | [diff] [blame] | 924 | CHECK(!redirect_chain_.empty()); |
Hiroshige Hayashizaki | 334d0fa | 2023-08-16 23:33:27 | [diff] [blame] | 925 | if (!redirect_chain_.back()->response_reader_->GetHead()) { |
| 926 | // Either the last PrefetchResponseReader is for a redirect response, or for |
| 927 | // a final response not yet receiving its header. |
| 928 | return nullptr; |
| 929 | } |
| 930 | return redirect_chain_.back()->response_reader_.get(); |
| 931 | } |
| 932 | |
kenoss | f7b4d60d | 2024-07-16 15:15:08 | [diff] [blame] | 933 | const network::mojom::URLResponseHead* PrefetchContainer::GetNonRedirectHead() |
| 934 | const { |
| 935 | return GetNonRedirectResponseReader() |
| 936 | ? GetNonRedirectResponseReader()->GetHead() |
| 937 | : nullptr; |
| 938 | } |
| 939 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 940 | void PrefetchContainer::CancelStreamingURLLoaderIfNotServing() { |
| 941 | if (!streaming_loader_) { |
| 942 | return; |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 943 | } |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 944 | streaming_loader_->CancelIfNotServing(); |
| 945 | streaming_loader_.reset(); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 946 | } |
| 947 | |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 948 | void PrefetchContainer::OnDeterminedHead() { |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 949 | SetLoadState(LoadState::kDeterminedHead); |
| 950 | |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 951 | if (GetNonRedirectHead()) { |
| 952 | time_header_determined_successfully_ = base::TimeTicks::Now(); |
| 953 | } |
| 954 | |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 955 | // Propagates the header to `no_vary_search_data_` if a non-redirect response |
| 956 | // header is got. |
Taiyo Mizuhashi | 9a1ab988 | 2024-11-28 08:29:08 | [diff] [blame] | 957 | MaybeSetNoVarySearchData(); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 958 | |
| 959 | for (auto& observer : observers_) { |
| 960 | observer.OnDeterminedHead(*this); |
| 961 | } |
| 962 | } |
| 963 | |
Taiyo Mizuhashi | 9a1ab988 | 2024-11-28 08:29:08 | [diff] [blame] | 964 | void PrefetchContainer::MaybeSetNoVarySearchData() { |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 965 | CHECK(!no_vary_search_data_.has_value()); |
kenoss | f7b4d60d | 2024-07-16 15:15:08 | [diff] [blame] | 966 | |
| 967 | if (!GetNonRedirectHead()) { |
| 968 | return; |
| 969 | } |
| 970 | |
Taiyo Mizuhashi | 9a1ab988 | 2024-11-28 08:29:08 | [diff] [blame] | 971 | // RenderFrameHostImpl will be used to display error messagse in DevTools |
| 972 | // console. Can be null when the prefetch is browser-initiated. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 973 | RenderFrameHostImpl* rfhi_can_be_null = nullptr; |
| 974 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 975 | rfhi_can_be_null = renderer_initiator_info->GetRenderFrameHost(); |
| 976 | } |
Taiyo Mizuhashi | 9a1ab988 | 2024-11-28 08:29:08 | [diff] [blame] | 977 | no_vary_search_data_ = no_vary_search::ProcessHead( |
| 978 | *GetNonRedirectHead(), GetURL(), rfhi_can_be_null); |
kenoss | f7b4d60d | 2024-07-16 15:15:08 | [diff] [blame] | 979 | } |
| 980 | |
elabadysayed | f7d6b00f | 2025-02-05 11:27:00 | [diff] [blame] | 981 | void PrefetchContainer::StartTimeoutTimerIfNeeded( |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 982 | base::OnceClosure on_timeout_callback) { |
Hiroshige Hayashizaki | af26143 | 2025-08-19 19:04:32 | [diff] [blame] | 983 | if (request().ttl().is_positive()) { |
elabadysayed | f7d6b00f | 2025-02-05 11:27:00 | [diff] [blame] | 984 | CHECK(!timeout_timer_); |
| 985 | timeout_timer_ = std::make_unique<base::OneShotTimer>(); |
Hiroshige Hayashizaki | af26143 | 2025-08-19 19:04:32 | [diff] [blame] | 986 | timeout_timer_->Start(FROM_HERE, request().ttl(), |
| 987 | std::move(on_timeout_callback)); |
elabadysayed | f7d6b00f | 2025-02-05 11:27:00 | [diff] [blame] | 988 | } |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 989 | } |
| 990 | |
Hiroshige Hayashizaki | 29e14dd | 2025-03-14 16:03:07 | [diff] [blame] | 991 | // static |
| 992 | void PrefetchContainer::SetPrefetchResponseCompletedCallbackForTesting( |
| 993 | PrefetchResponseCompletedCallbackForTesting callback) { |
| 994 | GetPrefetchResponseCompletedCallbackForTesting() = // IN-TEST |
| 995 | std::move(callback); |
| 996 | } |
| 997 | |
Hiroshige Hayashizaki | db5f196 | 2025-07-14 22:38:20 | [diff] [blame] | 998 | void PrefetchContainer::OnPrefetchCompleteInternal( |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 999 | const network::URLLoaderCompletionStatus& completion_status) { |
| 1000 | DVLOG(1) << *this << "::OnPrefetchComplete"; |
| 1001 | |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 1002 | UMA_HISTOGRAM_COUNTS_100("PrefetchProxy.Prefetch.RedirectChainSize", |
| 1003 | redirect_chain_.size()); |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1004 | |
| 1005 | if (GetNonRedirectResponseReader()) { |
| 1006 | UpdatePrefetchRequestMetrics( |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1007 | GetNonRedirectResponseReader()->GetHead()); |
| 1008 | UpdateServingPageMetrics(); |
| 1009 | } else { |
Liviu Tinta | 1fe1a6d | 2023-09-20 19:44:04 | [diff] [blame] | 1010 | DVLOG(1) << *this << "::OnPrefetchComplete:" |
| 1011 | << "no non redirect response reader"; |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1012 | } |
| 1013 | |
| 1014 | if (IsDecoy()) { |
| 1015 | SetPrefetchStatus(PrefetchStatus::kPrefetchIsPrivacyDecoy); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 1016 | return; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1017 | } |
| 1018 | |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 1019 | // TODO(crbug.com/40250089): Call |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1020 | // `devtools_instrumentation::OnPrefetchBodyDataReceived()` with body of the |
| 1021 | // response. |
| 1022 | NotifyPrefetchRequestComplete(completion_status); |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1023 | |
| 1024 | int net_error = completion_status.error_code; |
| 1025 | int64_t body_length = completion_status.decoded_body_length; |
| 1026 | |
| 1027 | RecordPrefetchProxyPrefetchMainframeNetError(net_error); |
| 1028 | |
| 1029 | // Updates the prefetch's status if it hasn't been updated since the request |
| 1030 | // first started. For the prefetch to reach the network stack, it must have |
Hiroshige Hayashizaki | f1f8b360 | 2025-01-15 11:50:49 | [diff] [blame] | 1031 | // `PrefetchStatus::kPrefetchNotStarted` or beyond. |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1032 | DCHECK(HasPrefetchStatus()); |
| 1033 | if (GetPrefetchStatus() == PrefetchStatus::kPrefetchNotFinishedInTime) { |
| 1034 | SetPrefetchStatus(net_error == net::OK |
| 1035 | ? PrefetchStatus::kPrefetchSuccessful |
| 1036 | : PrefetchStatus::kPrefetchFailedNetError); |
| 1037 | UpdateServingPageMetrics(); |
| 1038 | } |
| 1039 | |
| 1040 | if (net_error == net::OK) { |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1041 | time_prefetch_completed_successfully_ = base::TimeTicks::Now(); |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1042 | RecordPrefetchProxyPrefetchMainframeBodyLength(body_length); |
| 1043 | } |
| 1044 | |
Wayne Jackson Jr. | b22d53b2 | 2024-11-15 11:40:06 | [diff] [blame] | 1045 | const PrefetchStatus prefetch_status = GetPrefetchStatus(); |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1046 | |
| 1047 | if (prefetch_status == PrefetchStatus::kPrefetchSuccessful) { |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1048 | // TODO(crbug.com/40946257): Current code doesn't support |
| 1049 | // PrefetchReferringPageMetrics when the prefetch is initiated by browser. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1050 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 1051 | if (renderer_initiator_info->prefetch_document_manager()) { |
| 1052 | renderer_initiator_info->prefetch_document_manager() |
| 1053 | ->OnPrefetchSuccessful(this); |
| 1054 | } |
Wayne Jackson Jr. | b22d53b2 | 2024-11-15 11:40:06 | [diff] [blame] | 1055 | } |
| 1056 | } |
| 1057 | |
Hiroshige Hayashizaki | b2a491b | 2025-08-19 18:55:41 | [diff] [blame] | 1058 | if (auto* browser_initiator_info = request().GetBrowserInitiatorInfo()) { |
| 1059 | if (auto* listener = browser_initiator_info->request_status_listener()) { |
| 1060 | switch (prefetch_status) { |
| 1061 | case PrefetchStatus::kPrefetchSuccessful: |
| 1062 | case PrefetchStatus::kPrefetchResponseUsed: |
| 1063 | listener->OnPrefetchResponseCompleted(); |
| 1064 | break; |
| 1065 | case PrefetchStatus::kPrefetchFailedNon2XX: { |
| 1066 | int response_code = |
| 1067 | GetNonRedirectHead() |
| 1068 | ? GetNonRedirectHead()->headers->response_code() |
| 1069 | : 0; |
| 1070 | listener->OnPrefetchResponseServerError(response_code); |
| 1071 | break; |
| 1072 | } |
| 1073 | default: |
| 1074 | listener->OnPrefetchResponseError(); |
| 1075 | break; |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1076 | } |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 1077 | } |
| 1078 | } |
Hiroshige Hayashizaki | db5f196 | 2025-07-14 22:38:20 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | void PrefetchContainer::OnPrefetchComplete( |
| 1082 | const network::URLLoaderCompletionStatus& completion_status) { |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 1083 | SetLoadState(LoadState::kCompletedOrFailed); |
Hiroshige Hayashizaki | db5f196 | 2025-07-14 22:38:20 | [diff] [blame] | 1084 | OnPrefetchCompleteInternal(completion_status); |
Taiyo Mizuhashi | 2e51a63 | 2025-03-28 02:33:36 | [diff] [blame] | 1085 | |
kenoss | b4e57f7 | 2025-06-16 08:28:40 | [diff] [blame] | 1086 | std::optional<int> response_code = std::nullopt; |
Hiroshige Hayashizaki | db5f196 | 2025-07-14 22:38:20 | [diff] [blame] | 1087 | int net_error = completion_status.error_code; |
kenoss | b4e57f7 | 2025-06-16 08:28:40 | [diff] [blame] | 1088 | if (net_error == net::OK && GetNonRedirectHead() && |
| 1089 | GetNonRedirectHead()->headers) { |
| 1090 | response_code = GetNonRedirectHead()->headers->response_code(); |
| 1091 | } |
| 1092 | for (auto& observer : observers_) { |
Hiroshige Hayashizaki | 6a103bad | 2025-07-11 02:15:32 | [diff] [blame] | 1093 | observer.OnPrefetchCompletedOrFailed(*this, completion_status, |
| 1094 | response_code); |
kenoss | b4e57f7 | 2025-06-16 08:28:40 | [diff] [blame] | 1095 | } |
| 1096 | |
Taiyo Mizuhashi | 2e51a63 | 2025-03-28 02:33:36 | [diff] [blame] | 1097 | if (GetPrefetchResponseCompletedCallbackForTesting()) { |
| 1098 | GetPrefetchResponseCompletedCallbackForTesting().Run( // IN-TEST |
| 1099 | GetWeakPtr()); |
| 1100 | } |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void PrefetchContainer::UpdatePrefetchRequestMetrics( |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 1104 | const network::mojom::URLResponseHead* head) { |
Liviu Tinta | 1fe1a6d | 2023-09-20 19:44:04 | [diff] [blame] | 1105 | DVLOG(1) << *this << "::UpdatePrefetchRequestMetrics:" |
| 1106 | << "head = " << head; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 1107 | |
| 1108 | if (head) |
| 1109 | header_latency_ = |
| 1110 | head->load_timing.receive_headers_end - head->load_timing.request_start; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 1111 | } |
| 1112 | |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1113 | PrefetchServableState PrefetchContainer::GetServableState( |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1114 | base::TimeDelta cacheable_duration) const { |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 1115 | // Servable if the non-redirect response (either fully or partially |
Hiroshige Hayashizaki | 334d0fa | 2023-08-16 23:33:27 | [diff] [blame] | 1116 | // received body) is servable. |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 1117 | if (GetNonRedirectResponseReader() && |
| 1118 | GetNonRedirectResponseReader()->Servable(cacheable_duration)) { |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1119 | return PrefetchServableState::kServable; |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 1120 | } |
| 1121 | |
Liviu Tinta | 1fe1a6d | 2023-09-20 19:44:04 | [diff] [blame] | 1122 | DVLOG(1) << *this << "(GetServableState)" |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1123 | << "(streaming_loader=" << GetStreamingURLLoader().get() |
Hiroshige Hayashizaki | b3e51a5 | 2025-01-18 00:17:21 | [diff] [blame] | 1124 | << ", LoadState=" << load_state_ << ")"; |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 1125 | // Can only block until head if the request has been started using a |
| 1126 | // streaming URL loader and head/failure/redirect hasn't been received yet. |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1127 | if (GetStreamingURLLoader() && |
Kouhei Ueno | fc48214 | 2024-07-03 01:32:57 | [diff] [blame] | 1128 | redirect_chain_.back()->response_reader_->IsWaitingForResponse()) { |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1129 | return PrefetchServableState::kShouldBlockUntilHeadReceived; |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 1130 | } |
| 1131 | |
kenoss | c649294 | 2025-04-10 19:36:32 | [diff] [blame] | 1132 | if (features::UsePrefetchPrerenderIntegration()) { |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1133 | switch (load_state_) { |
| 1134 | case LoadState::kNotStarted: |
| 1135 | case LoadState::kEligible: |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1136 | return PrefetchServableState::kShouldBlockUntilEligibilityGot; |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1137 | case LoadState::kFailedIneligible: |
| 1138 | case LoadState::kStarted: |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 1139 | case LoadState::kDeterminedHead: |
| 1140 | case LoadState::kCompletedOrFailed: |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1141 | case LoadState::kFailedHeldback: |
| 1142 | // nop |
| 1143 | break; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1147 | return PrefetchServableState::kNotServable; |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 1148 | } |
| 1149 | |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1150 | PrefetchSingleRedirectHop& |
| 1151 | PrefetchContainer::GetCurrentSingleRedirectHopToPrefetch() const { |
Hiroshige Hayashizaki | 8ce7d8d | 2023-05-26 00:16:32 | [diff] [blame] | 1152 | CHECK(redirect_chain_.size() > 0); |
| 1153 | return *redirect_chain_[redirect_chain_.size() - 1]; |
| 1154 | } |
| 1155 | |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1156 | const PrefetchSingleRedirectHop& |
| 1157 | PrefetchContainer::GetPreviousSingleRedirectHopToPrefetch() const { |
Hiroshige Hayashizaki | 8ce7d8d | 2023-05-26 00:16:32 | [diff] [blame] | 1158 | CHECK(redirect_chain_.size() > 1); |
| 1159 | return *redirect_chain_[redirect_chain_.size() - 2]; |
| 1160 | } |
| 1161 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1162 | void PrefetchContainer::SetServingPageMetrics( |
| 1163 | base::WeakPtr<PrefetchServingPageMetricsContainer> |
| 1164 | serving_page_metrics_container) { |
| 1165 | serving_page_metrics_container_ = serving_page_metrics_container; |
| 1166 | } |
| 1167 | |
| 1168 | void PrefetchContainer::UpdateServingPageMetrics() { |
Liviu Tinta | 1fe1a6d | 2023-09-20 19:44:04 | [diff] [blame] | 1169 | DVLOG(1) << *this << "::UpdateServingPageMetrics:" |
| 1170 | << "serving_page_metrics_container_ = " |
| 1171 | << serving_page_metrics_container_.get(); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1172 | if (!serving_page_metrics_container_) { |
| 1173 | return; |
| 1174 | } |
| 1175 | |
| 1176 | serving_page_metrics_container_->SetRequiredPrivatePrefetchProxy( |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1177 | request().prefetch_type().IsProxyRequiredWhenCrossOrigin()); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1178 | serving_page_metrics_container_->SetPrefetchHeaderLatency( |
| 1179 | GetPrefetchHeaderLatency()); |
| 1180 | if (HasPrefetchStatus()) { |
| 1181 | serving_page_metrics_container_->SetPrefetchStatus(GetPrefetchStatus()); |
| 1182 | } |
Liviu Tinta | d97a6a3 | 2022-12-08 23:28:40 | [diff] [blame] | 1183 | } |
| 1184 | |
Hiroshige Hayashizaki | 64802cf1 | 2025-01-16 23:18:46 | [diff] [blame] | 1185 | void PrefetchContainer::SimulatePrefetchEligibleForTest() { |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 1186 | if (request().attempt()) { |
| 1187 | request().attempt()->SetEligibility(PreloadingEligibility::kEligible); |
| 1188 | request().attempt()->SetHoldbackStatus(PreloadingHoldbackStatus::kAllowed); |
Adithya Srinivasan | a486d5e | 2024-03-08 15:19:32 | [diff] [blame] | 1189 | } |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1190 | SetLoadState(LoadState::kEligible); |
Hiroshige Hayashizaki | f1f8b360 | 2025-01-15 11:50:49 | [diff] [blame] | 1191 | SetPrefetchStatus(PrefetchStatus::kPrefetchNotStarted); |
Hiroshige Hayashizaki | 64802cf1 | 2025-01-16 23:18:46 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | void PrefetchContainer::SimulatePrefetchStartedForTest() { |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1195 | SetLoadState(LoadState::kStarted); |
Adithya Srinivasan | a486d5e | 2024-03-08 15:19:32 | [diff] [blame] | 1196 | SetPrefetchStatus(PrefetchStatus::kPrefetchNotFinishedInTime); |
| 1197 | } |
| 1198 | |
Hiroshige Hayashizaki | 64802cf1 | 2025-01-16 23:18:46 | [diff] [blame] | 1199 | void PrefetchContainer::SimulatePrefetchCompletedForTest() { |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1200 | SetPrefetchStatus(PrefetchStatus::kPrefetchSuccessful); |
| 1201 | } |
| 1202 | |
Hiroshige Hayashizaki | 64802cf1 | 2025-01-16 23:18:46 | [diff] [blame] | 1203 | void PrefetchContainer::SimulatePrefetchFailedIneligibleForTest( |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1204 | PreloadingEligibility eligibility) { |
| 1205 | CHECK_NE(PreloadingEligibility::kEligible, eligibility); |
| 1206 | |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 1207 | if (request().attempt()) { |
| 1208 | request().attempt()->SetEligibility(eligibility); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1209 | } |
| 1210 | SetLoadState(LoadState::kFailedIneligible); |
| 1211 | } |
| 1212 | |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 1213 | void PrefetchContainer::OnDetectedCookiesChange( |
kenoss | b1c65dbc | 2024-12-13 08:23:49 | [diff] [blame] | 1214 | std::optional<bool> |
| 1215 | is_unblock_for_cookies_changed_triggered_by_this_prefetch_container) { |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 1216 | // Multiple `PrefetchMatchResolver` can wait the same `PrefetchContainer`. So, |
| 1217 | // `OnDetectedCookiesChange()` can be called multiple times, |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1218 | if (on_detected_cookies_change_called_) { |
| 1219 | return; |
| 1220 | } |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 1221 | on_detected_cookies_change_called_ = true; |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1222 | |
kenoss | b1c65dbc | 2024-12-13 08:23:49 | [diff] [blame] | 1223 | // There are cases that `prefetch_status_` is failure but this method is |
| 1224 | // called. For more details, see |
| 1225 | // https://docs.google.com/document/d/1G48SaWbdOy1yNBT1wio2IHVuUtddF5VLFsT6BRSYPMI/edit?tab=t.hpkotaxo7tfh#heading=h.woaoy8erwx63 |
| 1226 | // |
| 1227 | // To prevent crash, we don't call `SetPrefetchStatus()`. |
| 1228 | if (prefetch_status_ && |
| 1229 | TriggeringOutcomeFromStatus(prefetch_status_.value()) == |
| 1230 | PreloadingTriggeringOutcome::kFailure) { |
| 1231 | SCOPED_CRASH_KEY_NUMBER("PrefetchContainer", "ODCC2_from", |
| 1232 | static_cast<int>(prefetch_status_.value())); |
| 1233 | if (is_unblock_for_cookies_changed_triggered_by_this_prefetch_container |
| 1234 | .has_value()) { |
| 1235 | SCOPED_CRASH_KEY_BOOL( |
| 1236 | "PrefetchContainer", "ODCC2_iufcctbtpc", |
| 1237 | is_unblock_for_cookies_changed_triggered_by_this_prefetch_container |
| 1238 | .value()); |
| 1239 | } |
| 1240 | base::debug::DumpWithoutCrashing(); |
| 1241 | return; |
| 1242 | } |
| 1243 | |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 1244 | CHECK_NE(GetPrefetchStatus(), PrefetchStatus::kPrefetchNotUsedCookiesChanged); |
| 1245 | SetPrefetchStatus(PrefetchStatus::kPrefetchNotUsedCookiesChanged); |
| 1246 | UpdateServingPageMetrics(); |
| 1247 | CancelStreamingURLLoaderIfNotServing(); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1248 | } |
| 1249 | |
Wayne Jackson Jr. | 03a15fa6 | 2024-09-16 14:42:15 | [diff] [blame] | 1250 | void PrefetchContainer::OnPrefetchStarted() { |
| 1251 | SetLoadState(PrefetchContainer::LoadState::kStarted); |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1252 | time_prefetch_started_ = base::TimeTicks::Now(); |
Wayne Jackson Jr. | 03a15fa6 | 2024-09-16 14:42:15 | [diff] [blame] | 1253 | } |
| 1254 | |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 1255 | bool PrefetchContainer::HasSameReferringURLForMetrics( |
| 1256 | const PrefetchContainer& other) const { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1257 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 1258 | if (auto* other_renderer_initiator_info = |
| 1259 | other.request().GetRendererInitiatorInfo()) { |
| 1260 | return renderer_initiator_info->url_hash() == |
| 1261 | other_renderer_initiator_info->url_hash(); |
| 1262 | } |
| 1263 | } |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | bool PrefetchContainer::HasSameReferringRenderFrameHostIdForMetrics( |
| 1268 | const PrefetchContainer& other) const { |
| 1269 | if (auto* renderer_initiator_info = request().GetRendererInitiatorInfo()) { |
| 1270 | if (auto* other_renderer_initiator_info = |
| 1271 | other.request().GetRendererInitiatorInfo()) { |
| 1272 | return renderer_initiator_info->GetRenderFrameHostId() == |
| 1273 | other_renderer_initiator_info->GetRenderFrameHostId(); |
| 1274 | } |
| 1275 | } |
| 1276 | return false; |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 1277 | } |
| 1278 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1279 | GURL PrefetchContainer::GetCurrentURL() const { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1280 | return GetCurrentSingleRedirectHopToPrefetch().url_; |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | GURL PrefetchContainer::GetPreviousURL() const { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1284 | return GetPreviousSingleRedirectHopToPrefetch().url_; |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1285 | } |
| 1286 | |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1287 | bool PrefetchContainer::IsIsolatedNetworkContextRequiredForCurrentPrefetch() |
| 1288 | const { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1289 | const PrefetchSingleRedirectHop& this_prefetch = |
| 1290 | GetCurrentSingleRedirectHopToPrefetch(); |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1291 | return this_prefetch.is_isolated_network_context_required_; |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 1292 | } |
| 1293 | |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1294 | bool PrefetchContainer::IsIsolatedNetworkContextRequiredForPreviousRedirectHop() |
| 1295 | const { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1296 | const PrefetchSingleRedirectHop& previous_prefetch = |
| 1297 | GetPreviousSingleRedirectHopToPrefetch(); |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1298 | return previous_prefetch.is_isolated_network_context_required_; |
| 1299 | } |
| 1300 | |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 1301 | base::WeakPtr<PrefetchResponseReader> |
| 1302 | PrefetchContainer::GetResponseReaderForCurrentPrefetch() { |
Hiroshige Hayashizaki | 87a4393 | 2025-08-12 06:57:25 | [diff] [blame] | 1303 | const PrefetchSingleRedirectHop& this_prefetch = |
| 1304 | GetCurrentSingleRedirectHopToPrefetch(); |
Hiroshige Hayashizaki | 4486220 | 2023-09-18 21:53:30 | [diff] [blame] | 1305 | CHECK(this_prefetch.response_reader_); |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 1306 | return this_prefetch.response_reader_->GetWeakPtr(); |
| 1307 | } |
| 1308 | |
Max Curran | c013750 | 2023-05-02 18:06:22 | [diff] [blame] | 1309 | bool PrefetchContainer::IsProxyRequiredForURL(const GURL& url) const { |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 1310 | return IsCrossOriginRequest(url::Origin::Create(url)) && |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1311 | request().prefetch_type().IsProxyRequiredWhenCrossOrigin(); |
Max Curran | c013750 | 2023-05-02 18:06:22 | [diff] [blame] | 1312 | } |
| 1313 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1314 | void PrefetchContainer::MakeResourceRequest( |
| 1315 | const net::HttpRequestHeaders& additional_headers) { |
| 1316 | // |AddRedirectHop| updates this request later on. Anything here that should |
| 1317 | // be changed on redirect should happen there. |
| 1318 | |
| 1319 | const GURL& url = GetURL(); |
| 1320 | url::Origin origin = url::Origin::Create(url); |
| 1321 | net::IsolationInfo isolation_info = net::IsolationInfo::Create( |
| 1322 | net::IsolationInfo::RequestType::kMainFrame, origin, origin, |
| 1323 | net::SiteForCookies::FromOrigin(origin)); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1324 | |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1325 | auto priority = [&] { |
Hiroshige Hayashizaki | 8b59d908 | 2025-08-19 18:58:04 | [diff] [blame] | 1326 | if (request().priority().has_value()) { |
| 1327 | switch (request().priority().value()) { |
Taiyo Mizuhashi | 39f0919 | 2025-06-23 08:57:13 | [diff] [blame] | 1328 | case PrefetchPriority::kLow: |
| 1329 | return net::RequestPriority::IDLE; |
| 1330 | case PrefetchPriority::kMedium: |
| 1331 | return net::RequestPriority::LOW; |
| 1332 | case PrefetchPriority::kHigh: |
| 1333 | return net::RequestPriority::MEDIUM; |
| 1334 | case PrefetchPriority::kHighest: |
| 1335 | return net::RequestPriority::HIGHEST; |
| 1336 | } |
| 1337 | } |
| 1338 | |
| 1339 | // TODO(crbug.com/426404355): Migrate to use `PrefetchPriority`. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1340 | if (IsSpeculationRuleType(request().prefetch_type().trigger_type())) { |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 1341 | // This may seem inverted (surely immediate prefetches would be higher |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 1342 | // priority), but the fact that we're doing this at all for more |
| 1343 | // conservative candidates suggests a strong engagement signal. |
| 1344 | // |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 1345 | // TODO(crbug.com/40276985): Ideally, we would actually use a combination |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 1346 | // of the actual engagement seen (rather than the minimum required to |
| 1347 | // trigger the candidate) and the declared eagerness, and update them as |
| 1348 | // the prefetch becomes increasingly likely. |
| 1349 | blink::mojom::SpeculationEagerness eagerness = |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1350 | request().prefetch_type().GetEagerness(); |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 1351 | switch (eagerness) { |
| 1352 | case blink::mojom::SpeculationEagerness::kConservative: |
| 1353 | return net::RequestPriority::MEDIUM; |
| 1354 | case blink::mojom::SpeculationEagerness::kModerate: |
| 1355 | return net::RequestPriority::LOW; |
Takashi Nakayama | 9eb7988f | 2025-06-25 06:14:00 | [diff] [blame] | 1356 | // TODO(crbug.com/40287486, crbug.com/406927300): Set appropriate value |
| 1357 | // after changing the behavior for `kEager` |
| 1358 | case blink::mojom::SpeculationEagerness::kEager: |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 1359 | case blink::mojom::SpeculationEagerness::kImmediate: |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 1360 | return net::RequestPriority::IDLE; |
| 1361 | } |
| 1362 | } else { |
Taiyo Mizuhashi | 2bef668 | 2025-03-07 08:58:45 | [diff] [blame] | 1363 | if (base::FeatureList::IsEnabled( |
| 1364 | features::kPrefetchNetworkPriorityForEmbedders)) { |
| 1365 | return net::RequestPriority::MEDIUM; |
| 1366 | } else { |
| 1367 | return net::RequestPriority::IDLE; |
| 1368 | } |
Taiyo Mizuhashi | 0889c07 | 2024-03-13 13:29:20 | [diff] [blame] | 1369 | } |
| 1370 | }(); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1371 | |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1372 | mojo::PendingRemote<network::mojom::DevToolsObserver> |
| 1373 | devtools_observer_remote; |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1374 | if (std::optional<mojo::PendingRemote<network::mojom::DevToolsObserver>> |
| 1375 | devtools_observer = MakeSelfOwnedNetworkServiceDevToolsObserver()) { |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1376 | devtools_observer_remote = std::move(devtools_observer.value()); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1377 | } |
| 1378 | |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1379 | // If we ever implement prefetching for subframes, this value should be |
| 1380 | // reconsidered, as this causes us to reset the site for cookies on cross-site |
| 1381 | // redirect. |
| 1382 | const bool is_main_frame = true; |
| 1383 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1384 | auto resource_request = CreateResourceRequestForNavigation( |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1385 | net::HttpRequestHeaders::kGetMethod, url, |
Hiroshige Hayashizaki | e3f67749 | 2025-01-07 05:50:25 | [diff] [blame] | 1386 | network::mojom::RequestDestination::kDocument, referrer_, isolation_info, |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1387 | std::move(devtools_observer_remote), priority, is_main_frame); |
| 1388 | |
| 1389 | // Note: Even without LOAD_DISABLE_CACHE, a cross-site prefetch uses a |
| 1390 | // separate network context, which means responses cached before the prefetch |
| 1391 | // are not visible to the prefetch, and anything cached by this request will |
| 1392 | // not be visible outside of the network context. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1393 | resource_request->load_flags = net::LOAD_PREFETCH; |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1394 | |
Hiroshige Hayashizaki | eaccf739 | 2025-08-19 18:56:02 | [diff] [blame] | 1395 | resource_request->headers.MergeFrom(request().additional_headers()); |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1396 | resource_request->headers.MergeFrom(additional_headers); |
Steven Wei | ac28475 | 2025-05-29 21:10:04 | [diff] [blame] | 1397 | if (!base::FeatureList::IsEnabled( |
| 1398 | blink::features::kRemovePurposeHeaderForPrefetch)) { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1399 | resource_request->headers.SetHeader(blink::kPurposeHeaderName, |
| 1400 | blink::kSecPurposePrefetchHeaderValue); |
Steven Wei | ac28475 | 2025-05-29 21:10:04 | [diff] [blame] | 1401 | } |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1402 | resource_request->headers.SetHeader(blink::kSecPurposeHeaderName, |
| 1403 | GetSecPurposeHeaderValue(url)); |
| 1404 | resource_request->headers.SetHeader("Upgrade-Insecure-Requests", "1"); |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1405 | |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 1406 | // Sec-Speculation-Tags is set only when the prefetch is triggered |
HuanPo Lin | d8887e3 | 2025-04-17 04:08:14 | [diff] [blame] | 1407 | // by speculation rules and it is not cross-site prefetch. |
| 1408 | // To see more details: |
| 1409 | // https://github.com/WICG/nav-speculation/blob/main/speculation-rules-tags.md#the-cross-site-case |
Hiroshige Hayashizaki | 995741b | 2025-08-19 18:41:13 | [diff] [blame] | 1410 | if (request().speculation_rules_tags().has_value() && |
| 1411 | !IsCrossSiteRequest(origin)) { |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 1412 | std::optional<std::string> serialized_list = |
Hiroshige Hayashizaki | 995741b | 2025-08-19 18:41:13 | [diff] [blame] | 1413 | request().speculation_rules_tags()->ConvertStringToHeaderString(); |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 1414 | CHECK(serialized_list.has_value()); |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1415 | resource_request->headers.SetHeader(blink::kSecSpeculationTagsHeaderName, |
| 1416 | serialized_list.value()); |
HuanPo Lin | 740620b | 2025-03-21 12:37:26 | [diff] [blame] | 1417 | } |
| 1418 | |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1419 | // There are sometimes other headers that are set during navigation. These |
| 1420 | // aren't yet supported for prefetch, including browsing topics. |
| 1421 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1422 | resource_request->devtools_request_id = RequestId(); |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1423 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1424 | AddClientHintsHeaders(origin, &resource_request->headers); |
Hiroshige Hayashizaki | 4a9ddf18 | 2025-08-19 19:04:47 | [diff] [blame] | 1425 | if (request().should_append_variations_header()) { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1426 | AddXClientDataHeader(*resource_request.get()); |
elabadysayed | 90651cc | 2025-03-28 00:23:22 | [diff] [blame] | 1427 | } |
Hiroshige Hayashizaki | eb3867c | 2024-12-26 07:30:30 | [diff] [blame] | 1428 | |
Hiroshige Hayashizaki | 9b63d50a | 2025-05-21 16:22:01 | [diff] [blame] | 1429 | // `URLLoaderNetworkServiceObserver` |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1430 | // (`resource_request->trusted_params->url_loader_network_observer`) is NOT |
| 1431 | // set here, because for prefetching request we don't want to ask users e.g. |
| 1432 | // for authentication/cert errors, and instead make the prefetch fail. Because |
| 1433 | // of this, `ServiceWorkerClient::GetOngoingNavigationRequestBeforeCommit()` |
| 1434 | // is never called. `NavPrefetchBrowserTest` has the corresponding test |
| 1435 | // coverage. |
Hiroshige Hayashizaki | 9b63d50a | 2025-05-21 16:22:01 | [diff] [blame] | 1436 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1437 | resource_request_ = std::move(resource_request); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1438 | } |
| 1439 | |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 1440 | void PrefetchContainer::UpdateReferrer( |
| 1441 | const GURL& new_referrer_url, |
| 1442 | const network::mojom::ReferrerPolicy& new_referrer_policy) { |
| 1443 | referrer_.url = new_referrer_url; |
| 1444 | referrer_.policy = new_referrer_policy; |
| 1445 | } |
| 1446 | |
Hiroshige Hayashizaki | 1d9c40b8 | 2025-08-19 18:44:44 | [diff] [blame] | 1447 | const PrefetchKey& PrefetchContainer::key() const { |
| 1448 | return request().key(); |
| 1449 | } |
| 1450 | |
| 1451 | const GURL& PrefetchContainer::GetURL() const { |
| 1452 | return request().key().url(); |
| 1453 | } |
| 1454 | |
Hiroshige Hayashizaki | 88d1017b | 2025-08-19 17:24:32 | [diff] [blame] | 1455 | const std::optional<net::HttpNoVarySearchData>& |
| 1456 | PrefetchContainer::GetNoVarySearchHint() const { |
| 1457 | return request().no_vary_search_hint(); |
| 1458 | } |
| 1459 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1460 | void PrefetchContainer::AddClientHintsHeaders( |
| 1461 | const url::Origin& origin, |
| 1462 | net::HttpRequestHeaders* request_headers) { |
| 1463 | if (!base::FeatureList::IsEnabled(features::kPrefetchClientHints)) { |
| 1464 | return; |
| 1465 | } |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 1466 | if (!request().browser_context()) { |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1467 | return; |
| 1468 | } |
| 1469 | ClientHintsControllerDelegate* client_hints_delegate = |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 1470 | request().browser_context()->GetClientHintsControllerDelegate(); |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1471 | if (!client_hints_delegate) { |
| 1472 | return; |
| 1473 | } |
| 1474 | |
Alison Gale | 923a33e | 2024-04-22 23:34:28 | [diff] [blame] | 1475 | // TODO(crbug.com/41497015): Consider supporting UA override mode here |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1476 | const bool is_ua_override_on = false; |
| 1477 | net::HttpRequestHeaders client_hints_headers; |
Hiroshige Hayashizaki | 0eab2b1 | 2025-08-19 18:56:51 | [diff] [blame] | 1478 | if (request().is_javascript_enabled()) { |
Kouhei Ueno | 27f86cb | 2025-02-06 09:03:36 | [diff] [blame] | 1479 | // Historically, `AddClientHintsHeadersToPrefetchNavigation` added |
Hiroshige Hayashizaki | 0eab2b1 | 2025-08-19 18:56:51 | [diff] [blame] | 1480 | // Client Hints headers iff `request().is_javascript_enabled()`, so the `if` |
| 1481 | // block here is to persist the behavior. |
Kouhei Ueno | 295957b | 2025-02-06 11:14:58 | [diff] [blame] | 1482 | // TODO(crbug.com/394716357): Revisit if we really want to allow prefetch |
| 1483 | // for non-Javascript enabled profile/origins. |
Kouhei Ueno | 27f86cb | 2025-02-06 09:03:36 | [diff] [blame] | 1484 | AddClientHintsHeadersToPrefetchNavigation( |
Hiroshige Hayashizaki | d75d328a | 2025-08-19 18:46:15 | [diff] [blame] | 1485 | origin, &client_hints_headers, request().browser_context(), |
| 1486 | client_hints_delegate, is_ua_override_on); |
Kouhei Ueno | 27f86cb | 2025-02-06 09:03:36 | [diff] [blame] | 1487 | } |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1488 | |
| 1489 | // Merge in the client hints which are suitable to include given this is a |
| 1490 | // prefetch, and potentially a cross-site only. (This logic might need to be |
| 1491 | // revisited if we ever supported prefetching in another site's partition, |
| 1492 | // such as in a subframe.) |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 1493 | const bool is_cross_site = IsCrossSiteRequest(origin); |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1494 | const auto cross_site_behavior = |
| 1495 | features::kPrefetchClientHintsCrossSiteBehavior.Get(); |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 1496 | if (!is_cross_site || |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1497 | cross_site_behavior == |
| 1498 | features::PrefetchClientHintsCrossSiteBehavior::kAll) { |
| 1499 | request_headers->MergeFrom(client_hints_headers); |
| 1500 | } else if (cross_site_behavior == |
| 1501 | features::PrefetchClientHintsCrossSiteBehavior::kLowEntropy) { |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1502 | for (const auto& [ch, header] : network::GetClientHintToNameMap()) { |
Chris Fredrickson | d923c68 | 2024-07-30 18:19:34 | [diff] [blame] | 1503 | if (blink::IsClientHintSentByDefault(ch)) { |
| 1504 | std::optional<std::string> header_value = |
| 1505 | client_hints_headers.GetHeader(header); |
| 1506 | if (header_value) { |
| 1507 | request_headers->SetHeader(header, std::move(header_value).value()); |
| 1508 | } |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 | } |
| 1513 | |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1514 | std::ostream& operator<<(std::ostream& ostream, |
| 1515 | const PrefetchContainer& prefetch_container) { |
| 1516 | return ostream << "PrefetchContainer[" << &prefetch_container |
kenoss | af6e0ae | 2024-09-05 03:06:50 | [diff] [blame] | 1517 | << ", Key=" << prefetch_container.key() << "]"; |
Hiroshige Hayashizaki | f7f16b8 | 2023-10-10 01:57:47 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | std::ostream& operator<<(std::ostream& ostream, |
Kouhei Ueno | 929ee8e1 | 2024-07-05 03:09:56 | [diff] [blame] | 1521 | PrefetchContainer::LoadState state) { |
| 1522 | switch (state) { |
| 1523 | case PrefetchContainer::LoadState::kNotStarted: |
| 1524 | return ostream << "NotStarted"; |
| 1525 | case PrefetchContainer::LoadState::kEligible: |
| 1526 | return ostream << "Eligible"; |
| 1527 | case PrefetchContainer::LoadState::kFailedIneligible: |
| 1528 | return ostream << "FailedIneligible"; |
| 1529 | case PrefetchContainer::LoadState::kStarted: |
| 1530 | return ostream << "Started"; |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 1531 | case PrefetchContainer::LoadState::kDeterminedHead: |
| 1532 | return ostream << "DeterminedHead"; |
| 1533 | case PrefetchContainer::LoadState::kCompletedOrFailed: |
| 1534 | return ostream << "CompletedOrFailed"; |
Kouhei Ueno | 929ee8e1 | 2024-07-05 03:09:56 | [diff] [blame] | 1535 | case PrefetchContainer::LoadState::kFailedHeldback: |
| 1536 | return ostream << "FailedHeldback"; |
| 1537 | } |
| 1538 | } |
| 1539 | |
kenoss | 5df53e05 | 2024-06-17 06:59:52 | [diff] [blame] | 1540 | const char* PrefetchContainer::GetSecPurposeHeaderValue( |
| 1541 | const GURL& request_url) const { |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 1542 | switch (request().preload_pipeline_info().planned_max_preloading_type()) { |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1543 | case PreloadingType::kPrefetch: |
| 1544 | if (IsProxyRequiredForURL(request_url)) { |
David Risney | a1dd2bf | 2025-03-06 03:40:42 | [diff] [blame] | 1545 | return blink::kSecPurposePrefetchAnonymousClientIpHeaderValue; |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1546 | } else { |
David Risney | a1dd2bf | 2025-03-06 03:40:42 | [diff] [blame] | 1547 | return blink::kSecPurposePrefetchHeaderValue; |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1548 | } |
Lingqi Chi | 844e05d4 | 2025-07-23 02:50:50 | [diff] [blame] | 1549 | case PreloadingType::kPrerenderUntilScript: |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1550 | case PreloadingType::kPrerender: |
| 1551 | if (IsProxyRequiredForURL(request_url)) { |
| 1552 | // Note that this path would be reachable if a prefetch ahead of |
| 1553 | // prerender were triggered with a speculation candidate with |
| 1554 | // `requires_anonymous_client_ip_when_cross_origin`. But such |
| 1555 | // Speculation Rules are discarded in blink. |
| 1556 | // |
| 1557 | // See |
| 1558 | // https://github.com/WICG/nav-speculation/blob/main/triggers.md#requirements |
Peter Boström | 01ab59a | 2024-08-15 02:39:49 | [diff] [blame] | 1559 | NOTREACHED(); |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1560 | } else { |
David Risney | a1dd2bf | 2025-03-06 03:40:42 | [diff] [blame] | 1561 | return blink::kSecPurposePrefetchPrerenderHeaderValue; |
kenoss | 8644f6bf | 2025-02-10 05:51:44 | [diff] [blame] | 1562 | } |
| 1563 | case PreloadingType::kUnspecified: |
| 1564 | case PreloadingType::kPreconnect: |
| 1565 | case PreloadingType::kNoStatePrefetch: |
| 1566 | case PreloadingType::kLinkPreview: |
| 1567 | NOTREACHED(); |
kenoss | 5df53e05 | 2024-06-17 06:59:52 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
Wayne Jackson Jr. | 00e3e8b | 2024-09-25 12:35:56 | [diff] [blame] | 1571 | void PrefetchContainer::OnInitialPrefetchFailedIneligible( |
| 1572 | PreloadingEligibility eligibility) { |
| 1573 | CHECK(redirect_chain_.size() == 1); |
| 1574 | CHECK_NE(eligibility, PreloadingEligibility::kEligible); |
Hiroshige Hayashizaki | b2a491b | 2025-08-19 18:55:41 | [diff] [blame] | 1575 | if (auto* browser_initiator_info = request().GetBrowserInitiatorInfo()) { |
| 1576 | if (auto* listener = browser_initiator_info->request_status_listener()) { |
| 1577 | listener->OnPrefetchStartFailedGeneric(); |
| 1578 | } |
Wayne Jackson Jr. | 00e3e8b | 2024-09-25 12:35:56 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1582 | void PrefetchContainer::AddObserver(Observer* observer) { |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1583 | observers_.AddObserver(observer); |
| 1584 | } |
| 1585 | |
| 1586 | void PrefetchContainer::RemoveObserver(Observer* observer) { |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1587 | observers_.RemoveObserver(observer); |
| 1588 | } |
| 1589 | |
kenoss | da3d13b | 2024-08-15 16:31:19 | [diff] [blame] | 1590 | bool PrefetchContainer::IsExactMatch(const GURL& url) const { |
| 1591 | return url == GetURL(); |
| 1592 | } |
| 1593 | |
| 1594 | bool PrefetchContainer::IsNoVarySearchHeaderMatch(const GURL& url) const { |
| 1595 | const std::optional<net::HttpNoVarySearchData>& no_vary_search_data = |
| 1596 | GetNoVarySearchData(); |
| 1597 | return no_vary_search_data && |
| 1598 | no_vary_search_data->AreEquivalent(url, GetURL()); |
| 1599 | } |
| 1600 | |
kenoss | b9b4063 | 2024-10-01 15:43:24 | [diff] [blame] | 1601 | bool PrefetchContainer::ShouldWaitForNoVarySearchHeader(const GURL& url) const { |
| 1602 | const std::optional<net::HttpNoVarySearchData>& no_vary_search_hint = |
Hiroshige Hayashizaki | 88d1017b | 2025-08-19 17:24:32 | [diff] [blame] | 1603 | request().no_vary_search_hint(); |
kenoss | b9b4063 | 2024-10-01 15:43:24 | [diff] [blame] | 1604 | return !GetNonRedirectHead() && no_vary_search_hint && |
| 1605 | no_vary_search_hint->AreEquivalent(url, GetURL()); |
| 1606 | } |
| 1607 | |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1608 | void PrefetchContainer::OnUnregisterCandidate( |
| 1609 | const GURL& navigated_url, |
| 1610 | bool is_served, |
Taiyo Mizuhashi | 09f571f | 2025-08-04 16:05:54 | [diff] [blame] | 1611 | PrefetchPotentialCandidateServingResult matching_result, |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1612 | bool is_nav_prerender, |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1613 | std::optional<base::TimeDelta> blocked_duration) { |
| 1614 | // Note that this method can be called with `is_in_dtor_` true. |
| 1615 | // |
| 1616 | // TODO(crbug.com/356314759): Avoid calling this with `is_in_dtor_` |
| 1617 | // true. |
| 1618 | |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1619 | if (is_served) { |
Taiyo Mizuhashi | a009caf | 2025-08-05 07:34:22 | [diff] [blame] | 1620 | served_count_++; |
kenoss | 8fb8285 | 2025-03-11 02:20:42 | [diff] [blame] | 1621 | |
| 1622 | UMA_HISTOGRAM_COUNTS_100("PrefetchProxy.AfterClick.RedirectChainSize", |
| 1623 | redirect_chain_.size()); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1624 | } |
| 1625 | |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1626 | RecordPrefetchMatchingBlockedNavigationHistogram(blocked_duration.has_value(), |
| 1627 | is_nav_prerender); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1628 | |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1629 | RecordBlockUntilHeadDurationHistogram(blocked_duration, is_served, |
| 1630 | is_nav_prerender); |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1631 | |
Taiyo Mizuhashi | 09f571f | 2025-08-04 16:05:54 | [diff] [blame] | 1632 | RecordPrefetchPotentialCandidateServingResultHistogram(matching_result); |
| 1633 | |
kenoss | c6c712a | 2025-03-10 23:17:02 | [diff] [blame] | 1634 | // Note that `PreloadingAttemptImpl::SetIsAccurateTriggering()` is called for |
| 1635 | // prefetch in |
| 1636 | // |
| 1637 | // - A. `PreloadingDataImpl::DidStartNavigation()` |
| 1638 | // - B. Here |
| 1639 | // |
| 1640 | // A covers prefetches that satisfy `bool(GetNonRedirectHead())` at that |
| 1641 | // timing. B covers almost all ones that were once potentially matching to the |
| 1642 | // navigation, including that was `kBlockUntilHead` state. |
| 1643 | // |
| 1644 | // Note that multiple calls are safe and set a correct value. |
| 1645 | // |
| 1646 | // Historical note: Before No-Vary-Search hint, the decision to use a |
| 1647 | // prefetched response was made at A. With No-Vary-Search hint the decision to |
| 1648 | // use an in-flight prefetched response is delayed until the headers are |
| 1649 | // received from the server. This happens after `DidStartNavigation()`. At |
| 1650 | // this point in the code we have already decided we are going to use the |
| 1651 | // prefetch, so we can safely call `SetIsAccurateTriggering()`. |
Hiroshige Hayashizaki | b374e64 | 2025-08-19 18:53:32 | [diff] [blame] | 1652 | if (request().attempt()) { |
| 1653 | static_cast<PreloadingAttemptImpl*>(request().attempt()) |
kenoss | f6fbd565 | 2024-09-04 00:40:28 | [diff] [blame] | 1654 | ->SetIsAccurateTriggering(navigated_url); |
| 1655 | } |
| 1656 | } |
| 1657 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1658 | void PrefetchContainer::MigrateNewlyAdded( |
| 1659 | std::unique_ptr<PrefetchContainer> added) { |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 1660 | // `inherited_preload_pipeline_infos_` increases only if it is managed under |
| 1661 | // `PrefetchService`. |
| 1662 | CHECK(added->inherited_preload_pipeline_infos_.empty()); |
| 1663 | |
| 1664 | // Propagate eligibility (and status) to `added`. |
| 1665 | // |
| 1666 | // Assume we don't. (*) case is problematic. |
| 1667 | // |
| 1668 | // - If eligibility is not got, eligibility and status will be propagated by |
| 1669 | // the following `OnEligibilityCheckComplete()` and |
| 1670 | // `SetPrefetchStatusWithoutUpdatingTriggeringOutcome()`. |
| 1671 | // - If eligibility is got and ineligible, this `PrefetchContainer` is |
| 1672 | // `kNotServed` and `MigrateNewlyAdded()` is not called. |
| 1673 | // - If eligibility is got and `kEligible`: |
| 1674 | // - If status is not got, status will be propagated by the following |
| 1675 | // `SetPrefetchStatusWithoutUpdatingTriggeringOutcome()`. |
| 1676 | // - If status is eventually `kPrefetchSuccessful` or |
| 1677 | // `kPrefetchResponseUsed`, `kPrefetchResponseUsed` will be propagated |
| 1678 | // at the prefetch matching end. |
| 1679 | // - If status is eventually failure, status is propagated, but |
| 1680 | // eligibility is `kUnspecified`. (*) |
| 1681 | // - If status is got and `kPrefetchSuccessful` or `kPrefetchResponseUsed`, |
| 1682 | // `kPrefetchResponseUsed` will be propagated at the prefetch matching |
| 1683 | // end. |
| 1684 | // - If status is got and failure, this `PrefetchContainer` is `kNotServed` |
| 1685 | // and `MigrateNewlyAdded()` is not called. |
| 1686 | // |
| 1687 | // In (*), `PrerenderHost` have to cancel prerender with eligibility |
| 1688 | // `kUnspecified` and status failure. It's relatively complicated condition. |
| 1689 | // See a test |
| 1690 | // `PrerendererImplBrowserTestPrefetchAhead.PrefetchMigratedPrefetchFailurePrerenderFailure`. |
| 1691 | // |
| 1692 | // To make things simple, we propagate both eligibility and status. |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 1693 | scoped_refptr<PreloadPipelineInfoImpl> added_preload_pipeline_info = |
| 1694 | base::WrapRefCounted(&added->request().preload_pipeline_info()); |
| 1695 | |
| 1696 | added_preload_pipeline_info->SetPrefetchEligibility( |
| 1697 | request().preload_pipeline_info().prefetch_eligibility()); |
| 1698 | if (auto prefetch_status = |
| 1699 | request().preload_pipeline_info().prefetch_status()) { |
| 1700 | added_preload_pipeline_info->SetPrefetchStatus(*prefetch_status); |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | inherited_preload_pipeline_infos_.push_back( |
Hiroshige Hayashizaki | 1ec17a5 | 2025-08-19 18:49:34 | [diff] [blame] | 1704 | std::move(added_preload_pipeline_info)); |
| 1705 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1706 | is_likely_ahead_of_prerender_ |= added->is_likely_ahead_of_prerender_; |
| 1707 | } |
| 1708 | |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1709 | void PrefetchContainer::NotifyPrefetchRequestWillBeSent( |
| 1710 | const network::mojom::URLResponseHeadPtr* redirect_head) { |
| 1711 | if (IsDecoy()) { |
| 1712 | return; |
| 1713 | } |
| 1714 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1715 | auto* renderer_initiator_info = request().GetRendererInitiatorInfo(); |
| 1716 | if (!renderer_initiator_info) { |
| 1717 | // Don't emit CDP events if the trigger is not speculation rules. |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1718 | return; |
| 1719 | } |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1720 | |
| 1721 | auto* rfh = renderer_initiator_info->GetRenderFrameHost(); |
| 1722 | auto* ftn = FrameTreeNode::From(rfh); |
| 1723 | if (!rfh) { |
| 1724 | // Don't emit CDP events if the initiator document isn't alive. |
| 1725 | return; |
| 1726 | } |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1727 | |
| 1728 | if (redirect_head && *redirect_head) { |
| 1729 | const network::mojom::URLResponseHeadDevToolsInfoPtr info = |
| 1730 | network::ExtractDevToolsInfo(**redirect_head); |
| 1731 | const GURL url = GetPreviousURL(); |
| 1732 | std::pair<const GURL&, const network::mojom::URLResponseHeadDevToolsInfo&> |
| 1733 | redirect_info{url, *info.get()}; |
| 1734 | devtools_instrumentation::OnPrefetchRequestWillBeSent( |
| 1735 | *ftn, RequestId(), rfh->GetLastCommittedURL(), *GetResourceRequest(), |
| 1736 | std::move(redirect_info)); |
| 1737 | } else { |
| 1738 | devtools_instrumentation::OnPrefetchRequestWillBeSent( |
| 1739 | *ftn, RequestId(), rfh->GetLastCommittedURL(), *GetResourceRequest(), |
| 1740 | std::nullopt); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | void PrefetchContainer::NotifyPrefetchResponseReceived( |
| 1745 | const network::mojom::URLResponseHead& head) { |
| 1746 | // Ensured by the caller `PrefetchService::OnPrefetchResponseStarted()`. |
| 1747 | CHECK(!IsDecoy()); |
| 1748 | |
Taiyo Mizuhashi | 98ecb38 | 2025-06-03 15:24:55 | [diff] [blame] | 1749 | time_url_request_started_ = head.load_timing.request_start; |
| 1750 | |
| 1751 | // DevTools plumbing. |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1752 | auto* renderer_initiator_info = request().GetRendererInitiatorInfo(); |
| 1753 | if (!renderer_initiator_info) { |
| 1754 | // Don't emit CDP events if the trigger is not speculation rules. |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1755 | return; |
| 1756 | } |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1757 | |
| 1758 | auto* ftn = |
| 1759 | FrameTreeNode::From(renderer_initiator_info->GetRenderFrameHost()); |
| 1760 | if (!ftn) { |
| 1761 | // Don't emit CDP events if the initiator document isn't alive. |
| 1762 | return; |
| 1763 | } |
| 1764 | |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1765 | devtools_instrumentation::OnPrefetchResponseReceived(ftn, RequestId(), |
| 1766 | GetCurrentURL(), head); |
| 1767 | } |
| 1768 | |
| 1769 | void PrefetchContainer::NotifyPrefetchRequestComplete( |
| 1770 | const network::URLLoaderCompletionStatus& completion_status) { |
| 1771 | // Ensured by the caller `PrefetchService::OnPrefetchResponseStarted()`. |
| 1772 | CHECK(!IsDecoy()); |
| 1773 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1774 | auto* renderer_initiator_info = request().GetRendererInitiatorInfo(); |
| 1775 | if (!renderer_initiator_info) { |
| 1776 | // Don't emit CDP events if the trigger is not speculation rules. |
| 1777 | return; |
| 1778 | } |
| 1779 | |
| 1780 | auto* ftn = |
| 1781 | FrameTreeNode::From(renderer_initiator_info->GetRenderFrameHost()); |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1782 | if (!ftn) { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1783 | // Don't emit CDP events if the initiator document isn't alive. |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1784 | return; |
| 1785 | } |
| 1786 | |
| 1787 | devtools_instrumentation::OnPrefetchRequestComplete(ftn, RequestId(), |
| 1788 | completion_status); |
| 1789 | } |
| 1790 | |
| 1791 | std::optional<mojo::PendingRemote<network::mojom::DevToolsObserver>> |
| 1792 | PrefetchContainer::MakeSelfOwnedNetworkServiceDevToolsObserver() { |
| 1793 | if (IsDecoy()) { |
| 1794 | return std::nullopt; |
| 1795 | } |
| 1796 | |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1797 | auto* renderer_initiator_info = request().GetRendererInitiatorInfo(); |
| 1798 | if (!renderer_initiator_info) { |
| 1799 | // Don't emit CDP events if the trigger is not speculation rules. |
| 1800 | return std::nullopt; |
| 1801 | } |
| 1802 | |
| 1803 | auto* ftn = |
| 1804 | FrameTreeNode::From(renderer_initiator_info->GetRenderFrameHost()); |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1805 | if (!ftn) { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1806 | // Don't emit CDP events if the initiator document isn't alive. |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1807 | return std::nullopt; |
| 1808 | } |
| 1809 | |
| 1810 | return NetworkServiceDevToolsObserver::MakeSelfOwned(ftn); |
| 1811 | } |
| 1812 | |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1813 | std::string PrefetchContainer::GetMetricsSuffix() const { |
Hiroshige Hayashizaki | f55280f6 | 2025-08-19 17:23:20 | [diff] [blame] | 1814 | std::optional<std::string> embedder_histogram_suffix; |
| 1815 | if (auto* browser_initiator_info = request().GetBrowserInitiatorInfo()) { |
| 1816 | embedder_histogram_suffix = |
| 1817 | browser_initiator_info->embedder_histogram_suffix(); |
| 1818 | } |
| 1819 | return GetMetricsSuffixTriggerTypeAndEagerness(request().prefetch_type(), |
| 1820 | embedder_histogram_suffix); |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1821 | } |
| 1822 | |
Adithya Srinivasan | 4d3dad0 | 2024-10-17 18:06:59 | [diff] [blame] | 1823 | void PrefetchContainer::MaybeRecordPrefetchStatusToUMA( |
| 1824 | PrefetchStatus prefetch_status) { |
| 1825 | if (prefetch_status_recorded_to_uma_) { |
| 1826 | return; |
| 1827 | } |
| 1828 | |
| 1829 | base::UmaHistogramEnumeration("Preloading.Prefetch.PrefetchStatus", |
| 1830 | prefetch_status); |
| 1831 | prefetch_status_recorded_to_uma_ = true; |
| 1832 | } |
| 1833 | |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 1834 | void PrefetchContainer::OnServiceWorkerStateDetermined( |
| 1835 | PrefetchServiceWorkerState service_worker_state) { |
| 1836 | switch (service_worker_state_) { |
| 1837 | case PrefetchServiceWorkerState::kDisallowed: |
| 1838 | CHECK_EQ(service_worker_state, PrefetchServiceWorkerState::kDisallowed); |
| 1839 | break; |
| 1840 | case PrefetchServiceWorkerState::kAllowed: |
| 1841 | CHECK_NE(service_worker_state, PrefetchServiceWorkerState::kAllowed); |
| 1842 | service_worker_state_ = service_worker_state; |
| 1843 | break; |
| 1844 | case PrefetchServiceWorkerState::kControlled: |
| 1845 | NOTREACHED(); |
| 1846 | } |
| 1847 | } |
| 1848 | |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1849 | void PrefetchContainer::RecordPrefetchDurationHistogram() { |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1850 | if (!time_added_to_prefetch_service_.has_value()) { |
| 1851 | return; |
| 1852 | } |
| 1853 | |
| 1854 | if (!time_initial_eligibility_got_.has_value()) { |
| 1855 | return; |
| 1856 | } |
| 1857 | |
| 1858 | base::UmaHistogramTimes( |
| 1859 | base::StrCat({ |
| 1860 | "Prefetch.PrefetchContainer.AddedToInitialEligibility.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1861 | GetMetricsSuffix(), |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1862 | }), |
| 1863 | time_initial_eligibility_got_.value() - |
| 1864 | time_added_to_prefetch_service_.value()); |
| 1865 | |
| 1866 | if (!time_prefetch_started_.has_value()) { |
| 1867 | return; |
| 1868 | } |
| 1869 | |
| 1870 | base::UmaHistogramTimes( |
| 1871 | base::StrCat({ |
| 1872 | "Prefetch.PrefetchContainer.AddedToPrefetchStarted.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1873 | GetMetricsSuffix(), |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1874 | }), |
| 1875 | time_prefetch_started_.value() - time_added_to_prefetch_service_.value()); |
| 1876 | |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1877 | base::UmaHistogramTimes( |
| 1878 | base::StrCat({ |
| 1879 | "Prefetch.PrefetchContainer.InitialEligibilityToPrefetchStarted.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1880 | GetMetricsSuffix(), |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1881 | }), |
| 1882 | time_prefetch_started_.value() - time_initial_eligibility_got_.value()); |
| 1883 | |
Taiyo Mizuhashi | 98ecb38 | 2025-06-03 15:24:55 | [diff] [blame] | 1884 | if (!time_url_request_started_.has_value()) { |
| 1885 | return; |
| 1886 | } |
| 1887 | |
| 1888 | base::UmaHistogramTimes(base::StrCat({ |
| 1889 | "Prefetch.PrefetchContainer." |
| 1890 | "AddedToURLRequestStarted.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1891 | GetMetricsSuffix(), |
Taiyo Mizuhashi | 98ecb38 | 2025-06-03 15:24:55 | [diff] [blame] | 1892 | }), |
| 1893 | time_url_request_started_.value() - |
| 1894 | time_added_to_prefetch_service_.value()); |
| 1895 | |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1896 | base::UmaHistogramTimes( |
| 1897 | base::StrCat({ |
| 1898 | "Prefetch.PrefetchContainer.PrefetchStartedToURLRequestStarted.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1899 | GetMetricsSuffix(), |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1900 | }), |
| 1901 | time_url_request_started_.value() - time_prefetch_started_.value()); |
| 1902 | |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1903 | if (!time_header_determined_successfully_.has_value()) { |
| 1904 | return; |
| 1905 | } |
| 1906 | |
Taiyo Mizuhashi | a12acc4 | 2025-04-25 23:40:36 | [diff] [blame] | 1907 | base::UmaHistogramTimes(base::StrCat({ |
| 1908 | "Prefetch.PrefetchContainer." |
| 1909 | "AddedToHeaderDeterminedSuccessfully.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1910 | GetMetricsSuffix(), |
Taiyo Mizuhashi | a12acc4 | 2025-04-25 23:40:36 | [diff] [blame] | 1911 | }), |
| 1912 | time_header_determined_successfully_.value() - |
| 1913 | time_added_to_prefetch_service_.value()); |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1914 | |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1915 | base::UmaHistogramTimes(base::StrCat({ |
| 1916 | "Prefetch.PrefetchContainer." |
| 1917 | "PrefetchStartedToHeaderDeterminedSuccessfully.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1918 | GetMetricsSuffix(), |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1919 | }), |
| 1920 | time_header_determined_successfully_.value() - |
| 1921 | time_prefetch_started_.value()); |
| 1922 | |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1923 | if (!time_prefetch_completed_successfully_.has_value()) { |
| 1924 | return; |
| 1925 | } |
| 1926 | |
Taiyo Mizuhashi | a12acc4 | 2025-04-25 23:40:36 | [diff] [blame] | 1927 | base::UmaHistogramTimes(base::StrCat({ |
| 1928 | "Prefetch.PrefetchContainer." |
| 1929 | "AddedToPrefetchCompletedSuccessfully.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1930 | GetMetricsSuffix(), |
Taiyo Mizuhashi | a12acc4 | 2025-04-25 23:40:36 | [diff] [blame] | 1931 | }), |
| 1932 | time_prefetch_completed_successfully_.value() - |
| 1933 | time_added_to_prefetch_service_.value()); |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1934 | |
| 1935 | base::UmaHistogramTimes(base::StrCat({ |
| 1936 | "Prefetch.PrefetchContainer." |
| 1937 | "PrefetchStartedToPrefetchCompletedSuccessfully.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1938 | GetMetricsSuffix(), |
Taiyo Mizuhashi | 98061e2 | 2025-07-12 05:26:54 | [diff] [blame] | 1939 | }), |
| 1940 | time_prefetch_completed_successfully_.value() - |
| 1941 | time_prefetch_started_.value()); |
kenoss | eab3c42 | 2025-04-03 12:50:19 | [diff] [blame] | 1942 | } |
| 1943 | |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1944 | void PrefetchContainer::RecordPrefetchMatchingBlockedNavigationHistogram( |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1945 | bool blocked_until_head, |
| 1946 | bool is_nav_prerender) { |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1947 | base::UmaHistogramBoolean( |
| 1948 | base::StrCat( |
| 1949 | {"Prefetch.PrefetchMatchingBlockedNavigation.PerMatchingCandidate.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1950 | GetMetricsSuffix()}), |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1951 | blocked_until_head); |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1952 | base::UmaHistogramBoolean( |
| 1953 | base::StrCat( |
| 1954 | {"Prefetch.PrefetchMatchingBlockedNavigation.PerMatchingCandidate.", |
| 1955 | is_nav_prerender ? "Prerender." : "NonPrerender.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1956 | GetMetricsSuffix()}), |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1957 | blocked_until_head); |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1958 | } |
| 1959 | |
| 1960 | void PrefetchContainer::RecordBlockUntilHeadDurationHistogram( |
| 1961 | const std::optional<base::TimeDelta>& blocked_duration, |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1962 | bool served, |
| 1963 | bool is_nav_prerender) { |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1964 | base::UmaHistogramTimes( |
| 1965 | base::StrCat({"Prefetch.BlockUntilHeadDuration.PerMatchingCandidate.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1966 | served ? "Served." : "NotServed.", GetMetricsSuffix()}), |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1967 | blocked_duration.value_or(base::Seconds(0))); |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1968 | base::UmaHistogramTimes( |
| 1969 | base::StrCat({"Prefetch.BlockUntilHeadDuration.PerMatchingCandidate.", |
| 1970 | is_nav_prerender ? "Prerender." : "NonPrerender.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1971 | served ? "Served." : "NotServed.", GetMetricsSuffix()}), |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1972 | blocked_duration.value_or(base::Seconds(0))); |
Taiyo Mizuhashi | 43066071f | 2025-04-25 23:40:22 | [diff] [blame] | 1973 | } |
kenoss | 4bdf2d8 | 2025-05-27 01:58:45 | [diff] [blame] | 1974 | |
Taiyo Mizuhashi | 09f571f | 2025-08-04 16:05:54 | [diff] [blame] | 1975 | void PrefetchContainer::RecordPrefetchPotentialCandidateServingResultHistogram( |
| 1976 | PrefetchPotentialCandidateServingResult matching_result) { |
| 1977 | base::UmaHistogramEnumeration( |
| 1978 | base::StrCat({"Prefetch.PrefetchPotentialCandidateServingResult." |
| 1979 | "PerMatchingCandidate.", |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1980 | GetMetricsSuffix()}), |
Taiyo Mizuhashi | 09f571f | 2025-08-04 16:05:54 | [diff] [blame] | 1981 | matching_result); |
| 1982 | } |
| 1983 | |
Taiyo Mizuhashi | a009caf | 2025-08-05 07:34:22 | [diff] [blame] | 1984 | void PrefetchContainer::RecordPrefetchContainerServedCountHistogram() { |
| 1985 | base::UmaHistogramCounts100( |
Hiroshige Hayashizaki | 03a5905 | 2025-08-12 14:44:23 | [diff] [blame] | 1986 | base::StrCat( |
| 1987 | {"Prefetch.PrefetchContainer.ServedCount.", GetMetricsSuffix()}), |
Taiyo Mizuhashi | a009caf | 2025-08-05 07:34:22 | [diff] [blame] | 1988 | served_count_); |
| 1989 | } |
| 1990 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 1991 | } // namespace content |