WebSocket.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2021, 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 Protocol {
  23. class WebSocketClient;
  24. class WebSocket;
  25. }
  26. namespace Web::HTML {
  27. class WebSocketClientManager : public Core::Object {
  28. C_OBJECT(WebSocketClientManager)
  29. public:
  30. static WebSocketClientManager& the();
  31. RefPtr<Protocol::WebSocket> connect(const URL&);
  32. private:
  33. WebSocketClientManager();
  34. RefPtr<Protocol::WebSocketClient> m_websocket_client;
  35. };
  36. class WebSocket final
  37. : public RefCounted<WebSocket>
  38. , public Weakable<WebSocket>
  39. , public DOM::EventTarget
  40. , public Bindings::Wrappable {
  41. public:
  42. enum class ReadyState : u16 {
  43. Connecting = 0,
  44. Open = 1,
  45. Closing = 2,
  46. Closed = 3,
  47. };
  48. using WrapperType = Bindings::WebSocketWrapper;
  49. static NonnullRefPtr<WebSocket> create(DOM::Window& window, URL& url)
  50. {
  51. return adopt_ref(*new WebSocket(window, url));
  52. }
  53. static DOM::ExceptionOr<NonnullRefPtr<WebSocket>> create_with_global_object(Bindings::WindowObject& window, const String& url);
  54. virtual ~WebSocket() override;
  55. using RefCounted::ref;
  56. using RefCounted::unref;
  57. String url() const { return m_url.to_string(); }
  58. #undef __ENUMERATE
  59. #define __ENUMERATE(attribute_name, event_name) \
  60. void set_##attribute_name(HTML::EventHandler); \
  61. HTML::EventHandler attribute_name();
  62. ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
  63. #undef __ENUMERATE
  64. void set_event_handler_attribute(const FlyString& name, HTML::EventHandler);
  65. HTML::EventHandler get_event_handler_attribute(const FlyString& name);
  66. ReadyState ready_state() const;
  67. String extensions() const;
  68. String protocol() const;
  69. const String& binary_type() { return m_binary_type; };
  70. void set_binary_type(const String& type) { m_binary_type = type; };
  71. DOM::ExceptionOr<void> close(u16 code, const String& reason);
  72. DOM::ExceptionOr<void> send(const String& data);
  73. private:
  74. virtual void ref_event_target() override { ref(); }
  75. virtual void unref_event_target() override { unref(); }
  76. virtual bool dispatch_event(NonnullRefPtr<DOM::Event>) override;
  77. virtual JS::Object* create_wrapper(JS::GlobalObject&) override;
  78. void on_open();
  79. void on_message(ByteBuffer message, bool is_text);
  80. void on_error();
  81. void on_close(u16 code, String reason, bool was_clean);
  82. explicit WebSocket(DOM::Window&, URL&);
  83. NonnullRefPtr<DOM::Window> m_window;
  84. URL m_url;
  85. String m_binary_type { "blob" };
  86. RefPtr<Protocol::WebSocket> m_websocket;
  87. };
  88. }