script: Check whether window is top level before looking for it in the ScriptThread#42390
Conversation
Signed-off-by: Jo Steven Novaryo <[email protected]>
Signed-off-by: Jo Steven Novaryo <[email protected]>
components/script/dom/window.rs
Outdated
| /// Returns the window proxy of the webview, which is the top-level ancestor browsing context. | ||
| /// 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). |
There was a problem hiding this comment.
A ScriptThread isn't necessarily in another thread (might be a process) and it might not have a dissimilar origin:
| /// is discarded or the [`Document`] is in another thread (having dissimilar origin). | |
| /// is discarded or the [`Document`] is in another `ScriptThread` |
There was a problem hiding this comment.
Ah, thanks! I have updated the comments.
components/script/dom/window.rs
Outdated
| let window_proxy = self.undiscarded_window_proxy()?; | ||
| if window_proxy.is_top_level() { |
There was a problem hiding this comment.
Why not just check Window::is_top_level here?
There was a problem hiding this comment.
Missed it! Thanks for pointing out.
components/script/dom/window.rs
Outdated
| let top_level_window_proxy = self | ||
| .script_window_proxies | ||
| .find_window_proxy(window_proxy.webview_id().into())?; | ||
| top_level_window_proxy.document() |
There was a problem hiding this comment.
No need for a temporary here:
| let top_level_window_proxy = self | |
| .script_window_proxies | |
| .find_window_proxy(window_proxy.webview_id().into())?; | |
| top_level_window_proxy.document() | |
| self | |
| .script_window_proxies | |
| .find_window_proxy(window_proxy.webview_id().into())? | |
| .document() |
components/script/dom/window.rs
Outdated
| .expect("Should always have a WindowProxy when calling WebdriverWindow"); | ||
| // Window must be top level browsing context. | ||
| assert!(window_proxy.browsing_context_id() == window_proxy.webview_id()); | ||
| assert!(window_proxy.is_top_level(), "Window must be top level browsing context."); |
There was a problem hiding this comment.
| assert!(window_proxy.is_top_level(), "Window must be top level browsing context."); | |
| assert!(self.is_top_level(), "Window must be top level browsing context."); |
components/script/dom/window.rs
Outdated
| self.script_window_proxies | ||
| .find_window_proxy(window_proxy.webview_id().into()) | ||
| }) | ||
| pub(crate) fn top_level_document(&self) -> Option<DomRoot<Document>> { |
There was a problem hiding this comment.
Maybe clarify that this returns the top-level Document only when it is local:
| pub(crate) fn top_level_document(&self) -> Option<DomRoot<Document>> { | |
| pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> { |
ScriptThread
| .script_window_proxies | ||
| .find_window_proxy(window_proxy.webview_id().into())? | ||
| .document() | ||
| } |
There was a problem hiding this comment.
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:
| } | |
| 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() |
076f393 to
ebc16d6
Compare
ebc16d6 to
20d2111
Compare
Signed-off-by: Jo Steven Novaryo <[email protected]>
20d2111 to
f53651b
Compare
Instead of immediately looking for the top-level browsing context's
Documentin theScriptThread, check whetherWindowis top-level beforehand. This is a small optimization.Testing: Existing WPTs (no behavior changes)