Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [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_service.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 6 | |
Peter Kasting | 1557e5f | 2025-01-28 01:14:08 | [diff] [blame] | 7 | #include <algorithm> |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 8 | #include <memory> |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 9 | #include <optional> |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 10 | #include <string_view> |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 11 | #include <utility> |
Andrew Rayskiy | f6599036 | 2024-02-27 18:43:24 | [diff] [blame] | 12 | #include <vector> |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 13 | |
Jeremy Roman | 84e6e10 | 2023-08-15 16:06:14 | [diff] [blame] | 14 | #include "base/auto_reset.h" |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 15 | #include "base/barrier_closure.h" |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 16 | #include "base/check_is_test.h" |
Liviu Tinta | 1fe1a6d | 2023-09-20 19:44:04 | [diff] [blame] | 17 | #include "base/containers/fixed_flat_set.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 18 | #include "base/feature_list.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 19 | #include "base/location.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 20 | #include "base/memory/weak_ptr.h" |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 21 | #include "base/metrics/histogram_functions.h" |
| 22 | #include "base/metrics/histogram_macros.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 23 | #include "base/timer/timer.h" |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 24 | #include "content/browser/browser_context_impl.h" |
keno | ba0b85b | 2023-08-23 16:40:42 | [diff] [blame] | 25 | #include "content/browser/devtools/render_frame_devtools_agent_host.h" |
Kevin McNee | 7ca2e85 | 2024-04-30 02:38:31 | [diff] [blame] | 26 | #include "content/browser/preloading/prefetch/no_vary_search_helper.h" |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 27 | #include "content/browser/preloading/prefetch/prefetch_container.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 28 | #include "content/browser/preloading/prefetch/prefetch_document_manager.h" |
| 29 | #include "content/browser/preloading/prefetch/prefetch_features.h" |
Taiyo Mizuhashi | 94ce74f | 2025-01-28 04:20:14 | [diff] [blame] | 30 | #include "content/browser/preloading/prefetch/prefetch_handle_impl.h" |
Liviu Tinta | 4eaa53c2 | 2023-08-02 21:01:06 | [diff] [blame] | 31 | #include "content/browser/preloading/prefetch/prefetch_match_resolver.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 32 | #include "content/browser/preloading/prefetch/prefetch_network_context.h" |
Max Curran | 0146f85 | 2022-08-01 19:49:33 | [diff] [blame] | 33 | #include "content/browser/preloading/prefetch/prefetch_origin_prober.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 34 | #include "content/browser/preloading/prefetch/prefetch_params.h" |
| 35 | #include "content/browser/preloading/prefetch/prefetch_proxy_configurator.h" |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 36 | #include "content/browser/preloading/prefetch/prefetch_response_reader.h" |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 37 | #include "content/browser/preloading/prefetch/prefetch_scheduler.h" |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 38 | #include "content/browser/preloading/prefetch/prefetch_servable_state.h" |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 39 | #include "content/browser/preloading/prefetch/prefetch_serving_handle.h" |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 40 | #include "content/browser/preloading/prefetch/prefetch_status.h" |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 41 | #include "content/browser/preloading/prefetch/prefetch_streaming_url_loader.h" |
Liviu Tinta | d608e01 | 2023-05-10 19:16:41 | [diff] [blame] | 42 | #include "content/browser/preloading/preloading_attempt_impl.h" |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 43 | #include "content/browser/preloading/prerender/prerender_features.h" |
Peter Conn | 3816460 | 2025-08-06 14:41:19 | [diff] [blame] | 44 | #include "content/browser/preloading/proxy_lookup_client_impl.h" |
David Sanders | d467daa6 | 2025-06-24 23:01:33 | [diff] [blame] | 45 | #include "content/browser/renderer_host/render_frame_host_impl.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 46 | #include "content/public/browser/browser_context.h" |
Jeremy Roman | bae6a42 | 2022-05-17 22:41:17 | [diff] [blame] | 47 | #include "content/public/browser/content_browser_client.h" |
Max Curran | 3df677bf | 2022-09-12 19:47:07 | [diff] [blame] | 48 | #include "content/public/browser/frame_accept_header.h" |
David Sanders | d467daa6 | 2025-06-24 23:01:33 | [diff] [blame] | 49 | #include "content/public/browser/frame_tree_node_id.h" |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 50 | #include "content/public/browser/prefetch_service_delegate.h" |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 51 | #include "content/public/browser/preloading.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 52 | #include "content/public/browser/render_frame_host.h" |
| 53 | #include "content/public/browser/render_process_host.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 54 | #include "content/public/browser/service_worker_context.h" |
Patrick Monette | a4511134 | 2024-09-24 00:33:29 | [diff] [blame] | 55 | #include "content/public/browser/spare_render_process_host_manager.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 56 | #include "content/public/browser/storage_partition.h" |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 57 | #include "content/public/browser/url_loader_request_interceptor.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 58 | #include "content/public/browser/visibility.h" |
| 59 | #include "content/public/browser/web_contents.h" |
Jeremy Roman | bae6a42 | 2022-05-17 22:41:17 | [diff] [blame] | 60 | #include "content/public/common/content_client.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 61 | #include "content/public/common/content_constants.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 62 | #include "net/base/load_flags.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 63 | #include "net/base/url_util.h" |
| 64 | #include "net/cookies/canonical_cookie.h" |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 65 | #include "net/cookies/cookie_partition_key_collection.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 66 | #include "net/cookies/site_for_cookies.h" |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 67 | #include "net/http/http_no_vary_search_data.h" |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 68 | #include "net/http/http_request_headers.h" |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 69 | #include "net/http/http_status_code.h" |
| 70 | #include "net/http/http_util.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 71 | #include "net/traffic_annotation/network_traffic_annotation.h" |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 72 | #include "services/network/public/cpp/devtools_observer_util.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 73 | #include "services/network/public/cpp/is_potentially_trustworthy.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 74 | #include "services/network/public/cpp/resource_request.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 75 | #include "services/network/public/mojom/cookie_manager.mojom.h" |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 76 | #include "services/network/public/mojom/devtools_observer.mojom.h" |
William Liu | 2c82547 | 2022-10-31 12:01:44 | [diff] [blame] | 77 | #include "services/network/public/mojom/url_response_head.mojom-shared.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 78 | #include "services/network/public/mojom/url_response_head.mojom.h" |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 79 | #include "third_party/blink/public/common/loader/referrer_utils.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 80 | #include "url/gurl.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 81 | #include "url/origin.h" |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 82 | #include "url/url_constants.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 83 | |
| 84 | namespace content { |
| 85 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 86 | namespace { |
| 87 | |
| 88 | static ServiceWorkerContext* g_service_worker_context_for_testing = nullptr; |
| 89 | |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 90 | bool (*g_host_non_unique_filter)(std::string_view) = nullptr; |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 91 | |
Hiroshige Hayashizaki | e51e2f26 | 2025-03-04 23:14:38 | [diff] [blame] | 92 | static network::SharedURLLoaderFactory* g_url_loader_factory_for_testing = |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 93 | nullptr; |
| 94 | |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 95 | static network::mojom::NetworkContext* |
| 96 | g_network_context_for_proxy_lookup_for_testing = nullptr; |
| 97 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 98 | PrefetchService::DelayEligibilityCheckForTesting& |
| 99 | GetDelayEligibilityCheckForTesting() { |
| 100 | static base::NoDestructor<PrefetchService::DelayEligibilityCheckForTesting> |
| 101 | prefetch_delay_eligibility_check_for_testing; |
| 102 | return *prefetch_delay_eligibility_check_for_testing; |
| 103 | } |
| 104 | |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 105 | std::optional<PreloadingEligibility>& GetForceIneligibilityForTesting() { |
| 106 | static std::optional<PreloadingEligibility> |
| 107 | prefetch_force_ineligibility_for_testing; |
| 108 | return prefetch_force_ineligibility_for_testing; |
| 109 | } |
| 110 | |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 111 | bool ShouldConsiderDecoyRequestForStatus(PreloadingEligibility eligibility) { |
| 112 | switch (eligibility) { |
| 113 | case PreloadingEligibility::kUserHasCookies: |
| 114 | case PreloadingEligibility::kUserHasServiceWorker: |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 115 | case PreloadingEligibility::kUserHasServiceWorkerNoFetchHandler: |
| 116 | case PreloadingEligibility::kRedirectFromServiceWorker: |
| 117 | case PreloadingEligibility::kRedirectToServiceWorker: |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 118 | // If the prefetch is not eligible because of cookie or a service worker, |
| 119 | // then maybe send a decoy. |
| 120 | return true; |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 121 | case PreloadingEligibility::kBatterySaverEnabled: |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 122 | case PreloadingEligibility::kDataSaverEnabled: |
| 123 | case PreloadingEligibility::kExistingProxy: |
| 124 | case PreloadingEligibility::kHostIsNonUnique: |
| 125 | case PreloadingEligibility::kNonDefaultStoragePartition: |
| 126 | case PreloadingEligibility::kPrefetchProxyNotAvailable: |
| 127 | case PreloadingEligibility::kPreloadingDisabled: |
| 128 | case PreloadingEligibility::kRetryAfter: |
| 129 | case PreloadingEligibility::kSameSiteCrossOriginPrefetchRequiredProxy: |
| 130 | case PreloadingEligibility::kSchemeIsNotHttps: |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 131 | // These statuses don't relate to any user state, so don't send a decoy |
| 132 | // request. |
| 133 | return false; |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 134 | case PreloadingEligibility::kEligible: |
| 135 | default: |
| 136 | // Other ineligible cases are not used in `PrefetchService`. |
Peter Boström | fc7ddc18 | 2024-10-31 19:37:21 | [diff] [blame] | 137 | NOTREACHED(); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | bool ShouldStartSpareRenderer() { |
Max Curran | 94511aa | 2022-08-10 20:28:26 | [diff] [blame] | 142 | if (!PrefetchStartsSpareRenderer()) { |
| 143 | return false; |
| 144 | } |
| 145 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 146 | for (RenderProcessHost::iterator iter(RenderProcessHost::AllHostsIterator()); |
| 147 | !iter.IsAtEnd(); iter.Advance()) { |
| 148 | if (iter.GetCurrentValue()->IsUnused()) { |
| 149 | // There is already a spare renderer. |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | return true; |
| 154 | } |
| 155 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 156 | void RecordPrefetchProxyPrefetchMainframeTotalTime( |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 157 | network::mojom::URLResponseHead* head) { |
| 158 | DCHECK(head); |
| 159 | |
| 160 | base::Time start = head->request_time; |
| 161 | base::Time end = head->response_time; |
| 162 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 163 | if (start.is_null() || end.is_null()) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | UMA_HISTOGRAM_CUSTOM_TIMES("PrefetchProxy.Prefetch.Mainframe.TotalTime", |
| 168 | end - start, base::Milliseconds(10), |
| 169 | base::Seconds(30), 100); |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 170 | } |
| 171 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 172 | void RecordPrefetchProxyPrefetchMainframeConnectTime( |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 173 | network::mojom::URLResponseHead* head) { |
| 174 | DCHECK(head); |
| 175 | |
| 176 | base::TimeTicks start = head->load_timing.connect_timing.connect_start; |
| 177 | base::TimeTicks end = head->load_timing.connect_timing.connect_end; |
| 178 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 179 | if (start.is_null() || end.is_null()) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | UMA_HISTOGRAM_TIMES("PrefetchProxy.Prefetch.Mainframe.ConnectTime", |
| 184 | end - start); |
| 185 | } |
| 186 | |
| 187 | void RecordPrefetchProxyPrefetchMainframeRespCode(int response_code) { |
| 188 | base::UmaHistogramSparse("PrefetchProxy.Prefetch.Mainframe.RespCode", |
| 189 | response_code); |
| 190 | } |
| 191 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 192 | void RecordPrefetchProxyPrefetchMainframeCookiesToCopy( |
| 193 | size_t cookie_list_size) { |
| 194 | UMA_HISTOGRAM_COUNTS_100("PrefetchProxy.Prefetch.Mainframe.CookiesToCopy", |
| 195 | cookie_list_size); |
| 196 | } |
| 197 | |
| 198 | void CookieSetHelper(base::RepeatingClosure closure, |
| 199 | net::CookieAccessResult access_result) { |
| 200 | closure.Run(); |
| 201 | } |
| 202 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 203 | // Returns true if the prefetch is heldback, and set the holdback status |
| 204 | // correspondingly. |
| 205 | bool CheckAndSetPrefetchHoldbackStatus( |
| 206 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 207 | if (!prefetch_container->HasPreloadingAttempt()) { |
| 208 | return false; |
| 209 | } |
keno | ba0b85b | 2023-08-23 16:40:42 | [diff] [blame] | 210 | |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 211 | bool devtools_client_exist = [&] { |
| 212 | // Currently DevTools only supports when the prefetch is initiated by |
| 213 | // renderer. |
| 214 | if (!prefetch_container->IsRendererInitiated()) { |
| 215 | return false; |
| 216 | } |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 217 | RenderFrameHostImpl* initiator_rfh = RenderFrameHostImpl::FromID( |
| 218 | prefetch_container->GetReferringRenderFrameHostId()); |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 219 | return initiator_rfh && |
| 220 | RenderFrameDevToolsAgentHost::GetFor(initiator_rfh) != nullptr; |
| 221 | }(); |
| 222 | |
| 223 | // Normally PreloadingAttemptImpl::ShouldHoldback() eventually computes its |
kenoss | 9b82a0d | 2025-03-25 06:25:51 | [diff] [blame] | 224 | // `holdback_status_`, but we forcely set the status in some special cases |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 225 | // below, by calling PreloadingAttemptImpl::SetHoldbackStatus(). |
| 226 | // As its comment describes, this is expected to be called only once. |
kenoss | 9b82a0d | 2025-03-25 06:25:51 | [diff] [blame] | 227 | // |
| 228 | // Note that, alternatively, determining holdback status can be done in |
| 229 | // triggers, e.g. in `PreloadingAttemptImpl::ctor()`. For more details, see |
| 230 | // https://crbug.com/406123867 |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 231 | |
| 232 | if (devtools_client_exist) { |
| 233 | // 1. When developers debug Speculation Rules Prefetch using DevTools, |
| 234 | // always set status to kAllowed for developer experience. |
| 235 | prefetch_container->preloading_attempt()->SetHoldbackStatus( |
| 236 | PreloadingHoldbackStatus::kAllowed); |
kenoss | 9b82a0d | 2025-03-25 06:25:51 | [diff] [blame] | 237 | } else if (prefetch_container->IsLikelyAheadOfPrerender()) { |
| 238 | // 2. If PrefetchContainer is likely ahead of prerender, always set status |
| 239 | // to kAllowed as it is likely used for prerender. |
| 240 | // |
| 241 | // Note that we don't use `PrefetchContainer::overridden_holdback_status_` |
| 242 | // for this purpose because it can't handle a prefetch that was not ahead of |
| 243 | // prerender but another ahead of prerender one is migrated into it. We need |
| 244 | // to update migration if we'd like to do it. |
| 245 | prefetch_container->preloading_attempt()->SetHoldbackStatus( |
| 246 | PreloadingHoldbackStatus::kAllowed); |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 247 | } else if (prefetch_container->HasOverriddenHoldbackStatus()) { |
kenoss | 9b82a0d | 2025-03-25 06:25:51 | [diff] [blame] | 248 | // 3. If PrefetchContainer has custom overridden status, set that value. |
Taiyo Mizuhashi | 26f86c6b3 | 2024-10-02 03:58:46 | [diff] [blame] | 249 | prefetch_container->preloading_attempt()->SetHoldbackStatus( |
| 250 | prefetch_container->GetOverriddenHoldbackStatus()); |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 251 | } |
keno | ba0b85b | 2023-08-23 16:40:42 | [diff] [blame] | 252 | |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 253 | if (prefetch_container->preloading_attempt()->ShouldHoldback()) { |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 254 | prefetch_container->SetLoadState( |
| 255 | PrefetchContainer::LoadState::kFailedHeldback); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 256 | prefetch_container->SetPrefetchStatus(PrefetchStatus::kPrefetchHeldback); |
| 257 | return true; |
| 258 | } |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 259 | return false; |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 260 | } |
| 261 | |
Avi Drissman | dcc8e68 | 2024-09-04 14:14:48 | [diff] [blame] | 262 | BrowserContext* BrowserContextFromFrameTreeNodeId( |
| 263 | FrameTreeNodeId frame_tree_node_id) { |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 264 | WebContents* web_content = |
| 265 | WebContents::FromFrameTreeNodeId(frame_tree_node_id); |
| 266 | if (!web_content) { |
| 267 | return nullptr; |
| 268 | } |
| 269 | return web_content->GetBrowserContext(); |
| 270 | } |
| 271 | |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 272 | void RecordRedirectResult(PrefetchRedirectResult result) { |
| 273 | UMA_HISTOGRAM_ENUMERATION("PrefetchProxy.Redirect.Result", result); |
| 274 | } |
| 275 | |
| 276 | void RecordRedirectNetworkContextTransition( |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 277 | bool previous_requires_isolated_network_context, |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 278 | bool redirect_requires_isolated_network_context) { |
| 279 | PrefetchRedirectNetworkContextTransition transition; |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 280 | if (!previous_requires_isolated_network_context && |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 281 | !redirect_requires_isolated_network_context) { |
| 282 | transition = PrefetchRedirectNetworkContextTransition::kDefaultToDefault; |
| 283 | } |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 284 | if (!previous_requires_isolated_network_context && |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 285 | redirect_requires_isolated_network_context) { |
| 286 | transition = PrefetchRedirectNetworkContextTransition::kDefaultToIsolated; |
| 287 | } |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 288 | if (previous_requires_isolated_network_context && |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 289 | !redirect_requires_isolated_network_context) { |
| 290 | transition = PrefetchRedirectNetworkContextTransition::kIsolatedToDefault; |
| 291 | } |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 292 | if (previous_requires_isolated_network_context && |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 293 | redirect_requires_isolated_network_context) { |
| 294 | transition = PrefetchRedirectNetworkContextTransition::kIsolatedToIsolated; |
| 295 | } |
| 296 | |
| 297 | UMA_HISTOGRAM_ENUMERATION( |
| 298 | "PrefetchProxy.Redirect.NetworkContextStateTransition", transition); |
| 299 | } |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 300 | |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 301 | void OnIsolatedCookieCopyComplete(PrefetchServingHandle serving_handle) { |
| 302 | if (serving_handle) { |
| 303 | serving_handle.OnIsolatedCookieCopyComplete(); |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 304 | } |
| 305 | } |
| 306 | |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 307 | bool IsReferrerPolicySufficientlyStrict( |
| 308 | const network::mojom::ReferrerPolicy& referrer_policy) { |
| 309 | // https://github.com/WICG/nav-speculation/blob/main/prefetch.bs#L606 |
| 310 | // "", "`strict-origin-when-cross-origin`", "`strict-origin`", |
| 311 | // "`same-origin`", "`no-referrer`". |
| 312 | switch (referrer_policy) { |
| 313 | case network::mojom::ReferrerPolicy::kDefault: |
| 314 | case network::mojom::ReferrerPolicy::kStrictOriginWhenCrossOrigin: |
| 315 | case network::mojom::ReferrerPolicy::kSameOrigin: |
| 316 | case network::mojom::ReferrerPolicy::kStrictOrigin: |
| 317 | return true; |
| 318 | case network::mojom::ReferrerPolicy::kAlways: |
| 319 | case network::mojom::ReferrerPolicy::kNoReferrerWhenDowngrade: |
| 320 | case network::mojom::ReferrerPolicy::kNever: |
| 321 | case network::mojom::ReferrerPolicy::kOrigin: |
| 322 | case network::mojom::ReferrerPolicy::kOriginWhenCrossOrigin: |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 327 | } // namespace |
| 328 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 329 | // static |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 330 | PrefetchService* PrefetchService::GetFromFrameTreeNodeId( |
Avi Drissman | dcc8e68 | 2024-09-04 14:14:48 | [diff] [blame] | 331 | FrameTreeNodeId frame_tree_node_id) { |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 332 | BrowserContext* browser_context = |
| 333 | BrowserContextFromFrameTreeNodeId(frame_tree_node_id); |
| 334 | if (!browser_context) { |
| 335 | return nullptr; |
| 336 | } |
| 337 | return BrowserContextImpl::From(browser_context)->GetPrefetchService(); |
| 338 | } |
| 339 | |
| 340 | void PrefetchService::SetFromFrameTreeNodeIdForTesting( |
Avi Drissman | dcc8e68 | 2024-09-04 14:14:48 | [diff] [blame] | 341 | FrameTreeNodeId frame_tree_node_id, |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 342 | std::unique_ptr<PrefetchService> prefetch_service) { |
| 343 | BrowserContext* browser_context = |
| 344 | BrowserContextFromFrameTreeNodeId(frame_tree_node_id); |
| 345 | CHECK(browser_context); |
| 346 | return BrowserContextImpl::From(browser_context) |
| 347 | ->SetPrefetchServiceForTesting(std::move(prefetch_service)); // IN-TEST |
| 348 | } |
| 349 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 350 | PrefetchService::PrefetchService(BrowserContext* browser_context) |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 351 | : browser_context_(browser_context), |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 352 | delegate_(GetContentClient()->browser()->CreatePrefetchServiceDelegate( |
| 353 | browser_context_)), |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 354 | prefetch_proxy_configurator_( |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 355 | PrefetchProxyConfigurator::MaybeCreatePrefetchProxyConfigurator( |
| 356 | PrefetchProxyHost(delegate_ |
| 357 | ? delegate_->GetDefaultPrefetchProxyHost() |
| 358 | : GURL("")), |
Max Curran | 0146f85 | 2022-08-01 19:49:33 | [diff] [blame] | 359 | delegate_ ? delegate_->GetAPIKey() : "")), |
| 360 | origin_prober_(std::make_unique<PrefetchOriginProber>( |
| 361 | browser_context_, |
| 362 | PrefetchDNSCanaryCheckURL( |
| 363 | delegate_ ? delegate_->GetDefaultDNSCanaryCheckURL() : GURL("")), |
| 364 | PrefetchTLSCanaryCheckURL( |
| 365 | delegate_ ? delegate_->GetDefaultTLSCanaryCheckURL() |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 366 | : GURL("")))), |
| 367 | scheduler_(UsePrefetchScheduler() |
| 368 | ? std::make_unique<PrefetchScheduler>(this) |
| 369 | : nullptr) {} |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 370 | |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 371 | PrefetchService::~PrefetchService() { // NOLINT(modernize-use-equals-default) |
| 372 | // This is to avoid notifying `PrefetchService::OnWillBeDestroyed()` in the |
| 373 | // middle of `PrefetchService` destruction. |
| 374 | // This is for the future when we add some logic to `OnWillBeDestroyed()`. |
| 375 | // Currently `OnWillBeDestroyed()` is empty and thus this is no-op and we have |
| 376 | // to skip `modernize-use-equals-default` check. |
| 377 | for (const auto& iter : owned_prefetches()) { |
| 378 | if (iter.second) { |
| 379 | iter.second->RemoveObserver(this); |
| 380 | } |
| 381 | } |
| 382 | } |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 383 | |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 384 | void PrefetchService::SetPrefetchServiceDelegateForTesting( |
| 385 | std::unique_ptr<PrefetchServiceDelegate> delegate) { |
| 386 | DCHECK(!delegate_); |
| 387 | delegate_ = std::move(delegate); |
| 388 | } |
| 389 | |
Hiroshige Hayashizaki | 805be686 | 2023-03-28 08:16:22 | [diff] [blame] | 390 | PrefetchOriginProber* PrefetchService::GetPrefetchOriginProber() const { |
| 391 | return origin_prober_.get(); |
| 392 | } |
| 393 | |
Hiroshige Hayashizaki | 3148421 | 2024-01-30 13:35:21 | [diff] [blame] | 394 | void PrefetchService::AddPrefetchContainerWithoutStartingPrefetch( |
Hiroshige Hayashizaki | 7a34d018 | 2023-11-07 20:54:34 | [diff] [blame] | 395 | std::unique_ptr<PrefetchContainer> owned_prefetch_container) { |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 396 | RecordExistingPrefetchWithMatchingURL(*owned_prefetch_container); |
Hiroshige Hayashizaki | edf8ac3 | 2023-10-21 03:30:12 | [diff] [blame] | 397 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 398 | enum class Action { |
| 399 | kTakeOldWithMigration, |
| 400 | kReplaceOldWithNew, |
| 401 | kTakeNew, |
| 402 | }; |
| 403 | |
| 404 | // The comment below might be old. Currently, |
| 405 | // `PrefetchDocumentManager::PrefetchUrl()` reject colficting prefetches |
| 406 | // execpt for ahead of prerender case. |
| 407 | // |
| 408 | // TODO(crbug.com/371179869): Integrate these two processes. |
| 409 | // |
Hiroshige Hayashizaki | f59d902 | 2023-11-08 00:08:09 | [diff] [blame] | 410 | // A newly submitted prefetch could already be in |owned_prefetches_| if and |
Hiroshige Hayashizaki | edf8ac3 | 2023-10-21 03:30:12 | [diff] [blame] | 411 | // only if: |
| 412 | // 1) There was a same origin navigaition that used the same renderer. |
| 413 | // 2) Both pages requested a prefetch for the same URL. |
| 414 | // 3) The prefetch from the first page had at least started its network |
| 415 | // request (which would mean that it is in |owned_prefetches_| and owned |
| 416 | // by the prefetch service). |
| 417 | // If this happens, then we just delete the old prefetch and add the new |
Hiroshige Hayashizaki | f59d902 | 2023-11-08 00:08:09 | [diff] [blame] | 418 | // prefetch to |owned_prefetches_|. |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 419 | // |
| 420 | // Note that we might replace this by preserving existing prefetch and |
| 421 | // additional works, e.g. adding some properties to the old one and prolonging |
| 422 | // cacheable duration, to prevent additional fetch. See also |
| 423 | // https://chromium-review.googlesource.com/c/chromium/src/+/3880874/comment/5ecccbf7_8fbcba96/ |
| 424 | // |
| 425 | // TODO(crbug.com/372186548): Revisit the merging process and comments here |
| 426 | // and below. |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 427 | auto prefetch_iter = owned_prefetches().find(owned_prefetch_container->key()); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 428 | Action action = [&]() { |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 429 | if (prefetch_iter == owned_prefetches().end()) { |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 430 | return Action::kTakeNew; |
| 431 | } |
| 432 | PrefetchContainer& prefetch_container_old = *prefetch_iter->second; |
Hiroshige Hayashizaki | 7a34d018 | 2023-11-07 20:54:34 | [diff] [blame] | 433 | |
kenoss | c649294 | 2025-04-10 19:36:32 | [diff] [blame] | 434 | if (!features::UsePrefetchPrerenderIntegration()) { |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 435 | return Action::kReplaceOldWithNew; |
| 436 | } |
| 437 | |
| 438 | switch ( |
| 439 | prefetch_container_old.GetServableState(PrefetchCacheableDuration())) { |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 440 | case PrefetchServableState::kNotServable: |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 441 | return Action::kReplaceOldWithNew; |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 442 | case PrefetchServableState::kShouldBlockUntilEligibilityGot: |
| 443 | case PrefetchServableState::kShouldBlockUntilHeadReceived: |
| 444 | case PrefetchServableState::kServable: |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 445 | // nop |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | // Take preload pipeline info of prefetch ahead of prerender. |
| 450 | // |
| 451 | // Consider the following screnario (especially, in the same SpecRules and |
| 452 | // upgrading ): |
| 453 | // |
| 454 | // - A document adds SpecRules of prefetch A for URL X. |
| 455 | // - A document adds SpecRules of prerender B' for URL X. |
| 456 | // |
| 457 | // With `kPrerender2FallbackPrefetchSpecRules`, B' triggers prefetch ahead |
| 458 | // of prerender B for URL X. Sites use SpecRules A+B' with expectation |
| 459 | // "prefetch X then prerender X", but the order of |
Hiroshige Hayashizaki | 40a2532f | 2025-03-07 21:42:00 | [diff] [blame] | 460 | // `PrefetchService::AddPrefetchContainer*()` for A and B is unstable in |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 461 | // general. |
| 462 | // |
| 463 | // `PrerenderHost` of B' needs to know eligibility and status of B. We use |
| 464 | // `PreloadPipelineInfo` for this purpose. |
| 465 | // |
| 466 | // - If A is followed by B, take A and migrate B into A. A inherits |
| 467 | // `PreloadPipelineInfo` of B. |
| 468 | // - If B is followed by A, just reject A (by |
| 469 | // `PrefetchDocumentManager::PrefetchUrl()`). |
| 470 | // |
| 471 | // See also tests `PrerendererImplBrowserTestPrefetchAhead.*`. |
| 472 | |
| 473 | return Action::kTakeOldWithMigration; |
| 474 | }(); |
| 475 | |
| 476 | switch (action) { |
| 477 | case Action::kTakeOldWithMigration: |
| 478 | prefetch_iter->second->MigrateNewlyAdded( |
| 479 | std::move(owned_prefetch_container)); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 480 | if (UsePrefetchScheduler()) { |
| 481 | scheduler_->NotifyAttributeMightChangedAndProgressAsync( |
kenoss | e8d8bbb5 | 2025-05-13 12:35:04 | [diff] [blame] | 482 | *prefetch_iter->second, /*should_progress=*/false); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 483 | } |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 484 | break; |
| 485 | case Action::kReplaceOldWithNew: |
kenoss | e8d8bbb5 | 2025-05-13 12:35:04 | [diff] [blame] | 486 | ResetPrefetchContainer(prefetch_iter->second->GetWeakPtr(), |
| 487 | /*should_progress=*/false); |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 488 | AddPrefetchContainerToOwnedPrefetches( |
| 489 | std::move(owned_prefetch_container)); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 490 | break; |
| 491 | case Action::kTakeNew: |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 492 | AddPrefetchContainerToOwnedPrefetches( |
| 493 | std::move(owned_prefetch_container)); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 494 | break; |
| 495 | } |
Hiroshige Hayashizaki | 3148421 | 2024-01-30 13:35:21 | [diff] [blame] | 496 | } |
Hiroshige Hayashizaki | edf8ac3 | 2023-10-21 03:30:12 | [diff] [blame] | 497 | |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 498 | void PrefetchService::AddPrefetchContainerToOwnedPrefetches( |
| 499 | std::unique_ptr<PrefetchContainer> owned_prefetch_container) { |
| 500 | const base::WeakPtr<PrefetchContainer> prefetch_container = |
| 501 | owned_prefetch_container->GetWeakPtr(); |
| 502 | |
| 503 | // There should be no existing entry for `prefetch_container_key`. |
| 504 | CHECK(owned_prefetches_ |
| 505 | .emplace(prefetch_container->key(), |
| 506 | std::move(owned_prefetch_container)) |
| 507 | .second); |
| 508 | |
| 509 | prefetch_container->OnAddedToPrefetchService(); |
| 510 | |
| 511 | prefetch_container->AddObserver(this); |
| 512 | } |
| 513 | |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 514 | bool PrefetchService::IsPrefetchDuplicate( |
| 515 | GURL& url, |
| 516 | std::optional<net::HttpNoVarySearchData> no_vary_search_hint) { |
| 517 | TRACE_EVENT0("loading", "PrefetchService::IsPrefetchDuplicate"); |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 518 | for (const auto& [key, prefetch_container] : owned_prefetches()) { |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 519 | if (IsPrefetchStale(prefetch_container->GetWeakPtr())) { |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | // We will only compare the URLs if the no-vary-search hints match for |
| 524 | // determinism. This is because comparing URLs with different no-vary-search |
| 525 | // hints will change the outcome of the comparison based on the order the |
| 526 | // requests happened in. |
| 527 | // |
| 528 | // This approach optimizes for determinism over minimizing wasted |
| 529 | // or redundant prefetches. |
| 530 | bool nvs_hints_match = |
| 531 | no_vary_search_hint == prefetch_container->GetNoVarySearchHint(); |
| 532 | if (!nvs_hints_match) { |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | bool urls_equal; |
| 537 | if (no_vary_search_hint) { |
| 538 | urls_equal = no_vary_search_hint->AreEquivalent(url, key.url()); |
| 539 | } else { |
| 540 | // If there is no no-vary-search hint, just compare the URLs. |
| 541 | urls_equal = url == key.url(); |
| 542 | } |
| 543 | |
| 544 | if (!urls_equal) { |
| 545 | continue; |
| 546 | } |
| 547 | return true; |
| 548 | } |
| 549 | return false; |
| 550 | } |
| 551 | |
Hiroshige Hayashizaki | a129d91c | 2025-03-05 22:28:12 | [diff] [blame] | 552 | bool PrefetchService::IsPrefetchAttemptFailedOrDiscardedInternal( |
| 553 | base::PassKey<PrefetchDocumentManager>, |
| 554 | PrefetchContainer::Key key) const { |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 555 | auto it = owned_prefetches().find(key); |
| 556 | if (it == owned_prefetches().end() || !it->second) { |
Hiroshige Hayashizaki | a129d91c | 2025-03-05 22:28:12 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
| 560 | const std::unique_ptr<PrefetchContainer>& container = it->second; |
| 561 | if (!container->HasPrefetchStatus()) { |
| 562 | return false; // the container is not processed yet |
| 563 | } |
| 564 | |
| 565 | switch (container->GetPrefetchStatus()) { |
| 566 | case PrefetchStatus::kPrefetchSuccessful: |
| 567 | case PrefetchStatus::kPrefetchResponseUsed: |
| 568 | return false; |
| 569 | case PrefetchStatus::kPrefetchIneligibleUserHasCookies: |
| 570 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorker: |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 571 | case PrefetchStatus::kPrefetchIneligibleUserHasServiceWorkerNoFetchHandler: |
| 572 | case PrefetchStatus::kPrefetchIneligibleRedirectFromServiceWorker: |
| 573 | case PrefetchStatus::kPrefetchIneligibleRedirectToServiceWorker: |
Hiroshige Hayashizaki | a129d91c | 2025-03-05 22:28:12 | [diff] [blame] | 574 | case PrefetchStatus::kPrefetchIneligibleSchemeIsNotHttps: |
| 575 | case PrefetchStatus::kPrefetchIneligibleNonDefaultStoragePartition: |
| 576 | case PrefetchStatus::kPrefetchIneligibleRetryAfter: |
| 577 | case PrefetchStatus::kPrefetchIneligiblePrefetchProxyNotAvailable: |
| 578 | case PrefetchStatus::kPrefetchIneligibleHostIsNonUnique: |
| 579 | case PrefetchStatus::kPrefetchIneligibleDataSaverEnabled: |
| 580 | case PrefetchStatus::kPrefetchIneligibleBatterySaverEnabled: |
| 581 | case PrefetchStatus::kPrefetchIneligiblePreloadingDisabled: |
| 582 | case PrefetchStatus::kPrefetchIneligibleExistingProxy: |
| 583 | case PrefetchStatus::kPrefetchIsStale: |
| 584 | case PrefetchStatus::kPrefetchNotUsedProbeFailed: |
| 585 | case PrefetchStatus::kPrefetchNotStarted: |
| 586 | case PrefetchStatus::kPrefetchNotFinishedInTime: |
| 587 | case PrefetchStatus::kPrefetchFailedNetError: |
| 588 | case PrefetchStatus::kPrefetchFailedNon2XX: |
| 589 | case PrefetchStatus::kPrefetchFailedMIMENotSupported: |
| 590 | case PrefetchStatus::kPrefetchIsPrivacyDecoy: |
| 591 | case PrefetchStatus::kPrefetchNotUsedCookiesChanged: |
| 592 | case PrefetchStatus::kPrefetchHeldback: |
| 593 | case PrefetchStatus::kPrefetchFailedInvalidRedirect: |
| 594 | case PrefetchStatus::kPrefetchFailedIneligibleRedirect: |
| 595 | case PrefetchStatus:: |
| 596 | kPrefetchIneligibleSameSiteCrossOriginPrefetchRequiredProxy: |
| 597 | case PrefetchStatus::kPrefetchEvictedAfterCandidateRemoved: |
| 598 | case PrefetchStatus::kPrefetchEvictedForNewerPrefetch: |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 599 | case PrefetchStatus::kPrefetchEvictedAfterBrowsingDataRemoved: |
Hiroshige Hayashizaki | a129d91c | 2025-03-05 22:28:12 | [diff] [blame] | 600 | return true; |
| 601 | } |
| 602 | } |
| 603 | |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 604 | bool PrefetchService::IsPrefetchStale( |
| 605 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 606 | TRACE_EVENT0("loading", "PrefetchService::IsPrefetchStale"); |
| 607 | if (!prefetch_container) { |
| 608 | return true; |
| 609 | } |
| 610 | |
| 611 | // `PrefetchContainer::LoadState` check. |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 612 | switch (prefetch_container->GetLoadState()) { |
| 613 | case PrefetchContainer::LoadState::kFailedIneligible: |
| 614 | case PrefetchContainer::LoadState::kFailedHeldback: |
| 615 | return true; |
| 616 | case PrefetchContainer::LoadState::kNotStarted: |
| 617 | case PrefetchContainer::LoadState::kEligible: |
| 618 | case PrefetchContainer::LoadState::kStarted: |
| 619 | case PrefetchContainer::LoadState::kDeterminedHead: |
| 620 | case PrefetchContainer::LoadState::kCompletedOrFailed: |
| 621 | break; |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 622 | } |
| 623 | |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 624 | // `PrefetchServableState` check. |
| 625 | PrefetchServableState servable_state = |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 626 | prefetch_container->GetServableState(PrefetchCacheableDuration()); |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 627 | if (servable_state == PrefetchServableState::kNotServable) { |
Wayne Jackson Jr. | 0118d2a | 2025-02-21 10:53:46 | [diff] [blame] | 628 | return true; |
| 629 | } |
| 630 | return false; |
| 631 | } |
| 632 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 633 | // Parameter class used during eligibility check and `OnGotEligibility*` methods |
| 634 | // (`callback`). |
| 635 | struct PrefetchService::CheckEligibilityParams final { |
| 636 | void Finish(PreloadingEligibility eligibility) && { |
| 637 | // `callback_local` is needed to avoid use-after-move. |
| 638 | auto callback_local = std::move(callback); |
| 639 | std::move(callback_local).Run(std::move(*this), eligibility); |
| 640 | } |
| 641 | |
| 642 | // Returns if proxy is required for the next request. |
| 643 | bool IsProxyRequired() const { |
| 644 | CHECK(prefetch_container); |
| 645 | return prefetch_container->IsProxyRequiredForURL(url) && |
| 646 | !ShouldPrefetchBypassProxyForTestHost(url.host()); |
| 647 | } |
| 648 | |
| 649 | base::WeakPtr<PrefetchContainer> prefetch_container; |
| 650 | |
| 651 | // The URL of the next request. |
| 652 | GURL url; |
| 653 | |
| 654 | // Whether this is eligibility check for a redirect, or for an initial |
| 655 | // request. |
| 656 | bool is_redirect; |
| 657 | |
| 658 | // TODO(crbug.com/432783906): Add a `CHECK()` to ensure `callback` is always |
| 659 | // called. However, there are some cases where `callback` is not called (e.g. |
| 660 | // when `PrefetchService` is destroyed during eligibility check, which a valid |
| 661 | // exception, as well as other suspicious cases). |
| 662 | base::OnceCallback<void(CheckEligibilityParams, PreloadingEligibility)> |
| 663 | callback; |
| 664 | }; |
| 665 | |
Taiyo Mizuhashi | 94ce74f | 2025-01-28 04:20:14 | [diff] [blame] | 666 | std::unique_ptr<PrefetchHandle> PrefetchService::AddPrefetchContainerWithHandle( |
| 667 | std::unique_ptr<PrefetchContainer> owned_prefetch_container) { |
| 668 | base::WeakPtr<PrefetchContainer> prefetch_container = |
| 669 | owned_prefetch_container->GetWeakPtr(); |
Hiroshige Hayashizaki | 3148421 | 2024-01-30 13:35:21 | [diff] [blame] | 670 | AddPrefetchContainerWithoutStartingPrefetch( |
| 671 | std::move(owned_prefetch_container)); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 672 | |
Hiroshige Hayashizaki | 40a2532f | 2025-03-07 21:42:00 | [diff] [blame] | 673 | if (prefetch_container) { |
| 674 | PrefetchUrl(prefetch_container); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 675 | } |
Hiroshige Hayashizaki | 40a2532f | 2025-03-07 21:42:00 | [diff] [blame] | 676 | |
| 677 | return std::make_unique<PrefetchHandleImpl>(GetWeakPtr(), prefetch_container); |
Hiroshige Hayashizaki | 7a34d018 | 2023-11-07 20:54:34 | [diff] [blame] | 678 | } |
| 679 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 680 | void PrefetchService::AddPrefetchContainerWithoutStartingPrefetchForTesting( |
| 681 | std::unique_ptr<PrefetchContainer> prefetch_container) { |
| 682 | AddPrefetchContainerWithoutStartingPrefetch(std::move(prefetch_container)); |
| 683 | } |
| 684 | |
Hiroshige Hayashizaki | 7a34d018 | 2023-11-07 20:54:34 | [diff] [blame] | 685 | void PrefetchService::PrefetchUrl( |
| 686 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 687 | CHECK(prefetch_container); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 688 | TRACE_EVENT1("loading", "PrefetchService::PrefetchUrl", "prefetch_url", |
| 689 | prefetch_container->GetURL()); |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 690 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 691 | auto params = CheckEligibilityParams( |
| 692 | {.prefetch_container = prefetch_container, |
| 693 | .url = prefetch_container->GetURL(), |
| 694 | .is_redirect = false, |
| 695 | .callback = |
| 696 | base::BindOnce(&PrefetchService::OnGotEligibilityForNonRedirect, |
| 697 | weak_method_factory_.GetWeakPtr())}); |
| 698 | |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 699 | if (delegate_) { |
Hiroshige Hayashizaki | 84e9843 | 2025-07-23 01:06:47 | [diff] [blame] | 700 | const auto eligibility_from_delegate = delegate_->IsSomePreloadingEnabled(); |
Johann | 41a6083 | 2023-01-25 16:20:33 | [diff] [blame] | 701 | // If pre* actions are disabled then don't prefetch. |
Hiroshige Hayashizaki | 84e9843 | 2025-07-23 01:06:47 | [diff] [blame] | 702 | if (eligibility_from_delegate != PreloadingEligibility::kEligible) { |
| 703 | std::move(params).Finish(eligibility_from_delegate); |
| 704 | return; |
Johann | 41a6083 | 2023-01-25 16:20:33 | [diff] [blame] | 705 | } |
| 706 | |
Jeremy Roman | 95c2194d | 2022-11-30 17:20:25 | [diff] [blame] | 707 | const auto& prefetch_type = prefetch_container->GetPrefetchType(); |
Kouhei Ueno | f332bdf1 | 2023-11-10 06:25:37 | [diff] [blame] | 708 | if (prefetch_type.IsProxyRequiredWhenCrossOrigin()) { |
Jeremy Roman | 95c2194d | 2022-11-30 17:20:25 | [diff] [blame] | 709 | bool allow_all_domains = |
| 710 | PrefetchAllowAllDomains() || |
| 711 | (PrefetchAllowAllDomainsForExtendedPreloading() && |
| 712 | delegate_->IsExtendedPreloadingEnabled()); |
| 713 | if (!allow_all_domains && |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 714 | prefetch_container->GetReferringOrigin().has_value() && |
Jeremy Roman | 95c2194d | 2022-11-30 17:20:25 | [diff] [blame] | 715 | !delegate_->IsDomainInPrefetchAllowList( |
Taiyo Mizuhashi | 9dfeb63 | 2024-10-24 08:44:20 | [diff] [blame] | 716 | prefetch_container->GetReferringOrigin().value().GetURL())) { |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 717 | DVLOG(1) << *prefetch_container |
| 718 | << ": not prefetched (not in allow list)"; |
Jeremy Roman | 95c2194d | 2022-11-30 17:20:25 | [diff] [blame] | 719 | return; |
| 720 | } |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 721 | } |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 722 | |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 723 | // TODO(crbug.com/40946257): Current code doesn't support PageLoadMetrics |
| 724 | // when the prefetch is initiated by browser. |
| 725 | if (prefetch_container->IsRendererInitiated()) { |
| 726 | if (auto* rfh = RenderFrameHost::FromID( |
| 727 | prefetch_container->GetReferringRenderFrameHostId())) { |
| 728 | if (auto* web_contents = WebContents::FromRenderFrameHost(rfh)) { |
| 729 | delegate_->OnPrefetchLikely(web_contents); |
| 730 | } |
Taiyo Mizuhashi | 21a2a25 | 2024-03-19 05:40:58 | [diff] [blame] | 731 | } |
| 732 | } |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 733 | } |
| 734 | |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 735 | if (GetDelayEligibilityCheckForTesting()) { |
| 736 | GetDelayEligibilityCheckForTesting().Run( // IN-TEST |
| 737 | base::BindOnce(&PrefetchService::CheckEligibilityOfPrefetch, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 738 | weak_method_factory_.GetWeakPtr(), std::move(params))); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 739 | return; |
| 740 | } |
| 741 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 742 | CheckEligibilityOfPrefetch(std::move(params)); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | void PrefetchService::CheckEligibilityOfPrefetch( |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 746 | CheckEligibilityParams params) { |
| 747 | const auto prefetch_container = params.prefetch_container; |
Hiroshige Hayashizaki | f4fc2260 | 2025-07-23 01:01:02 | [diff] [blame] | 748 | if (!prefetch_container) { |
| 749 | // Test-only where the eligibility check is paused and resumed via |
| 750 | // `GetDelayEligibilityCheckForTesting()`. |
| 751 | std::move(params).Finish(PreloadingEligibility::kEligible); |
| 752 | return; |
| 753 | } |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 754 | CHECK(prefetch_container); |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 755 | TRACE_EVENT_BEGIN("loading", "PrefetchService::CheckEligibility", |
| 756 | perfetto::Track::FromPointer(this)); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 757 | |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 758 | // Inject failure in tests. |
| 759 | if (GetForceIneligibilityForTesting().has_value()) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 760 | std::move(params).Finish( |
| 761 | GetForceIneligibilityForTesting().value()); // IN-TEST |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 762 | return; |
| 763 | } |
| 764 | |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 765 | // TODO(crbug.com/40215782): Clean up the following checks by: 1) |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 766 | // moving each check to a separate function, and 2) requiring that failed |
| 767 | // checks provide a PrefetchStatus related to the check. |
| 768 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 769 | // While a registry-controlled domain could still resolve to a non-publicly |
| 770 | // routable IP, this allows hosts which are very unlikely to work via the |
| 771 | // proxy to be discarded immediately. |
Kouhei Ueno | 58165ea | 2023-11-13 03:41:07 | [diff] [blame] | 772 | // |
| 773 | // Conditions on the outer-most if block: |
| 774 | // Host-uniqueness check is only applied to proxied prefetches, where that |
| 775 | // matters. Also, we bypass the check for the test hosts, since we run the |
| 776 | // test web servers on the localhost or private networks, where the check |
| 777 | // fails. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 778 | if (params.IsProxyRequired()) { |
Kouhei Ueno | 58165ea | 2023-11-13 03:41:07 | [diff] [blame] | 779 | bool is_host_non_unique = |
| 780 | g_host_non_unique_filter |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 781 | ? g_host_non_unique_filter(params.url.HostNoBrackets()) |
| 782 | : net::IsHostnameNonUnique(params.url.HostNoBrackets()); |
Kouhei Ueno | 58165ea | 2023-11-13 03:41:07 | [diff] [blame] | 783 | if (is_host_non_unique) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 784 | std::move(params).Finish(PreloadingEligibility::kHostIsNonUnique); |
Kouhei Ueno | 58165ea | 2023-11-13 03:41:07 | [diff] [blame] | 785 | return; |
| 786 | } |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | // Only HTTP(S) URLs which are believed to be secure are eligible. |
| 790 | // For proxied prefetches, we only want HTTPS URLs. |
| 791 | // For non-proxied prefetches, other URLs (notably localhost HTTP) is also |
| 792 | // acceptable. This is common during development. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 793 | const bool is_secure_http = |
| 794 | params.IsProxyRequired() |
| 795 | ? params.url.SchemeIs(url::kHttpsScheme) |
| 796 | : (params.url.SchemeIsHTTPOrHTTPS() && |
| 797 | network::IsUrlPotentiallyTrustworthy(params.url)); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 798 | if (!is_secure_http) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 799 | std::move(params).Finish(PreloadingEligibility::kSchemeIsNotHttps); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 800 | return; |
| 801 | } |
| 802 | |
Kouhei Ueno | ea2fcff86 | 2023-11-15 09:08:45 | [diff] [blame] | 803 | // Fail the prefetch (or more precisely, PrefetchContainer::SinglePrefetch) |
| 804 | // early if it is going to go through a proxy, and we know that it is not |
| 805 | // available. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 806 | if (params.IsProxyRequired() && |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 807 | (!prefetch_proxy_configurator_ || |
| 808 | !prefetch_proxy_configurator_->IsPrefetchProxyAvailable())) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 809 | std::move(params).Finish(PreloadingEligibility::kPrefetchProxyNotAvailable); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 810 | return; |
| 811 | } |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 812 | |
| 813 | // Only the default storage partition is supported since that is where we |
| 814 | // check for service workers and existing cookies. |
| 815 | StoragePartition* default_storage_partition = |
| 816 | browser_context_->GetDefaultStoragePartition(); |
| 817 | if (default_storage_partition != |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 818 | browser_context_->GetStoragePartitionForUrl(params.url, |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 819 | /*can_create=*/false)) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 820 | std::move(params).Finish( |
| 821 | PreloadingEligibility::kNonDefaultStoragePartition); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 822 | return; |
| 823 | } |
| 824 | |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 825 | // If we have recently received a "retry-after" for the origin, then don't |
| 826 | // send new prefetches. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 827 | if (delegate_ && !delegate_->IsOriginOutsideRetryAfterWindow(params.url)) { |
| 828 | std::move(params).Finish(PreloadingEligibility::kRetryAfter); |
Max Curran | ead64a6 | 2022-06-22 01:10:52 | [diff] [blame] | 829 | return; |
| 830 | } |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 831 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 832 | CheckHasServiceWorker(std::move(params)); |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 833 | } |
| 834 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 835 | void PrefetchService::CheckHasServiceWorker(CheckEligibilityParams params) { |
| 836 | const auto prefetch_container = params.prefetch_container; |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 837 | CHECK(prefetch_container); |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 838 | TRACE_EVENT_BEGIN("loading", "PrefetchService::CheckHasServiceWorker", |
| 839 | perfetto::Track::FromPointer(this)); |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 840 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 841 | if (params.is_redirect) { |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 842 | switch (prefetch_container->service_worker_state()) { |
| 843 | case PrefetchServiceWorkerState::kDisallowed: |
| 844 | break; |
| 845 | |
| 846 | case PrefetchServiceWorkerState::kAllowed: |
| 847 | // Should have been transitioned out already. |
| 848 | NOTREACHED(); |
| 849 | |
| 850 | case PrefetchServiceWorkerState::kControlled: |
Hiroshige Hayashizaki | b4af4c2d | 2025-03-17 18:59:10 | [diff] [blame] | 851 | // Currently we disallow redirects from ServiceWorker-controlled |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 852 | // prefetches. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 853 | std::move(params).Finish( |
| 854 | PreloadingEligibility::kRedirectFromServiceWorker); |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 855 | return; |
| 856 | } |
| 857 | } else { |
| 858 | switch (prefetch_container->service_worker_state()) { |
| 859 | case PrefetchServiceWorkerState::kDisallowed: |
| 860 | break; |
| 861 | |
| 862 | case PrefetchServiceWorkerState::kAllowed: |
| 863 | // The controlling ServiceWorker will be checked by |
| 864 | // `ServiceWorkerMainResourceLoaderInterceptor` from |
| 865 | // `PrefetchStreamingURLLoader`, not here during eligibility check. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 866 | OnGotServiceWorkerResult(std::move(params), base::Time::Now(), |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 867 | ServiceWorkerCapability::NO_SERVICE_WORKER); |
| 868 | return; |
| 869 | |
| 870 | case PrefetchServiceWorkerState::kControlled: |
| 871 | NOTREACHED(); |
| 872 | } |
| 873 | } |
| 874 | |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 875 | // This service worker check assumes that the prefetch will only ever be |
| 876 | // performed in a first-party context (main frame prefetch). At the moment |
| 877 | // that is true but if it ever changes then the StorageKey will need to be |
| 878 | // constructed with the top-level site to ensure correct partitioning. |
| 879 | ServiceWorkerContext* service_worker_context = |
| 880 | g_service_worker_context_for_testing |
| 881 | ? g_service_worker_context_for_testing |
| 882 | : browser_context_->GetDefaultStoragePartition() |
| 883 | ->GetServiceWorkerContext(); |
Liviu Tinta | 9120230 | 2023-09-26 17:49:11 | [diff] [blame] | 884 | CHECK(service_worker_context); |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 885 | auto key = |
| 886 | blink::StorageKey::CreateFirstParty(url::Origin::Create(params.url)); |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 887 | // Check `MaybeHasRegistrationForStorageKey` first as it is much faster than |
| 888 | // calling `CheckHasServiceWorker`. |
Liviu Tinta | 9120230 | 2023-09-26 17:49:11 | [diff] [blame] | 889 | auto has_registration_for_storage_key = |
| 890 | service_worker_context->MaybeHasRegistrationForStorageKey(key); |
| 891 | if (prefetch_container->HasPreloadingAttempt()) { |
| 892 | auto* preloading_attempt = static_cast<PreloadingAttemptImpl*>( |
| 893 | prefetch_container->preloading_attempt().get()); |
| 894 | CHECK(preloading_attempt); |
| 895 | preloading_attempt->SetServiceWorkerRegisteredCheck( |
| 896 | has_registration_for_storage_key |
| 897 | ? PreloadingAttemptImpl::ServiceWorkerRegisteredCheck::kPath |
| 898 | : PreloadingAttemptImpl::ServiceWorkerRegisteredCheck::kOriginOnly); |
| 899 | } |
| 900 | if (!has_registration_for_storage_key) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 901 | OnGotServiceWorkerResult(std::move(params), base::Time::Now(), |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 902 | ServiceWorkerCapability::NO_SERVICE_WORKER); |
| 903 | return; |
| 904 | } |
Liviu Tinta | 9120230 | 2023-09-26 17:49:11 | [diff] [blame] | 905 | // Start recording here the start of the check for Service Worker registration |
| 906 | // for url. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 907 | // `url` is needed to avoid use-after-move. |
| 908 | const GURL url = params.url; |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 909 | service_worker_context->CheckHasServiceWorker( |
| 910 | url, key, |
| 911 | base::BindOnce(&PrefetchService::OnGotServiceWorkerResult, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 912 | weak_method_factory_.GetWeakPtr(), std::move(params), |
| 913 | base::Time::Now())); |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | void PrefetchService::OnGotServiceWorkerResult( |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 917 | CheckEligibilityParams params, |
Liviu Tinta | 9120230 | 2023-09-26 17:49:11 | [diff] [blame] | 918 | base::Time check_has_service_worker_start_time, |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 919 | ServiceWorkerCapability service_worker_capability) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 920 | const auto prefetch_container = params.prefetch_container; |
| 921 | |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 922 | // End "PrefetchService::CheckHasServiceWorker" trace event. |
| 923 | TRACE_EVENT_END("loading", perfetto::Track::FromPointer(this)); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 924 | TRACE_EVENT1("loading", "PrefetchService::OnGotServiceWorkerResult", |
| 925 | "prefetch_url", |
| 926 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Liviu Tinta | 8f750bf | 2023-10-13 21:49:22 | [diff] [blame] | 927 | if (!prefetch_container) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 928 | std::move(params).Finish(PreloadingEligibility::kEligible); |
Liviu Tinta | 8f750bf | 2023-10-13 21:49:22 | [diff] [blame] | 929 | return; |
| 930 | } |
| 931 | CHECK(prefetch_container); |
| 932 | if (prefetch_container->HasPreloadingAttempt()) { |
Liviu Tinta | 9120230 | 2023-09-26 17:49:11 | [diff] [blame] | 933 | const auto duration = |
| 934 | base::Time::Now() - check_has_service_worker_start_time; |
| 935 | auto* preloading_attempt = static_cast<PreloadingAttemptImpl*>( |
| 936 | prefetch_container->preloading_attempt().get()); |
| 937 | CHECK(preloading_attempt); |
| 938 | preloading_attempt->SetServiceWorkerRegisteredCheckDuration(duration); |
| 939 | } |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 940 | // Note that after ServiceWorker+Prefetch support is implemented, |
| 941 | // - For ServiceWorker-eligible prefetches, |
| 942 | // `ServiceWorkerCapability::NO_SERVICE_WORKER` is passed here and thus the |
| 943 | // ServiceWorker-related ineligibility values here are not used. |
| 944 | // - For ServiceWorker-ineligible prefetches (e.g. cross-site prefetches), |
| 945 | // they still goes through the checks below and the ServiceWorker-related |
| 946 | // ineligibility values here are still valid and used. |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 947 | switch (service_worker_capability) { |
| 948 | case ServiceWorkerCapability::NO_SERVICE_WORKER: |
Hiroshige Hayashizaki | 13aac4c | 2024-11-22 21:03:19 | [diff] [blame] | 949 | break; |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 950 | case ServiceWorkerCapability::SERVICE_WORKER_NO_FETCH_HANDLER: |
Hiroshige Hayashizaki | 13aac4c | 2024-11-22 21:03:19 | [diff] [blame] | 951 | if (base::FeatureList::IsEnabled( |
| 952 | features::kPrefetchServiceWorkerNoFetchHandlerFix)) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 953 | std::move(params).Finish( |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 954 | PreloadingEligibility::kUserHasServiceWorkerNoFetchHandler); |
Hiroshige Hayashizaki | 13aac4c | 2024-11-22 21:03:19 | [diff] [blame] | 955 | return; |
| 956 | } |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 957 | break; |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 958 | case ServiceWorkerCapability::SERVICE_WORKER_WITH_FETCH_HANDLER: { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 959 | std::move(params).Finish( |
| 960 | params.is_redirect ? PreloadingEligibility::kRedirectToServiceWorker |
| 961 | : PreloadingEligibility::kUserHasServiceWorker); |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 962 | return; |
Hiroshige Hayashizaki | 5fcc71e | 2025-03-06 21:50:31 | [diff] [blame] | 963 | } |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 964 | } |
Liviu Tinta | 631f96ef | 2023-10-03 00:05:16 | [diff] [blame] | 965 | // This blocks same-site cross-origin prefetches that require the prefetch |
| 966 | // proxy. Same-site prefetches are made using the default network context, and |
| 967 | // the prefetch request cannot be configured to use the proxy in that network |
| 968 | // context. |
Alison Gale | 770f3fc | 2024-04-27 00:39:58 | [diff] [blame] | 969 | // TODO(crbug.com/40265797): Allow same-site cross-origin prefetches |
Liviu Tinta | 631f96ef | 2023-10-03 00:05:16 | [diff] [blame] | 970 | // that require the prefetch proxy to be made. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 971 | if (params.IsProxyRequired() && |
Liviu Tinta | 631f96ef | 2023-10-03 00:05:16 | [diff] [blame] | 972 | !prefetch_container |
| 973 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 974 | std::move(params).Finish( |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 975 | PreloadingEligibility::kSameSiteCrossOriginPrefetchRequiredProxy); |
Liviu Tinta | 631f96ef | 2023-10-03 00:05:16 | [diff] [blame] | 976 | return; |
| 977 | } |
| 978 | // We do not need to check the cookies of prefetches that do not need an |
| 979 | // isolated network context. |
| 980 | if (!prefetch_container |
| 981 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 982 | std::move(params).Finish(PreloadingEligibility::kEligible); |
Liviu Tinta | 631f96ef | 2023-10-03 00:05:16 | [diff] [blame] | 983 | return; |
| 984 | } |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 985 | |
Liviu Tinta | f7d62a7 | 2023-08-29 21:48:39 | [diff] [blame] | 986 | StoragePartition* default_storage_partition = |
| 987 | browser_context_->GetDefaultStoragePartition(); |
| 988 | CHECK(default_storage_partition); |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 989 | TRACE_EVENT_BEGIN("loading", "PrefetchService::CheckCookies", |
| 990 | perfetto::Track::FromPointer(this)); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 991 | net::CookieOptions options = net::CookieOptions::MakeAllInclusive(); |
| 992 | options.set_return_excluded_cookies(); |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 993 | // `url` is needed to avoid use-after-move. |
| 994 | const GURL url = params.url; |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 995 | default_storage_partition->GetCookieManagerForBrowserProcess()->GetCookieList( |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 996 | url, options, net::CookiePartitionKeyCollection::Todo(), |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 997 | base::BindOnce(&PrefetchService::OnGotCookiesForEligibilityCheck, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 998 | weak_method_factory_.GetWeakPtr(), std::move(params))); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | void PrefetchService::OnGotCookiesForEligibilityCheck( |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1002 | CheckEligibilityParams params, |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1003 | const net::CookieAccessResultList& cookie_list, |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 1004 | const net::CookieAccessResultList& excluded_cookies) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1005 | const auto prefetch_container = params.prefetch_container; |
| 1006 | |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 1007 | // End "PrefetchService::CheckCookies" trace event. |
| 1008 | TRACE_EVENT_END("loading", perfetto::Track::FromPointer(this)); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1009 | TRACE_EVENT1("loading", "PrefetchService::OnGotCookiesForEligibilityCheck", |
| 1010 | "prefetch_url", |
| 1011 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1012 | if (!prefetch_container) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1013 | std::move(params).Finish(PreloadingEligibility::kEligible); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | if (!cookie_list.empty()) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1018 | std::move(params).Finish(PreloadingEligibility::kUserHasCookies); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1019 | return; |
| 1020 | } |
| 1021 | |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 1022 | if (base::FeatureList::IsEnabled( |
| 1023 | features::kPrefetchStateContaminationMitigation)) { |
| 1024 | // The cookie eligibility check just happened, and we might proceed anyway. |
| 1025 | // We might therefore need to delay further processing to the extent |
| 1026 | // required to obscure the outcome of this check from the current site. |
| 1027 | auto* initiator_rfh = RenderFrameHost::FromID( |
| 1028 | prefetch_container->GetReferringRenderFrameHostId()); |
Taiyo Mizuhashi | 1eaa2dab | 2025-03-18 16:08:41 | [diff] [blame] | 1029 | const bool is_contamination_exempt = [&] { |
| 1030 | if (!prefetch_container->IsRendererInitiated()) { |
| 1031 | // When browser-initiated prefetches, we can calculates prefetch's |
| 1032 | // contamination exemption from the referring origin. Currently CCT |
| 1033 | // prefetch is only the case hitting this, so the callee will check |
| 1034 | // whether it is behind the feature flag tentatively. |
| 1035 | // TODO(crbug.com/40946257): Migrate to use this in all cases. |
| 1036 | return delegate_ && |
| 1037 | prefetch_container->GetReferringOrigin().has_value() && |
| 1038 | delegate_->IsContaminationExemptPerOrigin( |
| 1039 | prefetch_container->GetReferringOrigin().value()); |
| 1040 | } else { |
| 1041 | return delegate_ && initiator_rfh && |
| 1042 | delegate_->IsContaminationExempt( |
| 1043 | initiator_rfh->GetLastCommittedURL()); |
| 1044 | } |
| 1045 | }(); |
| 1046 | |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 1047 | if (!is_contamination_exempt) { |
| 1048 | prefetch_container->MarkCrossSiteContaminated(); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1052 | // Cookies are tricky because cookies for different paths or a higher level |
| 1053 | // domain (e.g.: m.foo.com and foo.com) may not show up in |cookie_list|, but |
| 1054 | // they will show up in |excluded_cookies|. To check for any cookies for a |
| 1055 | // domain, compare the domains of the prefetched |url| and the domains of all |
| 1056 | // the returned cookies. |
| 1057 | bool excluded_cookie_has_tld = false; |
| 1058 | for (const auto& cookie_result : excluded_cookies) { |
| 1059 | if (cookie_result.cookie.IsExpired(base::Time::Now())) { |
| 1060 | // Expired cookies don't count. |
| 1061 | continue; |
| 1062 | } |
| 1063 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1064 | if (params.url.DomainIs(cookie_result.cookie.DomainWithoutDot())) { |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1065 | excluded_cookie_has_tld = true; |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (excluded_cookie_has_tld) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1071 | std::move(params).Finish(PreloadingEligibility::kUserHasCookies); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1072 | return; |
| 1073 | } |
| 1074 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1075 | StartProxyLookupCheck(std::move(params)); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1076 | } |
| 1077 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1078 | void PrefetchService::StartProxyLookupCheck(CheckEligibilityParams params) { |
| 1079 | const auto prefetch_container = params.prefetch_container; |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1080 | // Same origin prefetches (which use the default network context and cannot |
| 1081 | // use the prefetch proxy) can use the existing proxy settings. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 1082 | // TODO(crbug.com/40231580): Copy proxy settings over to the isolated |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1083 | // network context for the prefetch in order to allow non-private cross origin |
| 1084 | // prefetches to be made using the existing proxy settings. |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1085 | if (!prefetch_container |
| 1086 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1087 | std::move(params).Finish(PreloadingEligibility::kEligible); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1088 | return; |
| 1089 | } |
| 1090 | |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 1091 | TRACE_EVENT_BEGIN("loading", "PrefetchService::ProxyCheck", |
| 1092 | perfetto::Track::FromPointer(this)); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1093 | // Start proxy check for this prefetch, and give ownership of the |
| 1094 | // |ProxyLookupClientImpl| to |prefetch_container|. |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1095 | // `url` is needed to avoid use-after-move. |
| 1096 | const GURL url = params.url; |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1097 | prefetch_container->TakeProxyLookupClient( |
| 1098 | std::make_unique<ProxyLookupClientImpl>( |
Lingqi Chi | a8e39ed5 | 2024-08-29 05:20:37 | [diff] [blame] | 1099 | url, |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1100 | base::BindOnce(&PrefetchService::OnGotProxyLookupResult, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1101 | weak_method_factory_.GetWeakPtr(), std::move(params)), |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1102 | g_network_context_for_proxy_lookup_for_testing |
| 1103 | ? g_network_context_for_proxy_lookup_for_testing |
| 1104 | : browser_context_->GetDefaultStoragePartition() |
| 1105 | ->GetNetworkContext())); |
| 1106 | } |
| 1107 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1108 | void PrefetchService::OnGotProxyLookupResult(CheckEligibilityParams params, |
| 1109 | bool has_proxy) { |
| 1110 | const auto prefetch_container = params.prefetch_container; |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 1111 | // End "PrefetchService::ProxyCheck" trace event. |
| 1112 | TRACE_EVENT_END("loading", perfetto::Track::FromPointer(this)); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1113 | TRACE_EVENT1("loading", "PrefetchService::OnGotProxyLookupResult", |
| 1114 | "prefetch_url", |
| 1115 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1116 | if (!prefetch_container) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1117 | std::move(params).Finish(PreloadingEligibility::kEligible); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1118 | return; |
| 1119 | } |
| 1120 | |
| 1121 | prefetch_container->ReleaseProxyLookupClient(); |
| 1122 | if (has_proxy) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1123 | std::move(params).Finish(PreloadingEligibility::kExistingProxy); |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 1124 | return; |
| 1125 | } |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 1126 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1127 | std::move(params).Finish(PreloadingEligibility::kEligible); |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | void PrefetchService::OnGotEligibilityForNonRedirect( |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1131 | CheckEligibilityParams params, |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1132 | PreloadingEligibility eligibility) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1133 | const auto prefetch_container = params.prefetch_container; |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 1134 | // End "PrefetchService::CheckEligibility" trace event. |
| 1135 | TRACE_EVENT_END("loading", perfetto::Track::FromPointer(this)); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1136 | TRACE_EVENT1("loading", "PrefetchService::OnGotEligibilityForNonRedirect", |
| 1137 | "prefetch_url", |
| 1138 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1139 | if (!prefetch_container) { |
Melissa Zhang | 455fa15e | 2022-12-15 04:01:50 | [diff] [blame] | 1140 | return; |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1141 | } |
| 1142 | |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1143 | const bool eligible = eligibility == PreloadingEligibility::kEligible; |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1144 | bool is_decoy = false; |
| 1145 | if (!eligible) { |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1146 | is_decoy = |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1147 | params.IsProxyRequired() && |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1148 | ShouldConsiderDecoyRequestForStatus(eligibility) && |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1149 | PrefetchServiceSendDecoyRequestForIneligblePrefetch( |
| 1150 | delegate_ ? delegate_->DisableDecoysBasedOnUserSettings() : false); |
| 1151 | } |
| 1152 | // The prefetch decoy is pushed onto the queue and the network request will be |
| 1153 | // dispatched, but the response will not be used. Thus it is eligible but a |
| 1154 | // failure. |
| 1155 | prefetch_container->SetIsDecoy(is_decoy); |
| 1156 | if (is_decoy) { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1157 | prefetch_container->OnEligibilityCheckComplete( |
| 1158 | PreloadingEligibility::kEligible); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1159 | } else { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1160 | prefetch_container->OnEligibilityCheckComplete(eligibility); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | if (!eligible && !is_decoy) { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1164 | DVLOG(1) |
| 1165 | << *prefetch_container |
| 1166 | << ": not prefetched (not eligible nor decoy. PreloadingEligibility=" |
| 1167 | << static_cast<int>(eligibility) << ")"; |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1168 | return; |
| 1169 | } |
| 1170 | |
| 1171 | if (!is_decoy) { |
| 1172 | prefetch_container->SetPrefetchStatus(PrefetchStatus::kPrefetchNotStarted); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1173 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1174 | // Registers a cookie listener for this prefetch if it is using an isolated |
| 1175 | // network context. If the cookies in the default partition associated with |
| 1176 | // this URL change after this point, then the prefetched resources should |
| 1177 | // not be served. |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1178 | if (prefetch_container |
| 1179 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1180 | prefetch_container->RegisterCookieListener( |
Hiroshige Hayashizaki | eec97ed1 | 2023-05-26 06:38:32 | [diff] [blame] | 1181 | browser_context_->GetDefaultStoragePartition() |
| 1182 | ->GetCookieManagerForBrowserProcess()); |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1183 | } |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1184 | } |
Max Curran | 9e88b79 | 2023-05-11 22:12:58 | [diff] [blame] | 1185 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1186 | if (!UsePrefetchScheduler()) { |
| 1187 | prefetch_queue_.push_back(std::move(prefetch_container)); |
| 1188 | Prefetch(); |
| 1189 | } else { |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1190 | if (features::kPrefetchSchedulerProgressSyncBestEffort.Get()) { |
| 1191 | ScheduleAndProgress(std::move(prefetch_container)); |
| 1192 | } else { |
| 1193 | ScheduleAndProgressAsync(std::move(prefetch_container)); |
| 1194 | } |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1195 | } |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1196 | } |
| 1197 | |
kenoss | 0c7bbc7 | 2024-07-24 08:52:40 | [diff] [blame] | 1198 | void PrefetchService::OnGotEligibilityForRedirect( |
kenoss | f8d9f35a | 2024-06-25 07:05:37 | [diff] [blame] | 1199 | net::RedirectInfo redirect_info, |
Hiroshige Hayashizaki | 1ae6168b | 2023-06-13 18:15:42 | [diff] [blame] | 1200 | network::mojom::URLResponseHeadPtr redirect_head, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1201 | CheckEligibilityParams params, |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1202 | PreloadingEligibility eligibility) { |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1203 | const auto prefetch_container = params.prefetch_container; |
Etienne Pierre-doray | e0fb4b16 | 2025-08-07 16:44:48 | [diff] [blame] | 1204 | // End "PrefetchService::CheckEligibility" trace event. |
| 1205 | TRACE_EVENT_END("loading", perfetto::Track::FromPointer(this)); |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1206 | TRACE_EVENT1("loading", "PrefetchService::OnGotEligibilityForRedirect", |
| 1207 | "prefetch_url", |
| 1208 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1209 | if (!prefetch_container) { |
| 1210 | return; |
| 1211 | } |
| 1212 | |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1213 | const bool eligible = eligibility == PreloadingEligibility::kEligible; |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 1214 | RecordRedirectResult(eligible |
| 1215 | ? PrefetchRedirectResult::kSuccessRedirectFollowed |
| 1216 | : PrefetchRedirectResult::kFailedIneligible); |
| 1217 | |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1218 | // If the redirect is ineligible, the prefetch may change into a decoy. |
| 1219 | bool is_decoy = false; |
| 1220 | if (!eligible) { |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1221 | is_decoy = |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1222 | params.IsProxyRequired() && |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1223 | ShouldConsiderDecoyRequestForStatus(eligibility) && |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1224 | PrefetchServiceSendDecoyRequestForIneligblePrefetch( |
| 1225 | delegate_ ? delegate_->DisableDecoysBasedOnUserSettings() : false); |
| 1226 | } |
| 1227 | prefetch_container->SetIsDecoy(prefetch_container->IsDecoy() || is_decoy); |
| 1228 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1229 | // Inform the prefetch container of the result of the eligibility check |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1230 | if (prefetch_container->IsDecoy()) { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1231 | prefetch_container->OnEligibilityCheckComplete( |
| 1232 | PreloadingEligibility::kEligible); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1233 | } else { |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 1234 | prefetch_container->OnEligibilityCheckComplete(eligibility); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1235 | if (eligible && |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1236 | prefetch_container |
| 1237 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch()) { |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1238 | prefetch_container->RegisterCookieListener( |
Hiroshige Hayashizaki | eec97ed1 | 2023-05-26 06:38:32 | [diff] [blame] | 1239 | browser_context_->GetDefaultStoragePartition() |
| 1240 | ->GetCookieManagerForBrowserProcess()); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1244 | // TODO(crbug.com/396133768): Consider setting appropriate PrefetchStatus. |
| 1245 | auto streaming_url_loader = prefetch_container->GetStreamingURLLoader(); |
| 1246 | if (!streaming_url_loader) { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1247 | if (!UsePrefetchScheduler()) { |
| 1248 | if (active_prefetch_ == prefetch_container->key()) { |
| 1249 | active_prefetch_ = std::nullopt; |
| 1250 | Prefetch(); |
| 1251 | } |
| 1252 | } else { |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1253 | // TODO(crbug.com/400761083): Use |
| 1254 | // `ResetPrefetchContainerAndProgressAsync()` instead. |
| 1255 | RemoveFromSchedulerAndProgressAsync(*prefetch_container); |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1256 | } |
| 1257 | return; |
| 1258 | } |
| 1259 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1260 | // If the redirect is not eligible and the prefetch is not a decoy, then stop |
| 1261 | // the prefetch. |
| 1262 | if (!eligible && !prefetch_container->IsDecoy()) { |
kenoss | f7cea393 | 2025-03-28 03:47:37 | [diff] [blame] | 1263 | CHECK(IsPrefetchContainerInActiveSet(*prefetch_container)); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1264 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1265 | if (!UsePrefetchScheduler()) { |
| 1266 | active_prefetch_ = std::nullopt; |
| 1267 | streaming_url_loader->HandleRedirect(PrefetchRedirectStatus::kFail, |
| 1268 | redirect_info, |
| 1269 | std::move(redirect_head)); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1270 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1271 | Prefetch(); |
| 1272 | } else { |
| 1273 | // Remove first as it requires that `PrefetchContainer` is available. |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1274 | RemoveFromSchedulerAndProgressAsync(*prefetch_container); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1275 | |
| 1276 | streaming_url_loader->HandleRedirect(PrefetchRedirectStatus::kFail, |
| 1277 | redirect_info, |
| 1278 | std::move(redirect_head)); |
| 1279 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1280 | // TODO(crbug.com/400761083): Use |
| 1281 | // `ResetPrefetchContainerAndProgressAsync()` instead. |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1282 | } |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1283 | return; |
| 1284 | } |
| 1285 | |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1286 | prefetch_container->NotifyPrefetchRequestWillBeSent(&redirect_head); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1287 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1288 | // If the redirect requires a change in network contexts, then stop the |
| 1289 | // current streaming URL loader and start a new streaming URL loader for the |
| 1290 | // redirect URL. |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1291 | if (prefetch_container |
| 1292 | ->IsIsolatedNetworkContextRequiredForCurrentPrefetch() != |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1293 | prefetch_container |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1294 | ->IsIsolatedNetworkContextRequiredForPreviousRedirectHop()) { |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1295 | streaming_url_loader->HandleRedirect( |
Hiroshige Hayashizaki | ae8c560 | 2023-08-29 00:42:00 | [diff] [blame] | 1296 | PrefetchRedirectStatus::kSwitchNetworkContext, redirect_info, |
| 1297 | std::move(redirect_head)); |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 1298 | // The new ResponseReader is associated with the new streaming URL loader at |
| 1299 | // the PrefetchStreamingURLLoader constructor. |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1300 | SendPrefetchRequest(prefetch_container); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1301 | |
| 1302 | return; |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1303 | } |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1304 | |
| 1305 | // Otherwise, follow the redirect in the same streaming URL loader. |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1306 | streaming_url_loader->HandleRedirect(PrefetchRedirectStatus::kFollow, |
| 1307 | redirect_info, std::move(redirect_head)); |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 1308 | // Associate the new ResponseReader with the current streaming URL loader. |
Taiyo Mizuhashi | 08c47d1 | 2025-03-05 23:46:07 | [diff] [blame] | 1309 | streaming_url_loader->SetResponseReader( |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 1310 | prefetch_container->GetResponseReaderForCurrentPrefetch()); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1311 | } |
| 1312 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1313 | void PrefetchService::Prefetch() { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1314 | CHECK(!UsePrefetchScheduler()); |
| 1315 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1316 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 1317 | |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1318 | // Asserts that re-entrancy doesn't happen. |
| 1319 | #if DCHECK_IS_ON() |
| 1320 | DCHECK(!prefetch_reentrancy_guard_); |
Jeremy Roman | 84e6e10 | 2023-08-15 16:06:14 | [diff] [blame] | 1321 | base::AutoReset reset_guard(&prefetch_reentrancy_guard_, true); |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1322 | #endif |
| 1323 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1324 | if (PrefetchCloseIdleSockets()) { |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1325 | for (const auto& iter : owned_prefetches()) { |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1326 | if (iter.second) { |
| 1327 | iter.second->CloseIdleConnections(); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1328 | } |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | base::WeakPtr<PrefetchContainer> next_prefetch = nullptr; |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1333 | base::WeakPtr<PrefetchContainer> prefetch_to_evict = nullptr; |
| 1334 | while ((std::tie(next_prefetch, prefetch_to_evict) = |
| 1335 | PopNextPrefetchContainer()) != |
| 1336 | std::make_tuple(nullptr, nullptr)) { |
kenoss | 03cdaca3 | 2025-03-28 03:05:14 | [diff] [blame] | 1337 | if (prefetch_to_evict) { |
| 1338 | EvictPrefetch(std::move(prefetch_to_evict)); |
| 1339 | } |
| 1340 | StartSinglePrefetch(next_prefetch); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1341 | } |
| 1342 | } |
| 1343 | |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1344 | std::tuple<base::WeakPtr<PrefetchContainer>, base::WeakPtr<PrefetchContainer>> |
| 1345 | PrefetchService::PopNextPrefetchContainer() { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1346 | CHECK(!UsePrefetchScheduler()); |
| 1347 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1348 | auto new_end = std::remove_if( |
| 1349 | prefetch_queue_.begin(), prefetch_queue_.end(), |
Max Curran | 7c6210c8 | 2022-09-07 00:19:05 | [diff] [blame] | 1350 | [&](const base::WeakPtr<PrefetchContainer>& prefetch_container) { |
| 1351 | // Remove all prefetches from queue that no longer exist. |
keno | 8ae912c | 2023-04-28 06:01:54 | [diff] [blame] | 1352 | return !prefetch_container; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1353 | }); |
| 1354 | prefetch_queue_.erase(new_end, prefetch_queue_.end()); |
| 1355 | |
Adithya Srinivasan | 9d1b6fa | 2024-08-29 15:04:35 | [diff] [blame] | 1356 | // Don't start any new prefetches if we are currently running one. |
| 1357 | if (active_prefetch_.has_value()) { |
| 1358 | DVLOG(1) << "PrefetchService::PopNextPrefetchContainer: already running a " |
| 1359 | "prefetch."; |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1360 | return std::make_tuple(nullptr, nullptr); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1361 | } |
| 1362 | |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1363 | base::WeakPtr<PrefetchContainer> prefetch_to_evict; |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1364 | // Get the first prefetch can be prefetched currently. For the triggers |
| 1365 | // managed by PrefetchDocumentManager, this depends on the state of the |
| 1366 | // initiating document, and the number of completed prefetches (this can also |
| 1367 | // result in previously completed prefetches being evicted). |
Peter Kasting | 1557e5f | 2025-01-28 01:14:08 | [diff] [blame] | 1368 | auto prefetch_iter = std::ranges::find_if( |
Peter Kasting | 837e2ccb | 2022-09-07 14:13:17 | [diff] [blame] | 1369 | prefetch_queue_, |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1370 | [&](const base::WeakPtr<PrefetchContainer>& prefetch_container) { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1371 | // Keep this method as similar as much as possible to |
| 1372 | // `IsReadyToStartLoading` in |
| 1373 | // //content/browser/preloading/prefetch/prefetch_scheduler.cc. |
| 1374 | |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1375 | if (!prefetch_container->IsRendererInitiated()) { |
| 1376 | // TODO(crbug.com/40946257): Revisit the resource limits and |
| 1377 | // conditions for starting browser-initiated prefetch. |
| 1378 | return true; |
| 1379 | } |
| 1380 | |
| 1381 | auto* prefetch_document_manager = |
| 1382 | prefetch_container->GetPrefetchDocumentManager(); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1383 | // If there is no manager in renderer-initiated prefetch (can happen |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1384 | // only in tests), just bypass the check. |
| 1385 | if (!prefetch_document_manager) { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1386 | CHECK_IS_TEST(); |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1387 | return true; |
| 1388 | } |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1389 | bool can_prefetch_now = false; |
| 1390 | std::tie(can_prefetch_now, prefetch_to_evict) = |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1391 | prefetch_document_manager->CanPrefetchNow(prefetch_container.get()); |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1392 | // |prefetch_to_evict| should only be set if |can_prefetch_now| is true. |
| 1393 | DCHECK(!prefetch_to_evict || can_prefetch_now); |
| 1394 | return can_prefetch_now; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1395 | }); |
| 1396 | if (prefetch_iter == prefetch_queue_.end()) { |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1397 | return std::make_tuple(nullptr, nullptr); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | base::WeakPtr<PrefetchContainer> next_prefetch_container = *prefetch_iter; |
| 1401 | prefetch_queue_.erase(prefetch_iter); |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1402 | return std::make_tuple(next_prefetch_container, prefetch_to_evict); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1403 | } |
| 1404 | |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1405 | void PrefetchService::OnPrefetchTimeout( |
| 1406 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
Adithya Srinivasan | 006b365 | 2023-12-14 14:52:12 | [diff] [blame] | 1407 | prefetch_container->SetPrefetchStatus(PrefetchStatus::kPrefetchIsStale); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1408 | if (!UsePrefetchScheduler()) { |
| 1409 | ResetPrefetchContainer(prefetch_container); |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1410 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1411 | if (!active_prefetch_) { |
| 1412 | Prefetch(); |
| 1413 | } |
| 1414 | } else { |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1415 | ResetPrefetchContainerAndProgressAsync(std::move(prefetch_container)); |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 1416 | } |
| 1417 | } |
| 1418 | |
Taiyo Mizuhashi | 94ce74f | 2025-01-28 04:20:14 | [diff] [blame] | 1419 | void PrefetchService::MayReleasePrefetch( |
| 1420 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1421 | if (!prefetch_container) { |
| 1422 | return; |
| 1423 | } |
| 1424 | |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1425 | if (!base::Contains(owned_prefetches(), prefetch_container->key())) { |
Taiyo Mizuhashi | 94ce74f | 2025-01-28 04:20:14 | [diff] [blame] | 1426 | return; |
| 1427 | } |
| 1428 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1429 | if (!UsePrefetchScheduler()) { |
| 1430 | ResetPrefetchContainer(prefetch_container); |
Taiyo Mizuhashi | 4e624a2 | 2025-05-15 11:28:54 | [diff] [blame] | 1431 | if (base::FeatureList::IsEnabled( |
| 1432 | features::kPrefetchQueueingPartialFixWithoutScheduler) && |
| 1433 | !active_prefetch_) { |
| 1434 | base::SequencedTaskRunner::GetCurrentDefault()->PostTask( |
| 1435 | FROM_HERE, base::BindOnce(&PrefetchService::Prefetch, |
| 1436 | weak_method_factory_.GetWeakPtr())); |
| 1437 | } |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1438 | } else { |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1439 | ResetPrefetchContainerAndProgressAsync(std::move(prefetch_container)); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1440 | } |
Taiyo Mizuhashi | 94ce74f | 2025-01-28 04:20:14 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | void PrefetchService::ResetPrefetchContainer( |
kenoss | e8d8bbb5 | 2025-05-13 12:35:04 | [diff] [blame] | 1444 | base::WeakPtr<PrefetchContainer> prefetch_container, |
| 1445 | bool should_progress) { |
Hiroshige Hayashizaki | f59d902 | 2023-11-08 00:08:09 | [diff] [blame] | 1446 | CHECK(prefetch_container); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1447 | |
| 1448 | if (!UsePrefetchScheduler()) { |
| 1449 | if (active_prefetch_ == prefetch_container->key()) { |
| 1450 | active_prefetch_ = std::nullopt; |
| 1451 | } |
| 1452 | } else { |
| 1453 | // Remove before calling `PrefetchContainer::dtor()` as `PrefetchScheduler` |
| 1454 | // manages them with weak pointers. |
kenoss | e8d8bbb5 | 2025-05-13 12:35:04 | [diff] [blame] | 1455 | scheduler_->RemoveAndProgressAsync(*prefetch_container, should_progress); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1456 | } |
| 1457 | |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1458 | auto it = owned_prefetches().find(prefetch_container->key()); |
| 1459 | CHECK(it != owned_prefetches().end()); |
Hiroshige Hayashizaki | f59d902 | 2023-11-08 00:08:09 | [diff] [blame] | 1460 | CHECK_EQ(it->second.get(), prefetch_container.get()); |
Hiroshige Hayashizaki | f59d902 | 2023-11-08 00:08:09 | [diff] [blame] | 1461 | owned_prefetches_.erase(it); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1462 | } |
| 1463 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1464 | void PrefetchService::ScheduleAndProgress( |
| 1465 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1466 | CHECK(UsePrefetchScheduler()); |
| 1467 | CHECK(prefetch_container); |
| 1468 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1469 | scheduler_->PushAndProgress(*prefetch_container); |
| 1470 | } |
| 1471 | |
| 1472 | void PrefetchService::ScheduleAndProgressAsync( |
| 1473 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1474 | CHECK(UsePrefetchScheduler()); |
| 1475 | CHECK(prefetch_container); |
| 1476 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1477 | scheduler_->PushAndProgressAsync(*prefetch_container); |
| 1478 | |
| 1479 | // `PrefetchScheduler::Progress()` will be called asynchronously. |
| 1480 | } |
| 1481 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1482 | void PrefetchService::ResetPrefetchContainerAndProgressAsync( |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1483 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1484 | CHECK(UsePrefetchScheduler()); |
| 1485 | |
| 1486 | ResetPrefetchContainer(std::move(prefetch_container)); |
| 1487 | |
| 1488 | // `PrefetchScheduler::Progress()` will be called asynchronously. |
| 1489 | } |
| 1490 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1491 | void PrefetchService::ResetPrefetchContainersAndProgressAsync( |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1492 | std::vector<base::WeakPtr<PrefetchContainer>> prefetch_containers) { |
| 1493 | CHECK(UsePrefetchScheduler()); |
| 1494 | |
| 1495 | for (auto& prefetch_container : prefetch_containers) { |
| 1496 | ResetPrefetchContainer(std::move(prefetch_container)); |
kenoss | ea7ac6a2 | 2025-04-03 09:05:54 | [diff] [blame] | 1497 | } |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1498 | |
| 1499 | // `PrefetchScheduler::Progress()` will be called asynchronously. |
| 1500 | } |
| 1501 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1502 | void PrefetchService::RemoveFromSchedulerAndProgressAsync( |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1503 | PrefetchContainer& prefetch_container) { |
| 1504 | CHECK(UsePrefetchScheduler()); |
| 1505 | |
| 1506 | scheduler_->RemoveAndProgressAsync(prefetch_container); |
| 1507 | |
| 1508 | // `PrefetchScheduler::Progress()` will be called asynchronously. |
| 1509 | } |
| 1510 | |
| 1511 | void PrefetchService::OnCandidatesUpdated() { |
| 1512 | if (!UsePrefetchScheduler()) { |
| 1513 | if (!active_prefetch_) { |
| 1514 | Prefetch(); |
| 1515 | } |
| 1516 | } else { |
| 1517 | // Before `kPrefetchScheduler`, calling `Prefetch()` here was necessary to |
| 1518 | // progress scheduling as modifying `PrefetchService::queue_` doesn't set |
| 1519 | // `active_prefetch_`. |
| 1520 | // |
| 1521 | // After `kPrefetchScheduler`, `PrefetchScheduler` ensures that modifying |
| 1522 | // `PrefetchQueue` triggers `PrefetchScheduler::Progress()` eventually. So, |
| 1523 | // we believe that this explicit `Progress()` call is not necessary. But we |
| 1524 | // keep it because 1. It's safe (as it's not reentrancy) and noop if not |
| 1525 | // necessary. 2. We should another experiment to remove the call as we are |
| 1526 | // using `PrefetchScheduler` in some experiments. |
| 1527 | // |
| 1528 | // TODO(crbug.com/406754449): Consider to remove it. |
| 1529 | scheduler_->Progress(); |
| 1530 | } |
| 1531 | } |
| 1532 | |
| 1533 | void PrefetchService::EvictPrefetch( |
| 1534 | base::PassKey<PrefetchScheduler>, |
| 1535 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1536 | EvictPrefetch(std::move(prefetch_container)); |
kenoss | ea7ac6a2 | 2025-04-03 09:05:54 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | void PrefetchService::EvictPrefetch( |
kenoss | 03cdaca3 | 2025-03-28 03:05:14 | [diff] [blame] | 1540 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1541 | CHECK(prefetch_container); |
| 1542 | |
| 1543 | prefetch_container->SetPrefetchStatus( |
| 1544 | PrefetchStatus::kPrefetchEvictedForNewerPrefetch); |
| 1545 | ResetPrefetchContainer(std::move(prefetch_container)); |
| 1546 | } |
| 1547 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1548 | bool PrefetchService::StartSinglePrefetch( |
| 1549 | base::PassKey<PrefetchScheduler>, |
| 1550 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
| 1551 | return StartSinglePrefetch(std::move(prefetch_container)); |
| 1552 | } |
| 1553 | |
| 1554 | bool PrefetchService::StartSinglePrefetch( |
kenoss | 03cdaca3 | 2025-03-28 03:05:14 | [diff] [blame] | 1555 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1556 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 1557 | CHECK(prefetch_container); |
| 1558 | CHECK_EQ(prefetch_container->GetLoadState(), |
| 1559 | PrefetchContainer::LoadState::kEligible); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1560 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1561 | // Do not prefetch for a Holdback control group. Called after the checks in |
| 1562 | // `PopNextPrefetchContainer` because we want to compare against the |
| 1563 | // prefetches that would have been dispatched. |
| 1564 | if (CheckAndSetPrefetchHoldbackStatus(prefetch_container)) { |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1565 | DVLOG(1) << *prefetch_container |
| 1566 | << ": not prefetched (holdback control group)"; |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1567 | return false; |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 1568 | } |
| 1569 | |
Wayne Jackson Jr. | 03a15fa6 | 2024-09-16 14:42:15 | [diff] [blame] | 1570 | prefetch_container->OnPrefetchStarted(); |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 1571 | |
elabadysayed | f7d6b00f | 2025-02-05 11:27:00 | [diff] [blame] | 1572 | // Checks if the `PrefetchContainer` has a specific TTL (Time-to-Live) |
| 1573 | // configured. If a TTL is configured, the prefetch container will be eligible |
| 1574 | // for removal after the TTL expires. Otherwise, it will remain alive |
| 1575 | // indefinitely. |
| 1576 | // |
| 1577 | // The default TTL is determined by |
| 1578 | // `PrefetchContainerDefaultTtlInPrefetchService()`, which may return a zero |
| 1579 | // or negative value, indicating an indefinite TTL. |
| 1580 | prefetch_container->StartTimeoutTimerIfNeeded( |
| 1581 | base::BindOnce(&PrefetchService::OnPrefetchTimeout, |
| 1582 | weak_method_factory_.GetWeakPtr(), prefetch_container)); |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 1583 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1584 | if (!UsePrefetchScheduler()) { |
| 1585 | active_prefetch_.emplace(prefetch_container->key()); |
| 1586 | } |
keno | 8ae912c | 2023-04-28 06:01:54 | [diff] [blame] | 1587 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1588 | if (!prefetch_container->IsDecoy()) { |
| 1589 | // The status is updated to be successful or failed when it finishes. |
| 1590 | prefetch_container->SetPrefetchStatus( |
| 1591 | PrefetchStatus::kPrefetchNotFinishedInTime); |
| 1592 | } |
| 1593 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1594 | net::HttpRequestHeaders additional_headers; |
| 1595 | additional_headers.SetHeader( |
| 1596 | net::HttpRequestHeaders::kAccept, |
| 1597 | FrameAcceptHeaderValue(/*allow_sxg_responses=*/true, browser_context_)); |
| 1598 | prefetch_container->MakeResourceRequest(additional_headers); |
| 1599 | |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1600 | prefetch_container->NotifyPrefetchRequestWillBeSent( |
| 1601 | /*redirect_head=*/nullptr); |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1602 | |
| 1603 | SendPrefetchRequest(prefetch_container); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1604 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1605 | PrefetchDocumentManager* prefetch_document_manager = |
| 1606 | prefetch_container->GetPrefetchDocumentManager(); |
Max Curran | e6f63dca | 2023-05-17 01:16:22 | [diff] [blame] | 1607 | if (prefetch_container->GetPrefetchType().IsProxyRequiredWhenCrossOrigin() && |
| 1608 | !prefetch_container->IsDecoy() && |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1609 | (!prefetch_document_manager || |
| 1610 | !prefetch_document_manager->HaveCanaryChecksStarted())) { |
Taiyo Mizuhashi | 1b23ca6 | 2024-03-28 22:07:37 | [diff] [blame] | 1611 | // TODO(crbug.com/40946257): Currently browser-initiated prefetch will |
| 1612 | // always perform canary checks since there is no PrefetchDocumentManager. |
| 1613 | // Revisit and add proper handlings. |
| 1614 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1615 | // Make sure canary checks have run so we know the result by the time we |
| 1616 | // want to use the prefetch. Checking the canary cache can be a slow and |
| 1617 | // blocking operation (see crbug.com/1266018), so we only do this for the |
| 1618 | // first non-decoy prefetch we make on the page. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 1619 | // TODO(crbug.com/40801832): once this bug is fixed, fire off canary check |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1620 | // regardless of whether the request is a decoy or not. |
| 1621 | origin_prober_->RunCanaryChecksIfNeeded(); |
| 1622 | |
| 1623 | if (prefetch_document_manager) { |
| 1624 | prefetch_document_manager->OnCanaryChecksStarted(); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | // Start a spare renderer now so that it will be ready by the time it is |
| 1629 | // useful to have. |
| 1630 | if (ShouldStartSpareRenderer()) { |
Patrick Monette | a4511134 | 2024-09-24 00:33:29 | [diff] [blame] | 1631 | SpareRenderProcessHostManager::Get().WarmupSpare(browser_context_); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1632 | } |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1633 | |
| 1634 | return true; |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1635 | } |
| 1636 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1637 | void PrefetchService::SendPrefetchRequest( |
| 1638 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
Yoshisato Yanagisawa | d2c11f7 | 2025-02-03 10:49:35 | [diff] [blame] | 1639 | TRACE_EVENT("loading", "PrefetchService::SendPrefetchRequest"); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1640 | net::NetworkTrafficAnnotationTag traffic_annotation = |
| 1641 | net::DefineNetworkTrafficAnnotation("speculation_rules_prefetch", |
| 1642 | R"( |
| 1643 | semantics { |
| 1644 | sender: "Speculation Rules Prefetch Loader" |
| 1645 | description: |
| 1646 | "Prefetches the mainframe HTML of a page specified via " |
| 1647 | "speculation rules. This is done out-of-band of normal " |
| 1648 | "prefetches to allow total isolation of this request from the " |
| 1649 | "rest of browser traffic and user state like cookies and cache." |
| 1650 | trigger: |
| 1651 | "Used only when this feature and speculation rules feature are " |
| 1652 | "enabled." |
| 1653 | data: "None." |
| 1654 | destination: WEBSITE |
| 1655 | } |
| 1656 | policy { |
| 1657 | cookies_allowed: NO |
| 1658 | setting: |
| 1659 | "Users can control this via a setting specific to each content " |
| 1660 | "embedder." |
| 1661 | policy_exception_justification: "Not implemented." |
| 1662 | })"); |
| 1663 | |
Hiroshige Hayashizaki | c2560e6 | 2023-09-19 01:03:36 | [diff] [blame] | 1664 | auto streaming_loader = PrefetchStreamingURLLoader::CreateAndStart( |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 1665 | GetURLLoaderFactoryForCurrentPrefetch(prefetch_container), |
| 1666 | *prefetch_container->GetResourceRequest(), traffic_annotation, |
| 1667 | PrefetchTimeoutDuration(), |
Hiroshige Hayashizaki | 64b9182 | 2023-08-29 05:15:52 | [diff] [blame] | 1668 | base::BindOnce(&PrefetchService::OnPrefetchResponseStarted, |
| 1669 | base::Unretained(this), prefetch_container), |
Hiroshige Hayashizaki | 64b9182 | 2023-08-29 05:15:52 | [diff] [blame] | 1670 | base::BindRepeating(&PrefetchService::OnPrefetchRedirect, |
| 1671 | base::Unretained(this), prefetch_container), |
Hiroshige Hayashizaki | 0830e18 | 2025-03-14 02:03:06 | [diff] [blame] | 1672 | prefetch_container->GetResponseReaderForCurrentPrefetch(), |
| 1673 | prefetch_container->service_worker_state(), browser_context_, |
| 1674 | base::BindOnce(&PrefetchContainer::OnServiceWorkerStateDetermined, |
| 1675 | prefetch_container)); |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 1676 | prefetch_container->SetStreamingURLLoader(std::move(streaming_loader)); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1677 | |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1678 | DVLOG(1) << *prefetch_container << ": PrefetchStreamingURLLoader is created."; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1679 | } |
| 1680 | |
Hiroshige Hayashizaki | e51e2f26 | 2025-03-04 23:14:38 | [diff] [blame] | 1681 | scoped_refptr<network::SharedURLLoaderFactory> |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1682 | PrefetchService::GetURLLoaderFactoryForCurrentPrefetch( |
| 1683 | base::WeakPtr<PrefetchContainer> prefetch_container) { |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1684 | DCHECK(prefetch_container); |
| 1685 | if (g_url_loader_factory_for_testing) { |
Hiroshige Hayashizaki | e51e2f26 | 2025-03-04 23:14:38 | [diff] [blame] | 1686 | return base::WrapRefCounted(g_url_loader_factory_for_testing); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1687 | } |
Kouhei Ueno | 4442db9 | 2023-11-13 06:38:13 | [diff] [blame] | 1688 | return prefetch_container->GetOrCreateNetworkContextForCurrentPrefetch() |
| 1689 | ->GetURLLoaderFactory(this); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1690 | } |
| 1691 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1692 | void PrefetchService::OnPrefetchRedirect( |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1693 | base::WeakPtr<PrefetchContainer> prefetch_container, |
| 1694 | const net::RedirectInfo& redirect_info, |
Hiroshige Hayashizaki | 1ae6168b | 2023-06-13 18:15:42 | [diff] [blame] | 1695 | network::mojom::URLResponseHeadPtr redirect_head) { |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1696 | TRACE_EVENT1("loading", "PrefetchService::OnPrefetchRedirect", "prefetch_url", |
| 1697 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1698 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1699 | |
Max Curran | d7c0569 | 2023-03-30 19:33:12 | [diff] [blame] | 1700 | if (!prefetch_container) { |
Max Curran | 7d2578b | 2023-04-12 19:19:28 | [diff] [blame] | 1701 | RecordRedirectResult(PrefetchRedirectResult::kFailedNullPrefetch); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1702 | return; |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1703 | } |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1704 | |
kenoss | f7cea393 | 2025-03-28 03:47:37 | [diff] [blame] | 1705 | CHECK(IsPrefetchContainerInActiveSet(*prefetch_container)); |
Max Curran | e044494 | 2022-09-06 23:55:23 | [diff] [blame] | 1706 | |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 1707 | // Update the prefetch's referrer in case a redirect requires a change in |
| 1708 | // network context and a new request needs to be started. |
Hiroshige Hayashizaki | 1919c7c | 2023-10-13 21:46:42 | [diff] [blame] | 1709 | const auto new_referrer_policy = |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 1710 | blink::ReferrerUtils::NetToMojoReferrerPolicy( |
Hiroshige Hayashizaki | 1919c7c | 2023-10-13 21:46:42 | [diff] [blame] | 1711 | redirect_info.new_referrer_policy); |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 1712 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1713 | std::optional<PrefetchRedirectResult> failure; |
Jeremy Roman | 337e495 | 2024-07-04 20:36:49 | [diff] [blame] | 1714 | if (redirect_info.new_method != "GET") { |
Hiroshige Hayashizaki | 1ae6168b | 2023-06-13 18:15:42 | [diff] [blame] | 1715 | failure = PrefetchRedirectResult::kFailedInvalidMethod; |
| 1716 | } else if (!redirect_head->headers || |
| 1717 | redirect_head->headers->response_code() < 300 || |
| 1718 | redirect_head->headers->response_code() >= 400) { |
| 1719 | failure = PrefetchRedirectResult::kFailedInvalidResponseCode; |
Charlie Harrison | 15329a92 | 2025-03-07 14:36:18 | [diff] [blame] | 1720 | } else if (!net::SchemefulSite::IsSameSite( |
| 1721 | prefetch_container->GetCurrentURL(), redirect_info.new_url) && |
Hiroshige Hayashizaki | 1919c7c | 2023-10-13 21:46:42 | [diff] [blame] | 1722 | !IsReferrerPolicySufficientlyStrict(new_referrer_policy)) { |
| 1723 | // The new referrer policy is not sufficiently strict to allow cross-site |
| 1724 | // redirects. |
Hiroshige Hayashizaki | 1ae6168b | 2023-06-13 18:15:42 | [diff] [blame] | 1725 | failure = PrefetchRedirectResult::kFailedInsufficientReferrerPolicy; |
| 1726 | } |
| 1727 | |
| 1728 | if (failure) { |
kenoss | f7cea393 | 2025-03-28 03:47:37 | [diff] [blame] | 1729 | CHECK(IsPrefetchContainerInActiveSet(*prefetch_container)); |
Igor Kraskevich | dd52f57 | 2025-04-03 11:02:41 | [diff] [blame] | 1730 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1731 | if (!UsePrefetchScheduler()) { |
| 1732 | active_prefetch_ = std::nullopt; |
| 1733 | prefetch_container->SetPrefetchStatus( |
| 1734 | PrefetchStatus::kPrefetchFailedInvalidRedirect); |
| 1735 | if (auto streaming_url_loader = |
| 1736 | prefetch_container->GetStreamingURLLoader()) { |
| 1737 | streaming_url_loader->HandleRedirect(PrefetchRedirectStatus::kFail, |
| 1738 | redirect_info, |
| 1739 | std::move(redirect_head)); |
| 1740 | } |
| 1741 | |
| 1742 | Prefetch(); |
| 1743 | RecordRedirectResult(*failure); |
| 1744 | } else { |
| 1745 | RecordRedirectResult(*failure); |
| 1746 | |
| 1747 | prefetch_container->SetPrefetchStatus( |
| 1748 | PrefetchStatus::kPrefetchFailedInvalidRedirect); |
| 1749 | |
| 1750 | // Remove first as it requires that `PrefetchContainer` is available. |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1751 | RemoveFromSchedulerAndProgressAsync(*prefetch_container); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1752 | |
| 1753 | if (auto streaming_url_loader = |
| 1754 | prefetch_container->GetStreamingURLLoader()) { |
| 1755 | streaming_url_loader->HandleRedirect(PrefetchRedirectStatus::kFail, |
| 1756 | redirect_info, |
| 1757 | std::move(redirect_head)); |
| 1758 | } |
| 1759 | |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 1760 | // TODO(crbug.com/400761083): Use |
| 1761 | // `ResetPrefetchContainerAndProgressAsync()` instead. |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1762 | } |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1763 | return; |
Iman Saboori | 65c356d | 2022-07-15 14:47:47 | [diff] [blame] | 1764 | } |
| 1765 | |
Hiroshige Hayashizaki | 1919c7c | 2023-10-13 21:46:42 | [diff] [blame] | 1766 | prefetch_container->AddRedirectHop(redirect_info); |
| 1767 | prefetch_container->UpdateReferrer(GURL(redirect_info.new_referrer), |
| 1768 | new_referrer_policy); |
| 1769 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 1770 | RecordRedirectNetworkContextTransition( |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 1771 | prefetch_container |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 1772 | ->IsIsolatedNetworkContextRequiredForPreviousRedirectHop(), |
| 1773 | prefetch_container->IsIsolatedNetworkContextRequiredForCurrentPrefetch()); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 1774 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1775 | auto params = CheckEligibilityParams( |
| 1776 | {.prefetch_container = prefetch_container, |
| 1777 | .url = redirect_info.new_url, |
| 1778 | .is_redirect = true, |
| 1779 | .callback = base::BindOnce(&PrefetchService::OnGotEligibilityForRedirect, |
| 1780 | weak_method_factory_.GetWeakPtr(), |
| 1781 | redirect_info, std::move(redirect_head))}); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1782 | |
| 1783 | if (GetDelayEligibilityCheckForTesting()) { |
| 1784 | GetDelayEligibilityCheckForTesting().Run( // IN-TEST |
| 1785 | base::BindOnce(&PrefetchService::CheckEligibilityOfPrefetch, |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1786 | weak_method_factory_.GetWeakPtr(), std::move(params))); |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 1787 | return; |
| 1788 | } |
| 1789 | |
Hiroshige Hayashizaki | 692e3eb | 2025-07-18 21:03:49 | [diff] [blame] | 1790 | CheckEligibilityOfPrefetch(std::move(params)); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 1791 | } |
| 1792 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1793 | std::optional<PrefetchErrorOnResponseReceived> |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1794 | PrefetchService::OnPrefetchResponseStarted( |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1795 | base::WeakPtr<PrefetchContainer> prefetch_container, |
| 1796 | network::mojom::URLResponseHead* head) { |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1797 | TRACE_EVENT1("loading", "PrefetchService::OnPrefetchResponseStarted", |
| 1798 | "prefetch_url", |
| 1799 | prefetch_container ? prefetch_container->GetURL().spec() : ""); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1800 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1801 | |
| 1802 | if (!prefetch_container || prefetch_container->IsDecoy()) { |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1803 | return PrefetchErrorOnResponseReceived::kPrefetchWasDecoy; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1804 | } |
| 1805 | |
| 1806 | if (!head) { |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1807 | return PrefetchErrorOnResponseReceived::kFailedInvalidHead; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1808 | } |
| 1809 | |
Jeremy Roman | e561b41 | 2024-02-15 18:31:34 | [diff] [blame] | 1810 | if (prefetch_container && prefetch_container->IsCrossSiteContaminated()) { |
| 1811 | head->is_prefetch_with_cross_site_contamination = true; |
| 1812 | } |
| 1813 | |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 1814 | prefetch_container->NotifyPrefetchResponseReceived(*head); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1815 | |
| 1816 | if (!head->headers) { |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1817 | return PrefetchErrorOnResponseReceived::kFailedInvalidHeaders; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1818 | } |
| 1819 | |
| 1820 | RecordPrefetchProxyPrefetchMainframeTotalTime(head); |
| 1821 | RecordPrefetchProxyPrefetchMainframeConnectTime(head); |
| 1822 | |
| 1823 | int response_code = head->headers->response_code(); |
| 1824 | RecordPrefetchProxyPrefetchMainframeRespCode(response_code); |
| 1825 | if (response_code < 200 || response_code >= 300) { |
| 1826 | prefetch_container->SetPrefetchStatus( |
| 1827 | PrefetchStatus::kPrefetchFailedNon2XX); |
| 1828 | |
| 1829 | if (response_code == net::HTTP_SERVICE_UNAVAILABLE) { |
| 1830 | base::TimeDelta retry_after; |
| 1831 | std::string retry_after_string; |
| 1832 | if (head->headers->EnumerateHeader(nullptr, "Retry-After", |
| 1833 | &retry_after_string) && |
| 1834 | net::HttpUtil::ParseRetryAfterHeader( |
| 1835 | retry_after_string, base::Time::Now(), &retry_after) && |
| 1836 | delegate_) { |
Max Curran | 14dd787 | 2023-03-20 19:57:36 | [diff] [blame] | 1837 | // Cap the retry after value to a maximum. |
Kouhei Ueno | 5ad2196 | 2023-11-16 04:38:54 | [diff] [blame] | 1838 | static constexpr base::TimeDelta max_retry_after = base::Days(7); |
Kouhei Ueno | 824da49 | 2023-11-16 02:09:20 | [diff] [blame] | 1839 | if (retry_after > max_retry_after) { |
| 1840 | retry_after = max_retry_after; |
Max Curran | 14dd787 | 2023-03-20 19:57:36 | [diff] [blame] | 1841 | } |
| 1842 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1843 | delegate_->ReportOriginRetryAfter(prefetch_container->GetURL(), |
| 1844 | retry_after); |
| 1845 | } |
| 1846 | } |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1847 | return PrefetchErrorOnResponseReceived::kFailedNon2XX; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1848 | } |
| 1849 | |
| 1850 | if (PrefetchServiceHTMLOnly() && head->mime_type != "text/html") { |
| 1851 | prefetch_container->SetPrefetchStatus( |
| 1852 | PrefetchStatus::kPrefetchFailedMIMENotSupported); |
Hiroshige Hayashizaki | 556ac55 | 2023-10-24 01:36:32 | [diff] [blame] | 1853 | return PrefetchErrorOnResponseReceived::kFailedMIMENotSupported; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1854 | } |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 1855 | return std::nullopt; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1856 | } |
| 1857 | |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 1858 | void PrefetchService::OnWillBeDestroyed(PrefetchContainer& prefetch_container) { |
| 1859 | } |
| 1860 | |
| 1861 | void PrefetchService::OnGotInitialEligibility( |
| 1862 | PrefetchContainer& prefetch_container, |
| 1863 | PreloadingEligibility eligibility) {} |
| 1864 | |
| 1865 | void PrefetchService::OnDeterminedHead(PrefetchContainer& prefetch_container) {} |
| 1866 | |
| 1867 | void PrefetchService::OnPrefetchCompletedOrFailed( |
| 1868 | PrefetchContainer& prefetch_container, |
| 1869 | const network::URLLoaderCompletionStatus& completion_status, |
Hiroshige Hayashizaki | 12ab6d7 | 2025-07-15 00:27:45 | [diff] [blame] | 1870 | const std::optional<int>& response_code) { |
| 1871 | TRACE_EVENT2("loading", "PrefetchService::OnPrefetchCompletedOrFailed", |
| 1872 | "prefetch_url", prefetch_container.GetURL().spec(), |
Taiyo Mizuhashi | 3697ede | 2025-04-01 01:34:20 | [diff] [blame] | 1873 | "completion_status.error_code", completion_status.error_code); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1874 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
Hiroshige Hayashizaki | 12ab6d7 | 2025-07-15 00:27:45 | [diff] [blame] | 1875 | CHECK(IsPrefetchContainerInActiveSet(prefetch_container)); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1876 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1877 | if (!UsePrefetchScheduler()) { |
| 1878 | active_prefetch_ = std::nullopt; |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1879 | Prefetch(); |
| 1880 | } else { |
Hiroshige Hayashizaki | 12ab6d7 | 2025-07-15 00:27:45 | [diff] [blame] | 1881 | RemoveFromSchedulerAndProgressAsync(prefetch_container); |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1882 | } |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 1883 | } |
| 1884 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1885 | void PrefetchService::CopyIsolatedCookies( |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1886 | const PrefetchServingHandle& serving_handle) { |
| 1887 | DCHECK(serving_handle); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1888 | |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1889 | if (!serving_handle.GetCurrentNetworkContextToServe()) { |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1890 | // Not set in unit tests. |
| 1891 | return; |
| 1892 | } |
| 1893 | |
| 1894 | // We only need to copy cookies if the prefetch used an isolated network |
| 1895 | // context. |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1896 | if (!serving_handle.IsIsolatedNetworkContextRequiredToServe()) { |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1897 | return; |
| 1898 | } |
| 1899 | |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1900 | serving_handle.OnIsolatedCookieCopyStart(); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1901 | net::CookieOptions options = net::CookieOptions::MakeAllInclusive(); |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1902 | serving_handle.GetCurrentNetworkContextToServe() |
| 1903 | ->GetCookieManager() |
| 1904 | ->GetCookieList( |
| 1905 | serving_handle.GetCurrentURLToServe(), options, |
| 1906 | net::CookiePartitionKeyCollection::Todo(), |
| 1907 | base::BindOnce(&PrefetchService::OnGotIsolatedCookiesForCopy, |
| 1908 | weak_method_factory_.GetWeakPtr(), |
| 1909 | serving_handle.Clone())); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1910 | } |
| 1911 | |
| 1912 | void PrefetchService::OnGotIsolatedCookiesForCopy( |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1913 | PrefetchServingHandle serving_handle, |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1914 | const net::CookieAccessResultList& cookie_list, |
| 1915 | const net::CookieAccessResultList& excluded_cookies) { |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1916 | serving_handle.OnIsolatedCookiesReadCompleteAndWriteStart(); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1917 | RecordPrefetchProxyPrefetchMainframeCookiesToCopy(cookie_list.size()); |
| 1918 | |
| 1919 | if (cookie_list.empty()) { |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1920 | serving_handle.OnIsolatedCookieCopyComplete(); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1921 | return; |
| 1922 | } |
| 1923 | |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1924 | const auto current_url = serving_handle.GetCurrentURLToServe(); |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 1925 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1926 | base::RepeatingClosure barrier = base::BarrierClosure( |
| 1927 | cookie_list.size(), |
Hiroshige Hayashizaki | 16b6e54f | 2025-08-12 06:56:57 | [diff] [blame] | 1928 | base::BindOnce(&OnIsolatedCookieCopyComplete, std::move(serving_handle))); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1929 | |
| 1930 | net::CookieOptions options = net::CookieOptions::MakeAllInclusive(); |
| 1931 | for (const net::CookieWithAccessResult& cookie : cookie_list) { |
| 1932 | browser_context_->GetDefaultStoragePartition() |
| 1933 | ->GetCookieManagerForBrowserProcess() |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 1934 | ->SetCanonicalCookie(cookie.cookie, current_url, options, |
| 1935 | base::BindOnce(&CookieSetHelper, barrier)); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 1936 | } |
| 1937 | } |
| 1938 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1939 | // TODO(crbug.com/406754449): Inline this function when removing the feature |
| 1940 | // flag. |
kenoss | f7cea393 | 2025-03-28 03:47:37 | [diff] [blame] | 1941 | bool PrefetchService::IsPrefetchContainerInActiveSet( |
| 1942 | const PrefetchContainer& prefetch_container) { |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 1943 | if (!UsePrefetchScheduler()) { |
| 1944 | return active_prefetch_ == prefetch_container.key(); |
| 1945 | } else { |
| 1946 | return scheduler_->IsInActiveSet(prefetch_container); |
| 1947 | } |
kenoss | f7cea393 | 2025-03-28 03:47:37 | [diff] [blame] | 1948 | } |
| 1949 | |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1950 | void PrefetchService::DumpPrefetchesForDebug() const { |
| 1951 | #if DCHECK_IS_ON() |
| 1952 | std::ostringstream ss; |
| 1953 | ss << "PrefetchService[" << this << "]:" << std::endl; |
| 1954 | |
| 1955 | ss << "Owned:" << std::endl; |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1956 | for (const auto& entry : owned_prefetches()) { |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 1957 | ss << *entry.second << std::endl; |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1958 | } |
| 1959 | |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 1960 | DVLOG(1) << ss.str(); |
| 1961 | #endif // DCHECK_IS_ON() |
| 1962 | } |
| 1963 | |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 1964 | std::pair<std::vector<PrefetchContainer*>, |
| 1965 | base::flat_map<PrefetchContainer::Key, PrefetchServableState>> |
kenoss | 4dc06866 | 2024-10-04 05:03:50 | [diff] [blame] | 1966 | PrefetchService::CollectMatchCandidates( |
Hiroshige Hayashizaki | 6a2bc75 | 2023-10-31 19:08:11 | [diff] [blame] | 1967 | const PrefetchContainer::Key& key, |
kenoss | 804a72dc | 2024-10-11 12:43:49 | [diff] [blame] | 1968 | bool is_nav_prerender, |
Hiroshige Hayashizaki | 6a2bc75 | 2023-10-31 19:08:11 | [diff] [blame] | 1969 | base::WeakPtr<PrefetchServingPageMetricsContainer> |
| 1970 | serving_page_metrics_container) { |
kenoss | e013512 | 2024-09-25 05:21:34 | [diff] [blame] | 1971 | return CollectMatchCandidatesGeneric( |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1972 | owned_prefetches(), key, is_nav_prerender, |
kenoss | 804a72dc | 2024-10-11 12:43:49 | [diff] [blame] | 1973 | std::move(serving_page_metrics_container)); |
Liviu Tinta | d608e01 | 2023-05-10 19:16:41 | [diff] [blame] | 1974 | } |
| 1975 | |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 1976 | base::WeakPtr<PrefetchContainer> PrefetchService::MatchUrl( |
| 1977 | const PrefetchContainer::Key& key) const { |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1978 | return no_vary_search::MatchUrl(key, owned_prefetches()); |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 1979 | } |
| 1980 | |
| 1981 | std::vector<std::pair<GURL, base::WeakPtr<PrefetchContainer>>> |
| 1982 | PrefetchService::GetAllForUrlWithoutRefAndQueryForTesting( |
| 1983 | const PrefetchContainer::Key& key) const { |
| 1984 | return no_vary_search::GetAllForUrlWithoutRefAndQueryForTesting( |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 1985 | key, owned_prefetches()); |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 1986 | } |
| 1987 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1988 | // static |
| 1989 | void PrefetchService::SetServiceWorkerContextForTesting( |
| 1990 | ServiceWorkerContext* context) { |
| 1991 | g_service_worker_context_for_testing = context; |
| 1992 | } |
| 1993 | |
| 1994 | // static |
| 1995 | void PrefetchService::SetHostNonUniqueFilterForTesting( |
Md Hasibul Hasan | a963a934 | 2024-04-03 10:15:14 | [diff] [blame] | 1996 | bool (*filter)(std::string_view)) { |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 1997 | g_host_non_unique_filter = filter; |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 1998 | } |
| 1999 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 2000 | // static |
| 2001 | void PrefetchService::SetURLLoaderFactoryForTesting( |
Hiroshige Hayashizaki | e51e2f26 | 2025-03-04 23:14:38 | [diff] [blame] | 2002 | network::SharedURLLoaderFactory* url_loader_factory) { |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 2003 | g_url_loader_factory_for_testing = url_loader_factory; |
| 2004 | } |
| 2005 | |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 2006 | // static |
| 2007 | void PrefetchService::SetNetworkContextForProxyLookupForTesting( |
| 2008 | network::mojom::NetworkContext* network_context) { |
| 2009 | g_network_context_for_proxy_lookup_for_testing = network_context; |
| 2010 | } |
| 2011 | |
Taiyo Mizuhashi | 9395b7c8 | 2024-05-16 20:30:26 | [diff] [blame] | 2012 | // static |
kenoss | b68c9e8 | 2024-10-10 10:11:15 | [diff] [blame] | 2013 | void PrefetchService::SetDelayEligibilityCheckForTesting( |
| 2014 | DelayEligibilityCheckForTesting callback) { |
| 2015 | GetDelayEligibilityCheckForTesting() = // IN-TEST |
| 2016 | std::move(callback); |
| 2017 | } |
| 2018 | |
| 2019 | // static |
kenoss | 4858a80 | 2024-10-11 06:50:56 | [diff] [blame] | 2020 | void PrefetchService::SetForceIneligibilityForTesting( |
| 2021 | PreloadingEligibility eligibility) { |
| 2022 | GetForceIneligibilityForTesting() = // IN-TEST |
| 2023 | eligibility; |
| 2024 | } |
| 2025 | |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 2026 | base::WeakPtr<PrefetchService> PrefetchService::GetWeakPtr() { |
| 2027 | return weak_method_factory_.GetWeakPtr(); |
| 2028 | } |
| 2029 | |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 2030 | void PrefetchService::RecordExistingPrefetchWithMatchingURL( |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 2031 | const PrefetchContainer& prefetch_container) const { |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 2032 | bool matching_prefetch = false; |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2033 | int num_matching_prefetches = 0; |
| 2034 | |
| 2035 | int num_matching_eligible_prefetch = 0; |
| 2036 | int num_matching_servable_prefetch = 0; |
| 2037 | int num_matching_prefetch_same_referrer = 0; |
| 2038 | int num_matching_prefetch_same_rfh = 0; |
| 2039 | |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 2040 | for (const auto& prefetch_iter : owned_prefetches()) { |
Max Curran | 080b600 | 2022-07-14 19:36:33 | [diff] [blame] | 2041 | if (prefetch_iter.second && |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 2042 | prefetch_iter.second->GetURL() == prefetch_container.GetURL()) { |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 2043 | matching_prefetch = true; |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2044 | num_matching_prefetches++; |
| 2045 | |
Hiroshige Hayashizaki | 7a19780 | 2025-07-18 21:05:36 | [diff] [blame] | 2046 | switch (prefetch_iter.second->GetLoadState()) { |
| 2047 | case PrefetchContainer::LoadState::kNotStarted: |
| 2048 | case PrefetchContainer::LoadState::kFailedIneligible: |
| 2049 | break; |
| 2050 | case PrefetchContainer::LoadState::kEligible: |
| 2051 | case PrefetchContainer::LoadState::kFailedHeldback: |
| 2052 | case PrefetchContainer::LoadState::kStarted: |
Hiroshige Hayashizaki | 6b7035c0 | 2025-07-18 21:06:32 | [diff] [blame] | 2053 | case PrefetchContainer::LoadState::kDeterminedHead: |
| 2054 | case PrefetchContainer::LoadState::kCompletedOrFailed: |
Hiroshige Hayashizaki | 7a19780 | 2025-07-18 21:05:36 | [diff] [blame] | 2055 | num_matching_eligible_prefetch++; |
| 2056 | break; |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2057 | } |
| 2058 | |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 2059 | switch ( |
| 2060 | prefetch_iter.second->GetServableState(PrefetchCacheableDuration())) { |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 2061 | case PrefetchServableState::kNotServable: |
| 2062 | case PrefetchServableState::kShouldBlockUntilHeadReceived: |
| 2063 | case PrefetchServableState::kShouldBlockUntilEligibilityGot: |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 2064 | break; |
Hiroshige Hayashizaki | b3ff61d | 2025-08-12 06:28:08 | [diff] [blame] | 2065 | case PrefetchServableState::kServable: |
kenoss | 8596b68 | 2025-07-01 10:35:46 | [diff] [blame] | 2066 | num_matching_servable_prefetch++; |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 2067 | break; |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2068 | } |
| 2069 | |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 2070 | if (prefetch_iter.second->HasSameReferringURLForMetrics( |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 2071 | prefetch_container)) { |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2072 | num_matching_prefetch_same_referrer++; |
| 2073 | } |
| 2074 | |
| 2075 | if (prefetch_iter.second->GetReferringRenderFrameHostId() == |
Hiroshige Hayashizaki | 70db51b | 2025-07-14 23:09:49 | [diff] [blame] | 2076 | prefetch_container.GetReferringRenderFrameHostId()) { |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2077 | num_matching_prefetch_same_rfh++; |
| 2078 | } |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 2079 | } |
| 2080 | } |
| 2081 | |
| 2082 | base::UmaHistogramBoolean( |
| 2083 | "PrefetchProxy.Prefetch.ExistingPrefetchWithMatchingURL", |
| 2084 | matching_prefetch); |
Max Curran | 4f4c802a | 2022-10-27 23:52:04 | [diff] [blame] | 2085 | base::UmaHistogramCounts100( |
| 2086 | "PrefetchProxy.Prefetch.NumExistingPrefetchWithMatchingURL", |
| 2087 | num_matching_prefetches); |
| 2088 | |
| 2089 | if (matching_prefetch) { |
| 2090 | base::UmaHistogramCounts100( |
| 2091 | "PrefetchProxy.Prefetch.NumExistingEligiblePrefetchWithMatchingURL", |
| 2092 | num_matching_eligible_prefetch); |
| 2093 | base::UmaHistogramCounts100( |
| 2094 | "PrefetchProxy.Prefetch.NumExistingServablePrefetchWithMatchingURL", |
| 2095 | num_matching_servable_prefetch); |
| 2096 | base::UmaHistogramCounts100( |
| 2097 | "PrefetchProxy.Prefetch.NumExistingPrefetchWithMatchingURLAndReferrer", |
| 2098 | num_matching_prefetch_same_referrer); |
| 2099 | base::UmaHistogramCounts100( |
| 2100 | "PrefetchProxy.Prefetch." |
| 2101 | "NumExistingPrefetchWithMatchingURLAndRenderFrameHost", |
| 2102 | num_matching_prefetch_same_rfh); |
| 2103 | } |
Max Curran | 679a3d5 | 2022-07-11 23:03:31 | [diff] [blame] | 2104 | } |
| 2105 | |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 2106 | void PrefetchService::EvictPrefetchesForBrowsingDataRemoval( |
| 2107 | const StoragePartition::StorageKeyMatcherFunction& storage_key_filter, |
| 2108 | PrefetchStatus status) { |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 2109 | std::vector<base::WeakPtr<PrefetchContainer>> prefetches_to_reset; |
Hiroshige Hayashizaki | 72ea9b9 | 2025-07-05 02:47:56 | [diff] [blame] | 2110 | for (const auto& prefetch_iter : owned_prefetches()) { |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 2111 | base::WeakPtr<PrefetchContainer> prefetch_container = |
| 2112 | prefetch_iter.second->GetWeakPtr(); |
| 2113 | CHECK(prefetch_container); |
Taiyo Mizuhashi | af23c43 | 2025-06-23 07:41:37 | [diff] [blame] | 2114 | |
| 2115 | // If `referring_origin` is std::nullopt (e.g some browser-initiated |
| 2116 | // prefetch), use the origin of the prefetch URL itself, since we generally |
| 2117 | // handle no referring origin prefetches as a same-origin prefetch fashion. |
| 2118 | const url::Origin target_origin = |
| 2119 | prefetch_container->GetReferringOrigin().value_or( |
| 2120 | url::Origin::Create(prefetch_container->GetURL())); |
| 2121 | if (storage_key_filter.Run( |
| 2122 | blink::StorageKey::CreateFirstParty(target_origin))) { |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 2123 | prefetch_container->SetPrefetchStatus(status); |
| 2124 | prefetches_to_reset.push_back(prefetch_container); |
| 2125 | } |
| 2126 | } |
| 2127 | |
kenoss | 1b8ebc2 | 2025-04-04 00:11:35 | [diff] [blame] | 2128 | if (!UsePrefetchScheduler()) { |
| 2129 | for (const auto& prefetch_container : prefetches_to_reset) { |
| 2130 | ResetPrefetchContainer(prefetch_container); |
| 2131 | } |
| 2132 | } else { |
kenoss | 0a56736 | 2025-05-16 11:18:55 | [diff] [blame] | 2133 | ResetPrefetchContainersAndProgressAsync(std::move(prefetches_to_reset)); |
Steven Wei | e829c85f7 | 2025-03-17 18:57:59 | [diff] [blame] | 2134 | } |
| 2135 | } |
| 2136 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 2137 | } // namespace content |