diff options
author | Valtteri Koskivuori <vkoskiv@gmail.com> | 2022-04-02 00:14:04 +0300 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-04-03 13:13:10 +0100 |
commit | 45a81f5a2ce7ff8108ae8c29a3c5560ef5cbbbfe (patch) | |
tree | df01e2cf05fb4c0cbfc18b8e4fe0198a9bcd39c1 /Userland/Applications/Browser | |
parent | f2b4c044dbd73c42dd0b35c4400dda7681af478b (diff) | |
download | serenity-45a81f5a2ce7ff8108ae8c29a3c5560ef5cbbbfe.zip |
Browser+LibWeb+WebContent: Add ability to inspect local storage
The storage inspector now has a new tab for local storage. The next step
would be to persist local storage and receive real-time notifications
for changes to update the table view.
Diffstat (limited to 'Userland/Applications/Browser')
-rw-r--r-- | Userland/Applications/Browser/BrowserWindow.cpp | 4 | ||||
-rw-r--r-- | Userland/Applications/Browser/CMakeLists.txt | 1 | ||||
-rw-r--r-- | Userland/Applications/Browser/LocalStorageModel.cpp | 69 | ||||
-rw-r--r-- | Userland/Applications/Browser/LocalStorageModel.h | 33 | ||||
-rw-r--r-- | Userland/Applications/Browser/StorageWidget.cpp | 30 | ||||
-rw-r--r-- | Userland/Applications/Browser/StorageWidget.h | 9 | ||||
-rw-r--r-- | Userland/Applications/Browser/Tab.cpp | 6 | ||||
-rw-r--r-- | Userland/Applications/Browser/Tab.h | 1 |
8 files changed, 149 insertions, 4 deletions
diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index fbf17a707c..7996f09a5b 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -555,6 +555,10 @@ void BrowserWindow::create_new_tab(URL url, bool activate) return m_cookie_jar.get_all_cookies(); }; + new_tab.on_get_local_storage_entries = [this]() { + return active_tab().m_web_content_view->get_local_storage_entries(); + }; + new_tab.load(url); dbgln_if(SPAM_DEBUG, "Added new tab {:p}, loading {}", &new_tab, url); diff --git a/Userland/Applications/Browser/CMakeLists.txt b/Userland/Applications/Browser/CMakeLists.txt index 42a91864eb..ddb1d4ac3a 100644 --- a/Userland/Applications/Browser/CMakeLists.txt +++ b/Userland/Applications/Browser/CMakeLists.txt @@ -25,6 +25,7 @@ set(SOURCES History.cpp IconBag.cpp InspectorWidget.cpp + LocalStorageModel.cpp StorageWidget.cpp StorageWidgetGML.h Tab.cpp diff --git a/Userland/Applications/Browser/LocalStorageModel.cpp b/Userland/Applications/Browser/LocalStorageModel.cpp new file mode 100644 index 0000000000..f90da090c7 --- /dev/null +++ b/Userland/Applications/Browser/LocalStorageModel.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2022, Valtteri Koskivuori <vkoskiv@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include "LocalStorageModel.h" + +namespace Browser { + +void LocalStorageModel::set_items(OrderedHashMap<String, String> map) +{ + begin_insert_rows({}, m_local_storage_entries.size(), m_local_storage_entries.size()); + m_local_storage_entries = map; + end_insert_rows(); + + did_update(DontInvalidateIndices); +} + +void LocalStorageModel::clear_items() +{ + begin_insert_rows({}, m_local_storage_entries.size(), m_local_storage_entries.size()); + m_local_storage_entries.clear(); + end_insert_rows(); + + did_update(DontInvalidateIndices); +} + +String LocalStorageModel::column_name(int column) const +{ + switch (column) { + case Column::Key: + return "Key"; + case Column::Value: + return "Value"; + case Column::__Count: + return {}; + } + + return {}; +} + +GUI::ModelIndex LocalStorageModel::index(int row, int column, GUI::ModelIndex const&) const +{ + if (static_cast<size_t>(row) < m_local_storage_entries.size()) + return create_index(row, column, NULL); + return {}; +} + +GUI::Variant LocalStorageModel::data(GUI::ModelIndex const& index, GUI::ModelRole role) const +{ + if (role != GUI::ModelRole::Display) + return {}; + + auto const& keys = m_local_storage_entries.keys(); + auto const& local_storage_key = keys[index.row()]; + auto const& local_storage_value = m_local_storage_entries.get(local_storage_key).value_or({}); + + switch (index.column()) { + case Column::Key: + return local_storage_key; + case Column::Value: + return local_storage_value; + } + + VERIFY_NOT_REACHED(); +} + +} diff --git a/Userland/Applications/Browser/LocalStorageModel.h b/Userland/Applications/Browser/LocalStorageModel.h new file mode 100644 index 0000000000..b210c931bf --- /dev/null +++ b/Userland/Applications/Browser/LocalStorageModel.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022, Valtteri Koskivuori <vkoskiv@gmail.com> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <LibGUI/Model.h> + +namespace Browser { + +class LocalStorageModel final : public GUI::Model { +public: + enum Column { + Key, + Value, + __Count, + }; + + void set_items(OrderedHashMap<String, String> map); + void clear_items(); + virtual int row_count(GUI::ModelIndex const&) const override { return m_local_storage_entries.size(); } + virtual int column_count(GUI::ModelIndex const& = GUI::ModelIndex()) const override { return Column::__Count; } + virtual String column_name(int column) const override; + virtual GUI::ModelIndex index(int row, int column = 0, GUI::ModelIndex const& = GUI::ModelIndex()) const override; + virtual GUI::Variant data(GUI::ModelIndex const& index, GUI::ModelRole role = GUI::ModelRole::Display) const override; + +private: + OrderedHashMap<String, String> m_local_storage_entries; +}; + +} diff --git a/Userland/Applications/Browser/StorageWidget.cpp b/Userland/Applications/Browser/StorageWidget.cpp index 498db6c202..e02f61bd75 100644 --- a/Userland/Applications/Browser/StorageWidget.cpp +++ b/Userland/Applications/Browser/StorageWidget.cpp @@ -6,6 +6,7 @@ #include "StorageWidget.h" #include "CookiesModel.h" +#include "LocalStorageModel.h" #include <AK/Variant.h> #include <Applications/Browser/CookiesTabGML.h> #include <Applications/Browser/StorageWidgetGML.h> @@ -26,12 +27,25 @@ StorageWidget::StorageWidget() m_cookies_table_view = cookies_tab->find_descendant_of_type_named<GUI::TableView>("cookies_tableview"); m_cookies_model = adopt_ref(*new CookiesModel()); - m_sorting_model = MUST(GUI::SortingProxyModel::create(*m_cookies_model)); - m_sorting_model->set_sort_role(GUI::ModelRole::Display); + m_cookie_sorting_model = MUST(GUI::SortingProxyModel::create(*m_cookies_model)); + m_cookie_sorting_model->set_sort_role(GUI::ModelRole::Display); - m_cookies_table_view->set_model(m_sorting_model); + m_cookies_table_view->set_model(m_cookie_sorting_model); m_cookies_table_view->set_column_headers_visible(true); m_cookies_table_view->set_alternating_row_colors(true); + + auto local_storage_tab = tab_widget.try_add_tab<GUI::Widget>("Local Storage").release_value_but_fixme_should_propagate_errors(); + local_storage_tab->load_from_gml(cookies_tab_gml); + + m_local_storage_table_view = local_storage_tab->find_descendant_of_type_named<GUI::TableView>("cookies_tableview"); + m_local_storage_model = adopt_ref(*new LocalStorageModel()); + + m_local_storage_sorting_model = MUST(GUI::SortingProxyModel::create(*m_local_storage_model)); + m_local_storage_sorting_model->set_sort_role(GUI::ModelRole::Display); + + m_local_storage_table_view->set_model(m_local_storage_sorting_model); + m_local_storage_table_view->set_column_headers_visible(true); + m_local_storage_table_view->set_alternating_row_colors(true); } void StorageWidget::add_cookie(Web::Cookie::Cookie const& cookie) @@ -44,4 +58,14 @@ void StorageWidget::clear_cookies() m_cookies_model->clear_items(); } +void StorageWidget::set_local_storage_entries(OrderedHashMap<String, String> entries) +{ + m_local_storage_model->set_items(entries); +} + +void StorageWidget::clear_local_storage_entries() +{ + m_local_storage_model->clear_items(); +} + } diff --git a/Userland/Applications/Browser/StorageWidget.h b/Userland/Applications/Browser/StorageWidget.h index e1090f9ede..fef4860178 100644 --- a/Userland/Applications/Browser/StorageWidget.h +++ b/Userland/Applications/Browser/StorageWidget.h @@ -7,6 +7,7 @@ #pragma once #include "CookiesModel.h" +#include "LocalStorageModel.h" #include "Tab.h" #include <LibGUI/SortingProxyModel.h> #include <LibGUI/Widget.h> @@ -22,12 +23,18 @@ public: void add_cookie(Web::Cookie::Cookie const& cookie); void clear_cookies(); + void set_local_storage_entries(OrderedHashMap<String, String> entries); + void clear_local_storage_entries(); + private: StorageWidget(); RefPtr<GUI::TableView> m_cookies_table_view; RefPtr<CookiesModel> m_cookies_model; - RefPtr<GUI::SortingProxyModel> m_sorting_model; + RefPtr<GUI::SortingProxyModel> m_cookie_sorting_model; + RefPtr<GUI::TableView> m_local_storage_table_view; + RefPtr<LocalStorageModel> m_local_storage_model; + RefPtr<GUI::SortingProxyModel> m_local_storage_sorting_model; }; } diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 7fa98ec1fb..3e654ee149 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -613,6 +613,12 @@ void Tab::show_storage_inspector() m_storage_widget->add_cookie(cookie); } + if (on_get_local_storage_entries) { + auto local_storage_entries = on_get_local_storage_entries(); + m_storage_widget->clear_local_storage_entries(); + m_storage_widget->set_local_storage_entries(local_storage_entries); + } + auto* window = m_storage_widget->window(); window->show(); window->move_to_front(); diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 3b060ac586..67f85f0cb6 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -65,6 +65,7 @@ public: Function<void(const URL&, Web::Cookie::ParsedCookie const& cookie, Web::Cookie::Source source)> on_set_cookie; Function<void()> on_dump_cookies; Function<Vector<Web::Cookie::Cookie>()> on_want_cookies; + Function<OrderedHashMap<String, String>()> on_get_local_storage_entries; enum class InspectorTarget { Document, |