Upstream version 5.34.92.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 "modules/websockets/WebSocketChannel.h"
37 #include "modules/websockets/WebSocketChannelClient.h"
38
39 #include "wtf/PassOwnPtr.h"
40 #include "wtf/PassRefPtr.h"
41 #include "wtf/RefCounted.h"
42 #include "wtf/RefPtr.h"
43 #include "wtf/Threading.h"
44 #include "wtf/Vector.h"
45 #include "wtf/text/WTFString.h"
46
47 namespace WebCore {
48
49 class BlobDataHandle;
50 class KURL;
51 class ExecutionContext;
52 class ThreadableWebSocketChannelClientWrapper;
53 class WorkerGlobalScope;
54 class WorkerLoaderProxy;
55 class WorkerRunLoop;
56
57 class WorkerThreadableWebSocketChannel FINAL : public RefCounted<WorkerThreadableWebSocketChannel>, public WebSocketChannel {
58     WTF_MAKE_FAST_ALLOCATED;
59 public:
60     static PassRefPtr<WebSocketChannel> create(WorkerGlobalScope* workerGlobalScope, WebSocketChannelClient* client, const String& taskMode, const String& sourceURL, unsigned lineNumber)
61     {
62         return adoptRef(new WorkerThreadableWebSocketChannel(workerGlobalScope, client, taskMode, sourceURL, lineNumber));
63     }
64     virtual ~WorkerThreadableWebSocketChannel();
65
66     // WebSocketChannel functions.
67     virtual void connect(const KURL&, const String& protocol) OVERRIDE;
68     virtual String subprotocol() OVERRIDE;
69     virtual String extensions() OVERRIDE;
70     virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
71     virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
72     virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
73     virtual unsigned long bufferedAmount() const OVERRIDE;
74     virtual void close(int code, const String& reason) OVERRIDE;
75     virtual void fail(const String& reason, MessageLevel, const String&, unsigned) OVERRIDE;
76     virtual void disconnect() OVERRIDE; // Will suppress didClose().
77     virtual void suspend() OVERRIDE;
78     virtual void resume() OVERRIDE;
79
80     // Generated by the bridge. The Peer and its bridge should have identical
81     // lifetimes.
82     class Peer FINAL : public WebSocketChannelClient {
83         WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED;
84     public:
85         // sourceURLAtConnection and lineNumberAtConnection parameters may
86         // be shown when the connection fails.
87         static Peer* create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> clientWrapper, WorkerLoaderProxy& loaderProxy, ExecutionContext* context, const String& taskMode, const String& sourceURLAtConnection, unsigned lineNumberAtConnection)
88         {
89             return new Peer(clientWrapper, loaderProxy, context, taskMode, sourceURLAtConnection, lineNumberAtConnection);
90         }
91         virtual ~Peer();
92
93         void connect(const KURL&, const String& protocol);
94         void send(const String& message);
95         void send(const ArrayBuffer&);
96         void send(PassRefPtr<BlobDataHandle>);
97         void bufferedAmount();
98         void close(int code, const String& reason);
99         void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
100         void disconnect();
101         void suspend();
102         void resume();
103
104         // WebSocketChannelClient functions.
105         virtual void didConnect() OVERRIDE;
106         virtual void didReceiveMessage(const String& message) OVERRIDE;
107         virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
108         virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
109         virtual void didStartClosingHandshake() OVERRIDE;
110         virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
111         virtual void didReceiveMessageError() OVERRIDE;
112
113     private:
114         Peer(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderProxy&, ExecutionContext*, const String& taskMode, const String& sourceURL, unsigned lineNumber);
115
116         RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
117         WorkerLoaderProxy& m_loaderProxy;
118         RefPtr<WebSocketChannel> m_mainWebSocketChannel;
119         String m_taskMode;
120     };
121
122     using RefCounted<WorkerThreadableWebSocketChannel>::ref;
123     using RefCounted<WorkerThreadableWebSocketChannel>::deref;
124
125 protected:
126     // WebSocketChannel functions.
127     virtual void refWebSocketChannel() OVERRIDE { ref(); }
128     virtual void derefWebSocketChannel() OVERRIDE { deref(); }
129
130 private:
131     // Bridge for Peer. Running on the worker thread.
132     class Bridge : public RefCounted<Bridge> {
133     public:
134         static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, PassRefPtr<WorkerGlobalScope> workerGlobalScope, const String& taskMode)
135         {
136             return adoptRef(new Bridge(workerClientWrapper, workerGlobalScope, taskMode));
137         }
138         ~Bridge();
139         // sourceURLAtConnection and lineNumberAtConnection parameters may
140         // be shown when the connection fails.
141         void initialize(const String& sourceURLAtConnection, unsigned lineNumberAtConnection);
142         void connect(const KURL&, const String& protocol);
143         WebSocketChannel::SendResult send(const String& message);
144         WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength);
145         WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>);
146         unsigned long bufferedAmount();
147         void close(int code, const String& reason);
148         void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
149         void disconnect();
150         void suspend();
151         void resume();
152
153         using RefCounted<Bridge>::ref;
154         using RefCounted<Bridge>::deref;
155
156     private:
157         Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassRefPtr<WorkerGlobalScope>, const String& taskMode);
158
159         static void setWebSocketChannel(ExecutionContext*, Bridge* thisPtr, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
160
161         // Executed on the main thread to create a Peer for this bridge.
162         // sourceURL and lineNumber provides the source filename and
163         // the line number information at the connection initiation
164         // respectively. They may be shown when the connection fails.
165         static void mainThreadInitialize(ExecutionContext*, WorkerLoaderProxy*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& taskMode, const String& sourceURL, unsigned lineNumber);
166
167         // Executed on the worker context's thread.
168         void clearClientWrapper();
169
170         void setMethodNotCompleted();
171         void waitForMethodCompletion();
172
173         RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
174         RefPtr<WorkerGlobalScope> m_workerGlobalScope;
175         WorkerLoaderProxy& m_loaderProxy;
176         String m_taskMode;
177         Peer* m_peer;
178     };
179
180     WorkerThreadableWebSocketChannel(WorkerGlobalScope*, WebSocketChannelClient*, const String& taskMode, const String& sourceURL, unsigned lineNumber);
181
182     static void mainThreadConnect(ExecutionContext*, Peer*, const KURL&, const String& protocol);
183     static void mainThreadSend(ExecutionContext*, Peer*, const String& message);
184     static void mainThreadSendArrayBuffer(ExecutionContext*, Peer*, PassOwnPtr<Vector<char> >);
185     static void mainThreadSendBlob(ExecutionContext*, Peer*, PassRefPtr<BlobDataHandle>);
186     static void mainThreadBufferedAmount(ExecutionContext*, Peer*);
187     static void mainThreadClose(ExecutionContext*, Peer*, int code, const String& reason);
188     static void mainThreadFail(ExecutionContext*, Peer*, const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
189     static void mainThreadDestroy(ExecutionContext*, PassOwnPtr<Peer>);
190     static void mainThreadSuspend(ExecutionContext*, Peer*);
191     static void mainThreadResume(ExecutionContext*, Peer*);
192
193     class WorkerGlobalScopeDidInitializeTask;
194
195     RefPtr<WorkerGlobalScope> m_workerGlobalScope;
196     RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
197     RefPtr<Bridge> m_bridge;
198     String m_sourceURLAtConnection;
199     unsigned m_lineNumberAtConnection;
200 };
201
202 } // namespace WebCore
203
204 #endif // WorkerThreadableWebSocketChannel_h