mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 08:47:44 +00:00
LibWeb: Abstract the LibProtocol WebSockets connection
Much like the ImageDecoder change, this moves the underlying connection of the Web::WebSockets class from LibWeb to LibWebView, removing the need for LibProtocol in LibWeb for this specific use-case.
This commit is contained in:
parent
2198091bbc
commit
2a359695c6
6 changed files with 255 additions and 54 deletions
|
@ -4,6 +4,7 @@ set(SOURCES
|
|||
OutOfProcessWebView.cpp
|
||||
StylePropertiesModel.cpp
|
||||
WebContentClient.cpp
|
||||
WebSocketClientAdapter.cpp
|
||||
)
|
||||
|
||||
set(GENERATED_SOURCES
|
||||
|
@ -14,6 +15,6 @@ set(GENERATED_SOURCES
|
|||
)
|
||||
|
||||
serenity_lib(LibWebView webview)
|
||||
target_link_libraries(LibWebView LibGfx LibGUI LibImageDecoderClient LibIPC LibWeb)
|
||||
target_link_libraries(LibWebView LibGfx LibGUI LibImageDecoderClient LibIPC LibProtocol LibWeb)
|
||||
|
||||
add_subdirectory(DumpLayoutTree)
|
||||
|
|
126
Userland/Libraries/LibWebView/WebSocketClientAdapter.cpp
Normal file
126
Userland/Libraries/LibWebView/WebSocketClientAdapter.cpp
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibProtocol/WebSocket.h>
|
||||
#include <LibProtocol/WebSocketClient.h>
|
||||
#include <LibWebView/WebSocketClientAdapter.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
RefPtr<WebSocketClientSocketAdapter> WebSocketClientSocketAdapter::create(NonnullRefPtr<Protocol::WebSocket> websocket)
|
||||
{
|
||||
return adopt_ref(*new WebSocketClientSocketAdapter(move(websocket)));
|
||||
}
|
||||
|
||||
WebSocketClientSocketAdapter::WebSocketClientSocketAdapter(NonnullRefPtr<Protocol::WebSocket> websocket)
|
||||
: m_websocket(move(websocket))
|
||||
{
|
||||
m_websocket->on_open = [weak_this = make_weak_ptr()] {
|
||||
if (auto strong_this = weak_this.strong_ref())
|
||||
if (strong_this->on_open)
|
||||
strong_this->on_open();
|
||||
};
|
||||
m_websocket->on_message = [weak_this = make_weak_ptr()](auto message) {
|
||||
if (auto strong_this = weak_this.strong_ref()) {
|
||||
if (strong_this->on_message) {
|
||||
strong_this->on_message(Web::WebSockets::WebSocketClientSocket::Message {
|
||||
.data = move(message.data),
|
||||
.is_text = message.is_text,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
m_websocket->on_error = [weak_this = make_weak_ptr()](auto error) {
|
||||
if (auto strong_this = weak_this.strong_ref()) {
|
||||
if (strong_this->on_error) {
|
||||
switch (error) {
|
||||
case Protocol::WebSocket::Error::CouldNotEstablishConnection:
|
||||
strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::CouldNotEstablishConnection);
|
||||
return;
|
||||
case Protocol::WebSocket::Error::ConnectionUpgradeFailed:
|
||||
strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::ConnectionUpgradeFailed);
|
||||
return;
|
||||
case Protocol::WebSocket::Error::ServerClosedSocket:
|
||||
strong_this->on_error(Web::WebSockets::WebSocketClientSocket::Error::ServerClosedSocket);
|
||||
return;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
};
|
||||
m_websocket->on_close = [weak_this = make_weak_ptr()](u16 code, String reason, bool was_clean) {
|
||||
if (auto strong_this = weak_this.strong_ref())
|
||||
if (strong_this->on_close)
|
||||
strong_this->on_close(code, move(reason), was_clean);
|
||||
};
|
||||
m_websocket->on_certificate_requested = [weak_this = make_weak_ptr()] {
|
||||
if (auto strong_this = weak_this.strong_ref()) {
|
||||
if (strong_this->on_certificate_requested) {
|
||||
auto certificate_and_key = weak_this->on_certificate_requested();
|
||||
return Protocol::WebSocket::CertificateAndKey {
|
||||
.certificate = move(certificate_and_key.certificate),
|
||||
.key = move(certificate_and_key.key),
|
||||
};
|
||||
}
|
||||
}
|
||||
return Protocol::WebSocket::CertificateAndKey {};
|
||||
};
|
||||
}
|
||||
|
||||
WebSocketClientSocketAdapter::~WebSocketClientSocketAdapter() = default;
|
||||
|
||||
Web::WebSockets::WebSocket::ReadyState WebSocketClientSocketAdapter::ready_state()
|
||||
{
|
||||
switch (m_websocket->ready_state()) {
|
||||
case Protocol::WebSocket::ReadyState::Connecting:
|
||||
return Web::WebSockets::WebSocket::ReadyState::Connecting;
|
||||
case Protocol::WebSocket::ReadyState::Open:
|
||||
return Web::WebSockets::WebSocket::ReadyState::Open;
|
||||
case Protocol::WebSocket::ReadyState::Closing:
|
||||
return Web::WebSockets::WebSocket::ReadyState::Closing;
|
||||
case Protocol::WebSocket::ReadyState::Closed:
|
||||
return Web::WebSockets::WebSocket::ReadyState::Closed;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
void WebSocketClientSocketAdapter::send(ByteBuffer binary_or_text_message, bool is_text)
|
||||
{
|
||||
m_websocket->send(binary_or_text_message, is_text);
|
||||
}
|
||||
|
||||
void WebSocketClientSocketAdapter::send(StringView text_message)
|
||||
{
|
||||
m_websocket->send(text_message);
|
||||
}
|
||||
|
||||
void WebSocketClientSocketAdapter::close(u16 code, String reason)
|
||||
{
|
||||
m_websocket->close(code, reason);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<WebSocketClientManagerAdapter>> WebSocketClientManagerAdapter::try_create()
|
||||
{
|
||||
auto websocket_client = TRY(Protocol::WebSocketClient::try_create());
|
||||
return adopt_nonnull_ref_or_enomem(new (nothrow) WebSocketClientManagerAdapter(move(websocket_client)));
|
||||
}
|
||||
|
||||
WebSocketClientManagerAdapter::WebSocketClientManagerAdapter(NonnullRefPtr<Protocol::WebSocketClient> websocket_client)
|
||||
: m_websocket_client(move(websocket_client))
|
||||
{
|
||||
}
|
||||
|
||||
WebSocketClientManagerAdapter::~WebSocketClientManagerAdapter() = default;
|
||||
|
||||
RefPtr<Web::WebSockets::WebSocketClientSocket> WebSocketClientManagerAdapter::connect(const AK::URL& url, String const& origin)
|
||||
{
|
||||
auto underlying_websocket = m_websocket_client->connect(url, origin);
|
||||
if (!underlying_websocket)
|
||||
return {};
|
||||
return WebSocketClientSocketAdapter::create(underlying_websocket.release_nonnull());
|
||||
}
|
||||
|
||||
}
|
54
Userland/Libraries/LibWebView/WebSocketClientAdapter.h
Normal file
54
Userland/Libraries/LibWebView/WebSocketClientAdapter.h
Normal file
|
@ -0,0 +1,54 @@
|
|||
/*
|
||||
* Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/NonnullRefPtr.h>
|
||||
#include <AK/Weakable.h>
|
||||
#include <LibWeb/WebSockets/WebSocket.h>
|
||||
|
||||
namespace Protocol {
|
||||
class WebSocket;
|
||||
class WebSocketClient;
|
||||
};
|
||||
|
||||
namespace WebView {
|
||||
|
||||
class WebSocketClientSocketAdapter
|
||||
: public Web::WebSockets::WebSocketClientSocket
|
||||
, public Weakable<WebSocketClientSocketAdapter> {
|
||||
public:
|
||||
static RefPtr<WebSocketClientSocketAdapter> create(NonnullRefPtr<Protocol::WebSocket>);
|
||||
virtual ~WebSocketClientSocketAdapter() override;
|
||||
|
||||
virtual Web::WebSockets::WebSocket::ReadyState ready_state() override;
|
||||
|
||||
virtual void send(ByteBuffer binary_or_text_message, bool is_text) override;
|
||||
virtual void send(StringView text_message) override;
|
||||
virtual void close(u16 code = 1005, String reason = {}) override;
|
||||
|
||||
private:
|
||||
WebSocketClientSocketAdapter(NonnullRefPtr<Protocol::WebSocket>);
|
||||
|
||||
NonnullRefPtr<Protocol::WebSocket> m_websocket;
|
||||
};
|
||||
|
||||
class WebSocketClientManagerAdapter : public Web::WebSockets::WebSocketClientManager {
|
||||
public:
|
||||
static ErrorOr<NonnullRefPtr<WebSocketClientManagerAdapter>> try_create();
|
||||
|
||||
virtual ~WebSocketClientManagerAdapter() override;
|
||||
|
||||
virtual RefPtr<Web::WebSockets::WebSocketClientSocket> connect(const AK::URL&, String const& origin) override;
|
||||
|
||||
private:
|
||||
WebSocketClientManagerAdapter(NonnullRefPtr<Protocol::WebSocketClient>);
|
||||
|
||||
NonnullRefPtr<Protocol::WebSocketClient> m_websocket_client;
|
||||
};
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue