WebSocket.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Object.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. public:
  27. enum class ReadyState : u16 {
  28. Connecting = 0,
  29. Open = 1,
  30. Closing = 2,
  31. Closed = 3,
  32. };
  33. static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, String const& url, Optional<Variant<String, Vector<String>>> const& protocols);
  34. virtual ~WebSocket() override;
  35. WebIDL::ExceptionOr<String> url() const { return TRY_OR_THROW_OOM(vm(), m_url.to_string()); }
  36. void set_url(AK::URL url) { m_url = move(url); }
  37. #undef __ENUMERATE
  38. #define __ENUMERATE(attribute_name, event_name) \
  39. void set_##attribute_name(WebIDL::CallbackType*); \
  40. WebIDL::CallbackType* attribute_name();
  41. ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
  42. #undef __ENUMERATE
  43. ReadyState ready_state() const;
  44. String extensions() const;
  45. WebIDL::ExceptionOr<String> protocol() const;
  46. String const& binary_type() { return m_binary_type; }
  47. void set_binary_type(String const& type) { m_binary_type = type; }
  48. WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<String> reason);
  49. WebIDL::ExceptionOr<void> send(Variant<JS::Handle<JS::Object>, JS::Handle<FileAPI::Blob>, String> const& data);
  50. private:
  51. void on_open();
  52. void on_message(ByteBuffer message, bool is_text);
  53. void on_error();
  54. void on_close(u16 code, String reason, bool was_clean);
  55. WebSocket(JS::Realm&);
  56. virtual JS::ThrowCompletionOr<void> initialize(JS::Realm&) override;
  57. ErrorOr<void> establish_web_socket_connection(AK::URL& url_record, Vector<String>& protocols, HTML::EnvironmentSettingsObject& client);
  58. AK::URL m_url;
  59. String m_binary_type { "blob"_string.release_value_but_fixme_should_propagate_errors() };
  60. RefPtr<WebSocketClientSocket> m_websocket;
  61. };
  62. class WebSocketClientSocket : public RefCounted<WebSocketClientSocket> {
  63. public:
  64. virtual ~WebSocketClientSocket();
  65. struct CertificateAndKey {
  66. DeprecatedString certificate;
  67. DeprecatedString key;
  68. };
  69. struct Message {
  70. ByteBuffer data;
  71. bool is_text { false };
  72. };
  73. enum class Error {
  74. CouldNotEstablishConnection,
  75. ConnectionUpgradeFailed,
  76. ServerClosedSocket,
  77. };
  78. virtual Web::WebSockets::WebSocket::ReadyState ready_state() = 0;
  79. virtual DeprecatedString subprotocol_in_use() = 0;
  80. virtual void send(ByteBuffer binary_or_text_message, bool is_text) = 0;
  81. virtual void send(StringView text_message) = 0;
  82. virtual void close(u16 code = 1005, DeprecatedString reason = {}) = 0;
  83. Function<void()> on_open;
  84. Function<void(Message)> on_message;
  85. Function<void(Error)> on_error;
  86. Function<void(u16 code, DeprecatedString reason, bool was_clean)> on_close;
  87. Function<CertificateAndKey()> on_certificate_requested;
  88. protected:
  89. explicit WebSocketClientSocket();
  90. };
  91. class WebSocketClientManager : public Core::Object {
  92. C_OBJECT_ABSTRACT(WebSocketClientManager)
  93. public:
  94. static void initialize(RefPtr<WebSocketClientManager>);
  95. static WebSocketClientManager& the();
  96. virtual RefPtr<WebSocketClientSocket> connect(AK::URL const&, DeprecatedString const& origin, Vector<DeprecatedString> const& protocols) = 0;
  97. protected:
  98. explicit WebSocketClientManager();
  99. };
  100. }