2 * Copyright (C) 2009 Ericsson AB
4 * Copyright (C) 2010 Apple Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
16 * 3. Neither the name of Ericsson nor the names of its contributors
17 * may be used to endorse or promote products derived from this
18 * software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 #include "ActiveDOMObject.h"
37 #include "EventTarget.h"
39 #include "ThreadableLoaderClient.h"
41 #include <wtf/RefPtr.h>
42 #include <wtf/Vector.h>
47 class ResourceResponse;
48 class TextResourceDecoder;
49 class ThreadableLoader;
51 class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
52 WTF_MAKE_FAST_ALLOCATED;
54 static PassRefPtr<EventSource> create(ScriptExecutionContext*, const String& url, ExceptionCode&);
55 virtual ~EventSource();
57 static const unsigned long long defaultReconnectDelay;
67 State readyState() const;
69 DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
70 DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
71 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
75 using RefCounted<EventSource>::ref;
76 using RefCounted<EventSource>::deref;
78 virtual const AtomicString& interfaceName() const;
79 virtual ScriptExecutionContext* scriptExecutionContext() const;
84 EventSource(const KURL&, ScriptExecutionContext*);
86 virtual void refEventTarget() { ref(); }
87 virtual void derefEventTarget() { deref(); }
88 virtual EventTargetData* eventTargetData();
89 virtual EventTargetData* ensureEventTargetData();
91 virtual void didReceiveResponse(unsigned long, const ResourceResponse&);
92 virtual void didReceiveData(const char*, int);
93 virtual void didFinishLoading(unsigned long, double);
94 virtual void didFail(const ResourceError&);
95 virtual void didFailRedirectCheck();
98 void networkRequestEnded();
99 void scheduleReconnect();
100 void reconnectTimerFired(Timer<EventSource>*);
101 void parseEventStream();
102 void parseEventStreamLine(unsigned int pos, int fieldLength, int lineLength);
103 PassRefPtr<MessageEvent> createMessageEvent();
108 RefPtr<TextResourceDecoder> m_decoder;
109 RefPtr<ThreadableLoader> m_loader;
110 Timer<EventSource> m_reconnectTimer;
111 Vector<UChar> m_receiveBuf;
112 bool m_discardTrailingNewline;
113 bool m_requestInFlight;
116 Vector<UChar> m_data;
117 String m_currentlyParsedEventId;
118 String m_lastEventId;
119 unsigned long long m_reconnectDelay;
122 EventTargetData m_eventTargetData;
125 } // namespace WebCore
127 #endif // EventSource_h