| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2011 Google Inc. All rights reserved. | 2 * Copyright (C) 2011 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #ifndef WorkerThreadableWebSocketChannel_h | 31 #ifndef WorkerThreadableWebSocketChannel_h |
| 32 #define WorkerThreadableWebSocketChannel_h | 32 #define WorkerThreadableWebSocketChannel_h |
| 33 | 33 |
| 34 #include "core/inspector/ScriptCallFrame.h" |
| 35 #include "core/page/ConsoleTypes.h" |
| 34 #include "core/workers/WorkerContext.h" | 36 #include "core/workers/WorkerContext.h" |
| 35 #include "modules/websockets/WebSocketChannel.h" | 37 #include "modules/websockets/WebSocketChannel.h" |
| 36 #include "modules/websockets/WebSocketChannelClient.h" | 38 #include "modules/websockets/WebSocketChannelClient.h" |
| 37 | 39 |
| 40 #include "wtf/PassOwnPtr.h" |
| 38 #include "wtf/PassRefPtr.h" | 41 #include "wtf/PassRefPtr.h" |
| 39 #include "wtf/RefCounted.h" | 42 #include "wtf/RefCounted.h" |
| 40 #include "wtf/RefPtr.h" | 43 #include "wtf/RefPtr.h" |
| 41 #include "wtf/Threading.h" | 44 #include "wtf/Threading.h" |
| 45 #include "wtf/Vector.h" |
| 42 #include "wtf/text/WTFString.h" | 46 #include "wtf/text/WTFString.h" |
| 43 | 47 |
| 44 namespace WebCore { | 48 namespace WebCore { |
| 45 | 49 |
| 46 class KURL; | 50 class KURL; |
| 47 class ScriptExecutionContext; | 51 class ScriptExecutionContext; |
| 48 class ThreadableWebSocketChannelClientWrapper; | 52 class ThreadableWebSocketChannelClientWrapper; |
| 49 class WorkerContext; | 53 class WorkerContext; |
| 50 class WorkerLoaderProxy; | 54 class WorkerLoaderProxy; |
| 51 class WorkerRunLoop; | 55 class WorkerRunLoop; |
| 52 | 56 |
| 53 class WorkerThreadableWebSocketChannel : public RefCounted<WorkerThreadableWebSo
cketChannel>, public WebSocketChannel { | 57 class WorkerThreadableWebSocketChannel : public RefCounted<WorkerThreadableWebSo
cketChannel>, public WebSocketChannel { |
| 54 WTF_MAKE_FAST_ALLOCATED; | 58 WTF_MAKE_FAST_ALLOCATED; |
| 55 public: | 59 public: |
| 56 static PassRefPtr<WebSocketChannel> create(WorkerContext* workerContext, Web
SocketChannelClient* client, const String& taskMode) | 60 static PassRefPtr<WebSocketChannel> create(WorkerContext* workerContext, Web
SocketChannelClient* client, const String& taskMode) |
| 57 { | 61 { |
| 58 return adoptRef(new WorkerThreadableWebSocketChannel(workerContext, clie
nt, taskMode)); | 62 return adoptRef(new WorkerThreadableWebSocketChannel(workerContext, clie
nt, taskMode)); |
| 59 } | 63 } |
| 60 virtual ~WorkerThreadableWebSocketChannel(); | 64 virtual ~WorkerThreadableWebSocketChannel(); |
| 61 | 65 |
| 62 // WebSocketChannel functions. | 66 // WebSocketChannel functions. |
| 63 virtual void connect(const KURL&, const String& protocol) OVERRIDE; | 67 virtual void connect(const KURL&, const String& protocol) OVERRIDE; |
| 64 virtual String subprotocol() OVERRIDE; | 68 virtual String subprotocol() OVERRIDE; |
| 65 virtual String extensions() OVERRIDE; | 69 virtual String extensions() OVERRIDE; |
| 66 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; | 70 virtual WebSocketChannel::SendResult send(const String& message) OVERRIDE; |
| 67 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; | 71 virtual WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteO
ffset, unsigned byteLength) OVERRIDE; |
| 68 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; | 72 virtual WebSocketChannel::SendResult send(const Blob&) OVERRIDE; |
| 69 virtual unsigned long bufferedAmount() const OVERRIDE; | 73 virtual unsigned long bufferedAmount() const OVERRIDE; |
| 70 virtual void close(int code, const String& reason) OVERRIDE; | 74 virtual void close(int code, const String& reason) OVERRIDE; |
| 71 virtual void fail(const String& reason) OVERRIDE; | 75 virtual void fail(const String& reason, MessageLevel) OVERRIDE; |
| 76 virtual void fail(const String& reason, MessageLevel, PassOwnPtr<CallStackWr
apper>) OVERRIDE; |
| 72 virtual void disconnect() OVERRIDE; // Will suppress didClose(). | 77 virtual void disconnect() OVERRIDE; // Will suppress didClose(). |
| 73 virtual void suspend() OVERRIDE; | 78 virtual void suspend() OVERRIDE; |
| 74 virtual void resume() OVERRIDE; | 79 virtual void resume() OVERRIDE; |
| 75 | 80 |
| 76 // Generated by the bridge. The Peer and its bridge should have identical | 81 // Generated by the bridge. The Peer and its bridge should have identical |
| 77 // lifetimes. | 82 // lifetimes. |
| 78 class Peer : public WebSocketChannelClient { | 83 class Peer : public WebSocketChannelClient { |
| 79 WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED; | 84 WTF_MAKE_NONCOPYABLE(Peer); WTF_MAKE_FAST_ALLOCATED; |
| 80 public: | 85 public: |
| 81 static Peer* create(PassRefPtr<ThreadableWebSocketChannelClientWrapper>
clientWrapper, WorkerLoaderProxy& loaderProxy, ScriptExecutionContext* context,
const String& taskMode) | 86 // The ScriptCallFrame parameter provides the source filename and the li
ne number information at the connection initiation, |
| 87 // which may be shown when the connection fails. |
| 88 static Peer* create(PassRefPtr<ThreadableWebSocketChannelClientWrapper>
clientWrapper, WorkerLoaderProxy& loaderProxy, ScriptExecutionContext* context,
const String& taskMode, const ScriptCallFrame& frame) |
| 82 { | 89 { |
| 83 return new Peer(clientWrapper, loaderProxy, context, taskMode); | 90 return new Peer(clientWrapper, loaderProxy, context, taskMode, frame
); |
| 84 } | 91 } |
| 85 ~Peer(); | 92 ~Peer(); |
| 86 | 93 |
| 87 void connect(const KURL&, const String& protocol); | 94 void connect(const KURL&, const String& protocol); |
| 88 void send(const String& message); | 95 void send(const String& message); |
| 89 void send(const ArrayBuffer&); | 96 void send(const ArrayBuffer&); |
| 90 void send(const Blob&); | 97 void send(const Blob&); |
| 91 void bufferedAmount(); | 98 void bufferedAmount(); |
| 92 void close(int code, const String& reason); | 99 void close(int code, const String& reason); |
| 93 void fail(const String& reason); | 100 void fail(const String& reason, MessageLevel, PassOwnPtr<CallStackWrappe
r>); |
| 94 void disconnect(); | 101 void disconnect(); |
| 95 void suspend(); | 102 void suspend(); |
| 96 void resume(); | 103 void resume(); |
| 97 | 104 |
| 98 // WebSocketChannelClient functions. | 105 // WebSocketChannelClient functions. |
| 99 virtual void didConnect() OVERRIDE; | 106 virtual void didConnect() OVERRIDE; |
| 100 virtual void didReceiveMessage(const String& message) OVERRIDE; | 107 virtual void didReceiveMessage(const String& message) OVERRIDE; |
| 101 virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE; | 108 virtual void didReceiveBinaryData(PassOwnPtr<Vector<char> >) OVERRIDE; |
| 102 virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERR
IDE; | 109 virtual void didUpdateBufferedAmount(unsigned long bufferedAmount) OVERR
IDE; |
| 103 virtual void didStartClosingHandshake() OVERRIDE; | 110 virtual void didStartClosingHandshake() OVERRIDE; |
| 104 virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHand
shakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE; | 111 virtual void didClose(unsigned long unhandledBufferedAmount, ClosingHand
shakeCompletionStatus, unsigned short code, const String& reason) OVERRIDE; |
| 105 virtual void didReceiveMessageError() OVERRIDE; | 112 virtual void didReceiveMessageError() OVERRIDE; |
| 106 | 113 |
| 107 private: | 114 private: |
| 108 Peer(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderPr
oxy&, ScriptExecutionContext*, const String& taskMode); | 115 Peer(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, WorkerLoaderPr
oxy&, ScriptExecutionContext*, const String& taskMode, const ScriptCallFrame&); |
| 109 | 116 |
| 110 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; | 117 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; |
| 111 WorkerLoaderProxy& m_loaderProxy; | 118 WorkerLoaderProxy& m_loaderProxy; |
| 112 RefPtr<WebSocketChannel> m_mainWebSocketChannel; | 119 RefPtr<WebSocketChannel> m_mainWebSocketChannel; |
| 113 String m_taskMode; | 120 String m_taskMode; |
| 114 }; | 121 }; |
| 115 | 122 |
| 116 using RefCounted<WorkerThreadableWebSocketChannel>::ref; | 123 using RefCounted<WorkerThreadableWebSocketChannel>::ref; |
| 117 using RefCounted<WorkerThreadableWebSocketChannel>::deref; | 124 using RefCounted<WorkerThreadableWebSocketChannel>::deref; |
| 118 | 125 |
| 119 protected: | 126 protected: |
| 120 // WebSocketChannel functions. | 127 // WebSocketChannel functions. |
| 121 virtual void refWebSocketChannel() OVERRIDE { ref(); } | 128 virtual void refWebSocketChannel() OVERRIDE { ref(); } |
| 122 virtual void derefWebSocketChannel() OVERRIDE { deref(); } | 129 virtual void derefWebSocketChannel() OVERRIDE { deref(); } |
| 123 | 130 |
| 124 private: | 131 private: |
| 125 // Bridge for Peer. Running on the worker thread. | 132 // Bridge for Peer. Running on the worker thread. |
| 126 class Bridge : public RefCounted<Bridge> { | 133 class Bridge : public RefCounted<Bridge> { |
| 127 public: | 134 public: |
| 128 static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelCl
ientWrapper> workerClientWrapper, PassRefPtr<WorkerContext> workerContext, const
String& taskMode) | 135 static PassRefPtr<Bridge> create(PassRefPtr<ThreadableWebSocketChannelCl
ientWrapper> workerClientWrapper, PassRefPtr<WorkerContext> workerContext, const
String& taskMode) |
| 129 { | 136 { |
| 130 return adoptRef(new Bridge(workerClientWrapper, workerContext, taskM
ode)); | 137 return adoptRef(new Bridge(workerClientWrapper, workerContext, taskM
ode)); |
| 131 } | 138 } |
| 132 ~Bridge(); | 139 ~Bridge(); |
| 133 void initialize(); | 140 // The ScriptCallFrame parameter provides the source filename and the li
ne number information at the connection initiation, |
| 141 // which may be shown when the connection fails. |
| 142 void initialize(const ScriptCallFrame&); |
| 134 void connect(const KURL&, const String& protocol); | 143 void connect(const KURL&, const String& protocol); |
| 135 WebSocketChannel::SendResult send(const String& message); | 144 WebSocketChannel::SendResult send(const String& message); |
| 136 WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffse
t, unsigned byteLength); | 145 WebSocketChannel::SendResult send(const ArrayBuffer&, unsigned byteOffse
t, unsigned byteLength); |
| 137 WebSocketChannel::SendResult send(const Blob&); | 146 WebSocketChannel::SendResult send(const Blob&); |
| 138 unsigned long bufferedAmount(); | 147 unsigned long bufferedAmount(); |
| 139 void close(int code, const String& reason); | 148 void close(int code, const String& reason); |
| 140 void fail(const String& reason); | 149 void fail(const String& reason, MessageLevel, PassOwnPtr<CallStackWrappe
r>); |
| 141 void disconnect(); | 150 void disconnect(); |
| 142 void suspend(); | 151 void suspend(); |
| 143 void resume(); | 152 void resume(); |
| 144 | 153 |
| 145 using RefCounted<Bridge>::ref; | 154 using RefCounted<Bridge>::ref; |
| 146 using RefCounted<Bridge>::deref; | 155 using RefCounted<Bridge>::deref; |
| 147 | 156 |
| 148 private: | 157 private: |
| 149 Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassRefPtr<W
orkerContext>, const String& taskMode); | 158 Bridge(PassRefPtr<ThreadableWebSocketChannelClientWrapper>, PassRefPtr<W
orkerContext>, const String& taskMode); |
| 150 | 159 |
| 151 static void setWebSocketChannel(ScriptExecutionContext*, Bridge* thisPtr
, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>); | 160 static void setWebSocketChannel(ScriptExecutionContext*, Bridge* thisPtr
, Peer*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>); |
| 152 | 161 |
| 153 // Executed on the main thread to create a Peer for this bridge. | 162 // Executed on the main thread to create a Peer for this bridge. |
| 154 static void mainThreadInitialize(ScriptExecutionContext*, WorkerLoaderPr
oxy*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& taskMod
e); | 163 // sourceURL and lineNumber provides the source filename and the line nu
mber information at the connection initiation respectively. |
| 164 // They may be shown when the connection fails. |
| 165 static void mainThreadInitialize(ScriptExecutionContext*, WorkerLoaderPr
oxy*, PassRefPtr<ThreadableWebSocketChannelClientWrapper>, const String& taskMod
e, const String& sourceURL, unsigned lineNumber); |
| 155 | 166 |
| 156 // Executed on the worker context's thread. | 167 // Executed on the worker context's thread. |
| 157 void clearClientWrapper(); | 168 void clearClientWrapper(); |
| 158 | 169 |
| 159 void setMethodNotCompleted(); | 170 void setMethodNotCompleted(); |
| 160 void waitForMethodCompletion(); | 171 void waitForMethodCompletion(); |
| 161 | 172 |
| 162 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; | 173 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; |
| 163 RefPtr<WorkerContext> m_workerContext; | 174 RefPtr<WorkerContext> m_workerContext; |
| 164 WorkerLoaderProxy& m_loaderProxy; | 175 WorkerLoaderProxy& m_loaderProxy; |
| 165 String m_taskMode; | 176 String m_taskMode; |
| 166 Peer* m_peer; | 177 Peer* m_peer; |
| 167 }; | 178 }; |
| 168 | 179 |
| 169 WorkerThreadableWebSocketChannel(WorkerContext*, WebSocketChannelClient*, co
nst String& taskMode); | 180 WorkerThreadableWebSocketChannel(WorkerContext*, WebSocketChannelClient*, co
nst String& taskMode); |
| 170 | 181 |
| 171 static void mainThreadConnect(ScriptExecutionContext*, Peer*, const KURL&, c
onst String& protocol); | 182 static void mainThreadConnect(ScriptExecutionContext*, Peer*, const KURL&, c
onst String& protocol); |
| 172 static void mainThreadSend(ScriptExecutionContext*, Peer*, const String& mes
sage); | 183 static void mainThreadSend(ScriptExecutionContext*, Peer*, const String& mes
sage); |
| 173 static void mainThreadSendArrayBuffer(ScriptExecutionContext*, Peer*, PassOw
nPtr<Vector<char> >); | 184 static void mainThreadSendArrayBuffer(ScriptExecutionContext*, Peer*, PassOw
nPtr<Vector<char> >); |
| 174 static void mainThreadSendBlob(ScriptExecutionContext*, Peer*, const KURL&,
const String& type, long long size); | 185 static void mainThreadSendBlob(ScriptExecutionContext*, Peer*, const KURL&,
const String& type, long long size); |
| 175 static void mainThreadBufferedAmount(ScriptExecutionContext*, Peer*); | 186 static void mainThreadBufferedAmount(ScriptExecutionContext*, Peer*); |
| 176 static void mainThreadClose(ScriptExecutionContext*, Peer*, int code, const
String& reason); | 187 static void mainThreadClose(ScriptExecutionContext*, Peer*, int code, const
String& reason); |
| 177 static void mainThreadFail(ScriptExecutionContext*, Peer*, const String& rea
son); | 188 static void mainThreadFail(ScriptExecutionContext*, Peer*, const String& rea
son, MessageLevel, PassOwnPtr<CallStackWrapper>); |
| 178 static void mainThreadDestroy(ScriptExecutionContext*, PassOwnPtr<Peer>); | 189 static void mainThreadDestroy(ScriptExecutionContext*, PassOwnPtr<Peer>); |
| 179 static void mainThreadSuspend(ScriptExecutionContext*, Peer*); | 190 static void mainThreadSuspend(ScriptExecutionContext*, Peer*); |
| 180 static void mainThreadResume(ScriptExecutionContext*, Peer*); | 191 static void mainThreadResume(ScriptExecutionContext*, Peer*); |
| 181 | 192 |
| 182 class WorkerContextDidInitializeTask; | 193 class WorkerContextDidInitializeTask; |
| 183 | 194 |
| 184 RefPtr<WorkerContext> m_workerContext; | 195 RefPtr<WorkerContext> m_workerContext; |
| 185 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; | 196 RefPtr<ThreadableWebSocketChannelClientWrapper> m_workerClientWrapper; |
| 186 RefPtr<Bridge> m_bridge; | 197 RefPtr<Bridge> m_bridge; |
| 198 ScriptCallFrame m_callFrameAtConnection; |
| 187 }; | 199 }; |
| 188 | 200 |
| 189 } // namespace WebCore | 201 } // namespace WebCore |
| 190 | 202 |
| 191 #endif // WorkerThreadableWebSocketChannel_h | 203 #endif // WorkerThreadableWebSocketChannel_h |
| OLD | NEW |