Chromium Code Reviews| Index: content/browser/presentation/presentation_service_impl.cc |
| diff --git a/content/browser/presentation/presentation_service_impl.cc b/content/browser/presentation/presentation_service_impl.cc |
| index 6b62c5f509bd50fe01cd5882fac0e8b715e68065..a7edf67bca01e5216d7ba2d95c69dac1b9cc63c6 100644 |
| --- a/content/browser/presentation/presentation_service_impl.cc |
| +++ b/content/browser/presentation/presentation_service_impl.cc |
| @@ -101,58 +101,63 @@ void PresentationServiceImpl::OnConnectionError() { |
| delete this; |
| } |
| -PresentationServiceImpl::ScreenAvailabilityContext* |
| -PresentationServiceImpl::GetOrCreateAvailabilityContext( |
| - const std::string& presentation_url) { |
| - auto it = availability_contexts_.find(presentation_url); |
| - if (it == availability_contexts_.end()) { |
| - linked_ptr<ScreenAvailabilityContext> context( |
| - new ScreenAvailabilityContext(presentation_url)); |
| - if (!delegate_->AddScreenAvailabilityListener( |
| - render_process_id_, render_frame_id_, context.get())) { |
| - DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
| - return nullptr; |
| - } |
| - it = availability_contexts_.insert( |
| - std::make_pair(context->GetPresentationUrl(), context)).first; |
| - } |
| - return it->second.get(); |
| +void PresentationServiceImpl::SetClient( |
| + presentation::PresentationServiceClientPtr client) { |
| + client_ = client.Pass(); |
|
mark a. foltz
2015/05/09 07:01:32
DCHECK(!client_) and DCHECK(client) before doing t
imcheng (use chromium acct)
2015/05/11 22:01:54
Added DCHECK(client_.is_null());. client should be
|
| } |
| -void PresentationServiceImpl::ListenForScreenAvailability( |
| - const mojo::String& presentation_url, |
| - const ScreenAvailabilityMojoCallback& callback) { |
| +void PresentationServiceImpl::ListenForScreenAvailability() { |
|
mark a. foltz
2015/05/09 07:01:32
Ah, so this is a side effect of setting the DPU.
imcheng (use chromium acct)
2015/05/11 22:01:54
Yes. If we are already listening for screen availa
|
| DVLOG(2) << "ListenForScreenAvailability"; |
| - if (!delegate_) { |
| - callback.Run(presentation_url, false); |
| + if (!delegate_) |
|
mark a. foltz
2015/05/09 07:01:32
If the delegate isn't supported by the embedder ma
imcheng (use chromium acct)
2015/05/11 22:01:54
Ideally that would be the case. We could add a fla
|
| return; |
| - } |
| - ScreenAvailabilityContext* context = |
| - GetOrCreateAvailabilityContext(presentation_url.get()); |
| - if (!context) { |
| - callback.Run(presentation_url, false); |
| + if (screen_availability_listener_.get() && |
| + screen_availability_listener_->GetPresentationUrl() == |
| + default_presentation_url_) { |
| return; |
| } |
| - context->CallbackReceived(callback); |
| + |
| + CreateScreenAvailabilityListener(default_presentation_url_); |
| } |
| -void PresentationServiceImpl::RemoveScreenAvailabilityListener( |
| - const mojo::String& presentation_url) { |
| - DVLOG(2) << "RemoveScreenAvailabilityListener"; |
| - if (!delegate_) |
| - return; |
| +void PresentationServiceImpl::CreateScreenAvailabilityListener( |
| + const std::string& presentation_url) { |
| + DCHECK(delegate_); |
| + |
| + // (1) Unregister old listener with delegate |
| + if (screen_availability_listener_.get()) { |
|
whywhat
2015/05/11 17:58:18
Couldn't you just call StopListeningForScreenAvail
imcheng (use chromium acct)
2015/05/11 22:01:53
Done.
|
| + delegate_->RemoveScreenAvailabilityListener( |
| + render_process_id_, |
| + render_frame_id_, |
| + screen_availability_listener_.get()); |
| + } |
| - const std::string& presentation_url_str = presentation_url.get(); |
| - auto it = availability_contexts_.find(presentation_url_str); |
| - if (it == availability_contexts_.end()) |
| + // (2) Replace old listener with new listener |
| + screen_availability_listener_.reset(new ScreenAvailabilityListenerImpl( |
| + presentation_url, this)); |
| + |
| + // (3) Register new listener with delegate |
| + if (!delegate_->AddScreenAvailabilityListener( |
| + render_process_id_, |
| + render_frame_id_, |
| + screen_availability_listener_.get())) { |
| + DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; |
| + screen_availability_listener_.reset(); |
| + } |
| +} |
| + |
| +void PresentationServiceImpl::StopListeningForScreenAvailability() { |
| + DVLOG(2) << "StopListeningForScreenAvailability"; |
| + if (!delegate_) |
| return; |
| - delegate_->RemoveScreenAvailabilityListener( |
| - render_process_id_, render_frame_id_, it->second.get()); |
| - // Resolve the context's pending callbacks before removing it. |
| - it->second->OnScreenAvailabilityChanged(false); |
| - availability_contexts_.erase(it); |
| + if (screen_availability_listener_.get()) { |
| + delegate_->RemoveScreenAvailabilityListener( |
| + render_process_id_, |
| + render_frame_id_, |
| + screen_availability_listener_.get()); |
| + screen_availability_listener_.reset(); |
| + } |
| } |
| void PresentationServiceImpl::ListenForDefaultSessionStart( |
| @@ -285,19 +290,6 @@ void PresentationServiceImpl::RunAndEraseNewSessionMojoCallback( |
| pending_session_cbs_.erase(it); |
| } |
| -void PresentationServiceImpl::DoSetDefaultPresentationUrl( |
| - const std::string& default_presentation_url, |
| - const std::string& default_presentation_id) { |
| - DCHECK(delegate_); |
| - delegate_->SetDefaultPresentationUrl( |
| - render_process_id_, |
| - render_frame_id_, |
| - default_presentation_url, |
| - default_presentation_id); |
| - default_presentation_url_ = default_presentation_url; |
| - default_presentation_id_ = default_presentation_id; |
| -} |
| - |
| void PresentationServiceImpl::SetDefaultPresentationURL( |
| const mojo::String& default_presentation_url, |
| const mojo::String& default_presentation_id) { |
| @@ -314,28 +306,19 @@ void PresentationServiceImpl::SetDefaultPresentationURL( |
| return; |
| } |
| - auto old_it = availability_contexts_.find(old_default_url); |
| - // Haven't started listening yet. |
| - if (old_it == availability_contexts_.end()) { |
| - DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); |
| - return; |
| + if (old_default_url != new_default_url) { |
| + // If DPU changed, replace screen availability listeners if any. |
| + if (screen_availability_listener_.get()) |
| + CreateScreenAvailabilityListener(new_default_url); |
| } |
| - // Have already started listening. Create a listener for the new URL and |
| - // transfer the callbacks from the old listener, if any. |
| - // This is done so that a listener added before default URL is changed |
| - // will continue to work. |
| - ScreenAvailabilityContext* context = |
| - GetOrCreateAvailabilityContext(new_default_url); |
| - old_it->second->PassPendingCallbacks(context); |
| - |
| - // Remove listener for old default presentation URL. |
| - delegate_->RemoveScreenAvailabilityListener( |
| + delegate_->SetDefaultPresentationUrl( |
| render_process_id_, |
| render_frame_id_, |
| - old_it->second.get()); |
| - availability_contexts_.erase(old_it); |
| - DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); |
| + default_presentation_url, |
| + default_presentation_id); |
| + default_presentation_url_ = default_presentation_url; |
| + default_presentation_id_ = default_presentation_id; |
| } |
| void PresentationServiceImpl::CloseSession( |
| @@ -439,7 +422,7 @@ void PresentationServiceImpl::Reset() { |
| default_presentation_url_.clear(); |
| default_presentation_id_.clear(); |
| - availability_contexts_.clear(); |
| + screen_availability_listener_.reset(); |
| queued_start_session_requests_.clear(); |
| FlushNewSessionCallbacks(); |
| default_session_start_context_.reset(); |
| @@ -471,65 +454,26 @@ void PresentationServiceImpl::OnDefaultPresentationStarted( |
| default_session_start_context_->set_session(session); |
| } |
| -PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext( |
| - const std::string& presentation_url) |
| - : presentation_url_(presentation_url) { |
| -} |
| - |
| -PresentationServiceImpl::ScreenAvailabilityContext:: |
| -~ScreenAvailabilityContext() { |
| - // Ensure that pending callbacks are flushed. |
| - OnScreenAvailabilityChanged(false); |
| +PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| +::ScreenAvailabilityListenerImpl( |
| + const std::string& presentation_url, |
| + PresentationServiceImpl* service) |
| + : presentation_url_(presentation_url), |
| + service_(service) { |
| } |
|
whywhat
2015/05/11 17:58:18
DCHECK(service_)?
imcheng (use chromium acct)
2015/05/11 22:01:54
Done.
|
| -void PresentationServiceImpl::ScreenAvailabilityContext::CallbackReceived( |
| - const ScreenAvailabilityMojoCallback& callback) { |
| - // NOTE: This will overwrite previously registered callback if any. |
| - if (!available_ptr_) { |
| - // No results yet, store callback for later invocation. |
| - callbacks_.push_back(new ScreenAvailabilityMojoCallback(callback)); |
| - } else { |
| - // Run callback now, reset result. |
| - // There shouldn't be any callbacks stored in this scenario. |
| - DCHECK(!HasPendingCallbacks()); |
| - callback.Run(presentation_url_, *available_ptr_); |
| - available_ptr_.reset(); |
| - } |
| +PresentationServiceImpl::ScreenAvailabilityListenerImpl:: |
| +~ScreenAvailabilityListenerImpl() { |
| } |
| -std::string PresentationServiceImpl::ScreenAvailabilityContext |
| +std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| ::GetPresentationUrl() const { |
| return presentation_url_; |
| } |
| -void PresentationServiceImpl::ScreenAvailabilityContext |
| +void PresentationServiceImpl::ScreenAvailabilityListenerImpl |
| ::OnScreenAvailabilityChanged(bool available) { |
| - if (!HasPendingCallbacks()) { |
| - // No callback, stash the result for now. |
| - available_ptr_.reset(new bool(available)); |
| - } else { |
| - // Invoke callbacks and erase them. |
| - // There shouldn't be any result stored in this scenario. |
| - DCHECK(!available_ptr_); |
| - ScopedVector<ScreenAvailabilityMojoCallback> callbacks; |
| - callbacks.swap(callbacks_); |
| - for (const auto& callback : callbacks) |
| - callback->Run(presentation_url_, available); |
| - } |
| -} |
| - |
| -void PresentationServiceImpl::ScreenAvailabilityContext |
| - ::PassPendingCallbacks( |
| - PresentationServiceImpl::ScreenAvailabilityContext* other) { |
| - std::vector<ScreenAvailabilityMojoCallback*> callbacks; |
| - callbacks_.release(&callbacks); |
| - std::copy(callbacks.begin(), callbacks.end(), |
| - std::back_inserter(other->callbacks_)); |
| -} |
| - |
| -bool PresentationServiceImpl::ScreenAvailabilityContext |
| - ::HasPendingCallbacks() const { |
| - return !callbacks_.empty(); |
| + service_->client_->OnScreenAvailabilityUpdated(available); |
|
whywhat
2015/05/11 17:58:18
DCHECK client_?
imcheng (use chromium acct)
2015/05/11 22:01:53
Added !is_null() DCHECK to ctor.
|
| } |
| PresentationServiceImpl::StartSessionRequest::StartSessionRequest( |