Serenity Operating System
at master 130 lines 4.0 kB view raw
1/* 2 * Copyright (c) 2021-2022, Dex♪ <dexes.ttp@gmail.com> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#pragma once 8 9#include <AK/ByteBuffer.h> 10#include <AK/URL.h> 11#include <LibCore/Object.h> 12#include <LibWeb/Bindings/PlatformObject.h> 13#include <LibWeb/DOM/EventTarget.h> 14#include <LibWeb/Forward.h> 15#include <LibWeb/HTML/Window.h> 16#include <LibWeb/WebIDL/ExceptionOr.h> 17 18#define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \ 19 E(onerror, HTML::EventNames::error) \ 20 E(onclose, HTML::EventNames::close) \ 21 E(onopen, HTML::EventNames::open) \ 22 E(onmessage, HTML::EventNames::message) 23 24namespace Web::WebSockets { 25 26class WebSocketClientSocket; 27class WebSocketClientManager; 28 29class WebSocket final : public DOM::EventTarget { 30 WEB_PLATFORM_OBJECT(WebSocket, DOM::EventTarget); 31 32public: 33 enum class ReadyState : u16 { 34 Connecting = 0, 35 Open = 1, 36 Closing = 2, 37 Closed = 3, 38 }; 39 40 static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, DeprecatedString const& url, Optional<Variant<DeprecatedString, Vector<DeprecatedString>>> const& protocols); 41 42 virtual ~WebSocket() override; 43 44 DeprecatedString url() const { return m_url.to_deprecated_string(); } 45 46#undef __ENUMERATE 47#define __ENUMERATE(attribute_name, event_name) \ 48 void set_##attribute_name(WebIDL::CallbackType*); \ 49 WebIDL::CallbackType* attribute_name(); 50 ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE) 51#undef __ENUMERATE 52 53 ReadyState ready_state() const; 54 DeprecatedString extensions() const; 55 DeprecatedString protocol() const; 56 57 DeprecatedString const& binary_type() { return m_binary_type; }; 58 void set_binary_type(DeprecatedString const& type) { m_binary_type = type; }; 59 60 WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<DeprecatedString> reason); 61 WebIDL::ExceptionOr<void> send(DeprecatedString const& data); 62 63private: 64 void on_open(); 65 void on_message(ByteBuffer message, bool is_text); 66 void on_error(); 67 void on_close(u16 code, DeprecatedString reason, bool was_clean); 68 69 WebSocket(HTML::Window&, AK::URL&, Vector<DeprecatedString> const& protocols); 70 71 virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override; 72 virtual void visit_edges(Cell::Visitor&) override; 73 74 JS::NonnullGCPtr<HTML::Window> m_window; 75 76 AK::URL m_url; 77 DeprecatedString m_binary_type { "blob" }; 78 RefPtr<WebSocketClientSocket> m_websocket; 79}; 80 81class WebSocketClientSocket : public RefCounted<WebSocketClientSocket> { 82public: 83 virtual ~WebSocketClientSocket(); 84 85 struct CertificateAndKey { 86 DeprecatedString certificate; 87 DeprecatedString key; 88 }; 89 90 struct Message { 91 ByteBuffer data; 92 bool is_text { false }; 93 }; 94 95 enum class Error { 96 CouldNotEstablishConnection, 97 ConnectionUpgradeFailed, 98 ServerClosedSocket, 99 }; 100 101 virtual Web::WebSockets::WebSocket::ReadyState ready_state() = 0; 102 virtual DeprecatedString subprotocol_in_use() = 0; 103 104 virtual void send(ByteBuffer binary_or_text_message, bool is_text) = 0; 105 virtual void send(StringView text_message) = 0; 106 virtual void close(u16 code = 1005, DeprecatedString reason = {}) = 0; 107 108 Function<void()> on_open; 109 Function<void(Message)> on_message; 110 Function<void(Error)> on_error; 111 Function<void(u16 code, DeprecatedString reason, bool was_clean)> on_close; 112 Function<CertificateAndKey()> on_certificate_requested; 113 114protected: 115 explicit WebSocketClientSocket(); 116}; 117 118class WebSocketClientManager : public Core::Object { 119 C_OBJECT_ABSTRACT(WebSocketClientManager) 120public: 121 static void initialize(RefPtr<WebSocketClientManager>); 122 static WebSocketClientManager& the(); 123 124 virtual RefPtr<WebSocketClientSocket> connect(AK::URL const&, DeprecatedString const& origin, Vector<DeprecatedString> const& protocols) = 0; 125 126protected: 127 explicit WebSocketClientManager(); 128}; 129 130}