1 /* 2 * Copyright (C) 2009 Ericsson AB 3 * All rights reserved. 4 * Copyright (C) 2010 Apple Inc. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 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 15 * distribution. 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. 19 * 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. 31 */ 32 33 #ifndef EventSource_h 34 #define EventSource_h 35 36 #if ENABLE(EVENTSOURCE) 37 38 #include "ActiveDOMObject.h" 39 #include "EventTarget.h" 40 #include "KURL.h" 41 #include "ThreadableLoaderClient.h" 42 #include "Timer.h" 43 #include <wtf/RefPtr.h> 44 #include <wtf/Vector.h> 45 46 namespace WebCore { 47 48 class MessageEvent; 49 class ResourceResponse; 50 class TextResourceDecoder; 51 class ThreadableLoader; 52 53 class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject { 54 WTF_MAKE_FAST_ALLOCATED; 55 public: 56 static PassRefPtr<EventSource> create(const String& url, ScriptExecutionContext*, ExceptionCode&); 57 virtual ~EventSource(); 58 59 static const unsigned long long defaultReconnectDelay; 60 61 String url() const; 62 63 enum State { 64 CONNECTING = 0, 65 OPEN = 1, 66 CLOSED = 2, 67 }; 68 69 State readyState() const; 70 71 DEFINE_ATTRIBUTE_EVENT_LISTENER(open); 72 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 73 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 74 75 void close(); 76 77 using RefCounted<EventSource>::ref; 78 using RefCounted<EventSource>::deref; 79 80 virtual EventSource* toEventSource() { return this; } 81 virtual ScriptExecutionContext* scriptExecutionContext() const; 82 83 virtual void stop(); 84 85 private: 86 EventSource(const KURL&, ScriptExecutionContext*); 87 88 virtual void refEventTarget() { ref(); } 89 virtual void derefEventTarget() { deref(); } 90 virtual EventTargetData* eventTargetData(); 91 virtual EventTargetData* ensureEventTargetData(); 92 93 virtual void didReceiveResponse(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(); 98 99 void connect(); 100 void endRequest(); 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(); 106 107 KURL m_url; 108 State m_state; 109 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_failSilently; 116 bool m_requestInFlight; 117 118 String m_eventName; 119 Vector<UChar> m_data; 120 String m_lastEventId; 121 unsigned long long m_reconnectDelay; 122 String m_origin; 123 124 EventTargetData m_eventTargetData; 125 }; 126 127 } // namespace WebCore 128 129 #endif // ENABLE(EVENTSOURCE) 130 131 #endif // EventSource_h 132