1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 13:07:46 +00:00

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.
This commit is contained in:
Andreas Kling 2022-09-07 20:30:31 +02:00
parent 7e5a8bd4b0
commit 9567e211e7
28 changed files with 365 additions and 42 deletions

View file

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

View file

@ -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();
}
}

View file

@ -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;
};
}

View file

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

View file

@ -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;
};
}

View file

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