1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 23:47:45 +00:00

LibWeb+WebContent+Browser: Plumb visibility state from GUI to web pages

OOPWV now reacts to show/hide events and informs LibWeb about the state
change. This makes visibilitychange events fire when switching tabs. :^)
This commit is contained in:
Andreas Kling 2022-09-19 20:50:33 +02:00
parent 5908873b45
commit d4acdac317
14 changed files with 150 additions and 11 deletions

View file

@ -1113,7 +1113,7 @@ DOM::ExceptionOr<void> BrowsingContext::traverse_the_history(size_t entry_index,
new_document->set_page_showing(true);
// 3. Update the visibility state of newDocument to "hidden".
new_document->update_the_visibility_state("hidden");
new_document->update_the_visibility_state(VisibilityState::Hidden);
// 4. Fire a page transition event named pageshow at newDocument's relevant global object with true.
auto& window = verify_cast<HTML::Window>(relevant_global_object(*new_document));
@ -1248,18 +1248,55 @@ BrowsingContext const* BrowsingContext::the_one_permitted_sandboxed_navigator()
}
// https://html.spec.whatwg.org/multipage/browsers.html#document-family
bool BrowsingContext::document_family_contains(DOM::Document const& document) const
Vector<JS::Handle<DOM::Document>> BrowsingContext::document_family() const
{
HashTable<DOM::Document const*> family;
HashTable<DOM::Document*> documents;
for (auto& entry : m_session_history) {
if (!entry.document)
continue;
if (family.set(entry.document) == AK::HashSetResult::ReplacedExistingEntry)
if (documents.set(entry.document.ptr()) == AK::HashSetResult::ReplacedExistingEntry)
continue;
// FIXME: The document family of a Document object consists of the union of all the document families of the browsing contexts in the list of the descendant browsing contexts of the Document object.
for (auto& context : entry.document->list_of_descendant_browsing_contexts()) {
for (auto& document : context->document_family()) {
documents.set(document.ptr());
}
}
}
return family.contains(&document);
Vector<JS::Handle<DOM::Document>> family;
for (auto* document : documents) {
family.append(*document);
}
return family;
}
// https://html.spec.whatwg.org/multipage/browsers.html#document-family
bool BrowsingContext::document_family_contains(DOM::Document const& document) const
{
return document_family().first_matching([&](auto& entry) { return entry.ptr() == &document; }).has_value();
}
VisibilityState BrowsingContext::system_visibility_state() const
{
return m_system_visibility_state;
}
// https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
void BrowsingContext::set_system_visibility_state(VisibilityState visibility_state)
{
if (m_system_visibility_state == visibility_state)
return;
m_system_visibility_state = visibility_state;
// When a user-agent determines that the system visibility state for top-level browsing context context
// has changed to newState, it must queue a task on the user interaction task source to update
// the visibility state of all the Document objects in the top-level browsing context's document family with newState.
auto document_family = top_level_browsing_context().document_family();
queue_global_task(Task::Source::UserInteraction, Bindings::main_thread_vm().current_realm()->global_object(), [visibility_state, document_family = move(document_family)]() mutable {
for (auto& document : document_family) {
document->update_the_visibility_state(visibility_state);
}
});
}
}

View file

@ -18,6 +18,7 @@
#include <LibWeb/HTML/HistoryHandlingBehavior.h>
#include <LibWeb/HTML/Origin.h>
#include <LibWeb/HTML/SessionHistoryEntry.h>
#include <LibWeb/HTML/VisibilityState.h>
#include <LibWeb/Loader/FrameLoader.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Platform/Timer.h>
@ -158,8 +159,12 @@ public:
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#traverse-the-history
DOM::ExceptionOr<void> traverse_the_history(size_t entry_index, HistoryHandlingBehavior = HistoryHandlingBehavior::Default, bool explicit_history_navigation = false);
Vector<JS::Handle<DOM::Document>> document_family() const;
bool document_family_contains(DOM::Document const&) const;
VisibilityState system_visibility_state() const;
void set_system_visibility_state(VisibilityState);
private:
explicit BrowsingContext(Page&, HTML::BrowsingContextContainer*);
@ -205,6 +210,9 @@ private:
// https://html.spec.whatwg.org/multipage/browsers.html#tlbc-group
RefPtr<BrowsingContextGroup> m_group;
// https://html.spec.whatwg.org/multipage/interaction.html#system-visibility-state
VisibilityState m_system_visibility_state { VisibilityState::Hidden };
};
HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin);

View file

@ -18,6 +18,12 @@
namespace Web::HTML {
HashTable<BrowsingContextContainer*>& BrowsingContextContainer::all_instances()
{
static HashTable<BrowsingContextContainer*> set;
return set;
}
BrowsingContextContainer::BrowsingContextContainer(DOM::Document& document, DOM::QualifiedName qualified_name)
: HTMLElement(document, move(qualified_name))
{

View file

@ -16,6 +16,8 @@ class BrowsingContextContainer : public HTMLElement {
public:
virtual ~BrowsingContextContainer() override;
static HashTable<BrowsingContextContainer*>& all_instances();
BrowsingContext* nested_browsing_context() { return m_nested_browsing_context; }
BrowsingContext const* nested_browsing_context() const { return m_nested_browsing_context; }

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace Web::HTML {
enum VisibilityState {
Hidden,
Visible,
};
}