Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 5 | #ifndef CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_CONTAINER_H_ |
| 6 | #define CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_CONTAINER_H_ |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 7 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 8 | #include <optional> |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 9 | #include <utility> |
| 10 | |
Ali Hijazi | 3162891 | 2023-06-05 18:13:41 | [diff] [blame] | 11 | #include "base/memory/raw_ref.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 13 | #include "base/time/time.h" |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 14 | #include "content/browser/preloading/prefetch/prefetch_probe_result.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 15 | #include "content/browser/preloading/prefetch/prefetch_status.h" |
Hiroshige Hayashizaki | c853c030 | 2023-09-13 08:51:07 | [diff] [blame] | 16 | #include "content/browser/preloading/prefetch/prefetch_streaming_url_loader_common_types.h" |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 17 | #include "content/browser/preloading/prefetch/prefetch_type.h" |
Max Curran | 14dd787 | 2023-03-20 19:57:36 | [diff] [blame] | 18 | #include "content/browser/preloading/speculation_host_devtools_observer.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 19 | #include "content/common/content_export.h" |
| 20 | #include "content/public/browser/global_routing_id.h" |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 21 | #include "content/public/browser/preloading.h" |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 22 | #include "content/public/browser/preloading_data.h" |
Liviu Tinta | 68a4580 | 2023-04-05 18:32:20 | [diff] [blame] | 23 | #include "net/http/http_no_vary_search_data.h" |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 24 | #include "services/metrics/public/cpp/ukm_source_id.h" |
Hiroshige Hayashizaki | 2df4529 | 2023-10-10 22:59:03 | [diff] [blame] | 25 | #include "third_party/blink/public/common/tokens/tokens.h" |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 26 | #include "url/gurl.h" |
| 27 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 28 | namespace network { |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 29 | namespace mojom { |
| 30 | class CookieManager; |
| 31 | } // namespace mojom |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 32 | } // namespace network |
| 33 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 34 | namespace content { |
| 35 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 36 | class BrowserContext; |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 37 | class PrefetchCookieListener; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 38 | class PrefetchDocumentManager; |
| 39 | class PrefetchNetworkContext; |
Hiroshige Hayashizaki | c853c030 | 2023-09-13 08:51:07 | [diff] [blame] | 40 | class PrefetchResponseReader; |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 41 | class PrefetchService; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 42 | class PrefetchServingPageMetricsContainer; |
| 43 | class PrefetchStreamingURLLoader; |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 44 | class PreloadingAttempt; |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 45 | class ProxyLookupClientImpl; |
Hiroshige Hayashizaki | 2cbf67b9 | 2023-10-23 17:40:07 | [diff] [blame] | 46 | class RenderFrameHost; |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 47 | class RenderFrameHostImpl; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 48 | |
William Liu | ef93473 | 2022-11-02 22:46:29 | [diff] [blame] | 49 | // Holds the relevant size information of the prefetched response. The struct is |
| 50 | // installed onto `PrefetchContainer`, and gets passed into |
| 51 | // `PrefetchFromStringURLLoader` to notify the associated `URLLoaderClient` of |
| 52 | // the actual size of the response, as `PrefetchFromStringURLLoader` is not |
| 53 | // aware of the prefetched request. |
| 54 | struct PrefetchResponseSizes { |
| 55 | int64_t encoded_data_length; |
| 56 | int64_t encoded_body_length; |
| 57 | int64_t decoded_body_length; |
| 58 | }; |
| 59 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 60 | // This class contains the state for a request to prefetch a specific URL. |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 61 | // |
| 62 | // A `PrefetchContainer` can have multiple `PrefetchContainer::SinglePrefetch`es |
| 63 | // and `PrefetchStreamingURLLoader`s to support redirects. Each |
| 64 | // `PrefetchContainer::SinglePrefetch` in `redirect_chain_` corresponds to a |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 65 | // single redirect hop, while a single `PrefetchStreamingURLLoader` can receive |
| 66 | // multiple redirect hops unless network context switching is needed. |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 67 | // |
| 68 | // For example: |
| 69 | // |
| 70 | // |PrefetchStreamingURLLoader A-----| |PrefetchStreamingURLLoader B ---------| |
| 71 | // HandleRedirect - HandleRedirect - HandleRedirect - ReceiveResponse-Finish |
| 72 | // |SinglePrefetch0| |SinglePrefetch1| |SinglePrefetch2| |SinglePrefetch3-----| |
| 73 | // |
| 74 | // While prefetching (see methods named like "ForCurrentPrefetch" or |
| 75 | // "ToPrefetch"), `SinglePrefetch`es and `PrefetchStreamingURLLoader`s (among |
| 76 | // other members) are added and filled. The steps for creating these objects and |
| 77 | // associating with each other span multiple classes/methods: |
| 78 | // |
| 79 | // 1. A new `PrefetchContainer::SinglePrefetch` and thus a new |
| 80 | // `PrefetchResponseReader` is created and added to `redirect_chain_`. |
| 81 | // This is done either in: |
| 82 | // - `PrefetchContainer` constructor [for an initial request], or |
| 83 | // - `AddRedirectHop()` [for a redirect]. |
| 84 | // |
| 85 | // 2. The new `PrefetchResponseReader` (created at Step 1, referenced as |
| 86 | // `GetResponseReaderForCurrentPrefetch()`) is associated with the |
| 87 | // `PrefetchStreamingURLLoader` to be used. |
| 88 | // This is done either in (see the indirect call sites of |
| 89 | // `PrefetchStreamingURLLoader::SetResponseReader()`): |
| 90 | // - `PrefetchService::StartSinglePrefetch()` [initial request] or |
| 91 | // - `PrefetchService::OnGotEligibilityResultForRedirect()` [redirect]. |
| 92 | // A new `PrefetchStreamingURLLoader` is also created if needed in |
| 93 | // `PrefetchService::MakePrefetchRequest()`. |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 94 | class CONTENT_EXPORT PrefetchContainer { |
| 95 | public: |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 96 | // When `matcher` is null (only in unit tests), |
| 97 | // `PreloadingData::GetSameURLMatcher` is used. |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 98 | PrefetchContainer( |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 99 | RenderFrameHostImpl& referring_render_frame_host, |
Hiroshige Hayashizaki | 2df4529 | 2023-10-10 22:59:03 | [diff] [blame] | 100 | const blink::DocumentToken& referring_document_token, |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 101 | const GURL& url, |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 102 | const PrefetchType& prefetch_type, |
Kevin McNee | e6ca089 | 2022-09-28 15:36:22 | [diff] [blame] | 103 | const blink::mojom::Referrer& referrer, |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 104 | std::optional<net::HttpNoVarySearchData> no_vary_search_expected, |
Hiroshige Hayashizaki | a00e4c8 | 2023-10-31 20:20:17 | [diff] [blame] | 105 | base::WeakPtr<PrefetchDocumentManager> prefetch_document_manager, |
Taiyo Mizuhashi | 0192d3f | 2024-02-15 05:09:43 | [diff] [blame^] | 106 | base::WeakPtr<PreloadingAttempt> attempt = nullptr); |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 107 | ~PrefetchContainer(); |
| 108 | |
| 109 | PrefetchContainer(const PrefetchContainer&) = delete; |
| 110 | PrefetchContainer& operator=(const PrefetchContainer&) = delete; |
| 111 | |
| 112 | // Defines the key to uniquely identify a prefetch. |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 113 | class CONTENT_EXPORT Key { |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 114 | public: |
| 115 | Key() = delete; |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 116 | Key(net::NetworkIsolationKey nik, GURL prefetch_url); |
| 117 | Key(blink::DocumentToken referring_document_token, GURL prefetch_url); |
| 118 | ~Key(); |
| 119 | |
| 120 | Key(const Key&); |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 121 | |
| 122 | bool operator==(const Key& rhs) const = default; |
| 123 | bool operator<(const Key& rhs) const { |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 124 | if (referring_document_token_or_nik_ != |
| 125 | rhs.referring_document_token_or_nik_) { |
| 126 | return referring_document_token_or_nik_ < |
| 127 | rhs.referring_document_token_or_nik_; |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 128 | } |
| 129 | return prefetch_url_ < rhs.prefetch_url_; |
| 130 | } |
| 131 | |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 132 | const GURL& prefetch_url() const { return prefetch_url_; } |
| 133 | |
Kouhei Ueno | ebacf89 | 2023-11-14 03:41:43 | [diff] [blame] | 134 | Key WithNewUrl(const GURL& new_url) const { |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 135 | return Key(referring_document_token_or_nik_, new_url); |
Kouhei Ueno | ebacf89 | 2023-11-14 03:41:43 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool NonUrlPartIsSame(const Key& other) const { |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 139 | return referring_document_token_or_nik_ == |
| 140 | other.referring_document_token_or_nik_; |
Kouhei Ueno | ebacf89 | 2023-11-14 03:41:43 | [diff] [blame] | 141 | } |
| 142 | |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 143 | private: |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 144 | Key(absl::variant<blink::DocumentToken, net::NetworkIsolationKey> |
| 145 | referring_document_token_or_nik, |
| 146 | GURL prefetch_url); |
| 147 | |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 148 | friend CONTENT_EXPORT std::ostream& operator<<(std::ostream& ostream, |
| 149 | const Key& prefetch_key); |
| 150 | |
Kouhei Ueno | 34c7c6915 | 2023-11-16 04:42:42 | [diff] [blame] | 151 | const absl::variant<blink::DocumentToken, net::NetworkIsolationKey> |
| 152 | referring_document_token_or_nik_; |
Kouhei Ueno | 45b00ef2 | 2023-11-10 02:22:58 | [diff] [blame] | 153 | const GURL prefetch_url_; |
| 154 | }; |
| 155 | |
Kouhei Ueno | 8ce5445 | 2023-11-21 03:58:02 | [diff] [blame] | 156 | const Key& GetPrefetchContainerKey() const { return key_; } |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 157 | |
Rakina Zata Amni | 39d8e7e | 2022-10-20 18:18:30 | [diff] [blame] | 158 | // The ID of the RenderFrameHost that triggered the prefetch. |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 159 | GlobalRenderFrameHostId GetReferringRenderFrameHostId() const { |
| 160 | return referring_render_frame_host_id_; |
| 161 | } |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 162 | bool HasSameReferringURLForMetrics(const PrefetchContainer& other) const; |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 163 | |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 164 | // The initial URL that was requested to be prefetched. |
Kouhei Ueno | 8ce5445 | 2023-11-21 03:58:02 | [diff] [blame] | 165 | const GURL& GetURL() const { return key_.prefetch_url(); } |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 166 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 167 | // The current URL being fetched. |
| 168 | GURL GetCurrentURL() const; |
| 169 | |
| 170 | // The previous URL, if this has been redirected. Invalid to call otherwise. |
| 171 | GURL GetPreviousURL() const; |
| 172 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 173 | // The type of this prefetch. Controls how the prefetch is handled. |
| 174 | const PrefetchType& GetPrefetchType() const { return prefetch_type_; } |
| 175 | |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 176 | // Whether or not an isolated network context is required to the next |
| 177 | // prefetch. |
| 178 | bool IsIsolatedNetworkContextRequiredForCurrentPrefetch() const; |
Max Curran | 4870096 | 2023-05-15 18:35:52 | [diff] [blame] | 179 | |
| 180 | // Whether or not an isolated network context is required for the previous |
| 181 | // redirect hop of the given url. |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 182 | bool IsIsolatedNetworkContextRequiredForPreviousRedirectHop() const; |
| 183 | |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 184 | base::WeakPtr<PrefetchResponseReader> GetResponseReaderForCurrentPrefetch(); |
| 185 | |
Max Curran | c013750 | 2023-05-02 18:06:22 | [diff] [blame] | 186 | // Whether or not the prefetch proxy would be required to fetch the given url |
| 187 | // based on |prefetch_type_|. |
| 188 | bool IsProxyRequiredForURL(const GURL& url) const; |
| 189 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 190 | const network::ResourceRequest* GetResourceRequest() const { |
| 191 | return resource_request_.get(); |
| 192 | } |
| 193 | void MakeResourceRequest(const net::HttpRequestHeaders& additional_headers); |
| 194 | |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 195 | // Updates |referrer_| after a redirect. |
| 196 | void UpdateReferrer( |
| 197 | const GURL& new_referrer_url, |
| 198 | const network::mojom::ReferrerPolicy& new_referrer_policy); |
| 199 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 200 | const std::optional<net::HttpNoVarySearchData>& GetNoVarySearchHint() const { |
Liviu Tinta | 953bde88 | 2023-05-05 13:21:35 | [diff] [blame] | 201 | return no_vary_search_hint_; |
Liviu Tinta | 68a4580 | 2023-04-05 18:32:20 | [diff] [blame] | 202 | } |
| 203 | |
Alan Cutter | 5525a85 | 2022-10-07 03:53:31 | [diff] [blame] | 204 | base::WeakPtr<PrefetchContainer> GetWeakPtr() { |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 205 | return weak_method_factory_.GetWeakPtr(); |
| 206 | } |
| 207 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 208 | // The status of the current prefetch. Note that |HasPrefetchStatus| will be |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 209 | // initially false until |SetPrefetchStatus| is called. |SetPrefetchStatus| |
| 210 | // also sets |attempt_| PreloadingHoldbackStatus, PreloadingTriggeringOutcome |
| 211 | // and PreloadingFailureReason. It is only safe to call after |
| 212 | // `OnEligibilityCheckComplete`. |
| 213 | void SetPrefetchStatus(PrefetchStatus prefetch_status); |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 214 | bool HasPrefetchStatus() const { return prefetch_status_.has_value(); } |
| 215 | PrefetchStatus GetPrefetchStatus() const; |
| 216 | |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 217 | // The state enum of the current prefetch, to replace `PrefetchStatus`. |
| 218 | // https://crbug.com/1494771 |
| 219 | // Design doc for PrefetchContainer state transitions: |
| 220 | // https://docs.google.com/document/d/1dK4mAVoRrgTVTGdewthI_hA8AHirgXW8k6BmpK9gnBE/edit?usp=sharing |
| 221 | enum class LoadState { |
| 222 | // --- Phase 1. [Initial state] |
| 223 | kNotStarted, |
| 224 | |
| 225 | // --- Phase 2. The eligibility check for the initial request has completed |
| 226 | // and `PreloadingAttempt::SetEligibility()` has been called. |
| 227 | |
| 228 | // Found eligible. |
| 229 | kEligible, |
| 230 | |
| 231 | // [Final state] Found ineligible. `redirect_chain_[0].eligibility_` |
| 232 | // contains the reason for being ineligible. |
| 233 | kFailedIneligible, |
| 234 | |
| 235 | // --- Phase 3. PrefetchService::StartSinglePrefetch() has been called and |
| 236 | // the holdback check has completed. |
| 237 | |
| 238 | // [Final state] Not heldback. |
| 239 | // |
| 240 | // On this state, refer to `PrefetchResponseReader`s for detailed |
| 241 | // prefetching state and servability. |
| 242 | // |
| 243 | // Also, refer to `attempt_` for triggering outcome and failure reasons for |
| 244 | // metrics. |
| 245 | // `PreloadingAttempt::SetFailureReason()` can be only called on this state. |
| 246 | // Note that these states of `attempt_` don't directly affect |
| 247 | // `PrefetchResponseReader`'s servability. |
| 248 | // (e.g. `PrefetchResponseReader::GetServableState()` can be still |
| 249 | // `kServable` even if `attempt_` has a failure). |
| 250 | kStarted, |
| 251 | |
| 252 | // [Final state] Heldback due to `PreloadingAttempt::ShouldHoldback()`. |
| 253 | kFailedHeldback, |
| 254 | }; |
| 255 | void SetLoadState(LoadState prefetch_status); |
| 256 | LoadState GetLoadState() const; |
| 257 | |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 258 | // Controls ownership of the |ProxyLookupClientImpl| used during the |
| 259 | // eligibility check. |
| 260 | void TakeProxyLookupClient( |
| 261 | std::unique_ptr<ProxyLookupClientImpl> proxy_lookup_client); |
| 262 | std::unique_ptr<ProxyLookupClientImpl> ReleaseProxyLookupClient(); |
| 263 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 264 | // Whether or not the prefetch was determined to be eligibile. |
Hiroshige Hayashizaki | 939c5ed4 | 2023-11-01 03:29:21 | [diff] [blame] | 265 | void OnEligibilityCheckComplete(PreloadingEligibility eligibility); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 266 | bool IsInitialPrefetchEligible() const; |
| 267 | |
| 268 | // Adds a the new URL to |redirect_chain_|. |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 269 | void AddRedirectHop(const net::RedirectInfo& redirect_info); |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 270 | |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 271 | // The length of the redirect chain for this prefetch. |
| 272 | size_t GetRedirectChainSize() const { return redirect_chain_.size(); } |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 273 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 274 | // Whether this prefetch is a decoy. Decoy prefetches will not store the |
| 275 | // response, and not serve any prefetched resources. |
| 276 | void SetIsDecoy(bool is_decoy) { is_decoy_ = is_decoy; } |
| 277 | bool IsDecoy() const { return is_decoy_; } |
| 278 | |
Hiroshige Hayashizaki | eec97ed1 | 2023-05-26 06:38:32 | [diff] [blame] | 279 | // Allows for |PrefetchCookieListener|s to be reigsitered for |
| 280 | // `GetCurrentSinglePrefetchToPrefetch()`. |
| 281 | void RegisterCookieListener(network::mojom::CookieManager* cookie_manager); |
Max Curran | 7dbdea4d | 2023-03-21 23:47:37 | [diff] [blame] | 282 | void StopAllCookieListeners(); |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 283 | |
Hiroshige Hayashizaki | b8c4bf60 | 2023-05-26 01:34:58 | [diff] [blame] | 284 | // The network context used to make network requests for the next prefetch. |
Kouhei Ueno | 4442db9 | 2023-11-13 06:38:13 | [diff] [blame] | 285 | PrefetchNetworkContext* GetOrCreateNetworkContextForCurrentPrefetch(); |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 286 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 287 | // Closes idle connections for all elements in |network_contexts_|. |
| 288 | void CloseIdleConnections(); |
| 289 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 290 | // Set the currently prefetching |PrefetchStreamingURLLoader|. |
| 291 | void SetStreamingURLLoader( |
| 292 | base::WeakPtr<PrefetchStreamingURLLoader> streaming_loader); |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 293 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 294 | // Returns the URL loader being used for prefetching the current redirect hop. |
Hiroshige Hayashizaki | 334d0fa | 2023-08-16 23:33:27 | [diff] [blame] | 295 | // This method should be used during prefetching and shouldn't be called for |
| 296 | // serving purpose. |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 297 | const base::WeakPtr<PrefetchStreamingURLLoader>& GetStreamingURLLoader() |
| 298 | const; |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 299 | |
Hiroshige Hayashizaki | 5c50ec5 | 2023-09-13 00:35:49 | [diff] [blame] | 300 | bool IsStreamingURLLoaderDeletionScheduledForTesting() const; |
| 301 | |
Hiroshige Hayashizaki | 334d0fa | 2023-08-16 23:33:27 | [diff] [blame] | 302 | // Returns the PrefetchResponseReader corresponding to the last non-redirect |
| 303 | // response, if already received its head, or otherwise nullptr. |
| 304 | const PrefetchResponseReader* GetNonRedirectResponseReader() const; |
| 305 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 306 | // Clears |streaming_loader_| and cancels its loading, if any of its |
| 307 | // corresponding `PrefetchResponseReader` does NOT start serving. Currently |
| 308 | // this itself doesn't mark `this` as failed and thus can leave `this` |
| 309 | // stalled. Therefore, call this method only if `this` can be no longer used |
| 310 | // for serving, e.g. on the destructor or when |
| 311 | // `HaveDefaultContextCookiesChanged()` is true. |
| 312 | // TODO(crbug.com/1449360): For callsites outside the destructor, remove the |
| 313 | // call or mark `this` as failed, because the current behavior (== existing |
| 314 | // behavior, previously as `ResetAllStreamingURLLoaders()`) might potentially |
| 315 | // cause issues when there are multiple navigations using `this` concurrently. |
| 316 | void CancelStreamingURLLoaderIfNotServing(); |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 317 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 318 | // The |PrefetchDocumentManager| that requested |this|. |
| 319 | PrefetchDocumentManager* GetPrefetchDocumentManager() const; |
| 320 | |
Max Curran | 983f21a5 | 2023-03-23 23:52:58 | [diff] [blame] | 321 | // Called when |PrefetchService::GetPrefetchToServe| and |
| 322 | // |PrefetchService::ReturnPrefetchToServe| with |this|. |
| 323 | void OnGetPrefetchToServe(bool blocked_until_head); |
| 324 | void OnReturnPrefetchToServe(bool served); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 325 | |
Max Curran | e044494 | 2022-09-06 23:55:23 | [diff] [blame] | 326 | // Returns whether or not this prefetch has been considered to serve for a |
| 327 | // navigation in the past. If it has, then it shouldn't be used for any future |
| 328 | // navigations. |
Hiroshige Hayashizaki | c0b6aea | 2023-10-17 21:56:43 | [diff] [blame] | 329 | bool HasPrefetchBeenConsideredToServe() const; |
Max Curran | e044494 | 2022-09-06 23:55:23 | [diff] [blame] | 330 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 331 | // Called when |PrefetchService::OnPrefetchComplete| is called for the |
| 332 | // prefetch. This happens when |loader_| fully downloads the requested |
| 333 | // resource. |
Hiroshige Hayashizaki | 77083bc8 | 2023-11-28 06:04:17 | [diff] [blame] | 334 | void OnPrefetchComplete( |
| 335 | const network::URLLoaderCompletionStatus& completion_status); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 336 | |
Max Curran | e6679ca | 2023-06-06 19:01:39 | [diff] [blame] | 337 | // Allows for a timer to be used to limit the maximum amount of time that a |
| 338 | // navigation can be blocked waiting for the head of this prefetch to be |
| 339 | // received. |
| 340 | void TakeBlockUntilHeadTimer( |
| 341 | std::unique_ptr<base::OneShotTimer> block_until_head_timer); |
| 342 | void ResetBlockUntilHeadTimer(); |
| 343 | |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 344 | enum class ServableState { |
| 345 | // Not servable nor should block until head received. |
| 346 | kNotServable, |
| 347 | |
| 348 | // Servable. |
| 349 | kServable, |
| 350 | |
| 351 | // |PrefetchService| should block until the head of |this| is |
| 352 | // received on a navigation to a matching URL. |
| 353 | kShouldBlockUntilHeadReceived, |
| 354 | }; |
Hiroshige Hayashizaki | 8f74d22b | 2023-09-28 04:48:08 | [diff] [blame] | 355 | |
| 356 | // Note: Even if this returns `kServable`, `CreateRequestHandler()` can still |
| 357 | // fail (returning null handler) due to final checks. See also the comment for |
| 358 | // `PrefetchResponseReader::CreateRequestHandler()`. |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 359 | ServableState GetServableState(base::TimeDelta cacheable_duration) const; |
Max Curran | 6b93426f | 2022-05-03 00:55:52 | [diff] [blame] | 360 | |
Hiroshige Hayashizaki | b5879de | 2023-07-14 20:22:13 | [diff] [blame] | 361 | // Called once it is determined whether or not the prefetch is servable, i.e. |
| 362 | // either when non-redirect response head is received, or when determined not |
| 363 | // servable. |
| 364 | void OnReceivedHead(); |
| 365 | void SetOnReceivedHeadCallback(base::OnceClosure on_received_head_callback); |
| 366 | base::OnceClosure ReleaseOnReceivedHeadCallback(); |
Liviu Tinta | d97a6a3 | 2022-12-08 23:28:40 | [diff] [blame] | 367 | |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 368 | void StartTimeoutTimer(base::TimeDelta timeout, |
| 369 | base::OnceClosure on_timeout_callback); |
| 370 | |
Liviu Tinta | d97a6a3 | 2022-12-08 23:28:40 | [diff] [blame] | 371 | // Returns the head of the prefetched response. If there is no valid response, |
| 372 | // then returns null. |
| 373 | const network::mojom::URLResponseHead* GetHead(); |
| 374 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 375 | // Returns the time between the prefetch request was sent and the time the |
| 376 | // response headers were received. Not set if the prefetch request hasn't been |
| 377 | // sent or the response headers haven't arrived. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 378 | std::optional<base::TimeDelta> GetPrefetchHeaderLatency() const { |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 379 | return header_latency_; |
| 380 | } |
| 381 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 382 | // Allow for the serving page to metrics when changes to the prefetch occur. |
| 383 | void SetServingPageMetrics(base::WeakPtr<PrefetchServingPageMetricsContainer> |
| 384 | serving_page_metrics_container); |
| 385 | void UpdateServingPageMetrics(); |
| 386 | |
Iman Saboori | 65c356d | 2022-07-15 14:47:47 | [diff] [blame] | 387 | // Returns request id to be used by DevTools |
| 388 | const std::string& RequestId() const { return request_id_; } |
| 389 | |
| 390 | // Sets DevTools observer |
| 391 | void SetDevToolsObserver( |
Johann | a9fe85f | 2023-01-17 10:15:43 | [diff] [blame] | 392 | base::WeakPtr<SpeculationHostDevToolsObserver> devtools_observer) { |
Iman Saboori | 65c356d | 2022-07-15 14:47:47 | [diff] [blame] | 393 | devtools_observer_ = std::move(devtools_observer); |
| 394 | } |
| 395 | |
| 396 | // Returns DevTool observer |
| 397 | const base::WeakPtr<SpeculationHostDevToolsObserver>& GetDevToolsObserver() |
| 398 | const { |
| 399 | return devtools_observer_; |
| 400 | } |
| 401 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 402 | const std::optional<PrefetchResponseSizes>& GetPrefetchResponseSizes() const { |
William Liu | ef93473 | 2022-11-02 22:46:29 | [diff] [blame] | 403 | return prefetch_response_sizes_; |
| 404 | } |
| 405 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 406 | bool HasPreloadingAttempt() { return !!attempt_; } |
Simon Pelchat | 9ed3e99 | 2023-02-17 01:16:16 | [diff] [blame] | 407 | base::WeakPtr<PreloadingAttempt> preloading_attempt() { return attempt_; } |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 408 | |
| 409 | // Simulates a prefetch container that reaches the interceptor. It sets the |
| 410 | // `attempt_` to the correct state: `PreloadingEligibility::kEligible`, |
| 411 | // `PreloadingHoldbackStatus::kAllowed` and |
| 412 | // `PreloadingTriggeringOutcome::kReady`. |
| 413 | void SimulateAttemptAtInterceptorForTest(); |
| 414 | void DisablePrecogLoggingForTest() { attempt_ = nullptr; } |
| 415 | |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 416 | const std::optional<net::HttpNoVarySearchData>& GetNoVarySearchData() const { |
Hiroshige Hayashizaki | 47a8363 | 2023-06-16 18:09:22 | [diff] [blame] | 417 | return no_vary_search_data_; |
| 418 | } |
Hiroshige Hayashizaki | 2cbf67b9 | 2023-10-23 17:40:07 | [diff] [blame] | 419 | // Sets `no_vary_search_data_` from `GetHead()`. Exposed for tests. |
| 420 | void SetNoVarySearchData(RenderFrameHost* rfh); |
Hiroshige Hayashizaki | 47a8363 | 2023-06-16 18:09:22 | [diff] [blame] | 421 | |
Hiroshige Hayashizaki | a51f624be | 2023-10-24 16:29:57 | [diff] [blame] | 422 | // Called when cookies changes are detected via |
| 423 | // `HaveDefaultContextCookiesChanged()`, either for `this` or other |
| 424 | // `PrefetchContainer`s under the same `PrefetchMatchResolver`. |
| 425 | void OnCookiesChanged(); |
| 426 | |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 427 | class SinglePrefetch; |
| 428 | |
| 429 | // A `Reader` represents the current state of serving. |
| 430 | // The `Reader` methods all operate on the currently *serving* |
| 431 | // `SinglePrefetch`, which is the element in |redirect_chain_| at index |
| 432 | // |index_redirect_chain_to_serve_|. |
| 433 | // |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 434 | // This works like `base::WeakPtr<PrefetchContainer>` plus additional states, |
| 435 | // so check that the reader is valid (e.g. `if (reader)`) before calling other |
| 436 | // methods (except for `Clone()`). |
| 437 | // |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 438 | // TODO(crbug.com/1449360): Allow multiple Readers for a PrefetchContainer. |
| 439 | // This might need ownership/lifetime changes of `Reader` and further cleaning |
| 440 | // up the dependencies between `PrefetchContainer` and `Reader`. |
| 441 | class CONTENT_EXPORT Reader final { |
| 442 | public: |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 443 | Reader(); |
| 444 | |
| 445 | Reader(base::WeakPtr<PrefetchContainer> prefetch_container, |
| 446 | size_t index_redirect_chain_to_serve); |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 447 | |
| 448 | Reader(const Reader&) = delete; |
| 449 | Reader& operator=(const Reader&) = delete; |
| 450 | |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 451 | Reader(Reader&&); |
| 452 | Reader& operator=(Reader&&); |
| 453 | |
| 454 | ~Reader(); |
| 455 | |
| 456 | PrefetchContainer* GetPrefetchContainer() const { |
| 457 | return prefetch_container_.get(); |
| 458 | } |
| 459 | Reader Clone() const; |
| 460 | |
| 461 | // Returns true if `this` is valid. |
| 462 | // Do not call methods below if false. |
| 463 | explicit operator bool() const { return GetPrefetchContainer(); } |
| 464 | |
| 465 | // Methods redirecting to `prefetch_container_`. |
Hiroshige Hayashizaki | e556eb0 | 2023-09-13 00:20:48 | [diff] [blame] | 466 | PrefetchContainer::ServableState GetServableState( |
| 467 | base::TimeDelta cacheable_duration) const; |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 468 | bool HasPrefetchStatus() const; |
| 469 | PrefetchStatus GetPrefetchStatus() const; |
| 470 | |
| 471 | // Returns whether the Reader reached the end. If true, the methods below |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 472 | // shouldn't be called, because the current `SinglePrefetch` doesn't exist. |
| 473 | bool IsEnd() const; |
| 474 | |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 475 | // Whether or not an isolated network context is required to serve. |
| 476 | bool IsIsolatedNetworkContextRequiredToServe() const; |
| 477 | |
| 478 | PrefetchNetworkContext* GetCurrentNetworkContextToServe() const; |
| 479 | |
| 480 | bool HaveDefaultContextCookiesChanged() const; |
| 481 | |
| 482 | // Before a prefetch can be served, any cookies added to the isolated |
| 483 | // network context must be copied over to the default network context. These |
| 484 | // functions are used to check and update the status of this process, as |
| 485 | // well as record metrics about how long this process takes. |
| 486 | bool HasIsolatedCookieCopyStarted() const; |
| 487 | bool IsIsolatedCookieCopyInProgress() const; |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 488 | void OnIsolatedCookieCopyStart() const; |
| 489 | void OnIsolatedCookiesReadCompleteAndWriteStart() const; |
| 490 | void OnIsolatedCookieCopyComplete() const; |
| 491 | void OnInterceptorCheckCookieCopy() const; |
| 492 | void SetOnCookieCopyCompleteCallback(base::OnceClosure callback) const; |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 493 | |
| 494 | // Called with the result of the probe. If the probing feature is enabled, |
| 495 | // then a probe must complete successfully before the prefetch can be |
| 496 | // served. |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 497 | void OnPrefetchProbeResult(PrefetchProbeResult probe_result) const; |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 498 | |
| 499 | // Checks if the given URL matches the the URL that can be served next. |
| 500 | bool DoesCurrentURLToServeMatch(const GURL& url) const; |
| 501 | |
| 502 | // Returns the URL that can be served next. |
| 503 | const GURL& GetCurrentURLToServe() const; |
| 504 | |
Hiroshige Hayashizaki | 3b7e8e06 | 2023-08-14 21:47:39 | [diff] [blame] | 505 | // Gets the current PrefetchResponseReader. |
| 506 | base::WeakPtr<PrefetchResponseReader> |
| 507 | GetCurrentResponseReaderToServeForTesting(); |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 508 | |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 509 | // Called when one element of |redirect_chain_| is served and the next |
| 510 | // element can now be served. |
| 511 | void AdvanceCurrentURLToServe() { index_redirect_chain_to_serve_++; } |
| 512 | |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 513 | // Returns the `SinglePrefetch` to be served next. |
| 514 | const SinglePrefetch& GetCurrentSinglePrefetchToServe() const; |
| 515 | |
Hiroshige Hayashizaki | 8f74d22b | 2023-09-28 04:48:08 | [diff] [blame] | 516 | // See the comment for `PrefetchResponseReader::CreateRequestHandler()`. |
Hiroshige Hayashizaki | c853c030 | 2023-09-13 08:51:07 | [diff] [blame] | 517 | PrefetchRequestHandler CreateRequestHandler(); |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 518 | |
Hiroshige Hayashizaki | 73e4289 | 2023-06-26 16:48:48 | [diff] [blame] | 519 | private: |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 520 | base::WeakPtr<PrefetchContainer> prefetch_container_; |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 521 | |
| 522 | // The index of the element in |prefetch_container_.redirect_chain_| that |
| 523 | // can be served. |
| 524 | size_t index_redirect_chain_to_serve_ = 0; |
| 525 | }; |
| 526 | |
Hiroshige Hayashizaki | 00b3f00 | 2023-07-15 00:32:48 | [diff] [blame] | 527 | Reader CreateReader(); |
Hiroshige Hayashizaki | 6a99ee9 | 2023-05-30 19:54:37 | [diff] [blame] | 528 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 529 | protected: |
Hiroshige Hayashizaki | c0b6aea | 2023-10-17 21:56:43 | [diff] [blame] | 530 | friend class PrefetchContainerTestBase; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 531 | |
| 532 | // Updates metrics based on the result of the prefetch request. |
| 533 | void UpdatePrefetchRequestMetrics( |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 534 | const std::optional<network::URLLoaderCompletionStatus>& |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 535 | completion_status, |
| 536 | const network::mojom::URLResponseHead* head); |
| 537 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 538 | private: |
Robert Lin | 92f3617e | 2023-05-20 10:14:49 | [diff] [blame] | 539 | // Update |prefetch_status_| and report prefetch status to |
| 540 | // DevTools without updating TriggeringOutcome. |
| 541 | void SetPrefetchStatusWithoutUpdatingTriggeringOutcome( |
| 542 | PrefetchStatus prefetch_status); |
| 543 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 544 | // Add client hints headers to a request bound for |origin|. |
| 545 | void AddClientHintsHeaders(const url::Origin& origin, |
| 546 | net::HttpRequestHeaders* request_headers); |
| 547 | |
Hiroshige Hayashizaki | 8ce7d8d | 2023-05-26 00:16:32 | [diff] [blame] | 548 | // Returns the `SinglePrefetch` to be prefetched next. This is the last |
| 549 | // element in `redirect_chain_`, because, during prefetching from the network, |
| 550 | // we push back `SinglePrefetch`s to `redirect_chain_` and access the latest |
| 551 | // redirect hop. |
| 552 | SinglePrefetch& GetCurrentSinglePrefetchToPrefetch() const; |
| 553 | |
| 554 | // Returns the `SinglePrefetch` for the redirect leg before |
| 555 | // `GetCurrentSinglePrefetchToPrefetch()`. This must be called only if `this` |
| 556 | // has redirect(s). |
| 557 | const SinglePrefetch& GetPreviousSinglePrefetchToPrefetch() const; |
| 558 | |
Hiroshige Hayashizaki | 2df4529 | 2023-10-10 22:59:03 | [diff] [blame] | 559 | // The ID of the RenderFrameHost/Document that triggered the prefetch. |
| 560 | const GlobalRenderFrameHostId referring_render_frame_host_id_; |
Kevin McNee | 06824c7 | 2024-02-06 18:59:52 | [diff] [blame] | 561 | // The origin of the page that requested the prefetch. |
| 562 | const url::Origin referring_origin_; |
| 563 | // The URL of the page that requested the prefetch, stored as a hash as we |
| 564 | // just need it for equality checks for metrics. |
| 565 | const size_t referring_url_hash_; |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 566 | |
Kouhei Ueno | 8ce5445 | 2023-11-21 03:58:02 | [diff] [blame] | 567 | // The key used to match this PrefetchContainer, including the URL that was |
| 568 | // requested to prefetch. |
| 569 | const PrefetchContainer::Key key_; |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 570 | |
| 571 | // The type of this prefetch. This controls some specific details about how |
| 572 | // the prefetch is handled, including whether an isolated network context or |
| 573 | // the default network context is used to perform the prefetch, whether or |
| 574 | // not the preftch proxy is used, and whether or not subresources are |
| 575 | // prefetched. |
| 576 | PrefetchType prefetch_type_; |
| 577 | |
Kevin McNee | e6ca089 | 2022-09-28 15:36:22 | [diff] [blame] | 578 | // The referrer to use for the request. |
Devlin Cronin | 7f318c1 | 2023-06-09 00:57:01 | [diff] [blame] | 579 | blink::mojom::Referrer referrer_; |
Kevin McNee | e6ca089 | 2022-09-28 15:36:22 | [diff] [blame] | 580 | |
Jeremy Roman | 45c89d01 | 2023-08-30 21:22:02 | [diff] [blame] | 581 | // Information about the current prefetch request. Updated when a redirect is |
| 582 | // encountered, whether or not the direct can be processed by the same URL |
| 583 | // loader or requires the instantiation of a new loader. |
| 584 | std::unique_ptr<network::ResourceRequest> resource_request_; |
| 585 | |
Hiroshige Hayashizaki | 47a8363 | 2023-06-16 18:09:22 | [diff] [blame] | 586 | // The No-Vary-Search response data, parsed from the actual response header |
| 587 | // (`GetHead()`). |
Hiroshige Hayashizaki | 2cbf67b9 | 2023-10-23 17:40:07 | [diff] [blame] | 588 | // Unless this is set, `no_vary_search` helpers don't perform No-Vary-Search |
| 589 | // matching for `this`, even if `GetHead()` has No-Vary-Search headers. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 590 | std::optional<net::HttpNoVarySearchData> no_vary_search_data_; |
Hiroshige Hayashizaki | 47a8363 | 2023-06-16 18:09:22 | [diff] [blame] | 591 | |
| 592 | // The No-Vary-Search hint of the prefetch, which is specified by the |
| 593 | // speculation rules and can be different from actual `no_vary_search_data_`. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 594 | const std::optional<net::HttpNoVarySearchData> no_vary_search_hint_; |
Liviu Tinta | 68a4580 | 2023-04-05 18:32:20 | [diff] [blame] | 595 | |
Taiyo Mizuhashi | fc6afd6c | 2024-01-26 06:53:07 | [diff] [blame] | 596 | // The |PrefetchDocumentManager| that requested |this|. |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 597 | base::WeakPtr<PrefetchDocumentManager> prefetch_document_manager_; |
| 598 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 599 | // The |BrowserContext| in which this is being run. |
| 600 | base::WeakPtr<BrowserContext> browser_context_; |
| 601 | |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 602 | // The current status, if any, of the prefetch. |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 603 | // TODO(crbug.com/1494771): Use `load_state_` instead for non-metrics purpose. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 604 | std::optional<PrefetchStatus> prefetch_status_; |
Max Curran | 146bf44 | 2022-03-28 23:22:14 | [diff] [blame] | 605 | |
Hiroshige Hayashizaki | 4ce0f429 | 2023-11-07 19:24:58 | [diff] [blame] | 606 | // The current status of the prefetch. |
| 607 | LoadState load_state_ = LoadState::kNotStarted; |
| 608 | |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 609 | // Looks up the proxy settings in the default network context all URLs in |
| 610 | // |redirect_chain_|. |
Max Curran | cc1ab0c | 2022-09-12 22:03:11 | [diff] [blame] | 611 | std::unique_ptr<ProxyLookupClientImpl> proxy_lookup_client_; |
| 612 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 613 | // Whether this prefetch is a decoy or not. If the prefetch is a decoy then |
| 614 | // any prefetched resources will not be served. |
| 615 | bool is_decoy_ = false; |
| 616 | |
Kouhei Ueno | 8ce5445 | 2023-11-21 03:58:02 | [diff] [blame] | 617 | // The redirect chain resulting from prefetching |GetURL()|. |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 618 | std::vector<std::unique_ptr<SinglePrefetch>> redirect_chain_; |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 619 | |
Max Curran | a84311e | 2023-05-16 20:40:25 | [diff] [blame] | 620 | // The network contexts used for this prefetch. They key corresponds to the |
| 621 | // |is_isolated_network_context_required| param of the |
| 622 | // |PrefetchNetworkContext|. |
| 623 | std::map<bool, std::unique_ptr<PrefetchNetworkContext>> network_contexts_; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 624 | |
Hiroshige Hayashizaki | 638018f | 2023-09-12 17:05:45 | [diff] [blame] | 625 | // The currently prefetching streaming URL loader, prefetching the last |
| 626 | // element of `redirect_chain_`. Multiple streaming URL loaders can be used in |
| 627 | // the event a redirect causes a change in the network context, but here only |
| 628 | // one (=last) `PrefetchStreamingURLLoader` is kept here, because when |
| 629 | // switching the network context and `PrefetchStreamingURLLoader`s, the old |
| 630 | // `PrefetchStreamingURLLoader` is scheduled for deletion and then the new |
| 631 | // `PrefetchStreamingURLLoader` is set here. |
| 632 | base::WeakPtr<PrefetchStreamingURLLoader> streaming_loader_; |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 633 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 634 | // The time at which |prefetched_response_| was received. This is used to |
| 635 | // determine whether or not |prefetched_response_| is stale. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 636 | std::optional<base::TimeTicks> prefetch_received_time_; |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 637 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 638 | ukm::SourceId ukm_source_id_; |
| 639 | |
William Liu | ef93473 | 2022-11-02 22:46:29 | [diff] [blame] | 640 | // The sizes information of the prefetched response. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 641 | std::optional<PrefetchResponseSizes> prefetch_response_sizes_; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 642 | |
| 643 | // The amount of time it took for the prefetch to complete. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 644 | std::optional<base::TimeDelta> fetch_duration_; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 645 | |
| 646 | // The amount of time it took for the headers to be received. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 647 | std::optional<base::TimeDelta> header_latency_; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 648 | |
| 649 | // Whether or not a navigation to this prefetch occurred. |
| 650 | bool navigated_to_ = false; |
| 651 | |
| 652 | // The result of probe when checked on navigation. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 653 | std::optional<PrefetchProbeResult> probe_result_; |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 654 | |
Max Curran | 892ca542 | 2022-12-12 20:55:34 | [diff] [blame] | 655 | // Reference to metrics related to the page that considered using this |
| 656 | // prefetch. |
| 657 | base::WeakPtr<PrefetchServingPageMetricsContainer> |
| 658 | serving_page_metrics_container_; |
| 659 | |
Iman Saboori | 65c356d | 2022-07-15 14:47:47 | [diff] [blame] | 660 | // Request identifier used by DevTools |
| 661 | std::string request_id_; |
| 662 | |
| 663 | // Weak pointer to DevTools observer |
| 664 | base::WeakPtr<SpeculationHostDevToolsObserver> devtools_observer_; |
| 665 | |
William Liu | 7708905 | 2022-12-15 18:53:35 | [diff] [blame] | 666 | // `PreloadingAttempt` is used to track the lifecycle of the preloading event, |
| 667 | // and reports various statuses to UKM dashboard. It is initialised along with |
| 668 | // `this`, and destroyed when `WCO::DidFinishNavigation` is fired. |
| 669 | // `attempt_`'s eligibility is set in `OnEligibilityCheckComplete`, and its |
| 670 | // holdback status, triggering outcome and failure reason are set in |
| 671 | // `SetPrefetchStatus`. |
| 672 | base::WeakPtr<PreloadingAttempt> attempt_; |
| 673 | |
Robert Lin | a5f5ca7 | 2023-03-23 02:44:48 | [diff] [blame] | 674 | // A DevTools token used to identify initiator document if the prefetch is |
| 675 | // triggered by SpeculationRules. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 676 | std::optional<base::UnguessableToken> initiator_devtools_navigation_token_ = |
| 677 | std::nullopt; |
Robert Lin | a5f5ca7 | 2023-03-23 02:44:48 | [diff] [blame] | 678 | |
Max Curran | 983f21a5 | 2023-03-23 23:52:58 | [diff] [blame] | 679 | // The time at which |PrefetchService| started blocking until the head of |
| 680 | // |this| was received. |
Arthur Sonzogni | c686e8f | 2024-01-11 08:36:37 | [diff] [blame] | 681 | std::optional<base::TimeTicks> blocked_until_head_start_time_; |
Max Curran | 983f21a5 | 2023-03-23 23:52:58 | [diff] [blame] | 682 | |
Max Curran | e6679ca | 2023-06-06 19:01:39 | [diff] [blame] | 683 | // A timer used to limit the maximum amount of time that a navigation can be |
| 684 | // blocked waiting for the head of this prefetch to be received. |
| 685 | std::unique_ptr<base::OneShotTimer> block_until_head_timer_; |
| 686 | |
Hiroshige Hayashizaki | b5879de | 2023-07-14 20:22:13 | [diff] [blame] | 687 | // Called when `OnReceivedHead()` is called. |
| 688 | base::OnceClosure on_received_head_callback_; |
| 689 | |
Hiroshige Hayashizaki | d2161a2e | 2023-10-23 16:22:50 | [diff] [blame] | 690 | std::unique_ptr<base::OneShotTimer> timeout_timer_; |
| 691 | |
Jeremy Roman | 586b5ec | 2024-02-13 15:14:40 | [diff] [blame] | 692 | // Whether JavaScript is on in this contents (or was, when this prefetch |
| 693 | // started). This affects Client Hints behavior. Per-origin settings are |
| 694 | // handled later, according to |
| 695 | // |ClientHintsControllerDelegate::IsJavaScriptAllowed|. |
| 696 | bool is_javascript_enabled_ = false; |
| 697 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 698 | base::WeakPtrFactory<PrefetchContainer> weak_method_factory_{this}; |
| 699 | }; |
| 700 | |
Hiroshige Hayashizaki | 3876eee | 2023-03-27 04:42:04 | [diff] [blame] | 701 | // For debug logs. |
| 702 | CONTENT_EXPORT std::ostream& operator<<( |
| 703 | std::ostream& ostream, |
| 704 | const PrefetchContainer& prefetch_container); |
| 705 | |
Hiroshige Hayashizaki | 4d53c80 | 2023-09-28 05:36:40 | [diff] [blame] | 706 | CONTENT_EXPORT std::ostream& operator<<( |
| 707 | std::ostream& ostream, |
Hiroshige Hayashizaki | f7f16b8 | 2023-10-10 01:57:47 | [diff] [blame] | 708 | const PrefetchContainer::Key& prefetch_key); |
| 709 | |
| 710 | CONTENT_EXPORT std::ostream& operator<<( |
| 711 | std::ostream& ostream, |
Hiroshige Hayashizaki | 4d53c80 | 2023-09-28 05:36:40 | [diff] [blame] | 712 | PrefetchContainer::ServableState servable_state); |
| 713 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 714 | } // namespace content |
| 715 | |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 716 | #endif // CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_CONTAINER_H_ |