From c82f678fc615117caaf055219143960203ccd3e9 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Mon, 15 May 2023 11:17:58 -0400 Subject: [PATCH] LibWeb+WebContent: Add APIs to control video playback state This allows for the browser process to control the play/pause state, whether we paint user agent controls on the video, and whether the video loops when it finishes playing. --- Userland/Libraries/LibWeb/Page/Page.cpp | 80 +++++++++++++++++++ Userland/Libraries/LibWeb/Page/Page.h | 5 ++ .../LibWebView/ViewImplementation.cpp | 15 ++++ .../Libraries/LibWebView/ViewImplementation.h | 4 + .../WebContent/ConnectionFromClient.cpp | 15 ++++ .../WebContent/ConnectionFromClient.h | 4 + Userland/Services/WebContent/PageHost.cpp | 15 ++++ Userland/Services/WebContent/PageHost.h | 4 + .../Services/WebContent/WebContentServer.ipc | 4 + 9 files changed, 146 insertions(+) diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index 7a6cd692408..e1e5f3e915e 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -5,10 +5,12 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include #include #include +#include #include #include #include @@ -281,4 +283,82 @@ void Page::did_request_video_context_menu(i32 video_id, CSSPixelPoint position, client().page_did_request_video_context_menu(position, url, target, modifiers, is_playing, has_user_agent_controls, is_looping); } +WebIDL::ExceptionOr Page::toggle_video_play_state() +{ + auto video_element = video_context_menu_element(); + if (!video_element) + return {}; + + // FIXME: This runs from outside the context of any user script, so we do not have a running execution + // context. This pushes one to allow the promise creation hook to run. + auto& environment_settings = video_element->document().relevant_settings_object(); + environment_settings.prepare_to_run_script(); + + ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } }; + + if (video_element->potentially_playing()) + TRY(video_element->pause()); + else + TRY(video_element->play()); + + return {}; +} + +WebIDL::ExceptionOr Page::toggle_video_loop_state() +{ + auto video_element = video_context_menu_element(); + if (!video_element) + return {}; + + // FIXME: This runs from outside the context of any user script, so we do not have a running execution + // context. This pushes one to allow the promise creation hook to run. + auto& environment_settings = video_element->document().relevant_settings_object(); + environment_settings.prepare_to_run_script(); + + ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } }; + + if (video_element->has_attribute(HTML::AttributeNames::loop)) + video_element->remove_attribute(HTML::AttributeNames::loop); + else + TRY(video_element->set_attribute(HTML::AttributeNames::loop, {})); + + return {}; +} + +WebIDL::ExceptionOr Page::toggle_video_controls_state() +{ + auto video_element = video_context_menu_element(); + if (!video_element) + return {}; + + // FIXME: This runs from outside the context of any user script, so we do not have a running execution + // context. This pushes one to allow the promise creation hook to run. + auto& environment_settings = video_element->document().relevant_settings_object(); + environment_settings.prepare_to_run_script(); + + ScopeGuard guard { [&] { environment_settings.clean_up_after_running_script(); } }; + + if (video_element->has_attribute(HTML::AttributeNames::controls)) + video_element->remove_attribute(HTML::AttributeNames::controls); + else + TRY(video_element->set_attribute(HTML::AttributeNames::controls, {})); + + return {}; +} + +JS::GCPtr Page::video_context_menu_element() +{ + if (!m_video_context_menu_element_id.has_value()) + return nullptr; + + auto* dom_node = DOM::Node::from_id(*m_video_context_menu_element_id); + if (dom_node == nullptr) + return nullptr; + + if (!is(dom_node)) + return nullptr; + + return static_cast(dom_node); +} + } diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index ac0b9b073a1..0b1ef936d9b 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -116,10 +116,15 @@ public: void accept_dialog(); void did_request_video_context_menu(i32 video_id, CSSPixelPoint, AK::URL const&, DeprecatedString const& target, unsigned modifiers, bool is_playing, bool has_user_agent_controls, bool is_looping); + WebIDL::ExceptionOr toggle_video_play_state(); + WebIDL::ExceptionOr toggle_video_loop_state(); + WebIDL::ExceptionOr toggle_video_controls_state(); bool pdf_viewer_supported() const { return m_pdf_viewer_supported; } private: + JS::GCPtr video_context_menu_element(); + PageClient& m_client; JS::Handle m_top_level_browsing_context; diff --git a/Userland/Libraries/LibWebView/ViewImplementation.cpp b/Userland/Libraries/LibWebView/ViewImplementation.cpp index 5d09a8ac682..85cd532f862 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.cpp +++ b/Userland/Libraries/LibWebView/ViewImplementation.cpp @@ -131,6 +131,21 @@ void ViewImplementation::run_javascript(StringView js_source) client().async_run_javascript(js_source); } +void ViewImplementation::toggle_video_play_state() +{ + client().async_toggle_video_play_state(); +} + +void ViewImplementation::toggle_video_loop_state() +{ + client().async_toggle_video_loop_state(); +} + +void ViewImplementation::toggle_video_controls_state() +{ + client().async_toggle_video_controls_state(); +} + void ViewImplementation::handle_resize() { resize_backing_stores_if_needed(WindowResizeInProgress::Yes); diff --git a/Userland/Libraries/LibWebView/ViewImplementation.h b/Userland/Libraries/LibWebView/ViewImplementation.h index 21ec8de72a2..23e8a5d2d4a 100644 --- a/Userland/Libraries/LibWebView/ViewImplementation.h +++ b/Userland/Libraries/LibWebView/ViewImplementation.h @@ -70,6 +70,10 @@ public: void run_javascript(StringView); + void toggle_video_play_state(); + void toggle_video_loop_state(); + void toggle_video_controls_state(); + virtual void notify_server_did_layout(Badge, Gfx::IntSize content_size) = 0; virtual void notify_server_did_paint(Badge, i32 bitmap_id, Gfx::IntSize) = 0; virtual void notify_server_did_invalidate_content_rect(Badge, Gfx::IntRect const&) = 0; diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 329d08953f7..c248d6a0d05 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -756,6 +756,21 @@ void ConnectionFromClient::prompt_closed(Optional const& response) m_page_host->prompt_closed(response); } +void ConnectionFromClient::toggle_video_play_state() +{ + m_page_host->toggle_video_play_state().release_value_but_fixme_should_propagate_errors(); +} + +void ConnectionFromClient::toggle_video_loop_state() +{ + m_page_host->toggle_video_loop_state().release_value_but_fixme_should_propagate_errors(); +} + +void ConnectionFromClient::toggle_video_controls_state() +{ + m_page_host->toggle_video_controls_state().release_value_but_fixme_should_propagate_errors(); +} + void ConnectionFromClient::inspect_accessibility_tree() { if (auto* doc = page().top_level_browsing_context().active_document()) { diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index 0cf634560a2..d2ed7a58bcf 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -95,6 +95,10 @@ private: virtual void confirm_closed(bool accepted) override; virtual void prompt_closed(Optional const& response) override; + virtual void toggle_video_play_state() override; + virtual void toggle_video_loop_state() override; + virtual void toggle_video_controls_state() override; + virtual Messages::WebContentServer::TakeDocumentScreenshotResponse take_document_screenshot() override; virtual Messages::WebContentServer::GetLocalStorageEntriesResponse get_local_storage_entries() override; diff --git a/Userland/Services/WebContent/PageHost.cpp b/Userland/Services/WebContent/PageHost.cpp index 0a505ee6548..258a359e10b 100644 --- a/Userland/Services/WebContent/PageHost.cpp +++ b/Userland/Services/WebContent/PageHost.cpp @@ -334,6 +334,21 @@ void PageHost::prompt_closed(Optional response) page().prompt_closed(move(response)); } +Web::WebIDL::ExceptionOr PageHost::toggle_video_play_state() +{ + return page().toggle_video_play_state(); +} + +Web::WebIDL::ExceptionOr PageHost::toggle_video_loop_state() +{ + return page().toggle_video_loop_state(); +} + +Web::WebIDL::ExceptionOr PageHost::toggle_video_controls_state() +{ + return page().toggle_video_controls_state(); +} + void PageHost::page_did_request_accept_dialog() { m_client.async_did_request_accept_dialog(); diff --git a/Userland/Services/WebContent/PageHost.h b/Userland/Services/WebContent/PageHost.h index 08c259f217e..9f8b068057d 100644 --- a/Userland/Services/WebContent/PageHost.h +++ b/Userland/Services/WebContent/PageHost.h @@ -49,6 +49,10 @@ public: void confirm_closed(bool accepted); void prompt_closed(Optional response); + Web::WebIDL::ExceptionOr toggle_video_play_state(); + Web::WebIDL::ExceptionOr toggle_video_loop_state(); + Web::WebIDL::ExceptionOr toggle_video_controls_state(); + [[nodiscard]] Gfx::Color background_color() const; private: diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 85331602d5f..748b6deb876 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -76,4 +76,8 @@ endpoint WebContentServer alert_closed() =| confirm_closed(bool accepted) =| prompt_closed(Optional response) =| + + toggle_video_play_state() =| + toggle_video_loop_state() =| + toggle_video_controls_state() =| }