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

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

Issue 1314413005: [Presentation API] 1-UA presentation support + presenter APIs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments #18-21 Created 5 years, 2 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 #include <string> 8 #include <string>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 return; 195 return;
196 196
197 scoped_ptr<ScreenAvailabilityListenerImpl> listener( 197 scoped_ptr<ScreenAvailabilityListenerImpl> listener(
198 new ScreenAvailabilityListenerImpl(availability_url, this)); 198 new ScreenAvailabilityListenerImpl(availability_url, this));
199 if (delegate_->AddScreenAvailabilityListener( 199 if (delegate_->AddScreenAvailabilityListener(
200 render_process_id_, 200 render_process_id_,
201 render_frame_id_, 201 render_frame_id_,
202 listener.get())) { 202 listener.get())) {
203 screen_availability_listeners_.set(availability_url, listener.Pass()); 203 screen_availability_listeners_.set(availability_url, listener.Pass());
204 } else { 204 } else {
205 DVLOG(1) << "AddScreenAvailabilityListener failed. Ignoring request."; 205 DVLOG(1) << "AddScreenAvailabilityListener failed. Returning false.";
206 client_->OnScreenAvailabilityUpdated(url, false);
206 } 207 }
207 } 208 }
208 209
209 void PresentationServiceImpl::StopListeningForScreenAvailability( 210 void PresentationServiceImpl::StopListeningForScreenAvailability(
210 const mojo::String& url) { 211 const mojo::String& url) {
211 DVLOG(2) << "StopListeningForScreenAvailability " << url; 212 DVLOG(2) << "StopListeningForScreenAvailability " << url;
212 if (!delegate_) 213 if (!delegate_)
213 return; 214 return;
214 215
215 const std::string& availability_url = url.get(); 216 const std::string& availability_url = url.get();
216 auto listener_it = screen_availability_listeners_.find(availability_url); 217 auto listener_it = screen_availability_listeners_.find(availability_url);
217 if (listener_it == screen_availability_listeners_.end()) 218 if (listener_it == screen_availability_listeners_.end())
218 return; 219 return;
219 220
220 delegate_->RemoveScreenAvailabilityListener( 221 delegate_->RemoveScreenAvailabilityListener(
221 render_process_id_, 222 render_process_id_,
222 render_frame_id_, 223 render_frame_id_,
223 listener_it->second); 224 listener_it->second);
224 screen_availability_listeners_.erase(listener_it); 225 screen_availability_listeners_.erase(listener_it);
225 } 226 }
226 227
227 void PresentationServiceImpl::ListenForDefaultSessionStart(
228 const DefaultSessionMojoCallback& callback) {
229 if (!default_session_start_context_.get())
230 default_session_start_context_.reset(new DefaultSessionStartContext);
231 default_session_start_context_->AddCallback(callback);
232 }
233
234 void PresentationServiceImpl::StartSession( 228 void PresentationServiceImpl::StartSession(
235 const mojo::String& presentation_url, 229 const mojo::String& presentation_url,
236 const NewSessionMojoCallback& callback) { 230 const NewSessionMojoCallback& callback) {
237 DVLOG(2) << "StartSession"; 231 DVLOG(2) << "StartSession";
238 if (!delegate_) { 232 if (!delegate_) {
239 callback.Run( 233 callback.Run(
240 presentation::PresentationSessionInfoPtr(), 234 presentation::PresentationSessionInfoPtr(),
241 presentation::PresentationError::From( 235 presentation::PresentationError::From(
242 PresentationError(PRESENTATION_ERROR_NO_AVAILABLE_SCREENS, 236 PresentationError(PRESENTATION_ERROR_NO_AVAILABLE_SCREENS,
243 "No screens found."))); 237 "No screens found.")));
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
359 pending_join_session_cbs_.erase(it); 353 pending_join_session_cbs_.erase(it);
360 } 354 }
361 355
362 void PresentationServiceImpl::SetDefaultPresentationURL( 356 void PresentationServiceImpl::SetDefaultPresentationURL(
363 const mojo::String& url) { 357 const mojo::String& url) {
364 DVLOG(2) << "SetDefaultPresentationURL"; 358 DVLOG(2) << "SetDefaultPresentationURL";
365 if (!delegate_) 359 if (!delegate_)
366 return; 360 return;
367 361
368 const std::string& new_default_url = url.get(); 362 const std::string& new_default_url = url.get();
369 if (default_presentation_url_ == new_default_url) 363 if (default_presentation_url_ == new_default_url)
miu 2015/10/20 00:56:50 Why is this check here? It feels like a waste of
imcheng 2016/06/13 22:30:01 It looks like PSDImpl already does some form of de
370 return; 364 return;
365
366 default_presentation_url_ = new_default_url;
371 delegate_->SetDefaultPresentationUrl( 367 delegate_->SetDefaultPresentationUrl(
372 render_process_id_, 368 render_process_id_,
373 render_frame_id_, 369 render_frame_id_,
374 new_default_url); 370 default_presentation_url_,
375 default_presentation_url_ = new_default_url; 371 base::Bind(&PresentationServiceImpl::OnDefaultPresentationStarted,
372 weak_factory_.GetWeakPtr()));
376 } 373 }
377 374
378 void PresentationServiceImpl::SendSessionMessage( 375 void PresentationServiceImpl::SendSessionMessage(
379 presentation::PresentationSessionInfoPtr session, 376 presentation::PresentationSessionInfoPtr session,
380 presentation::SessionMessagePtr session_message, 377 presentation::SessionMessagePtr session_message,
381 const SendMessageMojoCallback& callback) { 378 const SendMessageMojoCallback& callback) {
382 DVLOG(2) << "SendSessionMessage"; 379 DVLOG(2) << "SendSessionMessage";
383 DCHECK(!session_message.is_null()); 380 DCHECK(!session_message.is_null());
384 // send_message_callback_ should be null by now, otherwise resetting of 381 // send_message_callback_ should be null by now, otherwise resetting of
385 // send_message_callback_ with new callback will drop the old callback. 382 // send_message_callback_ with new callback will drop the old callback.
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 if (!delegate_) 447 if (!delegate_)
451 return; 448 return;
452 449
453 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>()); 450 PresentationSessionInfo session_info(session.To<PresentationSessionInfo>());
454 delegate_->ListenForSessionMessages( 451 delegate_->ListenForSessionMessages(
455 render_process_id_, render_frame_id_, session_info, 452 render_process_id_, render_frame_id_, session_info,
456 base::Bind(&PresentationServiceImpl::OnSessionMessages, 453 base::Bind(&PresentationServiceImpl::OnSessionMessages,
457 weak_factory_.GetWeakPtr(), session_info)); 454 weak_factory_.GetWeakPtr(), session_info));
458 } 455 }
459 456
457 void PresentationServiceImpl::GetPresentationReceiverSession(
458 const PresentationSessionMojoCallback& callback) {
459 DVLOG(2) << "GetPresentationReceiverSession";
460 DCHECK(receiver_session_callback_.is_null());
miu 2015/10/20 00:56:50 and DCHECK(!callback.is_null())
imcheng 2016/06/13 22:30:01 Done.
461 if (!delegate_) {
462 callback.Run(presentation::PresentationSessionInfoPtr());
463 return;
464 }
465
466 receiver_session_callback_ = callback;
467 delegate_->NotifyWhenReceiverSessionIsAvailable(
468 render_process_id_, render_frame_id_,
469 base::Bind(&PresentationServiceImpl::OnGetPresentationReceiverSession,
470 weak_factory_.GetWeakPtr()));
471 }
472
473 void PresentationServiceImpl::OnGetPresentationReceiverSession(
474 const content::PresentationSessionInfo* session_info) {
475 if (!receiver_session_callback_.is_null()) {
miu 2015/10/20 00:56:50 nit: Reduce indent on majority of the code by nega
imcheng 2016/06/13 22:30:01 Removed.
476 receiver_session_callback_.Run(
477 session_info
478 ? presentation::PresentationSessionInfo::From(*session_info)
479 : presentation::PresentationSessionInfoPtr());
480 receiver_session_callback_.reset();
481 }
482 }
483
460 void PresentationServiceImpl::OnSessionMessages( 484 void PresentationServiceImpl::OnSessionMessages(
461 const PresentationSessionInfo& session, 485 const PresentationSessionInfo& session,
462 const ScopedVector<PresentationSessionMessage>& messages, 486 const ScopedVector<PresentationSessionMessage>& messages,
463 bool pass_ownership) { 487 bool pass_ownership) {
464 DCHECK(client_); 488 DCHECK(client_);
465 489
466 DVLOG(2) << "OnSessionMessages"; 490 DVLOG(2) << "OnSessionMessages";
467 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size()); 491 mojo::Array<presentation::SessionMessagePtr> mojoMessages(messages.size());
468 for (size_t i = 0; i < messages.size(); ++i) 492 for (size_t i = 0; i < messages.size(); ++i)
469 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership); 493 mojoMessages[i] = ToMojoSessionMessage(messages[i], pass_ownership);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
516 default_presentation_url_.clear(); 540 default_presentation_url_.clear();
517 541
518 screen_availability_listeners_.clear(); 542 screen_availability_listeners_.clear();
519 session_state_listeners_.clear(); 543 session_state_listeners_.clear();
520 544
521 start_session_request_id_ = kInvalidRequestSessionId; 545 start_session_request_id_ = kInvalidRequestSessionId;
522 pending_start_session_cb_.reset(); 546 pending_start_session_cb_.reset();
523 547
524 pending_join_session_cbs_.clear(); 548 pending_join_session_cbs_.clear();
525 549
526 default_session_start_context_.reset(); 550 if (on_session_messages_callback_) {
527
528 if (on_session_messages_callback_.get()) {
529 on_session_messages_callback_->Run( 551 on_session_messages_callback_->Run(
530 mojo::Array<presentation::SessionMessagePtr>()); 552 mojo::Array<presentation::SessionMessagePtr>());
531 on_session_messages_callback_.reset(); 553 on_session_messages_callback_.reset();
532 } 554 }
533 555
534 if (send_message_callback_) { 556 if (send_message_callback_) {
535 // Run the callback with false, indicating the renderer to stop sending 557 // Run the callback with false, indicating the renderer to stop sending
536 // the requests and invalidate all pending requests. 558 // the requests and invalidate all pending requests.
537 send_message_callback_->Run(false); 559 send_message_callback_->Run(false);
538 send_message_callback_.reset(); 560 send_message_callback_.reset();
539 } 561 }
562
563 if (!receiver_session_callback_.is_null()) {
564 receiver_session_callback_.Run(presentation::PresentationSessionInfoPtr());
565 receiver_session_callback_.reset();
566 }
540 } 567 }
541 568
542 void PresentationServiceImpl::OnDelegateDestroyed() { 569 void PresentationServiceImpl::OnDelegateDestroyed() {
543 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed"; 570 DVLOG(2) << "PresentationServiceImpl::OnDelegateDestroyed";
544 delegate_ = nullptr; 571 delegate_ = nullptr;
545 Reset(); 572 Reset();
546 } 573 }
547 574
548 void PresentationServiceImpl::OnDefaultPresentationStarted( 575 void PresentationServiceImpl::OnDefaultPresentationStarted(
549 const PresentationSessionInfo& session) { 576 const PresentationSessionInfo& session) {
550 if (default_session_start_context_.get()) 577 client_->OnDefaultPresentationStarted(
551 default_session_start_context_->set_session(session); 578 presentation::PresentationSessionInfo::From(session));
552 } 579 }
553 580
554 PresentationServiceImpl::ScreenAvailabilityListenerImpl 581 PresentationServiceImpl::ScreenAvailabilityListenerImpl
555 ::ScreenAvailabilityListenerImpl( 582 ::ScreenAvailabilityListenerImpl(
556 const std::string& availability_url, 583 const std::string& availability_url,
557 PresentationServiceImpl* service) 584 PresentationServiceImpl* service)
558 : availability_url_(availability_url), 585 : availability_url_(availability_url),
559 service_(service) { 586 service_(service) {
560 DCHECK(service_); 587 DCHECK(service_);
561 DCHECK(service_->client_.get()); 588 DCHECK(service_->client_.get());
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 } 642 }
616 643
617 void PresentationServiceImpl::NewSessionMojoCallbackWrapper::Run( 644 void PresentationServiceImpl::NewSessionMojoCallbackWrapper::Run(
618 presentation::PresentationSessionInfoPtr session, 645 presentation::PresentationSessionInfoPtr session,
619 presentation::PresentationErrorPtr error) { 646 presentation::PresentationErrorPtr error) {
620 DCHECK(!callback_.is_null()); 647 DCHECK(!callback_.is_null());
621 callback_.Run(session.Pass(), error.Pass()); 648 callback_.Run(session.Pass(), error.Pass());
622 callback_.reset(); 649 callback_.reset();
623 } 650 }
624 651
625 PresentationServiceImpl::DefaultSessionStartContext
626 ::DefaultSessionStartContext() {
627 }
628
629 PresentationServiceImpl::DefaultSessionStartContext
630 ::~DefaultSessionStartContext() {
631 Reset();
632 }
633
634 void PresentationServiceImpl::DefaultSessionStartContext::AddCallback(
635 const DefaultSessionMojoCallback& callback) {
636 if (session_.get()) {
637 DCHECK(callbacks_.empty());
638 callback.Run(presentation::PresentationSessionInfo::From(*session_));
639 session_.reset();
640 } else {
641 callbacks_.push_back(new DefaultSessionMojoCallback(callback));
642 }
643 }
644
645 void PresentationServiceImpl::DefaultSessionStartContext::set_session(
646 const PresentationSessionInfo& session) {
647 if (callbacks_.empty()) {
648 session_.reset(new PresentationSessionInfo(session));
649 } else {
650 DCHECK(!session_.get());
651 ScopedVector<DefaultSessionMojoCallback> callbacks;
652 callbacks.swap(callbacks_);
653 for (const auto& callback : callbacks)
654 callback->Run(presentation::PresentationSessionInfo::From(session));
655 }
656 }
657
658 void PresentationServiceImpl::DefaultSessionStartContext::Reset() {
659 ScopedVector<DefaultSessionMojoCallback> callbacks;
660 callbacks.swap(callbacks_);
661 for (const auto& callback : callbacks)
662 callback->Run(presentation::PresentationSessionInfoPtr());
663 session_.reset();
664 }
665
666 } // namespace content 652 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698