LibWebView: Create a dedicated settings observer for the Application

We had a bit of an awkward setup where we want the Application to be a
SettingsObserver, but neither the Settings object nor the Application
itself was fully initialized by the time the observer was created. So
we invented a deferred observer initializer specifically for the
Application.

Instead, let's just create a dedicated observer subclass that is owned
by the Application. We can then create it once we have the singleton
Application appropriately set up.
This commit is contained in:
Timothy Flynn 2025-04-23 12:37:05 -04:00 committed by Tim Flynn
parent 6539c72e7e
commit 6a6c56aabe
Notes: github-actions[bot] 2025-04-24 00:00:00 +00:00
4 changed files with 65 additions and 90 deletions

View file

@ -26,13 +26,32 @@ namespace WebView {
Application* Application::s_the = nullptr;
struct ApplicationSettingsObserver : public SettingsObserver {
virtual void dns_settings_changed() override
{
Application::settings().dns_settings().visit(
[](SystemDNS) {
Application::request_server_client().async_set_use_system_dns();
},
[](DNSOverTLS const& dns_over_tls) {
dbgln("Setting DNS server to {}:{} with TLS", dns_over_tls.server_address, dns_over_tls.port);
Application::request_server_client().async_set_dns_server(dns_over_tls.server_address, dns_over_tls.port, true);
},
[](DNSOverUDP const& dns_over_udp) {
dbgln("Setting DNS server to {}:{}", dns_over_udp.server_address, dns_over_udp.port);
Application::request_server_client().async_set_dns_server(dns_over_udp.server_address, dns_over_udp.port, false);
});
}
};
Application::Application()
: SettingsObserver(AddToObservers::No) // Application::the() is not set yet.
, m_settings(Settings::create({}))
: m_settings(Settings::create({}))
{
VERIFY(!s_the);
s_the = this;
m_settings_observer = make<ApplicationSettingsObserver>();
// No need to monitor the system time zone if the TZ environment variable is set, as it overrides system preferences.
if (!Core::Environment::has("TZ"sv)) {
if (auto time_zone_watcher = Core::TimeZoneWatcher::create(); time_zone_watcher.is_error()) {
@ -52,13 +71,13 @@ Application::Application()
m_process_manager.on_process_exited = [this](Process&& process) {
process_did_exit(move(process));
};
SettingsObserver::complete_delayed_registration();
}
Application::~Application()
{
SettingsObserver::complete_delayed_unregistration();
// Explicitly delete the settings observer first, as the observer destructor will refer to Application::the().
m_settings_observer.clear();
s_the = nullptr;
}
@ -175,9 +194,6 @@ void Application::initialize(Main::Arguments const& arguments)
.devtools_port = devtools_port,
};
if (m_browser_options.dns_settings.has_value())
m_settings.set_dns_settings(m_browser_options.dns_settings.value(), true);
if (webdriver_content_ipc_path.has_value())
m_browser_options.webdriver_content_ipc_path = *webdriver_content_ipc_path;
@ -268,6 +284,10 @@ ErrorOr<void> Application::launch_request_server()
{
// FIXME: Create an abstraction to re-spawn the RequestServer and re-hook up its client hooks to each tab on crash
m_request_server_client = TRY(launch_request_server_process());
if (m_browser_options.dns_settings.has_value())
m_settings.set_dns_settings(m_browser_options.dns_settings.value(), true);
return {};
}
@ -717,23 +737,4 @@ void Application::request_console_messages(DevTools::TabDescription const& descr
view->js_console_request_messages(start_index);
}
void Application::dns_settings_changed()
{
if (!m_request_server_client)
return;
auto dns_settings = settings().dns_settings();
auto& rs_client = *m_request_server_client;
dns_settings.visit(
[&](SystemDNS) {
rs_client.async_set_use_system_dns();
},
[&](DNSOverTLS const& dns_over_tls) {
dbgln("Setting DNS server to {}:{} with TLS", dns_over_tls.server_address, dns_over_tls.port);
rs_client.async_set_dns_server(dns_over_tls.server_address, dns_over_tls.port, true);
},
[&](DNSOverUDP const& dns_over_udp) {
dbgln("Setting DNS server to {}:{}", dns_over_udp.server_address, dns_over_udp.port);
rs_client.async_set_dns_server(dns_over_udp.server_address, dns_over_udp.port, false);
});
}
}