#ifndef NewWebSocketChannelImpl_h
#define NewWebSocketChannelImpl_h
#include "core/dom/ContextLifecycleObserver.h"
#include "core/fileapi/Blob.h"
#include "core/fileapi/FileError.h"
#include "core/frame/ConsoleTypes.h"
#include "modules/websockets/WebSocketChannel.h"
#include "platform/weborigin/KURL.h"
#include "public/platform/WebSocketHandle.h"
#include "public/platform/WebSocketHandleClient.h"
#include "wtf/ArrayBuffer.h"
#include "wtf/Deque.h"
#include "wtf/FastAllocBase.h"
#include "wtf/OwnPtr.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
#include "wtf/RefPtr.h"
#include "wtf/Vector.h"
#include "wtf/text/CString.h"
#include "wtf/text/WTFString.h"
namespace blink {
class WebSocketHandshakeRequestInfo;
class WebSocketHandshakeResponseInfo;
}
namespace WebCore {
class Document;
class WebSocketHandshakeRequest;
class NewWebSocketChannelImpl FINAL : public WebSocketChannel, public RefCounted<NewWebSocketChannelImpl>, public blink::WebSocketHandleClient, public ContextLifecycleObserver {
WTF_MAKE_FAST_ALLOCATED;
public:
static PassRefPtr<NewWebSocketChannelImpl> create(ExecutionContext* context, WebSocketChannelClient* client, const String& sourceURL = String(), unsigned lineNumber = 0)
{
return adoptRef(new NewWebSocketChannelImpl(context, client, sourceURL, lineNumber));
}
virtual ~NewWebSocketChannelImpl();
virtual void connect(const KURL&, const String& protocol) OVERRIDE;
virtual String subprotocol() OVERRIDE;
virtual String extensions() OVERRIDE;
virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
virtual unsigned long bufferedAmount() const OVERRIDE;
virtual void close(int code, const String& reason) OVERRIDE;
virtual void fail(const String& reason, MessageLevel, const String&, unsigned lineNumber) OVERRIDE;
using WebSocketChannel::fail;
virtual void disconnect() OVERRIDE;
using RefCounted<NewWebSocketChannelImpl>::ref;
using RefCounted<NewWebSocketChannelImpl>::deref;
virtual void suspend() OVERRIDE;
virtual void resume() OVERRIDE;
private:
enum MessageType {
MessageTypeText,
MessageTypeBlob,
MessageTypeArrayBuffer,
};
struct Message {
explicit Message(const String&);
explicit Message(PassRefPtr<BlobDataHandle>);
explicit Message(PassRefPtr<ArrayBuffer>);
MessageType type;
CString text;
RefPtr<BlobDataHandle> blobDataHandle;
RefPtr<ArrayBuffer> arrayBuffer;
};
struct ReceivedMessage {
bool isMessageText;
Vector<char> data;
};
class BlobLoader;
NewWebSocketChannelImpl(ExecutionContext*, WebSocketChannelClient*, const String&, unsigned);
void sendInternal();
void flowControlIfNecessary();
void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_sourceURLAtConstruction, m_lineNumberAtConstruction); }
void abortAsyncOperations();
void handleDidClose(bool wasClean, unsigned short code, const String& reason);
Document* document();
virtual void didConnect(blink::WebSocketHandle*, bool fail, const blink::WebString& selectedProtocol, const blink::WebString& extensions) OVERRIDE;
virtual void didStartOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeRequestInfo&) OVERRIDE;
virtual void didFinishOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeResponseInfo&) OVERRIDE;
virtual void didFail(blink::WebSocketHandle*, const blink::WebString& message) OVERRIDE;
virtual void didReceiveData(blink::WebSocketHandle*, bool fin, blink::WebSocketHandle::MessageType, const char* data, size_t ) OVERRIDE;
virtual void didClose(blink::WebSocketHandle*, bool wasClean, unsigned short code, const blink::WebString& reason) OVERRIDE;
virtual void didReceiveFlowControl(blink::WebSocketHandle*, int64_t quota) OVERRIDE;
virtual void didStartClosingHandshake(blink::WebSocketHandle*) OVERRIDE;
void didFinishLoadingBlob(PassRefPtr<ArrayBuffer>);
void didFailLoadingBlob(FileError::ErrorCode);
virtual void refWebSocketChannel() OVERRIDE { ref(); }
virtual void derefWebSocketChannel() OVERRIDE { deref(); }
virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); }
OwnPtr<blink::WebSocketHandle> m_handle;
WebSocketChannelClient* m_client;
KURL m_url;
unsigned long m_identifier;
OwnPtr<BlobLoader> m_blobLoader;
Deque<Message> m_messages;
Vector<char> m_receivingMessageData;
bool m_receivingMessageTypeIsText;
int64_t m_sendingQuota;
int64_t m_receivedDataSizeForFlowControl;
unsigned long m_bufferedAmount;
size_t m_sentSizeOfTopMessage;
String m_subprotocol;
String m_extensions;
String m_sourceURLAtConstruction;
unsigned m_lineNumberAtConstruction;
RefPtr<WebSocketHandshakeRequest> m_handshakeRequest;
static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15;
};
}
#endif