Home | History | Annotate | Download | only in page
      1 /*
      2  * Copyright (C) 2009 Ericsson AB
      3  * 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 #if ENABLE(EVENTSOURCE)
     36 
     37 #include "ActiveDOMObject.h"
     38 #include "AtomicStringHash.h"
     39 #include "EventNames.h"
     40 #include "EventTarget.h"
     41 #include "KURL.h"
     42 #include "ThreadableLoaderClient.h"
     43 #include "Timer.h"
     44 
     45 #include <wtf/HashMap.h>
     46 #include <wtf/PassRefPtr.h>
     47 #include <wtf/RefPtr.h>
     48 #include <wtf/Vector.h>
     49 
     50 namespace WebCore {
     51 
     52     class MessageEvent;
     53     class ResourceResponse;
     54     class TextResourceDecoder;
     55     class ThreadableLoader;
     56 
     57     class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
     58     public:
     59         static PassRefPtr<EventSource> create(const String& url, ScriptExecutionContext* context, ExceptionCode& ec) { return adoptRef(new EventSource(url, context, ec)); }
     60         virtual ~EventSource();
     61 
     62         static const unsigned long long defaultReconnectDelay;
     63 
     64         String url() const;
     65 
     66         enum State {
     67             CONNECTING = 0,
     68             OPEN = 1,
     69             CLOSED = 2,
     70         };
     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         using RefCounted<EventSource>::ref;
     81         using RefCounted<EventSource>::deref;
     82 
     83         virtual EventSource* toEventSource() { return this; }
     84         virtual ScriptExecutionContext* scriptExecutionContext() const;
     85 
     86         virtual void stop();
     87 
     88     private:
     89         EventSource(const String& url, ScriptExecutionContext* context, ExceptionCode& ec);
     90 
     91         virtual void refEventTarget() { ref(); }
     92         virtual void derefEventTarget() { deref(); }
     93         virtual EventTargetData* eventTargetData();
     94         virtual EventTargetData* ensureEventTargetData();
     95 
     96         virtual void didReceiveResponse(const ResourceResponse& response);
     97         virtual void didReceiveData(const char* data, int length);
     98         virtual void didFinishLoading(unsigned long);
     99         virtual void didFail(const ResourceError& error);
    100         virtual void didFailRedirectCheck();
    101 
    102         void connect();
    103         void endRequest();
    104         void scheduleReconnect();
    105         void reconnectTimerFired(Timer<EventSource>*);
    106         void parseEventStream();
    107         void parseEventStreamLine(unsigned int pos, int fieldLength, int lineLength);
    108         PassRefPtr<MessageEvent> createMessageEvent();
    109 
    110         KURL m_url;
    111         State m_state;
    112 
    113         RefPtr<TextResourceDecoder> m_decoder;
    114         RefPtr<ThreadableLoader> m_loader;
    115         Timer<EventSource> m_reconnectTimer;
    116         Vector<UChar> m_receiveBuf;
    117         bool m_discardTrailingNewline;
    118         bool m_failSilently;
    119         bool m_requestInFlight;
    120 
    121         String m_eventName;
    122         Vector<UChar> m_data;
    123         String m_lastEventId;
    124         unsigned long long m_reconnectDelay;
    125         String m_origin;
    126 
    127         EventTargetData m_eventTargetData;
    128     };
    129 
    130 } // namespace WebCore
    131 
    132 #endif // ENABLE(EVENTSOURCE)
    133 
    134 #endif // EventSource_h
    135