2 * Copyright (C) 2011 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef WorkerThreadableWebSocketChannel_h
32 #define WorkerThreadableWebSocketChannel_h
34 #include "core/dom/ExecutionContextTask.h"
35 #include "core/frame/ConsoleTypes.h"
36 #include "core/workers/WorkerGlobalScope.h"
37 #include "modules/websockets/WebSocketChannel.h"
38 #include "modules/websockets/WebSocketChannelClient.h"
39 #include "platform/heap/Handle.h"
41 #include "wtf/PassOwnPtr.h"
42 #include "wtf/PassRefPtr.h"
43 #include "wtf/RefCounted.h"
44 #include "wtf/RefPtr.h"
45 #include "wtf/Threading.h"
46 #include "wtf/Vector.h"
47 #include "wtf/WeakPtr.h"
48 #include "wtf/text/WTFString.h"
51 class WebWaitableEvent;
58 class ExecutionContext;
59 class ThreadableWebSocketChannelClientWrapper;
60 class ThreadableWebSocketChannelSyncHelper;
61 class WorkerGlobalScope;
62 class WorkerLoaderProxy;
65 class WorkerThreadableWebSocketChannel FINAL : public WebSocketChannel {
66 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED;
68 static PassRefPtrWillBeRawPtr<WebSocketChannel> create(WorkerGlobalScope& workerGlobalScope, WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber)
70 return adoptRefWillBeRefCountedGarbageCollected(new WorkerThreadableWebSocketChannel(workerGlobalScope, client, sourceURL, lineNumber));
72 virtual ~WorkerThreadableWebSocketChannel();
74 // WebSocketChannel functions.
75 virtual bool connect(const KURL&, const String& protocol) OVERRIDE;
76 virtual String subprotocol() OVERRIDE;
77 virtual String extensions() OVERRIDE;
78 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE;
79 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE;
80 virtual WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>) OVERRIDE;
81 virtual unsigned long bufferedAmount() const OVERRIDE;
82 virtual void close(int code, const String& reason) OVERRIDE;
83 virtual void fail(const String& reason, MessageLevel, const String&, unsigned) OVERRIDE;
84 virtual void disconnect() OVERRIDE; // Will suppress didClose().
85 virtual void suspend() OVERRIDE;
86 virtual void resume() OVERRIDE;
88 // Generated by the bridge. The Peer is destructed by an async call from
89 // Bridge, and may outlive the bridge. All methods of this class must
90 // be called on the main thread.
91 class Peer FINAL : public WebSocketChannelClient {
92 WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED;
96 // sourceURLAtConnection and lineNumberAtConnection parameters may
97 // be shown when the connection fails.
98 static void initialize(ExecutionContext*, PassRefPtr<WeakReference<Peer> >, WorkerLoaderProxy*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& sourceURLAtConnection, unsigned lineNumberAtConnection, PassOwnPtr<ThreadableWebSocketChannelSyncHelper>);
101 void connect(const KURL&, const String& protocol);
102 void send(const String& message);
103 void sendArrayBuffer(PassOwnPtr<Vector<char> >);
104 void sendBlob(PassRefPtr<BlobDataHandle>);
105 void bufferedAmount();
106 void close(int code, const String& reason);
107 void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
112 // WebSocketChannelClient functions.
113 virtual void didConnect() OVERRIDE;
114 virtual void didReceiveMessage(const String& message) OVERRIDE;
115 virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE;
116 virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERRIDE;
117 virtual void didStartClosingHandshake() OVERRIDE;
118 virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE;
119 virtual void didReceiveMessageError() OVERRIDE;
122 Peer(PassRefPtr<WeakReference<Peer> >, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderProxy&, ExecutionContext*, const String& sourceURL, unsigned lineNumber, PassOwnPtr<ThreadableWebSocketChannelSyncHelper>);
124 const RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
125 WorkerLoaderProxy& m_loaderProxy;
126 RefPtrWillBePersistent<WebSocketChannel> m_mainWebSocketChannel;
127 OwnPtr<ThreadableWebSocketChannelSyncHelper> m_syncHelper;
128 WeakPtrFactory<Peer> m_weakFactory;
132 // Bridge for Peer. Running on the worker thread.
133 class Bridge : public RefCounted<Bridge> {
135 static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelClientWrapper> workerClientWrapper, WorkerGlobalScope& workerGlobalScope)
137 return adoptRef(new Bridge(workerClientWrapper, workerGlobalScope));
140 // sourceURLAtConnection and lineNumberAtConnection parameters may
141 // be shown when the connection fails.
142 void initialize(const String& sourceURLAtConnection, unsigned lineNumberAtConnection);
143 bool connect(const KURL&, const String& protocol);
144 WebSocketChannel::SendResult send(const String& message);
145 WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength);
146 WebSocketChannel::SendResult send(PassRefPtr<BlobDataHandle>);
147 unsigned long bufferedAmount();
148 void close(int code, const String& reason);
149 void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber);
155 Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerGlobalScope&);
157 static void setWebSocketChannel(ExecutionContext*, Bridge* thisPtr, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>);
159 // Executed on the worker context's thread.
160 void clearClientWrapper();
162 // Returns false if shutdown event is received before method completion.
163 bool waitForMethodCompletion(PassOwnPtr<ExecutionContextTask>);
165 void terminatePeer();
167 bool hasTerminatedPeer() { return !m_syncHelper; }
169 const RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
170 RefPtrWillBePersistent<WorkerGlobalScope> m_workerGlobalScope;
171 WorkerLoaderProxy& m_loaderProxy;
172 ThreadableWebSocketChannelSyncHelper* m_syncHelper;
173 WeakPtr<Peer> m_peer;
176 WorkerThreadableWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, const String& sourceURL, unsigned lineNumber);
178 const RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper;
179 RefPtr<Bridge> m_bridge;
180 String m_sourceURLAtConnection;
181 unsigned m_lineNumberAtConnection;
184 } // namespace WebCore
186 #endif // WorkerThreadableWebSocketChannel_h