15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/*
2926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles) * Copyright (C) 2009, 2012 Ericsson AB. All rights reserved.
35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2010 Apple Inc. All rights reserved.
45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without
65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions
75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * are met:
85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 1. Redistributions of source code must retain the above copyright
105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer.
115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 2. Redistributions in binary form must reproduce the above copyright
125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    notice, this list of conditions and the following disclaimer
135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    in the documentation and/or other materials provided with the
145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    distribution.
155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 3. Neither the name of Ericsson nor the names of its contributors
165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    may be used to endorse or promote products derived from this
175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *    software without specific prior written permission.
185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) *
195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */
315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
325c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#ifndef EventSource_h
335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#define EventSource_h
345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
3553e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/dom/ActiveDOMObject.h"
3606f816c7c76bc45a15e452ade8a34e8af077693eTorne (Richard Coles)#include "core/events/EventTarget.h"
3753e740f4a82e17f3ae59772501622dc354e42336Torne (Richard Coles)#include "core/loader/ThreadableLoaderClient.h"
381e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)#include "platform/Timer.h"
39f91f5fa1608c2cdd9af1842fb5dadbe78275be2aBo Liu#include "platform/heap/Handle.h"
4051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/weborigin/KURL.h"
41521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include "wtf/RefPtr.h"
42521d96ec04ace82590870fb04353ec4f82bb150fTorne (Richard Coles)#include "wtf/Vector.h"
435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
44c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink {
455c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
467242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucciclass EventSourceInit;
47df95704c49daea886ddad70775bda23618d6274dBen Murdochclass ExceptionState;
48926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class MessageEvent;
49926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class ResourceResponse;
50926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class TextResourceDecoder;
51926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)class ThreadableLoader;
52926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
539e12abdf8c3a23d52091ea54ebb6a04d327f9300Torne (Richard Coles)class EventSource FINAL : public RefCountedWillBeGarbageCollectedFinalized<EventSource>, public EventTargetWithInlineData, private ThreadableLoaderClient, public ActiveDOMObject {
547242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    DEFINE_WRAPPERTYPEINFO();
55d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    REFCOUNTED_EVENT_TARGET(EventSource);
56d6cdb82654e8f3343a693ca752d5c4cee0324e17Torne (Richard Coles)    WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(EventSource);
577242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
58926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)public:
597242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    static PassRefPtrWillBeRawPtr<EventSource> create(ExecutionContext*, const String& url, const EventSourceInit&, ExceptionState&);
60926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    virtual ~EventSource();
61926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
62926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static const unsigned long long defaultReconnectDelay;
63926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
64926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    String url() const;
65926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    bool withCredentials() const;
66926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
67926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    typedef short State;
68926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static const State CONNECTING = 0;
69926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static const State OPEN = 1;
70926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    static const State CLOSED = 2;
71926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
72926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    State readyState() const;
73926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
74926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
75926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
76926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
77926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
78926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void close();
79926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
801e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    virtual const AtomicString& interfaceName() const OVERRIDE;
811e202183a5dc46166763171984b285173f8585e5Torne (Richard Coles)    virtual ExecutionContext* executionContext() const OVERRIDE;
82926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
83bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    // ActiveDOMObject
84bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    //
85d5428f32f5d1719f774f62e19147104ca245a3abTorne (Richard Coles)    // Note: suspend() is noop since ScopedPageLoadDeferrer calls
86bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    // Page::setDefersLoading() and it defers delivery of events from the
87bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    // loader, and therefore the methods of this class for receiving
88bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    // asynchronous events from the loader won't be invoked.
89bfe3590b1806e3ff18f46ee3af5d4b83078f305aTorne (Richard Coles)    virtual void stop() OVERRIDE;
90926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
91197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch    virtual bool hasPendingActivity() const OVERRIDE;
92197021e6b966cfb06891637935ef33fff06433d1Ben Murdoch
93926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)private:
947242dc3dbeb210b5e876a3c42d1ec1a667fc621aPrimiano Tucci    EventSource(ExecutionContext*, const KURL&, const EventSourceInit&);
95926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
9609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didReceiveResponse(unsigned long, const ResourceResponse&) OVERRIDE;
9709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didReceiveData(const char*, int) OVERRIDE;
9809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didFinishLoading(unsigned long, double) OVERRIDE;
9909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didFail(const ResourceError&) OVERRIDE;
10009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didFailAccessControlCheck(const ResourceError&) OVERRIDE;
10109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    virtual void didFailRedirectCheck() OVERRIDE;
102926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
10351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void scheduleInitialConnect();
104926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void connect();
105926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void networkRequestEnded();
106926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void scheduleReconnect();
10751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    void connectTimerFired(Timer<EventSource>*);
108926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void abortConnectionAttempt();
109926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void parseEventStream();
110926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    void parseEventStreamLine(unsigned pos, int fieldLength, int lineLength);
111a9984bf9ddc3cf73fdae3f29134a2bab379e7029Ben Murdoch    PassRefPtrWillBeRawPtr<MessageEvent> createMessageEvent();
112926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
113926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    KURL m_url;
114926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    bool m_withCredentials;
115926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    State m_state;
116926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
11751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    OwnPtr<TextResourceDecoder> m_decoder;
118926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    RefPtr<ThreadableLoader> m_loader;
11951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)    Timer<EventSource> m_connectTimer;
120926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    Vector<UChar> m_receiveBuf;
121926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    bool m_discardTrailingNewline;
122926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    bool m_requestInFlight;
123926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)
12409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    AtomicString m_eventName;
125926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    Vector<UChar> m_data;
12609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    AtomicString m_currentlyParsedEventId;
12709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    AtomicString m_lastEventId;
128926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    unsigned long long m_reconnectDelay;
129926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)    String m_eventStreamOrigin;
130926b001d589ce2f10facb93dd4b87578ea35a855Torne (Richard Coles)};
1315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
132c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink
1335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)
1345c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#endif // EventSource_h
135