blob: 6fa40d787581096864db149cf69881b5dc734393 [file] [log] [blame]
Hiroshige Hayashizaki16b6e54f2025-08-12 06:56:571// Copyright 2025 The Chromium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SERVING_HANDLE_H_
6#define CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SERVING_HANDLE_H_
7
Hiroshige Hayashizaki386c5022025-08-12 14:43:378#include "base/memory/weak_ptr.h"
9#include "content/browser/preloading/prefetch/prefetch_streaming_url_loader_common_types.h"
10#include "content/common/content_export.h"
11
12class GURL;
Hiroshige Hayashizaki16b6e54f2025-08-12 06:56:5713
14namespace content {
15
Hiroshige Hayashizaki386c5022025-08-12 14:43:3716class PrefetchContainer;
17class PrefetchNetworkContext;
18class PrefetchResponseReader;
19class PrefetchSingleRedirectHop;
20class ServiceWorkerClient;
21enum class PrefetchProbeResult;
22enum class PrefetchServableState;
23enum class PrefetchStatus;
24
25// A `PrefetchServingHandle` represents the current state of serving
26// for a `PrefetchContainer`. The `PrefetchServingHandle` methods all
27// operate on the currently *serving* `PrefetchSingleRedirectHop`,
28// which is the element in |PrefetchContainer::redirect_chain()| at index
29// |index_redirect_chain_to_serve_|.
30//
31// This works like `base::WeakPtr<PrefetchContainer>` plus additional states,
32// so check that the reader is valid (e.g. `if (reader)`) before calling other
33// methods (except for `Clone()`).
34class CONTENT_EXPORT PrefetchServingHandle final {
35 public:
36 PrefetchServingHandle();
37
38 PrefetchServingHandle(base::WeakPtr<PrefetchContainer> prefetch_container,
39 size_t index_redirect_chain_to_serve);
40
41 PrefetchServingHandle(const PrefetchServingHandle&) = delete;
42 PrefetchServingHandle& operator=(const PrefetchServingHandle&) = delete;
43
44 PrefetchServingHandle(PrefetchServingHandle&&);
45 PrefetchServingHandle& operator=(PrefetchServingHandle&&);
46
47 ~PrefetchServingHandle();
48
49 PrefetchContainer* GetPrefetchContainer() const {
50 return prefetch_container_.get();
51 }
52 PrefetchServingHandle Clone() const;
53
54 // Returns true if `this` is valid.
55 // Do not call methods below if false.
56 explicit operator bool() const { return GetPrefetchContainer(); }
57
58 // Methods redirecting to `GetPrefetchContainer()`.
59 PrefetchServableState GetServableState(
60 base::TimeDelta cacheable_duration) const;
61 bool HasPrefetchStatus() const;
62 PrefetchStatus GetPrefetchStatus() const;
63
64 // Returns whether `this` reached the end. If true, the methods below
65 // shouldn't be called, because the current `SingleRedirectHop` doesn't exist.
66 bool IsEnd() const;
67
68 // Whether or not an isolated network context is required to serve.
69 bool IsIsolatedNetworkContextRequiredToServe() const;
70
71 PrefetchNetworkContext* GetCurrentNetworkContextToServe() const;
72
73 bool HaveDefaultContextCookiesChanged() const;
74
75 // Before a prefetch can be served, any cookies added to the isolated
76 // network context must be copied over to the default network context. These
77 // functions are used to check and update the status of this process, as
78 // well as record metrics about how long this process takes.
79 bool HasIsolatedCookieCopyStarted() const;
80 bool IsIsolatedCookieCopyInProgress() const;
81 void OnIsolatedCookieCopyStart() const;
82 void OnIsolatedCookiesReadCompleteAndWriteStart() const;
83 void OnIsolatedCookieCopyComplete() const;
84 void OnInterceptorCheckCookieCopy() const;
85 void SetOnCookieCopyCompleteCallback(base::OnceClosure callback) const;
86
87 // Called with the result of the probe. If the probing feature is enabled,
88 // then a probe must complete successfully before the prefetch can be
89 // served.
90 void OnPrefetchProbeResult(PrefetchProbeResult probe_result) const;
91
92 // Checks if the given URL matches the the URL that can be served next.
93 bool DoesCurrentURLToServeMatch(const GURL& url) const;
94
95 // Returns the URL that can be served next.
96 const GURL& GetCurrentURLToServe() const;
97
98 // Gets the current PrefetchResponseReader.
99 base::WeakPtr<PrefetchResponseReader>
100 GetCurrentResponseReaderToServeForTesting();
101
102 // Called when one element of |redirect_chain_| is served and the next
103 // element can now be served.
104 void AdvanceCurrentURLToServe() { index_redirect_chain_to_serve_++; }
105
106 // See the comment for `PrefetchResponseReader::CreateRequestHandler()`.
107 std::pair<PrefetchRequestHandler, base::WeakPtr<ServiceWorkerClient>>
108 CreateRequestHandler();
109
110 // See the corresponding functions on `PrefetchResponseReader`.
111 // These apply to the current `SingleRedirectHop` (and so, may change as the
112 // prefetch advances through a redirect change).
113 bool VariesOnCookieIndices() const;
114 bool MatchesCookieIndices(
115 base::span<const std::pair<std::string, std::string>> cookies) const;
116
117 private:
118 const std::vector<std::unique_ptr<PrefetchSingleRedirectHop>>&
119 redirect_chain() const;
120
121 // Returns the `SingleRedirectHop` to be served next.
122 const PrefetchSingleRedirectHop& GetCurrentSingleRedirectHopToServe() const;
123
124 base::WeakPtr<PrefetchContainer> prefetch_container_;
125
126 // The index of the element in |GetPrefetchContainer()->redirect_chain()| that
127 // can be served.
128 size_t index_redirect_chain_to_serve_ = 0;
129};
Hiroshige Hayashizaki16b6e54f2025-08-12 06:56:57130
131} // namespace content
132
133#endif // CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_SERVING_HANDLE_H_