LibWebView: Add a command line option to override the User-Agent

This commit just adds a command line option to case-insensitively accept
a User-Agent name to use as the UA override. The UIs will individually
need to make use of this option.
This commit is contained in:
Timothy Flynn 2024-08-28 10:26:11 -04:00 committed by Tim Ledbetter
commit a04327a0c9
Notes: github-actions[bot] 2024-08-29 12:06:59 +00:00
4 changed files with 27 additions and 0 deletions

View file

@ -11,6 +11,7 @@
#include <LibImageDecoderClient/Client.h>
#include <LibWebView/Application.h>
#include <LibWebView/URL.h>
#include <LibWebView/UserAgent.h>
#include <LibWebView/WebContentClient.h>
namespace WebView {
@ -59,6 +60,7 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_
Optional<StringView> debug_process;
Optional<StringView> profile_process;
Optional<StringView> webdriver_content_ipc_path;
Optional<StringView> user_agent_preset;
bool log_all_js_exceptions = false;
bool enable_idl_tracing = false;
bool enable_http_cache = false;
@ -83,6 +85,16 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_
args_parser.add_option(expose_internals_object, "Expose internals object", "expose-internals-object");
args_parser.add_option(force_cpu_painting, "Force CPU painting", "force-cpu-painting");
args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");
args_parser.add_option(Core::ArgsParser::Option {
.argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
.help_string = "Name of the User-Agent preset to use in place of the default User-Agent",
.long_name = "user-agent-preset",
.value_name = "name",
.accept_value = [&](StringView value) {
user_agent_preset = normalize_user_agent_name(value);
return user_agent_preset.has_value();
},
});
create_platform_arguments(args_parser);
args_parser.parse(arguments);
@ -114,6 +126,7 @@ void Application::initialize(Main::Arguments const& arguments, URL::URL new_tab_
m_web_content_options = {
.command_line = MUST(String::join(' ', arguments.strings)),
.executable_path = MUST(String::from_byte_string(MUST(Core::System::current_executable_path()))),
.user_agent_preset = move(user_agent_preset),
.log_all_js_exceptions = log_all_js_exceptions ? LogAllJSExceptions::Yes : LogAllJSExceptions::No,
.enable_idl_tracing = enable_idl_tracing ? EnableIDLTracing::Yes : EnableIDLTracing::No,
.enable_http_cache = enable_http_cache ? EnableHTTPCache::Yes : EnableHTTPCache::No,

View file

@ -93,6 +93,7 @@ struct WebContentOptions {
String command_line;
String executable_path;
Optional<ByteString> config_path {};
Optional<StringView> user_agent_preset {};
IsLayoutTestMode is_layout_test_mode { IsLayoutTestMode::No };
UseLagomNetworking use_lagom_networking { UseLagomNetworking::Yes };
LogAllJSExceptions log_all_js_exceptions { LogAllJSExceptions::No };

View file

@ -19,4 +19,14 @@ OrderedHashMap<StringView, StringView> const user_agents = {
{ "Safari iOS Mobile"sv, "Mozilla/5.0 (iPhone; CPU iPhone OS 17_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.5 Mobile/15E148 Safari/604.1"sv },
};
Optional<StringView> normalize_user_agent_name(StringView name)
{
for (auto const& user_agent : user_agents) {
if (user_agent.key.equals_ignoring_ascii_case(name))
return user_agent.key;
}
return {};
}
}

View file

@ -7,10 +7,13 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
namespace WebView {
extern OrderedHashMap<StringView, StringView> const user_agents;
Optional<StringView> normalize_user_agent_name(StringView);
}