Skip to main content

browser_protocol/preload/
mod.rs

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