Chromium Code Reviews| 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 |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/guid.h" | 12 #include "base/guid.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/observer_list.h" | 15 #include "base/observer_list.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
| 18 #include "chrome/browser/media/router/issues_observer.h" | 18 #include "chrome/browser/media/router/issues_observer.h" |
| 19 #include "chrome/browser/media/router/media_router_factory.h" | 19 #include "chrome/browser/media/router/media_router_factory.h" |
| 20 #include "chrome/browser/media/router/media_routes_observer.h" | 20 #include "chrome/browser/media/router/media_routes_observer.h" |
| 21 #include "chrome/browser/media/router/media_sinks_observer.h" | 21 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 22 #include "chrome/browser/media/router/media_source_helper.h" | 22 #include "chrome/browser/media/router/media_source_helper.h" |
| 23 #include "chrome/browser/media/router/mojo/media_route_controller.h" | |
| 23 #include "chrome/browser/media/router/mojo/media_route_provider_util_win.h" | 24 #include "chrome/browser/media/router/mojo/media_route_provider_util_win.h" |
| 24 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h" | 25 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h" |
| 25 #include "chrome/browser/media/router/route_message.h" | 26 #include "chrome/browser/media/router/route_message.h" |
| 26 #include "chrome/browser/media/router/route_message_observer.h" | 27 #include "chrome/browser/media/router/route_message_observer.h" |
| 27 #include "chrome/browser/sessions/session_tab_helper.h" | 28 #include "chrome/browser/sessions/session_tab_helper.h" |
| 28 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 29 #include "extensions/browser/process_manager.h" | 30 #include "extensions/browser/process_manager.h" |
| 30 | 31 |
| 31 #define DVLOG_WITH_INSTANCE(level) \ | 32 #define DVLOG_WITH_INSTANCE(level) \ |
| 32 DVLOG(level) << "MR #" << instance_id_ << ": " | 33 DVLOG(level) << "MR #" << instance_id_ << ": " |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 routes_query->joinable_route_ids = joinable_route_ids; | 224 routes_query->joinable_route_ids = joinable_route_ids; |
| 224 | 225 |
| 225 if (routes_query->observers.might_have_observers()) { | 226 if (routes_query->observers.might_have_observers()) { |
| 226 for (auto& observer : routes_query->observers) | 227 for (auto& observer : routes_query->observers) |
| 227 observer.OnRoutesUpdated(routes, joinable_route_ids); | 228 observer.OnRoutesUpdated(routes, joinable_route_ids); |
| 228 } else { | 229 } else { |
| 229 DVLOG_WITH_INSTANCE(1) | 230 DVLOG_WITH_INSTANCE(1) |
| 230 << "Received routes update without any active observers: " | 231 << "Received routes update without any active observers: " |
| 231 << media_source; | 232 << media_source; |
| 232 } | 233 } |
| 234 RemoveInvalidRouteControllers(routes); | |
| 233 } | 235 } |
| 234 | 236 |
| 235 void MediaRouterMojoImpl::RouteResponseReceived( | 237 void MediaRouterMojoImpl::RouteResponseReceived( |
| 236 const std::string& presentation_id, | 238 const std::string& presentation_id, |
| 237 bool is_incognito, | 239 bool is_incognito, |
| 238 const std::vector<MediaRouteResponseCallback>& callbacks, | 240 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 239 bool is_join, | 241 bool is_join, |
| 240 const base::Optional<MediaRoute>& media_route, | 242 const base::Optional<MediaRoute>& media_route, |
| 241 const base::Optional<std::string>& error_text, | 243 const base::Optional<std::string>& error_text, |
| 242 RouteRequestResult::ResultCode result_code) { | 244 RouteRequestResult::ResultCode result_code) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 const std::string& domain, | 393 const std::string& domain, |
| 392 const MediaSinkSearchResponseCallback& sink_callback) { | 394 const MediaSinkSearchResponseCallback& sink_callback) { |
| 393 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 395 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 394 | 396 |
| 395 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); | 397 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); |
| 396 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSearchSinks, | 398 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSearchSinks, |
| 397 base::Unretained(this), sink_id, source_id, | 399 base::Unretained(this), sink_id, source_id, |
| 398 search_input, domain, sink_callback)); | 400 search_input, domain, sink_callback)); |
| 399 } | 401 } |
| 400 | 402 |
| 403 scoped_refptr<MediaRouteController> MediaRouterMojoImpl::GetRouteController( | |
| 404 const MediaRoute::Id& route_id) { | |
| 405 if (!IsRouteValid(route_id)) | |
| 406 return nullptr; | |
| 407 | |
| 408 auto it = route_controllers_.find(route_id); | |
| 409 if (it != route_controllers_.end()) | |
| 410 return scoped_refptr<MediaRouteController>(it->second); | |
| 411 | |
| 412 scoped_refptr<MediaRouteController> route_controller = | |
| 413 new MediaRouteController(route_id, this); | |
| 414 route_controllers_.insert({route_id, route_controller.get()}); | |
| 415 | |
| 416 RunOrDefer( | |
| 417 base::Bind(&MediaRouterMojoImpl::DoCreateMediaRouteController, | |
|
imcheng
2017/04/05 07:18:02
As discussed, a flaw of doing this is that the Moj
takumif
2017/04/06 19:38:32
I tried using OnceClosures again and somehow got i
| |
| 418 base::Unretained(this), route_id, | |
| 419 base::Bind(&MediaRouterMojoImpl::OnMediaControllerCreated, | |
| 420 base::Unretained(this), route_id))); | |
| 421 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSetMediaRouteStatusObserver, | |
| 422 base::Unretained(this), route_id)); | |
| 423 | |
| 424 return route_controller; | |
| 425 } | |
| 426 | |
| 401 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( | 427 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( |
| 402 MediaSinksObserver* observer) { | 428 MediaSinksObserver* observer) { |
| 403 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 429 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 404 | 430 |
| 405 // Create an observer list for the media source and add |observer| | 431 // Create an observer list for the media source and add |observer| |
| 406 // to it. Fail if |observer| is already registered. | 432 // to it. Fail if |observer| is already registered. |
| 407 const std::string& source_id = observer->source().id(); | 433 const std::string& source_id = observer->source().id(); |
| 408 std::unique_ptr<MediaSinksQuery>& sinks_query = sinks_queries_[source_id]; | 434 std::unique_ptr<MediaSinksQuery>& sinks_query = sinks_queries_[source_id]; |
| 409 bool is_new_query = false; | 435 bool is_new_query = false; |
| 410 if (!sinks_query) { | 436 if (!sinks_query) { |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 584 it->second->RemoveObserver(observer); | 610 it->second->RemoveObserver(observer); |
| 585 if (!it->second->might_have_observers()) { | 611 if (!it->second->might_have_observers()) { |
| 586 message_observers_.erase(route_id); | 612 message_observers_.erase(route_id); |
| 587 SetWakeReason( | 613 SetWakeReason( |
| 588 MediaRouteProviderWakeReason::STOP_LISTENING_FOR_ROUTE_MESSAGES); | 614 MediaRouteProviderWakeReason::STOP_LISTENING_FOR_ROUTE_MESSAGES); |
| 589 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopListeningForRouteMessages, | 615 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopListeningForRouteMessages, |
| 590 base::Unretained(this), route_id)); | 616 base::Unretained(this), route_id)); |
| 591 } | 617 } |
| 592 } | 618 } |
| 593 | 619 |
| 620 void MediaRouterMojoImpl::DetachRouteController( | |
| 621 const MediaRoute::Id& route_id, | |
| 622 MediaRouteController* controller) { | |
| 623 auto it = route_controllers_.find(route_id); | |
| 624 if (it != route_controllers_.end() && it->second == controller) | |
| 625 route_controllers_.erase(it); | |
| 626 } | |
| 627 | |
| 594 void MediaRouterMojoImpl::DoCreateRoute( | 628 void MediaRouterMojoImpl::DoCreateRoute( |
| 595 const MediaSource::Id& source_id, | 629 const MediaSource::Id& source_id, |
| 596 const MediaSink::Id& sink_id, | 630 const MediaSink::Id& sink_id, |
| 597 const url::Origin& origin, | 631 const url::Origin& origin, |
| 598 int tab_id, | 632 int tab_id, |
| 599 const std::vector<MediaRouteResponseCallback>& callbacks, | 633 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 600 base::TimeDelta timeout, | 634 base::TimeDelta timeout, |
| 601 bool incognito) { | 635 bool incognito) { |
| 602 std::string presentation_id = MediaRouterBase::CreatePresentationId(); | 636 std::string presentation_id = MediaRouterBase::CreatePresentationId(); |
| 603 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id | 637 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 695 const std::string& domain, | 729 const std::string& domain, |
| 696 const MediaSinkSearchResponseCallback& sink_callback) { | 730 const MediaSinkSearchResponseCallback& sink_callback) { |
| 697 DVLOG_WITH_INSTANCE(1) << "SearchSinks"; | 731 DVLOG_WITH_INSTANCE(1) << "SearchSinks"; |
| 698 auto sink_search_criteria = mojom::SinkSearchCriteria::New(); | 732 auto sink_search_criteria = mojom::SinkSearchCriteria::New(); |
| 699 sink_search_criteria->input = search_input; | 733 sink_search_criteria->input = search_input; |
| 700 sink_search_criteria->domain = domain; | 734 sink_search_criteria->domain = domain; |
| 701 media_route_provider_->SearchSinks( | 735 media_route_provider_->SearchSinks( |
| 702 sink_id, source_id, std::move(sink_search_criteria), sink_callback); | 736 sink_id, source_id, std::move(sink_search_criteria), sink_callback); |
| 703 } | 737 } |
| 704 | 738 |
| 739 void MediaRouterMojoImpl::DoCreateMediaRouteController( | |
| 740 const MediaRoute::Id& route_id, | |
| 741 base::Callback<void(bool)> callback) { | |
| 742 DVLOG_WITH_INSTANCE(1) << "DoCreateMediaRouteController"; | |
| 743 auto it = route_controllers_.find(route_id); | |
| 744 if (it == route_controllers_.end()) | |
| 745 return; | |
| 746 | |
| 747 media_route_provider_->CreateMediaRouteController( | |
| 748 route_id, it->second->GetMediaControllerRequest(), callback); | |
| 749 } | |
| 750 | |
| 751 void MediaRouterMojoImpl::DoSetMediaRouteStatusObserver( | |
| 752 const MediaRoute::Id& route_id) { | |
| 753 DVLOG_WITH_INSTANCE(1) << "DoSetMediaRouteStatusObserver"; | |
| 754 auto it = route_controllers_.find(route_id); | |
| 755 if (it == route_controllers_.end()) | |
| 756 return; | |
| 757 | |
| 758 media_route_provider_->SetMediaRouteStatusObserver( | |
| 759 route_id, it->second->BindObserverPtr()); | |
| 760 } | |
| 761 | |
| 705 void MediaRouterMojoImpl::OnRouteMessagesReceived( | 762 void MediaRouterMojoImpl::OnRouteMessagesReceived( |
| 706 const std::string& route_id, | 763 const std::string& route_id, |
| 707 const std::vector<RouteMessage>& messages) { | 764 const std::vector<RouteMessage>& messages) { |
| 708 DVLOG_WITH_INSTANCE(1) << "OnRouteMessagesReceived"; | 765 DVLOG_WITH_INSTANCE(1) << "OnRouteMessagesReceived"; |
| 709 | 766 |
| 710 if (messages.empty()) | 767 if (messages.empty()) |
| 711 return; | 768 return; |
| 712 | 769 |
| 713 auto it = message_observers_.find(route_id); | 770 auto it = message_observers_.find(route_id); |
| 714 if (it == message_observers_.end()) { | 771 if (it == message_observers_.end()) { |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 972 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoUpdateMediaSinks, | 1029 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoUpdateMediaSinks, |
| 973 base::Unretained(this), source_id)); | 1030 base::Unretained(this), source_id)); |
| 974 } | 1031 } |
| 975 | 1032 |
| 976 void MediaRouterMojoImpl::DoUpdateMediaSinks( | 1033 void MediaRouterMojoImpl::DoUpdateMediaSinks( |
| 977 const MediaSource::Id& source_id) { | 1034 const MediaSource::Id& source_id) { |
| 978 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; | 1035 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; |
| 979 media_route_provider_->UpdateMediaSinks(source_id); | 1036 media_route_provider_->UpdateMediaSinks(source_id); |
| 980 } | 1037 } |
| 981 | 1038 |
| 1039 void MediaRouterMojoImpl::RemoveInvalidRouteControllers( | |
| 1040 const std::vector<MediaRoute>& routes) { | |
| 1041 auto it = route_controllers_.begin(); | |
| 1042 while (it != route_controllers_.end()) { | |
| 1043 if (IsRouteValid(it->first)) { | |
| 1044 ++it; | |
| 1045 } else { | |
| 1046 it->second->OnRouteInvalid(); | |
| 1047 it = route_controllers_.erase(it); | |
| 1048 } | |
| 1049 } | |
| 1050 } | |
| 1051 | |
| 1052 void MediaRouterMojoImpl::OnMediaControllerCreated( | |
| 1053 const MediaRoute::Id& route_id, | |
| 1054 bool success) { | |
| 1055 if (success) | |
| 1056 return; | |
| 1057 | |
| 1058 auto it = route_controllers_.find(route_id); | |
| 1059 if (it != route_controllers_.end()) | |
| 1060 it->second->Invalidate(); | |
|
imcheng
2017/04/05 07:18:02
Erase the controller from the mapping here; this c
takumif
2017/04/06 19:38:32
Done.
| |
| 1061 } | |
| 1062 | |
| 982 } // namespace media_router | 1063 } // namespace media_router |
| OLD | NEW |