| 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 18 matching lines...) Expand all Loading... |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "modules/websockets/WorkerWebSocketChannel.h" | 31 #include "modules/websockets/WorkerWebSocketChannel.h" |
| 32 | 32 |
| 33 #include <memory> | 33 #include <memory> |
| 34 #include "core/dom/DOMArrayBuffer.h" | 34 #include "core/dom/DOMArrayBuffer.h" |
| 35 #include "core/dom/ExecutionContext.h" | 35 #include "core/dom/ExecutionContext.h" |
| 36 #include "core/fileapi/Blob.h" | 36 #include "core/fileapi/Blob.h" |
| 37 #include "core/loader/ThreadableLoadingContext.h" | 37 #include "core/loader/ThreadableLoadingContext.h" |
| 38 #include "core/workers/WorkerGlobalScope.h" | 38 #include "core/workers/WorkerGlobalScope.h" |
| 39 #include "core/workers/WorkerLoaderProxy.h" | |
| 40 #include "core/workers/WorkerThread.h" | 39 #include "core/workers/WorkerThread.h" |
| 41 #include "modules/websockets/DocumentWebSocketChannel.h" | 40 #include "modules/websockets/DocumentWebSocketChannel.h" |
| 42 #include "platform/CrossThreadFunctional.h" | 41 #include "platform/CrossThreadFunctional.h" |
| 43 #include "platform/WaitableEvent.h" | 42 #include "platform/WaitableEvent.h" |
| 44 #include "platform/heap/SafePoint.h" | 43 #include "platform/heap/SafePoint.h" |
| 45 #include "platform/wtf/Assertions.h" | 44 #include "platform/wtf/Assertions.h" |
| 46 #include "platform/wtf/Functional.h" | 45 #include "platform/wtf/Functional.h" |
| 47 #include "platform/wtf/PtrUtil.h" | 46 #include "platform/wtf/PtrUtil.h" |
| 48 #include "platform/wtf/text/CString.h" | 47 #include "platform/wtf/text/CString.h" |
| 49 #include "platform/wtf/text/WTFString.h" | 48 #include "platform/wtf/text/WTFString.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 bridge_->Disconnect(); | 153 bridge_->Disconnect(); |
| 155 bridge_.Clear(); | 154 bridge_.Clear(); |
| 156 } | 155 } |
| 157 | 156 |
| 158 DEFINE_TRACE(WorkerWebSocketChannel) { | 157 DEFINE_TRACE(WorkerWebSocketChannel) { |
| 159 visitor->Trace(bridge_); | 158 visitor->Trace(bridge_); |
| 160 WebSocketChannel::Trace(visitor); | 159 WebSocketChannel::Trace(visitor); |
| 161 } | 160 } |
| 162 | 161 |
| 163 Peer::Peer(Bridge* bridge, | 162 Peer::Peer(Bridge* bridge, |
| 164 PassRefPtr<WorkerLoaderProxy> loader_proxy, | |
| 165 RefPtr<WebTaskRunner> worker_networking_task_runner, | 163 RefPtr<WebTaskRunner> worker_networking_task_runner, |
| 166 WorkerThreadLifecycleContext* worker_thread_lifecycle_context) | 164 WorkerThreadLifecycleContext* worker_thread_lifecycle_context) |
| 167 : WorkerThreadLifecycleObserver(worker_thread_lifecycle_context), | 165 : WorkerThreadLifecycleObserver(worker_thread_lifecycle_context), |
| 168 bridge_(bridge), | 166 bridge_(bridge), |
| 169 loader_proxy_(std::move(loader_proxy)), | |
| 170 worker_networking_task_runner_(std::move(worker_networking_task_runner)), | 167 worker_networking_task_runner_(std::move(worker_networking_task_runner)), |
| 171 main_web_socket_channel_(nullptr) { | 168 main_web_socket_channel_(nullptr) { |
| 172 DCHECK(IsMainThread()); | 169 DCHECK(IsMainThread()); |
| 173 } | 170 } |
| 174 | 171 |
| 175 Peer::~Peer() { | 172 Peer::~Peer() { |
| 176 DCHECK(IsMainThread()); | 173 DCHECK(IsMainThread()); |
| 177 } | 174 } |
| 178 | 175 |
| 179 bool Peer::Initialize(std::unique_ptr<SourceLocation> location, | 176 bool Peer::Initialize(std::unique_ptr<SourceLocation> location, |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 DEFINE_TRACE(Peer) { | 348 DEFINE_TRACE(Peer) { |
| 352 visitor->Trace(main_web_socket_channel_); | 349 visitor->Trace(main_web_socket_channel_); |
| 353 WebSocketChannelClient::Trace(visitor); | 350 WebSocketChannelClient::Trace(visitor); |
| 354 WorkerThreadLifecycleObserver::Trace(visitor); | 351 WorkerThreadLifecycleObserver::Trace(visitor); |
| 355 } | 352 } |
| 356 | 353 |
| 357 Bridge::Bridge(WebSocketChannelClient* client, | 354 Bridge::Bridge(WebSocketChannelClient* client, |
| 358 WorkerGlobalScope& worker_global_scope) | 355 WorkerGlobalScope& worker_global_scope) |
| 359 : client_(client), | 356 : client_(client), |
| 360 worker_global_scope_(worker_global_scope), | 357 worker_global_scope_(worker_global_scope), |
| 361 loader_proxy_(worker_global_scope_->GetThread()->GetWorkerLoaderProxy()), | |
| 362 parent_frame_task_runners_( | 358 parent_frame_task_runners_( |
| 363 worker_global_scope_->GetThread()->GetParentFrameTaskRunners()) {} | 359 worker_global_scope_->GetThread()->GetParentFrameTaskRunners()) {} |
| 364 | 360 |
| 365 Bridge::~Bridge() { | 361 Bridge::~Bridge() { |
| 366 DCHECK(!peer_); | 362 DCHECK(!peer_); |
| 367 } | 363 } |
| 368 | 364 |
| 369 void Bridge::ConnectOnMainThread( | 365 void Bridge::ConnectOnMainThread( |
| 370 std::unique_ptr<SourceLocation> location, | 366 std::unique_ptr<SourceLocation> location, |
| 371 RefPtr<WorkerLoaderProxy> loader_proxy, | 367 ThreadableLoadingContext* loading_context, |
| 372 RefPtr<WebTaskRunner> worker_networking_task_runner, | 368 RefPtr<WebTaskRunner> worker_networking_task_runner, |
| 373 WorkerThreadLifecycleContext* worker_thread_lifecycle_context, | 369 WorkerThreadLifecycleContext* worker_thread_lifecycle_context, |
| 374 const KURL& url, | 370 const KURL& url, |
| 375 const String& protocol, | 371 const String& protocol, |
| 376 WebSocketChannelSyncHelper* sync_helper) { | 372 WebSocketChannelSyncHelper* sync_helper) { |
| 377 DCHECK(IsMainThread()); | 373 DCHECK(IsMainThread()); |
| 378 DCHECK(!peer_); | 374 DCHECK(!peer_); |
| 379 ThreadableLoadingContext* loading_context = | 375 Peer* peer = new Peer(this, std::move(worker_networking_task_runner), |
| 380 loader_proxy->GetThreadableLoadingContext(); | 376 worker_thread_lifecycle_context); |
| 381 if (!loading_context) | |
| 382 return; | |
| 383 Peer* peer = | |
| 384 new Peer(this, loader_proxy_, std::move(worker_networking_task_runner), | |
| 385 worker_thread_lifecycle_context); | |
| 386 if (peer->Initialize(std::move(location), loading_context)) { | 377 if (peer->Initialize(std::move(location), loading_context)) { |
| 387 peer_ = peer; | 378 peer_ = peer; |
| 388 sync_helper->SetConnectRequestResult(peer_->Connect(url, protocol)); | 379 sync_helper->SetConnectRequestResult(peer_->Connect(url, protocol)); |
| 389 } | 380 } |
| 390 sync_helper->SignalWorkerThread(); | 381 sync_helper->SignalWorkerThread(); |
| 391 } | 382 } |
| 392 | 383 |
| 393 bool Bridge::Connect(std::unique_ptr<SourceLocation> location, | 384 bool Bridge::Connect(std::unique_ptr<SourceLocation> location, |
| 394 const KURL& url, | 385 const KURL& url, |
| 395 const String& protocol) { | 386 const String& protocol) { |
| 387 DCHECK(worker_global_scope_->IsContextThread()); |
| 396 // 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 |
| 397 // content check must synchronously be conducted. | 389 // content check must synchronously be conducted. |
| 398 WebSocketChannelSyncHelper sync_helper; | 390 WebSocketChannelSyncHelper sync_helper; |
| 399 RefPtr<WebTaskRunner> worker_networking_task_runner = | 391 RefPtr<WebTaskRunner> worker_networking_task_runner = |
| 400 TaskRunnerHelper::Get(TaskType::kNetworking, worker_global_scope_.Get()); | 392 TaskRunnerHelper::Get(TaskType::kNetworking, worker_global_scope_.Get()); |
| 393 WorkerThread* worker_thread = worker_global_scope_->GetThread(); |
| 401 parent_frame_task_runners_->Get(TaskType::kNetworking) | 394 parent_frame_task_runners_->Get(TaskType::kNetworking) |
| 402 ->PostTask( | 395 ->PostTask( |
| 403 BLINK_FROM_HERE, | 396 BLINK_FROM_HERE, |
| 404 CrossThreadBind(&Bridge::ConnectOnMainThread, | 397 CrossThreadBind( |
| 405 WrapCrossThreadPersistent(this), | 398 &Bridge::ConnectOnMainThread, WrapCrossThreadPersistent(this), |
| 406 WTF::Passed(location->Clone()), loader_proxy_, | 399 WTF::Passed(location->Clone()), |
| 407 std::move(worker_networking_task_runner), | 400 WrapCrossThreadPersistent(worker_thread->GetLoadingContext()), |
| 408 WrapCrossThreadPersistent( | 401 std::move(worker_networking_task_runner), |
| 409 worker_global_scope_->GetThread() | 402 WrapCrossThreadPersistent( |
| 410 ->GetWorkerThreadLifecycleContext()), | 403 worker_thread->GetWorkerThreadLifecycleContext()), |
| 411 url, protocol, CrossThreadUnretained(&sync_helper))); | 404 url, protocol, CrossThreadUnretained(&sync_helper))); |
| 412 sync_helper.Wait(); | 405 sync_helper.Wait(); |
| 413 return sync_helper.ConnectRequestResult(); | 406 return sync_helper.ConnectRequestResult(); |
| 414 } | 407 } |
| 415 | 408 |
| 416 void Bridge::Send(const CString& message) { | 409 void Bridge::Send(const CString& message) { |
| 417 DCHECK(peer_); | 410 DCHECK(peer_); |
| 418 std::unique_ptr<Vector<char>> data = | 411 std::unique_ptr<Vector<char>> data = |
| 419 WTF::WrapUnique(new Vector<char>(message.length())); | 412 WTF::WrapUnique(new Vector<char>(message.length())); |
| 420 if (message.length()) | 413 if (message.length()) |
| 421 memcpy(data->data(), static_cast<const char*>(message.data()), | 414 memcpy(data->data(), static_cast<const char*>(message.data()), |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 peer_ = nullptr; | 474 peer_ = nullptr; |
| 482 worker_global_scope_.Clear(); | 475 worker_global_scope_.Clear(); |
| 483 } | 476 } |
| 484 | 477 |
| 485 DEFINE_TRACE(Bridge) { | 478 DEFINE_TRACE(Bridge) { |
| 486 visitor->Trace(client_); | 479 visitor->Trace(client_); |
| 487 visitor->Trace(worker_global_scope_); | 480 visitor->Trace(worker_global_scope_); |
| 488 } | 481 } |
| 489 | 482 |
| 490 } // namespace blink | 483 } // namespace blink |
| OLD | NEW |