EventSource.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/String.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/StringView.h>
  10. #include <AK/Time.h>
  11. #include <LibGC/Ptr.h>
  12. #include <LibJS/Forward.h>
  13. #include <LibURL/URL.h>
  14. #include <LibWeb/DOM/EventTarget.h>
  15. #include <LibWeb/Forward.h>
  16. #include <LibWeb/WebIDL/ExceptionOr.h>
  17. #include <LibWeb/WebIDL/Types.h>
  18. namespace Web::HTML {
  19. struct EventSourceInit {
  20. bool with_credentials { false };
  21. };
  22. class EventSource : public DOM::EventTarget {
  23. WEB_PLATFORM_OBJECT(EventSource, DOM::EventTarget);
  24. GC_DECLARE_ALLOCATOR(EventSource);
  25. public:
  26. virtual ~EventSource() override;
  27. static WebIDL::ExceptionOr<GC::Ref<EventSource>> construct_impl(JS::Realm&, StringView url, EventSourceInit event_source_init_dict = {});
  28. // https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-url
  29. String url() const { return MUST(String::from_byte_string(m_url.serialize())); }
  30. // https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-withcredentials
  31. bool with_credentials() const { return m_with_credentials; }
  32. enum class ReadyState : WebIDL::UnsignedShort {
  33. Connecting = 0,
  34. Open = 1,
  35. Closed = 2,
  36. };
  37. // https://html.spec.whatwg.org/multipage/server-sent-events.html#dom-eventsource-readystate
  38. ReadyState ready_state() const { return m_ready_state; }
  39. void set_onopen(WebIDL::CallbackType*);
  40. WebIDL::CallbackType* onopen();
  41. void set_onmessage(WebIDL::CallbackType*);
  42. WebIDL::CallbackType* onmessage();
  43. void set_onerror(WebIDL::CallbackType*);
  44. WebIDL::CallbackType* onerror();
  45. void close();
  46. void forcibly_close();
  47. private:
  48. explicit EventSource(JS::Realm&);
  49. virtual void initialize(JS::Realm&) override;
  50. virtual void finalize() override;
  51. virtual void visit_edges(Cell::Visitor&) override;
  52. void announce_the_connection();
  53. void reestablish_the_connection();
  54. void fail_the_connection();
  55. void interpret_response(StringView);
  56. void process_field(StringView field, StringView value);
  57. void dispatch_the_event();
  58. // https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-eventsource-url
  59. URL::URL m_url;
  60. // https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-request
  61. GC::Ptr<Fetch::Infrastructure::Request> m_request;
  62. // https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-reconnection-time
  63. AK::Duration m_reconnection_time { AK::Duration::from_seconds(3) };
  64. // https://html.spec.whatwg.org/multipage/server-sent-events.html#concept-event-stream-last-event-id
  65. String m_last_event_id;
  66. String m_event_type;
  67. StringBuilder m_data;
  68. bool m_with_credentials { false };
  69. ReadyState m_ready_state { ReadyState::Connecting };
  70. GC::Ptr<Fetch::Infrastructure::FetchAlgorithms> m_fetch_algorithms;
  71. GC::Ptr<Fetch::Infrastructure::FetchController> m_fetch_controller;
  72. };
  73. }