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

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

Powered by Google App Engine
This is Rietveld 408576698