1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 21:25:00 +00:00
serenity/Userland/Libraries/LibWeb/WebSockets/WebSocket.h
Ali Mohammad Pur 5e1499d104 Everywhere: Rename {Deprecated => Byte}String
This commit un-deprecates DeprecatedString, and repurposes it as a byte
string.
As the null state has already been removed, there are no other
particularly hairy blockers in repurposing this type as a byte string
(what it _really_ is).

This commit is auto-generated:
  $ xs=$(ack -l \bDeprecatedString\b\|deprecated_string AK Userland \
    Meta Ports Ladybird Tests Kernel)
  $ perl -pie 's/\bDeprecatedString\b/ByteString/g;
    s/deprecated_string/byte_string/g' $xs
  $ clang-format --style=file -i \
    $(git diff --name-only | grep \.cpp\|\.h)
  $ gn format $(git ls-files '*.gn' '*.gni')
2023-12-17 18:25:10 +03:30

132 lines
4 KiB
C++

/*
* Copyright (c) 2021-2022, Dex♪ <dexes.ttp@gmail.com>
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/ByteBuffer.h>
#include <AK/URL.h>
#include <LibCore/EventReceiver.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
#define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \
E(onerror, HTML::EventNames::error) \
E(onclose, HTML::EventNames::close) \
E(onopen, HTML::EventNames::open) \
E(onmessage, HTML::EventNames::message)
namespace Web::WebSockets {
class WebSocketClientSocket;
class WebSocketClientManager;
class WebSocket final : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(WebSocket, DOM::EventTarget);
JS_DECLARE_ALLOCATOR(WebSocket);
public:
enum class ReadyState : u16 {
Connecting = 0,
Open = 1,
Closing = 2,
Closed = 3,
};
static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, String const& url, Optional<Variant<String, Vector<String>>> const& protocols);
virtual ~WebSocket() override;
WebIDL::ExceptionOr<String> url() const { return TRY_OR_THROW_OOM(vm(), m_url.to_string()); }
void set_url(AK::URL url) { m_url = move(url); }
#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \
void set_##attribute_name(WebIDL::CallbackType*); \
WebIDL::CallbackType* attribute_name();
ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE
ReadyState ready_state() const;
String extensions() const;
WebIDL::ExceptionOr<String> protocol() const;
String const& binary_type() { return m_binary_type; }
void set_binary_type(String const& type) { m_binary_type = type; }
WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<String> reason);
WebIDL::ExceptionOr<void> send(Variant<JS::Handle<WebIDL::BufferSource>, JS::Handle<FileAPI::Blob>, String> const& data);
private:
void on_open();
void on_message(ByteBuffer message, bool is_text);
void on_error();
void on_close(u16 code, String reason, bool was_clean);
WebSocket(JS::Realm&);
virtual void initialize(JS::Realm&) override;
ErrorOr<void> establish_web_socket_connection(AK::URL& url_record, Vector<String>& protocols, HTML::EnvironmentSettingsObject& client);
AK::URL m_url;
String m_binary_type { "blob"_string };
RefPtr<WebSocketClientSocket> m_websocket;
};
class WebSocketClientSocket : public RefCounted<WebSocketClientSocket> {
public:
virtual ~WebSocketClientSocket();
struct CertificateAndKey {
ByteString certificate;
ByteString key;
};
struct Message {
ByteBuffer data;
bool is_text { false };
};
enum class Error {
CouldNotEstablishConnection,
ConnectionUpgradeFailed,
ServerClosedSocket,
};
virtual Web::WebSockets::WebSocket::ReadyState ready_state() = 0;
virtual ByteString subprotocol_in_use() = 0;
virtual void send(ByteBuffer binary_or_text_message, bool is_text) = 0;
virtual void send(StringView text_message) = 0;
virtual void close(u16 code = 1005, ByteString reason = {}) = 0;
Function<void()> on_open;
Function<void(Message)> on_message;
Function<void(Error)> on_error;
Function<void(u16 code, ByteString reason, bool was_clean)> on_close;
Function<CertificateAndKey()> on_certificate_requested;
protected:
explicit WebSocketClientSocket();
};
class WebSocketClientManager : public Core::EventReceiver {
C_OBJECT_ABSTRACT(WebSocketClientManager)
public:
static void initialize(RefPtr<WebSocketClientManager>);
static WebSocketClientManager& the();
virtual RefPtr<WebSocketClientSocket> connect(AK::URL const&, ByteString const& origin, Vector<ByteString> const& protocols) = 0;
protected:
explicit WebSocketClientManager();
};
}