| 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 "config.h" | 31 #include "config.h" |
| 32 | 32 |
| 33 #include "modules/websockets/WorkerThreadableWebSocketChannel.h" | 33 #include "modules/websockets/WorkerThreadableWebSocketChannel.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/fileapi/Blob.h" | 40 #include "core/fileapi/Blob.h" |
| 40 #include "core/inspector/ScriptCallFrame.h" | 41 #include "core/inspector/ScriptCallFrame.h" |
| 41 #include "core/inspector/ScriptCallStack.h" | 42 #include "core/inspector/ScriptCallStack.h" |
| 42 #include "core/workers/WorkerLoaderProxy.h" | 43 #include "core/workers/WorkerLoaderProxy.h" |
| 43 #include "core/workers/WorkerRunLoop.h" | 44 #include "core/workers/WorkerRunLoop.h" |
| 44 #include "core/workers/WorkerThread.h" | 45 #include "core/workers/WorkerThread.h" |
| 45 #include "modules/websockets/MainThreadWebSocketChannel.h" | 46 #include "modules/websockets/MainThreadWebSocketChannel.h" |
| 46 #include "modules/websockets/NewWebSocketChannelImpl.h" | 47 #include "modules/websockets/NewWebSocketChannelImpl.h" |
| 47 #include "modules/websockets/ThreadableWebSocketChannelClientWrapper.h" | 48 #include "modules/websockets/ThreadableWebSocketChannelClientWrapper.h" |
| 48 #include "platform/RuntimeEnabledFeatures.h" | 49 #include "platform/RuntimeEnabledFeatures.h" |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 490 disconnect(); | 491 disconnect(); |
| 491 } | 492 } |
| 492 } | 493 } |
| 493 | 494 |
| 494 bool WorkerThreadableWebSocketChannel::Bridge::connect(const KURL& url, const St
ring& protocol) | 495 bool WorkerThreadableWebSocketChannel::Bridge::connect(const KURL& url, const St
ring& protocol) |
| 495 { | 496 { |
| 496 if (hasTerminatedPeer()) | 497 if (hasTerminatedPeer()) |
| 497 return false; | 498 return false; |
| 498 | 499 |
| 499 RefPtrWillBeRawPtr<Bridge> protect(this); | 500 RefPtrWillBeRawPtr<Bridge> protect(this); |
| 500 // It is important to seprate task creation from calling | 501 if (!waitForMethodCompletion(createCallClosureTask(&Peer::connect, m_peer, u
rl, protocol))) |
| 501 // waitForMethodCompletion. See the above comment. | |
| 502 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::conn
ect, m_peer, url.copy(), protocol.isolatedCopy())); | |
| 503 if (!waitForMethodCompletion(task.release())) | |
| 504 return false; | 502 return false; |
| 505 | 503 |
| 506 return m_syncHelper->connectRequestResult(); | 504 return m_syncHelper->connectRequestResult(); |
| 507 } | 505 } |
| 508 | 506 |
| 509 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(cons
t String& message) | 507 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(cons
t String& message) |
| 510 { | 508 { |
| 511 if (hasTerminatedPeer()) | 509 if (hasTerminatedPeer()) |
| 512 return WebSocketChannel::SendFail; | 510 return WebSocketChannel::SendFail; |
| 513 | 511 |
| 514 RefPtrWillBeRawPtr<Bridge> protect(this); | 512 RefPtrWillBeRawPtr<Bridge> protect(this); |
| 515 // It is important to seprate task creation from calling | 513 if (!waitForMethodCompletion(createCallClosureTask(&Peer::send, m_peer, mess
age))) |
| 516 // waitForMethodCompletion. See the above comment. | |
| 517 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::send
, m_peer, message.isolatedCopy())); | |
| 518 if (!waitForMethodCompletion(task.release())) | |
| 519 return WebSocketChannel::SendFail; | 514 return WebSocketChannel::SendFail; |
| 520 | 515 |
| 521 return m_syncHelper->sendRequestResult(); | 516 return m_syncHelper->sendRequestResult(); |
| 522 } | 517 } |
| 523 | 518 |
| 524 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(cons
t ArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength) | 519 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(cons
t ArrayBuffer& binaryData, unsigned byteOffset, unsigned byteLength) |
| 525 { | 520 { |
| 526 if (hasTerminatedPeer()) | 521 if (hasTerminatedPeer()) |
| 527 return WebSocketChannel::SendFail; | 522 return WebSocketChannel::SendFail; |
| 528 | 523 |
| 529 // ArrayBuffer isn't thread-safe, hence the content of ArrayBuffer is copied
into Vector<char>. | 524 // ArrayBuffer isn't thread-safe, hence the content of ArrayBuffer is copied
into Vector<char>. |
| 530 OwnPtr<Vector<char> > data = adoptPtr(new Vector<char>(byteLength)); | 525 OwnPtr<Vector<char> > data = adoptPtr(new Vector<char>(byteLength)); |
| 531 if (binaryData.byteLength()) | 526 if (binaryData.byteLength()) |
| 532 memcpy(data->data(), static_cast<const char*>(binaryData.data()) + byteO
ffset, byteLength); | 527 memcpy(data->data(), static_cast<const char*>(binaryData.data()) + byteO
ffset, byteLength); |
| 533 | 528 |
| 534 RefPtrWillBeRawPtr<Bridge> protect(this); | 529 RefPtrWillBeRawPtr<Bridge> protect(this); |
| 535 // It is important to seprate task creation from calling | 530 if (!waitForMethodCompletion(createCallClosureTask(&Peer::sendArrayBuffer, m
_peer, data.release()))) |
| 536 // waitForMethodCompletion. See the above comment. | |
| 537 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::send
ArrayBuffer, m_peer, data.release())); | |
| 538 if (!waitForMethodCompletion(task.release())) | |
| 539 return WebSocketChannel::SendFail; | 531 return WebSocketChannel::SendFail; |
| 540 | 532 |
| 541 return m_syncHelper->sendRequestResult(); | 533 return m_syncHelper->sendRequestResult(); |
| 542 } | 534 } |
| 543 | 535 |
| 544 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(Pass
RefPtr<BlobDataHandle> data) | 536 WebSocketChannel::SendResult WorkerThreadableWebSocketChannel::Bridge::send(Pass
RefPtr<BlobDataHandle> data) |
| 545 { | 537 { |
| 546 if (hasTerminatedPeer()) | 538 if (hasTerminatedPeer()) |
| 547 return WebSocketChannel::SendFail; | 539 return WebSocketChannel::SendFail; |
| 548 | 540 |
| 549 RefPtrWillBeRawPtr<Bridge> protect(this); | 541 RefPtrWillBeRawPtr<Bridge> protect(this); |
| 550 // It is important to seprate task creation from calling | 542 if (!waitForMethodCompletion(createCallClosureTask(&Peer::sendBlob, m_peer,
data))) |
| 551 // waitForMethodCompletion. See the above comment. | |
| 552 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::send
Blob, m_peer, data)); | |
| 553 if (!waitForMethodCompletion(task.release())) | |
| 554 return WebSocketChannel::SendFail; | 543 return WebSocketChannel::SendFail; |
| 555 | 544 |
| 556 return m_syncHelper->sendRequestResult(); | 545 return m_syncHelper->sendRequestResult(); |
| 557 } | 546 } |
| 558 | 547 |
| 559 void WorkerThreadableWebSocketChannel::Bridge::close(int code, const String& rea
son) | 548 void WorkerThreadableWebSocketChannel::Bridge::close(int code, const String& rea
son) |
| 560 { | 549 { |
| 561 if (hasTerminatedPeer()) | 550 if (hasTerminatedPeer()) |
| 562 return; | 551 return; |
| 563 | 552 |
| 564 // It is important to seprate task creation from calling | 553 m_loaderProxy.postTaskToLoader(createCallClosureTask(&Peer::close, m_peer, c
ode, reason)); |
| 565 // waitForMethodCompletion. See the above comment. | |
| 566 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::clos
e, m_peer, code, reason.isolatedCopy())); | |
| 567 m_loaderProxy.postTaskToLoader(task.release()); | |
| 568 } | 554 } |
| 569 | 555 |
| 570 void WorkerThreadableWebSocketChannel::Bridge::fail(const String& reason, Messag
eLevel level, const String& sourceURL, unsigned lineNumber) | 556 void WorkerThreadableWebSocketChannel::Bridge::fail(const String& reason, Messag
eLevel level, const String& sourceURL, unsigned lineNumber) |
| 571 { | 557 { |
| 572 if (hasTerminatedPeer()) | 558 if (hasTerminatedPeer()) |
| 573 return; | 559 return; |
| 574 | 560 |
| 575 // It is important to seprate task creation from calling | 561 m_loaderProxy.postTaskToLoader(createCallClosureTask(&Peer::fail, m_peer, re
ason, level, sourceURL, lineNumber)); |
| 576 // waitForMethodCompletion. See the above comment. | |
| 577 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::fail
, m_peer, reason.isolatedCopy(), level, sourceURL.isolatedCopy(), lineNumber)); | |
| 578 m_loaderProxy.postTaskToLoader(task.release()); | |
| 579 } | 562 } |
| 580 | 563 |
| 581 void WorkerThreadableWebSocketChannel::Bridge::disconnect() | 564 void WorkerThreadableWebSocketChannel::Bridge::disconnect() |
| 582 { | 565 { |
| 583 if (hasTerminatedPeer()) | 566 if (hasTerminatedPeer()) |
| 584 return; | 567 return; |
| 585 | 568 |
| 586 clearClientWrapper(); | 569 clearClientWrapper(); |
| 587 terminatePeer(); | 570 terminatePeer(); |
| 588 } | 571 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 608 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack); | 591 ThreadState::SafePointScope scope(ThreadState::HeapPointersOnStack); |
| 609 blink::Platform::current()->waitMultipleEvents(events); | 592 blink::Platform::current()->waitMultipleEvents(events); |
| 610 // This is checking whether a shutdown event is fired or not. | 593 // This is checking whether a shutdown event is fired or not. |
| 611 return !m_workerGlobalScope->thread()->runLoop().terminated(); | 594 return !m_workerGlobalScope->thread()->runLoop().terminated(); |
| 612 } | 595 } |
| 613 | 596 |
| 614 void WorkerThreadableWebSocketChannel::Bridge::terminatePeer() | 597 void WorkerThreadableWebSocketChannel::Bridge::terminatePeer() |
| 615 { | 598 { |
| 616 ASSERT(!hasTerminatedPeer()); | 599 ASSERT(!hasTerminatedPeer()); |
| 617 | 600 |
| 618 // It is important to seprate task creation from calling | |
| 619 // waitForMethodCompletion. See the above comment. | |
| 620 OwnPtr<ExecutionContextTask> task = CallClosureTask::create(bind(&Peer::dest
roy, m_peer)); | |
| 621 #if ENABLE(OILPAN) | 601 #if ENABLE(OILPAN) |
| 622 // The worker thread has to wait for the main thread to complete Peer::destr
oy, | 602 // The worker thread has to wait for the main thread to complete Peer::destr
oy, |
| 623 // because the worker thread has to make sure that the main thread does not
have any | 603 // because the worker thread has to make sure that the main thread does not
have any |
| 624 // references to on-heap objects allocated in the thread heap of the worker
thread | 604 // references to on-heap objects allocated in the thread heap of the worker
thread |
| 625 // before the worker thread shuts down. | 605 // before the worker thread shuts down. |
| 626 waitForMethodCompletion(task.release()); | 606 waitForMethodCompletion(createCallClosureTask(&Peer::destroy, m_peer)); |
| 627 #else | 607 #else |
| 628 m_loaderProxy.postTaskToLoader(task.release()); | 608 m_loaderProxy.postTaskToLoader(createCallClosureTask(&Peer::destroy, m_peer)
); |
| 629 #endif | 609 #endif |
| 630 | 610 |
| 631 // Peer::destroy() deletes m_peer and then m_syncHelper will be released. | 611 // Peer::destroy() deletes m_peer and then m_syncHelper will be released. |
| 632 // We must not touch m_syncHelper any more. | 612 // We must not touch m_syncHelper any more. |
| 633 m_syncHelper = nullptr; | 613 m_syncHelper = nullptr; |
| 634 | 614 |
| 635 // We won't use this any more. | 615 // We won't use this any more. |
| 636 m_workerGlobalScope = nullptr; | 616 m_workerGlobalScope = nullptr; |
| 637 } | 617 } |
| 638 | 618 |
| 639 void WorkerThreadableWebSocketChannel::Bridge::trace(Visitor* visitor) | 619 void WorkerThreadableWebSocketChannel::Bridge::trace(Visitor* visitor) |
| 640 { | 620 { |
| 641 visitor->trace(m_workerClientWrapper); | 621 visitor->trace(m_workerClientWrapper); |
| 642 visitor->trace(m_workerGlobalScope); | 622 visitor->trace(m_workerGlobalScope); |
| 643 visitor->trace(m_syncHelper); | 623 visitor->trace(m_syncHelper); |
| 644 visitor->trace(m_peer); | 624 visitor->trace(m_peer); |
| 645 } | 625 } |
| 646 | 626 |
| 647 } // namespace WebCore | 627 } // namespace WebCore |
| OLD | NEW |