Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / websockets / NewWebSocketChannelImpl.h
1 /*
2  * Copyright (C) 2013 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef NewWebSocketChannelImpl_h
32 #define NewWebSocketChannelImpl_h
33
34 #include "core/dom/ContextLifecycleObserver.h"
35 #include "core/fileapi/Blob.h"
36 #include "core/fileapi/FileError.h"
37 #include "core/frame/ConsoleTypes.h"
38 #include "modules/websockets/WebSocketChannel.h"
39 #include "platform/weborigin/KURL.h"
40 #include "public/platform/WebSocketHandle.h"
41 #include "public/platform/WebSocketHandleClient.h"
42 #include "wtf/ArrayBuffer.h"
43 #include "wtf/Deque.h"
44 #include "wtf/FastAllocBase.h"
45 #include "wtf/OwnPtr.h"
46 #include "wtf/PassRefPtr.h"
47 #include "wtf/RefPtr.h"
48 #include "wtf/Vector.h"
49 #include "wtf/text/CString.h"
50 #include "wtf/text/WTFString.h"
51
52 namespace blink {
53
54 class WebSocketHandshakeRequestInfo;
55 class WebSocketHandshakeResponseInfo;
56
57 } // namespace blink
58
59 namespace WebCore {
60
61 class Document;
62 class WebSocketHandshakeRequest;
63
64 // This class may replace MainThreadWebSocketChannel.
65 class NewWebSocketChannelImpl FINAL : public WebSocketChannel, public blink::WebSocketHandleClient, public ContextLifecycleObserver {
66     WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
67 public:
68     // You can specify the source file and the line number information
69     // explicitly by passing the last parameter.
70     // In the usual case, they are set automatically and you don't have to
71     // pass it.
72     static PassRefPtrWillBeRawPtr<NewWebSocketChannelImpl> create(ExecutionContext* context, WebSocketChannelClient* client, const String& sourceURL = String(), unsigned lineNumber = 0)
73     {
74         return adoptRefWillBeRefCountedGarbageCollected(new NewWebSocketChannelImpl(context, client, sourceURL, lineNumber));
75     }
76     virtual ~NewWebSocketChannelImpl();
77
78     // WebSocketChannel functions.
79     virtual bool connect(const KURL&, const String& protocol) OVERRIDE;
80     virtual String subprotocol() OVERRIDE;
81     virtual String extensions() OVERRIDE;
82     virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
83     virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
84     virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
85     virtual unsigned long bufferedAmount() const OVERRIDE;
86     // Start closing handshake. Use the CloseEventCodeNotSpecified for the code
87     // argument to omit payload.
88     virtual void close(int code, const String& reason) OVERRIDE;
89     virtual void fail(const String& reason, MessageLevel, const String&, unsigned lineNumber) OVERRIDE;
90     using WebSocketChannel::fail;
91     virtual void disconnect() OVERRIDE;
92
93     virtual void suspend() OVERRIDE;
94     virtual void resume() OVERRIDE;
95
96     virtual void trace(Visitor*) OVERRIDE;
97
98 private:
99     enum MessageType {
100         MessageTypeText,
101         MessageTypeBlob,
102         MessageTypeArrayBuffer,
103     };
104
105     struct Message {
106         explicit Message(const String&);
107         explicit Message(PassRefPtr<BlobDataHandle>);
108         explicit Message(PassRefPtr<ArrayBuffer>);
109         MessageType type;
110         CString text;
111         RefPtr<BlobDataHandle> blobDataHandle;
112         RefPtr<ArrayBuffer> arrayBuffer;
113     };
114
115     struct ReceivedMessage {
116         bool isMessageText;
117         Vector<char> data;
118     };
119
120     class BlobLoader;
121
122     NewWebSocketChannelImpl(ExecutionContext*, WebSocketChannelClient*, const String&, unsigned);
123     void sendInternal();
124     void flowControlIfNecessary();
125     void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_sourceURLAtConstruction, m_lineNumberAtConstruction); }
126     void abortAsyncOperations();
127     void handleDidClose(bool wasClean, unsigned short code, const String& reason);
128     Document* document(); // can be called only when m_identifier > 0.
129
130     // WebSocketHandleClient functions.
131     virtual void didConnect(blink::WebSocketHandle*, bool fail, const blink::WebString& selectedProtocol, const blink::WebString& extensions) OVERRIDE;
132     virtual void didStartOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeRequestInfo&) OVERRIDE;
133     virtual void didFinishOpeningHandshake(blink::WebSocketHandle*, const blink::WebSocketHandshakeResponseInfo&) OVERRIDE;
134     virtual void didFail(blink::WebSocketHandle*, const blink::WebString& message) OVERRIDE;
135     virtual void didReceiveData(blink::WebSocketHandle*, bool fin, blink::WebSocketHandle::MessageType, const char* data, size_t /* size */) OVERRIDE;
136     virtual void didClose(blink::WebSocketHandle*, bool wasClean, unsigned short code, const blink::WebString& reason) OVERRIDE;
137     virtual void didReceiveFlowControl(blink::WebSocketHandle*, int64_t quota) OVERRIDE;
138     virtual void didStartClosingHandshake(blink::WebSocketHandle*) OVERRIDE;
139
140     // Methods for BlobLoader.
141     void didFinishLoadingBlob(PassRefPtr<ArrayBuffer>);
142     void didFailLoadingBlob(FileError::ErrorCode);
143
144     // LifecycleObserver functions.
145     // This object must be destroyed before the context.
146     virtual void contextDestroyed() OVERRIDE { ASSERT_NOT_REACHED(); }
147
148     // m_handle is a handle of the connection.
149     // m_handle == 0 means this channel is closed.
150     OwnPtr<blink::WebSocketHandle> m_handle;
151
152     // m_client can be deleted while this channel is alive, but this class
153     // expects that disconnect() is called before the deletion.
154     WebSocketChannelClient* m_client;
155     KURL m_url;
156     // m_identifier > 0 means calling scriptContextExecution() returns a Document.
157     unsigned long m_identifier;
158     OwnPtrWillBeMember<BlobLoader> m_blobLoader;
159     Deque<Message> m_messages;
160     Vector<char> m_receivingMessageData;
161
162     bool m_receivingMessageTypeIsText;
163     int64_t m_sendingQuota;
164     int64_t m_receivedDataSizeForFlowControl;
165     unsigned long m_bufferedAmount;
166     size_t m_sentSizeOfTopMessage;
167     String m_subprotocol;
168     String m_extensions;
169
170     String m_sourceURLAtConstruction;
171     unsigned m_lineNumberAtConstruction;
172     RefPtr<WebSocketHandshakeRequest> m_handshakeRequest;
173
174     static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15;
175 };
176
177 } // namespace WebCore
178
179 #endif // NewWebSocketChannelImpl_h