Home | History | Annotate | Download | only in page
      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 "core/dom/ActiveDOMObject.h"
     36 #include "core/events/EventTarget.h"
     37 #include "core/loader/ThreadableLoaderClient.h"
     38 #include "platform/Timer.h"
     39 #include "platform/heap/Handle.h"
     40 #include "platform/weborigin/KURL.h"
     41 #include "wtf/RefPtr.h"
     42 #include "wtf/Vector.h"
     43 
     44 namespace blink {
     45 
     46 class EventSourceInit;
     47 class ExceptionState;
     48 class MessageEvent;
     49 class ResourceResponse;
     50 class TextResourceDecoder;
     51 class ThreadableLoader;
     52 
     53 class EventSource FINAL : public RefCountedWillBeGarbageCollectedFinalized<EventSource>, public EventTargetWithInlineData, private ThreadableLoaderClient, public ActiveDOMObject {
     54     DEFINE_WRAPPERTYPEINFO();
     55     REFCOUNTED_EVENT_TARGET(EventSource);
     56     WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventSource);
     57     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
     58 public:
     59     static PassRefPtrWillBeRawPtr<EventSource> create(ExecutionContext*, const String& url, const EventSourceInit&, 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     virtual bool hasPendingActivity() const OVERRIDE;
     92 
     93 private:
     94     EventSource(ExecutionContext*, const KURL&, const EventSourceInit&);
     95 
     96     virtual void didReceiveResponse(unsigned long, const ResourceResponse&) OVERRIDE;
     97     virtual void didReceiveData(const char*, int) OVERRIDE;
     98     virtual void didFinishLoading(unsigned long, double) OVERRIDE;
     99     virtual void didFail(const ResourceError&) OVERRIDE;
    100     virtual void didFailAccessControlCheck(const ResourceError&) OVERRIDE;
    101     virtual void didFailRedirectCheck() OVERRIDE;
    102 
    103     void scheduleInitialConnect();
    104     void connect();
    105     void networkRequestEnded();
    106     void scheduleReconnect();
    107     void connectTimerFired(Timer<EventSource>*);
    108     void abortConnectionAttempt();
    109     void parseEventStream();
    110     void parseEventStreamLine(unsigned pos, int fieldLength, int lineLength);
    111     PassRefPtrWillBeRawPtr<MessageEvent> createMessageEvent();
    112 
    113     KURL m_url;
    114     bool m_withCredentials;
    115     State m_state;
    116 
    117     OwnPtr<TextResourceDecoder> m_decoder;
    118     RefPtr<ThreadableLoader> m_loader;
    119     Timer<EventSource> m_connectTimer;
    120     Vector<UChar> m_receiveBuf;
    121     bool m_discardTrailingNewline;
    122     bool m_requestInFlight;
    123 
    124     AtomicString m_eventName;
    125     Vector<UChar> m_data;
    126     AtomicString m_currentlyParsedEventId;
    127     AtomicString m_lastEventId;
    128     unsigned long long m_reconnectDelay;
    129     String m_eventStreamOrigin;
    130 };
    131 
    132 } // namespace blink
    133 
    134 #endif // EventSource_h
    135