| OLD | NEW |
| 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 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 if (!sinks_query->observers.might_have_observers()) { | 230 if (!sinks_query->observers.might_have_observers()) { |
| 231 DVLOG_WITH_INSTANCE(1) | 231 DVLOG_WITH_INSTANCE(1) |
| 232 << "Received sink list without any active observers: " << media_source; | 232 << "Received sink list without any active observers: " << media_source; |
| 233 } else { | 233 } else { |
| 234 FOR_EACH_OBSERVER( | 234 FOR_EACH_OBSERVER( |
| 235 MediaSinksObserver, sinks_query->observers, | 235 MediaSinksObserver, sinks_query->observers, |
| 236 OnSinksUpdated(sinks_query->cached_sink_list, sinks_query->origins)); | 236 OnSinksUpdated(sinks_query->cached_sink_list, sinks_query->origins)); |
| 237 } | 237 } |
| 238 } | 238 } |
| 239 | 239 |
| 240 void MediaRouterMojoImpl::OnSearchSinkIdReceived( |
| 241 const mojo::String& pseudo_sink_id, |
| 242 const mojo::String& sink_id) { |
| 243 auto sink_id_callback_entry = search_callbacks_.find(pseudo_sink_id); |
| 244 if (sink_id_callback_entry == search_callbacks_.end()) { |
| 245 // Another concurrent search query might have already called the callback |
| 246 // the MRPM was trying to reference. |
| 247 return; |
| 248 } |
| 249 for (auto& callback : sink_id_callback_entry->second) { |
| 250 callback.Run(sink_id); |
| 251 } |
| 252 search_callbacks_.erase(sink_id_callback_entry); |
| 253 } |
| 254 |
| 240 void MediaRouterMojoImpl::OnRoutesUpdated( | 255 void MediaRouterMojoImpl::OnRoutesUpdated( |
| 241 mojo::Array<interfaces::MediaRoutePtr> routes, | 256 mojo::Array<interfaces::MediaRoutePtr> routes, |
| 242 const mojo::String& media_source, | 257 const mojo::String& media_source, |
| 243 mojo::Array<mojo::String> joinable_route_ids) { | 258 mojo::Array<mojo::String> joinable_route_ids) { |
| 244 DCHECK(thread_checker_.CalledOnValidThread()); | 259 DCHECK(thread_checker_.CalledOnValidThread()); |
| 245 | 260 |
| 246 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated"; | 261 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated"; |
| 247 auto it = routes_queries_.find(media_source); | 262 auto it = routes_queries_.find(media_source); |
| 248 if (it == routes_queries_.end() || | 263 if (it == routes_queries_.end() || |
| 249 !(it->second->observers.might_have_observers())) { | 264 !(it->second->observers.might_have_observers())) { |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 void MediaRouterMojoImpl::OnUserGesture() { | 447 void MediaRouterMojoImpl::OnUserGesture() { |
| 433 // Allow MRPM to intelligently update sinks and observers by passing in a | 448 // Allow MRPM to intelligently update sinks and observers by passing in a |
| 434 // media source. | 449 // media source. |
| 435 UpdateMediaSinks(MediaSourceForDesktop().id()); | 450 UpdateMediaSinks(MediaSourceForDesktop().id()); |
| 436 | 451 |
| 437 #if defined(OS_WIN) | 452 #if defined(OS_WIN) |
| 438 EnsureMdnsDiscoveryEnabled(); | 453 EnsureMdnsDiscoveryEnabled(); |
| 439 #endif | 454 #endif |
| 440 } | 455 } |
| 441 | 456 |
| 457 void MediaRouterMojoImpl::SearchSinksAndCreateRoute( |
| 458 const MediaSink::Id& sink_id, |
| 459 const MediaSource::Id& source_id, |
| 460 const std::string& search_input, |
| 461 const std::string& domain, |
| 462 const GURL& origin, |
| 463 content::WebContents* web_contents, |
| 464 const std::vector<MediaRouteResponseCallback>& route_callbacks, |
| 465 const MediaSinkSearchResponseCallback& sink_callback, |
| 466 base::TimeDelta timeout, |
| 467 bool off_the_record) { |
| 468 DCHECK(thread_checker_.CalledOnValidThread()); |
| 469 |
| 470 int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 471 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); |
| 472 RunOrDefer( |
| 473 base::Bind(&MediaRouterMojoImpl::DoSearchSinksAndCreateRoute, |
| 474 base::Unretained(this), sink_id, source_id, search_input, |
| 475 domain, origin.is_empty() ? "" : origin.spec(), tab_id, |
| 476 route_callbacks, sink_callback, timeout, off_the_record)); |
| 477 } |
| 478 |
| 442 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( | 479 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( |
| 443 MediaSinksObserver* observer) { | 480 MediaSinksObserver* observer) { |
| 444 DCHECK(thread_checker_.CalledOnValidThread()); | 481 DCHECK(thread_checker_.CalledOnValidThread()); |
| 445 | 482 |
| 446 // Create an observer list for the media source and add |observer| | 483 // Create an observer list for the media source and add |observer| |
| 447 // to it. Fail if |observer| is already registered. | 484 // to it. Fail if |observer| is already registered. |
| 448 const std::string& source_id = observer->source().id(); | 485 const std::string& source_id = observer->source().id(); |
| 449 auto* sinks_query = sinks_queries_.get(source_id); | 486 auto* sinks_query = sinks_queries_.get(source_id); |
| 450 bool new_query = false; | 487 bool new_query = false; |
| 451 if (!sinks_query) { | 488 if (!sinks_query) { |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 void MediaRouterMojoImpl::DoStopListeningForRouteMessages( | 737 void MediaRouterMojoImpl::DoStopListeningForRouteMessages( |
| 701 const MediaRoute::Id& route_id) { | 738 const MediaRoute::Id& route_id) { |
| 702 DVLOG_WITH_INSTANCE(1) << "StopListeningForRouteMessages"; | 739 DVLOG_WITH_INSTANCE(1) << "StopListeningForRouteMessages"; |
| 703 | 740 |
| 704 // No need to erase |route_ids_listening_for_messages_| entry here. | 741 // No need to erase |route_ids_listening_for_messages_| entry here. |
| 705 // It will be removed when there are no more observers by the time | 742 // It will be removed when there are no more observers by the time |
| 706 // |OnRouteMessagesReceived| is invoked. | 743 // |OnRouteMessagesReceived| is invoked. |
| 707 media_route_provider_->StopListeningForRouteMessages(route_id); | 744 media_route_provider_->StopListeningForRouteMessages(route_id); |
| 708 } | 745 } |
| 709 | 746 |
| 747 void MediaRouterMojoImpl::DoSearchSinksAndCreateRoute( |
| 748 const MediaSink::Id& sink_id, |
| 749 const MediaSource::Id& source_id, |
| 750 const std::string& search_input, |
| 751 const std::string& domain, |
| 752 const std::string& origin, |
| 753 int tab_id, |
| 754 const std::vector<MediaRouteResponseCallback>& route_callbacks, |
| 755 const MediaSinkSearchResponseCallback& sink_callback, |
| 756 base::TimeDelta timeout, |
| 757 bool off_the_record) { |
| 758 auto sink_callback_entry = search_callbacks_.find(sink_id); |
| 759 if (sink_callback_entry != search_callbacks_.end()) { |
| 760 sink_callback_entry->second.push_back(sink_callback); |
| 761 } else { |
| 762 std::vector<MediaSinkSearchResponseCallback> callbacks{sink_callback}; |
| 763 search_callbacks_.insert(std::make_pair(sink_id, callbacks)); |
| 764 } |
| 765 |
| 766 std::string presentation_id("mr_"); |
| 767 presentation_id += base::GenerateGUID(); |
| 768 DVLOG_WITH_INSTANCE(1) << "SearchSinksAndCreateRoute"; |
| 769 auto sink_search_criteria = interfaces::SinkSearchCriteria::New(); |
| 770 sink_search_criteria->input = search_input; |
| 771 sink_search_criteria->domain = domain; |
| 772 media_route_provider_->SearchSinksAndCreateRoute( |
| 773 sink_id, source_id, std::move(sink_search_criteria), presentation_id, |
| 774 origin, tab_id, |
| 775 timeout > base::TimeDelta() ? timeout.InMilliseconds() : 0, |
| 776 off_the_record, base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 777 weak_factory_.GetWeakPtr(), presentation_id, |
| 778 off_the_record, route_callbacks)); |
| 779 } |
| 780 |
| 710 void MediaRouterMojoImpl::OnRouteMessagesReceived( | 781 void MediaRouterMojoImpl::OnRouteMessagesReceived( |
| 711 const MediaRoute::Id& route_id, | 782 const MediaRoute::Id& route_id, |
| 712 mojo::Array<interfaces::RouteMessagePtr> messages, | 783 mojo::Array<interfaces::RouteMessagePtr> messages, |
| 713 bool error) { | 784 bool error) { |
| 714 DVLOG(1) << "OnRouteMessagesReceived"; | 785 DVLOG(1) << "OnRouteMessagesReceived"; |
| 715 | 786 |
| 716 // If |messages| is null, then no more messages will come from this route. | 787 // If |messages| is null, then no more messages will come from this route. |
| 717 // We can stop listening. | 788 // We can stop listening. |
| 718 if (error) { | 789 if (error) { |
| 719 DVLOG(2) << "Encountered error in OnRouteMessagesReceived for " << route_id; | 790 DVLOG(2) << "Encountered error in OnRouteMessagesReceived for " << route_id; |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 998 base::Unretained(this), source_id)); | 1069 base::Unretained(this), source_id)); |
| 999 } | 1070 } |
| 1000 | 1071 |
| 1001 void MediaRouterMojoImpl::DoUpdateMediaSinks( | 1072 void MediaRouterMojoImpl::DoUpdateMediaSinks( |
| 1002 const MediaSource::Id& source_id) { | 1073 const MediaSource::Id& source_id) { |
| 1003 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; | 1074 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; |
| 1004 media_route_provider_->UpdateMediaSinks(source_id); | 1075 media_route_provider_->UpdateMediaSinks(source_id); |
| 1005 } | 1076 } |
| 1006 | 1077 |
| 1007 } // namespace media_router | 1078 } // namespace media_router |
| OLD | NEW |