| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 | 76 |
| 77 } // namespace | 77 } // namespace |
| 78 | 78 |
| 79 namespace content { | 79 namespace content { |
| 80 | 80 |
| 81 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) | 81 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) |
| 82 : RenderFrameObserver(render_frame), | 82 : RenderFrameObserver(render_frame), |
| 83 controller_(nullptr), | 83 controller_(nullptr), |
| 84 binding_(this), | 84 binding_(this), |
| 85 listening_state_(ListeningState::Inactive), | 85 listening_state_(ListeningState::Inactive), |
| 86 last_known_availability_(false) { | 86 last_known_availability_(false), |
| 87 listening_for_messages_(false) { |
| 87 } | 88 } |
| 88 | 89 |
| 89 PresentationDispatcher::~PresentationDispatcher() { | 90 PresentationDispatcher::~PresentationDispatcher() { |
| 90 // Controller should be destroyed before the dispatcher when frame is | 91 // Controller should be destroyed before the dispatcher when frame is |
| 91 // destroyed. | 92 // destroyed. |
| 92 DCHECK(!controller_); | 93 DCHECK(!controller_); |
| 93 } | 94 } |
| 94 | 95 |
| 95 void PresentationDispatcher::setController( | 96 void PresentationDispatcher::setController( |
| 96 blink::WebPresentationController* controller) { | 97 blink::WebPresentationController* controller) { |
| (...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 bool is_new_navigation, | 336 bool is_new_navigation, |
| 336 bool is_same_page_navigation) { | 337 bool is_same_page_navigation) { |
| 337 blink::WebFrame* frame = render_frame()->GetWebFrame(); | 338 blink::WebFrame* frame = render_frame()->GetWebFrame(); |
| 338 // If not top-level navigation. | 339 // If not top-level navigation. |
| 339 if (frame->parent() || is_same_page_navigation) | 340 if (frame->parent() || is_same_page_navigation) |
| 340 return; | 341 return; |
| 341 | 342 |
| 342 // Remove all pending send message requests. | 343 // Remove all pending send message requests. |
| 343 MessageRequestQueue empty; | 344 MessageRequestQueue empty; |
| 344 std::swap(message_request_queue_, empty); | 345 std::swap(message_request_queue_, empty); |
| 346 |
| 347 listening_for_messages_ = false; |
| 345 } | 348 } |
| 346 | 349 |
| 347 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { | 350 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { |
| 348 last_known_availability_ = available; | 351 last_known_availability_ = available; |
| 349 | 352 |
| 350 if (listening_state_ == ListeningState::Waiting) | 353 if (listening_state_ == ListeningState::Waiting) |
| 351 listening_state_ = ListeningState::Active; | 354 listening_state_ = ListeningState::Active; |
| 352 | 355 |
| 353 for (auto observer : availability_observers_) | 356 for (auto observer : availability_observers_) |
| 354 observer->availabilityChanged(available); | 357 observer->availabilityChanged(available); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 return; | 391 return; |
| 389 | 392 |
| 390 // Reset the callback to get the next event. | 393 // Reset the callback to get the next event. |
| 391 presentation_service_->ListenForDefaultSessionStart(base::Bind( | 394 presentation_service_->ListenForDefaultSessionStart(base::Bind( |
| 392 &PresentationDispatcher::OnDefaultSessionStarted, | 395 &PresentationDispatcher::OnDefaultSessionStarted, |
| 393 base::Unretained(this))); | 396 base::Unretained(this))); |
| 394 | 397 |
| 395 if (!session_info.is_null()) { | 398 if (!session_info.is_null()) { |
| 396 controller_->didStartDefaultSession( | 399 controller_->didStartDefaultSession( |
| 397 new PresentationSessionClient(session_info.Pass())); | 400 new PresentationSessionClient(session_info.Pass())); |
| 401 StartListenForMessages(); |
| 398 } | 402 } |
| 399 } | 403 } |
| 400 | 404 |
| 401 void PresentationDispatcher::OnSessionCreated( | 405 void PresentationDispatcher::OnSessionCreated( |
| 402 blink::WebPresentationSessionClientCallbacks* callback, | 406 blink::WebPresentationSessionClientCallbacks* callback, |
| 403 presentation::PresentationSessionInfoPtr session_info, | 407 presentation::PresentationSessionInfoPtr session_info, |
| 404 presentation::PresentationErrorPtr error) { | 408 presentation::PresentationErrorPtr error) { |
| 405 DCHECK(callback); | 409 DCHECK(callback); |
| 406 if (!error.is_null()) { | 410 if (!error.is_null()) { |
| 407 DCHECK(session_info.is_null()); | 411 DCHECK(session_info.is_null()); |
| 408 callback->onError(new blink::WebPresentationError( | 412 callback->onError(new blink::WebPresentationError( |
| 409 GetWebPresentationErrorTypeFromMojo(error->error_type), | 413 GetWebPresentationErrorTypeFromMojo(error->error_type), |
| 410 blink::WebString::fromUTF8(error->message))); | 414 blink::WebString::fromUTF8(error->message))); |
| 411 return; | 415 return; |
| 412 } | 416 } |
| 413 | 417 |
| 414 DCHECK(!session_info.is_null()); | 418 DCHECK(!session_info.is_null()); |
| 415 callback->onSuccess(new PresentationSessionClient(session_info.Pass())); | 419 callback->onSuccess(new PresentationSessionClient(session_info.Pass())); |
| 420 StartListenForMessages(); |
| 421 } |
| 422 |
| 423 void PresentationDispatcher::StartListenForMessages() { |
| 424 if (listening_for_messages_) |
| 425 return; |
| 426 |
| 427 listening_for_messages_ = true; |
| 416 presentation_service_->ListenForSessionMessages( | 428 presentation_service_->ListenForSessionMessages( |
| 417 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, | 429 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, |
| 418 base::Unretained(this))); | 430 base::Unretained(this))); |
| 419 } | 431 } |
| 420 | 432 |
| 421 void PresentationDispatcher::OnSessionStateChanged( | 433 void PresentationDispatcher::OnSessionStateChanged( |
| 422 presentation::PresentationSessionInfoPtr session_info, | 434 presentation::PresentationSessionInfoPtr session_info, |
| 423 presentation::PresentationSessionState session_state) { | 435 presentation::PresentationSessionState session_state) { |
| 424 if (!controller_) | 436 if (!controller_) |
| 425 return; | 437 return; |
| 426 | 438 |
| 427 DCHECK(!session_info.is_null()); | 439 DCHECK(!session_info.is_null()); |
| 428 controller_->didChangeSessionState( | 440 controller_->didChangeSessionState( |
| 429 new PresentationSessionClient(session_info.Pass()), | 441 new PresentationSessionClient(session_info.Pass()), |
| 430 GetWebPresentationSessionStateFromMojo(session_state)); | 442 GetWebPresentationSessionStateFromMojo(session_state)); |
| 431 } | 443 } |
| 432 | 444 |
| 433 void PresentationDispatcher::OnSessionMessagesReceived( | 445 void PresentationDispatcher::OnSessionMessagesReceived( |
| 434 mojo::Array<presentation::SessionMessagePtr> messages) { | 446 mojo::Array<presentation::SessionMessagePtr> messages) { |
| 447 if (!listening_for_messages_) |
| 448 return; // messages may come after the frame navigated. |
| 449 |
| 435 // When messages is null, there is an error at presentation service side. | 450 // When messages is null, there is an error at presentation service side. |
| 436 if (!controller_ || messages.is_null()) | 451 if (!controller_ || messages.is_null()) { |
| 452 listening_for_messages_ = false; |
| 437 return; | 453 return; |
| 454 } |
| 438 | 455 |
| 439 for (size_t i = 0; i < messages.size(); ++i) { | 456 for (size_t i = 0; i < messages.size(); ++i) { |
| 440 if (messages[i]->type == | 457 if (messages[i]->type == |
| 441 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT) { | 458 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT) { |
| 442 controller_->didReceiveSessionTextMessage( | 459 controller_->didReceiveSessionTextMessage( |
| 443 new PresentationSessionClient(messages[i]->presentation_url, | 460 new PresentationSessionClient(messages[i]->presentation_url, |
| 444 messages[i]->presentation_id), | 461 messages[i]->presentation_id), |
| 445 blink::WebString::fromUTF8(messages[i]->message)); | 462 blink::WebString::fromUTF8(messages[i]->message)); |
| 446 } else { | 463 } else { |
| 447 // TODO(haibinlu): handle binary message | 464 // TODO(haibinlu): handle binary message |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 481 if (should_listen) { | 498 if (should_listen) { |
| 482 listening_state_ = ListeningState::Waiting; | 499 listening_state_ = ListeningState::Waiting; |
| 483 presentation_service_->ListenForScreenAvailability(); | 500 presentation_service_->ListenForScreenAvailability(); |
| 484 } else { | 501 } else { |
| 485 listening_state_ = ListeningState::Inactive; | 502 listening_state_ = ListeningState::Inactive; |
| 486 presentation_service_->StopListeningForScreenAvailability(); | 503 presentation_service_->StopListeningForScreenAvailability(); |
| 487 } | 504 } |
| 488 } | 505 } |
| 489 | 506 |
| 490 } // namespace content | 507 } // namespace content |
| OLD | NEW |