Skip to content

script: Check whether window is top level before looking for it in the ScriptThread#42390

Merged
TimvdLippe merged 3 commits intoservo:mainfrom
stevennovaryo:window-top-level
Feb 6, 2026
Merged

script: Check whether window is top level before looking for it in the ScriptThread#42390
TimvdLippe merged 3 commits intoservo:mainfrom
stevennovaryo:window-top-level

Conversation

@stevennovaryo
Copy link
Copy Markdown
Contributor

@stevennovaryo stevennovaryo commented Feb 6, 2026

Instead of immediately looking for the top-level browsing context's Document in the ScriptThread, check whether Window is top-level beforehand. This is a small optimization.

Testing: Existing WPTs (no behavior changes)

Signed-off-by: Jo Steven Novaryo <[email protected]>
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Feb 6, 2026
Signed-off-by: Jo Steven Novaryo <[email protected]>
/// 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).
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Suggested change
/// is discarded or the [`Document`] is in another thread (having dissimilar origin).
/// is discarded or the [`Document`] is in another `ScriptThread`

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks! I have updated the comments.

Comment on lines +656 to +657
let window_proxy = self.undiscarded_window_proxy()?;
if window_proxy.is_top_level() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just check Window::is_top_level here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed it! Thanks for pointing out.

Comment on lines +660 to +663
let top_level_window_proxy = self
.script_window_proxies
.find_window_proxy(window_proxy.webview_id().into())?;
top_level_window_proxy.document()
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for a temporary here:

Suggested change
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()

.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.");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.");

self.script_window_proxies
.find_window_proxy(window_proxy.webview_id().into())
})
pub(crate) fn top_level_document(&self) -> Option<DomRoot<Document>> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe clarify that this returns the top-level Document only when it is local:

Suggested change
pub(crate) fn top_level_document(&self) -> Option<DomRoot<Document>> {
pub(crate) fn top_level_document_if_local(&self) -> Option<DomRoot<Document>> {

@mrobinson mrobinson changed the title script: Check whether window is top level before looking for it in the script thread script: Check whether window is top level before looking for it in the ScriptThread Feb 6, 2026
.script_window_proxies
.find_window_proxy(window_proxy.webview_id().into())?
.document()
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

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

@servo-highfive servo-highfive removed the S-awaiting-review There is new code that needs to be reviewed. label Feb 6, 2026
@servo-highfive servo-highfive added the S-awaiting-review There is new code that needs to be reviewed. label Feb 6, 2026
Signed-off-by: Jo Steven Novaryo <[email protected]>
@TimvdLippe TimvdLippe added this pull request to the merge queue Feb 6, 2026
@servo-highfive servo-highfive added the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 6, 2026
Merged via the queue into servo:main with commit 936aaf1 Feb 6, 2026
29 checks passed
@servo-highfive servo-highfive removed the S-awaiting-merge The PR is in the process of compiling and running tests on the automated CI. label Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-awaiting-review There is new code that needs to be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants