Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(424)

Side by Side Diff: content/browser/presentation/presentation_service_impl.cc

Issue 1131053005: [Presentation API] Convert screen availability API into Client style. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/browser/presentation/presentation_service_impl.h" 5 #include "content/browser/presentation/presentation_service_impl.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "content/browser/presentation/presentation_type_converters.h" 10 #include "content/browser/presentation/presentation_type_converters.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 binding_.reset(new mojo::Binding<presentation::PresentationService>( 94 binding_.reset(new mojo::Binding<presentation::PresentationService>(
95 this, request.Pass())); 95 this, request.Pass()));
96 binding_->set_error_handler(this); 96 binding_->set_error_handler(this);
97 } 97 }
98 98
99 void PresentationServiceImpl::OnConnectionError() { 99 void PresentationServiceImpl::OnConnectionError() {
100 DVLOG(1) << "OnConnectionError"; 100 DVLOG(1) << "OnConnectionError";
101 delete this; 101 delete this;
102 } 102 }
103 103
104 PresentationServiceImpl::ScreenAvailabilityContext* 104 void PresentationServiceImpl::SetClient(
105 PresentationServiceImpl::GetOrCreateAvailabilityContext( 105 presentation::PresentationServiceClientPtr client) {
106 const std::string& presentation_url) { 106 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
107 auto it = availability_contexts_.find(presentation_url);
108 if (it == availability_contexts_.end()) {
109 linked_ptr<ScreenAvailabilityContext> context(
110 new ScreenAvailabilityContext(presentation_url));
111 if (!delegate_->AddScreenAvailabilityListener(
112 render_process_id_, render_frame_id_, context.get())) {
113 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
114 return nullptr;
115 }
116 it = availability_contexts_.insert(
117 std::make_pair(context->GetPresentationUrl(), context)).first;
118 }
119 return it->second.get();
120 } 107 }
121 108
122 void PresentationServiceImpl::ListenForScreenAvailability( 109 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
123 const mojo::String& presentation_url,
124 const ScreenAvailabilityMojoCallback& callback) {
125 DVLOG(2) << "ListenForScreenAvailability"; 110 DVLOG(2) << "ListenForScreenAvailability";
126 if (!delegate_) { 111 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
127 callback.Run(presentation_url, false); 112 return;
113
114 if (screen_availability_listener_.get() &&
115 screen_availability_listener_->GetPresentationUrl() ==
116 default_presentation_url_) {
128 return; 117 return;
129 } 118 }
130 119
131 ScreenAvailabilityContext* context = 120 CreateScreenAvailabilityListener(default_presentation_url_);
132 GetOrCreateAvailabilityContext(presentation_url.get());
133 if (!context) {
134 callback.Run(presentation_url, false);
135 return;
136 }
137 context->CallbackReceived(callback);
138 } 121 }
139 122
140 void PresentationServiceImpl::RemoveScreenAvailabilityListener( 123 void PresentationServiceImpl::CreateScreenAvailabilityListener(
141 const mojo::String& presentation_url) { 124 const std::string& presentation_url) {
142 DVLOG(2) << "RemoveScreenAvailabilityListener"; 125 DCHECK(delegate_);
126
127 // (1) Unregister old listener with delegate
128 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.
129 delegate_->RemoveScreenAvailabilityListener(
130 render_process_id_,
131 render_frame_id_,
132 screen_availability_listener_.get());
133 }
134
135 // (2) Replace old listener with new listener
136 screen_availability_listener_.reset(new ScreenAvailabilityListenerImpl(
137 presentation_url, this));
138
139 // (3) Register new listener with delegate
140 if (!delegate_->AddScreenAvailabilityListener(
141 render_process_id_,
142 render_frame_id_,
143 screen_availability_listener_.get())) {
144 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request.";
145 screen_availability_listener_.reset();
146 }
147 }
148
149 void PresentationServiceImpl::StopListeningForScreenAvailability() {
150 DVLOG(2) << "StopListeningForScreenAvailability";
143 if (!delegate_) 151 if (!delegate_)
144 return; 152 return;
145 153
146 const std::string& presentation_url_str = presentation_url.get(); 154 if (screen_availability_listener_.get()) {
147 auto it = availability_contexts_.find(presentation_url_str); 155 delegate_->RemoveScreenAvailabilityListener(
148 if (it == availability_contexts_.end()) 156 render_process_id_,
149 return; 157 render_frame_id_,
150 158 screen_availability_listener_.get());
151 delegate_->RemoveScreenAvailabilityListener( 159 screen_availability_listener_.reset();
152 render_process_id_, render_frame_id_, it->second.get()); 160 }
153 // Resolve the context's pending callbacks before removing it.
154 it->second->OnScreenAvailabilityChanged(false);
155 availability_contexts_.erase(it);
156 } 161 }
157 162
158 void PresentationServiceImpl::ListenForDefaultSessionStart( 163 void PresentationServiceImpl::ListenForDefaultSessionStart(
159 const DefaultSessionMojoCallback& callback) { 164 const DefaultSessionMojoCallback& callback) {
160 if (!default_session_start_context_.get()) 165 if (!default_session_start_context_.get())
161 default_session_start_context_.reset(new DefaultSessionStartContext); 166 default_session_start_context_.reset(new DefaultSessionStartContext);
162 default_session_start_context_->AddCallback(callback); 167 default_session_start_context_->AddCallback(callback);
163 } 168 }
164 169
165 void PresentationServiceImpl::StartSession( 170 void PresentationServiceImpl::StartSession(
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
278 presentation::PresentationErrorPtr error) { 283 presentation::PresentationErrorPtr error) {
279 auto it = pending_session_cbs_.find(request_session_id); 284 auto it = pending_session_cbs_.find(request_session_id);
280 if (it == pending_session_cbs_.end()) 285 if (it == pending_session_cbs_.end())
281 return; 286 return;
282 287
283 DCHECK(it->second.get()); 288 DCHECK(it->second.get());
284 it->second->Run(session.Pass(), error.Pass()); 289 it->second->Run(session.Pass(), error.Pass());
285 pending_session_cbs_.erase(it); 290 pending_session_cbs_.erase(it);
286 } 291 }
287 292
288 void PresentationServiceImpl::DoSetDefaultPresentationUrl(
289 const std::string& default_presentation_url,
290 const std::string& default_presentation_id) {
291 DCHECK(delegate_);
292 delegate_->SetDefaultPresentationUrl(
293 render_process_id_,
294 render_frame_id_,
295 default_presentation_url,
296 default_presentation_id);
297 default_presentation_url_ = default_presentation_url;
298 default_presentation_id_ = default_presentation_id;
299 }
300
301 void PresentationServiceImpl::SetDefaultPresentationURL( 293 void PresentationServiceImpl::SetDefaultPresentationURL(
302 const mojo::String& default_presentation_url, 294 const mojo::String& default_presentation_url,
303 const mojo::String& default_presentation_id) { 295 const mojo::String& default_presentation_id) {
304 DVLOG(2) << "SetDefaultPresentationURL"; 296 DVLOG(2) << "SetDefaultPresentationURL";
305 if (!delegate_) 297 if (!delegate_)
306 return; 298 return;
307 299
308 const std::string& old_default_url = default_presentation_url_; 300 const std::string& old_default_url = default_presentation_url_;
309 const std::string& new_default_url = default_presentation_url.get(); 301 const std::string& new_default_url = default_presentation_url.get();
310 302
311 // Don't call delegate if nothing changed. 303 // Don't call delegate if nothing changed.
312 if (old_default_url == new_default_url && 304 if (old_default_url == new_default_url &&
313 default_presentation_id_ == default_presentation_id) { 305 default_presentation_id_ == default_presentation_id) {
314 return; 306 return;
315 } 307 }
316 308
317 auto old_it = availability_contexts_.find(old_default_url); 309 if (old_default_url != new_default_url) {
318 // Haven't started listening yet. 310 // If DPU changed, replace screen availability listeners if any.
319 if (old_it == availability_contexts_.end()) { 311 if (screen_availability_listener_.get())
320 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); 312 CreateScreenAvailabilityListener(new_default_url);
321 return;
322 } 313 }
323 314
324 // Have already started listening. Create a listener for the new URL and 315 delegate_->SetDefaultPresentationUrl(
325 // transfer the callbacks from the old listener, if any.
326 // This is done so that a listener added before default URL is changed
327 // will continue to work.
328 ScreenAvailabilityContext* context =
329 GetOrCreateAvailabilityContext(new_default_url);
330 old_it->second->PassPendingCallbacks(context);
331
332 // Remove listener for old default presentation URL.
333 delegate_->RemoveScreenAvailabilityListener(
334 render_process_id_, 316 render_process_id_,
335 render_frame_id_, 317 render_frame_id_,
336 old_it->second.get()); 318 default_presentation_url,
337 availability_contexts_.erase(old_it); 319 default_presentation_id);
338 DoSetDefaultPresentationUrl(new_default_url, default_presentation_id); 320 default_presentation_url_ = default_presentation_url;
321 default_presentation_id_ = default_presentation_id;
339 } 322 }
340 323
341 void PresentationServiceImpl::CloseSession( 324 void PresentationServiceImpl::CloseSession(
342 const mojo::String& presentation_url, 325 const mojo::String& presentation_url,
343 const mojo::String& presentation_id) { 326 const mojo::String& presentation_id) {
344 NOTIMPLEMENTED(); 327 NOTIMPLEMENTED();
345 } 328 }
346 329
347 void PresentationServiceImpl::ListenForSessionStateChange( 330 void PresentationServiceImpl::ListenForSessionStateChange(
348 const SessionStateCallback& callback) { 331 const SessionStateCallback& callback) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 delete this; 415 delete this;
433 } 416 }
434 417
435 void PresentationServiceImpl::Reset() { 418 void PresentationServiceImpl::Reset() {
436 DVLOG(2) << "PresentationServiceImpl::Reset"; 419 DVLOG(2) << "PresentationServiceImpl::Reset";
437 if (delegate_) 420 if (delegate_)
438 delegate_->Reset(render_process_id_, render_frame_id_); 421 delegate_->Reset(render_process_id_, render_frame_id_);
439 422
440 default_presentation_url_.clear(); 423 default_presentation_url_.clear();
441 default_presentation_id_.clear(); 424 default_presentation_id_.clear();
442 availability_contexts_.clear(); 425 screen_availability_listener_.reset();
443 queued_start_session_requests_.clear(); 426 queued_start_session_requests_.clear();
444 FlushNewSessionCallbacks(); 427 FlushNewSessionCallbacks();
445 default_session_start_context_.reset(); 428 default_session_start_context_.reset();
446 if (on_session_messages_callback_.get()) { 429 if (on_session_messages_callback_.get()) {
447 on_session_messages_callback_->Run( 430 on_session_messages_callback_->Run(
448 mojo::Array<presentation::SessionMessagePtr>()); 431 mojo::Array<presentation::SessionMessagePtr>());
449 on_session_messages_callback_.reset(); 432 on_session_messages_callback_.reset();
450 } 433 }
451 } 434 }
452 435
(...skipping 11 matching lines...) Expand all
464 delegate_ = nullptr; 447 delegate_ = nullptr;
465 Reset(); 448 Reset();
466 } 449 }
467 450
468 void PresentationServiceImpl::OnDefaultPresentationStarted( 451 void PresentationServiceImpl::OnDefaultPresentationStarted(
469 const PresentationSessionInfo& session) { 452 const PresentationSessionInfo& session) {
470 if (default_session_start_context_.get()) 453 if (default_session_start_context_.get())
471 default_session_start_context_->set_session(session); 454 default_session_start_context_->set_session(session);
472 } 455 }
473 456
474 PresentationServiceImpl::ScreenAvailabilityContext::ScreenAvailabilityContext( 457 PresentationServiceImpl::ScreenAvailabilityListenerImpl
475 const std::string& presentation_url) 458 ::ScreenAvailabilityListenerImpl(
476 : presentation_url_(presentation_url) { 459 const std::string& presentation_url,
460 PresentationServiceImpl* service)
461 : presentation_url_(presentation_url),
462 service_(service) {
477 } 463 }
whywhat 2015/05/11 17:58:18 DCHECK(service_)?
imcheng (use chromium acct) 2015/05/11 22:01:54 Done.
478 464
479 PresentationServiceImpl::ScreenAvailabilityContext:: 465 PresentationServiceImpl::ScreenAvailabilityListenerImpl::
480 ~ScreenAvailabilityContext() { 466 ~ScreenAvailabilityListenerImpl() {
481 // Ensure that pending callbacks are flushed.
482 OnScreenAvailabilityChanged(false);
483 } 467 }
484 468
485 void PresentationServiceImpl::ScreenAvailabilityContext::CallbackReceived( 469 std::string PresentationServiceImpl::ScreenAvailabilityListenerImpl
486 const ScreenAvailabilityMojoCallback& callback) {
487 // NOTE: This will overwrite previously registered callback if any.
488 if (!available_ptr_) {
489 // No results yet, store callback for later invocation.
490 callbacks_.push_back(new ScreenAvailabilityMojoCallback(callback));
491 } else {
492 // Run callback now, reset result.
493 // There shouldn't be any callbacks stored in this scenario.
494 DCHECK(!HasPendingCallbacks());
495 callback.Run(presentation_url_, *available_ptr_);
496 available_ptr_.reset();
497 }
498 }
499
500 std::string PresentationServiceImpl::ScreenAvailabilityContext
501 ::GetPresentationUrl() const { 470 ::GetPresentationUrl() const {
502 return presentation_url_; 471 return presentation_url_;
503 } 472 }
504 473
505 void PresentationServiceImpl::ScreenAvailabilityContext 474 void PresentationServiceImpl::ScreenAvailabilityListenerImpl
506 ::OnScreenAvailabilityChanged(bool available) { 475 ::OnScreenAvailabilityChanged(bool available) {
507 if (!HasPendingCallbacks()) { 476 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.
508 // No callback, stash the result for now.
509 available_ptr_.reset(new bool(available));
510 } else {
511 // Invoke callbacks and erase them.
512 // There shouldn't be any result stored in this scenario.
513 DCHECK(!available_ptr_);
514 ScopedVector<ScreenAvailabilityMojoCallback> callbacks;
515 callbacks.swap(callbacks_);
516 for (const auto& callback : callbacks)
517 callback->Run(presentation_url_, available);
518 }
519 }
520
521 void PresentationServiceImpl::ScreenAvailabilityContext
522 ::PassPendingCallbacks(
523 PresentationServiceImpl::ScreenAvailabilityContext* other) {
524 std::vector<ScreenAvailabilityMojoCallback*> callbacks;
525 callbacks_.release(&callbacks);
526 std::copy(callbacks.begin(), callbacks.end(),
527 std::back_inserter(other->callbacks_));
528 }
529
530 bool PresentationServiceImpl::ScreenAvailabilityContext
531 ::HasPendingCallbacks() const {
532 return !callbacks_.empty();
533 } 477 }
534 478
535 PresentationServiceImpl::StartSessionRequest::StartSessionRequest( 479 PresentationServiceImpl::StartSessionRequest::StartSessionRequest(
536 const std::string& presentation_url, 480 const std::string& presentation_url,
537 const std::string& presentation_id, 481 const std::string& presentation_id,
538 const NewSessionMojoCallback& callback) 482 const NewSessionMojoCallback& callback)
539 : presentation_url_(presentation_url), 483 : presentation_url_(presentation_url),
540 presentation_id_(presentation_id), 484 presentation_id_(presentation_id),
541 callback_(callback) { 485 callback_(callback) {
542 } 486 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
590 void PresentationServiceImpl::DefaultSessionStartContext::Reset() { 534 void PresentationServiceImpl::DefaultSessionStartContext::Reset() {
591 ScopedVector<DefaultSessionMojoCallback> callbacks; 535 ScopedVector<DefaultSessionMojoCallback> callbacks;
592 callbacks.swap(callbacks_); 536 callbacks.swap(callbacks_);
593 for (const auto& callback : callbacks) 537 for (const auto& callback : callbacks)
594 callback->Run(presentation::PresentationSessionInfoPtr()); 538 callback->Run(presentation::PresentationSessionInfoPtr());
595 session_.reset(); 539 session_.reset();
596 } 540 }
597 541
598 } // namespace content 542 } // namespace content
599 543
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698