WebSocket.h 3.9 KB

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