Skip to main content

script/scripting/
fetching_options.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use net_traits::ReferrerPolicy;
6use net_traits::request::{CredentialsMode, ParserMetadata};
7use script_bindings::str::DOMString;
8use servo_url::ServoUrl;
9
10use crate::dom::globalscope::GlobalScope;
11
12#[derive(Clone, Debug, Default, MallocSizeOf)]
13pub(crate) enum ScriptFetchPriority {
14    High,
15    Low,
16    #[default]
17    Auto,
18}
19
20impl From<DOMString> for ScriptFetchPriority {
21    fn from(value: DOMString) -> Self {
22        match_domstring_ascii!(value,
23            "high" => ScriptFetchPriority::High,
24            "low" => ScriptFetchPriority::Low,
25            "auto" => ScriptFetchPriority::Auto,
26            _ => unreachable!(),
27        )
28    }
29}
30
31impl From<&str> for ScriptFetchPriority {
32    fn from(value: &str) -> Self {
33        match value {
34            "high" => ScriptFetchPriority::High,
35            "low" => ScriptFetchPriority::Low,
36            "auto" => ScriptFetchPriority::Auto,
37            _ => Default::default(),
38        }
39    }
40}
41
42#[derive(Clone, Debug, MallocSizeOf)]
43/// <https://html.spec.whatwg.org/multipage/#script-fetch-options>
44pub(crate) struct ScriptFetchOptions {
45    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-integrity>
46    pub(crate) integrity_metadata: String,
47    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-credentials>
48    pub(crate) credentials_mode: CredentialsMode,
49    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-nonce>
50    pub(crate) cryptographic_nonce: String,
51    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-parser>
52    pub(crate) parser_metadata: ParserMetadata,
53    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-referrer-policy>
54    pub(crate) referrer_policy: ReferrerPolicy,
55    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-render-blocking>
56    pub(crate) render_blocking: bool,
57    /// <https://html.spec.whatwg.org/multipage/#concept-script-fetch-options-fetch-priority>
58    pub(crate) fetch_priority: ScriptFetchPriority,
59}
60
61impl ScriptFetchOptions {
62    /// <https://html.spec.whatwg.org/multipage/#default-script-fetch-options>
63    pub(crate) fn default_classic_script() -> ScriptFetchOptions {
64        // > The default script fetch options are a script fetch options whose
65        // > cryptographic nonce is the empty string,
66        // > integrity metadata is the empty string,
67        // > parser metadata is "not-parser-inserted",
68        // > credentials mode is "same-origin",
69        // > referrer policy is the empty string,
70        // > and fetch priority is "auto".
71        Self {
72            cryptographic_nonce: String::new(),
73            integrity_metadata: String::new(),
74            parser_metadata: ParserMetadata::NotParserInserted,
75            credentials_mode: CredentialsMode::CredentialsSameOrigin,
76            referrer_policy: ReferrerPolicy::EmptyString,
77            render_blocking: false,
78            fetch_priority: Default::default(),
79        }
80    }
81
82    /// <https://html.spec.whatwg.org/multipage/#get-the-descendant-script-fetch-options>
83    pub(crate) fn descendant_fetch_options(
84        &self,
85        url: &ServoUrl,
86        global: &GlobalScope,
87    ) -> ScriptFetchOptions {
88        // Step 2. Let integrity be the result of resolving a module integrity metadata with url and settingsObject.
89        let integrity = global.import_map().resolve_a_module_integrity_metadata(url);
90
91        // Step 1. Let newOptions be a copy of originalOptions.
92        // Step 5. Return newOptions.
93        Self {
94            // Step 3. Set newOptions's integrity metadata to integrity.
95            integrity_metadata: integrity,
96            cryptographic_nonce: self.cryptographic_nonce.clone(),
97            credentials_mode: self.credentials_mode,
98            parser_metadata: self.parser_metadata,
99            referrer_policy: self.referrer_policy,
100            render_blocking: self.render_blocking,
101            // Step 4. Set newOptions's fetch priority to "auto".
102            fetch_priority: Default::default(),
103        }
104    }
105}
106
107// A private value used by classic and javascript module scripts, to fetch dependencies, resolve
108// dynamic imports and module specifiers.
109pub(crate) struct BaseScript {
110    pub(crate) base_url: ServoUrl,
111    pub(crate) options: ScriptFetchOptions,
112}
113
114impl BaseScript {
115    pub(crate) fn new(base_url: ServoUrl, options: ScriptFetchOptions) -> Self {
116        BaseScript { base_url, options }
117    }
118}