2 * Copyright (C) 2009, 2012 Ericsson AB. All rights reserved.
3 * Copyright (C) 2010 Apple Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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.
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.
35 #include "core/dom/ActiveDOMObject.h"
36 #include "core/events/EventTarget.h"
37 #include "core/loader/ThreadableLoaderClient.h"
38 #include "platform/Timer.h"
39 #include "platform/heap/Handle.h"
40 #include "platform/weborigin/KURL.h"
41 #include "wtf/RefPtr.h"
42 #include "wtf/Vector.h"
46 class EventSourceInit;
49 class ResourceResponse;
50 class TextResourceDecoder;
51 class ThreadableLoader;
53 class EventSource final : public RefCountedWillBeGarbageCollectedFinalized<EventSource>, public EventTargetWithInlineData, private ThreadableLoaderClient, public ActiveDOMObject {
54 DEFINE_WRAPPERTYPEINFO();
55 REFCOUNTED_EVENT_TARGET(EventSource);
56 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventSource);
57 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
59 static PassRefPtrWillBeRawPtr<EventSource> create(ExecutionContext*, const String& url, const EventSourceInit&, ExceptionState&);
60 virtual ~EventSource();
62 static const unsigned long long defaultReconnectDelay;
65 bool withCredentials() const;
68 static const State CONNECTING = 0;
69 static const State OPEN = 1;
70 static const State CLOSED = 2;
72 State readyState() const;
74 DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
75 DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
76 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
80 virtual const AtomicString& interfaceName() const override;
81 virtual ExecutionContext* executionContext() const override;
85 // Note: suspend() is noop since ScopedPageLoadDeferrer calls
86 // Page::setDefersLoading() and it defers delivery of events from the
87 // loader, and therefore the methods of this class for receiving
88 // asynchronous events from the loader won't be invoked.
89 virtual void stop() override;
91 virtual bool hasPendingActivity() const override;
94 EventSource(ExecutionContext*, const KURL&, const EventSourceInit&);
96 virtual void didReceiveResponse(unsigned long, const ResourceResponse&, PassOwnPtr<WebDataConsumerHandle>) override;
97 virtual void didReceiveData(const char*, unsigned) override;
98 virtual void didFinishLoading(unsigned long, double) override;
99 virtual void didFail(const ResourceError&) override;
100 virtual void didFailAccessControlCheck(const ResourceError&) override;
101 virtual void didFailRedirectCheck() override;
103 void scheduleInitialConnect();
105 void networkRequestEnded();
106 void scheduleReconnect();
107 void connectTimerFired(Timer<EventSource>*);
108 void abortConnectionAttempt();
109 void parseEventStream();
110 void parseEventStreamLine(unsigned pos, int fieldLength, int lineLength);
111 PassRefPtrWillBeRawPtr<MessageEvent> createMessageEvent();
114 bool m_withCredentials;
117 OwnPtr<TextResourceDecoder> m_decoder;
118 RefPtr<ThreadableLoader> m_loader;
119 Timer<EventSource> m_connectTimer;
120 Vector<UChar> m_receiveBuf;
121 bool m_discardTrailingNewline;
122 bool m_requestInFlight;
124 AtomicString m_eventName;
125 Vector<UChar> m_data;
126 AtomicString m_currentlyParsedEventId;
127 AtomicString m_lastEventId;
128 unsigned long long m_reconnectDelay;
129 String m_eventStreamOrigin;
134 #endif // EventSource_h