Repository: servo/servo
File: components/script/dom/globalscope.rs (Rust)

Code before:
            return window.Performance();
        }
        if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
            return worker.Performance();
        }
        unreachable!();
    }

    /// <https://w3c.github.io/performance-timeline/#supportedentrytypes-attribute>
    pub(crate) fn supported_performance_entry_types(
        &self,
        cx: SafeJSContext,
        retval: MutableHandleValue,
        can_gc: CanGc,
    ) {
        self.frozen_supported_performance_entry_types.get_or_init(
            || {
                VALID_ENTRY_TYPES
                    .iter()
                    .map(|t| DOMString::from(t.to_string()))
                    .collect()
            },
            cx,
            retval,
            can_gc,
        );
    }

    pub(crate) fn get_https_state(&self) -> HttpsState {
        self.https_state.get()
    }

    pub(crate) fn set_https_state(&self, https_state: HttpsState) {
        self.https_state.set(https_state);
    }

    /// <https://html.spec.whatwg.org/multipage/#secure-context>
    pub(crate) fn is_secure_context(&self) -> bool {
        // This differs from the specification, but it seems that
        // `inherited_secure_context` implements more-or-less the exact same logic, in a
        // different manner. Workers inherit whether or not their in a secure context and
        // worklets do as well (they can only be created in secure contexts).
        if Some(false) == self.inherited_secure_context {
            return false;
        }
        if let Some(creation_url) = self.creation_url() {
            if creation_url.scheme() == "blob" && Some(true) == self.inherited_secure_context {
                return true;
            }
            return creation_url.is_potentially_trustworthy();
        }

Review comment (suggestion):
Step 1.2.1, which is for `WorkerGlobalScope`, isn't implemented here. Let's add a TODO.

> If global is a [WorkerGlobalScope](https://html.spec.whatwg.org/multipage/workers.html#workerglobalscope), then:
>

> 1. If global's [owner set](https://html.spec.whatwg.org/multipage/workers.html#concept-WorkerGlobalScope-owner-set)[0]'s [relevant settings object](https://html.spec.whatwg.org/multipage/webappapis.html#relevant-settings-object) is a [secure context](https://html.spec.whatwg.org/multipage/webappapis.html#secure-context), then return true.

> 2. Return false.

Code after:
    }

    pub(crate) fn get_https_state(&self) -> HttpsState {
        self.https_state.get()
    }

    pub(crate) fn set_https_state(&self, https_state: HttpsState) {
        self.https_state.set(https_state);
    }

    /// <https://html.spec.whatwg.org/multipage/#secure-context>
    pub(crate) fn is_secure_context(&self) -> bool {
        // This differs from the specification, but it seems that
        // `inherited_secure_context` implements more-or-less the exact same logic, in a
        // different manner. Workers inherit whether or not their in a secure context and
        // worklets do as well (they can only be created in secure contexts).
        if Some(false) == self.inherited_secure_context {
            return false;
        }
        // Step 1. If environment is an environment settings object, then:
        // Step 1.1. Let global be environment's global object.
        match self.top_level_creation_url() {
            None => {
                // Workers and worklets don't have a top-level creation URL
                assert!(
                    self.downcast::<WorkerGlobalScope>().is_some() ||
                        self.downcast::<WorkletGlobalScope>().is_some()
                );
                true
            },
            Some(top_level_creation_url) => {
                assert!(self.downcast::<Window>().is_some());
                // Step 2. If the result of Is url potentially trustworthy?
                // given environment's top-level creation URL is "Potentially Trustworthy", then return true.
                // Step 3. Return false.
                if top_level_creation_url.scheme() == "blob" &&
                    Some(true) == self.inherited_secure_context
                {
                    return true;
                }
                top_level_creation_url.is_potentially_trustworthy()
            },
        }
    }

    /// <https://www.w3.org/TR/CSP/#get-csp-of-object>
    pub(crate) fn get_csp_list(&self) -> Option<CspList> {
        if self.downcast::<Window>().is_some() || self.downcast::<WorkerGlobalScope>().is_some() {
            return self.policy_container().csp_list;
        }
        // TODO: Worklet global scopes.