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

Code before:
            }));
    }

    pub(crate) fn buffered_reports(&self) -> Vec<Report> {
        self.report_list.borrow().clone()
    }

    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 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

Review comment (suggestion):
A ScriptThread isn't necessarily in another thread (might be a process) and it might not have a dissimilar origin:

```suggestion
    /// is discarded or the [`Document`] is in another `ScriptThread`
```

Code after:
            }));
    }

    pub(crate) fn buffered_reports(&self) -> Vec<Report> {
        self.report_list.borrow().clone()
    }

    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
    }