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#ifndef EventSource_h 33#define EventSource_h 34 35#include "ActiveDOMObject.h" 36#include "EventTarget.h" 37#include "KURL.h" 38#include "ThreadableLoaderClient.h" 39#include "Timer.h" 40#include <wtf/RefPtr.h> 41#include <wtf/Vector.h> 42 43namespace WebCore { 44 45class Dictionary; 46class MessageEvent; 47class ResourceResponse; 48class TextResourceDecoder; 49class ThreadableLoader; 50 51class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject { 52 WTF_MAKE_FAST_ALLOCATED; 53public: 54 static PassRefPtr<EventSource> create(ScriptExecutionContext*, const String& url, const Dictionary&, ExceptionCode&); 55 virtual ~EventSource(); 56 57 static const unsigned long long defaultReconnectDelay; 58 59 String url() const; 60 bool withCredentials() const; 61 62 typedef short State; 63 static const State CONNECTING = 0; 64 static const State OPEN = 1; 65 static const State CLOSED = 2; 66 67 State readyState() const; 68 69 DEFINE_ATTRIBUTE_EVENT_LISTENER(open); 70 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 71 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 72 73 void close(); 74 75 using RefCounted<EventSource>::ref; 76 using RefCounted<EventSource>::deref; 77 78 virtual const AtomicString& interfaceName() const; 79 virtual ScriptExecutionContext* scriptExecutionContext() const; 80 81 virtual void stop(); 82 83private: 84 EventSource(ScriptExecutionContext*, const KURL&, const Dictionary&); 85 86 virtual void refEventTarget() { ref(); } 87 virtual void derefEventTarget() { deref(); } 88 virtual EventTargetData* eventTargetData(); 89 virtual EventTargetData* ensureEventTargetData(); 90 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 didFailAccessControlCheck(const ResourceError&); 96 virtual void didFailRedirectCheck(); 97 98 void connect(); 99 void networkRequestEnded(); 100 void scheduleInitialConnect(); 101 void scheduleReconnect(); 102 void connectTimerFired(Timer<EventSource>*); 103 void abortConnectionAttempt(); 104 void parseEventStream(); 105 void parseEventStreamLine(unsigned pos, int fieldLength, int lineLength); 106 PassRefPtr<MessageEvent> createMessageEvent(); 107 108 KURL m_url; 109 bool m_withCredentials; 110 State m_state; 111 112 RefPtr<TextResourceDecoder> m_decoder; 113 RefPtr<ThreadableLoader> m_loader; 114 Timer<EventSource> m_connectTimer; 115 Vector<UChar> m_receiveBuf; 116 bool m_discardTrailingNewline; 117 bool m_requestInFlight; 118 119 String m_eventName; 120 Vector<UChar> m_data; 121 String m_currentlyParsedEventId; 122 String m_lastEventId; 123 unsigned long long m_reconnectDelay; 124 String m_eventStreamOrigin; 125 126 EventTargetData m_eventTargetData; 127}; 128 129} // namespace WebCore 130 131#endif // EventSource_h 132