servoshell: Do not focus and raise new auxiliary WebDriver-created WebViews (#37284)

For Desktop port of `request_open_auxiliary_webview`, stay on the
original WebView if the request originates WebDriver.

This is to make sure `webdriver_server::handle_new_window` does not
focus the new window, according to spec. See
c7eba2dbba/tests/wpt/tests/webdriver/tests/classic/new_window/new_window.py (L31-L37)

**To clarify**: this won't change the behaviour when user interacts, but
only affects WebDriver [New
Window](https://w3c.github.io/webdriver/#new-window).

Testing: `./mach test-wpt -r --log-raw "D:/servo log/all.txt"
./tests/wpt/tests/webdriver/tests/classic --product servodriver` based
on 96b0973037

---------

Signed-off-by: Euclid Ye <yezhizhenjiakang@gmail.com>
This commit is contained in:
Euclid Ye 2025-06-09 19:07:09 +08:00 committed by GitHub
parent 0fa3de3937
commit a3c792e5aa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
27 changed files with 76 additions and 158 deletions

View file

@ -4702,25 +4702,38 @@ where
WebDriverCommandMsg::CloseWebView(webview_id) => { WebDriverCommandMsg::CloseWebView(webview_id) => {
self.handle_close_top_level_browsing_context(webview_id); self.handle_close_top_level_browsing_context(webview_id);
}, },
WebDriverCommandMsg::NewWebView(webview_id, sender, load_sender) => { WebDriverCommandMsg::NewWebView(
let (chan, port) = match ipc::channel() { originating_webview_id,
response_sender,
load_status_sender,
) => {
let (embedder_sender, receiver) = match ipc::channel() {
Ok(result) => result, Ok(result) => result,
Err(error) => return warn!("Failed to create channel: {error:?}"), Err(error) => return warn!("Failed to create channel: {error:?}"),
}; };
self.embedder_proxy self.embedder_proxy.send(EmbedderMsg::AllowOpeningWebView(
.send(EmbedderMsg::AllowOpeningWebView(webview_id, chan)); originating_webview_id,
let (webview_id, viewport_details) = match port.recv() { embedder_sender,
Ok(Some((webview_id, viewport_details))) => (webview_id, viewport_details), ));
let (new_webview_id, viewport_details) = match receiver.recv() {
Ok(Some((new_webview_id, viewport_details))) => {
(new_webview_id, viewport_details)
},
Ok(None) => return warn!("Embedder refused to allow opening webview"), Ok(None) => return warn!("Embedder refused to allow opening webview"),
Err(error) => return warn!("Failed to receive webview id: {error:?}"), Err(error) => return warn!("Failed to receive webview id: {error:?}"),
}; };
self.handle_new_top_level_browsing_context( self.handle_new_top_level_browsing_context(
ServoUrl::parse_with_base(None, "about:blank").expect("Infallible parse"), ServoUrl::parse_with_base(None, "about:blank").expect("Infallible parse"),
webview_id, new_webview_id,
viewport_details, viewport_details,
Some(load_sender), Some(load_status_sender),
); );
let _ = sender.send(webview_id); if let Err(error) = response_sender.send(new_webview_id) {
error!(
"WebDriverCommandMsg::NewWebView: IPC error when sending new_webview_id \
to webdriver server: {error}"
);
}
}, },
WebDriverCommandMsg::FocusWebView(webview_id) => { WebDriverCommandMsg::FocusWebView(webview_id) => {
self.handle_focus_web_view(webview_id); self.handle_focus_web_view(webview_id);

View file

@ -466,26 +466,40 @@ impl WindowProxy {
features: DOMString, features: DOMString,
can_gc: CanGc, can_gc: CanGc,
) -> Fallible<Option<DomRoot<WindowProxy>>> { ) -> Fallible<Option<DomRoot<WindowProxy>>> {
// Step 4. // Step 5. If target is the empty string, then set target to "_blank".
let non_empty_target = match target.as_ref() { let non_empty_target = match target.as_ref() {
"" => DOMString::from("_blank"), "" => DOMString::from("_blank"),
_ => target, _ => target,
}; };
// Step 5 // Step 6. Let tokenizedFeatures be the result of tokenizing features.
let tokenized_features = tokenize_open_features(features); let tokenized_features = tokenize_open_features(features);
// Step 7-9 // Step 7 - 8.
// If tokenizedFeatures["noreferrer"] exists, then set noreferrer to
// the result of parsing tokenizedFeatures["noreferrer"] as a boolean feature.
let noreferrer = parse_open_feature_boolean(&tokenized_features, "noreferrer"); let noreferrer = parse_open_feature_boolean(&tokenized_features, "noreferrer");
// Step 9. Let noopener be the result of getting noopener for window
// open with sourceDocument, tokenizedFeatures, and urlRecord.
let noopener = if noreferrer { let noopener = if noreferrer {
true true
} else { } else {
parse_open_feature_boolean(&tokenized_features, "noopener") parse_open_feature_boolean(&tokenized_features, "noopener")
}; };
// Step 10, 11 // (TODO) Step 10. Remove tokenizedFeatures["noopener"] and tokenizedFeatures["noreferrer"].
// (TODO) Step 11. Let referrerPolicy be the empty string.
// (TODO) Step 12. If noreferrer is true, then set referrerPolicy to "no-referrer".
// Step 13 - 14
// Let targetNavigable and windowType be the result of applying the rules for
// choosing a navigable given target, sourceDocument's node navigable, and noopener.
// If targetNavigable is null, then return null.
let (chosen, new) = match self.choose_browsing_context(non_empty_target, noopener) { let (chosen, new) = match self.choose_browsing_context(non_empty_target, noopener) {
(Some(chosen), new) => (chosen, new), (Some(chosen), new) => (chosen, new),
(None, _) => return Ok(None), (None, _) => return Ok(None),
}; };
// TODO Step 12, set up browsing context features. // TODO Step 15.2, Set up browsing context features for targetNavigable's
// active browsing context given tokenizedFeatures.
let target_document = match chosen.document() { let target_document = match chosen.document() {
Some(target_document) => target_document, Some(target_document) => target_document,
None => return Ok(None), None => return Ok(None),
@ -496,7 +510,7 @@ impl WindowProxy {
false false
}; };
let target_window = target_document.window(); let target_window = target_document.window();
// Step 13, and 14.4, will have happened elsewhere, // Step 15.3 and 15.4 will have happened elsewhere,
// since we've created a new browsing context and loaded it with about:blank. // since we've created a new browsing context and loaded it with about:blank.
if !url.is_empty() { if !url.is_empty() {
let existing_document = self let existing_document = self
@ -504,18 +518,18 @@ impl WindowProxy {
.get() .get()
.and_then(ScriptThread::find_document) .and_then(ScriptThread::find_document)
.unwrap(); .unwrap();
// Step 14.1
let url = match existing_document.url().join(&url) { let url = match existing_document.url().join(&url) {
Ok(url) => url, Ok(url) => url,
Err(_) => return Err(Error::Syntax), Err(_) => return Err(Error::Syntax),
}; };
// Step 14.3
let referrer = if noreferrer { let referrer = if noreferrer {
Referrer::NoReferrer Referrer::NoReferrer
} else { } else {
target_window.as_global_scope().get_referrer() target_window.as_global_scope().get_referrer()
}; };
// Step 14.5 // Step 15.5 Otherwise, navigate targetNavigable to urlRecord using sourceDocument,
// with referrerPolicy set to referrerPolicy and exceptionsEnabled set to true.
// FIXME: referrerPolicy may not be used properly here. exceptionsEnabled not used.
let referrer_policy = target_document.get_referrer_policy(); let referrer_policy = target_document.get_referrer_policy();
let pipeline_id = target_window.pipeline_id(); let pipeline_id = target_window.pipeline_id();
let secure = target_window.as_global_scope().is_secure_context(); let secure = target_window.as_global_scope().is_secure_context();
@ -534,14 +548,13 @@ impl WindowProxy {
} else { } else {
NavigationHistoryBehavior::Push NavigationHistoryBehavior::Push
}; };
target_window.load_url(history_handling, false, load_data, can_gc); target_window.load_url(history_handling, false, load_data, can_gc);
} }
// Step 17 (Dis-owning has been done in create_auxiliary_browsing_context).
if noopener { if noopener {
// Step 15 (Dis-owning has been done in create_auxiliary_browsing_context).
return Ok(None); return Ok(None);
} }
// Step 17. // Step 18
Ok(target_document.browsing_context()) Ok(target_document.browsing_context())
} }

View file

@ -79,6 +79,8 @@ fn find_node_by_unique_id(
match documents.find_document(pipeline) { match documents.find_document(pipeline) {
Some(doc) => find_node_by_unique_id_in_document(&doc, node_id), Some(doc) => find_node_by_unique_id_in_document(&doc, node_id),
None => { None => {
// FIXME: This is unreacheable!! Because we already early return in Constellation
// To be Fixed soon
if ScriptThread::has_node_id(pipeline, &node_id) { if ScriptThread::has_node_id(pipeline, &node_id) {
Err(ErrorStatus::StaleElementReference) Err(ErrorStatus::StaleElementReference)
} else { } else {

View file

@ -949,6 +949,7 @@ impl Handler {
))) )))
} }
/// <https://w3c.github.io/webdriver/#new-window>
fn handle_new_window( fn handle_new_window(
&mut self, &mut self,
_parameters: &NewWindowParameters, _parameters: &NewWindowParameters,
@ -956,11 +957,16 @@ impl Handler {
let (sender, receiver) = ipc::channel().unwrap(); let (sender, receiver) = ipc::channel().unwrap();
let session = self.session().unwrap(); let session = self.session().unwrap();
// Step 2. (TODO) If session's current top-level browsing context is no longer open,
// return error with error code no such window.
let cmd_msg = WebDriverCommandMsg::NewWebView( let cmd_msg = WebDriverCommandMsg::NewWebView(
session.webview_id, session.webview_id,
sender, sender,
self.load_status_sender.clone(), self.load_status_sender.clone(),
); );
// Step 5. Create a new top-level browsing context by running the window open steps.
// This MUST be done without invoking the focusing steps.
self.constellation_chan self.constellation_chan
.send(EmbedderToConstellationMessage::WebDriverCommand(cmd_msg)) .send(EmbedderToConstellationMessage::WebDriverCommand(cmd_msg))
.unwrap(); .unwrap();
@ -968,8 +974,6 @@ impl Handler {
let mut handle = self.session.as_ref().unwrap().id.to_string(); let mut handle = self.session.as_ref().unwrap().id.to_string();
if let Ok(new_webview_id) = receiver.recv() { if let Ok(new_webview_id) = receiver.recv() {
let session = self.session_mut().unwrap(); let session = self.session_mut().unwrap();
session.webview_id = new_webview_id;
session.browsing_context_id = BrowsingContextId::from(new_webview_id);
let new_handle = Uuid::new_v4().to_string(); let new_handle = Uuid::new_v4().to_string();
handle = new_handle.clone(); handle = new_handle.clone();
session.window_handles.insert(new_webview_id, new_handle); session.window_handles.insert(new_webview_id, new_handle);

View file

@ -12,7 +12,7 @@ use image::{DynamicImage, ImageFormat};
use keyboard_types::{Key, KeyboardEvent, Modifiers, ShortcutMatcher}; use keyboard_types::{Key, KeyboardEvent, Modifiers, ShortcutMatcher};
use log::{error, info}; use log::{error, info};
use servo::base::id::WebViewId; use servo::base::id::WebViewId;
use servo::config::pref; use servo::config::{opts, pref};
use servo::ipc_channel::ipc::IpcSender; use servo::ipc_channel::ipc::IpcSender;
use servo::webrender_api::ScrollLocation; use servo::webrender_api::ScrollLocation;
use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize}; use servo::webrender_api::units::{DeviceIntPoint, DeviceIntRect, DeviceIntSize};
@ -477,9 +477,13 @@ impl WebViewDelegate for RunningAppState {
.build(); .build();
webview.notify_theme_change(self.inner().window.theme()); webview.notify_theme_change(self.inner().window.theme());
webview.focus(); // When WebDriver is enabled, do not focus and raise the WebView to the top,
webview.raise_to_top(true); // as that is what the specification expects. Otherwise, we would like `window.open()`
// to create a new foreground tab
if opts::get().webdriver_port.is_some() {
webview.focus();
webview.raise_to_top(true);
}
self.add(webview.clone()); self.add(webview.clone());
Some(webview) Some(webview)
} }

View file

@ -4,6 +4,3 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL

View file

@ -1,9 +0,0 @@
[shadow_dom.py]
[test_shadow_element_click[host_element\]]
expected: FAIL
[test_nested_shadow_element_click[outer_element\]]
expected: FAIL
[test_nested_shadow_element_click[inner_element\]]
expected: FAIL

View file

@ -5,9 +5,6 @@
[test_iframe_is_interactable] [test_iframe_is_interactable]
expected: FAIL expected: FAIL
[test_readonly_element]
expected: FAIL
[test_not_a_focusable_element] [test_not_a_focusable_element]
expected: FAIL expected: FAIL
@ -22,3 +19,9 @@
[test_disabled] [test_disabled]
expected: FAIL expected: FAIL
[test_transparent_element]
expected: FAIL
[test_readonly_element]
expected: FAIL

View file

@ -5,5 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]] [test_surrogates]
expected: FAIL expected: FAIL

View file

@ -1,7 +1,6 @@
[find.py] [find.py]
expected: TIMEOUT
[test_no_browsing_context] [test_no_browsing_context]
expected: ERROR expected: FAIL
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
@ -15,33 +14,12 @@
[test_no_such_element_with_unknown_selector[existent-inside-shadow-root\]] [test_no_such_element_with_unknown_selector[existent-inside-shadow-root\]]
expected: FAIL expected: FAIL
[test_no_such_element_with_startnode_from_other_window_handle]
expected: FAIL
[test_no_such_element_with_startnode_from_other_frame] [test_no_such_element_with_startnode_from_other_frame]
expected: FAIL expected: FAIL
[test_stale_element_reference[top_context\]]
expected: FAIL
[test_stale_element_reference[child_context\]]
expected: FAIL
[test_find_element[xpath-//a\]] [test_find_element[xpath-//a\]]
expected: FAIL expected: FAIL
[test_xhtml_namespace[css selector-#linkText\]]
expected: FAIL
[test_xhtml_namespace[link text-full link text\]]
expected: FAIL
[test_xhtml_namespace[partial link text-link text\]]
expected: FAIL
[test_xhtml_namespace[tag name-a\]]
expected: FAIL
[test_xhtml_namespace[xpath-//*[name()='a'\]\]] [test_xhtml_namespace[xpath-//*[name()='a'\]\]]
expected: FAIL expected: FAIL

View file

@ -5,8 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_computed_roles[<article>foo</article>-article-article\]] [test_computed_roles[<article>foo</article>-article-article\]]
expected: FAIL expected: FAIL

View file

@ -5,9 +5,6 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_boolean_attribute[audio-attrs0\]] [test_boolean_attribute[audio-attrs0\]]
expected: FAIL expected: FAIL

View file

@ -4,6 +4,3 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL

View file

@ -5,8 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_web_reference[shadowRoot-ShadowRoot\]] [test_web_reference[shadowRoot-ShadowRoot\]]
expected: FAIL expected: FAIL

View file

@ -5,8 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_basic] [test_basic]
expected: FAIL expected: FAIL

View file

@ -1,33 +1,3 @@
[get.py] [get.py]
[test_no_top_browsing_context]
expected: FAIL
[test_no_browsing_context] [test_no_browsing_context]
expected: FAIL expected: FAIL
[test_no_such_element_with_invalid_value]
expected: FAIL
[test_no_such_element_from_other_window_handle[open\]]
expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_no_such_element_from_other_frame[open\]]
expected: FAIL
[test_no_such_element_from_other_frame[closed\]]
expected: FAIL
[test_stale_element_reference[top_context\]]
expected: FAIL
[test_stale_element_reference[child_context\]]
expected: FAIL
[test_get_shadow_root]
expected: FAIL
[test_no_shadow_root]
expected: FAIL

View file

@ -5,8 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_get_element_tag_name] [test_get_element_tag_name]
expected: FAIL expected: FAIL

View file

@ -5,9 +5,6 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_transform_capitalize[space\]] [test_transform_capitalize[space\]]
expected: FAIL expected: FAIL

View file

@ -5,9 +5,6 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_stale_element_reference[child_context\]] [test_stale_element_reference[child_context\]]
expected: FAIL expected: FAIL

View file

@ -5,8 +5,5 @@
[test_no_such_element_with_shadow_root] [test_no_such_element_with_shadow_root]
expected: FAIL expected: FAIL
[test_no_such_element_from_other_window_handle[closed\]]
expected: FAIL
[test_stale_element_reference[child_context\]] [test_stale_element_reference[child_context\]]
expected: FAIL expected: FAIL

View file

@ -1,6 +1,3 @@
[new.py] [new.py]
[test_no_top_browsing_context] [test_no_top_browsing_context]
expected: FAIL expected: FAIL
[test_no_browsing_context]
expected: ERROR

View file

@ -1,9 +1,3 @@
[new_tab.py] [new_tab.py]
[test_keeps_current_window_handle]
expected: FAIL
[test_opens_about_blank_in_new_tab]
expected: FAIL
[test_initial_selection_for_contenteditable] [test_initial_selection_for_contenteditable]
expected: FAIL expected: FAIL

View file

@ -2,11 +2,5 @@
[test_payload] [test_payload]
expected: FAIL expected: FAIL
[test_keeps_current_window_handle]
expected: FAIL
[test_opens_about_blank_in_new_window]
expected: FAIL
[test_initial_selection_for_contenteditable] [test_initial_selection_for_contenteditable]
expected: FAIL expected: FAIL

View file

@ -9,19 +9,16 @@
expected: FAIL expected: FAIL
[test_backspace_erases_keys] [test_backspace_erases_keys]
expected: ERROR expected: FAIL
[test_element_in_shadow_tree[outer-open\]] [test_element_in_shadow_tree[outer-open\]]
expected: ERROR expected: FAIL
[test_element_in_shadow_tree[outer-closed\]] [test_element_in_shadow_tree[outer-closed\]]
expected: ERROR expected: FAIL
[test_element_in_shadow_tree[inner-open\]] [test_element_in_shadow_tree[inner-open\]]
expected: ERROR expected: FAIL
[test_element_in_shadow_tree[inner-closed\]] [test_element_in_shadow_tree[inner-closed\]]
expected: ERROR expected: FAIL
[test_element_not_focused]
expected: ERROR

View file

@ -1,10 +1,4 @@
[pointer_origin.py] [pointer_origin.py]
[test_viewport_inside]
expected: FAIL
[test_pointer_inside]
expected: FAIL
[test_element_center_point] [test_element_center_point]
expected: FAIL expected: FAIL

View file

@ -13,6 +13,3 @@
[test_no_browsing_context_when_already_top_level] [test_no_browsing_context_when_already_top_level]
expected: FAIL expected: FAIL
[test_frame_id_shadow_root]
expected: FAIL

View file

@ -4,6 +4,3 @@
[test_no_browsing_context] [test_no_browsing_context]
expected: FAIL expected: FAIL
[test_format_and_dimensions]
expected: FAIL