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 "bindings/v8/ScriptWrappable.h" 36 #include "core/dom/ActiveDOMObject.h" 37 #include "core/events/EventTarget.h" 38 #include "core/loader/ThreadableLoaderClient.h" 39 #include "platform/Timer.h" 40 #include "platform/heap/Handle.h" 41 #include "platform/weborigin/KURL.h" 42 #include "wtf/RefPtr.h" 43 #include "wtf/Vector.h" 44 45 namespace WebCore { 46 47 class Dictionary; 48 class ExceptionState; 49 class MessageEvent; 50 class ResourceResponse; 51 class TextResourceDecoder; 52 class ThreadableLoader; 53 54 class EventSource FINAL : public RefCountedWillBeRefCountedGarbageCollected<EventSource>, public ScriptWrappable, public EventTargetWithInlineData, private ThreadableLoaderClient, public ActiveDOMObject { 55 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED; 56 REFCOUNTED_EVENT_TARGET(EventSource); 57 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventSource); 58 public: 59 static PassRefPtrWillBeRawPtr<EventSource> create(ExecutionContext*, const String& url, const Dictionary&, ExceptionState&); 60 virtual ~EventSource(); 61 62 static const unsigned long long defaultReconnectDelay; 63 64 String url() const; 65 bool withCredentials() const; 66 67 typedef short State; 68 static const State CONNECTING = 0; 69 static const State OPEN = 1; 70 static const State CLOSED = 2; 71 72 State readyState() const; 73 74 DEFINE_ATTRIBUTE_EVENT_LISTENER(open); 75 DEFINE_ATTRIBUTE_EVENT_LISTENER(message); 76 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); 77 78 void close(); 79 80 virtual const AtomicString& interfaceName() const OVERRIDE; 81 virtual ExecutionContext* executionContext() const OVERRIDE; 82 83 // ActiveDOMObject 84 // 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; 90 91 private: 92 EventSource(ExecutionContext*, const KURL&, const Dictionary&); 93 94 virtual void didReceiveResponse(unsigned long, const ResourceResponse&) OVERRIDE; 95 virtual void didReceiveData(const char*, int) OVERRIDE; 96 virtual void didFinishLoading(unsigned long, double) OVERRIDE; 97 virtual void didFail(const ResourceError&) OVERRIDE; 98 virtual void didFailAccessControlCheck(const ResourceError&) OVERRIDE; 99 virtual void didFailRedirectCheck() OVERRIDE; 100 101 void scheduleInitialConnect(); 102 void connect(); 103 void networkRequestEnded(); 104 void scheduleReconnect(); 105 void connectTimerFired(Timer<EventSource>*); 106 void abortConnectionAttempt(); 107 void parseEventStream(); 108 void parseEventStreamLine(unsigned pos, int fieldLength, int lineLength); 109 PassRefPtrWillBeRawPtr<MessageEvent> createMessageEvent(); 110 111 KURL m_url; 112 bool m_withCredentials; 113 State m_state; 114 115 OwnPtr<TextResourceDecoder> m_decoder; 116 RefPtr<ThreadableLoader> m_loader; 117 Timer<EventSource> m_connectTimer; 118 Vector<UChar> m_receiveBuf; 119 bool m_discardTrailingNewline; 120 bool m_requestInFlight; 121 122 AtomicString m_eventName; 123 Vector<UChar> m_data; 124 AtomicString m_currentlyParsedEventId; 125 AtomicString m_lastEventId; 126 unsigned long long m_reconnectDelay; 127 String m_eventStreamOrigin; 128 }; 129 130 } // namespace WebCore 131 132 #endif // EventSource_h 133