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

Code before:

    /// Returns the window proxy if it has not been discarded.
    /// <https://html.spec.whatwg.org/multipage/#a-browsing-context-is-discarded>
    pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
        self.window_proxy.get().and_then(|window_proxy| {
            if window_proxy.is_browsing_context_discarded() {
                None
            } else {
                Some(window_proxy)
            }
        })
    }

    /// Get the active [`Document`] of top-level browsing context, or return [`Window`]'s [`Document`]
    /// if it's browing context is the top-level browsing context. Returning none if the [`WindowProxy`]
    /// is discarded or the [`Document`] is in another thread (having dissimilar origin).
    /// <https://html.spec.whatwg.org/multipage/#top-level-browsing-context>
    pub(crate) fn top_level_document(&self) -> Option<DomRoot<Document>> {
        let window_proxy = self.undiscarded_window_proxy()?;
        if window_proxy.is_top_level() {
            Some(self.Document())
        } else {
            let top_level_window_proxy = self
                .script_window_proxies
                .find_window_proxy(window_proxy.webview_id().into())?;
            top_level_window_proxy.document()
        }
    }

    #[cfg(feature = "bluetooth")]
    pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
        self.bluetooth_thread.clone()
    }

    #[cfg(feature = "bluetooth")]
    pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
        &self.bluetooth_extra_permission_data
    }

    pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
        &self.error_reporter
    }

    pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
        self.webgl_chan
            .as_ref()
            .map(|chan| WebGLCommandSender::new(chan.clone()))
    }

    #[cfg(feature = "webxr")]
    pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {

Review comment (suggestion):
No need for a temporary here:

```suggestion
            self
                .script_window_proxies
                .find_window_proxy(window_proxy.webview_id().into())?
                .document()
```

Code after:

    /// Returns the window proxy if it has not been discarded.
    /// <https://html.spec.whatwg.org/multipage/#a-browsing-context-is-discarded>
    pub(crate) fn undiscarded_window_proxy(&self) -> Option<DomRoot<WindowProxy>> {
        self.window_proxy.get().and_then(|window_proxy| {
            if window_proxy.is_browsing_context_discarded() {
                None
            } else {
                Some(window_proxy)
            }
        })
    }

    /// Get the active [`Document`] of top-level browsing context, or return [`Window`]'s [`Document`]
    /// if it's browing context is the top-level browsing context. Returning none if the [`WindowProxy`]
    /// is discarded or the [`Document`] is in another `ScriptThread`.
    /// <https://html.spec.whatwg.org/multipage/#top-level-browsing-context>
    pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {
        if self.is_top_level() {
            return Some(self.Document());
        }

        let window_proxy = self.undiscarded_window_proxy()?;
        self.script_window_proxies
            .find_window_proxy(window_proxy.webview_id().into())?
            .document()
    }

    #[cfg(feature = "bluetooth")]
    pub(crate) fn bluetooth_thread(&self) -> GenericSender<BluetoothRequest> {
        self.bluetooth_thread.clone()
    }

    #[cfg(feature = "bluetooth")]
    pub(crate) fn bluetooth_extra_permission_data(&self) -> &BluetoothExtraPermissionData {
        &self.bluetooth_extra_permission_data
    }

    pub(crate) fn css_error_reporter(&self) -> &CSSErrorReporter {
        &self.error_reporter
    }

    pub(crate) fn webgl_chan(&self) -> Option<WebGLCommandSender> {
        self.webgl_chan
            .as_ref()
            .map(|chan| WebGLCommandSender::new(chan.clone()))
    }

    #[cfg(feature = "webxr")]
    pub(crate) fn webxr_registry(&self) -> Option<webxr_api::Registry> {
        self.webxr_registry.clone()