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

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

    /// Schedule a [`TimerEventRequest`] on this [`GlobalScope`]'s [`timers::TimerScheduler`].
    /// Every Worker has its own scheduler, which handles events in the Worker event loop,
    /// but `Window`s use a shared scheduler associated with their [`ScriptThread`].
    pub(crate) fn schedule_timer(&self, request: TimerEventRequest) -> Option<TimerId> {
        match self.downcast::<WorkerGlobalScope>() {
            Some(worker_global) => Some(worker_global.timer_scheduler().schedule_timer(request)),
            _ => with_script_thread(|script_thread| Some(script_thread.schedule_timer(request))),
        }
    }

    /// Part of <https://fetch.spec.whatwg.org/#populate-request-from-client>
    pub(crate) fn request_client(&self) -> RequestClient {
        // Step 1.2.2. If global is a Window object and global’s navigable is not null,
        // then set request’s traversable for user prompts to global’s navigable’s traversable navigable.
        let window = self.downcast::<Window>();
        let preloaded_resources = window
            .map(|window: &Window| window.Document().preloaded_resources())
            .unwrap_or_default();
        let is_nested_browsing_context = window.is_some_and(|w| !w.is_top_level());
        RequestClient {
            preloaded_resources,
            policy_container: RequestPolicyContainer::PolicyContainer(self.policy_container()),
            origin: RequestOrigin::Origin(self.origin().immutable().clone()),
            is_nested_browsing_context,
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-policy-container>
    pub(crate) fn policy_container(&self) -> PolicyContainer {
        if let Some(window) = self.downcast::<Window>() {
            return window.Document().policy_container().to_owned();
        }
        if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
            return worker.policy_container().to_owned();
        }
        unreachable!();
    }

    /// Get the [base url](https://html.spec.whatwg.org/multipage/#api-base-url)
    /// for this global scope.
    pub(crate) fn api_base_url(&self) -> ServoUrl {
        if let Some(window) = self.downcast::<Window>() {
            // https://html.spec.whatwg.org/multipage/#script-settings-for-browsing-contexts:api-base-url
            return window.Document().base_url();

Review comment (suggestion):
```suggestion
        let is_nested_browsing_context = window.is_some_and(|window| !window.is_top_level());
```

Code after:
        }
        if let Some(worker) = self.downcast::<PaintWorkletGlobalScope>() {
            return worker.image_cache();
        }
        unreachable!();
    }

    /// Schedule a [`TimerEventRequest`] on this [`GlobalScope`]'s [`timers::TimerScheduler`].
    /// Every Worker has its own scheduler, which handles events in the Worker event loop,
    /// but `Window`s use a shared scheduler associated with their [`ScriptThread`].
    pub(crate) fn schedule_timer(&self, request: TimerEventRequest) -> Option<TimerId> {
        match self.downcast::<WorkerGlobalScope>() {
            Some(worker_global) => Some(worker_global.timer_scheduler().schedule_timer(request)),
            _ => with_script_thread(|script_thread| Some(script_thread.schedule_timer(request))),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#nested-browsing-context>
    pub(crate) fn is_nested_browsing_context(&self) -> bool {
        self.downcast::<Window>()
            .is_some_and(|window| !window.is_top_level())
    }

    /// Part of <https://fetch.spec.whatwg.org/#populate-request-from-client>
    pub(crate) fn request_client(&self) -> RequestClient {
        // Step 1.2.2. If global is a Window object and global’s navigable is not null,
        // then set request’s traversable for user prompts to global’s navigable’s traversable navigable.
        let window = self.downcast::<Window>();
        let preloaded_resources = window
            .map(|window: &Window| window.Document().preloaded_resources().clone())
            .unwrap_or_default();
        let is_nested_browsing_context = window.is_some_and(|window| !window.is_top_level());
        RequestClient {
            preloaded_resources,
            policy_container: RequestPolicyContainer::PolicyContainer(self.policy_container()),
            origin: RequestOrigin::Origin(self.origin().immutable().clone()),
            is_nested_browsing_context,
            insecure_requests_policy: self.insecure_requests_policy(),
        }
    }

    /// <https://html.spec.whatwg.org/multipage/#concept-settings-object-policy-container>
    pub(crate) fn policy_container(&self) -> PolicyContainer {
        if let Some(window) = self.downcast::<Window>() {
            return window.Document().policy_container().to_owned();
        }
        if let Some(worker) = self.downcast::<WorkerGlobalScope>() {
            return worker.policy_container().to_owned();
        }
        unreachable!();
    }