Skip to main content

browser_protocol/preload/
mod.rs

1use serde::{Serialize, Deserialize};
2
3/// Unique id
4
5pub type RuleSetId = String;
6
7/// Corresponds to SpeculationRuleSet
8
9#[derive(Debug, Clone, Serialize, Deserialize, Default)]
10#[serde(rename_all = "camelCase")]
11pub struct RuleSet {
12
13    pub id: RuleSetId,
14    /// Identifies a document which the rule set is associated with.
15
16    pub loaderId: crate::network::LoaderId,
17    /// Source text of JSON representing the rule set. If it comes from
18    /// '\<script\>' tag, it is the textContent of the node. Note that it is
19    /// a JSON for valid case.
20    /// 
21    /// See also:
22    /// - <https://wicg.github.io/nav-speculation/speculation-rules.html>
23    /// - <https://github.com/WICG/nav-speculation/blob/main/triggers.md>
24
25    pub sourceText: String,
26    /// A speculation rule set is either added through an inline
27    /// '\<script\>' tag or through an external resource via the
28    /// 'Speculation-Rules' HTTP header. For the first case, we include
29    /// the BackendNodeId of the relevant '\<script\>' tag. For the second
30    /// case, we include the external URL where the rule set was loaded
31    /// from, and also RequestId if Network domain is enabled.
32    /// 
33    /// See also:
34    /// - <https://wicg.github.io/nav-speculation/speculation-rules.html#speculation-rules-script>
35    /// - <https://wicg.github.io/nav-speculation/speculation-rules.html#speculation-rules-header>
36
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub backendNodeId: Option<crate::dom::BackendNodeId>,
39
40    #[serde(skip_serializing_if = "Option::is_none")]
41    pub url: Option<String>,
42
43    #[serde(skip_serializing_if = "Option::is_none")]
44    pub requestId: Option<crate::network::RequestId>,
45    /// Error information
46    /// 'errorMessage' is null iff 'errorType' is null.
47
48    #[serde(skip_serializing_if = "Option::is_none")]
49    pub errorType: Option<RuleSetErrorType>,
50    /// TODO(<https://crbug.com/1425354>): Replace this property with structured error.
51
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub errorMessage: Option<String>,
54    /// For more details, see:
55    /// <https://github.com/WICG/nav-speculation/blob/main/speculation-rules-tags.md>
56
57    #[serde(skip_serializing_if = "Option::is_none")]
58    pub tag: Option<String>,
59}
60
61
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
63pub enum RuleSetErrorType {
64    #[default]
65    SourceIsNotJsonObject,
66    InvalidRulesSkipped,
67    InvalidRulesetLevelTag,
68}
69
70/// The type of preloading attempted. It corresponds to
71/// mojom::SpeculationAction (although PrefetchWithSubresources is omitted as it
72/// isn't being used by clients).
73
74#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
75pub enum SpeculationAction {
76    #[default]
77    Prefetch,
78    Prerender,
79    PrerenderUntilScript,
80}
81
82/// Corresponds to mojom::SpeculationTargetHint.
83/// See <https://github.com/WICG/nav-speculation/blob/main/triggers.md#window-name-targeting-hints>
84
85#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
86pub enum SpeculationTargetHint {
87    #[default]
88    Blank,
89    SelfValue,
90}
91
92/// A key that identifies a preloading attempt.
93/// 
94/// The url used is the url specified by the trigger (i.e. the initial URL), and
95/// not the final url that is navigated to. For example, prerendering allows
96/// same-origin main frame navigations during the attempt, but the attempt is
97/// still keyed with the initial URL.
98
99#[derive(Debug, Clone, Serialize, Deserialize, Default)]
100#[serde(rename_all = "camelCase")]
101pub struct PreloadingAttemptKey {
102
103    pub loaderId: crate::network::LoaderId,
104
105    pub action: SpeculationAction,
106
107    pub url: String,
108
109    #[serde(skip_serializing_if = "Option::is_none")]
110    pub formSubmission: Option<bool>,
111
112    #[serde(skip_serializing_if = "Option::is_none")]
113    pub targetHint: Option<SpeculationTargetHint>,
114}
115
116/// Lists sources for a preloading attempt, specifically the ids of rule sets
117/// that had a speculation rule that triggered the attempt, and the
118/// BackendNodeIds of \<a href\> or \<area href\> elements that triggered the
119/// attempt (in the case of attempts triggered by a document rule). It is
120/// possible for multiple rule sets and links to trigger a single attempt.
121
122#[derive(Debug, Clone, Serialize, Deserialize, Default)]
123#[serde(rename_all = "camelCase")]
124pub struct PreloadingAttemptSource {
125
126    pub key: PreloadingAttemptKey,
127
128    pub ruleSetIds: Vec<RuleSetId>,
129
130    pub nodeIds: Vec<crate::dom::BackendNodeId>,
131}
132
133/// Chrome manages different types of preloads together using a
134/// concept of preloading pipeline. For example, if a site uses a
135/// SpeculationRules for prerender, Chrome first starts a prefetch and
136/// then upgrades it to prerender.
137/// 
138/// CDP events for them are emitted separately but they share
139/// 'PreloadPipelineId'.
140
141pub type PreloadPipelineId = String;
142
143/// List of FinalStatus reasons for Prerender2.
144
145#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
146pub enum PrerenderFinalStatus {
147    #[default]
148    Activated,
149    Destroyed,
150    LowEndDevice,
151    InvalidSchemeRedirect,
152    InvalidSchemeNavigation,
153    NavigationRequestBlockedByCsp,
154    MojoBinderPolicy,
155    RendererProcessCrashed,
156    RendererProcessKilled,
157    Download,
158    TriggerDestroyed,
159    NavigationNotCommitted,
160    NavigationBadHttpStatus,
161    ClientCertRequested,
162    NavigationRequestNetworkError,
163    CancelAllHostsForTesting,
164    DidFailLoad,
165    Stop,
166    SslCertificateError,
167    LoginAuthRequested,
168    UaChangeRequiresReload,
169    BlockedByClient,
170    AudioOutputDeviceRequested,
171    MixedContent,
172    TriggerBackgrounded,
173    MemoryLimitExceeded,
174    DataSaverEnabled,
175    TriggerUrlHasEffectiveUrl,
176    ActivatedBeforeStarted,
177    InactivePageRestriction,
178    StartFailed,
179    TimeoutBackgrounded,
180    CrossSiteRedirectInInitialNavigation,
181    CrossSiteNavigationInInitialNavigation,
182    SameSiteCrossOriginRedirectNotOptInInInitialNavigation,
183    SameSiteCrossOriginNavigationNotOptInInInitialNavigation,
184    ActivationNavigationParameterMismatch,
185    ActivatedInBackground,
186    EmbedderHostDisallowed,
187    ActivationNavigationDestroyedBeforeSuccess,
188    TabClosedByUserGesture,
189    TabClosedWithoutUserGesture,
190    PrimaryMainFrameRendererProcessCrashed,
191    PrimaryMainFrameRendererProcessKilled,
192    ActivationFramePolicyNotCompatible,
193    PreloadingDisabled,
194    BatterySaverEnabled,
195    ActivatedDuringMainFrameNavigation,
196    PreloadingUnsupportedByWebContents,
197    CrossSiteRedirectInMainFrameNavigation,
198    CrossSiteNavigationInMainFrameNavigation,
199    SameSiteCrossOriginRedirectNotOptInInMainFrameNavigation,
200    SameSiteCrossOriginNavigationNotOptInInMainFrameNavigation,
201    MemoryPressureOnTrigger,
202    MemoryPressureAfterTriggered,
203    PrerenderingDisabledByDevTools,
204    SpeculationRuleRemoved,
205    ActivatedWithAuxiliaryBrowsingContexts,
206    MaxNumOfRunningEagerPrerendersExceeded,
207    MaxNumOfRunningNonEagerPrerendersExceeded,
208    MaxNumOfRunningEmbedderPrerendersExceeded,
209    PrerenderingUrlHasEffectiveUrl,
210    RedirectedPrerenderingUrlHasEffectiveUrl,
211    ActivationUrlHasEffectiveUrl,
212    JavaScriptInterfaceAdded,
213    JavaScriptInterfaceRemoved,
214    AllPrerenderingCanceled,
215    WindowClosed,
216    SlowNetwork,
217    OtherPrerenderedPageActivated,
218    V8OptimizerDisabled,
219    PrerenderFailedDuringPrefetch,
220    BrowsingDataRemoved,
221    PrerenderHostReused,
222    FormSubmitWhenPrerendering,
223}
224
225/// Preloading status values, see also PreloadingTriggeringOutcome. This
226/// status is shared by prefetchStatusUpdated and prerenderStatusUpdated.
227
228#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
229pub enum PreloadingStatus {
230    #[default]
231    Pending,
232    Running,
233    Ready,
234    Success,
235    Failure,
236    NotSupported,
237}
238
239/// TODO(<https://crbug.com/1384419>): revisit the list of PrefetchStatus and
240/// filter out the ones that aren't necessary to the developers.
241
242#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
243pub enum PrefetchStatus {
244    #[default]
245    PrefetchAllowed,
246    PrefetchFailedIneligibleRedirect,
247    PrefetchFailedInvalidRedirect,
248    PrefetchFailedMIMENotSupported,
249    PrefetchFailedNetError,
250    PrefetchFailedNon2XX,
251    PrefetchEvictedAfterBrowsingDataRemoved,
252    PrefetchEvictedAfterCandidateRemoved,
253    PrefetchEvictedForNewerPrefetch,
254    PrefetchHeldback,
255    PrefetchIneligibleRetryAfter,
256    PrefetchIsPrivacyDecoy,
257    PrefetchIsStale,
258    PrefetchNotEligibleBrowserContextOffTheRecord,
259    PrefetchNotEligibleDataSaverEnabled,
260    PrefetchNotEligibleExistingProxy,
261    PrefetchNotEligibleHostIsNonUnique,
262    PrefetchNotEligibleNonDefaultStoragePartition,
263    PrefetchNotEligibleSameSiteCrossOriginPrefetchRequiredProxy,
264    PrefetchNotEligibleSchemeIsNotHttps,
265    PrefetchNotEligibleUserHasCookies,
266    PrefetchNotEligibleUserHasServiceWorker,
267    PrefetchNotEligibleUserHasServiceWorkerNoFetchHandler,
268    PrefetchNotEligibleRedirectFromServiceWorker,
269    PrefetchNotEligibleRedirectToServiceWorker,
270    PrefetchNotEligibleBatterySaverEnabled,
271    PrefetchNotEligiblePreloadingDisabled,
272    PrefetchNotFinishedInTime,
273    PrefetchNotStarted,
274    PrefetchNotUsedCookiesChanged,
275    PrefetchProxyNotAvailable,
276    PrefetchResponseUsed,
277    PrefetchSuccessfulButNotUsed,
278    PrefetchNotUsedProbeFailed,
279}
280
281/// Information of headers to be displayed when the header mismatch occurred.
282
283#[derive(Debug, Clone, Serialize, Deserialize, Default)]
284#[serde(rename_all = "camelCase")]
285pub struct PrerenderMismatchedHeaders {
286
287    pub headerName: String,
288
289    #[serde(skip_serializing_if = "Option::is_none")]
290    pub initialValue: Option<String>,
291
292    #[serde(skip_serializing_if = "Option::is_none")]
293    pub activationValue: Option<String>,
294}