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

Side by Side Diff: chrome/browser/media/router/mojo/media_router_mojo_impl.cc

Issue 2111303003: [Media Router] Replace route messaging API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix tests Created 4 years, 5 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 "chrome/browser/media/router/mojo/media_router_mojo_impl.h" 5 #include "chrome/browser/media/router/mojo/media_router_mojo_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <utility> 9 #include <utility>
10 10
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 if (!observer_list) { 589 if (!observer_list) {
590 observer_list = new base::ObserverList<PresentationSessionMessagesObserver>; 590 observer_list = new base::ObserverList<PresentationSessionMessagesObserver>;
591 messages_observers_.add(route_id, base::WrapUnique(observer_list)); 591 messages_observers_.add(route_id, base::WrapUnique(observer_list));
592 } else { 592 } else {
593 DCHECK(!observer_list->HasObserver(observer)); 593 DCHECK(!observer_list->HasObserver(observer));
594 } 594 }
595 595
596 bool should_listen = !observer_list->might_have_observers(); 596 bool should_listen = !observer_list->might_have_observers();
597 observer_list->AddObserver(observer); 597 observer_list->AddObserver(observer);
598 if (should_listen) { 598 if (should_listen) {
599 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoListenForRouteMessages, 599 SetWakeReason(
600 base::Unretained(this), route_id)); 600 MediaRouteProviderWakeReason::START_LISTENING_FOR_ROUTE_MESSAGES);
601 RunOrDefer(
602 base::Bind(&MediaRouterMojoImpl::DoStartListeningForRouteMessages,
603 base::Unretained(this), route_id));
601 } 604 }
602 } 605 }
603 606
604 void MediaRouterMojoImpl::UnregisterPresentationSessionMessagesObserver( 607 void MediaRouterMojoImpl::UnregisterPresentationSessionMessagesObserver(
605 PresentationSessionMessagesObserver* observer) { 608 PresentationSessionMessagesObserver* observer) {
606 DCHECK(thread_checker_.CalledOnValidThread()); 609 DCHECK(thread_checker_.CalledOnValidThread());
607 DCHECK(observer); 610 DCHECK(observer);
608 611
609 const MediaRoute::Id& route_id = observer->route_id(); 612 const MediaRoute::Id& route_id = observer->route_id();
610 auto* observer_list = messages_observers_.get(route_id); 613 auto* observer_list = messages_observers_.get(route_id);
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
703 const MediaRoute::Id& route_id, 706 const MediaRoute::Id& route_id,
704 std::unique_ptr<std::vector<uint8_t>> data, 707 std::unique_ptr<std::vector<uint8_t>> data,
705 const SendRouteMessageCallback& callback) { 708 const SendRouteMessageCallback& callback) {
706 DVLOG_WITH_INSTANCE(1) << "SendRouteBinaryMessage " << route_id; 709 DVLOG_WITH_INSTANCE(1) << "SendRouteBinaryMessage " << route_id;
707 mojo::Array<uint8_t> mojo_array; 710 mojo::Array<uint8_t> mojo_array;
708 mojo_array.Swap(data.get()); 711 mojo_array.Swap(data.get());
709 media_route_provider_->SendRouteBinaryMessage(route_id, std::move(mojo_array), 712 media_route_provider_->SendRouteBinaryMessage(route_id, std::move(mojo_array),
710 callback); 713 callback);
711 } 714 }
712 715
713 void MediaRouterMojoImpl::DoListenForRouteMessages( 716 void MediaRouterMojoImpl::DoStartListeningForRouteMessages(
714 const MediaRoute::Id& route_id) { 717 const MediaRoute::Id& route_id) {
715 DVLOG_WITH_INSTANCE(1) << "ListenForRouteMessages"; 718 DVLOG_WITH_INSTANCE(1) << "DoStartListeningForRouteMessages";
716 if (!ContainsValue(route_ids_listening_for_messages_, route_id)) { 719 media_route_provider_->StartListeningForRouteMessages(route_id);
717 route_ids_listening_for_messages_.insert(route_id);
718 media_route_provider_->ListenForRouteMessages(
719 route_id, base::Bind(&MediaRouterMojoImpl::OnRouteMessagesReceived,
720 base::Unretained(this), route_id));
721 }
722 } 720 }
723 721
724 void MediaRouterMojoImpl::DoStopListeningForRouteMessages( 722 void MediaRouterMojoImpl::DoStopListeningForRouteMessages(
725 const MediaRoute::Id& route_id) { 723 const MediaRoute::Id& route_id) {
726 DVLOG_WITH_INSTANCE(1) << "StopListeningForRouteMessages"; 724 DVLOG_WITH_INSTANCE(1) << "StopListeningForRouteMessages";
727
728 // No need to erase |route_ids_listening_for_messages_| entry here.
729 // It will be removed when there are no more observers by the time
730 // |OnRouteMessagesReceived| is invoked.
731 media_route_provider_->StopListeningForRouteMessages(route_id); 725 media_route_provider_->StopListeningForRouteMessages(route_id);
732 } 726 }
733 727
734 void MediaRouterMojoImpl::DoSearchSinks( 728 void MediaRouterMojoImpl::DoSearchSinks(
735 const MediaSink::Id& sink_id, 729 const MediaSink::Id& sink_id,
736 const MediaSource::Id& source_id, 730 const MediaSource::Id& source_id,
737 const std::string& search_input, 731 const std::string& search_input,
738 const std::string& domain, 732 const std::string& domain,
739 const MediaSinkSearchResponseCallback& sink_callback) { 733 const MediaSinkSearchResponseCallback& sink_callback) {
740 DVLOG_WITH_INSTANCE(1) << "SearchSinks"; 734 DVLOG_WITH_INSTANCE(1) << "SearchSinks";
741 auto sink_search_criteria = interfaces::SinkSearchCriteria::New(); 735 auto sink_search_criteria = interfaces::SinkSearchCriteria::New();
742 sink_search_criteria->input = search_input; 736 sink_search_criteria->input = search_input;
743 sink_search_criteria->domain = domain; 737 sink_search_criteria->domain = domain;
744 media_route_provider_->SearchSinks( 738 media_route_provider_->SearchSinks(
745 sink_id, source_id, std::move(sink_search_criteria), 739 sink_id, source_id, std::move(sink_search_criteria),
746 base::Bind(&ForwardSinkSearchCallback, sink_callback)); 740 base::Bind(&ForwardSinkSearchCallback, sink_callback));
747 } 741 }
748 742
749 void MediaRouterMojoImpl::OnRouteMessagesReceived( 743 void MediaRouterMojoImpl::OnRouteMessagesReceived(
750 const MediaRoute::Id& route_id, 744 const mojo::String& route_id,
751 mojo::Array<interfaces::RouteMessagePtr> messages, 745 mojo::Array<interfaces::RouteMessagePtr> messages) {
752 bool error) { 746 DVLOG_WITH_INSTANCE(1) << "OnRouteMessagesReceived";
753 DVLOG(1) << "OnRouteMessagesReceived";
754 747
755 // If |messages| is null, then no more messages will come from this route. 748 DCHECK(!messages.storage().empty());
756 // We can stop listening. 749
757 if (error) { 750 auto* observer_list = messages_observers_.get(route_id);
758 DVLOG(2) << "Encountered error in OnRouteMessagesReceived for " << route_id; 751 if (!observer_list) {
759 route_ids_listening_for_messages_.erase(route_id);
760 return; 752 return;
761 } 753 }
762 754
763 // Check if there are any observers remaining. If not, the messages 755 ScopedVector<content::PresentationSessionMessage> session_messages;
764 // can be discarded and we can stop listening for the next batch of messages. 756 session_messages.reserve(messages.size());
765 auto* observer_list = messages_observers_.get(route_id); 757 for (size_t i = 0; i < messages.size(); ++i) {
766 if (!observer_list) { 758 session_messages.push_back(
767 route_ids_listening_for_messages_.erase(route_id); 759 ConvertToPresentationSessionMessage(std::move(messages[i])));
768 return;
769 } 760 }
770 761 base::ObserverList<PresentationSessionMessagesObserver>::Iterator observer_it(
771 // If |messages| is empty, then |StopListeningForRouteMessages| was invoked 762 observer_list);
772 // but we have added back an observer since. Keep listening for more messages, 763 bool single_observer =
773 // but do not notify observers with empty list. 764 observer_it.GetNext() != nullptr && observer_it.GetNext() == nullptr;
dcheng 2016/07/18 16:14:34 This is... not nice. 1) //content/browser/present
imcheng 2016/07/18 18:56:41 The message type enum on the two definitions are s
774 if (!messages.storage().empty()) { 765 FOR_EACH_OBSERVER(PresentationSessionMessagesObserver, *observer_list,
775 ScopedVector<content::PresentationSessionMessage> session_messages; 766 OnMessagesReceived(session_messages, single_observer));
776 session_messages.reserve(messages.size());
777 for (size_t i = 0; i < messages.size(); ++i) {
778 session_messages.push_back(
779 ConvertToPresentationSessionMessage(std::move(messages[i])));
780 }
781 base::ObserverList<PresentationSessionMessagesObserver>::Iterator
782 observer_it(observer_list);
783 bool single_observer =
784 observer_it.GetNext() != nullptr && observer_it.GetNext() == nullptr;
785 FOR_EACH_OBSERVER(PresentationSessionMessagesObserver, *observer_list,
786 OnMessagesReceived(session_messages, single_observer));
787 }
788
789 // Listen for more messages.
790 media_route_provider_->ListenForRouteMessages(
791 route_id, base::Bind(&MediaRouterMojoImpl::OnRouteMessagesReceived,
792 base::Unretained(this), route_id));
793 } 767 }
794 768
795 void MediaRouterMojoImpl::OnSinkAvailabilityUpdated( 769 void MediaRouterMojoImpl::OnSinkAvailabilityUpdated(
796 SinkAvailability availability) { 770 SinkAvailability availability) {
797 if (availability_ == availability) 771 if (availability_ == availability)
798 return; 772 return;
799 773
800 availability_ = availability; 774 availability_ = availability;
801 if (availability_ == interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) { 775 if (availability_ == interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) {
802 // Sinks are no longer available. MRPM has already removed all sink queries. 776 // Sinks are no longer available. MRPM has already removed all sink queries.
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
1037 base::Unretained(this), source_id)); 1011 base::Unretained(this), source_id));
1038 } 1012 }
1039 1013
1040 void MediaRouterMojoImpl::DoUpdateMediaSinks( 1014 void MediaRouterMojoImpl::DoUpdateMediaSinks(
1041 const MediaSource::Id& source_id) { 1015 const MediaSource::Id& source_id) {
1042 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; 1016 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id;
1043 media_route_provider_->UpdateMediaSinks(source_id); 1017 media_route_provider_->UpdateMediaSinks(source_id);
1044 } 1018 }
1045 1019
1046 } // namespace media_router 1020 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698