Coverage Report

Created: 2026-02-14 08:01

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