OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011, 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2011, 2012 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 12 matching lines...) Expand all Loading... |
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
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 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #include "modules/websockets/WorkerThreadableWebSocketChannel.h" | 33 #include "modules/websockets/WorkerWebSocketChannel.h" |
34 | 34 |
35 #include "bindings/core/v8/ScriptCallStackFactory.h" | 35 #include "bindings/core/v8/ScriptCallStackFactory.h" |
36 #include "core/dom/CrossThreadTask.h" | 36 #include "core/dom/CrossThreadTask.h" |
37 #include "core/dom/Document.h" | 37 #include "core/dom/Document.h" |
38 #include "core/dom/ExecutionContext.h" | 38 #include "core/dom/ExecutionContext.h" |
39 #include "core/dom/ExecutionContextTask.h" | 39 #include "core/dom/ExecutionContextTask.h" |
40 #include "core/fileapi/Blob.h" | 40 #include "core/fileapi/Blob.h" |
41 #include "core/inspector/ScriptCallFrame.h" | 41 #include "core/inspector/ScriptCallFrame.h" |
42 #include "core/inspector/ScriptCallStack.h" | 42 #include "core/inspector/ScriptCallStack.h" |
43 #include "core/workers/WorkerGlobalScope.h" | 43 #include "core/workers/WorkerGlobalScope.h" |
44 #include "core/workers/WorkerLoaderProxy.h" | 44 #include "core/workers/WorkerLoaderProxy.h" |
45 #include "core/workers/WorkerThread.h" | 45 #include "core/workers/WorkerThread.h" |
46 #include "modules/websockets/DocumentWebSocketChannel.h" | 46 #include "modules/websockets/DocumentWebSocketChannel.h" |
47 #include "public/platform/Platform.h" | 47 #include "public/platform/Platform.h" |
48 #include "public/platform/WebWaitableEvent.h" | 48 #include "public/platform/WebWaitableEvent.h" |
49 #include "wtf/ArrayBuffer.h" | 49 #include "wtf/ArrayBuffer.h" |
50 #include "wtf/Assertions.h" | 50 #include "wtf/Assertions.h" |
51 #include "wtf/Functional.h" | 51 #include "wtf/Functional.h" |
52 #include "wtf/MainThread.h" | 52 #include "wtf/MainThread.h" |
53 #include "wtf/text/WTFString.h" | 53 #include "wtf/text/WTFString.h" |
54 | 54 |
55 namespace blink { | 55 namespace blink { |
56 | 56 |
57 typedef WorkerThreadableWebSocketChannel::Bridge Bridge; | 57 typedef WorkerWebSocketChannel::Bridge Bridge; |
58 typedef WorkerThreadableWebSocketChannel::Peer Peer; | 58 typedef WorkerWebSocketChannel::Peer Peer; |
59 | 59 |
60 // Created and destroyed on the worker thread. All setters of this class are | 60 // Created and destroyed on the worker thread. All setters of this class are |
61 // called on the main thread, while all getters are called on the worker | 61 // called on the main thread, while all getters are called on the worker |
62 // thread. signalWorkerThread() must be called before any getters are called. | 62 // thread. signalWorkerThread() must be called before any getters are called. |
63 class ThreadableWebSocketChannelSyncHelper : public GarbageCollectedFinalized<Th
readableWebSocketChannelSyncHelper> { | 63 class WebSocketChannelSyncHelper : public GarbageCollectedFinalized<WebSocketCha
nnelSyncHelper> { |
64 public: | 64 public: |
65 static ThreadableWebSocketChannelSyncHelper* create(PassOwnPtr<WebWaitableEv
ent> event) | 65 static WebSocketChannelSyncHelper* create(PassOwnPtr<WebWaitableEvent> event
) |
66 { | 66 { |
67 return new ThreadableWebSocketChannelSyncHelper(event); | 67 return new WebSocketChannelSyncHelper(event); |
68 } | 68 } |
69 | 69 |
70 ~ThreadableWebSocketChannelSyncHelper() | 70 ~WebSocketChannelSyncHelper() |
71 { | 71 { |
72 } | 72 } |
73 | 73 |
74 // All setters are called on the main thread. | 74 // All setters are called on the main thread. |
75 void setConnectRequestResult(bool connectRequestResult) | 75 void setConnectRequestResult(bool connectRequestResult) |
76 { | 76 { |
77 m_connectRequestResult = connectRequestResult; | 77 m_connectRequestResult = connectRequestResult; |
78 } | 78 } |
79 | 79 |
80 // All getter are called on the worker thread. | 80 // All getter are called on the worker thread. |
81 bool connectRequestResult() const | 81 bool connectRequestResult() const |
82 { | 82 { |
83 return m_connectRequestResult; | 83 return m_connectRequestResult; |
84 } | 84 } |
85 | 85 |
86 // This should be called after all setters are called and before any | 86 // This should be called after all setters are called and before any |
87 // getters are called. | 87 // getters are called. |
88 void signalWorkerThread() | 88 void signalWorkerThread() |
89 { | 89 { |
90 m_event->signal(); | 90 m_event->signal(); |
91 } | 91 } |
92 void wait() | 92 void wait() |
93 { | 93 { |
94 m_event->wait(); | 94 m_event->wait(); |
95 } | 95 } |
96 | 96 |
97 void trace(Visitor* visitor) { } | 97 void trace(Visitor* visitor) { } |
98 | 98 |
99 private: | 99 private: |
100 explicit ThreadableWebSocketChannelSyncHelper(PassOwnPtr<WebWaitableEvent> e
vent) | 100 explicit WebSocketChannelSyncHelper(PassOwnPtr<WebWaitableEvent> event) |
101 : m_event(event) | 101 : m_event(event) |
102 , m_connectRequestResult(false) | 102 , m_connectRequestResult(false) |
103 { | 103 { |
104 } | 104 } |
105 | 105 |
106 OwnPtr<WebWaitableEvent> m_event; | 106 OwnPtr<WebWaitableEvent> m_event; |
107 bool m_connectRequestResult; | 107 bool m_connectRequestResult; |
108 }; | 108 }; |
109 | 109 |
110 WorkerThreadableWebSocketChannel::WorkerThreadableWebSocketChannel(WorkerGlobalS
cope& workerGlobalScope, WebSocketChannelClient* client, const String& sourceURL
, unsigned lineNumber) | 110 WorkerWebSocketChannel::WorkerWebSocketChannel(WorkerGlobalScope& workerGlobalSc
ope, WebSocketChannelClient* client, const String& sourceURL, unsigned lineNumbe
r) |
111 : m_bridge(new Bridge(client, workerGlobalScope)) | 111 : m_bridge(new Bridge(client, workerGlobalScope)) |
112 , m_sourceURLAtConnection(sourceURL) | 112 , m_sourceURLAtConnection(sourceURL) |
113 , m_lineNumberAtConnection(lineNumber) | 113 , m_lineNumberAtConnection(lineNumber) |
114 { | 114 { |
115 m_bridge->initialize(sourceURL, lineNumber); | 115 m_bridge->initialize(sourceURL, lineNumber); |
116 } | 116 } |
117 | 117 |
118 WorkerThreadableWebSocketChannel::~WorkerThreadableWebSocketChannel() | 118 WorkerWebSocketChannel::~WorkerWebSocketChannel() |
119 { | 119 { |
120 ASSERT(!m_bridge); | 120 ASSERT(!m_bridge); |
121 } | 121 } |
122 | 122 |
123 bool WorkerThreadableWebSocketChannel::connect(const KURL& url, const String& pr
otocol) | 123 bool WorkerWebSocketChannel::connect(const KURL& url, const String& protocol) |
124 { | 124 { |
125 ASSERT(m_bridge); | 125 ASSERT(m_bridge); |
126 return m_bridge->connect(url, protocol); | 126 return m_bridge->connect(url, protocol); |
127 } | 127 } |
128 | 128 |
129 void WorkerThreadableWebSocketChannel::send(const String& message) | 129 void WorkerWebSocketChannel::send(const String& message) |
130 { | 130 { |
131 ASSERT(m_bridge); | 131 ASSERT(m_bridge); |
132 m_bridge->send(message); | 132 m_bridge->send(message); |
133 } | 133 } |
134 | 134 |
135 void WorkerThreadableWebSocketChannel::send(const ArrayBuffer& binaryData, unsig
ned byteOffset, unsigned byteLength) | 135 void WorkerWebSocketChannel::send(const ArrayBuffer& binaryData, unsigned byteOf
fset, unsigned byteLength) |
136 { | 136 { |
137 ASSERT(m_bridge); | 137 ASSERT(m_bridge); |
138 m_bridge->send(binaryData, byteOffset, byteLength); | 138 m_bridge->send(binaryData, byteOffset, byteLength); |
139 } | 139 } |
140 | 140 |
141 void WorkerThreadableWebSocketChannel::send(PassRefPtr<BlobDataHandle> blobData) | 141 void WorkerWebSocketChannel::send(PassRefPtr<BlobDataHandle> blobData) |
142 { | 142 { |
143 ASSERT(m_bridge); | 143 ASSERT(m_bridge); |
144 m_bridge->send(blobData); | 144 m_bridge->send(blobData); |
145 } | 145 } |
146 | 146 |
147 void WorkerThreadableWebSocketChannel::close(int code, const String& reason) | 147 void WorkerWebSocketChannel::close(int code, const String& reason) |
148 { | 148 { |
149 ASSERT(m_bridge); | 149 ASSERT(m_bridge); |
150 m_bridge->close(code, reason); | 150 m_bridge->close(code, reason); |
151 } | 151 } |
152 | 152 |
153 void WorkerThreadableWebSocketChannel::fail(const String& reason, MessageLevel l
evel, const String& sourceURL, unsigned lineNumber) | 153 void WorkerWebSocketChannel::fail(const String& reason, MessageLevel level, cons
t String& sourceURL, unsigned lineNumber) |
154 { | 154 { |
155 if (!m_bridge) | 155 if (!m_bridge) |
156 return; | 156 return; |
157 | 157 |
158 RefPtrWillBeRawPtr<ScriptCallStack> callStack = createScriptCallStack(1, tru
e); | 158 RefPtrWillBeRawPtr<ScriptCallStack> callStack = createScriptCallStack(1, tru
e); |
159 if (callStack && callStack->size()) { | 159 if (callStack && callStack->size()) { |
160 // In order to emulate the ConsoleMessage behavior, | 160 // In order to emulate the ConsoleMessage behavior, |
161 // we should ignore the specified url and line number if | 161 // we should ignore the specified url and line number if |
162 // we can get the JavaScript context. | 162 // we can get the JavaScript context. |
163 m_bridge->fail(reason, level, callStack->at(0).sourceURL(), callStack->a
t(0).lineNumber()); | 163 m_bridge->fail(reason, level, callStack->at(0).sourceURL(), callStack->a
t(0).lineNumber()); |
164 } else if (sourceURL.isEmpty() && !lineNumber) { | 164 } else if (sourceURL.isEmpty() && !lineNumber) { |
165 // No information is specified by the caller - use the url | 165 // No information is specified by the caller - use the url |
166 // and the line number at the connection. | 166 // and the line number at the connection. |
167 m_bridge->fail(reason, level, m_sourceURLAtConnection, m_lineNumberAtCon
nection); | 167 m_bridge->fail(reason, level, m_sourceURLAtConnection, m_lineNumberAtCon
nection); |
168 } else { | 168 } else { |
169 // Use the specified information. | 169 // Use the specified information. |
170 m_bridge->fail(reason, level, sourceURL, lineNumber); | 170 m_bridge->fail(reason, level, sourceURL, lineNumber); |
171 } | 171 } |
172 } | 172 } |
173 | 173 |
174 void WorkerThreadableWebSocketChannel::disconnect() | 174 void WorkerWebSocketChannel::disconnect() |
175 { | 175 { |
176 m_bridge->disconnect(); | 176 m_bridge->disconnect(); |
177 m_bridge.clear(); | 177 m_bridge.clear(); |
178 } | 178 } |
179 | 179 |
180 void WorkerThreadableWebSocketChannel::trace(Visitor* visitor) | 180 void WorkerWebSocketChannel::trace(Visitor* visitor) |
181 { | 181 { |
182 visitor->trace(m_bridge); | 182 visitor->trace(m_bridge); |
183 WebSocketChannel::trace(visitor); | 183 WebSocketChannel::trace(visitor); |
184 } | 184 } |
185 | 185 |
186 Peer::Peer(Bridge* bridge, WorkerLoaderProxy& loaderProxy, ThreadableWebSocketCh
annelSyncHelper* syncHelper) | 186 Peer::Peer(Bridge* bridge, WorkerLoaderProxy& loaderProxy, WebSocketChannelSyncH
elper* syncHelper) |
187 : m_bridge(bridge) | 187 : m_bridge(bridge) |
188 , m_loaderProxy(loaderProxy) | 188 , m_loaderProxy(loaderProxy) |
189 , m_mainWebSocketChannel(nullptr) | 189 , m_mainWebSocketChannel(nullptr) |
190 , m_syncHelper(syncHelper) | 190 , m_syncHelper(syncHelper) |
191 { | 191 { |
192 ASSERT(!isMainThread()); | 192 ASSERT(!isMainThread()); |
193 } | 193 } |
194 | 194 |
195 Peer::~Peer() | 195 Peer::~Peer() |
196 { | 196 { |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 visitor->trace(m_bridge); | 368 visitor->trace(m_bridge); |
369 visitor->trace(m_mainWebSocketChannel); | 369 visitor->trace(m_mainWebSocketChannel); |
370 visitor->trace(m_syncHelper); | 370 visitor->trace(m_syncHelper); |
371 WebSocketChannelClient::trace(visitor); | 371 WebSocketChannelClient::trace(visitor); |
372 } | 372 } |
373 | 373 |
374 Bridge::Bridge(WebSocketChannelClient* client, WorkerGlobalScope& workerGlobalSc
ope) | 374 Bridge::Bridge(WebSocketChannelClient* client, WorkerGlobalScope& workerGlobalSc
ope) |
375 : m_client(client) | 375 : m_client(client) |
376 , m_workerGlobalScope(workerGlobalScope) | 376 , m_workerGlobalScope(workerGlobalScope) |
377 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy()) | 377 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy()) |
378 , m_syncHelper(ThreadableWebSocketChannelSyncHelper::create(adoptPtr(Platfor
m::current()->createWaitableEvent()))) | 378 , m_syncHelper(WebSocketChannelSyncHelper::create(adoptPtr(Platform::current
()->createWaitableEvent()))) |
379 , m_peer(new Peer(this, m_loaderProxy, m_syncHelper)) | 379 , m_peer(new Peer(this, m_loaderProxy, m_syncHelper)) |
380 { | 380 { |
381 } | 381 } |
382 | 382 |
383 Bridge::~Bridge() | 383 Bridge::~Bridge() |
384 { | 384 { |
385 ASSERT(!m_peer); | 385 ASSERT(!m_peer); |
386 } | 386 } |
387 | 387 |
388 void Bridge::initialize(const String& sourceURL, unsigned lineNumber) | 388 void Bridge::initialize(const String& sourceURL, unsigned lineNumber) |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 | 473 |
474 void Bridge::trace(Visitor* visitor) | 474 void Bridge::trace(Visitor* visitor) |
475 { | 475 { |
476 visitor->trace(m_client); | 476 visitor->trace(m_client); |
477 visitor->trace(m_workerGlobalScope); | 477 visitor->trace(m_workerGlobalScope); |
478 visitor->trace(m_syncHelper); | 478 visitor->trace(m_syncHelper); |
479 visitor->trace(m_peer); | 479 visitor->trace(m_peer); |
480 } | 480 } |
481 | 481 |
482 } // namespace blink | 482 } // namespace blink |
OLD | NEW |