| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2021, Dex♪ <dexes.ttp@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/ByteBuffer.h>
 | 
					
						
							|  |  |  | #include <AK/RefCounted.h>
 | 
					
						
							|  |  |  | #include <AK/URL.h>
 | 
					
						
							|  |  |  | #include <AK/Weakable.h>
 | 
					
						
							|  |  |  | #include <LibCore/Object.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/WindowObject.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Wrappable.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/EventTarget.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/ExceptionOr.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENUMERATE_WEBSOCKET_EVENT_HANDLERS(E) \
 | 
					
						
							|  |  |  |     E(onerror, HTML::EventNames::error)       \ | 
					
						
							|  |  |  |     E(onclose, HTML::EventNames::close)       \ | 
					
						
							|  |  |  |     E(onopen, HTML::EventNames::open)         \ | 
					
						
							|  |  |  |     E(onmessage, HTML::EventNames::message) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Protocol { | 
					
						
							|  |  |  | class WebSocketClient; | 
					
						
							|  |  |  | class WebSocket; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-18 17:35:45 +00:00
										 |  |  | namespace Web::WebSockets { | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | class WebSocketClientManager : public Core::Object { | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |     C_OBJECT_ABSTRACT(WebSocketClientManager) | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  | public: | 
					
						
							|  |  |  |     static WebSocketClientManager& the(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     RefPtr<Protocol::WebSocket> connect(const AK::URL&); | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-01-14 13:12:49 +00:00
										 |  |  |     static ErrorOr<NonnullRefPtr<WebSocketClientManager>> try_create(); | 
					
						
							|  |  |  |     WebSocketClientManager(NonnullRefPtr<Protocol::WebSocketClient>); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  |     RefPtr<Protocol::WebSocketClient> m_websocket_client; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class WebSocket final | 
					
						
							|  |  |  |     : public RefCounted<WebSocket> | 
					
						
							|  |  |  |     , public Weakable<WebSocket> | 
					
						
							|  |  |  |     , public DOM::EventTarget | 
					
						
							|  |  |  |     , public Bindings::Wrappable { | 
					
						
							|  |  |  | public: | 
					
						
							|  |  |  |     enum class ReadyState : u16 { | 
					
						
							|  |  |  |         Connecting = 0, | 
					
						
							|  |  |  |         Open = 1, | 
					
						
							|  |  |  |         Closing = 2, | 
					
						
							|  |  |  |         Closed = 3, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     using WrapperType = Bindings::WebSocketWrapper; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     static NonnullRefPtr<WebSocket> create(DOM::Window& window, AK::URL& url) | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  |     { | 
					
						
							|  |  |  |         return adopt_ref(*new WebSocket(window, url)); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     static DOM::ExceptionOr<NonnullRefPtr<WebSocket>> create_with_global_object(Bindings::WindowObject& window, const String& url); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual ~WebSocket() override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     using RefCounted::ref; | 
					
						
							|  |  |  |     using RefCounted::unref; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     String url() const { return m_url.to_string(); } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							| 
									
										
										
										
											2021-10-14 18:03:08 +01:00
										 |  |  | #define __ENUMERATE(attribute_name, event_name)                  \
 | 
					
						
							|  |  |  |     void set_##attribute_name(Optional<Bindings::CallbackType>); \ | 
					
						
							|  |  |  |     Bindings::CallbackType* attribute_name(); | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  |     ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE) | 
					
						
							|  |  |  | #undef __ENUMERATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ReadyState ready_state() const; | 
					
						
							|  |  |  |     String extensions() const; | 
					
						
							|  |  |  |     String protocol() const; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const String& binary_type() { return m_binary_type; }; | 
					
						
							|  |  |  |     void set_binary_type(const String& type) { m_binary_type = type; }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     DOM::ExceptionOr<void> close(u16 code, const String& reason); | 
					
						
							|  |  |  |     DOM::ExceptionOr<void> send(const String& data); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | private: | 
					
						
							|  |  |  |     virtual void ref_event_target() override { ref(); } | 
					
						
							|  |  |  |     virtual void unref_event_target() override { unref(); } | 
					
						
							|  |  |  |     virtual JS::Object* create_wrapper(JS::GlobalObject&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     void on_open(); | 
					
						
							|  |  |  |     void on_message(ByteBuffer message, bool is_text); | 
					
						
							|  |  |  |     void on_error(); | 
					
						
							|  |  |  |     void on_close(u16 code, String reason, bool was_clean); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     explicit WebSocket(DOM::Window&, AK::URL&); | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     NonnullRefPtr<DOM::Window> m_window; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-13 00:33:23 +03:00
										 |  |  |     AK::URL m_url; | 
					
						
							| 
									
										
										
										
											2021-04-24 13:54:24 +02:00
										 |  |  |     String m_binary_type { "blob" }; | 
					
						
							|  |  |  |     RefPtr<Protocol::WebSocket> m_websocket; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |