| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/presentation/presentation_dispatcher.h" | 5 #include "content/renderer/presentation/presentation_dispatcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 219 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest( | 219 message_request_queue_.push(base::WrapUnique(CreateSendBinaryMessageRequest( |
| 220 presentationUrl, presentationId, | 220 presentationUrl, presentationId, |
| 221 blink::mojom::PresentationMessageType::BINARY, data, length, | 221 blink::mojom::PresentationMessageType::BINARY, data, length, |
| 222 connection_proxy))); | 222 connection_proxy))); |
| 223 // Start processing request if only one in the queue. | 223 // Start processing request if only one in the queue. |
| 224 if (message_request_queue_.size() == 1) | 224 if (message_request_queue_.size() == 1) |
| 225 DoSendMessage(message_request_queue_.front().get()); | 225 DoSendMessage(message_request_queue_.front().get()); |
| 226 } | 226 } |
| 227 | 227 |
| 228 void PresentationDispatcher::DoSendMessage(SendMessageRequest* request) { | 228 void PresentationDispatcher::DoSendMessage(SendMessageRequest* request) { |
| 229 ConnectToPresentationServiceIfNeeded(); | 229 DCHECK(request->connection_proxy); |
| 230 | 230 // TODO(crbug.com/684116): Remove static_cast after moving message queue logic |
| 231 presentation_service_->SendConnectionMessage( | 231 // from PresentationDispatcher to PresentationConnectionProxy. |
| 232 std::move(request->session_info), std::move(request->message), | 232 static_cast<const PresentationConnectionProxy*>(request->connection_proxy) |
| 233 base::Bind(&PresentationDispatcher::HandleSendMessageRequests, | 233 ->SendConnectionMessage( |
| 234 base::Unretained(this))); | 234 std::move(request->message), |
| 235 base::Bind(&PresentationDispatcher::HandleSendMessageRequests, |
| 236 base::Unretained(this))); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void PresentationDispatcher::HandleSendMessageRequests(bool success) { | 239 void PresentationDispatcher::HandleSendMessageRequests(bool success) { |
| 238 // In normal cases, message_request_queue_ should not be empty at this point | 240 // In normal cases, message_request_queue_ should not be empty at this point |
| 239 // of time, but when DidCommitProvisionalLoad() is invoked before receiving | 241 // of time, but when DidCommitProvisionalLoad() is invoked before receiving |
| 240 // the callback for previous send mojo call, queue would have been emptied. | 242 // the callback for previous send mojo call, queue would have been emptied. |
| 241 if (message_request_queue_.empty()) | 243 if (message_request_queue_.empty()) |
| 242 return; | 244 return; |
| 243 | 245 |
| 244 if (!success) { | 246 if (!success) { |
| 245 // PresentationServiceImpl is informing that Frame has been detached or | 247 // PresentationServiceImpl is informing that Frame has been detached or |
| 246 // navigated away. Invalidate all pending requests. | 248 // navigated away. Invalidate all pending requests. |
| 247 MessageRequestQueue empty; | 249 MessageRequestQueue empty; |
| 248 std::swap(message_request_queue_, empty); | 250 std::swap(message_request_queue_, empty); |
| 249 return; | 251 return; |
| 250 } | 252 } |
| 251 | 253 |
| 252 message_request_queue_.pop(); | 254 message_request_queue_.pop(); |
| 253 if (!message_request_queue_.empty()) { | 255 if (!message_request_queue_.empty()) { |
| 254 DoSendMessage(message_request_queue_.front().get()); | 256 DoSendMessage(message_request_queue_.front().get()); |
| 255 } | 257 } |
| 256 } | 258 } |
| 257 | 259 |
| 260 void PresentationDispatcher::SetControllerConnection( |
| 261 blink::mojom::PresentationSessionInfoPtr session_info, |
| 262 blink::WebPresentationConnection* connection) { |
| 263 DCHECK(connection); |
| 264 |
| 265 auto* controller_connection_proxy = new ControllerConnectionProxy(connection); |
| 266 connection->bindProxy(base::WrapUnique(controller_connection_proxy)); |
| 267 |
| 268 ConnectToPresentationServiceIfNeeded(); |
| 269 presentation_service_->SetPresentationConnection( |
| 270 session_info.Clone(), controller_connection_proxy->Bind(), |
| 271 controller_connection_proxy->MakeRemoteRequest()); |
| 272 } |
| 273 |
| 258 void PresentationDispatcher::closeSession( | 274 void PresentationDispatcher::closeSession( |
| 259 const blink::WebURL& presentationUrl, | 275 const blink::WebURL& presentationUrl, |
| 260 const blink::WebString& presentationId) { | 276 const blink::WebString& presentationId) { |
| 261 ConnectToPresentationServiceIfNeeded(); | 277 ConnectToPresentationServiceIfNeeded(); |
| 262 presentation_service_->CloseConnection(presentationUrl, | 278 presentation_service_->CloseConnection(presentationUrl, |
| 263 presentationId.utf8()); | 279 presentationId.utf8()); |
| 264 } | 280 } |
| 265 | 281 |
| 266 void PresentationDispatcher::terminateSession( | 282 void PresentationDispatcher::terminateSession( |
| 267 const blink::WebURL& presentationUrl, | 283 const blink::WebURL& presentationUrl, |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 for (auto* listener : modified_listeners) | 496 for (auto* listener : modified_listeners) |
| 481 TryRemoveAvailabilityListener(listener); | 497 TryRemoveAvailabilityListener(listener); |
| 482 } | 498 } |
| 483 | 499 |
| 484 void PresentationDispatcher::OnDefaultSessionStarted( | 500 void PresentationDispatcher::OnDefaultSessionStarted( |
| 485 blink::mojom::PresentationSessionInfoPtr session_info) { | 501 blink::mojom::PresentationSessionInfoPtr session_info) { |
| 486 if (!controller_) | 502 if (!controller_) |
| 487 return; | 503 return; |
| 488 | 504 |
| 489 if (!session_info.is_null()) { | 505 if (!session_info.is_null()) { |
| 490 presentation_service_->ListenForConnectionMessages(session_info.Clone()); | |
| 491 auto* connection = controller_->didStartDefaultSession( | 506 auto* connection = controller_->didStartDefaultSession( |
| 492 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); | 507 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); |
| 493 connection->bindProxy( | 508 |
| 494 base::MakeUnique<ControllerConnectionProxy>(connection)); | 509 if (connection) { |
| 510 SetControllerConnection(session_info.Clone(), connection); |
| 511 // Change blink connection state to 'connected' before listening to |
| 512 // connection message. Remove ListenForConnectionMessage() after |
| 513 // TODO(crbug.com/687011): use BrowserPresentationConnectionProxy to send |
| 514 // message from route to blink connection. |
| 515 presentation_service_->ListenForConnectionMessages(session_info.Clone()); |
| 516 } |
| 495 } | 517 } |
| 496 } | 518 } |
| 497 | 519 |
| 498 void PresentationDispatcher::OnSessionCreated( | 520 void PresentationDispatcher::OnSessionCreated( |
| 499 std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback, | 521 std::unique_ptr<blink::WebPresentationConnectionCallbacks> callback, |
| 500 blink::mojom::PresentationSessionInfoPtr session_info, | 522 blink::mojom::PresentationSessionInfoPtr session_info, |
| 501 blink::mojom::PresentationErrorPtr error) { | 523 blink::mojom::PresentationErrorPtr error) { |
| 502 DCHECK(callback); | 524 DCHECK(callback); |
| 503 if (!error.is_null()) { | 525 if (!error.is_null()) { |
| 504 DCHECK(session_info.is_null()); | 526 DCHECK(session_info.is_null()); |
| 505 callback->onError(blink::WebPresentationError( | 527 callback->onError(blink::WebPresentationError( |
| 506 GetWebPresentationErrorTypeFromMojo(error->error_type), | 528 GetWebPresentationErrorTypeFromMojo(error->error_type), |
| 507 blink::WebString::fromUTF8(error->message))); | 529 blink::WebString::fromUTF8(error->message))); |
| 508 return; | 530 return; |
| 509 } | 531 } |
| 510 | 532 |
| 511 DCHECK(!session_info.is_null()); | 533 DCHECK(!session_info.is_null()); |
| 512 presentation_service_->ListenForConnectionMessages(session_info.Clone()); | |
| 513 callback->onSuccess( | 534 callback->onSuccess( |
| 514 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); | 535 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); |
| 515 | 536 // Change blink connection state to 'connected' before listening to |
| 516 auto* connection = callback->getConnection(); | 537 // connection message. Remove ListenForConnectionMessage() after |
| 517 connection->bindProxy( | 538 // TODO(crbug.com/687011): use BrowserPresentationConnectionProxy to send |
| 518 base::MakeUnique<ControllerConnectionProxy>(connection)); | 539 // message from route to blink connection. |
| 540 SetControllerConnection(session_info.Clone(), callback->getConnection()); |
| 541 presentation_service_->ListenForConnectionMessages(session_info.Clone()); |
| 519 } | 542 } |
| 520 | 543 |
| 521 void PresentationDispatcher::OnReceiverConnectionAvailable( | 544 void PresentationDispatcher::OnReceiverConnectionAvailable( |
| 522 blink::mojom::PresentationSessionInfoPtr session_info, | 545 blink::mojom::PresentationSessionInfoPtr session_info, |
| 523 blink::mojom::PresentationConnectionPtr controller_connection_ptr, | 546 blink::mojom::PresentationConnectionPtr controller_connection_ptr, |
| 524 blink::mojom::PresentationConnectionRequest receiver_connection_request) { | 547 blink::mojom::PresentationConnectionRequest receiver_connection_request) { |
| 525 DCHECK(receiver_); | 548 DCHECK(receiver_); |
| 549 |
| 526 // Bind receiver_connection_proxy with PresentationConnection in receiver | 550 // Bind receiver_connection_proxy with PresentationConnection in receiver |
| 527 // page. | 551 // page. |
| 528 auto* connection = receiver_->onReceiverConnectionAvailable( | 552 auto* connection = receiver_->onReceiverConnectionAvailable( |
| 529 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); | 553 mojo::ConvertTo<blink::WebPresentationSessionInfo>(session_info)); |
| 530 auto* receiver_connection_proxy = new ReceiverConnectionProxy(connection); | 554 auto* receiver_connection_proxy = new ReceiverConnectionProxy(connection); |
| 531 connection->bindProxy(base::WrapUnique(receiver_connection_proxy)); | 555 connection->bindProxy(base::WrapUnique(receiver_connection_proxy)); |
| 532 | 556 |
| 533 receiver_connection_proxy->Bind(std::move(receiver_connection_request)); | 557 receiver_connection_proxy->Bind(std::move(receiver_connection_request)); |
| 534 receiver_connection_proxy->BindControllerConnection( | 558 receiver_connection_proxy->BindControllerConnection( |
| 535 std::move(controller_connection_ptr)); | 559 std::move(controller_connection_ptr)); |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 760 | 784 |
| 761 PresentationDispatcher::ListeningStatus::ListeningStatus( | 785 PresentationDispatcher::ListeningStatus::ListeningStatus( |
| 762 const GURL& availability_url) | 786 const GURL& availability_url) |
| 763 : url(availability_url), | 787 : url(availability_url), |
| 764 last_known_availability(ScreenAvailability::UNKNOWN), | 788 last_known_availability(ScreenAvailability::UNKNOWN), |
| 765 listening_state(ListeningState::INACTIVE) {} | 789 listening_state(ListeningState::INACTIVE) {} |
| 766 | 790 |
| 767 PresentationDispatcher::ListeningStatus::~ListeningStatus() {} | 791 PresentationDispatcher::ListeningStatus::~ListeningStatus() {} |
| 768 | 792 |
| 769 } // namespace content | 793 } // namespace content |
| OLD | NEW |