From 5089766af68f6aa9c39d829bafb5ffde6b1e76ca Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 21 Apr 2023 07:54:56 -0400 Subject: [PATCH] Browser+Ladybird+LibWeb: Port content filters to String --- Ladybird/WebContent/main.cpp | 5 +++-- Userland/Applications/Browser/Browser.h | 2 +- Userland/Applications/Browser/Tab.cpp | 6 +----- Userland/Applications/Browser/main.cpp | 11 +++++++---- Userland/Libraries/LibWeb/Loader/ContentFilter.cpp | 4 ++-- Userland/Libraries/LibWeb/Loader/ContentFilter.h | 5 +++-- Userland/Libraries/LibWebView/OutOfProcessWebView.cpp | 4 ++-- Userland/Libraries/LibWebView/OutOfProcessWebView.h | 2 +- Userland/Services/WebContent/ConnectionFromClient.cpp | 2 +- Userland/Services/WebContent/ConnectionFromClient.h | 2 +- Userland/Services/WebContent/WebContentServer.ipc | 2 +- 11 files changed, 23 insertions(+), 22 deletions(-) diff --git a/Ladybird/WebContent/main.cpp b/Ladybird/WebContent/main.cpp index 7eb3cbb452..ae8c424797 100644 --- a/Ladybird/WebContent/main.cpp +++ b/Ladybird/WebContent/main.cpp @@ -126,14 +126,15 @@ static ErrorOr load_content_filters() auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); - Vector patterns; + Vector patterns; while (TRY(ad_filter_list->can_read_line())) { auto line = TRY(ad_filter_list->read_line(buffer)); if (line.is_empty()) continue; - TRY(patterns.try_append(line)); + auto pattern = TRY(String::from_utf8(line)); + TRY(patterns.try_append(move(pattern))); } auto& content_filter = Web::ContentFilter::the(); diff --git a/Userland/Applications/Browser/Browser.h b/Userland/Applications/Browser/Browser.h index b95273a1a7..5e94030131 100644 --- a/Userland/Applications/Browser/Browser.h +++ b/Userland/Applications/Browser/Browser.h @@ -15,7 +15,7 @@ namespace Browser { extern DeprecatedString g_home_url; extern DeprecatedString g_new_tab_url; extern DeprecatedString g_search_engine; -extern Vector g_content_filters; +extern Vector g_content_filters; extern bool g_content_filters_enabled; extern Vector g_autoplay_allowlist; extern bool g_autoplay_allowed_on_all_websites; diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 1469ea2500..45645c4cb3 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -126,11 +126,7 @@ Tab::Tab(BrowserWindow& window) auto preferred_color_scheme = Web::CSS::preferred_color_scheme_from_string(Config::read_string("Browser"sv, "Preferences"sv, "ColorScheme"sv, "auto"sv)); m_web_content_view->set_preferred_color_scheme(preferred_color_scheme); - if (g_content_filters_enabled) - m_web_content_view->set_content_filters(g_content_filters); - else - m_web_content_view->set_content_filters({}); - + content_filters_changed(); autoplay_allowlist_changed(); m_web_content_view->set_proxy_mappings(g_proxies, g_proxy_mappings); diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index fd037227ab..8744fc7fab 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -34,7 +34,7 @@ namespace Browser { DeprecatedString g_search_engine; DeprecatedString g_home_url; DeprecatedString g_new_tab_url; -Vector g_content_filters; +Vector g_content_filters; bool g_content_filters_enabled { true }; Vector g_autoplay_allowlist; bool g_autoplay_allowed_on_all_websites { false }; @@ -47,7 +47,7 @@ DeprecatedString g_webdriver_content_ipc_path; static ErrorOr load_content_filters() { - auto file = TRY(Core::File::open(DeprecatedString::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory()), Core::File::OpenMode::Read)); + auto file = TRY(Core::File::open(TRY(String::formatted("{}/BrowserContentFilters.txt", Core::StandardPaths::config_directory())), Core::File::OpenMode::Read)); auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); @@ -55,8 +55,11 @@ static ErrorOr load_content_filters() while (TRY(ad_filter_list->can_read_line())) { auto line = TRY(ad_filter_list->read_line(buffer)); - if (!line.is_empty()) - Browser::g_content_filters.append(line); + if (line.is_empty()) + continue; + + auto pattern = TRY(String::from_utf8(line)); + TRY(Browser::g_content_filters.try_append(move(pattern))); } return {}; diff --git a/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp b/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp index 91fdad68bc..bf96499b5f 100644 --- a/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp +++ b/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp @@ -33,7 +33,7 @@ bool ContentFilter::is_filtered(const AK::URL& url) const return false; } -ErrorOr ContentFilter::set_patterns(ReadonlySpan patterns) +ErrorOr ContentFilter::set_patterns(ReadonlySpan patterns) { m_patterns.clear_with_capacity(); @@ -46,7 +46,7 @@ ErrorOr ContentFilter::set_patterns(ReadonlySpan pattern if (!pattern.ends_with('*')) TRY(builder.try_append('*')); - TRY(m_patterns.try_empend(builder.to_deprecated_string())); + TRY(m_patterns.try_empend(TRY(builder.to_string()))); } return {}; diff --git a/Userland/Libraries/LibWeb/Loader/ContentFilter.h b/Userland/Libraries/LibWeb/Loader/ContentFilter.h index a76d612ea7..bb085ebebd 100644 --- a/Userland/Libraries/LibWeb/Loader/ContentFilter.h +++ b/Userland/Libraries/LibWeb/Loader/ContentFilter.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include @@ -16,14 +17,14 @@ public: static ContentFilter& the(); bool is_filtered(const AK::URL&) const; - ErrorOr set_patterns(ReadonlySpan); + ErrorOr set_patterns(ReadonlySpan); private: ContentFilter(); ~ContentFilter(); struct Pattern { - DeprecatedString text; + String text; }; Vector m_patterns; }; diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp index 81ec7af17a..b976135a50 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.cpp @@ -595,9 +595,9 @@ OrderedHashMap OutOfProcessWebView::get_sess return client().get_session_storage_entries(); } -void OutOfProcessWebView::set_content_filters(Vector filters) +void OutOfProcessWebView::set_content_filters(Vector filters) { - client().async_set_content_filters(filters); + client().async_set_content_filters(move(filters)); } void OutOfProcessWebView::set_autoplay_allowed_on_all_websites() diff --git a/Userland/Libraries/LibWebView/OutOfProcessWebView.h b/Userland/Libraries/LibWebView/OutOfProcessWebView.h index 4744efc0cb..1f3dd23676 100644 --- a/Userland/Libraries/LibWebView/OutOfProcessWebView.h +++ b/Userland/Libraries/LibWebView/OutOfProcessWebView.h @@ -42,7 +42,7 @@ public: OrderedHashMap get_local_storage_entries(); OrderedHashMap get_session_storage_entries(); - void set_content_filters(Vector); + void set_content_filters(Vector); void set_autoplay_allowed_on_all_websites(); void set_autoplay_allowlist(Vector); void set_proxy_mappings(Vector proxies, HashMap mappings); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index 741ac68c9a..1c3d7fd21c 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -634,7 +634,7 @@ Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_la return builder.to_deprecated_string(); } -void ConnectionFromClient::set_content_filters(Vector const& filters) +void ConnectionFromClient::set_content_filters(Vector const& filters) { Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors(); } diff --git a/Userland/Services/WebContent/ConnectionFromClient.h b/Userland/Services/WebContent/ConnectionFromClient.h index 61353c3ef5..0cf634560a 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.h +++ b/Userland/Services/WebContent/ConnectionFromClient.h @@ -74,7 +74,7 @@ private: virtual void inspect_accessibility_tree() override; virtual Messages::WebContentServer::GetHoveredNodeIdResponse get_hovered_node_id() override; virtual Messages::WebContentServer::DumpLayoutTreeResponse dump_layout_tree() override; - virtual void set_content_filters(Vector const&) override; + virtual void set_content_filters(Vector const&) override; virtual void set_autoplay_allowed_on_all_websites() override; virtual void set_autoplay_allowlist(Vector const& allowlist) override; virtual void set_proxy_mappings(Vector const&, HashMap const&) override; diff --git a/Userland/Services/WebContent/WebContentServer.ipc b/Userland/Services/WebContent/WebContentServer.ipc index 31db29d292..85331602d5 100644 --- a/Userland/Services/WebContent/WebContentServer.ipc +++ b/Userland/Services/WebContent/WebContentServer.ipc @@ -54,7 +54,7 @@ endpoint WebContentServer get_selected_text() => (DeprecatedString selection) select_all() =| - set_content_filters(Vector filters) =| + set_content_filters(Vector filters) =| set_autoplay_allowed_on_all_websites() =| set_autoplay_allowlist(Vector allowlist) =| set_proxy_mappings(Vector proxies, HashMap mappings) =|