소스 검색

LibWebView+WebContent: Monitor for system time zone changes

This creates a TimeZoneWatcher in the UI process to inform all open
WebContent processes when the time zone changes. The WebContent process
will clear its time zone cache to retrieve a fresh zone the next time
it is asked for one.
Timothy Flynn 10 달 전
부모
커밋
d8c69a0e9e

+ 18 - 0
Userland/Libraries/LibWebView/Application.cpp

@@ -6,6 +6,8 @@
 
 #include <AK/Debug.h>
 #include <LibCore/ArgsParser.h>
+#include <LibCore/Environment.h>
+#include <LibCore/TimeZoneWatcher.h>
 #include <LibImageDecoderClient/Client.h>
 #include <LibWebView/Application.h>
 #include <LibWebView/URL.h>
@@ -20,6 +22,22 @@ Application::Application()
     VERIFY(!s_the);
     s_the = this;
 
+    // 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()) {
+            warnln("Unable to monitor system time zone: {}", time_zone_watcher.error());
+        } else {
+            m_time_zone_watcher = time_zone_watcher.release_value();
+
+            m_time_zone_watcher->on_time_zone_changed = []() {
+                WebContentClient::for_each_client([&](WebView::WebContentClient& client) {
+                    client.async_system_time_zone_changed();
+                    return IterationDecision::Continue;
+                });
+            };
+        }
+    }
+
     m_process_manager.on_process_exited = [this](Process&& process) {
         process_did_exit(move(process));
     };

+ 3 - 0
Userland/Libraries/LibWebView/Application.h

@@ -9,6 +9,7 @@
 #include <AK/Badge.h>
 #include <AK/Swift.h>
 #include <LibCore/EventLoop.h>
+#include <LibCore/Forward.h>
 #include <LibMain/Main.h>
 #include <LibURL/URL.h>
 #include <LibWebView/Options.h>
@@ -69,6 +70,8 @@ private:
     ChromeOptions m_chrome_options;
     WebContentOptions m_web_content_options;
 
+    OwnPtr<Core::TimeZoneWatcher> m_time_zone_watcher;
+
     Core::EventLoop m_event_loop;
     ProcessManager m_process_manager;
     bool m_in_shutdown { false };

+ 6 - 0
Userland/Services/WebContent/ConnectionFromClient.cpp

@@ -16,6 +16,7 @@
 #include <LibGfx/SystemTheme.h>
 #include <LibJS/Heap/Heap.h>
 #include <LibJS/Runtime/ConsoleObject.h>
+#include <LibUnicode/TimeZone.h>
 #include <LibWeb/ARIA/RoleType.h>
 #include <LibWeb/Bindings/MainThreadVM.h>
 #include <LibWeb/CSS/StyleComputer.h>
@@ -1169,4 +1170,9 @@ void ConnectionFromClient::enable_inspector_prototype(u64)
     Web::HTML::Window::set_inspector_object_exposed(true);
 }
 
+void ConnectionFromClient::system_time_zone_changed()
+{
+    Unicode::clear_system_time_zone_cache();
+}
+
 }

+ 2 - 0
Userland/Services/WebContent/ConnectionFromClient.h

@@ -145,6 +145,8 @@ private:
 
     virtual void paste(u64 page_id, String const& text) override;
 
+    virtual void system_time_zone_changed() override;
+
     void report_finished_handling_input_event(u64 page_id, bool event_was_handled);
 
     NonnullOwnPtr<PageHost> m_page_host;

+ 2 - 0
Userland/Services/WebContent/WebContentServer.ipc

@@ -115,4 +115,6 @@ endpoint WebContentServer
     set_user_style(u64 page_id, String source) =|
 
     enable_inspector_prototype(u64 page_id) =|
+
+    system_time_zone_changed() =|
 }