summaryrefslogtreecommitdiff
path: root/Userland/Services
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2022-09-07 20:30:31 +0200
committerAndreas Kling <kling@serenityos.org>2022-09-07 20:30:31 +0200
commit9567e211e7c0ae27a65c1f23c4fd9071e8c35e29 (patch)
treed0de19021f93a4ffa604c9a1e51218dbe3aa2e9d /Userland/Services
parent7e5a8bd4b04963325e219c0cbb4c629a456c6fbc (diff)
downloadserenity-9567e211e7c0ae27a65c1f23c4fd9071e8c35e29.zip
LibWeb+WebContent: Add abstraction layer for event loop and timers
Instead of using Core::EventLoop and Core::Timer directly, LibWeb now goes through a Web::Platform abstraction layer instead. This will allow us to plug in Qt's event loop (and QTimer) over in Ladybird, to avoid having to deal with multiple event loops.
Diffstat (limited to 'Userland/Services')
-rw-r--r--Userland/Services/WebContent/CMakeLists.txt6
-rw-r--r--Userland/Services/WebContent/EventLoopPluginSerenity.cpp34
-rw-r--r--Userland/Services/WebContent/EventLoopPluginSerenity.h23
-rw-r--r--Userland/Services/WebContent/TimerSerenity.cpp84
-rw-r--r--Userland/Services/WebContent/TimerSerenity.h42
-rw-r--r--Userland/Services/WebContent/main.cpp4
6 files changed, 191 insertions, 2 deletions
diff --git a/Userland/Services/WebContent/CMakeLists.txt b/Userland/Services/WebContent/CMakeLists.txt
index 3ec41acdb5..8327b15562 100644
--- a/Userland/Services/WebContent/CMakeLists.txt
+++ b/Userland/Services/WebContent/CMakeLists.txt
@@ -10,11 +10,13 @@ compile_ipc(WebContentClient.ipc WebContentClientEndpoint.h)
set(SOURCES
ConnectionFromClient.cpp
ConsoleGlobalObject.cpp
- main.cpp
+ EventLoopPluginSerenity.cpp
PageHost.cpp
+ TimerSerenity.cpp
+ WebContentClientEndpoint.h
WebContentConsoleClient.cpp
WebContentServerEndpoint.h
- WebContentClientEndpoint.h
+ main.cpp
)
serenity_bin(WebContent)
diff --git a/Userland/Services/WebContent/EventLoopPluginSerenity.cpp b/Userland/Services/WebContent/EventLoopPluginSerenity.cpp
new file mode 100644
index 0000000000..15a6c17dd1
--- /dev/null
+++ b/Userland/Services/WebContent/EventLoopPluginSerenity.cpp
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "EventLoopPluginSerenity.h"
+#include "TimerSerenity.h"
+#include <AK/Function.h>
+#include <AK/NonnullRefPtr.h>
+#include <LibCore/EventLoop.h>
+
+namespace WebContent {
+
+EventLoopPluginSerenity::EventLoopPluginSerenity() = default;
+EventLoopPluginSerenity::~EventLoopPluginSerenity() = default;
+
+void EventLoopPluginSerenity::spin_until(Function<bool()> goal_condition)
+{
+ Core::EventLoop::current().spin_until(move(goal_condition));
+}
+
+void EventLoopPluginSerenity::deferred_invoke(Function<void()> function)
+{
+ VERIFY(function);
+ Core::deferred_invoke(move(function));
+}
+
+NonnullRefPtr<Web::Platform::Timer> EventLoopPluginSerenity::create_timer()
+{
+ return TimerSerenity::create();
+}
+
+}
diff --git a/Userland/Services/WebContent/EventLoopPluginSerenity.h b/Userland/Services/WebContent/EventLoopPluginSerenity.h
new file mode 100644
index 0000000000..0493ac7ab1
--- /dev/null
+++ b/Userland/Services/WebContent/EventLoopPluginSerenity.h
@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibWeb/Platform/EventLoopPlugin.h>
+
+namespace WebContent {
+
+class EventLoopPluginSerenity final : public Web::Platform::EventLoopPlugin {
+public:
+ EventLoopPluginSerenity();
+ virtual ~EventLoopPluginSerenity() override;
+
+ virtual void spin_until(Function<bool()> goal_condition) override;
+ virtual void deferred_invoke(Function<void()>) override;
+ virtual NonnullRefPtr<Web::Platform::Timer> create_timer() override;
+};
+
+}
diff --git a/Userland/Services/WebContent/TimerSerenity.cpp b/Userland/Services/WebContent/TimerSerenity.cpp
new file mode 100644
index 0000000000..a702ba2eca
--- /dev/null
+++ b/Userland/Services/WebContent/TimerSerenity.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include "TimerSerenity.h"
+#include <AK/NonnullRefPtr.h>
+#include <LibCore/Timer.h>
+
+namespace WebContent {
+
+NonnullRefPtr<TimerSerenity> TimerSerenity::create()
+{
+ return adopt_ref(*new TimerSerenity);
+}
+
+TimerSerenity::TimerSerenity()
+ : m_timer(Core::Timer::construct())
+{
+ m_timer->on_timeout = [this] {
+ if (on_timeout)
+ on_timeout();
+ };
+}
+
+TimerSerenity::~TimerSerenity() = default;
+
+void TimerSerenity::start()
+{
+ m_timer->start();
+}
+
+void TimerSerenity::start(int interval_ms)
+{
+ m_timer->start(interval_ms);
+}
+
+void TimerSerenity::restart()
+{
+ m_timer->restart();
+}
+
+void TimerSerenity::restart(int interval_ms)
+{
+ m_timer->restart(interval_ms);
+}
+
+void TimerSerenity::stop()
+{
+ m_timer->stop();
+}
+
+void TimerSerenity::set_active(bool active)
+{
+ m_timer->set_active(active);
+}
+
+bool TimerSerenity::is_active() const
+{
+ return m_timer->is_active();
+}
+
+int TimerSerenity::interval() const
+{
+ return m_timer->interval();
+}
+
+void TimerSerenity::set_interval(int interval_ms)
+{
+ m_timer->set_interval(interval_ms);
+}
+
+bool TimerSerenity::is_single_shot() const
+{
+ return m_timer->is_single_shot();
+}
+
+void TimerSerenity::set_single_shot(bool single_shot)
+{
+ m_timer->set_single_shot(single_shot);
+}
+
+}
diff --git a/Userland/Services/WebContent/TimerSerenity.h b/Userland/Services/WebContent/TimerSerenity.h
new file mode 100644
index 0000000000..d0f9200839
--- /dev/null
+++ b/Userland/Services/WebContent/TimerSerenity.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/NonnullRefPtr.h>
+#include <LibCore/Forward.h>
+#include <LibWeb/Platform/Timer.h>
+
+namespace WebContent {
+
+class TimerSerenity final : public Web::Platform::Timer {
+public:
+ static NonnullRefPtr<TimerSerenity> create();
+
+ virtual ~TimerSerenity();
+
+ virtual void start() override;
+ virtual void start(int interval_ms) override;
+ virtual void restart() override;
+ virtual void restart(int interval_ms) override;
+ virtual void stop() override;
+
+ virtual void set_active(bool) override;
+
+ virtual bool is_active() const override;
+ virtual int interval() const override;
+ virtual void set_interval(int interval_ms) override;
+
+ virtual bool is_single_shot() const override;
+ virtual void set_single_shot(bool) override;
+
+private:
+ TimerSerenity();
+
+ NonnullRefPtr<Core::Timer> m_timer;
+};
+
+}
diff --git a/Userland/Services/WebContent/main.cpp b/Userland/Services/WebContent/main.cpp
index 9db9c3ed40..5a06e6e00c 100644
--- a/Userland/Services/WebContent/main.cpp
+++ b/Userland/Services/WebContent/main.cpp
@@ -4,6 +4,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
+#include "EventLoopPluginSerenity.h"
#include <LibCore/EventLoop.h>
#include <LibCore/LocalServer.h>
#include <LibCore/System.h>
@@ -11,6 +12,7 @@
#include <LibMain/Main.h>
#include <LibWeb/ImageDecoding.h>
#include <LibWeb/Loader/ResourceLoader.h>
+#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/WebSockets/WebSocket.h>
#include <LibWebView/ImageDecoderClientAdapter.h>
#include <LibWebView/RequestServerAdapter.h>
@@ -28,6 +30,8 @@ ErrorOr<int> serenity_main(Main::Arguments)
TRY(Core::System::unveil("/tmp/user/%uid/portal/websocket", "rw"));
TRY(Core::System::unveil(nullptr, nullptr));
+ Web::Platform::EventLoopPlugin::install(*new WebContent::EventLoopPluginSerenity);
+
Web::ImageDecoding::Decoder::initialize(WebView::ImageDecoderClientAdapter::create());
Web::WebSockets::WebSocketClientManager::initialize(TRY(WebView::WebSocketClientManagerAdapter::try_create()));
Web::ResourceLoader::initialize(TRY(WebView::RequestServerAdapter::try_create()));