Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / websockets / WorkerThreadableWebSocketChannel.h
1 /*
2  * Copyright (C) 2011 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 WorkerThreadableWebSocketChannel_h
32 #define WorkerThreadableWebSocketChannel_h
33
34 #include "core/frame/ConsoleTypes.h"
35 #include "core/workers/WorkerGlobalScope.h"
36 #include "heap/Handle.h"
37 #include "modules/websockets/WebSocketChannel.h"
38 #include "modules/websockets/WebSocketChannelClient.h"
39
40 #include "wtf/PassOwnPtr.h"
41 #include "wtf/PassRefPtr.h"
42 #include "wtf/RefCounted.h"
43 #include "wtf/RefPtr.h"
44 #include "wtf/Threading.h"
45 #include "wtf/Vector.h"
46 #include "wtf/WeakPtr.h"
47 #include "wtf/text/WTFString.h"
48
49 namespace blink {
50 class WebWaitableEvent;
51 }
52
53 namespace WebCore {
54
55 class BlobDataHandle;
56 class KURL;
57 class ExecutionContext;
58 class ThreadableWebSocketChannelClientWrapper;
59 class ThreadableWebSocketChannelSyncHelper;
60 class WorkerGlobalScope;
61 class WorkerLoaderProxy;
62 class WorkerRunLoop;
63
64 class WorkerThreadableWebSocketChannel FINAL : public RefCounted<WorkerThreadableWebSocketChannel>, public WebSocketChannel {
65     WTF_MAKE_FAST_ALLOCATED;
66 public:
67     static PassRefPtr<WebSocketChannel> create(WorkerGlobalScope* workerGlobalScope, WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber)
68     {
69         return adoptRef(new WorkerThreadableWebSocketChannel(workerGlobalScope, client, sourceURL, lineNumber));
70     }
71     virtual ~WorkerThreadableWebSocketChannel();
72
73     // WebSocketChannel functions.
74     virtual void connect(const KURL&, const String& protocol) OVERRIDE;
75     virtual String subprotocol() OVERRIDE;
76     virtual String extensions() OVERRIDE;
77     virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
78     virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
79     virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
80     virtual unsigned long bufferedAmount() const OVERRIDE;
81     virtual void close(int code, const String& reason) OVERRIDE;
82     virtual void fail(const String& reason, MessageLevel, const String&, unsigned) OVERRIDE;
83     virtual void disconnect() OVERRIDE; // Will suppress didClose().
84     virtual void suspend() OVERRIDE;
85     virtual void resume() OVERRIDE;
86
87     // Generated by the bridge. The Peer is destructed by an async call from
88     // Bridge, and may outlive the bridge. All methods of this class must
89     // be called on the main thread.
90     class Peer FINAL : public WebSocketChannelClient {
91         WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED;
92     public:
93         virtual ~Peer();
94
95         // sourceURLAtConnection and lineNumberAtConnection parameters may
96         // be shown when the connection fails.
97         static void initialize(ExecutionContext*, PassRefPtr<WeakReference<Peer> >, WorkerLoaderProxy*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& sourceURLAtConnection, unsigned lineNumberAtConnection, PassOwnPtr<ThreadableWebSocketChannelSyncHelper>);
98         void destroy();
99
100         void connect(const KURL&, const String& protocol);
101         void send(const String& message);
102         void sendArrayBuffer(PassOwnPtr<Vector<char> >);
103         void sendBlob(PassRefPtr<BlobDataHandle>);
104         void bufferedAmount();
105         void close(int code, const String& reason);
106         void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
107         void disconnect();
108         void suspend();
109         void resume();
110
111         // WebSocketChannelClient functions.
112         virtual void didConnect() OVERRIDE;
113         virtual void didReceiveMessage(const String& message) OVERRIDE;
114         virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
115         virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
116         virtual void didStartClosingHandshake() OVERRIDE;
117         virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
118         virtual void didReceiveMessageError() OVERRIDE;
119
120     private:
121         Peer(PassRefPtr<WeakReference<Peer> >, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderProxy&, ExecutionContext*, const String& sourceURL, unsigned lineNumber, PassOwnPtr<ThreadableWebSocketChannelSyncHelper>);
122
123         RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
124         WorkerLoaderProxy& m_loaderProxy;
125         RefPtr<WebSocketChannel> m_mainWebSocketChannel;
126         OwnPtr<ThreadableWebSocketChannelSyncHelper> m_syncHelper;
127         WeakPtrFactory<Peer> m_weakFactory;
128     };
129
130     using RefCounted<WorkerThreadableWebSocketChannel>::ref;
131     using RefCounted<WorkerThreadableWebSocketChannel>::deref;
132
133 protected:
134     // WebSocketChannel functions.
135     virtual void refWebSocketChannel() OVERRIDE { ref(); }
136     virtual void derefWebSocketChannel() OVERRIDE { deref(); }
137
138 private:
139     // Bridge for Peer. Running on the worker thread.
140     class Bridge : public RefCounted<Bridge> {
141     public:
142         static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, PassRefPtrWillBeRawPtr<WorkerGlobalScope> workerGlobalScope)
143         {
144             return adoptRef(new Bridge(workerClientWrapper, workerGlobalScope));
145         }
146         ~Bridge();
147         // sourceURLAtConnection and lineNumberAtConnection parameters may
148         // be shown when the connection fails.
149         void initialize(const String& sourceURLAtConnection, unsigned lineNumberAtConnection);
150         void connect(const KURL&, const String& protocol);
151         WebSocketChannel::SendResult send(const String& message);
152         WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength);
153         WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>);
154         unsigned long bufferedAmount();
155         void close(int code, const String& reason);
156         void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
157         void disconnect();
158         void suspend();
159         void resume();
160
161         using RefCounted<Bridge>::ref;
162         using RefCounted<Bridge>::deref;
163
164     private:
165         Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassRefPtrWillBeRawPtr<WorkerGlobalScope>);
166
167         static void setWebSocketChannel(ExecutionContext*, Bridge* thisPtr, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
168
169         // Executed on the worker context's thread.
170         void clearClientWrapper();
171
172         // Returns false if shutdown event is received before method completion.
173         bool waitForMethodCompletion();
174
175         void terminatePeer();
176
177         RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
178         RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope;
179         WorkerLoaderProxy& m_loaderProxy;
180         ThreadableWebSocketChannelSyncHelper* m_syncHelper;
181         WeakPtr<Peer> m_peer;
182     };
183
184     WorkerThreadableWebSocketChannel(WorkerGlobalScope*, WebSocketChannelClient*, const String& sourceURL, unsigned lineNumber);
185
186     RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope;
187     RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
188     RefPtr<Bridge> m_bridge;
189     String m_sourceURLAtConnection;
190     unsigned m_lineNumberAtConnection;
191 };
192
193 } // namespace WebCore
194
195 #endif // WorkerThreadableWebSocketChannel_h