From a8f0fa5dd4432b49c262c6503b390979dee00d29 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Sat, 14 Oct 2023 09:07:38 -0400 Subject: [PATCH] LibWebView+LibPublicSuffix: Merge LibPublicSuffix into LibWebView After d2c7e1ea7db060c1f01a740fab65f3e35a45545b, there is now only one user of LibPublicSuffix - the URL sanitation utility within LibWebView. Rather than having an entire library for the small Public Suffix data accessor, merge it into LibWebView. --- Meta/Lagom/CMakeLists.txt | 1 - .../Lagom/Tools/CodeGenerators/CMakeLists.txt | 2 +- .../LibPublicSuffix/CMakeLists.txt | 1 - .../CodeGenerators/LibWebView/CMakeLists.txt | 1 + .../GeneratePublicSuffixData.cpp | 14 +++--- Userland/Libraries/CMakeLists.txt | 1 - .../Libraries/LibPublicSuffix/CMakeLists.txt | 10 ---- Userland/Libraries/LibPublicSuffix/URL.cpp | 47 ------------------- Userland/Libraries/LibPublicSuffix/URL.h | 15 ------ Userland/Libraries/LibWebView/CMakeLists.txt | 8 +++- Userland/Libraries/LibWebView/URL.cpp | 44 +++++++++++++++-- 11 files changed, 57 insertions(+), 87 deletions(-) delete mode 100644 Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/CMakeLists.txt create mode 100644 Meta/Lagom/Tools/CodeGenerators/LibWebView/CMakeLists.txt rename Meta/Lagom/Tools/CodeGenerators/{LibPublicSuffix => LibWebView}/GeneratePublicSuffixData.cpp (96%) delete mode 100644 Userland/Libraries/LibPublicSuffix/CMakeLists.txt delete mode 100644 Userland/Libraries/LibPublicSuffix/URL.cpp delete mode 100644 Userland/Libraries/LibPublicSuffix/URL.h diff --git a/Meta/Lagom/CMakeLists.txt b/Meta/Lagom/CMakeLists.txt index b4990c22ea2..20c99b2ef71 100644 --- a/Meta/Lagom/CMakeLists.txt +++ b/Meta/Lagom/CMakeLists.txt @@ -427,7 +427,6 @@ if (BUILD_LAGOM) Markdown PDF Protocol - PublicSuffix Regex SoftGPU SQL diff --git a/Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt index f47a5c1612f..75af3133329 100644 --- a/Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt +++ b/Meta/Lagom/Tools/CodeGenerators/CMakeLists.txt @@ -6,8 +6,8 @@ endif() add_subdirectory(LibEDID) add_subdirectory(LibGL) add_subdirectory(LibLocale) -add_subdirectory(LibPublicSuffix) add_subdirectory(LibTimeZone) add_subdirectory(LibUnicode) add_subdirectory(LibWeb) +add_subdirectory(LibWebView) add_subdirectory(StateMachineGenerator) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/CMakeLists.txt deleted file mode 100644 index 93caf7a0a4b..00000000000 --- a/Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -lagom_tool(GeneratePublicSuffixData SOURCES GeneratePublicSuffixData.cpp LIBS LibMain) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibWebView/CMakeLists.txt b/Meta/Lagom/Tools/CodeGenerators/LibWebView/CMakeLists.txt new file mode 100644 index 00000000000..35dec51cc60 --- /dev/null +++ b/Meta/Lagom/Tools/CodeGenerators/LibWebView/CMakeLists.txt @@ -0,0 +1 @@ +lagom_tool(GeneratePublicSuffixData SOURCES GeneratePublicSuffixData.cpp LIBS LibMain) diff --git a/Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/GeneratePublicSuffixData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibWebView/GeneratePublicSuffixData.cpp similarity index 96% rename from Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/GeneratePublicSuffixData.cpp rename to Meta/Lagom/Tools/CodeGenerators/LibWebView/GeneratePublicSuffixData.cpp index 1177bf8b2bd..b7e69a86268 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibPublicSuffix/GeneratePublicSuffixData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibWebView/GeneratePublicSuffixData.cpp @@ -48,7 +48,7 @@ ErrorOr generate_header_file(Core::InputBufferedFile&, Core::File& file) #include #include -namespace PublicSuffix { +namespace WebView { class PublicSuffixData { protected: @@ -72,7 +72,7 @@ private: Trie m_dictionary; }; -} // namespace PublicSuffix +} )~~~"); @@ -85,11 +85,11 @@ ErrorOr generate_implementation_file(Core::InputBufferedFile& input, Core: StringBuilder builder; SourceGenerator generator { builder }; generator.append(R"~~~( -#include -#include #include +#include +#include -namespace PublicSuffix { +namespace WebView { static Vector s_public_suffixes {)~~~"); @@ -121,7 +121,7 @@ PublicSuffixData::PublicSuffixData() { // FIXME: Reduce the depth of this trie for (auto str : s_public_suffixes) { - MUST(m_dictionary.insert(str.begin(), str.end(), str, [](auto& parent, auto& it) -> Optional { + MUST(m_dictionary.insert(str.begin(), str.end(), str, [](auto& parent, auto& it) -> Optional { return DeprecatedString::formatted("{}{}", parent.metadata_value(), *it); })); } @@ -176,7 +176,7 @@ ErrorOr> PublicSuffixData::get_public_suffix(StringView string) return Optional {}; } -} // namespace PublicSuffix +} )~~~"); diff --git a/Userland/Libraries/CMakeLists.txt b/Userland/Libraries/CMakeLists.txt index b495263713c..83c0cec63e1 100644 --- a/Userland/Libraries/CMakeLists.txt +++ b/Userland/Libraries/CMakeLists.txt @@ -44,7 +44,6 @@ add_subdirectory(LibPartition) add_subdirectory(LibPCIDB) add_subdirectory(LibPDF) add_subdirectory(LibProtocol) -add_subdirectory(LibPublicSuffix) add_subdirectory(LibRegex) add_subdirectory(LibSanitizer) add_subdirectory(LibSoftGPU) diff --git a/Userland/Libraries/LibPublicSuffix/CMakeLists.txt b/Userland/Libraries/LibPublicSuffix/CMakeLists.txt deleted file mode 100644 index 71485ffc134..00000000000 --- a/Userland/Libraries/LibPublicSuffix/CMakeLists.txt +++ /dev/null @@ -1,10 +0,0 @@ -include(${SerenityOS_SOURCE_DIR}/Meta/CMake/public_suffix.cmake) - -set(SOURCES - URL.cpp - ${PUBLIC_SUFFIX_SOURCES} -) -set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED}) - -serenity_lib(LibPublicSuffix publicsuffix) -target_compile_definitions(LibPublicSuffix PRIVATE ENABLE_PUBLIC_SUFFIX_DOWNLOAD=$) diff --git a/Userland/Libraries/LibPublicSuffix/URL.cpp b/Userland/Libraries/LibPublicSuffix/URL.cpp deleted file mode 100644 index 5757facf4e7..00000000000 --- a/Userland/Libraries/LibPublicSuffix/URL.cpp +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (c) 2023, Cameron Youell - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include -#include -#include -#if defined(ENABLE_PUBLIC_SUFFIX_DOWNLOAD) -# include -#endif - -namespace PublicSuffix { -ErrorOr absolute_url(StringView url) -{ - String out = TRY(String::from_utf8(url)); -#if !defined(ENABLE_PUBLIC_SUFFIX_DOWNLOAD) - return out; -#else - if (!out.contains("://"sv)) - out = TRY(String::formatted("https://{}"sv, out)); - - auto final_url = URL::create_with_url_or_path(out.to_deprecated_string()); - if (!final_url.is_valid()) - return Error::from_string_view("Invalid URL"sv); - - if (final_url.host().has() || final_url.host().has()) - return out; - - if (final_url.scheme() != "http"sv && final_url.scheme() != "https"sv) - return out; - - if (final_url.host().has()) { - auto string_host = final_url.host().get(); - auto maybe_public_suffix = TRY(PublicSuffixData::the()->get_public_suffix(string_host)); - if (maybe_public_suffix.has_value()) - return out; - - if (string_host.ends_with_bytes(".local"sv) || string_host.ends_with_bytes("localhost"sv)) - return out; - } - - return Error::from_string_view("Invalid URL"sv); -#endif -} -} diff --git a/Userland/Libraries/LibPublicSuffix/URL.h b/Userland/Libraries/LibPublicSuffix/URL.h deleted file mode 100644 index c1d623d92ca..00000000000 --- a/Userland/Libraries/LibPublicSuffix/URL.h +++ /dev/null @@ -1,15 +0,0 @@ -/* - * Copyright (c) 2023, Cameron Youell - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include - -namespace PublicSuffix { - -ErrorOr absolute_url(StringView url); - -} diff --git a/Userland/Libraries/LibWebView/CMakeLists.txt b/Userland/Libraries/LibWebView/CMakeLists.txt index be33be92f30..49d847cee64 100644 --- a/Userland/Libraries/LibWebView/CMakeLists.txt +++ b/Userland/Libraries/LibWebView/CMakeLists.txt @@ -1,3 +1,5 @@ +include(${SerenityOS_SOURCE_DIR}/Meta/CMake/public_suffix.cmake) + set(SOURCES AccessibilityTreeModel.cpp AriaPropertiesStateModel.cpp @@ -14,8 +16,11 @@ set(SOURCES ViewImplementation.cpp WebContentClient.cpp WebSocketClientAdapter.cpp + ${PUBLIC_SUFFIX_SOURCES} ) +set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED}) + if (SERENITYOS) list(APPEND SOURCES OutOfProcessWebView.cpp) endif() @@ -41,7 +46,8 @@ set(GENERATED_SOURCES ) serenity_lib(LibWebView webview) -target_link_libraries(LibWebView PRIVATE LibCore LibFileSystem LibGfx LibGUI LibIPC LibProtocol LibPublicSuffix LibJS LibWeb LibSQL) +target_link_libraries(LibWebView PRIVATE LibCore LibFileSystem LibGfx LibGUI LibIPC LibProtocol LibJS LibWeb LibSQL) +target_compile_definitions(LibWebView PRIVATE ENABLE_PUBLIC_SUFFIX=$) if (SERENITYOS) target_link_libraries(LibWebView PRIVATE LibFileSystemAccessClient) diff --git a/Userland/Libraries/LibWebView/URL.cpp b/Userland/Libraries/LibWebView/URL.cpp index 856e3cc2f48..5dd93de60b7 100644 --- a/Userland/Libraries/LibWebView/URL.cpp +++ b/Userland/Libraries/LibWebView/URL.cpp @@ -1,16 +1,54 @@ /* * Copyright (c) 2023, Tim Flynn + * Copyright (c) 2023, Cameron Youell * * SPDX-License-Identifier: BSD-2-Clause */ +#include #include #include -#include #include +#if defined(ENABLE_PUBLIC_SUFFIX) +# include +#endif + namespace WebView { +static Optional query_public_suffix_list(StringView url_string) +{ + auto out = MUST(String::from_utf8(url_string)); + if (!out.contains("://"sv)) + out = MUST(String::formatted("https://{}"sv, out)); + + auto url = URL::create_with_url_or_path(out.to_deprecated_string()); + if (!url.is_valid()) + return {}; + +#if defined(ENABLE_PUBLIC_SUFFIX) + if (url.host().has() || url.host().has()) + return url; + + if (url.scheme() != "http"sv && url.scheme() != "https"sv) + return url; + + if (url.host().has()) { + auto const& host = url.host().get(); + + if (auto public_suffix = MUST(PublicSuffixData::the()->get_public_suffix(host)); public_suffix.has_value()) + return url; + + if (host.ends_with_bytes(".local"sv) || host.ends_with_bytes("localhost"sv)) + return url; + } + + return {}; +#else + return url; +#endif +} + Optional sanitize_url(StringView url, Optional search_engine, AppendTLD append_tld) { if (FileSystem::exists(url)) { @@ -38,8 +76,8 @@ Optional sanitize_url(StringView url, Optional search_engine, A } } - auto result = PublicSuffix::absolute_url(url); - if (result.is_error()) + auto result = query_public_suffix_list(url); + if (!result.has_value()) return format_search_engine(); return result.release_value();