| 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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 #include <memory> | 51 #include <memory> |
| 52 | 52 |
| 53 namespace blink { | 53 namespace blink { |
| 54 | 54 |
| 55 typedef WorkerWebSocketChannel::Bridge Bridge; | 55 typedef WorkerWebSocketChannel::Bridge Bridge; |
| 56 typedef WorkerWebSocketChannel::Peer Peer; | 56 typedef WorkerWebSocketChannel::Peer Peer; |
| 57 | 57 |
| 58 // Created and destroyed on the worker thread. All setters of this class are | 58 // Created and destroyed on the worker thread. All setters of this class are |
| 59 // called on the main thread, while all getters are called on the worker | 59 // called on the main thread, while all getters are called on the worker |
| 60 // thread. signalWorkerThread() must be called before any getters are called. | 60 // thread. signalWorkerThread() must be called before any getters are called. |
| 61 class WebSocketChannelSyncHelper : public GarbageCollectedFinalized<WebSocketCha
nnelSyncHelper> { | 61 class WebSocketChannelSyncHelper { |
| 62 public: | 62 public: |
| 63 static WebSocketChannelSyncHelper* create(std::unique_ptr<WaitableEvent> eve
nt) | 63 WebSocketChannelSyncHelper() {} |
| 64 { | 64 ~WebSocketChannelSyncHelper() {} |
| 65 return new WebSocketChannelSyncHelper(std::move(event)); | |
| 66 } | |
| 67 | |
| 68 ~WebSocketChannelSyncHelper() | |
| 69 { | |
| 70 } | |
| 71 | 65 |
| 72 // All setters are called on the main thread. | 66 // All setters are called on the main thread. |
| 73 void setConnectRequestResult(bool connectRequestResult) | 67 void setConnectRequestResult(bool connectRequestResult) |
| 74 { | 68 { |
| 69 DCHECK(isMainThread()); |
| 75 m_connectRequestResult = connectRequestResult; | 70 m_connectRequestResult = connectRequestResult; |
| 76 } | 71 } |
| 77 | 72 |
| 78 // All getter are called on the worker thread. | 73 // All getters are called on the worker thread. |
| 79 bool connectRequestResult() const | 74 bool connectRequestResult() const |
| 80 { | 75 { |
| 76 DCHECK(!isMainThread()); |
| 81 return m_connectRequestResult; | 77 return m_connectRequestResult; |
| 82 } | 78 } |
| 83 | 79 |
| 84 // This should be called after all setters are called and before any | 80 // This should be called after all setters are called and before any |
| 85 // getters are called. | 81 // getters are called. |
| 86 void signalWorkerThread() | 82 void signalWorkerThread() |
| 87 { | 83 { |
| 88 m_event->signal(); | 84 DCHECK(isMainThread()); |
| 85 m_event.signal(); |
| 89 } | 86 } |
| 87 |
| 90 void wait() | 88 void wait() |
| 91 { | 89 { |
| 92 m_event->wait(); | 90 DCHECK(!isMainThread()); |
| 91 m_event.wait(); |
| 93 } | 92 } |
| 94 | 93 |
| 95 DEFINE_INLINE_TRACE() { } | |
| 96 | |
| 97 private: | 94 private: |
| 98 explicit WebSocketChannelSyncHelper(std::unique_ptr<WaitableEvent> event) | 95 WaitableEvent m_event; |
| 99 : m_event(std::move(event)) | 96 bool m_connectRequestResult = false; |
| 100 , m_connectRequestResult(false) | |
| 101 { | |
| 102 } | |
| 103 | |
| 104 std::unique_ptr<WaitableEvent> m_event; | |
| 105 bool m_connectRequestResult; | |
| 106 }; | 97 }; |
| 107 | 98 |
| 108 WorkerWebSocketChannel::WorkerWebSocketChannel(WorkerGlobalScope& workerGlobalSc
ope, WebSocketChannelClient* client, std::unique_ptr<SourceLocation> location) | 99 WorkerWebSocketChannel::WorkerWebSocketChannel(WorkerGlobalScope& workerGlobalSc
ope, WebSocketChannelClient* client, std::unique_ptr<SourceLocation> location) |
| 109 : m_bridge(new Bridge(client, workerGlobalScope)) | 100 : m_bridge(new Bridge(client, workerGlobalScope)) |
| 110 , m_locationAtConnection(std::move(location)) | 101 , m_locationAtConnection(std::move(location)) |
| 111 { | 102 { |
| 112 } | 103 } |
| 113 | 104 |
| 114 WorkerWebSocketChannel::~WorkerWebSocketChannel() | 105 WorkerWebSocketChannel::~WorkerWebSocketChannel() |
| 115 { | 106 { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 m_bridge->disconnect(); | 162 m_bridge->disconnect(); |
| 172 m_bridge.clear(); | 163 m_bridge.clear(); |
| 173 } | 164 } |
| 174 | 165 |
| 175 DEFINE_TRACE(WorkerWebSocketChannel) | 166 DEFINE_TRACE(WorkerWebSocketChannel) |
| 176 { | 167 { |
| 177 visitor->trace(m_bridge); | 168 visitor->trace(m_bridge); |
| 178 WebSocketChannel::trace(visitor); | 169 WebSocketChannel::trace(visitor); |
| 179 } | 170 } |
| 180 | 171 |
| 181 Peer::Peer(Bridge* bridge, PassRefPtr<WorkerLoaderProxy> loaderProxy, WebSocketC
hannelSyncHelper* syncHelper, WorkerThreadLifecycleContext* workerThreadLifecycl
eContext) | 172 Peer::Peer(Bridge* bridge, PassRefPtr<WorkerLoaderProxy> loaderProxy, WorkerThre
adLifecycleContext* workerThreadLifecycleContext) |
| 182 : WorkerThreadLifecycleObserver(workerThreadLifecycleContext) | 173 : WorkerThreadLifecycleObserver(workerThreadLifecycleContext) |
| 183 , m_bridge(bridge) | 174 , m_bridge(bridge) |
| 184 , m_loaderProxy(loaderProxy) | 175 , m_loaderProxy(loaderProxy) |
| 185 , m_mainWebSocketChannel(nullptr) | 176 , m_mainWebSocketChannel(nullptr) |
| 186 , m_syncHelper(syncHelper) | |
| 187 { | 177 { |
| 188 DCHECK(isMainThread()); | 178 DCHECK(isMainThread()); |
| 189 } | 179 } |
| 190 | 180 |
| 191 Peer::~Peer() | 181 Peer::~Peer() |
| 192 { | 182 { |
| 193 DCHECK(isMainThread()); | 183 DCHECK(isMainThread()); |
| 194 } | 184 } |
| 195 | 185 |
| 196 bool Peer::initialize(std::unique_ptr<SourceLocation> location, ExecutionContext
* context) | 186 bool Peer::initialize(std::unique_ptr<SourceLocation> location, ExecutionContext
* context) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 void Peer::sendBlob(PassRefPtr<BlobDataHandle> blobData) | 218 void Peer::sendBlob(PassRefPtr<BlobDataHandle> blobData) |
| 229 { | 219 { |
| 230 ASSERT(isMainThread()); | 220 ASSERT(isMainThread()); |
| 231 if (m_mainWebSocketChannel) | 221 if (m_mainWebSocketChannel) |
| 232 m_mainWebSocketChannel->send(blobData); | 222 m_mainWebSocketChannel->send(blobData); |
| 233 } | 223 } |
| 234 | 224 |
| 235 void Peer::close(int code, const String& reason) | 225 void Peer::close(int code, const String& reason) |
| 236 { | 226 { |
| 237 ASSERT(isMainThread()); | 227 ASSERT(isMainThread()); |
| 238 ASSERT(m_syncHelper); | |
| 239 if (!m_mainWebSocketChannel) | 228 if (!m_mainWebSocketChannel) |
| 240 return; | 229 return; |
| 241 m_mainWebSocketChannel->close(code, reason); | 230 m_mainWebSocketChannel->close(code, reason); |
| 242 } | 231 } |
| 243 | 232 |
| 244 void Peer::fail(const String& reason, MessageLevel level, std::unique_ptr<Source
Location> location) | 233 void Peer::fail(const String& reason, MessageLevel level, std::unique_ptr<Source
Location> location) |
| 245 { | 234 { |
| 246 ASSERT(isMainThread()); | 235 ASSERT(isMainThread()); |
| 247 ASSERT(m_syncHelper); | |
| 248 if (!m_mainWebSocketChannel) | 236 if (!m_mainWebSocketChannel) |
| 249 return; | 237 return; |
| 250 m_mainWebSocketChannel->fail(reason, level, std::move(location)); | 238 m_mainWebSocketChannel->fail(reason, level, std::move(location)); |
| 251 } | 239 } |
| 252 | 240 |
| 253 void Peer::disconnect() | 241 void Peer::disconnect() |
| 254 { | 242 { |
| 255 ASSERT(isMainThread()); | 243 ASSERT(isMainThread()); |
| 256 ASSERT(m_syncHelper); | 244 if (!m_mainWebSocketChannel) |
| 257 if (m_mainWebSocketChannel) { | 245 return; |
| 258 m_mainWebSocketChannel->disconnect(); | 246 m_mainWebSocketChannel->disconnect(); |
| 259 m_mainWebSocketChannel = nullptr; | 247 m_mainWebSocketChannel = nullptr; |
| 260 } | |
| 261 m_syncHelper->signalWorkerThread(); | |
| 262 } | 248 } |
| 263 | 249 |
| 264 static void workerGlobalScopeDidConnect(Bridge* bridge, const String& subprotoco
l, const String& extensions, ExecutionContext* context) | 250 static void workerGlobalScopeDidConnect(Bridge* bridge, const String& subprotoco
l, const String& extensions, ExecutionContext* context) |
| 265 { | 251 { |
| 266 ASSERT_UNUSED(context, context->isWorkerGlobalScope()); | 252 ASSERT_UNUSED(context, context->isWorkerGlobalScope()); |
| 267 if (bridge && bridge->client()) | 253 if (bridge && bridge->client()) |
| 268 bridge->client()->didConnect(subprotocol, extensions); | 254 bridge->client()->didConnect(subprotocol, extensions); |
| 269 } | 255 } |
| 270 | 256 |
| 271 void Peer::didConnect(const String& subprotocol, const String& extensions) | 257 void Peer::didConnect(const String& subprotocol, const String& extensions) |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 if (m_mainWebSocketChannel) { | 348 if (m_mainWebSocketChannel) { |
| 363 m_mainWebSocketChannel->disconnect(); | 349 m_mainWebSocketChannel->disconnect(); |
| 364 m_mainWebSocketChannel = nullptr; | 350 m_mainWebSocketChannel = nullptr; |
| 365 } | 351 } |
| 366 m_bridge = nullptr; | 352 m_bridge = nullptr; |
| 367 } | 353 } |
| 368 | 354 |
| 369 DEFINE_TRACE(Peer) | 355 DEFINE_TRACE(Peer) |
| 370 { | 356 { |
| 371 visitor->trace(m_mainWebSocketChannel); | 357 visitor->trace(m_mainWebSocketChannel); |
| 372 visitor->trace(m_syncHelper); | |
| 373 WebSocketChannelClient::trace(visitor); | 358 WebSocketChannelClient::trace(visitor); |
| 374 WorkerThreadLifecycleObserver::trace(visitor); | 359 WorkerThreadLifecycleObserver::trace(visitor); |
| 375 } | 360 } |
| 376 | 361 |
| 377 Bridge::Bridge(WebSocketChannelClient* client, WorkerGlobalScope& workerGlobalSc
ope) | 362 Bridge::Bridge(WebSocketChannelClient* client, WorkerGlobalScope& workerGlobalSc
ope) |
| 378 : m_client(client) | 363 : m_client(client) |
| 379 , m_workerGlobalScope(workerGlobalScope) | 364 , m_workerGlobalScope(workerGlobalScope) |
| 380 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy()) | 365 , m_loaderProxy(m_workerGlobalScope->thread()->workerLoaderProxy()) |
| 381 , m_syncHelper(WebSocketChannelSyncHelper::create(wrapUnique(new WaitableEve
nt()))) | |
| 382 { | 366 { |
| 383 } | 367 } |
| 384 | 368 |
| 385 Bridge::~Bridge() | 369 Bridge::~Bridge() |
| 386 { | 370 { |
| 387 ASSERT(!m_peer); | 371 ASSERT(!m_peer); |
| 388 } | 372 } |
| 389 | 373 |
| 390 void Bridge::connectOnMainThread(std::unique_ptr<SourceLocation> location, Worke
rThreadLifecycleContext* workerThreadLifecycleContext, const KURL& url, const St
ring& protocol, ExecutionContext* context) | 374 void Bridge::connectOnMainThread(std::unique_ptr<SourceLocation> location, Worke
rThreadLifecycleContext* workerThreadLifecycleContext, const KURL& url, const St
ring& protocol, WebSocketChannelSyncHelper* syncHelper, ExecutionContext* contex
t) |
| 391 { | 375 { |
| 392 DCHECK(isMainThread()); | 376 DCHECK(isMainThread()); |
| 393 DCHECK(!m_peer); | 377 DCHECK(!m_peer); |
| 394 Peer* peer = new Peer(this, m_loaderProxy, m_syncHelper, workerThreadLifecyc
leContext); | 378 Peer* peer = new Peer(this, m_loaderProxy, workerThreadLifecycleContext); |
| 395 if (peer->initialize(std::move(location), context)) { | 379 if (peer->initialize(std::move(location), context)) { |
| 396 m_peer = peer; | 380 m_peer = peer; |
| 397 bool result = m_peer->connect(url, protocol); | 381 syncHelper->setConnectRequestResult(m_peer->connect(url, protocol)); |
| 398 m_syncHelper->setConnectRequestResult(result); | |
| 399 } | 382 } |
| 400 m_syncHelper->signalWorkerThread(); | 383 syncHelper->signalWorkerThread(); |
| 401 } | 384 } |
| 402 | 385 |
| 403 bool Bridge::connect(std::unique_ptr<SourceLocation> location, const KURL& url,
const String& protocol) | 386 bool Bridge::connect(std::unique_ptr<SourceLocation> location, const KURL& url,
const String& protocol) |
| 404 { | 387 { |
| 405 // Wait for completion of the task on the main thread because the mixed | 388 // Wait for completion of the task on the main thread because the mixed |
| 406 // content check must synchronously be conducted. | 389 // content check must synchronously be conducted. |
| 407 if (!waitForMethodCompletion(BLINK_FROM_HERE, createCrossThreadTask(&Bridge:
:connectOnMainThread, wrapCrossThreadPersistent(this), passed(location->clone())
, wrapCrossThreadPersistent(m_workerGlobalScope->thread()->getWorkerThreadLifecy
cleContext()), url, protocol))) | 390 WebSocketChannelSyncHelper syncHelper; |
| 408 return false; | 391 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Brid
ge::connectOnMainThread, wrapCrossThreadPersistent(this), passed(location->clone
()), wrapCrossThreadPersistent(m_workerGlobalScope->thread()->getWorkerThreadLif
ecycleContext()), url, protocol, crossThreadUnretained(&syncHelper))); |
| 409 | 392 { |
| 410 return m_syncHelper->connectRequestResult(); | 393 SafePointScope scope(BlinkGC::HeapPointersOnStack); |
| 394 syncHelper.wait(); |
| 395 } |
| 396 return syncHelper.connectRequestResult(); |
| 411 } | 397 } |
| 412 | 398 |
| 413 void Bridge::send(const CString& message) | 399 void Bridge::send(const CString& message) |
| 414 { | 400 { |
| 415 ASSERT(m_peer); | 401 ASSERT(m_peer); |
| 416 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(message.len
gth())); | 402 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(message.len
gth())); |
| 417 if (message.length()) | 403 if (message.length()) |
| 418 memcpy(data->data(), static_cast<const char*>(message.data()), message.l
ength()); | 404 memcpy(data->data(), static_cast<const char*>(message.data()), message.l
ength()); |
| 419 | 405 |
| 420 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendTextAsCharVector, wrapCrossThreadPersistent(m_peer.get()), passed(std::mov
e(data)))); | 406 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendTextAsCharVector, m_peer, passed(std::move(data)))); |
| 421 } | 407 } |
| 422 | 408 |
| 423 void Bridge::send(const DOMArrayBuffer& binaryData, unsigned byteOffset, unsigne
d byteLength) | 409 void Bridge::send(const DOMArrayBuffer& binaryData, unsigned byteOffset, unsigne
d byteLength) |
| 424 { | 410 { |
| 425 ASSERT(m_peer); | 411 ASSERT(m_peer); |
| 426 // ArrayBuffer isn't thread-safe, hence the content of ArrayBuffer is copied
into Vector<char>. | 412 // ArrayBuffer isn't thread-safe, hence the content of ArrayBuffer is copied
into Vector<char>. |
| 427 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(byteLength)
); | 413 std::unique_ptr<Vector<char>> data = wrapUnique(new Vector<char>(byteLength)
); |
| 428 if (binaryData.byteLength()) | 414 if (binaryData.byteLength()) |
| 429 memcpy(data->data(), static_cast<const char*>(binaryData.data()) + byteO
ffset, byteLength); | 415 memcpy(data->data(), static_cast<const char*>(binaryData.data()) + byteO
ffset, byteLength); |
| 430 | 416 |
| 431 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendBinaryAsCharVector, wrapCrossThreadPersistent(m_peer.get()), passed(std::m
ove(data)))); | 417 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendBinaryAsCharVector, m_peer, passed(std::move(data)))); |
| 432 } | 418 } |
| 433 | 419 |
| 434 void Bridge::send(PassRefPtr<BlobDataHandle> data) | 420 void Bridge::send(PassRefPtr<BlobDataHandle> data) |
| 435 { | 421 { |
| 436 ASSERT(m_peer); | 422 ASSERT(m_peer); |
| 437 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendBlob, wrapCrossThreadPersistent(m_peer.get()), data)); | 423 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::sendBlob, m_peer, data)); |
| 438 } | 424 } |
| 439 | 425 |
| 440 void Bridge::close(int code, const String& reason) | 426 void Bridge::close(int code, const String& reason) |
| 441 { | 427 { |
| 442 ASSERT(m_peer); | 428 ASSERT(m_peer); |
| 443 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::close, wrapCrossThreadPersistent(m_peer.get()), code, reason)); | 429 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::close, m_peer, code, reason)); |
| 444 } | 430 } |
| 445 | 431 |
| 446 void Bridge::fail(const String& reason, MessageLevel level, std::unique_ptr<Sour
ceLocation> location) | 432 void Bridge::fail(const String& reason, MessageLevel level, std::unique_ptr<Sour
ceLocation> location) |
| 447 { | 433 { |
| 448 ASSERT(m_peer); | 434 ASSERT(m_peer); |
| 449 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::fail, wrapCrossThreadPersistent(m_peer.get()), reason, level, passed(location-
>clone()))); | 435 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::fail, m_peer, reason, level, passed(location->clone()))); |
| 450 } | 436 } |
| 451 | 437 |
| 452 void Bridge::disconnect() | 438 void Bridge::disconnect() |
| 453 { | 439 { |
| 454 if (!m_peer) | 440 if (!m_peer) |
| 455 return; | 441 return; |
| 456 | 442 |
| 457 // Wait for completion of the task on the main thread to ensure that | 443 m_loaderProxy->postTaskToLoader(BLINK_FROM_HERE, createCrossThreadTask(&Peer
::disconnect, m_peer)); |
| 458 // |m_peer| does not touch this Bridge object after this point. | |
| 459 waitForMethodCompletion(BLINK_FROM_HERE, createCrossThreadTask(&Peer::discon
nect, wrapCrossThreadPersistent(m_peer.get()))); | |
| 460 | 444 |
| 461 m_client = nullptr; | 445 m_client = nullptr; |
| 462 m_peer = nullptr; | 446 m_peer = nullptr; |
| 463 m_syncHelper = nullptr; | |
| 464 // We won't use this any more. | |
| 465 m_workerGlobalScope.clear(); | 447 m_workerGlobalScope.clear(); |
| 466 } | 448 } |
| 467 | 449 |
| 468 // Caller of this function should hold a reference to the bridge, because this f
unction may call WebSocket::didClose() in the end, | |
| 469 // which causes the bridge to get disconnected from the WebSocket and deleted if
there is no other reference. | |
| 470 bool Bridge::waitForMethodCompletion(const WebTraceLocation& location, std::uniq
ue_ptr<ExecutionContextTask> task) | |
| 471 { | |
| 472 ASSERT(m_workerGlobalScope); | |
| 473 ASSERT(m_syncHelper); | |
| 474 | |
| 475 m_loaderProxy->postTaskToLoader(location, std::move(task)); | |
| 476 | |
| 477 // We wait for the syncHelper event even if a shutdown event is fired. | |
| 478 // See https://codereview.chromium.org/267323004/#msg43 for why we need to w
ait this. | |
| 479 SafePointScope scope(BlinkGC::HeapPointersOnStack); | |
| 480 m_syncHelper->wait(); | |
| 481 // This is checking whether a shutdown event is fired or not. | |
| 482 return !m_workerGlobalScope->thread()->terminated(); | |
| 483 } | |
| 484 | |
| 485 DEFINE_TRACE(Bridge) | 450 DEFINE_TRACE(Bridge) |
| 486 { | 451 { |
| 487 visitor->trace(m_client); | 452 visitor->trace(m_client); |
| 488 visitor->trace(m_workerGlobalScope); | 453 visitor->trace(m_workerGlobalScope); |
| 489 visitor->trace(m_syncHelper); | |
| 490 } | 454 } |
| 491 | 455 |
| 492 } // namespace blink | 456 } // namespace blink |
| OLD | NEW |