From 4d9c0c7b223a6398f8d06ddfb08b574a86d5b8d6 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 20 Oct 2022 16:38:20 +0100 Subject: [PATCH] Browser: Add ability to delete cookies from Storage Inspector Adds actions to delete either a single cookie, or all of them. This looks weird, because the GUI doesn't update until you "Inspect > Open Storage Inspector", but it does function. :^) --- .../Applications/Browser/CookiesModel.cpp | 6 +++ Userland/Applications/Browser/CookiesModel.h | 2 + .../Applications/Browser/StorageWidget.cpp | 37 ++++++++++++++++++- Userland/Applications/Browser/StorageWidget.h | 5 +++ Userland/Applications/Browser/Tab.cpp | 4 ++ 5 files changed, 53 insertions(+), 1 deletion(-) diff --git a/Userland/Applications/Browser/CookiesModel.cpp b/Userland/Applications/Browser/CookiesModel.cpp index facae3722b..bea2296509 100644 --- a/Userland/Applications/Browser/CookiesModel.cpp +++ b/Userland/Applications/Browser/CookiesModel.cpp @@ -97,4 +97,10 @@ TriState CookiesModel::data_matches(GUI::ModelIndex const& index, GUI::Variant c return TriState::False; } +Web::Cookie::Cookie const& CookiesModel::get_cookie(GUI::ModelIndex const& index) const +{ + VERIFY(index.is_valid()); + return m_cookies[index.row()]; +} + } diff --git a/Userland/Applications/Browser/CookiesModel.h b/Userland/Applications/Browser/CookiesModel.h index 280a189c14..4c378115a2 100644 --- a/Userland/Applications/Browser/CookiesModel.h +++ b/Userland/Applications/Browser/CookiesModel.h @@ -34,6 +34,8 @@ public: virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override; virtual TriState data_matches(GUI::ModelIndex const& index, GUI::Variant const& term) const override; + Web::Cookie::Cookie const& get_cookie(GUI::ModelIndex const&) const; + private: AK::Vector m_cookies; }; diff --git a/Userland/Applications/Browser/StorageWidget.cpp b/Userland/Applications/Browser/StorageWidget.cpp index bc45b55b74..00cad32b82 100644 --- a/Userland/Applications/Browser/StorageWidget.cpp +++ b/Userland/Applications/Browser/StorageWidget.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, the SerenityOS developers. + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,8 +8,8 @@ #include "StorageWidget.h" #include "CookiesModel.h" #include "StorageModel.h" -#include #include +#include #include #include #include @@ -37,6 +38,32 @@ StorageWidget::StorageWidget() m_cookies_table_view->set_column_headers_visible(true); m_cookies_table_view->set_alternating_row_colors(true); + auto delete_cookie_action = GUI::Action::create( + "&Delete Cookie", { Key_Delete }, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/delete.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto const&) { + auto cookie_index = m_cookies_table_view->selection().first(); + delete_cookie(m_cookies_model->get_cookie(cookie_index)); + }, + m_cookies_table_view); + + auto delete_all_cookies_action = GUI::Action::create( + "Delete &All Cookies", [&](auto const&) { + auto cookie_count = m_cookies_model->row_count({}); + for (auto i = 0; i < cookie_count; ++i) { + auto cookie_index = m_cookies_model->index(i); + if (cookie_index.is_valid()) + delete_cookie(m_cookies_model->get_cookie(cookie_index)); + } + }, + m_cookies_table_view); + + m_cookies_context_menu = GUI::Menu::construct(); + m_cookies_context_menu->add_action(delete_cookie_action); + m_cookies_context_menu->add_action(delete_all_cookies_action); + m_cookies_table_view->on_context_menu_request = [&](auto& index, auto& event) { + if (index.is_valid()) + m_cookies_context_menu->popup(event.screen_position()); + }; + m_local_storage_table_view = tab_widget.find_descendant_of_type_named("local_storage_tableview"); m_local_storage_textbox = tab_widget.find_descendant_of_type_named("local_storage_filter_textbox"); m_local_storage_model = adopt_ref(*new StorageModel()); @@ -102,4 +129,12 @@ void StorageWidget::clear_session_storage_entries() m_session_storage_model->clear_items(); } +void StorageWidget::delete_cookie(Web::Cookie::Cookie cookie) +{ + // Delete cookie by making its expiry time in the past. + cookie.expiry_time = Core::DateTime::from_timestamp(0); + if (on_update_cookie) + on_update_cookie(move(cookie)); +} + } diff --git a/Userland/Applications/Browser/StorageWidget.h b/Userland/Applications/Browser/StorageWidget.h index cfde2c7295..11160fb822 100644 --- a/Userland/Applications/Browser/StorageWidget.h +++ b/Userland/Applications/Browser/StorageWidget.h @@ -24,6 +24,8 @@ public: void set_cookies_entries(Vector entries); void clear_cookies(); + Function on_update_cookie; + void set_local_storage_entries(OrderedHashMap entries); void clear_local_storage_entries(); @@ -33,10 +35,13 @@ public: private: StorageWidget(); + void delete_cookie(Web::Cookie::Cookie); + RefPtr m_cookies_table_view; RefPtr m_cookies_textbox; RefPtr m_cookies_model; RefPtr m_cookies_filtering_model; + RefPtr m_cookies_context_menu; RefPtr m_local_storage_table_view; RefPtr m_local_storage_textbox; diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 1306e1f0df..b149d5fa47 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -614,6 +614,10 @@ void Tab::show_storage_inspector() storage_window->set_title("Storage inspector"); storage_window->set_icon(g_icon_bag.cookie); m_storage_widget = storage_window->set_main_widget(); + m_storage_widget->on_update_cookie = [this](Web::Cookie::Cookie cookie) { + if (on_update_cookie) + on_update_cookie(url(), move(cookie)); + }; } if (on_get_cookies_entries) {