diff --git a/Ladybird/WebContent/main.cpp b/Ladybird/WebContent/main.cpp index cbe2a32c4d..7eb3cbb452 100644 --- a/Ladybird/WebContent/main.cpp +++ b/Ladybird/WebContent/main.cpp @@ -121,15 +121,24 @@ static ErrorOr load_content_filters() file_or_error = Core::File::open(DeprecatedString::formatted("{}/res/ladybird/BrowserContentFilters.txt", s_serenity_resource_root), Core::File::OpenMode::Read); if (file_or_error.is_error()) return file_or_error.release_error(); + auto file = file_or_error.release_value(); auto ad_filter_list = TRY(Core::BufferedFile::create(move(file))); auto buffer = TRY(ByteBuffer::create_uninitialized(4096)); + + Vector patterns; + while (TRY(ad_filter_list->can_read_line())) { auto line = TRY(ad_filter_list->read_line(buffer)); - if (!line.is_empty()) { - Web::ContentFilter::the().add_pattern(line); - } + if (line.is_empty()) + continue; + + TRY(patterns.try_append(line)); } + + auto& content_filter = Web::ContentFilter::the(); + TRY(content_filter.set_patterns(patterns)); + return {}; } diff --git a/Userland/Applications/Browser/main.cpp b/Userland/Applications/Browser/main.cpp index 0fefc3a807..fd037227ab 100644 --- a/Userland/Applications/Browser/main.cpp +++ b/Userland/Applications/Browser/main.cpp @@ -50,6 +50,9 @@ static ErrorOr load_content_filters() auto file = TRY(Core::File::open(DeprecatedString::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)); + + Browser::g_content_filters.clear_with_capacity(); + while (TRY(ad_filter_list->can_read_line())) { auto line = TRY(ad_filter_list->read_line(buffer)); if (!line.is_empty()) diff --git a/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp b/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp index 4caabbef1d..91fdad68bc 100644 --- a/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp +++ b/Userland/Libraries/LibWeb/Loader/ContentFilter.cpp @@ -33,15 +33,23 @@ bool ContentFilter::is_filtered(const AK::URL& url) const return false; } -void ContentFilter::add_pattern(DeprecatedString const& pattern) +ErrorOr ContentFilter::set_patterns(ReadonlySpan patterns) { - StringBuilder builder; - if (!pattern.starts_with('*')) - builder.append('*'); - builder.append(pattern); - if (!pattern.ends_with('*')) - builder.append('*'); - m_patterns.empend(builder.to_deprecated_string()); + m_patterns.clear_with_capacity(); + + for (auto const& pattern : patterns) { + StringBuilder builder; + + if (!pattern.starts_with('*')) + TRY(builder.try_append('*')); + TRY(builder.try_append(pattern)); + if (!pattern.ends_with('*')) + TRY(builder.try_append('*')); + + TRY(m_patterns.try_empend(builder.to_deprecated_string())); + } + + return {}; } } diff --git a/Userland/Libraries/LibWeb/Loader/ContentFilter.h b/Userland/Libraries/LibWeb/Loader/ContentFilter.h index 55ed5f3095..a76d612ea7 100644 --- a/Userland/Libraries/LibWeb/Loader/ContentFilter.h +++ b/Userland/Libraries/LibWeb/Loader/ContentFilter.h @@ -16,7 +16,7 @@ public: static ContentFilter& the(); bool is_filtered(const AK::URL&) const; - void add_pattern(DeprecatedString const&); + ErrorOr set_patterns(ReadonlySpan); private: ContentFilter(); diff --git a/Userland/Services/WebContent/ConnectionFromClient.cpp b/Userland/Services/WebContent/ConnectionFromClient.cpp index a24fff88af..741ac68c9a 100644 --- a/Userland/Services/WebContent/ConnectionFromClient.cpp +++ b/Userland/Services/WebContent/ConnectionFromClient.cpp @@ -636,8 +636,7 @@ Messages::WebContentServer::DumpLayoutTreeResponse ConnectionFromClient::dump_la void ConnectionFromClient::set_content_filters(Vector const& filters) { - for (auto& filter : filters) - Web::ContentFilter::the().add_pattern(filter); + Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors(); } void ConnectionFromClient::set_autoplay_allowed_on_all_websites()