| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/presentation/PresentationConnection.h" | 5 #include "modules/presentation/PresentationConnection.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 8 #include "core/dom/DOMArrayBuffer.h" | 8 #include "core/dom/DOMArrayBuffer.h" |
| 9 #include "core/dom/DOMArrayBufferView.h" | 9 #include "core/dom/DOMArrayBufferView.h" |
| 10 #include "core/dom/Document.h" | 10 #include "core/dom/Document.h" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/dom/ExecutionContextTask.h" |
| 12 #include "core/events/Event.h" | 13 #include "core/events/Event.h" |
| 13 #include "core/events/MessageEvent.h" | 14 #include "core/events/MessageEvent.h" |
| 14 #include "core/fileapi/FileReaderLoader.h" | 15 #include "core/fileapi/FileReaderLoader.h" |
| 15 #include "core/fileapi/FileReaderLoaderClient.h" | 16 #include "core/fileapi/FileReaderLoaderClient.h" |
| 16 #include "core/frame/Deprecation.h" | 17 #include "core/frame/Deprecation.h" |
| 17 #include "core/frame/LocalFrame.h" | 18 #include "core/frame/LocalFrame.h" |
| 18 #include "core/frame/UseCounter.h" | 19 #include "core/frame/UseCounter.h" |
| 19 #include "modules/EventTargetModules.h" | 20 #include "modules/EventTargetModules.h" |
| 20 #include "modules/presentation/Presentation.h" | 21 #include "modules/presentation/Presentation.h" |
| 21 #include "modules/presentation/PresentationConnectionAvailableEvent.h" | 22 #include "modules/presentation/PresentationConnectionAvailableEvent.h" |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 PresentationConnection* PresentationConnection::take( | 185 PresentationConnection* PresentationConnection::take( |
| 185 PresentationController* controller, | 186 PresentationController* controller, |
| 186 std::unique_ptr<WebPresentationConnectionClient> client, | 187 std::unique_ptr<WebPresentationConnectionClient> client, |
| 187 PresentationRequest* request) { | 188 PresentationRequest* request) { |
| 188 ASSERT(controller); | 189 ASSERT(controller); |
| 189 ASSERT(request); | 190 ASSERT(request); |
| 190 | 191 |
| 191 PresentationConnection* connection = new PresentationConnection( | 192 PresentationConnection* connection = new PresentationConnection( |
| 192 controller->frame(), client->getId(), client->getUrl()); | 193 controller->frame(), client->getId(), client->getUrl()); |
| 193 controller->registerConnection(connection); | 194 controller->registerConnection(connection); |
| 194 request->dispatchEvent(PresentationConnectionAvailableEvent::create( | 195 |
| 195 EventTypeNames::connectionavailable, connection)); | 196 // Fire onconnectionavailable event asynchronously. |
| 197 auto* event = PresentationConnectionAvailableEvent::create( |
| 198 EventTypeNames::connectionavailable, connection); |
| 199 request->getExecutionContext()->postTask( |
| 200 BLINK_FROM_HERE, |
| 201 createSameThreadTask(&PresentationConnection::dispatchEventAsync, |
| 202 wrapPersistent(request), wrapPersistent(event))); |
| 196 | 203 |
| 197 return connection; | 204 return connection; |
| 198 } | 205 } |
| 199 | 206 |
| 200 // static | 207 // static |
| 201 PresentationConnection* PresentationConnection::take( | 208 PresentationConnection* PresentationConnection::take( |
| 202 PresentationReceiver* receiver, | 209 PresentationReceiver* receiver, |
| 203 std::unique_ptr<WebPresentationConnectionClient> client) { | 210 std::unique_ptr<WebPresentationConnectionClient> client) { |
| 204 DCHECK(receiver); | 211 DCHECK(receiver); |
| 205 DCHECK(client); | 212 DCHECK(client); |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 408 WebPresentationConnectionState state) { | 415 WebPresentationConnectionState state) { |
| 409 if (m_state == state) | 416 if (m_state == state) |
| 410 return; | 417 return; |
| 411 | 418 |
| 412 m_state = state; | 419 m_state = state; |
| 413 switch (m_state) { | 420 switch (m_state) { |
| 414 case WebPresentationConnectionState::Connecting: | 421 case WebPresentationConnectionState::Connecting: |
| 415 NOTREACHED(); | 422 NOTREACHED(); |
| 416 return; | 423 return; |
| 417 case WebPresentationConnectionState::Connected: | 424 case WebPresentationConnectionState::Connected: |
| 418 dispatchEvent(Event::create(EventTypeNames::connect)); | 425 dispatchStateChangeEvent(Event::create(EventTypeNames::connect)); |
| 419 return; | 426 return; |
| 420 case WebPresentationConnectionState::Terminated: | 427 case WebPresentationConnectionState::Terminated: |
| 421 dispatchEvent(Event::create(EventTypeNames::terminate)); | 428 dispatchStateChangeEvent(Event::create(EventTypeNames::terminate)); |
| 422 return; | 429 return; |
| 423 // Closed state is handled in |didClose()|. | 430 // Closed state is handled in |didClose()|. |
| 424 case WebPresentationConnectionState::Closed: | 431 case WebPresentationConnectionState::Closed: |
| 425 NOTREACHED(); | 432 NOTREACHED(); |
| 426 return; | 433 return; |
| 427 } | 434 } |
| 428 NOTREACHED(); | 435 NOTREACHED(); |
| 429 } | 436 } |
| 430 | 437 |
| 431 void PresentationConnection::didClose( | 438 void PresentationConnection::didClose( |
| 432 WebPresentationConnectionCloseReason reason, | 439 WebPresentationConnectionCloseReason reason, |
| 433 const String& message) { | 440 const String& message) { |
| 434 if (m_state == WebPresentationConnectionState::Closed) | 441 if (m_state == WebPresentationConnectionState::Closed) |
| 435 return; | 442 return; |
| 436 | 443 |
| 437 m_state = WebPresentationConnectionState::Closed; | 444 m_state = WebPresentationConnectionState::Closed; |
| 438 dispatchEvent(PresentationConnectionCloseEvent::create( | 445 dispatchStateChangeEvent(PresentationConnectionCloseEvent::create( |
| 439 EventTypeNames::close, connectionCloseReasonToString(reason), message)); | 446 EventTypeNames::close, connectionCloseReasonToString(reason), message)); |
| 440 } | 447 } |
| 441 | 448 |
| 442 void PresentationConnection::didFinishLoadingBlob(DOMArrayBuffer* buffer) { | 449 void PresentationConnection::didFinishLoadingBlob(DOMArrayBuffer* buffer) { |
| 443 ASSERT(!m_messages.isEmpty() && m_messages.first()->type == MessageTypeBlob); | 450 ASSERT(!m_messages.isEmpty() && m_messages.first()->type == MessageTypeBlob); |
| 444 ASSERT(buffer && buffer->buffer()); | 451 ASSERT(buffer && buffer->buffer()); |
| 445 // Send the loaded blob immediately here and continue processing the queue. | 452 // Send the loaded blob immediately here and continue processing the queue. |
| 446 WebPresentationClient* client = presentationClient(getExecutionContext()); | 453 WebPresentationClient* client = presentationClient(getExecutionContext()); |
| 447 if (client) | 454 if (client) |
| 448 client->sendBlobData(m_url, m_id, | 455 client->sendBlobData(m_url, m_id, |
| 449 static_cast<const uint8_t*>(buffer->data()), | 456 static_cast<const uint8_t*>(buffer->data()), |
| 450 buffer->byteLength()); | 457 buffer->byteLength()); |
| 451 | 458 |
| 452 m_messages.removeFirst(); | 459 m_messages.removeFirst(); |
| 453 m_blobLoader.clear(); | 460 m_blobLoader.clear(); |
| 454 handleMessageQueue(); | 461 handleMessageQueue(); |
| 455 } | 462 } |
| 456 | 463 |
| 457 void PresentationConnection::didFailLoadingBlob( | 464 void PresentationConnection::didFailLoadingBlob( |
| 458 FileError::ErrorCode errorCode) { | 465 FileError::ErrorCode errorCode) { |
| 459 ASSERT(!m_messages.isEmpty() && m_messages.first()->type == MessageTypeBlob); | 466 ASSERT(!m_messages.isEmpty() && m_messages.first()->type == MessageTypeBlob); |
| 460 // FIXME: generate error message? | 467 // FIXME: generate error message? |
| 461 // Ignore the current failed blob item and continue with next items. | 468 // Ignore the current failed blob item and continue with next items. |
| 462 m_messages.removeFirst(); | 469 m_messages.removeFirst(); |
| 463 m_blobLoader.clear(); | 470 m_blobLoader.clear(); |
| 464 handleMessageQueue(); | 471 handleMessageQueue(); |
| 465 } | 472 } |
| 466 | 473 |
| 474 void PresentationConnection::dispatchStateChangeEvent(Event* event) { |
| 475 getExecutionContext()->postTask( |
| 476 BLINK_FROM_HERE, |
| 477 createSameThreadTask(&PresentationConnection::dispatchEventAsync, |
| 478 wrapPersistent(this), wrapPersistent(event))); |
| 479 } |
| 480 |
| 481 // static |
| 482 void PresentationConnection::dispatchEventAsync(EventTarget* target, |
| 483 Event* event) { |
| 484 DCHECK(target); |
| 485 DCHECK(event); |
| 486 target->dispatchEvent(event); |
| 487 } |
| 488 |
| 467 void PresentationConnection::tearDown() { | 489 void PresentationConnection::tearDown() { |
| 468 // Cancel current Blob loading if any. | 490 // Cancel current Blob loading if any. |
| 469 if (m_blobLoader) { | 491 if (m_blobLoader) { |
| 470 m_blobLoader->cancel(); | 492 m_blobLoader->cancel(); |
| 471 m_blobLoader.clear(); | 493 m_blobLoader.clear(); |
| 472 } | 494 } |
| 473 m_messages.clear(); | 495 m_messages.clear(); |
| 474 } | 496 } |
| 475 | 497 |
| 476 } // namespace blink | 498 } // namespace blink |
| OLD | NEW |