From ac8b892a25a0f00eb61ad6faf34aa52853f7d74e Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Thu, 4 May 2023 08:50:34 -0400 Subject: [PATCH] LibWeb: Pause HTMLMediaElement when its document becomes inactive For example, when navigating to another page, this ensures any media resource will not continue playing. --- Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp | 10 ++++++++++ Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp index a1c5e935e7..fd9a4cf34d 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -46,6 +47,14 @@ JS::ThrowCompletionOr HTMLMediaElement::initialize(JS::Realm& realm) set_prototype(&Bindings::ensure_web_prototype(realm, "HTMLMediaElement")); m_video_tracks = TRY(realm.heap().allocate(realm, realm)); + m_document_observer = TRY(realm.heap().allocate(realm, realm, document())); + + // https://html.spec.whatwg.org/multipage/media.html#playing-the-media-resource:media-element-82 + m_document_observer->document_became_inactive = [this]() { + // If the media element's node document stops being a fully active document, then the playback will stop until + // the document is active again. + pause_element().release_value_but_fixme_should_propagate_errors(); + }; return {}; } @@ -64,6 +73,7 @@ void HTMLMediaElement::visit_edges(Cell::Visitor& visitor) visitor.visit(m_error); visitor.visit(m_fetch_controller); visitor.visit(m_video_tracks); + visitor.visit(m_document_observer); } void HTMLMediaElement::parse_attribute(DeprecatedFlyString const& name, DeprecatedString const& value) diff --git a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h index 74109250f5..9638e5464a 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h +++ b/Userland/Libraries/LibWeb/HTML/HTMLMediaElement.h @@ -208,6 +208,8 @@ private: bool m_running_time_update_event_handler { false }; Optional