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.
This commit is contained in:
Timothy Flynn 2025-02-15 08:06:36 -05:00 committed by Tim Flynn
commit 3904765c4f
Notes: github-actions[bot] 2025-02-19 13:47:36 +00:00
2 changed files with 33 additions and 1 deletions

View file

@ -24,8 +24,29 @@
namespace WebView {
ViewImplementation::ViewImplementation()
static HashMap<u64, ViewImplementation*> s_all_views;
static u64 s_view_count = 1; // This has to start at 1 for Firefox DevTools.
void ViewImplementation::for_each_view(Function<IterationDecision(ViewImplementation&)> callback)
{
for (auto& view : s_all_views) {
if (callback(*view.value) == IterationDecision::Break)
break;
}
}
Optional<ViewImplementation&> 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);
}