10bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch/*
20bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * Copyright (C) 2009 Google Inc.  All rights reserved.
30bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *
40bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * Redistribution and use in source and binary forms, with or without
50bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * modification, are permitted provided that the following conditions are
60bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * met:
70bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *
80bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *     * Redistributions of source code must retain the above copyright
90bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * notice, this list of conditions and the following disclaimer.
100bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *     * Redistributions in binary form must reproduce the above
110bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * copyright notice, this list of conditions and the following disclaimer
120bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * in the documentation and/or other materials provided with the
130bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * distribution.
140bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *     * Neither the name of Google Inc. nor the names of its
150bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * contributors may be used to endorse or promote products derived from
160bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * this software without specific prior written permission.
170bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch *
180bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
190bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
200bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
210bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
220bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
230bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
240bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
250bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
260bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
270bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
280bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
290bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch */
300bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
310bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#ifndef WebSocket_h
320bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#define WebSocket_h
330bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
34231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#if ENABLE(WEB_SOCKETS)
35231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
360bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include "ActiveDOMObject.h"
370bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include "EventListener.h"
38231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "EventNames.h"
390bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include "EventTarget.h"
400bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include "KURL.h"
41231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block#include "WebSocketChannelClient.h"
42dd8bb3de4f353a81954234999f1fea748aee2ea9Ben Murdoch#include <wtf/Forward.h>
430bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include <wtf/OwnPtr.h>
440bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch#include <wtf/RefCounted.h>
45f486d19d62f1bc33246748b14b14a9dfa617b57fIain Merrick#include <wtf/text/AtomicStringHash.h>
460bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
470bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdochnamespace WebCore {
480bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
49d0825bca7fe65beaee391d30da42e937db621564Steve Block    class ThreadableWebSocketChannel;
500bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
51231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    class WebSocket : public RefCounted<WebSocket>, public EventTarget, public ActiveDOMObject, public WebSocketChannelClient {
52231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    public:
53cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block        static void setIsAvailable(bool);
54cac0f67c402d107cdb10971b95719e2ff9c7c76bSteve Block        static bool isAvailable();
55231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        static PassRefPtr<WebSocket> create(ScriptExecutionContext* context) { return adoptRef(new WebSocket(context)); }
56231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual ~WebSocket();
570bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
58231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        enum State {
59231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            CONNECTING = 0,
60231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            OPEN = 1,
61231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block            CLOSED = 2
62231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        };
630bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
64231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void connect(const KURL&, ExceptionCode&);
65231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void connect(const KURL&, const String& protocol, ExceptionCode&);
660bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
67231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        bool send(const String& message, ExceptionCode&);
680bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
69231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        void close();
700bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
71231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        const KURL& url() const;
72231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        State readyState() const;
73231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        unsigned long bufferedAmount() const;
740bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
75231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
76231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
77dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block        DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
78231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
790bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
80231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // EventTarget
81231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual WebSocket* toWebSocket() { return this; }
820bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
83231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual ScriptExecutionContext* scriptExecutionContext() const;
845e2bc6953fe6923165b8a5d7679939693a1d58d6Steve Block        virtual void contextDestroyed();
856c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        virtual bool canSuspend() const;
8668513a70bcd92384395513322f1b801e7bf9c729Steve Block        virtual void suspend(ReasonForSuspension);
876c2af9490927c3c5959b5cb07461b646f8b32f6cKristian Monsen        virtual void resume();
885e2bc6953fe6923165b8a5d7679939693a1d58d6Steve Block        virtual void stop();
890bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
90231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        using RefCounted<WebSocket>::ref;
91231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        using RefCounted<WebSocket>::deref;
920bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
93231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        // WebSocketChannelClient
94231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual void didConnect();
95d0825bca7fe65beaee391d30da42e937db621564Steve Block        virtual void didReceiveMessage(const String& message);
96dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block        virtual void didReceiveMessageError();
978a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block        virtual void didClose(unsigned long unhandledBufferedAmount);
980bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
99231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    private:
100231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        WebSocket(ScriptExecutionContext*);
1010bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
102231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual void refEventTarget() { ref(); }
103231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual void derefEventTarget() { deref(); }
104231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual EventTargetData* eventTargetData();
105231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        virtual EventTargetData* ensureEventTargetData();
1060bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
107d0825bca7fe65beaee391d30da42e937db621564Steve Block        RefPtr<ThreadableWebSocketChannel> m_channel;
1080bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
109231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        State m_state;
110231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        KURL m_url;
111231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        String m_protocol;
112231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block        EventTargetData m_eventTargetData;
1138a0914b749bbe7da7768e07a7db5c6d4bb09472bSteve Block        unsigned long m_bufferedAmountAfterClose;
114231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block    };
1150bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
116d0825bca7fe65beaee391d30da42e937db621564Steve Block} // namespace WebCore
1170bf48ef3be53ddaa52bbead65dfd75bf90e7a2b5Ben Murdoch
118d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // ENABLE(WEB_SOCKETS)
119231d4e3152a9c27a73b6ac7badbe6be673aa3ddfSteve Block
120d0825bca7fe65beaee391d30da42e937db621564Steve Block#endif // WebSocket_h
121