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::refcounted::Trusted;
8use servo_url::ServoUrl;
9
10use crate::dom::globalscope::GlobalScope;
11
12#[derive(Clone, Debug, MallocSizeOf)]
13/// <https://html.spec.whatwg.org/multipage/#script-fetch-options>
14pub(crate) struct ScriptFetchOptions {
15    pub(crate) integrity_metadata: String,
16    pub(crate) credentials_mode: CredentialsMode,
17    pub(crate) cryptographic_nonce: String,
18    pub(crate) parser_metadata: ParserMetadata,
19    pub(crate) referrer_policy: ReferrerPolicy,
20    pub(crate) render_blocking: bool,
21}
22
23impl ScriptFetchOptions {
24    /// <https://html.spec.whatwg.org/multipage/#default-classic-script-fetch-options>
25    pub(crate) fn default_classic_script() -> ScriptFetchOptions {
26        Self {
27            cryptographic_nonce: String::new(),
28            integrity_metadata: String::new(),
29            parser_metadata: ParserMetadata::NotParserInserted,
30            credentials_mode: CredentialsMode::CredentialsSameOrigin,
31            referrer_policy: ReferrerPolicy::EmptyString,
32            render_blocking: false,
33        }
34    }
35
36    /// <https://html.spec.whatwg.org/multipage/#descendant-script-fetch-options>
37    pub(crate) fn descendant_fetch_options(
38        &self,
39        url: &ServoUrl,
40        global: &GlobalScope,
41    ) -> ScriptFetchOptions {
42        // Step 2. Let integrity be the result of resolving a module integrity metadata with url and settingsObject.
43        let integrity = global.import_map().resolve_a_module_integrity_metadata(url);
44
45        // Step 1. Let newOptions be a copy of originalOptions.
46        // TODO Step 4. Set newOptions's fetch priority to "auto".
47        Self {
48            // Step 3. Set newOptions's integrity metadata to integrity.
49            integrity_metadata: integrity,
50            cryptographic_nonce: self.cryptographic_nonce.clone(),
51            credentials_mode: self.credentials_mode,
52            parser_metadata: self.parser_metadata,
53            referrer_policy: self.referrer_policy,
54            render_blocking: self.render_blocking,
55        }
56    }
57}
58
59// A private value used by classic and javascript module scripts, to fetch dependencies, resolve
60// dynamic imports and module specifiers.
61pub(crate) struct BaseScript {
62    pub(crate) base_url: ServoUrl,
63    pub(crate) options: ScriptFetchOptions,
64    pub(crate) owner: Option<Trusted<GlobalScope>>,
65}
66
67impl BaseScript {
68    pub(crate) fn new(
69        base_url: ServoUrl,
70        options: ScriptFetchOptions,
71        owner: Option<Trusted<GlobalScope>>,
72    ) -> Self {
73        BaseScript {
74            base_url,
75            options,
76            owner,
77        }
78    }
79}