ソースを参照

Browser+Ladybird+LibWeb: Port content filters to String

Timothy Flynn 2 年 前
コミット
5089766af6

+ 3 - 2
Ladybird/WebContent/main.cpp

@@ -126,14 +126,15 @@ static ErrorOr<void> load_content_filters()
     auto ad_filter_list = TRY(Core::BufferedFile::create(move(file)));
     auto buffer = TRY(ByteBuffer::create_uninitialized(4096));
 
-    Vector<DeprecatedString> patterns;
+    Vector<String> 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();

+ 1 - 1
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<DeprecatedString> g_content_filters;
+extern Vector<String> g_content_filters;
 extern bool g_content_filters_enabled;
 extern Vector<String> g_autoplay_allowlist;
 extern bool g_autoplay_allowed_on_all_websites;

+ 1 - 5
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);

+ 7 - 4
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<DeprecatedString> g_content_filters;
+Vector<String> g_content_filters;
 bool g_content_filters_enabled { true };
 Vector<String> g_autoplay_allowlist;
 bool g_autoplay_allowed_on_all_websites { false };
@@ -47,7 +47,7 @@ DeprecatedString g_webdriver_content_ipc_path;
 
 static ErrorOr<void> 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<void> 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 {};

+ 2 - 2
Userland/Libraries/LibWeb/Loader/ContentFilter.cpp

@@ -33,7 +33,7 @@ bool ContentFilter::is_filtered(const AK::URL& url) const
     return false;
 }
 
-ErrorOr<void> ContentFilter::set_patterns(ReadonlySpan<DeprecatedString> patterns)
+ErrorOr<void> ContentFilter::set_patterns(ReadonlySpan<String> patterns)
 {
     m_patterns.clear_with_capacity();
 
@@ -46,7 +46,7 @@ ErrorOr<void> ContentFilter::set_patterns(ReadonlySpan<DeprecatedString> 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 {};

+ 3 - 2
Userland/Libraries/LibWeb/Loader/ContentFilter.h

@@ -6,6 +6,7 @@
 
 #pragma once
 
+#include <AK/String.h>
 #include <AK/URL.h>
 #include <AK/Vector.h>
 
@@ -16,14 +17,14 @@ public:
     static ContentFilter& the();
 
     bool is_filtered(const AK::URL&) const;
-    ErrorOr<void> set_patterns(ReadonlySpan<DeprecatedString>);
+    ErrorOr<void> set_patterns(ReadonlySpan<String>);
 
 private:
     ContentFilter();
     ~ContentFilter();
 
     struct Pattern {
-        DeprecatedString text;
+        String text;
     };
     Vector<Pattern> m_patterns;
 };

+ 2 - 2
Userland/Libraries/LibWebView/OutOfProcessWebView.cpp

@@ -595,9 +595,9 @@ OrderedHashMap<DeprecatedString, DeprecatedString> OutOfProcessWebView::get_sess
     return client().get_session_storage_entries();
 }
 
-void OutOfProcessWebView::set_content_filters(Vector<DeprecatedString> filters)
+void OutOfProcessWebView::set_content_filters(Vector<String> filters)
 {
-    client().async_set_content_filters(filters);
+    client().async_set_content_filters(move(filters));
 }
 
 void OutOfProcessWebView::set_autoplay_allowed_on_all_websites()

+ 1 - 1
Userland/Libraries/LibWebView/OutOfProcessWebView.h

@@ -42,7 +42,7 @@ public:
     OrderedHashMap<DeprecatedString, DeprecatedString> get_local_storage_entries();
     OrderedHashMap<DeprecatedString, DeprecatedString> get_session_storage_entries();
 
-    void set_content_filters(Vector<DeprecatedString>);
+    void set_content_filters(Vector<String>);
     void set_autoplay_allowed_on_all_websites();
     void set_autoplay_allowlist(Vector<String>);
     void set_proxy_mappings(Vector<DeprecatedString> proxies, HashMap<DeprecatedString, size_t> mappings);

+ 1 - 1
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<DeprecatedString> const& filters)
+void ConnectionFromClient::set_content_filters(Vector<String> const& filters)
 {
     Web::ContentFilter::the().set_patterns(filters).release_value_but_fixme_should_propagate_errors();
 }

+ 1 - 1
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<DeprecatedString> const&) override;
+    virtual void set_content_filters(Vector<String> const&) override;
     virtual void set_autoplay_allowed_on_all_websites() override;
     virtual void set_autoplay_allowlist(Vector<String> const& allowlist) override;
     virtual void set_proxy_mappings(Vector<DeprecatedString> const&, HashMap<DeprecatedString, size_t> const&) override;

+ 1 - 1
Userland/Services/WebContent/WebContentServer.ipc

@@ -54,7 +54,7 @@ endpoint WebContentServer
     get_selected_text() => (DeprecatedString selection)
     select_all() =|
 
-    set_content_filters(Vector<DeprecatedString> filters) =|
+    set_content_filters(Vector<String> filters) =|
     set_autoplay_allowed_on_all_websites() =|
     set_autoplay_allowlist(Vector<String> allowlist) =|
     set_proxy_mappings(Vector<DeprecatedString> proxies, HashMap<DeprecatedString,size_t> mappings) =|