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 | #ifndef CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_DOCUMENT_MANAGER_H_ |
| 6 | #define CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_DOCUMENT_MANAGER_H_ |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 7 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 8 | #include <map> |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 9 | |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 10 | #include "base/functional/callback.h" |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 11 | #include "base/memory/weak_ptr.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 12 | #include "content/common/content_export.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 13 | #include "content/public/browser/document_user_data.h" |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 14 | #include "content/public/browser/prefetch_metrics.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 15 | #include "third_party/blink/public/mojom/speculation_rules/speculation_rules.mojom.h" |
Hiroshige Hayashizaki | 583b496 | 2025-08-19 18:58:26 | [diff] [blame] | 16 | #include "third_party/blink/public/mojom/tokens/tokens.mojom.h" |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 17 | #include "url/gurl.h" |
| 18 | |
| 19 | namespace content { |
| 20 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 21 | class PrefetchContainer; |
Hiroshige Hayashizaki | 22be5e4c | 2025-03-07 21:39:59 | [diff] [blame] | 22 | class PrefetchHandle; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 23 | class PrefetchService; |
Hiroshige Hayashizaki | 583b496 | 2025-08-19 18:58:26 | [diff] [blame] | 24 | class PrefetchType; |
| 25 | class PreloadPipelineInfo; |
Kevin McNee | 7ca2e85 | 2024-04-30 02:38:31 | [diff] [blame] | 26 | class PreloadingPredictor; |
Hiroshige Hayashizaki | 583b496 | 2025-08-19 18:58:26 | [diff] [blame] | 27 | class SpeculationRulesTags; |
| 28 | enum class PreloadingType; |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 29 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 30 | // Manages the state of and tracks metrics about prefetches for a single page |
| 31 | // load. |
| 32 | class CONTENT_EXPORT PrefetchDocumentManager |
Kouhei Ueno | 136156d | 2024-03-12 04:26:58 | [diff] [blame] | 33 | : public DocumentUserData<PrefetchDocumentManager> { |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 34 | public: |
Adithya Srinivasan | d476f4f8 | 2023-06-20 15:55:13 | [diff] [blame] | 35 | using PrefetchDestructionCallback = |
| 36 | base::RepeatingCallback<void(const GURL&)>; |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 37 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 38 | ~PrefetchDocumentManager() override; |
| 39 | |
| 40 | PrefetchDocumentManager(const PrefetchDocumentManager&) = delete; |
| 41 | const PrefetchDocumentManager operator=(const PrefetchDocumentManager&) = |
| 42 | delete; |
| 43 | |
Hiroshige Hayashizaki | 6a2bc75 | 2023-10-31 19:08:11 | [diff] [blame] | 44 | // Returns the `PrefetchDocumentManager` associated with a Document if already |
| 45 | // exists, or `nullptr` otherwise. |
| 46 | static PrefetchDocumentManager* FromDocumentToken( |
| 47 | int process_id, |
| 48 | const blink::DocumentToken& document_token); |
| 49 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 50 | // Processes the given speculation candidates to see if they can be |
| 51 | // prefetched. Any candidates that can be prefetched are removed from |
| 52 | // |candidates|, and a prefetch for the URL of the candidate is started. |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 53 | void ProcessCandidates( |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 54 | std::vector<blink::mojom::SpeculationCandidatePtr>& candidates); |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 55 | |
Adithya Srinivasan | 3bdb5ce | 2023-06-05 13:22:12 | [diff] [blame] | 56 | // Attempts to prefetch the given candidate. Returns true if a new prefetch |
| 57 | // for the candidate's URL is started. |
kenoss | 1ce36df59 | 2024-12-03 01:04:35 | [diff] [blame] | 58 | bool MaybePrefetch(blink::mojom::SpeculationCandidatePtr candidate, |
| 59 | const PreloadingPredictor& enacting_predictor); |
Adithya Srinivasan | 3bdb5ce | 2023-06-05 13:22:12 | [diff] [blame] | 60 | |
kenoss | 3bd73b8 | 2024-10-10 20:33:49 | [diff] [blame] | 61 | void PrefetchAheadOfPrerender( |
| 62 | scoped_refptr<PreloadPipelineInfo> preload_pipeline_info, |
| 63 | blink::mojom::SpeculationCandidatePtr candidate, |
| 64 | const PreloadingPredictor& enacting_predictor); |
kenoss | 9d57ee7b | 2024-06-03 01:36:43 | [diff] [blame] | 65 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 66 | // Starts the process to prefetch |url| with the given |prefetch_type|. |
Rulong Chen(陈汝龙) | bf12169c | 2024-12-16 05:38:16 | [diff] [blame] | 67 | void PrefetchUrl(const GURL& url, |
| 68 | const PrefetchType& prefetch_type, |
| 69 | const PreloadingPredictor& enacting_predictor, |
Rulong Chen(陈汝龙) | bf12169c | 2024-12-16 05:38:16 | [diff] [blame] | 70 | const blink::mojom::Referrer& referrer, |
Hiroki Nakagawa | c33034f | 2025-03-31 07:39:30 | [diff] [blame] | 71 | std::optional<SpeculationRulesTags> speculation_rules_tags, |
Rulong Chen(陈汝龙) | bf12169c | 2024-12-16 05:38:16 | [diff] [blame] | 72 | const network::mojom::NoVarySearchPtr& no_vary_search_hint, |
| 73 | scoped_refptr<PreloadPipelineInfo> preload_pipeline_info); |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 74 | |
Max Curran | 0146f85 | 2022-08-01 19:49:33 | [diff] [blame] | 75 | // Checking the canary cache can be a slow and blocking operation (see |
| 76 | // crbug.com/1266018), so we only do this for the first non-decoy prefetch we |
| 77 | // make on the page. |
| 78 | bool HaveCanaryChecksStarted() const { return have_canary_checks_started_; } |
| 79 | void OnCanaryChecksStarted() { have_canary_checks_started_ = true; } |
| 80 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 81 | // Returns metrics for prefetches requested by the associated page load. |
| 82 | PrefetchReferringPageMetrics& GetReferringPageMetrics() { |
| 83 | return referring_page_metrics_; |
| 84 | } |
| 85 | |
| 86 | // Updates metrics when the eligibility check for a prefetch requested by this |
| 87 | // page load is completed. |
| 88 | void OnEligibilityCheckComplete(bool is_eligible); |
| 89 | |
| 90 | // Updates metrics when the response for a prefetch requested by this page |
| 91 | // load is received. |
Adithya Srinivasan | f393dd0 | 2023-05-16 20:26:16 | [diff] [blame] | 92 | void OnPrefetchSuccessful(PrefetchContainer* prefetch); |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 93 | |
Iman Saboori | ab85b13 | 2022-11-15 22:17:23 | [diff] [blame] | 94 | // Whether the prefetch attempt for target |url| failed or discarded |
| 95 | bool IsPrefetchAttemptFailedOrDiscarded(const GURL& url); |
| 96 | |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 97 | // Returns a tuple: (can_prefetch_now, prefetch_to_evict). 'can_prefetch_now' |
| 98 | // is true if we can prefetch |next_prefetch| based on the state of the |
kenoss | 6ccd702 | 2025-05-20 14:24:21 | [diff] [blame] | 99 | // document, and the number of existing completed prefetches. The eagerness of |
| 100 | // |next_prefetch| is taken into account when making the decision. |
| 101 | // 'prefetch_to_evict' is set to an existing prefetch if one needs to be |
| 102 | // evicted to make space for the prefetch of |next_prefetch|, or nullptr |
| 103 | // otherwise. 'prefetch_to_evict' will only be non-null if 'can_prefetch_now' |
| 104 | // is true. |
Adithya Srinivasan | d5e37cb | 2023-07-05 14:45:23 | [diff] [blame] | 105 | std::tuple<bool, base::WeakPtr<PrefetchContainer>> CanPrefetchNow( |
| 106 | PrefetchContainer* next_prefetch); |
Adithya Srinivasan | f393dd0 | 2023-05-16 20:26:16 | [diff] [blame] | 107 | |
Adithya Srinivasan | d476f4f8 | 2023-06-20 15:55:13 | [diff] [blame] | 108 | // See documentation for |prefetch_destruction_callback_|. |
| 109 | void SetPrefetchDestructionCallback(PrefetchDestructionCallback callback); |
| 110 | |
| 111 | // Called when a PrefetchContainer started by |this| is being destroyed. |
| 112 | void PrefetchWillBeDestroyed(PrefetchContainer* prefetch); |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 113 | |
Max Curran | 5d4da4b4 | 2023-03-10 23:41:46 | [diff] [blame] | 114 | base::WeakPtr<PrefetchDocumentManager> GetWeakPtr() { |
| 115 | return weak_method_factory_.GetWeakPtr(); |
| 116 | } |
| 117 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 118 | static void SetPrefetchServiceForTesting(PrefetchService* prefetch_service); |
| 119 | |
kenoss | 087ae23 | 2024-12-10 05:06:04 | [diff] [blame] | 120 | void ResetPrefetchAheadOfPrerenderIfExist(const GURL& url); |
| 121 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 122 | private: |
| 123 | explicit PrefetchDocumentManager(RenderFrameHost* rfh); |
| 124 | friend DocumentUserData; |
| 125 | |
Max Curran | c4445fc | 2022-06-02 18:43:43 | [diff] [blame] | 126 | // Helper function to get the |PrefetchService| associated with |this|. |
| 127 | PrefetchService* GetPrefetchService() const; |
| 128 | |
kenoss | f7d06314 | 2024-10-10 08:34:28 | [diff] [blame] | 129 | bool IsPrefetchAttemptFailedOrDiscardedInternal( |
| 130 | const GURL& url, |
| 131 | PreloadingType planned_max_preloading_type); |
| 132 | |
Hiroshige Hayashizaki | 2df4529 | 2023-10-10 22:59:03 | [diff] [blame] | 133 | blink::DocumentToken document_token_; |
| 134 | |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 135 | // This map holds references to all |PrefetchContainer| associated with |
Hiroshige Hayashizaki | 7a34d018 | 2023-11-07 20:54:34 | [diff] [blame] | 136 | // |this|. |
kenoss | f7d06314 | 2024-10-10 08:34:28 | [diff] [blame] | 137 | // |
| 138 | // Keyed with `(url, planned_max_preloading_type)`. |
| 139 | // `planned_max_preloading_type == kPrerender` indicates it's ahead of |
| 140 | // prerender. |
| 141 | // |
| 142 | // We allow normal prefetch and prefetch ahead of prerender with the same key |
| 143 | // here, to handle and merge them in `PrefetchService`. |
Hiroshige Hayashizaki | 22be5e4c | 2025-03-07 21:39:59 | [diff] [blame] | 144 | std::map<std::pair<GURL, PreloadingType>, std::unique_ptr<PrefetchHandle>> |
kenoss | f7d06314 | 2024-10-10 08:34:28 | [diff] [blame] | 145 | all_prefetches_; |
Max Curran | 646fb64 | 2022-03-16 00:44:09 | [diff] [blame] | 146 | |
Max Curran | 0146f85 | 2022-08-01 19:49:33 | [diff] [blame] | 147 | // Stores whether or not canary checks have been started for this page. |
| 148 | bool have_canary_checks_started_{false}; |
| 149 | |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 150 | // A list of immediate prefetch requests (from this page) that have completed |
Adithya Srinivasan | 6bdb9bcbf | 2023-06-23 21:28:46 | [diff] [blame] | 151 | // (oldest to newest). |
Takashi Nakayama | 978f0a15 | 2025-06-17 08:26:25 | [diff] [blame] | 152 | std::vector<base::WeakPtr<PrefetchContainer>> completed_immediate_prefetches_; |
| 153 | // A list of non-immediate prefetch requests (from this page) that have |
| 154 | // completed (oldest to newest). |
| 155 | std::vector<base::WeakPtr<PrefetchContainer>> |
| 156 | completed_non_immediate_prefetches_; |
Adithya Srinivasan | f393dd0 | 2023-05-16 20:26:16 | [diff] [blame] | 157 | |
Max Curran | 210cffa | 2022-09-06 22:24:31 | [diff] [blame] | 158 | // Metrics related to the prefetches requested by this page load. |
| 159 | PrefetchReferringPageMetrics referring_page_metrics_; |
| 160 | |
Adithya Srinivasan | d476f4f8 | 2023-06-20 15:55:13 | [diff] [blame] | 161 | // Callback that is run when a prefetch started by |this| is being destroyed. |
| 162 | PrefetchDestructionCallback prefetch_destruction_callback_; |
Adithya Srinivasan | d1c68ba | 2023-05-30 19:54:25 | [diff] [blame] | 163 | |
Max Curran | 18a6f2b | 2022-05-02 23:13:24 | [diff] [blame] | 164 | base::WeakPtrFactory<PrefetchDocumentManager> weak_method_factory_{this}; |
| 165 | |
Max Curran | 6c2835ea | 2022-03-07 19:52:38 | [diff] [blame] | 166 | DOCUMENT_USER_DATA_KEY_DECL(); |
| 167 | }; |
| 168 | |
| 169 | } // namespace content |
| 170 | |
Sreeja Kamishetty | f66553a | 2022-07-14 17:41:27 | [diff] [blame] | 171 | #endif // CONTENT_BROWSER_PRELOADING_PREFETCH_PREFETCH_DOCUMENT_MANAGER_H_ |