File: chrome_preloading.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (255 lines) | stat: -rw-r--r-- 10,404 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_PRELOADING_CHROME_PRELOADING_H_
#define CHROME_BROWSER_PRELOADING_CHROME_PRELOADING_H_

#include <string>

#include "content/public/browser/browser_context.h"
#include "content/public/browser/preloading.h"

#include "url/gurl.h"

class TemplateURLService;

// If you change any of the following enums or static variables, please follow
// the process in go/preloading-dashboard-updates to update the mapping
// reflected in dashboard, or if you are not a Googler, please file an FYI bug
// on https://crbug.new with component Internals>Preload.

// Defines various embedder triggering mechanisms which triggers different
// preloading operations mentioned in //content/public/browser/preloading.h.

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// Advance numbering by +1 when adding a new element.
//
// Please make sure Chrome `PreloadingPredictor` are defined after 100
// (inclusive) as 99 and below are reserved for content-public and
// content-internal definitions. Both the value and the name should be unique
// across all the namespaces.
//
// LINT.IfChange
namespace chrome_preloading_predictor {
// When the preloading URL is predicted from the Omnibox Direct URL Input
// (DUI). This is used to perform various preloading operations like prefetch
// and prerender to load Omnibox predicted URLs faster.
inline constexpr content::PreloadingPredictor kOmniboxDirectURLInput(
    100,
    "OmniboxDirectURLInput");

// When a pointerdown (e.g. mousedown or touchstart) event happens on an
// anchor element with an href value pointing to an HTTP(S) origin, we may
// attempt to preload the link.
inline constexpr content::PreloadingPredictor kPointerDownOnAnchor(
    101,
    "PointerDownOnAnchor");

// When the preloading URL is predicted from the default search suggest
// service for faster search page loads.
inline constexpr content::PreloadingPredictor kDefaultSearchEngine(
    102,
    "DefaultSearchEngine");

// When the preloading URL is predicted from the default search suggest due to
// change in Omnibox selection.
inline constexpr content::PreloadingPredictor kOmniboxSearchPredictor(
    103,
    "OmniboxSearchPredictor");

// When the preloading URL is predicted from the default search suggest due to
// mouse being pressed down on a Omnibox Search suggestion.
inline constexpr content::PreloadingPredictor kOmniboxMousePredictor(
    104,
    "OmniboxMousePredictor");

// When the default match in omnibox has the search prefetch or prerender
// hint.
inline constexpr content::PreloadingPredictor kOmniboxSearchSuggestDefaultMatch(
    105,
    "OmniboxSearchSuggestDefaultMatch");

// When the user hovers their mouse over the back button.
inline constexpr content::PreloadingPredictor kBackButtonHover(
    106,
    "BackButtonHover");

// When a pointerdown (e.g. mousedown or touchstart) event happens on an
// bookmark bar link to an HTTPS origin, we may attempt to preload the link.
inline constexpr content::PreloadingPredictor kPointerDownOnBookmarkBar(
    107,
    "PointerDownOnBookmarkBar");

// When a mousehover event happens on a bookmark bar link to an HTTPS origin,
// we may attempt to preload the link.
inline constexpr content::PreloadingPredictor kMouseHoverOnBookmarkBar(
    108,
    "MouseHoverOnBookmarkBar");

// When a pointerdown (e.g. mousedown or touchstart) event happens on a
// new tab page link to an HTTPS origin, we may attempt to preload the link.
// TODO(crbug.com/376421273): This predictor would be replaced by
// kMouseHoverOrMouseDownOnNewTabPage.
inline constexpr content::PreloadingPredictor kPointerDownOnNewTabPage(
    109,
    "PointerDownOnNewTabPage");

// When a mousehover event happens on a new tab page link to an HTTPS origin,
// we may attempt to preload the link.
// TODO(crbug.com/376421273): This predictor would be replaced by
// kMouseHoverOrMouseDownOnNewTabPage.
inline constexpr content::PreloadingPredictor kMouseHoverOnNewTabPage(
    110,
    "MouseHoverOnNewTabPage");

// When the preloading URL is predicted from the default search suggest due to
// the user touching down on a Omnibox Search suggestion.
inline constexpr content::PreloadingPredictor kOmniboxTouchDownPredictor(
    111,
    "OmniboxTouchDownPredirector");

// When the Link-Preview loads a page with prerendering infrastractures.
// TODO(b:291867362): This is not used by the current implementation, but might
// be reused in the future.
inline constexpr content::PreloadingPredictor kLinkPreview(112, "LinkPreview");

// When a mousehover or mousedown event happens on a bookmark bar linking to an
// HTTPS origin, we may attempt to preload the link. This predictor, instead of
// using kPointerDownOnBookmarkBar or kMouseHoverOnBookmarkBar, is for solving
// the problem in https://crbug.com/1516514.
inline constexpr content::PreloadingPredictor
    kMouseHoverOrMouseDownOnBookmarkBar(113,
                                        "MouseHoverOrMouseDownOnBookmarkBar");

// When a touch event happens on a new tab page link to an HTTPS origin,
// we may attempt to preload the link.
inline constexpr content::PreloadingPredictor kTouchOnNewTabPage(
    114,
    "TouchOnNewTabPage");

// When a certain CCT prefetch API is triggered.
inline constexpr content::PreloadingPredictor kChromeCustomTabs(
    115,
    "ChromeCustomTabs");

// When a mousehover or mousedown event happens on a new tab page linking to an
// HTTPS origin, we may attempt to preload the link. This predictor, instead of
// using kPointerDownOnNewTabPage or MouseHoverOnNewTabPage, is for solving
// the problem in https://crbug.com/376421273.
inline constexpr content::PreloadingPredictor
    kMouseHoverOrMouseDownOnNewTabPage(116,
                                       "MouseHoverOrMouseDownOnNewTabPage");

// When the default search engine needs to prerender a prewarm page.
inline constexpr content::PreloadingPredictor kPrewarmDefaultSearchEngine(
    117,
    "PrewarmDefaultSearchEngine");
}  // namespace chrome_preloading_predictor
// LINT.ThenChange()

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
//
// LINT.IfChange
enum class ChromePreloadingEligibility {
  // Numbering starts from `kPreloadingEligibilityContentEnd` defined in
  // //content/public/preloading.h . Advance numbering by +1 when adding a new
  // element.

  // Chrome was unable to get a LoadingPredictor object for the user profile.
  kUnableToGetLoadingPredictor = static_cast<int>(
      content::PreloadingEligibility::kPreloadingEligibilityContentEnd),

  // Preloading was ineligible because Prefetch was not started and Prerender
  // can't be triggered.
  kPrefetchNotStarted =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      1,

  // Preloading was ineligible because Prefetch failed and Prerender can't be
  // triggered.
  kPrefetchFailed =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      2,

  // Preloading was ineligible because Prerender was already consumed and can't
  // be triggered again.
  kPrerenderConsumed =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      3,

  // Preloading was ineligible because the default search engine was not set.
  kSearchEngineNotValid =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      4,

  // Preloading can't be started because there are no search terms present.
  kNoSearchTerms =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      5,

  // Preloading was ineligible due to error in the network request.
  kPreloadingErrorBackOff =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      6,

  // Search urls are not eligible for certain types of preloading triggers.
  KDisallowSearchUrl =
      static_cast<int>(
          content::PreloadingEligibility::kPreloadingEligibilityContentEnd) +
      7,

  kMaxValue = KDisallowSearchUrl,
};
// LINT.ThenChange()

// Helper method to convert ChromePreloadingEligibility to
// content::PreloadingEligibility to avoid casting.
content::PreloadingEligibility ToPreloadingEligibility(
    ChromePreloadingEligibility eligibility);

// Helpers methods to extract search terms from a given URL.
TemplateURLService* GetTemplateURLServiceFromBrowserContext(
    content::BrowserContext* browser_context);
std::u16string ExtractSearchTermsFromURL(
    const TemplateURLService* const template_url_service,
    const GURL& url);
std::u16string ExtractSearchTermsFromURL(
    content::BrowserContext* browser_context,
    const GURL& url);

// Returns true if a canonical URL representation of a `preloading_url` can be
// generated. `canonical_url` is set to the canonical URL representation when
// this method returns `true`. The search query is returned in `search_terms` if
// the passing `search_terms` is not nullptr.
bool HasCanonicalPreloadingOmniboxSearchURL(
    const GURL& preloading_url,
    content::BrowserContext* browser_context,
    GURL* canonical_url,
    std::u16string* search_terms = nullptr);

// Returns true when |navigation_url| is considered as navigating to the same
// omnibox search results page as |canonical_preloading_search_url|.
bool IsSearchDestinationMatch(const GURL& canonical_preloading_search_url,
                              content::BrowserContext* browser_context,
                              const GURL& navigation_url);
// Returns true when |navigation_url| is considered as navigating to the same
// omnibox search results page as |canonical_preloading_search_url|. Includes
// the result from the default web url match operation.
bool IsSearchDestinationMatchWithWebUrlMatchResult(
    const GURL& canonical_preloading_search_url,
    content::BrowserContext* browser_context,
    const GURL& navigation_url,
    const std::optional<content::UrlMatchType>& default_web_url_match =
        std::nullopt);

#endif  // CHROME_BROWSER_PRELOADING_CHROME_PRELOADING_H_