| 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/browser/presentation/presentation_service_impl.h" | 5 #include "content/browser/presentation/presentation_service_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "content/public/browser/content_browser_client.h" | 14 #include "content/public/browser/content_browser_client.h" |
| 15 #include "content/public/browser/navigation_handle.h" | 15 #include "content/public/browser/navigation_handle.h" |
| 16 #include "content/public/browser/render_frame_host.h" | 16 #include "content/public/browser/render_frame_host.h" |
| 17 #include "content/public/browser/render_process_host.h" | 17 #include "content/public/browser/render_process_host.h" |
| 18 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/common/content_client.h" | 19 #include "content/public/common/content_client.h" |
| 20 #include "content/public/common/frame_navigate_params.h" | 20 #include "content/public/common/frame_navigate_params.h" |
| 21 #include "content/public/common/presentation_connection_message.h" | 21 #include "content/public/common/presentation_connection_message.h" |
| 22 #include "content/public/common/presentation_constants.h" | |
| 23 | 22 |
| 24 namespace content { | 23 namespace content { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 const int kInvalidRequestSessionId = -1; | 27 const int kInvalidRequestSessionId = -1; |
| 29 | 28 |
| 30 int GetNextRequestSessionId() { | 29 int GetNextRequestSessionId() { |
| 31 static int next_request_session_id = 0; | 30 static int next_request_session_id = 0; |
| 32 return ++next_request_session_id; | 31 return ++next_request_session_id; |
| 33 } | 32 } |
| 34 | 33 |
| 35 // Converts a PresentationConnectionMessage |input| to a ConnectionMessage. | |
| 36 // |input|: The message to convert. | |
| 37 // |pass_ownership|: If true, function may reuse strings or buffers from | |
| 38 // |input| without copying. |input| can be freely modified. | |
| 39 blink::mojom::ConnectionMessagePtr ToMojoConnectionMessage( | |
| 40 content::PresentationConnectionMessage* input, | |
| 41 bool pass_ownership) { | |
| 42 DCHECK(input); | |
| 43 blink::mojom::ConnectionMessagePtr output( | |
| 44 blink::mojom::ConnectionMessage::New()); | |
| 45 if (input->is_binary()) { | |
| 46 // binary data | |
| 47 DCHECK(input->data); | |
| 48 output->type = blink::mojom::PresentationMessageType::BINARY; | |
| 49 if (pass_ownership) { | |
| 50 output->data = std::move(*(input->data)); | |
| 51 } else { | |
| 52 output->data = *(input->data); | |
| 53 } | |
| 54 } else { | |
| 55 // string message | |
| 56 output->type = blink::mojom::PresentationMessageType::TEXT; | |
| 57 if (pass_ownership) { | |
| 58 output->message = std::move(input->message); | |
| 59 } else { | |
| 60 output->message = input->message; | |
| 61 } | |
| 62 } | |
| 63 return output; | |
| 64 } | |
| 65 | |
| 66 std::unique_ptr<PresentationConnectionMessage> GetPresentationConnectionMessage( | |
| 67 blink::mojom::ConnectionMessagePtr input) { | |
| 68 std::unique_ptr<content::PresentationConnectionMessage> output; | |
| 69 if (input.is_null()) | |
| 70 return output; | |
| 71 | |
| 72 switch (input->type) { | |
| 73 case blink::mojom::PresentationMessageType::TEXT: { | |
| 74 // Return nullptr PresentationConnectionMessage if invalid (unset | |
| 75 // |message|, | |
| 76 // set |data|, or size too large). | |
| 77 if (input->data || !input->message || | |
| 78 input->message->size() > | |
| 79 content::kMaxPresentationConnectionMessageSize) | |
| 80 return output; | |
| 81 | |
| 82 output.reset( | |
| 83 new PresentationConnectionMessage(PresentationMessageType::TEXT)); | |
| 84 output->message = std::move(input->message.value()); | |
| 85 return output; | |
| 86 } | |
| 87 case blink::mojom::PresentationMessageType::BINARY: { | |
| 88 // Return nullptr PresentationConnectionMessage if invalid (unset |data|, | |
| 89 // set | |
| 90 // |message|, or size too large). | |
| 91 if (!input->data || input->message || | |
| 92 input->data->size() > content::kMaxPresentationConnectionMessageSize) | |
| 93 return output; | |
| 94 | |
| 95 output.reset( | |
| 96 new PresentationConnectionMessage(PresentationMessageType::BINARY)); | |
| 97 output->data.reset( | |
| 98 new std::vector<uint8_t>(std::move(input->data.value()))); | |
| 99 return output; | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 NOTREACHED() << "Invalid presentation message type " << input->type; | |
| 104 return output; | |
| 105 } | |
| 106 | |
| 107 void InvokeNewSessionCallbackWithError( | 34 void InvokeNewSessionCallbackWithError( |
| 108 const PresentationServiceImpl::NewSessionCallback& callback) { | 35 const PresentationServiceImpl::NewSessionCallback& callback) { |
| 109 callback.Run(base::nullopt, | 36 callback.Run(base::nullopt, |
| 110 PresentationError( | 37 PresentationError( |
| 111 PRESENTATION_ERROR_PREVIOUS_START_IN_PROGRESS, | 38 PRESENTATION_ERROR_PREVIOUS_START_IN_PROGRESS, |
| 112 "There is already an unsettled Promise from a previous call " | 39 "There is already an unsettled Promise from a previous call " |
| 113 "to start.")); | 40 "to start.")); |
| 114 } | 41 } |
| 115 | 42 |
| 116 } // namespace | 43 } // namespace |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 | 296 |
| 370 default_presentation_urls_ = presentation_urls; | 297 default_presentation_urls_ = presentation_urls; |
| 371 controller_delegate_->SetDefaultPresentationUrls( | 298 controller_delegate_->SetDefaultPresentationUrls( |
| 372 render_process_id_, render_frame_id_, presentation_urls, | 299 render_process_id_, render_frame_id_, presentation_urls, |
| 373 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, | 300 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted, |
| 374 weak_factory_.GetWeakPtr())); | 301 weak_factory_.GetWeakPtr())); |
| 375 } | 302 } |
| 376 | 303 |
| 377 void PresentationServiceImpl::SendConnectionMessage( | 304 void PresentationServiceImpl::SendConnectionMessage( |
| 378 const PresentationSessionInfo& session_info, | 305 const PresentationSessionInfo& session_info, |
| 379 blink::mojom::ConnectionMessagePtr connection_message, | 306 const PresentationConnectionMessage& message, |
| 380 const SendConnectionMessageCallback& callback) { | 307 const SendConnectionMessageCallback& callback) { |
| 381 DVLOG(2) << "SendConnectionMessage [id]: " << session_info.presentation_id; | 308 DVLOG(2) << "SendConnectionMessage [id]: " << session_info.presentation_id; |
| 382 DCHECK(!connection_message.is_null()); | |
| 383 // send_message_callback_ should be null by now, otherwise resetting of | 309 // send_message_callback_ should be null by now, otherwise resetting of |
| 384 // send_message_callback_ with new callback will drop the old callback. | 310 // send_message_callback_ with new callback will drop the old callback. |
| 385 if (!controller_delegate_ || send_message_callback_) { | 311 if (!controller_delegate_ || send_message_callback_) { |
| 386 callback.Run(false); | 312 callback.Run(false); |
| 387 return; | 313 return; |
| 388 } | 314 } |
| 389 | 315 |
| 390 send_message_callback_.reset(new SendConnectionMessageCallback(callback)); | 316 send_message_callback_.reset(new SendConnectionMessageCallback(callback)); |
| 391 controller_delegate_->SendMessage( | 317 controller_delegate_->SendMessage( |
| 392 render_process_id_, render_frame_id_, session_info, | 318 render_process_id_, render_frame_id_, session_info, message, |
| 393 GetPresentationConnectionMessage(std::move(connection_message)), | |
| 394 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, | 319 base::Bind(&PresentationServiceImpl::OnSendMessageCallback, |
| 395 weak_factory_.GetWeakPtr())); | 320 weak_factory_.GetWeakPtr())); |
| 396 } | 321 } |
| 397 | 322 |
| 398 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { | 323 void PresentationServiceImpl::OnSendMessageCallback(bool sent) { |
| 399 // It is possible that Reset() is invoked before receiving this callback. | 324 // It is possible that Reset() is invoked before receiving this callback. |
| 400 // So, always check send_message_callback_ for non-null. | 325 // So, always check send_message_callback_ for non-null. |
| 401 if (send_message_callback_) { | 326 if (send_message_callback_) { |
| 402 send_message_callback_->Run(sent); | 327 send_message_callback_->Run(sent); |
| 403 send_message_callback_.reset(); | 328 send_message_callback_.reset(); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 473 return; | 398 return; |
| 474 | 399 |
| 475 controller_delegate_->ConnectToPresentation( | 400 controller_delegate_->ConnectToPresentation( |
| 476 render_process_id_, render_frame_id_, session_info, | 401 render_process_id_, render_frame_id_, session_info, |
| 477 std::move(controller_connection_ptr), | 402 std::move(controller_connection_ptr), |
| 478 std::move(receiver_connection_request)); | 403 std::move(receiver_connection_request)); |
| 479 } | 404 } |
| 480 | 405 |
| 481 void PresentationServiceImpl::OnConnectionMessages( | 406 void PresentationServiceImpl::OnConnectionMessages( |
| 482 const PresentationSessionInfo& session_info, | 407 const PresentationSessionInfo& session_info, |
| 483 const std::vector<std::unique_ptr<PresentationConnectionMessage>>& messages, | 408 const std::vector<PresentationConnectionMessage>& messages) { |
| 484 bool pass_ownership) { | |
| 485 DCHECK(client_); | 409 DCHECK(client_); |
| 486 | 410 |
| 487 DVLOG(2) << "OnConnectionMessages [id]: " << session_info.presentation_id; | 411 DVLOG(2) << "OnConnectionMessages [id]: " << session_info.presentation_id; |
| 488 std::vector<blink::mojom::ConnectionMessagePtr> mojo_messages( | 412 client_->OnConnectionMessagesReceived(session_info, messages); |
| 489 messages.size()); | |
| 490 std::transform( | |
| 491 messages.begin(), messages.end(), mojo_messages.begin(), | |
| 492 [pass_ownership]( | |
| 493 const std::unique_ptr<PresentationConnectionMessage>& message) { | |
| 494 return ToMojoConnectionMessage(message.get(), pass_ownership); | |
| 495 }); | |
| 496 | |
| 497 client_->OnConnectionMessagesReceived(session_info, std::move(mojo_messages)); | |
| 498 } | 413 } |
| 499 | 414 |
| 500 void PresentationServiceImpl::OnReceiverConnectionAvailable( | 415 void PresentationServiceImpl::OnReceiverConnectionAvailable( |
| 501 const content::PresentationSessionInfo& session_info, | 416 const content::PresentationSessionInfo& session_info, |
| 502 PresentationConnectionPtr controller_connection_ptr, | 417 PresentationConnectionPtr controller_connection_ptr, |
| 503 PresentationConnectionRequest receiver_connection_request) { | 418 PresentationConnectionRequest receiver_connection_request) { |
| 504 DVLOG(2) << "PresentationServiceImpl::OnReceiverConnectionAvailable"; | 419 DVLOG(2) << "PresentationServiceImpl::OnReceiverConnectionAvailable"; |
| 505 | 420 |
| 506 client_->OnReceiverConnectionAvailable( | 421 client_->OnReceiverConnectionAvailable( |
| 507 session_info, std::move(controller_connection_ptr), | 422 session_info, std::move(controller_connection_ptr), |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 | 476 |
| 562 screen_availability_listeners_.clear(); | 477 screen_availability_listeners_.clear(); |
| 563 | 478 |
| 564 start_session_request_id_ = kInvalidRequestSessionId; | 479 start_session_request_id_ = kInvalidRequestSessionId; |
| 565 pending_start_session_cb_.reset(); | 480 pending_start_session_cb_.reset(); |
| 566 | 481 |
| 567 pending_join_session_cbs_.clear(); | 482 pending_join_session_cbs_.clear(); |
| 568 | 483 |
| 569 if (on_connection_messages_callback_.get()) { | 484 if (on_connection_messages_callback_.get()) { |
| 570 on_connection_messages_callback_->Run( | 485 on_connection_messages_callback_->Run( |
| 571 std::vector<blink::mojom::ConnectionMessagePtr>()); | 486 std::vector<PresentationConnectionMessage>()); |
| 572 on_connection_messages_callback_.reset(); | 487 on_connection_messages_callback_.reset(); |
| 573 } | 488 } |
| 574 | 489 |
| 575 if (send_message_callback_) { | 490 if (send_message_callback_) { |
| 576 // Run the callback with false, indicating the renderer to stop sending | 491 // Run the callback with false, indicating the renderer to stop sending |
| 577 // the requests and invalidate all pending requests. | 492 // the requests and invalidate all pending requests. |
| 578 send_message_callback_->Run(false); | 493 send_message_callback_->Run(false); |
| 579 send_message_callback_.reset(); | 494 send_message_callback_.reset(); |
| 580 } | 495 } |
| 581 } | 496 } |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 | 549 |
| 635 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( | 550 void PresentationServiceImpl::NewSessionCallbackWrapper::Run( |
| 636 const base::Optional<PresentationSessionInfo>& session_info, | 551 const base::Optional<PresentationSessionInfo>& session_info, |
| 637 const base::Optional<PresentationError>& error) { | 552 const base::Optional<PresentationError>& error) { |
| 638 DCHECK(!callback_.is_null()); | 553 DCHECK(!callback_.is_null()); |
| 639 callback_.Run(session_info, error); | 554 callback_.Run(session_info, error); |
| 640 callback_.Reset(); | 555 callback_.Reset(); |
| 641 } | 556 } |
| 642 | 557 |
| 643 } // namespace content | 558 } // namespace content |
| OLD | NEW |