Skip to content

Commit 48aa16d

Browse files
committed
LibWebView: Add methods to remove cookies / WebStorage items
This allows removing cookies and local/session storage items last accessed since a provided timestamp.
1 parent c2c2698 commit 48aa16d

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

Libraries/LibWebView/CookieJar.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,11 @@ void CookieJar::expire_cookies_with_time_offset(AK::Duration offset)
233233
m_transient_storage.purge_expired_cookies(offset);
234234
}
235235

236+
void CookieJar::expire_cookies_accessed_since(UnixDateTime since)
237+
{
238+
m_transient_storage.expire_and_purge_cookies_accessed_since(since);
239+
}
240+
236241
Requests::CacheSizes CookieJar::estimate_storage_size_accessed_since(UnixDateTime since) const
237242
{
238243
return m_transient_storage.estimate_storage_size_accessed_since(since);
@@ -652,6 +657,18 @@ void CookieJar::TransientStorage::expire_and_purge_all_cookies()
652657
purge_expired_cookies();
653658
}
654659

660+
void CookieJar::TransientStorage::expire_and_purge_cookies_accessed_since(UnixDateTime since)
661+
{
662+
for (auto& [key, value] : m_cookies) {
663+
if (value.last_access_time >= since) {
664+
value.expiry_time = UnixDateTime::earliest();
665+
set_cookie(key, value);
666+
}
667+
}
668+
669+
purge_expired_cookies();
670+
}
671+
655672
Requests::CacheSizes CookieJar::TransientStorage::estimate_storage_size_accessed_since(UnixDateTime since) const
656673
{
657674
Requests::CacheSizes sizes;

Libraries/LibWebView/CookieJar.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class WEBVIEW_API CookieJar {
4747
Vector<Web::Cookie::Cookie> get_all_cookies_cookiestore(URL::URL const& url);
4848
Optional<Web::Cookie::Cookie> get_named_cookie(URL::URL const& url, StringView name);
4949
void expire_cookies_with_time_offset(AK::Duration);
50+
void expire_cookies_accessed_since(UnixDateTime since);
5051
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
5152

5253
private:
@@ -68,6 +69,7 @@ class WEBVIEW_API CookieJar {
6869

6970
UnixDateTime purge_expired_cookies(Optional<AK::Duration> offset = {});
7071
void expire_and_purge_all_cookies();
72+
void expire_and_purge_cookies_accessed_since(UnixDateTime since);
7173

7274
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
7375

Libraries/LibWebView/StorageJar.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ErrorOr<NonnullOwnPtr<StorageJar>> StorageJar::create(Database::Database& databa
5757
statements.get_item = TRY(database.prepare_statement("SELECT bottle_value FROM WebStorage WHERE storage_endpoint = ? AND storage_key = ? AND bottle_key = ?;"sv));
5858
statements.set_item = TRY(database.prepare_statement("INSERT OR REPLACE INTO WebStorage VALUES (?, ?, ?, ?, ?);"sv));
5959
statements.delete_item = TRY(database.prepare_statement("DELETE FROM WebStorage WHERE storage_endpoint = ? AND storage_key = ? AND bottle_key = ?;"sv));
60+
statements.delete_items_accessed_since = TRY(database.prepare_statement("DELETE FROM WebStorage WHERE last_access_time >= ?;"sv));
6061
statements.update_last_access_time = TRY(database.prepare_statement("UPDATE WebStorage SET last_access_time = ? WHERE storage_endpoint = ? AND storage_key = ? AND bottle_key = ?;"sv));
6162
statements.clear = TRY(database.prepare_statement("DELETE FROM WebStorage WHERE storage_endpoint = ? AND storage_key = ?;"sv));
6263
statements.get_keys = TRY(database.prepare_statement("SELECT bottle_key FROM WebStorage WHERE storage_endpoint = ? AND storage_key = ?;"sv));
@@ -125,6 +126,14 @@ void StorageJar::remove_item(StorageEndpointType storage_endpoint, String const&
125126
m_transient_storage.delete_item(storage_location);
126127
}
127128

129+
void StorageJar::remove_items_accessed_since(UnixDateTime since)
130+
{
131+
if (m_persisted_storage.has_value())
132+
m_persisted_storage->delete_items_accessed_since(since);
133+
else
134+
m_transient_storage.delete_items_accessed_since(since);
135+
}
136+
128137
void StorageJar::clear_storage_key(StorageEndpointType storage_endpoint, String const& storage_key)
129138
{
130139
if (m_persisted_storage.has_value())
@@ -181,6 +190,13 @@ void StorageJar::TransientStorage::delete_item(StorageLocation const& key)
181190
m_storage_items.remove(key);
182191
}
183192

193+
void StorageJar::TransientStorage::delete_items_accessed_since(UnixDateTime since)
194+
{
195+
m_storage_items.remove_all_matching([&](auto const&, auto const& entry) {
196+
return entry.last_access_time >= since;
197+
});
198+
}
199+
184200
void StorageJar::TransientStorage::clear(StorageEndpointType storage_endpoint, String const& storage_key)
185201
{
186202
Vector<StorageLocation> keys_to_remove;
@@ -284,6 +300,11 @@ void StorageJar::PersistedStorage::delete_item(StorageLocation const& key)
284300
key.bottle_key);
285301
}
286302

303+
void StorageJar::PersistedStorage::delete_items_accessed_since(UnixDateTime since)
304+
{
305+
database.execute_statement(statements.delete_items_accessed_since, {}, since);
306+
}
307+
287308
void StorageJar::PersistedStorage::clear(StorageEndpointType storage_endpoint, String const& storage_key)
288309
{
289310
database.execute_statement(

Libraries/LibWebView/StorageJar.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class WEBVIEW_API StorageJar {
4141
Optional<String> get_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& bottle_key);
4242
StorageOperationError set_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& bottle_key, String const& bottle_value);
4343
void remove_item(StorageEndpointType storage_endpoint, String const& storage_key, String const& key);
44+
void remove_items_accessed_since(UnixDateTime);
4445
void clear_storage_key(StorageEndpointType storage_endpoint, String const& storage_key);
4546
Vector<String> get_all_keys(StorageEndpointType storage_endpoint, String const& storage_key);
4647
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
@@ -50,6 +51,7 @@ class WEBVIEW_API StorageJar {
5051
Database::StatementID get_item { 0 };
5152
Database::StatementID set_item { 0 };
5253
Database::StatementID delete_item { 0 };
54+
Database::StatementID delete_items_accessed_since { 0 };
5355
Database::StatementID update_last_access_time { 0 };
5456
Database::StatementID clear { 0 };
5557
Database::StatementID get_keys { 0 };
@@ -62,6 +64,7 @@ class WEBVIEW_API StorageJar {
6264
Optional<String> get_item(StorageLocation const& key);
6365
StorageOperationError set_item(StorageLocation const& key, String const& value);
6466
void delete_item(StorageLocation const& key);
67+
void delete_items_accessed_since(UnixDateTime);
6568
void clear(StorageEndpointType storage_endpoint, String const& storage_key);
6669
Vector<String> get_keys(StorageEndpointType storage_endpoint, String const& storage_key);
6770
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;
@@ -79,6 +82,7 @@ class WEBVIEW_API StorageJar {
7982
Optional<String> get_item(StorageLocation const& key);
8083
StorageOperationError set_item(StorageLocation const& key, String const& value);
8184
void delete_item(StorageLocation const& key);
85+
void delete_items_accessed_since(UnixDateTime);
8286
void clear(StorageEndpointType storage_endpoint, String const& storage_key);
8387
Vector<String> get_keys(StorageEndpointType storage_endpoint, String const& storage_key);
8488
Requests::CacheSizes estimate_storage_size_accessed_since(UnixDateTime since) const;

0 commit comments

Comments
 (0)