Merge "[CherryPick] Refactoring: Move the content of HTMLInputElement::subtreeHasChan...
[framework/web/webkit-efl.git] / Source / WebCore / page / EventSource.h
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 #include "ActiveDOMObject.h"
37 #include "EventTarget.h"
38 #include "KURL.h"
39 #include "ThreadableLoaderClient.h"
40 #include "Timer.h"
41 #include <wtf/RefPtr.h>
42 #include <wtf/Vector.h>
43
44 namespace WebCore {
45
46     class MessageEvent;
47     class ResourceResponse;
48     class TextResourceDecoder;
49     class ThreadableLoader;
50
51     class EventSource : public RefCounted<EventSource>, public EventTarget, private ThreadableLoaderClient, public ActiveDOMObject {
52         WTF_MAKE_FAST_ALLOCATED;
53     public:
54         static PassRefPtr<EventSource> create(ScriptExecutionContext*, const String& url, ExceptionCode&);
55         virtual ~EventSource();
56
57         static const unsigned long long defaultReconnectDelay;
58
59         String url() const;
60
61         enum State {
62             CONNECTING = 0,
63             OPEN = 1,
64             CLOSED = 2,
65         };
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
83     private:
84         EventSource(const KURL&, ScriptExecutionContext*);
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 didFailRedirectCheck();
96
97         void connect();
98         void networkRequestEnded();
99         void scheduleReconnect();
100         void reconnectTimerFired(Timer<EventSource>*);
101         void parseEventStream();
102         void parseEventStreamLine(unsigned int pos, int fieldLength, int lineLength);
103         PassRefPtr<MessageEvent> createMessageEvent();
104
105         KURL m_url;
106         State m_state;
107
108         RefPtr<TextResourceDecoder> m_decoder;
109         RefPtr<ThreadableLoader> m_loader;
110         Timer<EventSource> m_reconnectTimer;
111         Vector<UChar> m_receiveBuf;
112         bool m_discardTrailingNewline;
113         bool m_requestInFlight;
114
115         String m_eventName;
116         Vector<UChar> m_data;
117         String m_currentlyParsedEventId;
118         String m_lastEventId;
119         unsigned long long m_reconnectDelay;
120         String m_origin;
121         
122         EventTargetData m_eventTargetData;
123     };
124
125 } // namespace WebCore
126
127 #endif // EventSource_h