Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / websockets / DocumentWebSocketChannel.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 DocumentWebSocketChannel_h
32 #define DocumentWebSocketChannel_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/PassOwnPtr.h"
47 #include "wtf/PassRefPtr.h"
48 #include "wtf/RefPtr.h"
49 #include "wtf/Vector.h"
50 #include "wtf/text/CString.h"
51 #include "wtf/text/WTFString.h"
52
53 namespace blink {
54
55 class Document;
56 class WebSocketHandshakeRequest;
57 class WebSocketHandshakeRequestInfo;
58 class WebSocketHandshakeResponseInfo;
59
60 // This class is a WebSocketChannel subclass that works with a Document in a
61 // DOMWindow (i.e. works in the main thread).
62 class DocumentWebSocketChannel final : public WebSocketChannel, public WebSocketHandleClient, public ContextLifecycleObserver {
63 public:
64     // You can specify the source file and the line number information
65     // explicitly by passing the last parameter.
66     // In the usual case, they are set automatically and you don't have to
67     // pass it.
68     // Specify handle explicitly only in tests.
69     static DocumentWebSocketChannel* create(ExecutionContext* context, WebSocketChannelClient* client, const String& sourceURL = String(), unsigned lineNumber = 0, WebSocketHandle *handle = 0)
70     {
71         return new DocumentWebSocketChannel(context, client, sourceURL, lineNumber, handle);
72     }
73     virtual ~DocumentWebSocketChannel();
74
75     // WebSocketChannel functions.
76     virtual bool connect(const KURL&, const String& protocol) override;
77     virtual void send(const String& message) override;
78     virtual void send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) override;
79     virtual void send(PassRefPtr<BlobDataHandle>) override;
80     virtual void send(PassOwnPtr<Vector<char> > data) override;
81     // Start closing handshake. Use the CloseEventCodeNotSpecified for the code
82     // argument to omit payload.
83     virtual void close(int code, const String& reason) override;
84     virtual void fail(const String& reason, MessageLevel, const String&, unsigned lineNumber) override;
85     virtual void disconnect() override;
86
87     virtual void suspend() override;
88     virtual void resume() override;
89
90     virtual void trace(Visitor*) override;
91
92 private:
93     enum MessageType {
94         MessageTypeText,
95         MessageTypeBlob,
96         MessageTypeArrayBuffer,
97         MessageTypeVector,
98         MessageTypeClose,
99     };
100
101     struct Message {
102         explicit Message(const String&);
103         explicit Message(PassRefPtr<BlobDataHandle>);
104         explicit Message(PassRefPtr<ArrayBuffer>);
105         explicit Message(PassOwnPtr<Vector<char> >);
106         Message(unsigned short code, const String& reason);
107
108         MessageType type;
109
110         CString text;
111         RefPtr<BlobDataHandle> blobDataHandle;
112         RefPtr<ArrayBuffer> arrayBuffer;
113         OwnPtr<Vector<char> > vectorData;
114         unsigned short code;
115         String reason;
116     };
117
118     struct ReceivedMessage {
119         bool isMessageText;
120         Vector<char> data;
121     };
122
123     class BlobLoader;
124
125     DocumentWebSocketChannel(ExecutionContext*, WebSocketChannelClient*, const String&, unsigned, WebSocketHandle*);
126     void sendInternal();
127     void flowControlIfNecessary();
128     void failAsError(const String& reason) { fail(reason, ErrorMessageLevel, m_sourceURLAtConstruction, m_lineNumberAtConstruction); }
129     void abortAsyncOperations();
130     void handleDidClose(bool wasClean, unsigned short code, const String& reason);
131     Document* document(); // can be called only when m_identifier > 0.
132
133     // WebSocketHandleClient functions.
134     virtual void didConnect(WebSocketHandle*, bool fail, const WebString& selectedProtocol, const WebString& extensions) override;
135     virtual void didStartOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeRequestInfo&) override;
136     virtual void didFinishOpeningHandshake(WebSocketHandle*, const WebSocketHandshakeResponseInfo&) override;
137     virtual void didFail(WebSocketHandle*, const WebString& message) override;
138     virtual void didReceiveData(WebSocketHandle*, bool fin, WebSocketHandle::MessageType, const char* data, size_t /* size */) override;
139     virtual void didClose(WebSocketHandle*, bool wasClean, unsigned short code, const WebString& reason) override;
140     virtual void didReceiveFlowControl(WebSocketHandle*, int64_t quota) override;
141     virtual void didStartClosingHandshake(WebSocketHandle*) override;
142
143     // Methods for BlobLoader.
144     void didFinishLoadingBlob(PassRefPtr<ArrayBuffer>);
145     void didFailLoadingBlob(FileError::ErrorCode);
146
147     // LifecycleObserver functions.
148     virtual void contextDestroyed() override
149     {
150         // In oilpan we cannot assume this channel's finalizer has been called
151         // before the document it is observing is dead and finalized since there
152         // is no eager finalization. Instead the finalization happens at the
153         // next GC which could be long enough after the Peer::destroy call for
154         // the context (ie. Document) to be dead too. If the context's finalizer
155         // is run first this method gets called. Instead we assert the channel
156         // has been disconnected which happens in Peer::destroy.
157         ASSERT(!m_handle);
158         ASSERT(!m_client);
159         ASSERT(!m_identifier);
160         ContextLifecycleObserver::contextDestroyed();
161     }
162
163     // m_handle is a handle of the connection.
164     // m_handle == 0 means this channel is closed.
165     OwnPtr<WebSocketHandle> m_handle;
166
167     // m_client can be deleted while this channel is alive, but this class
168     // expects that disconnect() is called before the deletion.
169     Member<WebSocketChannelClient> m_client;
170     KURL m_url;
171     // m_identifier > 0 means calling scriptContextExecution() returns a Document.
172     unsigned long m_identifier;
173     Member<BlobLoader> m_blobLoader;
174     Deque<OwnPtr<Message> > m_messages;
175     Vector<char> m_receivingMessageData;
176
177     bool m_receivingMessageTypeIsText;
178     int64_t m_sendingQuota;
179     int64_t m_receivedDataSizeForFlowControl;
180     size_t m_sentSizeOfTopMessage;
181
182     String m_sourceURLAtConstruction;
183     unsigned m_lineNumberAtConstruction;
184     RefPtr<WebSocketHandshakeRequest> m_handshakeRequest;
185
186     static const int64_t receivedDataSizeForFlowControlHighWaterMark = 1 << 15;
187 };
188
189 } // namespace blink
190
191 #endif // DocumentWebSocketChannel_h