1 | /* |
2 | * Copyright (C) 2009, 2012 Ericsson AB. All rights reserved. |
3 | * Copyright (C) 2010 Apple Inc. All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * 1. Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * 2. Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer |
13 | * in the documentation and/or other materials provided with the |
14 | * distribution. |
15 | * 3. Neither the name of Ericsson nor the names of its contributors |
16 | * may be used to endorse or promote products derived from this |
17 | * software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | */ |
31 | |
32 | #pragma once |
33 | |
34 | #include "ActiveDOMObject.h" |
35 | #include "EventTarget.h" |
36 | #include "ExceptionOr.h" |
37 | #include <wtf/URL.h> |
38 | #include "ThreadableLoaderClient.h" |
39 | #include "Timer.h" |
40 | #include <wtf/Vector.h> |
41 | |
42 | namespace WebCore { |
43 | |
44 | class MessageEvent; |
45 | class TextResourceDecoder; |
46 | class ThreadableLoader; |
47 | |
48 | class EventSource final : public RefCounted<EventSource>, public EventTargetWithInlineData, private ThreadableLoaderClient, public ActiveDOMObject { |
49 | WTF_MAKE_ISO_ALLOCATED(EventSource); |
50 | public: |
51 | struct Init { |
52 | bool withCredentials; |
53 | }; |
54 | static ExceptionOr<Ref<EventSource>> create(ScriptExecutionContext&, const String& url, const Init&); |
55 | virtual ~EventSource(); |
56 | |
57 | const String& url() const; |
58 | bool withCredentials() const; |
59 | |
60 | using State = short; |
61 | static const State CONNECTING = 0; |
62 | static const State OPEN = 1; |
63 | static const State CLOSED = 2; |
64 | |
65 | State readyState() const; |
66 | |
67 | void close(); |
68 | |
69 | using RefCounted::ref; |
70 | using RefCounted::deref; |
71 | |
72 | private: |
73 | EventSource(ScriptExecutionContext&, const URL&, const Init&); |
74 | |
75 | EventTargetInterface eventTargetInterface() const final { return EventSourceEventTargetInterfaceType; } |
76 | ScriptExecutionContext* scriptExecutionContext() const final { return ActiveDOMObject::scriptExecutionContext(); } |
77 | |
78 | void refEventTarget() final { ref(); } |
79 | void derefEventTarget() final { deref(); } |
80 | |
81 | // ThreadableLoaderClient |
82 | void didReceiveResponse(unsigned long, const ResourceResponse&) final; |
83 | void didReceiveData(const char*, int) final; |
84 | void didFinishLoading(unsigned long) final; |
85 | void didFail(const ResourceError&) final; |
86 | |
87 | void stop() final; |
88 | const char* activeDOMObjectName() const final; |
89 | bool canSuspendForDocumentSuspension() const final; |
90 | |
91 | void connect(); |
92 | void networkRequestEnded(); |
93 | void scheduleInitialConnect(); |
94 | void scheduleReconnect(); |
95 | void abortConnectionAttempt(); |
96 | void parseEventStream(); |
97 | void parseEventStreamLine(unsigned position, Optional<unsigned> fieldLength, unsigned lineLength); |
98 | void dispatchMessageEvent(); |
99 | |
100 | bool responseIsValid(const ResourceResponse&) const; |
101 | |
102 | static const uint64_t defaultReconnectDelay; |
103 | |
104 | URL m_url; |
105 | bool m_withCredentials; |
106 | State m_state { CONNECTING }; |
107 | |
108 | Ref<TextResourceDecoder> m_decoder; |
109 | RefPtr<ThreadableLoader> m_loader; |
110 | Timer m_connectTimer; |
111 | Vector<UChar> m_receiveBuffer; |
112 | bool m_discardTrailingNewline { false }; |
113 | bool m_requestInFlight { false }; |
114 | |
115 | AtomicString m_eventName; |
116 | Vector<UChar> m_data; |
117 | String m_currentlyParsedEventId; |
118 | String m_lastEventId; |
119 | uint64_t m_reconnectDelay { defaultReconnectDelay }; |
120 | String m_eventStreamOrigin; |
121 | }; |
122 | |
123 | inline const String& EventSource::url() const |
124 | { |
125 | return m_url.string(); |
126 | } |
127 | |
128 | inline bool EventSource::withCredentials() const |
129 | { |
130 | return m_withCredentials; |
131 | } |
132 | |
133 | inline EventSource::State EventSource::readyState() const |
134 | { |
135 | return m_state; |
136 | } |
137 | |
138 | } // namespace WebCore |
139 | |