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

Code before:

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-registered-reporting-observer-list>
    reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-reports>
    report_list: DomRefCell<Vec<Report>>,

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-endpoints>
    #[no_trace]
    endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,

    /// The window proxies the script thread knows.
    #[conditional_malloc_size_of]
    script_window_proxies: Rc<ScriptWindowProxies>,

    /// Whether or not this [`Window`] has a pending screenshot readiness request.
    has_pending_screenshot_readiness_request: Cell<bool>,

    /// Visual viewport interface that is associated to this [`Window`].
    /// <https://drafts.csswg.org/cssom-view/#dom-window-visualviewport>
    visual_viewport: MutNullableDom<VisualViewport>,

    /// A weak handle to script thread
    #[ignore_malloc_size_of = "Weak does not need to be accounted"]
    #[no_trace]
    weak_script_thread: Weak<ScriptThread>,
}

impl Window {
    pub(crate) fn webview_id(&self) -> WebViewId {
        self.webview_id
    }

    pub(crate) fn as_global_scope(&self) -> &GlobalScope {
        self.upcast::<GlobalScope>()
    }

    pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
        self.layout.borrow()
    }

    pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
        self.layout.borrow_mut()
    }

    pub(crate) fn get_exists_mut_observer(&self) -> bool {
        self.exists_mut_observer.get()
    }

    pub(crate) fn set_exists_mut_observer(&self) {
        self.exists_mut_observer.set(true);

Review comment (refactor):
Let's put this at the top of the struct after `globalscope` as it is quite central to the operation and also maybe simplify the name a bit:

```
    /// A reference to the currently operating `ScriptThread`. This should always be
    /// upgradeable to an `Rc` as long as the `ScriptThread` is running.
    #[no_trace]
    #[ignore_malloc_size_of = "Measured by the ScriptThread itself"]
    script_thread: Weak<ScriptThread>,
```


Code after:
    /// A shared marker for the validity of any cached layout values. A value of true
    /// indicates that any such values remain valid; any new layout that invalidates
    /// those values will cause the marker to be set to false.
    #[conditional_malloc_size_of]
    layout_marker: DomRefCell<Rc<Cell<bool>>>,

    /// <https://dom.spec.whatwg.org/#window-current-event>
    current_event: DomRefCell<Option<Dom<Event>>>,

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-registered-reporting-observer-list>
    reporting_observer_list: DomRefCell<Vec<DomRoot<ReportingObserver>>>,

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-reports>
    report_list: DomRefCell<Vec<Report>>,

    /// <https://w3c.github.io/reporting/#windoworworkerglobalscope-endpoints>
    #[no_trace]
    endpoints_list: DomRefCell<Vec<ReportingEndpoint>>,

    /// The window proxies the script thread knows.
    #[conditional_malloc_size_of]
    script_window_proxies: Rc<ScriptWindowProxies>,

    /// Whether or not this [`Window`] has a pending screenshot readiness request.
    has_pending_screenshot_readiness_request: Cell<bool>,

    /// Visual viewport interface that is associated to this [`Window`].
    /// <https://drafts.csswg.org/cssom-view/#dom-window-visualviewport>
    visual_viewport: MutNullableDom<VisualViewport>,
}

impl Window {
    pub(crate) fn script_thread(&self) -> Rc<ScriptThread> {
        Weak::upgrade(&self.weak_script_thread)
            .expect("Weak reference should always be upgradable when a ScriptThread is running")
    }

    pub(crate) fn webview_id(&self) -> WebViewId {
        self.webview_id
    }

    pub(crate) fn as_global_scope(&self) -> &GlobalScope {
        self.upcast::<GlobalScope>()
    }

    pub(crate) fn layout(&self) -> Ref<'_, Box<dyn Layout>> {
        self.layout.borrow()
    }

    pub(crate) fn layout_mut(&self) -> RefMut<'_, Box<dyn Layout>> {
        self.layout.borrow_mut()