Chromium Code Reviews| 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 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 330 bool is_new_navigation, | 331 bool is_new_navigation, |
| 331 bool is_same_page_navigation) { | 332 bool is_same_page_navigation) { |
| 332 blink::WebFrame* frame = render_frame()->GetWebFrame(); | 333 blink::WebFrame* frame = render_frame()->GetWebFrame(); |
| 333 // If not top-level navigation. | 334 // If not top-level navigation. |
| 334 if (frame->parent() || is_same_page_navigation) | 335 if (frame->parent() || is_same_page_navigation) |
| 335 return; | 336 return; |
| 336 | 337 |
| 337 // Remove all pending send message requests. | 338 // Remove all pending send message requests. |
| 338 MessageRequestQueue empty; | 339 MessageRequestQueue empty; |
| 339 std::swap(message_request_queue_, empty); | 340 std::swap(message_request_queue_, empty); |
| 341 | |
| 342 listening_for_messages_ = false; | |
| 340 } | 343 } |
| 341 | 344 |
| 342 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { | 345 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { |
| 343 last_known_availability_ = available; | 346 last_known_availability_ = available; |
| 344 | 347 |
| 345 if (listening_state_ == ListeningState::Waiting) | 348 if (listening_state_ == ListeningState::Waiting) |
| 346 listening_state_ = ListeningState::Active; | 349 listening_state_ = ListeningState::Active; |
| 347 | 350 |
| 348 for (auto observer : availability_observers_) | 351 for (auto observer : availability_observers_) |
| 349 observer->availabilityChanged(available); | 352 observer->availabilityChanged(available); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 363 return; | 366 return; |
| 364 | 367 |
| 365 // Reset the callback to get the next event. | 368 // Reset the callback to get the next event. |
| 366 presentation_service_->ListenForDefaultSessionStart(base::Bind( | 369 presentation_service_->ListenForDefaultSessionStart(base::Bind( |
| 367 &PresentationDispatcher::OnDefaultSessionStarted, | 370 &PresentationDispatcher::OnDefaultSessionStarted, |
| 368 base::Unretained(this))); | 371 base::Unretained(this))); |
| 369 | 372 |
| 370 if (!session_info.is_null()) { | 373 if (!session_info.is_null()) { |
| 371 controller_->didStartDefaultSession( | 374 controller_->didStartDefaultSession( |
| 372 new PresentationSessionClient(session_info.Pass())); | 375 new PresentationSessionClient(session_info.Pass())); |
| 376 StartListenForMessages(); | |
| 373 } | 377 } |
| 374 } | 378 } |
| 375 | 379 |
| 376 void PresentationDispatcher::OnSessionCreated( | 380 void PresentationDispatcher::OnSessionCreated( |
| 377 blink::WebPresentationSessionClientCallbacks* callback, | 381 blink::WebPresentationSessionClientCallbacks* callback, |
| 378 presentation::PresentationSessionInfoPtr session_info, | 382 presentation::PresentationSessionInfoPtr session_info, |
| 379 presentation::PresentationErrorPtr error) { | 383 presentation::PresentationErrorPtr error) { |
| 380 DCHECK(callback); | 384 DCHECK(callback); |
| 381 if (!error.is_null()) { | 385 if (!error.is_null()) { |
| 382 DCHECK(session_info.is_null()); | 386 DCHECK(session_info.is_null()); |
| 383 callback->onError(new blink::WebPresentationError( | 387 callback->onError(new blink::WebPresentationError( |
| 384 GetWebPresentationErrorTypeFromMojo(error->error_type), | 388 GetWebPresentationErrorTypeFromMojo(error->error_type), |
| 385 blink::WebString::fromUTF8(error->message))); | 389 blink::WebString::fromUTF8(error->message))); |
| 386 return; | 390 return; |
| 387 } | 391 } |
| 388 | 392 |
| 389 DCHECK(!session_info.is_null()); | 393 DCHECK(!session_info.is_null()); |
| 390 callback->onSuccess(new PresentationSessionClient(session_info.Pass())); | 394 callback->onSuccess(new PresentationSessionClient(session_info.Pass())); |
| 395 StartListenForMessages(); | |
| 396 } | |
| 397 | |
| 398 void PresentationDispatcher::StartListenForMessages() { | |
| 399 if (listening_for_messages_) | |
| 400 return; | |
| 401 | |
| 402 listening_for_messages_ = true; | |
| 391 presentation_service_->ListenForSessionMessages( | 403 presentation_service_->ListenForSessionMessages( |
| 392 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, | 404 base::Bind(&PresentationDispatcher::OnSessionMessagesReceived, |
| 393 base::Unretained(this))); | 405 base::Unretained(this))); |
| 394 } | 406 } |
| 395 | 407 |
| 396 void PresentationDispatcher::OnSessionStateChange( | 408 void PresentationDispatcher::OnSessionStateChange( |
| 397 presentation::PresentationSessionInfoPtr session_info, | 409 presentation::PresentationSessionInfoPtr session_info, |
| 398 presentation::PresentationSessionState session_state) { | 410 presentation::PresentationSessionState session_state) { |
| 399 if (!controller_) | 411 if (!controller_) |
| 400 return; | 412 return; |
| 401 | 413 |
| 402 presentation_service_->ListenForSessionStateChange(base::Bind( | 414 presentation_service_->ListenForSessionStateChange(base::Bind( |
| 403 &PresentationDispatcher::OnSessionStateChange, | 415 &PresentationDispatcher::OnSessionStateChange, |
| 404 base::Unretained(this))); | 416 base::Unretained(this))); |
| 405 | 417 |
| 406 DCHECK(!session_info.is_null()); | 418 DCHECK(!session_info.is_null()); |
| 407 controller_->didChangeSessionState( | 419 controller_->didChangeSessionState( |
| 408 new PresentationSessionClient(session_info.Pass()), | 420 new PresentationSessionClient(session_info.Pass()), |
| 409 GetWebPresentationSessionStateFromMojo(session_state)); | 421 GetWebPresentationSessionStateFromMojo(session_state)); |
| 410 } | 422 } |
| 411 | 423 |
| 412 void PresentationDispatcher::OnSessionMessagesReceived( | 424 void PresentationDispatcher::OnSessionMessagesReceived( |
| 413 mojo::Array<presentation::SessionMessagePtr> messages) { | 425 mojo::Array<presentation::SessionMessagePtr> messages) { |
| 426 if (!listening_for_messages_) | |
|
Kevin M
2015/07/08 20:16:08
Add a LOG/DLOG here to call our attention to stran
haibinlu
2015/07/08 20:47:41
added comments.
this may happen. frame navigated,
| |
| 427 return; | |
| 428 | |
| 414 // When messages is null, there is an error at presentation service side. | 429 // When messages is null, there is an error at presentation service side. |
| 415 if (!controller_ || messages.is_null()) | 430 if (!controller_ || messages.is_null()) { |
| 431 listening_for_messages_ = false; | |
|
Kevin M
2015/07/08 20:16:07
LOG?
haibinlu
2015/07/08 20:47:41
null message is expected to indicate messaging err
| |
| 416 return; | 432 return; |
| 433 } | |
| 417 | 434 |
| 418 for (size_t i = 0; i < messages.size(); ++i) { | 435 for (size_t i = 0; i < messages.size(); ++i) { |
| 419 if (messages[i]->type == | 436 if (messages[i]->type == |
| 420 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT) { | 437 presentation::PresentationMessageType::PRESENTATION_MESSAGE_TYPE_TEXT) { |
| 421 controller_->didReceiveSessionTextMessage( | 438 controller_->didReceiveSessionTextMessage( |
| 422 new PresentationSessionClient(messages[i]->presentation_url, | 439 new PresentationSessionClient(messages[i]->presentation_url, |
| 423 messages[i]->presentation_id), | 440 messages[i]->presentation_id), |
| 424 blink::WebString::fromUTF8(messages[i]->message)); | 441 blink::WebString::fromUTF8(messages[i]->message)); |
| 425 } else { | 442 } else { |
| 426 // TODO(haibinlu): handle binary message | 443 // TODO(haibinlu): handle binary message |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 465 if (should_listen) { | 482 if (should_listen) { |
| 466 listening_state_ = ListeningState::Waiting; | 483 listening_state_ = ListeningState::Waiting; |
| 467 presentation_service_->ListenForScreenAvailability(); | 484 presentation_service_->ListenForScreenAvailability(); |
| 468 } else { | 485 } else { |
| 469 listening_state_ = ListeningState::Inactive; | 486 listening_state_ = ListeningState::Inactive; |
| 470 presentation_service_->StopListeningForScreenAvailability(); | 487 presentation_service_->StopListeningForScreenAvailability(); |
| 471 } | 488 } |
| 472 } | 489 } |
| 473 | 490 |
| 474 } // namespace content | 491 } // namespace content |
| OLD | NEW |