| 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" |
| 11 #include "content/common/presentation/presentation_service.mojom.h" | 11 #include "content/common/presentation/presentation_service.mojom.h" |
| 12 #include "content/public/common/presentation_constants.h" | 12 #include "content/public/common/presentation_constants.h" |
| 13 #include "content/public/common/service_registry.h" | 13 #include "content/public/common/service_registry.h" |
| 14 #include "content/public/renderer/render_frame.h" | 14 #include "content/public/renderer/render_frame.h" |
| 15 #include "content/renderer/presentation/presentation_session_client.h" | 15 #include "content/renderer/presentation/presentation_session_client.h" |
| 16 #include "third_party/WebKit/public/platform/WebString.h" | 16 #include "third_party/WebKit/public/platform/WebString.h" |
| 17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nAvailabilityObserver.h" |
| 17 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" | 18 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nController.h" |
| 18 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nError.h" | 19 #include "third_party/WebKit/public/platform/modules/presentation/WebPresentatio
nError.h" |
| 19 #include "third_party/WebKit/public/web/WebDocument.h" | 20 #include "third_party/WebKit/public/web/WebDocument.h" |
| 20 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 21 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( | 26 blink::WebPresentationError::ErrorType GetWebPresentationErrorTypeFromMojo( |
| 26 presentation::PresentationErrorType mojoErrorType) { | 27 presentation::PresentationErrorType mojoErrorType) { |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 return session_message; | 74 return session_message; |
| 74 } | 75 } |
| 75 | 76 |
| 76 } // namespace | 77 } // namespace |
| 77 | 78 |
| 78 namespace content { | 79 namespace content { |
| 79 | 80 |
| 80 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) | 81 PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) |
| 81 : RenderFrameObserver(render_frame), | 82 : RenderFrameObserver(render_frame), |
| 82 controller_(nullptr), | 83 controller_(nullptr), |
| 83 binding_(this) { | 84 binding_(this), |
| 85 listening_state_(ListeningState::Inactive), |
| 86 last_known_availability_(false) { |
| 84 } | 87 } |
| 85 | 88 |
| 86 PresentationDispatcher::~PresentationDispatcher() { | 89 PresentationDispatcher::~PresentationDispatcher() { |
| 87 // Controller should be destroyed before the dispatcher when frame is | 90 // Controller should be destroyed before the dispatcher when frame is |
| 88 // destroyed. | 91 // destroyed. |
| 89 DCHECK(!controller_); | 92 DCHECK(!controller_); |
| 90 } | 93 } |
| 91 | 94 |
| 92 void PresentationDispatcher::setController( | 95 void PresentationDispatcher::setController( |
| 93 blink::WebPresentationController* controller) { | 96 blink::WebPresentationController* controller) { |
| (...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 void PresentationDispatcher::closeSession( | 286 void PresentationDispatcher::closeSession( |
| 284 const blink::WebString& presentationUrl, | 287 const blink::WebString& presentationUrl, |
| 285 const blink::WebString& presentationId) { | 288 const blink::WebString& presentationId) { |
| 286 ConnectToPresentationServiceIfNeeded(); | 289 ConnectToPresentationServiceIfNeeded(); |
| 287 | 290 |
| 288 presentation_service_->CloseSession( | 291 presentation_service_->CloseSession( |
| 289 presentationUrl.utf8(), | 292 presentationUrl.utf8(), |
| 290 presentationId.utf8()); | 293 presentationId.utf8()); |
| 291 } | 294 } |
| 292 | 295 |
| 296 void PresentationDispatcher::getAvailability( |
| 297 const blink::WebString& presentationUrl, |
| 298 blink::WebPresentationAvailabilityCallbacks* callbacks) { |
| 299 if (listening_state_ == ListeningState::Active) { |
| 300 callbacks->onSuccess(new bool(last_known_availability_)); |
| 301 delete callbacks; |
| 302 return; |
| 303 } |
| 304 |
| 305 availability_callbacks_.Add(callbacks); |
| 306 UpdateListeningState(); |
| 307 } |
| 308 |
| 309 void PresentationDispatcher::startListening( |
| 310 blink::WebPresentationAvailabilityObserver* observer) { |
| 311 availability_observers_.insert(observer); |
| 312 UpdateListeningState(); |
| 313 } |
| 314 |
| 315 void PresentationDispatcher::stopListening( |
| 316 blink::WebPresentationAvailabilityObserver* observer) { |
| 317 availability_observers_.erase(observer); |
| 318 UpdateListeningState(); |
| 319 } |
| 320 |
| 293 void PresentationDispatcher::DidChangeDefaultPresentation() { | 321 void PresentationDispatcher::DidChangeDefaultPresentation() { |
| 294 GURL presentation_url(GetPresentationURLFromFrame(render_frame())); | 322 GURL presentation_url(GetPresentationURLFromFrame(render_frame())); |
| 295 | 323 |
| 296 ConnectToPresentationServiceIfNeeded(); | 324 ConnectToPresentationServiceIfNeeded(); |
| 297 presentation_service_->SetDefaultPresentationURL( | 325 presentation_service_->SetDefaultPresentationURL( |
| 298 presentation_url.spec(), mojo::String()); | 326 presentation_url.spec(), mojo::String()); |
| 299 } | 327 } |
| 300 | 328 |
| 301 void PresentationDispatcher::DidCommitProvisionalLoad( | 329 void PresentationDispatcher::DidCommitProvisionalLoad( |
| 302 bool is_new_navigation, | 330 bool is_new_navigation, |
| 303 bool is_same_page_navigation) { | 331 bool is_same_page_navigation) { |
| 304 blink::WebFrame* frame = render_frame()->GetWebFrame(); | 332 blink::WebFrame* frame = render_frame()->GetWebFrame(); |
| 305 // If not top-level navigation. | 333 // If not top-level navigation. |
| 306 if (frame->parent() || is_same_page_navigation) | 334 if (frame->parent() || is_same_page_navigation) |
| 307 return; | 335 return; |
| 308 | 336 |
| 309 // Remove all pending send message requests. | 337 // Remove all pending send message requests. |
| 310 MessageRequestQueue empty; | 338 MessageRequestQueue empty; |
| 311 std::swap(message_request_queue_, empty); | 339 std::swap(message_request_queue_, empty); |
| 312 } | 340 } |
| 313 | 341 |
| 314 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { | 342 void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { |
| 315 if (controller_) | 343 last_known_availability_ = available; |
| 316 controller_->didChangeAvailability(available); | 344 |
| 345 if (listening_state_ == ListeningState::Waiting) |
| 346 listening_state_ = ListeningState::Active; |
| 347 |
| 348 for (auto observer : availability_observers_) |
| 349 observer->availabilityChanged(available); |
| 350 |
| 351 for (AvailabilityCallbacksMap::iterator iter(&availability_callbacks_); |
| 352 !iter.IsAtEnd(); iter.Advance()) { |
| 353 iter.GetCurrentValue()->onSuccess(new bool(available)); |
| 354 } |
| 355 availability_callbacks_.Clear(); |
| 356 |
| 357 UpdateListeningState(); |
| 317 } | 358 } |
| 318 | 359 |
| 319 void PresentationDispatcher::OnDefaultSessionStarted( | 360 void PresentationDispatcher::OnDefaultSessionStarted( |
| 320 presentation::PresentationSessionInfoPtr session_info) { | 361 presentation::PresentationSessionInfoPtr session_info) { |
| 321 if (!controller_) | 362 if (!controller_) |
| 322 return; | 363 return; |
| 323 | 364 |
| 324 // Reset the callback to get the next event. | 365 // Reset the callback to get the next event. |
| 325 presentation_service_->ListenForDefaultSessionStart(base::Bind( | 366 presentation_service_->ListenForDefaultSessionStart(base::Bind( |
| 326 &PresentationDispatcher::OnDefaultSessionStarted, | 367 &PresentationDispatcher::OnDefaultSessionStarted, |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 &PresentationDispatcher::OnDefaultSessionStarted, | 446 &PresentationDispatcher::OnDefaultSessionStarted, |
| 406 base::Unretained(this))); | 447 base::Unretained(this))); |
| 407 | 448 |
| 408 // TODO(imcheng): Uncomment this once it is implemented on the browser | 449 // TODO(imcheng): Uncomment this once it is implemented on the browser |
| 409 // side. (crbug.com/459006) | 450 // side. (crbug.com/459006) |
| 410 // presentation_service_->ListenForSessionStateChange(base::Bind( | 451 // presentation_service_->ListenForSessionStateChange(base::Bind( |
| 411 // &PresentationDispatcher::OnSessionStateChange, | 452 // &PresentationDispatcher::OnSessionStateChange, |
| 412 // base::Unretained(this))); | 453 // base::Unretained(this))); |
| 413 } | 454 } |
| 414 | 455 |
| 456 void PresentationDispatcher::UpdateListeningState() { |
| 457 bool should_listen = !availability_callbacks_.IsEmpty() || |
| 458 !availability_observers_.empty(); |
| 459 bool is_listening = listening_state_ != ListeningState::Inactive; |
| 460 |
| 461 if (should_listen == is_listening) |
| 462 return; |
| 463 |
| 464 ConnectToPresentationServiceIfNeeded(); |
| 465 if (should_listen) { |
| 466 listening_state_ = ListeningState::Waiting; |
| 467 presentation_service_->ListenForScreenAvailability(); |
| 468 } else { |
| 469 listening_state_ = ListeningState::Inactive; |
| 470 presentation_service_->StopListeningForScreenAvailability(); |
| 471 } |
| 472 } |
| 473 |
| 415 } // namespace content | 474 } // namespace content |
| OLD | NEW |