retina_fetch/
destination.rs

1// Copyright (C) 2023 Tristan Gerritsen <tristan@thewoosh.org>
2// All Rights Reserved.
3
4use strum::AsRefStr;
5
6/// The [Request Destination][spec] specifies what the destination of this
7/// `fetch` is.
8///
9/// [spec]: https://fetch.spec.whatwg.org/#concept-request-destination
10#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
11#[derive(AsRefStr)]
12#[strum(serialize_all = "lowercase")]
13pub enum RequestDestination {
14    /// HTML `<audio>`
15    Audio,
16
17    /// `audioScript.addModule()`
18    AudioWorklet,
19
20    /// A website is loaded, and this is e.g. the HTML document.
21    Document,
22
23    /// HTML `<embed>`
24    Embed,
25
26    /// CSS `@font-face`
27    Font,
28
29    /// HTML `<frame>`
30    Frame,
31
32    /// HTML `<iframe>`
33    IFrame,
34
35    /// # `""` initiator
36    /// * `<img src>`
37    /// * `/favicon` resource
38    /// * SVG `<image>`
39    /// * CSS `background-image`
40    /// * CSS `cursor`
41    /// * CSS `list-style-image`
42    ///
43    /// # `imageset` initiator
44    /// * HTML `<img srcset>`
45    /// * HTML `<picture>`
46    Image,
47
48    /// `<link rel=manifest>`
49    Manifest,
50
51    /// The empty string `""`. This can come from one of:
52    ///
53    /// # `""` initiator
54    /// 1. `navigator.sendBeacon()`
55    /// 2. `EventSource`
56    /// 3. `<a ping="">`
57    /// 4. `<area ping="">`
58    /// 5. `fetch()`
59    /// 6. `XMLHttpRequest`
60    /// 7. `WebSocket`
61    /// 8. _Cache API_
62    ///
63    /// # `download` initiator
64    /// * HTML `download` attribute
65    /// * _Save Link As..._ in the browser UI
66    ///
67    /// # `prefetch` initiator
68    /// * HTML `<link rel=prefetch>`
69    ///
70    /// # `prerender` initiator
71    /// * HTML `<link rel=prerender>`
72    #[default]
73    None,
74
75    /// HTML `<object>`
76    Object,
77
78    /// `CSS.paintWorklet.addModule()`
79    PaintWorklet,
80
81    /// CSP, NEL reports.
82    Report,
83
84    /// * `<script>`
85    /// * `importScripts()`
86    Script,
87
88    /// `navigator.serviceWorker.register()`
89    ServiceWorker,
90
91    /// `SharedWorker`
92    SharedWorker,
93
94    /// * HTML `<link rel=stylesheet>`
95    /// * CSS `@import`
96    Style,
97
98    /// HTML `<track>`
99    Track,
100
101    /// HTML `<video>`
102    Video,
103
104    /// `Federated Credential Management requests`
105    WebIdentity,
106
107    /// `Worker`
108    Worker,
109
110    /// `<?xml-stylesheet>`
111    Xslt,
112}
113
114impl RequestDestination {
115    /// Get the normative string representation, as per [Fetch][spec].
116    ///
117    /// [spec]: https://fetch.spec.whatwg.org/#concept-request-destination
118    pub fn as_str(&self) -> &str {
119        self.as_ref()
120    }
121
122    /// Specifies if the request is initiated by a script load, or activated by
123    /// some features in JavaScript.
124    ///
125    /// # Specification Warning
126    /// ___Algorithms that use script-like should also consider "`xslt`" as that
127    /// too can cause script execution. It is not included in the list as it is
128    /// not always relevant and might require different behavior.___
129    ///
130    /// # Specifications
131    /// * [Fetch API][spec]
132    ///
133    /// [spec]: https://fetch.spec.whatwg.org/#request-destination-script-like
134    pub fn is_script_like(&self) -> bool {
135        matches!(
136            self,
137            Self::AudioWorklet
138                | Self::PaintWorklet
139                | Self::Script
140                | Self::ServiceWorker
141                | Self::SharedWorker
142                | Self::Worker
143        )
144    }
145}