From 5d8cda59aece84f85d12ef282c9142c2284a6c4c Mon Sep 17 00:00:00 2001 From: Maciej Date: Mon, 31 Jan 2022 20:43:18 +0100 Subject: [PATCH] Browser: Update content filters if the config file changes --- Userland/Applications/Browser/Tab.cpp | 5 +++++ Userland/Applications/Browser/Tab.h | 1 + Userland/Applications/Browser/main.cpp | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 06ec6ab64f6..b31120fec61 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -442,6 +442,11 @@ void Tab::context_menu_requested(const Gfx::IntPoint& screen_position) m_tab_context_menu->popup(screen_position); } +void Tab::content_filters_changed() +{ + m_web_content_view->set_content_filters(g_content_filters); +} + GUI::AbstractScrollableWidget& Tab::view() { return *m_web_content_view; diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 0c47673cb60..dec2551ff4f 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -49,6 +49,7 @@ public: void did_become_active(); void context_menu_requested(const Gfx::IntPoint& screen_position); + void content_filters_changed(); void action_entered(GUI::Action&); void action_left(GUI::Action&); diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index a5f060463ec..4380261e95e 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -4,6 +4,8 @@ * SPDX-License-Identifier: BSD-2-Clause */ +#include "AK/IterationDecision.h" +#include "LibCore/FileWatcher.h" #include #include #include @@ -102,6 +104,21 @@ ErrorOr serenity_main(Main::Arguments arguments) Browser::CookieJar cookie_jar; auto window = Browser::BrowserWindow::construct(cookie_jar, first_url); + auto content_filters_watcher = TRY(Core::FileWatcher::create()); + content_filters_watcher->on_change = [&](Core::FileWatcherEvent const&) { + dbgln("Reloading content filters because config file changed"); + auto error = load_content_filters(); + if (error.is_error()) { + dbgln("Reloading content filters failed: {}", error.release_error()); + return; + } + window->tab_widget().for_each_child_of_type([](auto& tab) { + tab.content_filters_changed(); + return IterationDecision::Continue; + }); + }; + TRY(content_filters_watcher->add_watch(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::FileWatcherEvent::Type::ContentModified)); + app->on_action_enter = [&](GUI::Action& action) { if (auto* browser_window = dynamic_cast(app->active_window())) { auto* tab = static_cast(browser_window->tab_widget().active_widget());