From 3904765c4fc46b12b80aa38382335bb4161f2d68 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 15 Feb 2025 08:06:36 -0500 Subject: [PATCH] LibWebView: Add simple ID tracking to WebViews For Firefox DevTools, we will need to track WebViews by a numerical ID. Here, we just increment a static 64-bit counter. We can switch to using IDAllocator if we ever have an issue with this. This patch adds such an ID to the views and a couple of APIs to access WebViews after creation. --- Libraries/LibWebView/ViewImplementation.cpp | 25 ++++++++++++++++++++- Libraries/LibWebView/ViewImplementation.h | 9 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp index a619274e0db..c77811fcced 100644 --- a/Libraries/LibWebView/ViewImplementation.cpp +++ b/Libraries/LibWebView/ViewImplementation.cpp @@ -24,8 +24,29 @@ namespace WebView { -ViewImplementation::ViewImplementation() +static HashMap s_all_views; +static u64 s_view_count = 1; // This has to start at 1 for Firefox DevTools. + +void ViewImplementation::for_each_view(Function callback) { + for (auto& view : s_all_views) { + if (callback(*view.value) == IterationDecision::Break) + break; + } +} + +Optional ViewImplementation::find_view_by_id(u64 id) +{ + if (auto view = s_all_views.get(id); view.has_value()) + return *view.value(); + return {}; +} + +ViewImplementation::ViewImplementation() + : m_view_id(s_view_count++) +{ + s_all_views.set(m_view_id, this); + m_repeated_crash_timer = Core::Timer::create_single_shot(1000, [this] { // Reset the "crashing a lot" counter after 1 second in case we just // happen to be visiting crashy websites a lot. @@ -44,6 +65,8 @@ ViewImplementation::ViewImplementation() ViewImplementation::~ViewImplementation() { + s_all_views.remove(m_view_id); + if (m_client_state.client) m_client_state.client->unregister_view(m_client_state.page_index); } diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h index 46c91ca38a4..17fb9f2b138 100644 --- a/Libraries/LibWebView/ViewImplementation.h +++ b/Libraries/LibWebView/ViewImplementation.h @@ -43,6 +43,11 @@ public: String fonts_json; }; + static void for_each_view(Function); + static Optional find_view_by_id(u64); + + u64 view_id() const { return m_view_id; } + void set_url(Badge, URL::URL url) { m_url = move(url); } URL::URL const& url() const { return m_url; } @@ -304,6 +309,10 @@ protected: size_t m_number_of_elements_playing_audio { 0 }; Web::HTML::MuteState m_mute_state { Web::HTML::MuteState::Unmuted }; + + // FIXME: Reconcile this ID with `page_id`. The latter is only unique per WebContent connection, whereas the view ID + // is required to be globally unique for Firefox DevTools. + u64 m_view_id { 0 }; }; }