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

Code before:
    }

    pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
        *self.endpoints_list.borrow_mut() = endpoints;
    }

    /// 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>> {
        let window_proxy = self.undiscarded_window_proxy()?;
        if self.is_top_level() {
            Some(self.Document())
        } else {
            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()

Review comment (suggestion):
Small optimization here. Wait until knowing that the `Window` isn't top level to call `Window::undiscarded_window_proxy()`. Feel free to land with this change:

```suggestion
        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()
```

Code after:
    }

    pub(crate) fn set_endpoints_list(&self, endpoints: Vec<ReportingEndpoint>) {
        *self.endpoints_list.borrow_mut() = endpoints;
    }

    /// 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()))