LibWebView+LibPublicSuffix: Merge LibPublicSuffix into LibWebView

After d2c7e1ea7d, 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.
This commit is contained in:
Timothy Flynn 2023-10-14 09:07:38 -04:00 committed by Tim Flynn
parent a1cce69db0
commit a8f0fa5dd4
Notes: sideshowbarker 2024-07-17 11:30:05 +09:00
11 changed files with 57 additions and 87 deletions

View file

@ -427,7 +427,6 @@ if (BUILD_LAGOM)
Markdown
PDF
Protocol
PublicSuffix
Regex
SoftGPU
SQL

View file

@ -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)

View file

@ -1 +0,0 @@
lagom_tool(GeneratePublicSuffixData SOURCES GeneratePublicSuffixData.cpp LIBS LibMain)

View file

@ -0,0 +1 @@
lagom_tool(GeneratePublicSuffixData SOURCES GeneratePublicSuffixData.cpp LIBS LibMain)

View file

@ -48,7 +48,7 @@ ErrorOr<void> generate_header_file(Core::InputBufferedFile&, Core::File& file)
#include <AK/Forward.h>
#include <AK/Trie.h>
namespace PublicSuffix {
namespace WebView {
class PublicSuffixData {
protected:
@ -72,7 +72,7 @@ private:
Trie<char, DeprecatedString> m_dictionary;
};
} // namespace PublicSuffix
}
)~~~");
@ -85,11 +85,11 @@ ErrorOr<void> generate_implementation_file(Core::InputBufferedFile& input, Core:
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
#include <LibPublicSuffix/PublicSuffixData.h>
#include <AK/Vector.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibWebView/PublicSuffixData.h>
namespace PublicSuffix {
namespace WebView {
static Vector<StringView> 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<DeprecatedString> {
MUST(m_dictionary.insert(str.begin(), str.end(), str, [](auto& parent, auto& it) -> Optional<DeprecatedString> {
return DeprecatedString::formatted("{}{}", parent.metadata_value(), *it);
}));
}
@ -176,7 +176,7 @@ ErrorOr<Optional<String>> PublicSuffixData::get_public_suffix(StringView string)
return Optional<String> {};
}
} // namespace PublicSuffix
}
)~~~");

View file

@ -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)

View file

@ -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=$<BOOL:${ENABLE_PUBLIC_SUFFIX_DOWNLOAD}>)

View file

@ -1,47 +0,0 @@
/*
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <AK/URL.h>
#include <LibPublicSuffix/URL.h>
#if defined(ENABLE_PUBLIC_SUFFIX_DOWNLOAD)
# include <LibPublicSuffix/PublicSuffixData.h>
#endif
namespace PublicSuffix {
ErrorOr<String> 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<URL::IPv4Address>() || final_url.host().has<URL::IPv6Address>())
return out;
if (final_url.scheme() != "http"sv && final_url.scheme() != "https"sv)
return out;
if (final_url.host().has<String>()) {
auto string_host = final_url.host().get<String>();
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
}
}

View file

@ -1,15 +0,0 @@
/*
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
namespace PublicSuffix {
ErrorOr<String> absolute_url(StringView url);
}

View file

@ -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=$<BOOL:${ENABLE_PUBLIC_SUFFIX_DOWNLOAD}>)
if (SERENITYOS)
target_link_libraries(LibWebView PRIVATE LibFileSystemAccessClient)

View file

@ -1,16 +1,54 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <LibPublicSuffix/URL.h>
#include <LibWebView/URL.h>
#if defined(ENABLE_PUBLIC_SUFFIX)
# include <LibWebView/PublicSuffixData.h>
#endif
namespace WebView {
static Optional<URL> 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::IPv4Address>() || url.host().has<URL::IPv6Address>())
return url;
if (url.scheme() != "http"sv && url.scheme() != "https"sv)
return url;
if (url.host().has<String>()) {
auto const& host = url.host().get<String>();
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<URL> sanitize_url(StringView url, Optional<StringView> search_engine, AppendTLD append_tld)
{
if (FileSystem::exists(url)) {
@ -38,8 +76,8 @@ Optional<URL> sanitize_url(StringView url, Optional<StringView> 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();