2021-04-24 13:54:24 +02:00
|
|
|
/*
|
2022-04-30 11:26:21 +02:00
|
|
|
* Copyright (c) 2021-2022, Dex♪ <dexes.ttp@gmail.com>
|
2023-06-24 23:00:09 +02:00
|
|
|
* Copyright (c) 2023, Kenneth Myhra <kennethmyhra@serenityos.org>
|
2021-04-24 13:54:24 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/ByteBuffer.h>
|
2023-08-06 18:09:39 +02:00
|
|
|
#include <LibCore/EventReceiver.h>
|
2024-10-07 16:07:48 -04:00
|
|
|
#include <LibRequests/Forward.h>
|
|
|
|
#include <LibRequests/WebSocket.h>
|
2024-03-18 16:22:27 +13:00
|
|
|
#include <LibURL/URL.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/Bindings/PlatformObject.h>
|
2021-04-24 13:54:24 +02:00
|
|
|
#include <LibWeb/DOM/EventTarget.h>
|
|
|
|
#include <LibWeb/Forward.h>
|
2022-09-25 17:03:42 +01:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-04-24 13:54:24 +02:00
|
|
|
|
|
|
|
#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)
|
|
|
|
|
2022-02-18 17:35:45 +00:00
|
|
|
namespace Web::WebSockets {
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2022-08-28 13:42:07 +02:00
|
|
|
class WebSocket final : public DOM::EventTarget {
|
|
|
|
WEB_PLATFORM_OBJECT(WebSocket, DOM::EventTarget);
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DECLARE_ALLOCATOR(WebSocket);
|
2022-08-28 13:42:07 +02:00
|
|
|
|
2021-04-24 13:54:24 +02:00
|
|
|
public:
|
2024-11-15 04:01:23 +13:00
|
|
|
static WebIDL::ExceptionOr<GC::Ref<WebSocket>> construct_impl(JS::Realm&, String const& url, Optional<Variant<String, Vector<String>>> const& protocols);
|
2021-04-24 13:54:24 +02:00
|
|
|
|
|
|
|
virtual ~WebSocket() override;
|
|
|
|
|
2024-12-03 22:31:33 +13:00
|
|
|
String url() const { return m_url.to_string(); }
|
2024-03-18 16:22:27 +13:00
|
|
|
void set_url(URL::URL url) { m_url = move(url); }
|
2021-04-24 13:54:24 +02:00
|
|
|
|
|
|
|
#undef __ENUMERATE
|
2022-09-24 16:02:41 +01:00
|
|
|
#define __ENUMERATE(attribute_name, event_name) \
|
|
|
|
void set_##attribute_name(WebIDL::CallbackType*); \
|
|
|
|
WebIDL::CallbackType* attribute_name();
|
2021-04-24 13:54:24 +02:00
|
|
|
ENUMERATE_WEBSOCKET_EVENT_HANDLERS(__ENUMERATE)
|
|
|
|
#undef __ENUMERATE
|
|
|
|
|
2024-10-07 16:07:48 -04:00
|
|
|
Requests::WebSocket::ReadyState ready_state() const;
|
2023-06-17 09:18:55 +02:00
|
|
|
String extensions() const;
|
|
|
|
WebIDL::ExceptionOr<String> protocol() const;
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2023-07-07 22:48:11 -04:00
|
|
|
String const& binary_type() { return m_binary_type; }
|
|
|
|
void set_binary_type(String const& type) { m_binary_type = type; }
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2023-06-17 09:18:55 +02:00
|
|
|
WebIDL::ExceptionOr<void> close(Optional<u16> code, Optional<String> reason);
|
2024-11-15 04:01:23 +13:00
|
|
|
WebIDL::ExceptionOr<void> send(Variant<GC::Root<WebIDL::BufferSource>, GC::Root<FileAPI::Blob>, String> const& data);
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2025-02-25 12:41:52 +00:00
|
|
|
void make_disappear();
|
|
|
|
|
2021-04-24 13:54:24 +02:00
|
|
|
private:
|
|
|
|
void on_open();
|
|
|
|
void on_message(ByteBuffer message, bool is_text);
|
|
|
|
void on_error();
|
2023-06-17 09:18:55 +02:00
|
|
|
void on_close(u16 code, String reason, bool was_clean);
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2023-06-24 23:00:09 +02:00
|
|
|
WebSocket(JS::Realm&);
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
virtual void initialize(JS::Realm&) override;
|
2025-02-25 12:36:53 +00:00
|
|
|
virtual void finalize() override;
|
|
|
|
virtual bool must_survive_garbage_collection() const override;
|
2022-08-28 13:42:07 +02:00
|
|
|
|
2025-01-14 14:15:56 -07:00
|
|
|
ErrorOr<void> establish_web_socket_connection(URL::URL const& url_record, Vector<String> const& protocols, HTML::EnvironmentSettingsObject& client);
|
2021-04-24 13:54:24 +02:00
|
|
|
|
2024-03-18 16:22:27 +13:00
|
|
|
URL::URL m_url;
|
2023-08-07 11:12:38 +02:00
|
|
|
String m_binary_type { "blob"_string };
|
2024-10-07 16:07:48 -04:00
|
|
|
RefPtr<Requests::WebSocket> m_websocket;
|
2025-02-25 12:41:52 +00:00
|
|
|
|
|
|
|
IntrusiveListNode<WebSocket> m_list_node;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using List = IntrusiveList<&WebSocket::m_list_node>;
|
2021-04-24 13:54:24 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|