From 9bbad085468c64fcebee350b13a01ed0a3cf3d34 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 8 Nov 2022 19:17:29 +0100 Subject: [PATCH] LibWebSocket: Allow library clients to provide their own WebSocketImpl --- Userland/Libraries/LibWebSocket/WebSocket.cpp | 12 +++++++----- Userland/Libraries/LibWebSocket/WebSocket.h | 4 ++-- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibWebSocket/WebSocket.cpp b/Userland/Libraries/LibWebSocket/WebSocket.cpp index ff7bddec15..d3c7858cd4 100644 --- a/Userland/Libraries/LibWebSocket/WebSocket.cpp +++ b/Userland/Libraries/LibWebSocket/WebSocket.cpp @@ -17,21 +17,23 @@ namespace WebSocket { // Note : The websocket protocol is defined by RFC 6455, found at https://tools.ietf.org/html/rfc6455 // In this file, section numbers will refer to the RFC 6455 -NonnullRefPtr WebSocket::create(ConnectionInfo connection) +NonnullRefPtr WebSocket::create(ConnectionInfo connection, RefPtr impl) { - return adopt_ref(*new WebSocket(move(connection))); + return adopt_ref(*new WebSocket(move(connection), move(impl))); } -WebSocket::WebSocket(ConnectionInfo connection) +WebSocket::WebSocket(ConnectionInfo connection, RefPtr impl) : m_connection(move(connection)) + , m_impl(move(impl)) { } void WebSocket::start() { VERIFY(m_state == WebSocket::InternalState::NotStarted); - VERIFY(!m_impl); - m_impl = adopt_ref(*new WebSocketImplSerenity); + + if (!m_impl) + m_impl = adopt_ref(*new WebSocketImplSerenity); m_impl->on_connection_error = [this] { dbgln("WebSocket: Connection error (underlying socket)"); diff --git a/Userland/Libraries/LibWebSocket/WebSocket.h b/Userland/Libraries/LibWebSocket/WebSocket.h index 72fab05603..734e46f496 100644 --- a/Userland/Libraries/LibWebSocket/WebSocket.h +++ b/Userland/Libraries/LibWebSocket/WebSocket.h @@ -25,7 +25,7 @@ enum class ReadyState { class WebSocket final : public Core::Object { C_OBJECT(WebSocket) public: - static NonnullRefPtr create(ConnectionInfo); + static NonnullRefPtr create(ConnectionInfo, RefPtr = nullptr); virtual ~WebSocket() override = default; URL const& url() const { return m_connection.url(); } @@ -54,7 +54,7 @@ public: Function on_error; private: - explicit WebSocket(ConnectionInfo); + WebSocket(ConnectionInfo, RefPtr); // As defined in section 5.2 enum class OpCode : u8 {