WebSocket.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2021-2022, Dex♪ <dexes.ttp@gmail.com>
  3. * Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/ByteBuffer.h>
  9. #include <AK/URL.h>
  10. #include <LibCore/EventReceiver.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/DOM/EventTarget.h>
  13. #include <LibWeb/Forward.h>
  14. #include <LibWeb/HTML/Window.h>
  15. #include <LibWeb/WebIDL/ExceptionOr.h>
  16. #define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \
  17. E(onerror, HTML::EventNames::error) \
  18. E(onclose, HTML::EventNames::close) \
  19. E(onopen, HTML::EventNames::open) \
  20. E(onmessage, HTML::EventNames::message)
  21. namespace Web::WebSockets {
  22. class WebSocketClientSocket;
  23. class WebSocketClientManager;
  24. class WebSocket final : public DOM::EventTarget {
  25. WEB_PLATFORM_OBJECT(WebSocket, DOM::EventTarget);
  26. JS_DECLARE_ALLOCATOR(WebSocket);
  27. public:
  28. enum class ReadyState : u16 {
  29. Connecting = 0,
  30. Open = 1,
  31. Closing = 2,
  32. Closed = 3,
  33. };
  34. static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, String const& url, Optional<Variant<String, Vector<String>>> const& protocols);
  35. virtual ~WebSocket() override;
  36. WebIDL::ExceptionOr<String> url() const { return TRY_OR_THROW_OOM(vm(), m_url.to_string()); }
  37. void set_url(URL url) { m_url = move(url); }
  38. #undef __ENUMERATE
  39. #define __ENUMERATE(attribute_name, event_name) \
  40. void set_##attribute_name(WebIDL::CallbackType*); \
  41. WebIDL::CallbackType* attribute_name();
  42. ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
  43. #undef __ENUMERATE
  44. ReadyState ready_state() const;
  45. String extensions() const;
  46. WebIDL::ExceptionOr<String> protocol() const;
  47. String const& binary_type() { return m_binary_type; }
  48. void set_binary_type(String const& type) { m_binary_type = type; }
  49. WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<String> reason);
  50. WebIDL::ExceptionOr<void> send(Variant<JS::Handle<WebIDL::BufferSource>, JS::Handle<FileAPI::Blob>, String> const& data);
  51. private:
  52. void on_open();
  53. void on_message(ByteBuffer message, bool is_text);
  54. void on_error();
  55. void on_close(u16 code, String reason, bool was_clean);
  56. WebSocket(JS::Realm&);
  57. virtual void initialize(JS::Realm&) override;
  58. ErrorOr<void> establish_web_socket_connection(URL& url_record, Vector<String>& protocols, HTML::EnvironmentSettingsObject& client);
  59. URL m_url;
  60. String m_binary_type { "blob"_string };
  61. RefPtr<WebSocketClientSocket> m_websocket;
  62. };
  63. class WebSocketClientSocket : public RefCounted<WebSocketClientSocket> {
  64. public:
  65. virtual ~WebSocketClientSocket();
  66. struct CertificateAndKey {
  67. ByteString certificate;
  68. ByteString key;
  69. };
  70. struct Message {
  71. ByteBuffer data;
  72. bool is_text { false };
  73. };
  74. enum class Error {
  75. CouldNotEstablishConnection,
  76. ConnectionUpgradeFailed,
  77. ServerClosedSocket,
  78. };
  79. virtual Web::WebSockets::WebSocket::ReadyState ready_state() = 0;
  80. virtual ByteString subprotocol_in_use() = 0;
  81. virtual void send(ByteBuffer binary_or_text_message, bool is_text) = 0;
  82. virtual void send(StringView text_message) = 0;
  83. virtual void close(u16 code = 1005, ByteString reason = {}) = 0;
  84. Function<void()> on_open;
  85. Function<void(Message)> on_message;
  86. Function<void(Error)> on_error;
  87. Function<void(u16 code, ByteString reason, bool was_clean)> on_close;
  88. Function<CertificateAndKey()> on_certificate_requested;
  89. protected:
  90. explicit WebSocketClientSocket() = default;
  91. };
  92. }