#ifndef WebSocket_h
#define WebSocket_h
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ActiveDOMObject.h"
#include "core/events/EventListener.h"
#include "core/events/EventTarget.h"
#include "modules/websockets/WebSocketChannel.h"
#include "modules/websockets/WebSocketChannelClient.h"
#include "platform/Timer.h"
#include "platform/weborigin/KURL.h"
#include "wtf/Deque.h"
#include "wtf/Forward.h"
#include "wtf/OwnPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/text/AtomicStringHash.h"
namespace WebCore {
class Blob;
class ExceptionState;
class WebSocket FINAL : public RefCounted<WebSocket>, public ScriptWrappable, public EventTargetWithInlineData, public ActiveDOMObject, public WebSocketChannelClient {
REFCOUNTED_EVENT_TARGET(WebSocket);
public:
static const char* subProtocolSeperator();
static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, ExceptionState&);
static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, const String& protocol, ExceptionState&);
static PassRefPtr<WebSocket> create(ExecutionContext*, const String& url, const Vector<String>& protocols, ExceptionState&);
virtual ~WebSocket();
enum State {
CONNECTING = 0,
OPEN = 1,
CLOSING = 2,
CLOSED = 3
};
void connect(const String& url, ExceptionState&);
void connect(const String& url, const String& protocol, ExceptionState&);
void connect(const String& url, const Vector<String>& protocols, ExceptionState&);
void send(const String& message, ExceptionState&);
void send(ArrayBuffer*, ExceptionState&);
void send(ArrayBufferView*, ExceptionState&);
void send(Blob*, ExceptionState&);
void close(unsigned short code, const String& reason, ExceptionState&);
void close(ExceptionState&);
void close(unsigned short code, ExceptionState&);
const KURL& url() const;
State readyState() const;
unsigned long bufferedAmount() const;
String protocol() const;
String extensions() const;
String binaryType() const;
void setBinaryType(const String&);
DEFINE_ATTRIBUTE_EVENT_LISTENER(open);
DEFINE_ATTRIBUTE_EVENT_LISTENER(message);
DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
DEFINE_ATTRIBUTE_EVENT_LISTENER(close);
virtual const AtomicString& interfaceName() const OVERRIDE;
virtual ExecutionContext* executionContext() const OVERRIDE;
virtual void contextDestroyed() OVERRIDE;
virtual bool hasPendingActivity() const OVERRIDE;
virtual void suspend() OVERRIDE;
virtual void resume() OVERRIDE;
virtual void stop() OVERRIDE;
virtual void didConnect() OVERRIDE;
virtual void didReceiveMessage(const String& message) OVERRIDE;
virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
virtual void didReceiveMessageError() OVERRIDE;
virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
virtual void didStartClosingHandshake() OVERRIDE;
virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
private:
class EventQueue FINAL : public RefCounted<EventQueue> {
public:
static PassRefPtr<EventQueue> create(EventTarget* target) { return adoptRef(new EventQueue(target)); }
~EventQueue();
void dispatch(PassRefPtrWillBeRawPtr<Event> );
bool isEmpty() const;
void suspend();
void resume();
void stop();
private:
enum State {
Active,
Suspended,
Stopped,
};
explicit EventQueue(EventTarget*);
void dispatchQueuedEvents();
void resumeTimerFired(Timer<EventQueue>*);
State m_state;
EventTarget* m_target;
Deque<RefPtrWillBePersistent<Event> > m_events;
Timer<EventQueue> m_resumeTimer;
};
explicit WebSocket(ExecutionContext*);
void logError(const String& message);
void closeInternal(int, const String&, ExceptionState&);
size_t getFramingOverhead(size_t payloadSize);
void handleSendResult(WebSocketChannel::SendResult, ExceptionState&);
void updateBufferedAmountAfterClose(unsigned long);
void releaseChannel();
enum BinaryType {
BinaryTypeBlob,
BinaryTypeArrayBuffer
};
RefPtr<WebSocketChannel> m_channel;
State m_state;
KURL m_url;
unsigned long m_bufferedAmount;
unsigned long m_bufferedAmountAfterClose;
BinaryType m_binaryType;
String m_subprotocol;
String m_extensions;
RefPtr<EventQueue> m_eventQueue;
};
}
#endif