Chromium Code Reviews| Index: content/renderer/presentation/presentation_dispatcher.cc |
| diff --git a/content/renderer/presentation/presentation_dispatcher.cc b/content/renderer/presentation/presentation_dispatcher.cc |
| index f2029883f0b60fe6837857a73016d36a36decb50..cb7db256898ef89fc733c539e0aaff9fbe306af2 100644 |
| --- a/content/renderer/presentation/presentation_dispatcher.cc |
| +++ b/content/renderer/presentation/presentation_dispatcher.cc |
| @@ -14,6 +14,7 @@ |
| #include "content/public/renderer/render_frame.h" |
| #include "content/renderer/presentation/presentation_session_client.h" |
| #include "third_party/WebKit/public/platform/WebString.h" |
| +#include "third_party/WebKit/public/platform/modules/presentation/WebPresentationAvailabilityObserver.h" |
| #include "third_party/WebKit/public/platform/modules/presentation/WebPresentationController.h" |
| #include "third_party/WebKit/public/platform/modules/presentation/WebPresentationError.h" |
| #include "third_party/WebKit/public/web/WebDocument.h" |
| @@ -80,7 +81,9 @@ namespace content { |
| PresentationDispatcher::PresentationDispatcher(RenderFrame* render_frame) |
| : RenderFrameObserver(render_frame), |
| controller_(nullptr), |
| - binding_(this) { |
| + binding_(this), |
| + listening_(ListeningState::Inactive), |
| + previous_availability_(false) { |
| } |
| PresentationDispatcher::~PresentationDispatcher() { |
| @@ -290,6 +293,31 @@ void PresentationDispatcher::closeSession( |
| presentationId.utf8()); |
| } |
| +void PresentationDispatcher::getAvailability( |
| + const blink::WebString& presentationUrl, |
| + blink::WebPresentationAvailabilityCallbacks* callbacks) { |
| + if (listening_ == ListeningState::Active) { |
| + callbacks->onSuccess(&previous_availability_); |
| + delete callbacks; |
| + return; |
| + } |
| + |
| + availability_callbacks_.Add(callbacks); |
| + UpdateListeningState(); |
| +} |
| + |
| +void PresentationDispatcher::startListening( |
| + blink::WebPresentationAvailabilityObserver* observer) { |
| + availability_observers_.insert(observer); |
| + UpdateListeningState(); |
| +} |
| + |
| +void PresentationDispatcher::stopListening( |
| + blink::WebPresentationAvailabilityObserver* observer) { |
| + availability_observers_.erase(observer); |
| + UpdateListeningState(); |
| +} |
| + |
| void PresentationDispatcher::DidChangeDefaultPresentation() { |
| GURL presentation_url(GetPresentationURLFromFrame(render_frame())); |
| @@ -312,8 +340,18 @@ void PresentationDispatcher::DidCommitProvisionalLoad( |
| } |
| void PresentationDispatcher::OnScreenAvailabilityUpdated(bool available) { |
| - if (controller_) |
| - controller_->didChangeAvailability(available); |
| + previous_availability_ = available; |
|
whywhat
2015/07/02 22:42:48
nit: check if the last known availability is diffe
mlamouri (slow - plz ping)
2015/07/03 13:35:45
This is being checked before sending the event. Pr
|
| + |
| + for (auto observer : availability_observers_) |
| + observer->availabilityChanged(available); |
| + |
| + for (AvailabilityCallbacksMap::iterator iter(&availability_callbacks_); |
| + !iter.IsAtEnd(); iter.Advance()) { |
| + iter.GetCurrentValue()->onSuccess(&available); |
| + } |
| + availability_callbacks_.Clear(); |
| + |
| + UpdateListeningState(); |
| } |
| void PresentationDispatcher::OnDefaultSessionStarted( |
| @@ -412,4 +450,27 @@ void PresentationDispatcher::ConnectToPresentationServiceIfNeeded() { |
| // base::Unretained(this))); |
| } |
| +void PresentationDispatcher::UpdateListeningState() { |
| + bool should_listen = !availability_callbacks_.IsEmpty() || |
| + !availability_observers_.empty(); |
| + |
|
whywhat
2015/07/02 22:42:48
maybe something like this would be shorter and eas
mlamouri (slow - plz ping)
2015/07/03 13:35:45
Done.
|
| + if (!should_listen) { |
| + if (listening_ == ListeningState::Inactive) |
| + return; |
| + |
| + listening_ = ListeningState::Inactive; |
| + ConnectToPresentationServiceIfNeeded(); |
| + presentation_service_->StopListeningForScreenAvailability(); |
| + return; |
| + } |
| + |
| + // should_listen == true. |
| + if (listening_ != ListeningState::Inactive) |
| + return; |
| + |
| + listening_ = ListeningState::Waiting; |
| + ConnectToPresentationServiceIfNeeded(); |
| + presentation_service_->ListenForScreenAvailability(); |
| +} |
| + |
| } // namespace content |