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