Bläddra i källkod

LibWeb+LibWebView+WebContent: Inform chromes when audio is played/paused

Most browsers have some indicator when audio is playing in a tab, which
makes it easier to find that tab and mute unwanted audio. This adds an
IPC to allow the Ladybird chromes to do something similar.
Timothy Flynn 1 år sedan
förälder
incheckning
40c0dd81d2

+ 1 - 1
Meta/Lagom/Tools/CodeGenerators/IPCCompiler/main.cpp

@@ -70,7 +70,7 @@ static bool is_primitive_type(ByteString const& type)
 static bool is_simple_type(ByteString const& type)
 {
     // Small types that it makes sense just to pass by value.
-    return type.is_one_of("Gfx::Color", "Web::DevicePixels", "Gfx::IntPoint", "Gfx::FloatPoint", "Web::DevicePixelPoint", "Gfx::IntSize", "Gfx::FloatSize", "Web::DevicePixelSize", "Core::File::OpenMode", "Web::Cookie::Source", "Web::HTML::AllowMultipleFiles");
+    return type.is_one_of("Gfx::Color", "Web::DevicePixels", "Gfx::IntPoint", "Gfx::FloatPoint", "Web::DevicePixelPoint", "Gfx::IntSize", "Gfx::FloatSize", "Web::DevicePixelSize", "Core::File::OpenMode", "Web::Cookie::Source", "Web::HTML::AllowMultipleFiles", "Web::HTML::AudioPlayState");
 }
 
 static bool is_primitive_or_simple_type(ByteString const& type)

+ 16 - 0
Userland/Libraries/LibWeb/HTML/AudioPlayState.h

@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+namespace Web::HTML {
+
+enum class AudioPlayState {
+    Paused,
+    Playing,
+};
+
+}

+ 5 - 0
Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp

@@ -18,6 +18,7 @@
 #include <LibWeb/Fetch/Infrastructure/FetchController.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Requests.h>
 #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
+#include <LibWeb/HTML/AudioPlayState.h>
 #include <LibWeb/HTML/AudioTrack.h>
 #include <LibWeb/HTML/AudioTrackList.h>
 #include <LibWeb/HTML/CORSSettingAttribute.h>
@@ -35,6 +36,7 @@
 #include <LibWeb/HTML/VideoTrackList.h>
 #include <LibWeb/Layout/Node.h>
 #include <LibWeb/MimeSniff/MimeType.h>
+#include <LibWeb/Page/Page.h>
 #include <LibWeb/Painting/Paintable.h>
 #include <LibWeb/WebIDL/Promise.h>
 
@@ -1602,6 +1604,9 @@ void HTMLMediaElement::set_paused(bool paused)
     if (auto* paintable = this->paintable())
         paintable->set_needs_display();
     set_needs_style_update(true);
+
+    if (m_audio_tracks->has_enabled_track())
+        document().page().client().page_did_change_audio_play_state(paused ? AudioPlayState::Paused : AudioPlayState::Playing);
 }
 
 // https://html.spec.whatwg.org/multipage/media.html#blocked-media-element

+ 3 - 0
Userland/Libraries/LibWeb/Page/Page.h

@@ -30,6 +30,7 @@
 #include <LibWeb/Cookie/Cookie.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/HTML/ActivateTab.h>
+#include <LibWeb/HTML/AudioPlayState.h>
 #include <LibWeb/HTML/ColorPickerUpdateState.h>
 #include <LibWeb/HTML/FileFilter.h>
 #include <LibWeb/HTML/SelectItem.h>
@@ -295,6 +296,8 @@ public:
 
     virtual void page_did_insert_clipboard_entry([[maybe_unused]] String data, [[maybe_unused]] String presentation_style, [[maybe_unused]] String mime_type) { }
 
+    virtual void page_did_change_audio_play_state(HTML::AudioPlayState) { }
+
     virtual WebView::SocketPair request_worker_agent() { return { -1, -1 }; }
 
     virtual void inspector_did_load() { }

+ 8 - 0
Userland/Libraries/LibWebView/ViewImplementation.cpp

@@ -315,6 +315,14 @@ void ViewImplementation::toggle_media_controls_state()
     client().async_toggle_media_controls_state(page_id());
 }
 
+void ViewImplementation::did_change_audio_play_state(Badge<WebContentClient>, Web::HTML::AudioPlayState play_state)
+{
+    m_audio_play_state = play_state;
+
+    if (on_audio_play_state_changed)
+        on_audio_play_state_changed(m_audio_play_state);
+}
+
 void ViewImplementation::handle_resize()
 {
     resize_backing_stores_if_needed(WindowResizeInProgress::Yes);

+ 7 - 0
Userland/Libraries/LibWebView/ViewImplementation.h

@@ -17,6 +17,7 @@
 #include <LibGfx/StandardCursor.h>
 #include <LibWeb/Forward.h>
 #include <LibWeb/HTML/ActivateTab.h>
+#include <LibWeb/HTML/AudioPlayState.h>
 #include <LibWeb/HTML/ColorPickerUpdateState.h>
 #include <LibWeb/HTML/FileFilter.h>
 #include <LibWeb/HTML/SelectItem.h>
@@ -101,6 +102,9 @@ public:
     void toggle_media_loop_state();
     void toggle_media_controls_state();
 
+    void did_change_audio_play_state(Badge<WebContentClient>, Web::HTML::AudioPlayState);
+    Web::HTML::AudioPlayState audio_play_state() const { return m_audio_play_state; }
+
     enum class ScreenshotType {
         Visible,
         Full,
@@ -178,6 +182,7 @@ public:
     Function<void()> on_text_test_finish;
     Function<void(Gfx::Color)> on_theme_color_change;
     Function<void(String const&, String const&, String const&)> on_insert_clipboard_entry;
+    Function<void(Web::HTML::AudioPlayState)> on_audio_play_state_changed;
     Function<void()> on_inspector_loaded;
     Function<void(i32, Optional<Web::CSS::Selector::PseudoElement::Type> const&)> on_inspector_selected_dom_node;
     Function<void(i32, String const&)> on_inspector_set_dom_node_text;
@@ -252,6 +257,8 @@ protected:
     RefPtr<Core::Timer> m_repeated_crash_timer;
 
     RefPtr<Core::Promise<LexicalPath>> m_pending_screenshot;
+
+    Web::HTML::AudioPlayState m_audio_play_state { Web::HTML::AudioPlayState::Paused };
 };
 
 }

+ 12 - 0
Userland/Libraries/LibWebView/WebContentClient.cpp

@@ -870,6 +870,18 @@ void WebContentClient::did_insert_clipboard_entry(u64 page_id, String const& dat
         view.on_insert_clipboard_entry(data, presentation_style, mime_type);
 }
 
+void WebContentClient::did_change_audio_play_state(u64 page_id, Web::HTML::AudioPlayState play_state)
+{
+    auto maybe_view = m_views.get(page_id);
+    if (!maybe_view.has_value()) {
+        dbgln("Received insert clipboard entry for unknown page ID {}", page_id);
+        return;
+    }
+
+    auto& view = *maybe_view.value();
+    view.did_change_audio_play_state({}, play_state);
+}
+
 void WebContentClient::inspector_did_load(u64 page_id)
 {
     auto maybe_view = m_views.get(page_id);

+ 1 - 0
Userland/Libraries/LibWebView/WebContentClient.h

@@ -96,6 +96,7 @@ private:
     virtual void did_finish_text_test(u64 page_id) override;
     virtual void did_change_theme_color(u64 page_id, Gfx::Color color) override;
     virtual void did_insert_clipboard_entry(u64 page_id, String const& data, String const& presentation_style, String const& mime_type) override;
+    virtual void did_change_audio_play_state(u64 page_id, Web::HTML::AudioPlayState) override;
     virtual void inspector_did_load(u64 page_id) override;
     virtual void inspector_did_select_dom_node(u64 page_id, i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element) override;
     virtual void inspector_did_set_dom_node_text(u64 page_id, i32 node_id, String const& text) override;

+ 5 - 0
Userland/Services/WebContent/PageClient.cpp

@@ -577,6 +577,11 @@ void PageClient::page_did_insert_clipboard_entry(String data, String presentatio
     client().async_did_insert_clipboard_entry(m_id, move(data), move(presentation_style), move(mime_type));
 }
 
+void PageClient::page_did_change_audio_play_state(Web::HTML::AudioPlayState play_state)
+{
+    client().async_did_change_audio_play_state(m_id, play_state);
+}
+
 WebView::SocketPair PageClient::request_worker_agent()
 {
     auto response = client().send_sync_but_allow_failure<Messages::WebContentClient::RequestWorkerAgent>(m_id);

+ 2 - 0
Userland/Services/WebContent/PageClient.h

@@ -10,6 +10,7 @@
 
 #include <LibAccelGfx/Forward.h>
 #include <LibGfx/Rect.h>
+#include <LibWeb/HTML/AudioPlayState.h>
 #include <LibWeb/HTML/FileFilter.h>
 #include <LibWeb/Page/Page.h>
 #include <LibWeb/PixelUnits.h>
@@ -140,6 +141,7 @@ private:
     virtual void page_did_finish_text_test() override;
     virtual void page_did_change_theme_color(Gfx::Color color) override;
     virtual void page_did_insert_clipboard_entry(String data, String presentation_style, String mime_type) override;
+    virtual void page_did_change_audio_play_state(Web::HTML::AudioPlayState) override;
     virtual WebView::SocketPair request_worker_agent() override;
     virtual void inspector_did_load() override;
     virtual void inspector_did_select_dom_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement::Type> const& pseudo_element) override;

+ 3 - 0
Userland/Services/WebContent/WebContentClient.ipc

@@ -6,6 +6,7 @@
 #include <LibWeb/Cookie/ParsedCookie.h>
 #include <LibWeb/CSS/Selector.h>
 #include <LibWeb/HTML/ActivateTab.h>
+#include <LibWeb/HTML/AudioPlayState.h>
 #include <LibWeb/HTML/FileFilter.h>
 #include <LibWeb/HTML/SelectedFile.h>
 #include <LibWeb/HTML/SelectItem.h>
@@ -78,6 +79,8 @@ endpoint WebContentClient
     did_change_theme_color(u64 page_id, Gfx::Color color) =|
     did_insert_clipboard_entry(u64 page_id, String data, String presentation_style, String mime_type) =|
 
+    did_change_audio_play_state(u64 page_id, Web::HTML::AudioPlayState play_state) =|
+
     did_output_js_console_message(u64 page_id, i32 message_index) =|
     did_get_js_console_messages(u64 page_id, i32 start_index, Vector<ByteString> message_types, Vector<ByteString> messages) =|