Skip to content

Truly independent web browser

LibWeb: Require user attention for a gamepad user gesture

Luke Wildee93a9ed38060
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 3b290287d12..fb10ec07078 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -5842,6 +5842,14 @@ bool Document::has_focus() const
return false;
}
+// https://html.spec.whatwg.org/multipage/interaction.html#fully-active-descendant-of-a-top-level-traversable-with-user-attention
+bool Document::is_fully_active_descendant_of_a_top_level_traversable_with_user_attention() const
+{
+ // A Document d is a fully active descendant of a top-level traversable with user attention when d is fully active
+ // and d's node navigable's top-level traversable has user attention.
+ return is_fully_active() && page().top_level_traversable_has_user_attention();
+}
+
// https://html.spec.whatwg.org/multipage/interaction.html#allow-focus-steps
bool Document::allow_focus() const
{
diff --git a/Libraries/LibWeb/DOM/Document.h b/Libraries/LibWeb/DOM/Document.h
index b1dcc206cf4..7153d3e6e0c 100644
--- a/Libraries/LibWeb/DOM/Document.h
+++ b/Libraries/LibWeb/DOM/Document.h
@@ -967,6 +967,8 @@ public:
bool has_focus_for_bindings() const;
bool has_focus() const;
+ bool is_fully_active_descendant_of_a_top_level_traversable_with_user_attention() const;
+
bool allow_focus() const;
void set_parser(Badge<HTML::HTMLParser>, HTML::HTMLParser&);
diff --git a/Libraries/LibWeb/Gamepad/Gamepad.cpp b/Libraries/LibWeb/Gamepad/Gamepad.cpp
index 028549623a3..9f064831159 100644
--- a/Libraries/LibWeb/Gamepad/Gamepad.cpp
+++ b/Libraries/LibWeb/Gamepad/Gamepad.cpp
@@ -387,8 +387,14 @@ void Gamepad::update_gamepad_state(Badge<NavigatorGamepadPartial>)
// 6. Let navigator be gamepad's relevant global object's Navigator object.
auto navigator = window.navigator();
- // 7. If navigator.[[hasGamepadGesture]] is false and gamepad contains a gamepad user gesture:
- if (!navigator->has_gamepad_gesture() && contains_gamepad_user_gesture()) {
+ // 7. Let document be gamepad's relevant global object's associated Document; otherwise null.
+ // 8. If document is null, abort these steps.
+ // NB: A Window always has an associated Document.
+ auto& document = window.associated_document();
+
+ // 9. If navigator.[[hasGamepadGesture]] is false and document is a fully active descendant of a top-level
+ // traversable with user attention and gamepad contains a gamepad user gesture:
+ if (!navigator->has_gamepad_gesture() && document.is_fully_active_descendant_of_a_top_level_traversable_with_user_attention() && contains_gamepad_user_gesture()) {
// 1. Set navigator.[[hasGamepadGesture]] to true.
navigator->set_has_gamepad_gesture({}, true);
@@ -402,10 +408,7 @@ void Gamepad::update_gamepad_state(Badge<NavigatorGamepadPartial>)
// 2. Set connectedGamepad.[[timestamp]] to now.
connected_gamepad->m_timestamp = now;
- // 3. Let document be gamepad's relevant global object's associated Document; otherwise null.
- auto& document = window.associated_document();
-
- // 4. If document is not null and is fully active, then queue a global task on the gamepad task source
+ // 3. If document is not null and is fully active, then queue a global task on the gamepad task source
// to fire an event named gamepadconnected at gamepad's relevant global object using GamepadEvent
// with its gamepad attribute initialized to connectedGamepad.
if (document.is_fully_active()) {
diff --git a/Libraries/LibWeb/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 0835a22ddd5..cb0a03094ae 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -170,6 +170,17 @@ void Page::visit_edges(JS::Cell::Visitor& visitor)
});
}
+// https://html.spec.whatwg.org/multipage/interaction.html#user-attention
+bool Page::top_level_traversable_has_user_attention() const
+{
+ // A top-level traversable has user attention when its system visibility state is "visible", and it either has
+ // system focus or user agent widgets directly related to it can receive keyboard input channeled from the
+ // operating system.
+ // FIXME: Also give the top-level traversable user attention while a user agent widget related to it, such as the
+ // location bar, can receive keyboard input.
+ return m_system_visibility_state == HTML::VisibilityState::Visible && client().has_focus();
+}
+
// The navigable keyboard input goes to in the tab, which is the top-level traversable until focus moves into another.
GC::Ptr<HTML::Navigable> Page::focused_navigable() const
{
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index 2814b12d54a..5737370ea05 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -163,6 +163,7 @@ public:
HTML::VisibilityState system_visibility_state() const { return m_system_visibility_state; }
void set_system_visibility_state(HTML::VisibilityState visibility_state) { m_system_visibility_state = visibility_state; }
+ bool top_level_traversable_has_user_attention() const;
void unfullscreen_descendant_documents(Vector<GC::Root<HTML::Navigable>> const&);
enum class ElementIsRequestedElement : u8 {
No,
diff --git a/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.txt b/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.txt
new file mode 100644
index 00000000000..b5b4db843b7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.txt
@@ -0,0 +1,4 @@
+Received gamepadconnected event for 'Ladybird Virtual Gamepad' at index 0
+Gamepads in a new navigable before pressing a button: 0
+Gamepads in the new navigable after pressing a button without system focus: 0
+Gamepads in the new navigable after pressing a button with user attention: 1
diff --git a/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-without-user-attention.txt b/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-without-user-attention.txt
new file mode 100644
index 00000000000..3c1410a1b04
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/GamepadAPI/gamepad-not-exposed-without-user-attention.txt
@@ -0,0 +1,4 @@
+Gamepads after pressing a button without system focus: 0
+Gamepads after pressing a button while hidden: 0
+Received gamepadconnected event for 'Ladybird Virtual Gamepad' at index 0
+Gamepads after pressing a button with user attention: 1
diff --git a/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.html b/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.html
new file mode 100644
index 00000000000..37f86c114f7
--- /dev/null
+++ b/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-to-new-navigable-without-user-attention.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script src="gamepad-helper.js"></script>
+<script>
+ promiseTest(async () => {
+ const gamepad = internals.connectVirtualGamepad();
+ await pumpGamepadEvents();
+ listenForGamepadConnected();
+
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ gamepad.setButton(gamepad.buttons[0], false);
+ await pumpGamepadEvents();
+
+ const iframe = document.createElement("iframe");
+ const loaded = new Promise(resolve => iframe.addEventListener("load", resolve, { once: true }));
+ iframe.srcdoc = "<!DOCTYPE html>";
+ document.body.appendChild(iframe);
+ await loaded;
+ const iframeNavigator = iframe.contentWindow.navigator;
+ println(`Gamepads in a new navigable before pressing a button: ${iframeNavigator.getGamepads().length}`);
+
+ internals.setPageFocus(false);
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ println(`Gamepads in the new navigable after pressing a button without system focus: ${iframeNavigator.getGamepads().length}`);
+ gamepad.setButton(gamepad.buttons[0], false);
+ await pumpGamepadEvents();
+ internals.setPageFocus(true);
+
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ println(`Gamepads in the new navigable after pressing a button with user attention: ${iframeNavigator.getGamepads().length}`);
+
+ gamepad.disconnect();
+ await pumpGamepadEvents();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-without-user-attention.html b/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-without-user-attention.html
new file mode 100644
index 00000000000..0dc65398358
--- /dev/null
+++ b/Tests/LibWeb/Text/input/GamepadAPI/gamepad-not-exposed-without-user-attention.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script src="gamepad-helper.js"></script>
+<script>
+ promiseTest(async () => {
+ const gamepad = internals.connectVirtualGamepad();
+ await pumpGamepadEvents();
+ listenForGamepadConnected();
+
+ internals.setPageFocus(false);
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ println(`Gamepads after pressing a button without system focus: ${navigator.getGamepads().length}`);
+ gamepad.setButton(gamepad.buttons[0], false);
+ await pumpGamepadEvents();
+ internals.setPageFocus(true);
+
+ await setSystemVisibilityState("hidden");
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ println(`Gamepads after pressing a button while hidden: ${navigator.getGamepads().length}`);
+ gamepad.setButton(gamepad.buttons[0], false);
+ await pumpGamepadEvents();
+ await setSystemVisibilityState("visible");
+
+ gamepad.setButton(gamepad.buttons[0], true);
+ await pumpGamepadEvents();
+ println(`Gamepads after pressing a button with user attention: ${navigator.getGamepads().length}`);
+
+ gamepad.disconnect();
+ await pumpGamepadEvents();
+ });
+</script>