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 "modules/websockets/WebSocketChannel.h" 36 #include "modules/websockets/WebSocketChannelClient.h" 37 #include "platform/heap/Handle.h" 38 #include "wtf/Assertions.h" 39 #include "wtf/Forward.h" 40 #include "wtf/OwnPtr.h" 41 #include "wtf/RefPtr.h" 42 #include "wtf/Vector.h" 43 #include "wtf/text/WTFString.h" 44 45 namespace blink { 46 47 class BlobDataHandle; 48 class KURL; 49 class ExecutionContext; 50 class ExecutionContextTask; 51 class ThreadableWebSocketChannelSyncHelper; 52 class WorkerGlobalScope; 53 class WorkerLoaderProxy; 54 55 class WorkerThreadableWebSocketChannel FINAL : public WebSocketChannel { 56 WTF_MAKE_NONCOPYABLE(WorkerThreadableWebSocketChannel); 57 public: create(WorkerGlobalScope & workerGlobalScope,WebSocketChannelClient * client,const String & sourceURL,unsigned lineNumber)58 static WebSocketChannel* create(WorkerGlobalScope& workerGlobalScope, WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumber) 59 { 60 return adoptRefCountedGarbageCollected(new WorkerThreadableWebSocketChannel(workerGlobalScope, client, sourceURL, lineNumber)); 61 } 62 virtual ~WorkerThreadableWebSocketChannel(); 63 64 // WebSocketChannel functions. 65 virtual bool connect(const KURL&, const String& protocol) OVERRIDE; 66 virtual void send(const String& message) OVERRIDE; 67 virtual void send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength) OVERRIDE; 68 virtual void send(PassRefPtr<BlobDataHandle>) OVERRIDE; send(PassOwnPtr<Vector<char>>)69 virtual void send(PassOwnPtr<Vector<char> >) OVERRIDE 70 { 71 ASSERT_NOT_REACHED(); 72 } 73 virtual void close(int code, const String& reason) OVERRIDE; 74 virtual void fail(const String& reason, MessageLevel, const String&, unsigned) OVERRIDE; 75 virtual void disconnect() OVERRIDE; // Will suppress didClose(). suspend()76 virtual void suspend() OVERRIDE { } resume()77 virtual void resume() OVERRIDE { } 78 79 virtual void trace(Visitor*) OVERRIDE; 80 81 class Bridge; 82 // Allocated in the worker thread, but used in the main thread. 83 class Peer FINAL : public GarbageCollectedFinalized<Peer>, public WebSocketChannelClient { 84 USING_GARBAGE_COLLECTED_MIXIN(Peer); 85 WTF_MAKE_NONCOPYABLE(Peer); 86 public: 87 Peer(Bridge*, WorkerLoaderProxy&, ThreadableWebSocketChannelSyncHelper*); 88 virtual ~Peer(); 89 90 // sourceURLAtConnection and lineNumberAtConnection parameters may 91 // be shown when the connection fails. initialize(ExecutionContext * executionContext,Peer * peer,const String & sourceURLAtConnection,unsigned lineNumberAtConnection)92 static void initialize(ExecutionContext* executionContext, Peer* peer, const String& sourceURLAtConnection, unsigned lineNumberAtConnection) 93 { 94 peer->initializeInternal(executionContext, sourceURLAtConnection, lineNumberAtConnection); 95 } 96 97 void connect(const KURL&, const String& protocol); 98 void send(const String& message); 99 void sendArrayBuffer(PassOwnPtr<Vector<char> >); 100 void sendBlob(PassRefPtr<BlobDataHandle>); 101 void bufferedAmount(); 102 void close(int code, const String& reason); 103 void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber); 104 void disconnect(); 105 106 virtual void trace(Visitor*) OVERRIDE; 107 108 // WebSocketChannelClient functions. 109 virtual void didConnect(const String& subprotocol, const String& extensions) OVERRIDE; 110 virtual void didReceiveMessage(const String& message) OVERRIDE; 111 virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE; 112 virtual void didConsumeBufferedAmount(unsigned long) OVERRIDE; 113 virtual void didStartClosingHandshake() OVERRIDE; 114 virtual void didClose(ClosingHandshakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE; 115 virtual void didReceiveMessageError() OVERRIDE; 116 117 private: 118 void initializeInternal(ExecutionContext*, const String& sourceURLAtConnection, unsigned lineNumberAtConnection); 119 120 Member<Bridge> m_bridge; 121 WorkerLoaderProxy& m_loaderProxy; 122 Member<WebSocketChannel> m_mainWebSocketChannel; 123 Member<ThreadableWebSocketChannelSyncHelper> m_syncHelper; 124 }; 125 126 // Bridge for Peer. Running on the worker thread. 127 class Bridge FINAL : public GarbageCollectedFinalized<Bridge> { 128 WTF_MAKE_NONCOPYABLE(Bridge); 129 public: 130 Bridge(WebSocketChannelClient*, WorkerGlobalScope&); 131 ~Bridge(); 132 // sourceURLAtConnection and lineNumberAtConnection parameters may 133 // be shown when the connection fails. 134 void initialize(const String& sourceURLAtConnection, unsigned lineNumberAtConnection); 135 bool connect(const KURL&, const String& protocol); 136 void send(const String& message); 137 void send(const ArrayBuffer&, unsigned byteOffset, unsigned byteLength); 138 void send(PassRefPtr<BlobDataHandle>); 139 unsigned long bufferedAmount(); 140 void close(int code, const String& reason); 141 void fail(const String& reason, MessageLevel, const String& sourceURL, unsigned lineNumber); 142 void disconnect(); 143 144 // Returns null when |disconnect| has already been called. client()145 WebSocketChannelClient* client() { return m_client; } 146 147 void trace(Visitor*); 148 149 private: 150 // Returns false if shutdown event is received before method completion. 151 bool waitForMethodCompletion(PassOwnPtr<ExecutionContextTask>); 152 153 Member<WebSocketChannelClient> m_client; 154 RefPtrWillBeMember<WorkerGlobalScope> m_workerGlobalScope; 155 WorkerLoaderProxy& m_loaderProxy; 156 Member<ThreadableWebSocketChannelSyncHelper> m_syncHelper; 157 Member<Peer> m_peer; 158 }; 159 160 private: 161 WorkerThreadableWebSocketChannel(WorkerGlobalScope&, WebSocketChannelClient*, const String& sourceURL, unsigned lineNumber); 162 163 Member<Bridge> m_bridge; 164 String m_sourceURLAtConnection; 165 unsigned m_lineNumberAtConnection; 166 }; 167 168 } // namespace blink 169 170 #endif // WorkerThreadableWebSocketChannel_h 171