Skip to content

Truly independent web browser

LibWeb: Split the permission store into shared and per agent data

Andreas Kling9faff28cfdec
diff --git a/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp b/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
index 3951d53791b..c76063e0983 100644
--- a/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
+++ b/Libraries/LibWeb/PermissionsAPI/PermissionStore.cpp
@@ -4,10 +4,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include <AK/NeverDestroyed.h>
#include <LibGC/Root.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/HTML/Navigator.h>
+#include <LibWeb/HTML/Scripting/Agent.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/MediaCapture/MediaDevices.h>
#include <LibWeb/Page/Page.h>
@@ -53,8 +55,28 @@ PermissionStore& PermissionStore::the()
return s_the;
}
+struct AgentPermissionStatuses {
+ Vector<GC::Weak<PermissionStatus>> statuses;
+
+ // The newest snapshot that these statuses have seen.
+ Optional<u64> revision;
+};
+
+static AgentPermissionStatuses& agent_permission_statuses()
+{
+ static thread_local NeverDestroyed<AgentPermissionStatuses> statuses;
+ return *statuses;
+}
+
+void PermissionStore::forget_statuses_of_this_agent()
+{
+ agent_permission_statuses() = {};
+}
+
Optional<PermissionStoreEntry> PermissionStore::get_permission_store_entry(PermissionDescriptor const& descriptor, URL::Origin const& key)
{
+ MutexLocker locker(m_mutex);
+
// 1. If the user agent's permission store contains an entry whose descriptor is descriptor, and whose key is equal to key given descriptor, return that entry.
for (auto const& entry : m_entries) {
if (entry.descriptor.name == descriptor.name && permission_key_is_equal_to(descriptor, entry.permission_key, key))
@@ -67,6 +89,8 @@ Optional<PermissionStoreEntry> PermissionStore::get_permission_store_entry(Permi
void PermissionStore::set_permission_store_entry(PermissionDescriptor const& descriptor, URL::Origin const& key, PermissionState state)
{
+ MutexLocker locker(m_mutex);
+
// 1. Let newEntry be a new permission store entry whose descriptor is descriptor, and whose key is key, and whose state is state.
PermissionStoreEntry new_entry { descriptor, key, state };
@@ -82,8 +106,9 @@ void PermissionStore::set_permission_store_entry(PermissionDescriptor const& des
m_entries.append(move(new_entry));
}
-void PermissionStore::apply_snapshot(Web::Permissions::PermissionSnapshot snapshot, Notify notify)
+bool PermissionStore::store_snapshot(Web::Permissions::PermissionSnapshot const& snapshot)
{
+ MutexLocker locker(m_mutex);
auto changed = false;
if (!m_defaults_revision.has_value() || snapshot.revision > *m_defaults_revision) {
@@ -108,20 +133,41 @@ void PermissionStore::apply_snapshot(Web::Permissions::PermissionSnapshot snapsh
changed = true;
}
- if (!changed || notify == Notify::No)
+ return changed;
+}
+
+void PermissionStore::apply_snapshot(Web::Permissions::PermissionSnapshot snapshot, Notify notify)
+{
+ auto changed = store_snapshot(snapshot);
+ if (notify == Notify::No)
+ return;
+
+ // NB: The agent that got here first made the change, and the others still have to hear of it. An agent has heard of a
+ // revision only once its statuses have, which is not the case for one that it fetched without telling them.
+ auto& agent = agent_permission_statuses();
+ if (!agent.revision.has_value() || snapshot.revision > *agent.revision) {
+ agent.revision = snapshot.revision;
+ changed = true;
+ }
+
+ if (!changed)
return;
// NB: Updating a status allocates, and a collection then could clear another status's weak reference, so every
// live status is rooted first.
- m_statuses.remove_all_matching([](auto const& status) { return !status; });
+ agent.statuses.remove_all_matching([](auto const& status) { return !status; });
Vector<GC::Root<PermissionStatus>> statuses;
- for (auto const& status : m_statuses) {
+ for (auto const& status : agent.statuses) {
if (status)
statuses.append(GC::make_root(status.ptr()));
}
for (auto const& status : statuses)
status->update_from_store();
+ // Only a window agent has documents.
+ if (!HTML::surrounding_agent().is_similar_origin_window_agent())
+ return;
+
// A document whose microphone permission went away loses its live microphone tracks.
for (auto navigable : HTML::all_local_navigables()) {
auto document = navigable->active_document();
@@ -136,6 +182,7 @@ void PermissionStore::apply_snapshot(Web::Permissions::PermissionSnapshot snapsh
Web::Permissions::EffectiveState PermissionStore::state_for(Web::Permissions::PermissionName name, URL::Origin const& key, Page& page)
{
+ // NB: The caller holds the lock.
auto default_state = [&] {
switch (name) {
case Web::Permissions::PermissionName::Geolocation:
@@ -148,16 +195,23 @@ Web::Permissions::EffectiveState PermissionStore::state_for(Web::Permissions::Pe
VERIFY_NOT_REACHED();
};
- if (key.is_opaque())
+ if (key.is_opaque()) {
+ MutexLocker locker(m_mutex);
return default_state();
+ }
auto serialized_key = key.serialize();
- if (!m_sites.contains(serialized_key)) {
+ auto is_cached = [&] {
+ MutexLocker locker(m_mutex);
+ return m_sites.contains(serialized_key);
+ }();
+ if (!is_cached) {
// No status has seen this site's state yet, so there's nothing to tell them about.
if (auto snapshot = page.client().page_did_request_permission_snapshot(key); snapshot.has_value())
apply_snapshot(snapshot.release_value(), Notify::No);
}
+ MutexLocker locker(m_mutex);
if (auto site = m_sites.get(serialized_key); site.has_value()) {
for (auto const& entry : site->entries) {
if (entry.name == name)
@@ -169,6 +223,7 @@ Web::Permissions::EffectiveState PermissionStore::state_for(Web::Permissions::Pe
Vector<String> PermissionStore::cached_origins_for_testing() const
{
+ MutexLocker locker(m_mutex);
Vector<String> origins;
for (auto const& it : m_sites) {
if (!it.value.entries.is_empty())
@@ -179,7 +234,7 @@ Vector<String> PermissionStore::cached_origins_for_testing() const
void PermissionStore::register_status(PermissionStatus& status)
{
- m_statuses.append(status);
+ agent_permission_statuses().statuses.append(status);
}
}
diff --git a/Libraries/LibWeb/PermissionsAPI/PermissionStore.h b/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
index 4b0eba50c17..6733ed6bf78 100644
--- a/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
+++ b/Libraries/LibWeb/PermissionsAPI/PermissionStore.h
@@ -7,6 +7,7 @@
#pragma once
#include <AK/HashMap.h>
+#include <AK/Mutex.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/Vector.h>
@@ -29,6 +30,8 @@ struct PermissionStoreEntry {
};
// https://w3c.github.io/permissions/#dfn-permission-store
+// NB: The decisions are the process's and any thread may read them. The statuses that show them are cells, so each agent
+// keeps its own.
class WEB_API PermissionStore {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -48,6 +51,8 @@ public:
No,
Yes,
};
+ // With Notify::Yes this updates the statuses of the calling agent. Another agent gets the same snapshot from its own
+ // connection to the UI process, and the revision tells it that it has more to do than this store.
void apply_snapshot(Web::Permissions::PermissionSnapshot, Notify = Notify::Yes);
Web::Permissions::EffectiveState state_for(Web::Permissions::PermissionName, URL::Origin const& key, Page&);
@@ -57,11 +62,15 @@ public:
Vector<String> cached_origins_for_testing() const;
// For an agent that goes away, whose statuses have to go with its heap.
- void forget_statuses() { m_statuses.clear(); }
+ static void forget_statuses_of_this_agent();
private:
PermissionStore() = default;
+ bool store_snapshot(Web::Permissions::PermissionSnapshot const&);
+
+ mutable Mutex m_mutex;
+
Vector<PermissionStoreEntry> m_entries;
struct CachedSite {
@@ -73,7 +82,6 @@ private:
Web::Permissions::EffectiveState m_geolocation_default { Web::Permissions::EffectiveState::Prompt };
Web::Permissions::EffectiveState m_microphone_default { Web::Permissions::EffectiveState::Prompt };
Web::Permissions::EffectiveState m_notifications_default { Web::Permissions::EffectiveState::Prompt };
- Vector<GC::Weak<PermissionStatus>> m_statuses;
};
bool permission_key_is_equal_to(PermissionDescriptor const& descriptor, URL::Origin const& key1, URL::Origin const& key2);