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 #if ENABLE(EVENTSOURCE)
38 #include "ActiveDOMObject.h"
39 #include "EventTarget.h"
41 #include "ThreadableLoaderClient.h"
43 #include <wtf/RefPtr.h>
44 #include <wtf/Vector.h>
49 class ResourceResponse;
50 class TextResourceDecoder;
51 class ThreadableLoader;
53 class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
54 WTF_MAKE_FAST_ALLOCATED;
56 static PassRefPtr<EventSource> create(const String& url, ScriptExecutionContext*, ExceptionCode&);
57 virtual ~EventSource();
59 static const unsigned long long defaultReconnectDelay;
69 State readyState() const;
71 DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
72 DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
73 DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
77 using RefCounted<EventSource>::ref;
78 using RefCounted<EventSource>::deref;
80 virtual EventSource* toEventSource() { return this; }
81 virtual ScriptExecutionContext* scriptExecutionContext() const;
86 EventSource(const KURL&, ScriptExecutionContext*);
88 virtual void refEventTarget() { ref(); }
89 virtual void derefEventTarget() { deref(); }
90 virtual EventTargetData* eventTargetData();
91 virtual EventTargetData* ensureEventTargetData();
93 virtual void didReceiveResponse(unsigned long, const ResourceResponse&);
94 virtual void didReceiveData(const char*, int);
95 virtual void didFinishLoading(unsigned long, double);
96 virtual void didFail(const ResourceError&);
97 virtual void didFailRedirectCheck();
100 void networkRequestEnded();
101 void scheduleReconnect();
102 void reconnectTimerFired(Timer<EventSource>*);
103 void parseEventStream();
104 void parseEventStreamLine(unsigned int pos, int fieldLength, int lineLength);
105 PassRefPtr<MessageEvent> createMessageEvent();
110 RefPtr<TextResourceDecoder> m_decoder;
111 RefPtr<ThreadableLoader> m_loader;
112 Timer<EventSource> m_reconnectTimer;
113 Vector<UChar> m_receiveBuf;
114 bool m_discardTrailingNewline;
115 bool m_requestInFlight;
118 Vector<UChar> m_data;
119 String m_lastEventId;
120 unsigned long long m_reconnectDelay;
123 EventTargetData m_eventTargetData;
126 } // namespace WebCore
128 #endif // ENABLE(EVENTSOURCE)
130 #endif // EventSource_h