WebSocket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/URL.h>
  9. #include <LibCore/Object.h>
  10. #include <LibWeb/Bindings/PlatformObject.h>
  11. #include <LibWeb/DOM/EventTarget.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/HTML/Window.h>
  14. #include <LibWeb/WebIDL/ExceptionOr.h>
  15. #define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \
  16. E(onerror, HTML::EventNames::error) \
  17. E(onclose, HTML::EventNames::close) \
  18. E(onopen, HTML::EventNames::open) \
  19. E(onmessage, HTML::EventNames::message)
  20. namespace Web::WebSockets {
  21. class WebSocketClientSocket;
  22. class WebSocketClientManager;
  23. class WebSocket final : public DOM::EventTarget {
  24. WEB_PLATFORM_OBJECT(WebSocket, DOM::EventTarget);
  25. public:
  26. enum class ReadyState : u16 {
  27. Connecting = 0,
  28. Open = 1,
  29. Closing = 2,
  30. Closed = 3,
  31. };
  32. static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, DeprecatedString const& url, Optional<Variant<DeprecatedString, Vector<DeprecatedString>>> const& protocols);
  33. virtual ~WebSocket() override;
  34. DeprecatedString url() const { return m_url.to_deprecated_string(); }
  35. #undef __ENUMERATE
  36. #define __ENUMERATE(attribute_name, event_name) \
  37. void set_##attribute_name(WebIDL::CallbackType*); \
  38. WebIDL::CallbackType* attribute_name();
  39. ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
  40. #undef __ENUMERATE
  41. ReadyState ready_state() const;
  42. DeprecatedString extensions() const;
  43. DeprecatedString protocol() const;
  44. DeprecatedString const& binary_type() { return m_binary_type; };
  45. void set_binary_type(DeprecatedString const& type) { m_binary_type = type; };
  46. WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<DeprecatedString> reason);
  47. WebIDL::ExceptionOr<void> send(DeprecatedString const& data);
  48. private:
  49. void on_open();
  50. void on_message(ByteBuffer message, bool is_text);
  51. void on_error();
  52. void on_close(u16 code, DeprecatedString reason, bool was_clean);
  53. WebSocket(HTML::Window&, AK::URL&, Vector<DeprecatedString> const& protocols);
  54. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  55. virtual void visit_edges(Cell::Visitor&) override;
  56. JS::NonnullGCPtr<HTML::Window> m_window;
  57. AK::URL m_url;
  58. DeprecatedString m_binary_type { "blob" };
  59. RefPtr<WebSocketClientSocket> m_websocket;
  60. };
  61. class WebSocketClientSocket : public RefCounted<WebSocketClientSocket> {
  62. public:
  63. virtual ~WebSocketClientSocket();
  64. struct CertificateAndKey {
  65. DeprecatedString certificate;
  66. DeprecatedString key;
  67. };
  68. struct Message {
  69. ByteBuffer data;
  70. bool is_text { false };
  71. };
  72. enum class Error {
  73. CouldNotEstablishConnection,
  74. ConnectionUpgradeFailed,
  75. ServerClosedSocket,
  76. };
  77. virtual Web::WebSockets::WebSocket::ReadyState ready_state() = 0;
  78. virtual DeprecatedString subprotocol_in_use() = 0;
  79. virtual void send(ByteBuffer binary_or_text_message, bool is_text) = 0;
  80. virtual void send(StringView text_message) = 0;
  81. virtual void close(u16 code = 1005, DeprecatedString reason = {}) = 0;
  82. Function<void()> on_open;
  83. Function<void(Message)> on_message;
  84. Function<void(Error)> on_error;
  85. Function<void(u16 code, DeprecatedString reason, bool was_clean)> on_close;
  86. Function<CertificateAndKey()> on_certificate_requested;
  87. protected:
  88. explicit WebSocketClientSocket();
  89. };
  90. class WebSocketClientManager : public Core::Object {
  91. C_OBJECT_ABSTRACT(WebSocketClientManager)
  92. public:
  93. static void initialize(RefPtr<WebSocketClientManager>);
  94. static WebSocketClientManager& the();
  95. virtual RefPtr<WebSocketClientSocket> connect(AK::URL const&, DeprecatedString const& origin, Vector<DeprecatedString> const& protocols) = 0;
  96. protected:
  97. explicit WebSocketClientManager();
  98. };
  99. }