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/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
| 17 #include "base/stl_util.h" | 17 #include "base/stl_util.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "chrome/browser/media/router/issues_observer.h" | 19 #include "chrome/browser/media/router/issues_observer.h" |
| 20 #include "chrome/browser/media/router/media_router_factory.h" | 20 #include "chrome/browser/media/router/media_router_factory.h" |
| 21 #include "chrome/browser/media/router/media_routes_observer.h" | 21 #include "chrome/browser/media/router/media_routes_observer.h" |
| 22 #include "chrome/browser/media/router/media_sinks_observer.h" | 22 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 23 #include "chrome/browser/media/router/media_source_helper.h" | 23 #include "chrome/browser/media/router/media_source_helper.h" |
| 24 #include "chrome/browser/media/router/mojo/media_route_controller.h" | |
| 24 #include "chrome/browser/media/router/mojo/media_route_provider_util_win.h" | 25 #include "chrome/browser/media/router/mojo/media_route_provider_util_win.h" |
| 25 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h" | 26 #include "chrome/browser/media/router/mojo/media_router_mojo_metrics.h" |
| 26 #include "chrome/browser/media/router/route_message.h" | 27 #include "chrome/browser/media/router/route_message.h" |
| 27 #include "chrome/browser/media/router/route_message_observer.h" | 28 #include "chrome/browser/media/router/route_message_observer.h" |
| 28 #include "chrome/browser/sessions/session_tab_helper.h" | 29 #include "chrome/browser/sessions/session_tab_helper.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "extensions/browser/process_manager.h" | 31 #include "extensions/browser/process_manager.h" |
| 31 | 32 |
| 32 #define DVLOG_WITH_INSTANCE(level) \ | 33 #define DVLOG_WITH_INSTANCE(level) \ |
| 33 DVLOG(level) << "MR #" << instance_id_ << ": " | 34 DVLOG(level) << "MR #" << instance_id_ << ": " |
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 routes_query->joinable_route_ids = joinable_route_ids; | 225 routes_query->joinable_route_ids = joinable_route_ids; |
| 225 | 226 |
| 226 if (routes_query->observers.might_have_observers()) { | 227 if (routes_query->observers.might_have_observers()) { |
| 227 for (auto& observer : routes_query->observers) | 228 for (auto& observer : routes_query->observers) |
| 228 observer.OnRoutesUpdated(routes, joinable_route_ids); | 229 observer.OnRoutesUpdated(routes, joinable_route_ids); |
| 229 } else { | 230 } else { |
| 230 DVLOG_WITH_INSTANCE(1) | 231 DVLOG_WITH_INSTANCE(1) |
| 231 << "Received routes update without any active observers: " | 232 << "Received routes update without any active observers: " |
| 232 << media_source; | 233 << media_source; |
| 233 } | 234 } |
| 235 RemoveInvalidRouteControllers(routes); | |
| 234 } | 236 } |
| 235 | 237 |
| 236 void MediaRouterMojoImpl::RouteResponseReceived( | 238 void MediaRouterMojoImpl::RouteResponseReceived( |
| 237 const std::string& presentation_id, | 239 const std::string& presentation_id, |
| 238 bool is_incognito, | 240 bool is_incognito, |
| 239 const std::vector<MediaRouteResponseCallback>& callbacks, | 241 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 240 bool is_join, | 242 bool is_join, |
| 241 const base::Optional<MediaRoute>& media_route, | 243 const base::Optional<MediaRoute>& media_route, |
| 242 const base::Optional<std::string>& error_text, | 244 const base::Optional<std::string>& error_text, |
| 243 RouteRequestResult::ResultCode result_code) { | 245 RouteRequestResult::ResultCode result_code) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 392 const std::string& domain, | 394 const std::string& domain, |
| 393 const MediaSinkSearchResponseCallback& sink_callback) { | 395 const MediaSinkSearchResponseCallback& sink_callback) { |
| 394 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 396 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 395 | 397 |
| 396 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); | 398 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); |
| 397 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSearchSinks, | 399 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSearchSinks, |
| 398 base::Unretained(this), sink_id, source_id, | 400 base::Unretained(this), sink_id, source_id, |
| 399 search_input, domain, sink_callback)); | 401 search_input, domain, sink_callback)); |
| 400 } | 402 } |
| 401 | 403 |
| 404 scoped_refptr<MediaRouteController> MediaRouterMojoImpl::GetRouteController( | |
| 405 const MediaRoute::Id& route_id) { | |
| 406 auto it = route_controllers_.find(route_id); | |
| 407 if (it != route_controllers_.end()) | |
| 408 return scoped_refptr<MediaRouteController>(it->second); | |
| 409 | |
| 410 mojom::MediaControllerPtr media_controller; | |
| 411 mojom::MediaControllerRequest media_controller_request = | |
| 412 mojo::MakeRequest(&media_controller); | |
| 413 scoped_refptr<MediaRouteController> route_controller = | |
| 414 new MediaRouteController(route_id, std::move(media_controller), this); | |
| 415 | |
| 416 media_route_provider_->CreateMediaRouteController( | |
| 417 route_id, std::move(media_controller_request), | |
| 418 base::Bind(&MediaRouterMojoImpl::OnMediaControllerCreated, | |
| 419 base::Unretained(this), route_id)); | |
| 420 media_route_provider_->SetMediaRouteStatusObserver( | |
| 421 route_id, route_controller->BindObserverPtr()); | |
| 422 route_controllers_.insert({route_id, route_controller.get()}); | |
| 423 return route_controller; | |
| 424 } | |
| 425 | |
| 426 void MediaRouterMojoImpl::OnRouteControllerDestroyed( | |
| 427 const MediaRoute::Id& route_id) { | |
|
imcheng
2017/03/28 01:18:38
Consider passing in the controller pointer into th
takumif
2017/03/29 02:34:49
Done.
| |
| 428 route_controllers_.erase(route_id); | |
| 429 } | |
| 430 | |
| 402 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( | 431 bool MediaRouterMojoImpl::RegisterMediaSinksObserver( |
| 403 MediaSinksObserver* observer) { | 432 MediaSinksObserver* observer) { |
| 404 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 433 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 405 | 434 |
| 406 // Create an observer list for the media source and add |observer| | 435 // Create an observer list for the media source and add |observer| |
| 407 // to it. Fail if |observer| is already registered. | 436 // to it. Fail if |observer| is already registered. |
| 408 const std::string& source_id = observer->source().id(); | 437 const std::string& source_id = observer->source().id(); |
| 409 std::unique_ptr<MediaSinksQuery>& sinks_query = sinks_queries_[source_id]; | 438 std::unique_ptr<MediaSinksQuery>& sinks_query = sinks_queries_[source_id]; |
| 410 bool is_new_query = false; | 439 bool is_new_query = false; |
| 411 if (!sinks_query) { | 440 if (!sinks_query) { |
| (...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 973 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoUpdateMediaSinks, | 1002 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoUpdateMediaSinks, |
| 974 base::Unretained(this), source_id)); | 1003 base::Unretained(this), source_id)); |
| 975 } | 1004 } |
| 976 | 1005 |
| 977 void MediaRouterMojoImpl::DoUpdateMediaSinks( | 1006 void MediaRouterMojoImpl::DoUpdateMediaSinks( |
| 978 const MediaSource::Id& source_id) { | 1007 const MediaSource::Id& source_id) { |
| 979 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; | 1008 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; |
| 980 media_route_provider_->UpdateMediaSinks(source_id); | 1009 media_route_provider_->UpdateMediaSinks(source_id); |
| 981 } | 1010 } |
| 982 | 1011 |
| 1012 void MediaRouterMojoImpl::RemoveInvalidRouteControllers( | |
| 1013 const std::vector<MediaRoute>& routes) { | |
| 1014 auto it = route_controllers_.begin(); | |
| 1015 while (it != route_controllers_.end()) { | |
| 1016 const MediaRoute::Id& route_id = it->first; | |
| 1017 if (std::find_if(routes.begin(), routes.end(), | |
| 1018 [&route_id](const MediaRoute& route) { | |
| 1019 return route.media_route_id() == route_id; | |
| 1020 }) == routes.end()) { | |
| 1021 MediaRouteController* controller = it->second; | |
| 1022 it = route_controllers_.erase(it); | |
| 1023 // Invalidate() will result in another erase() call, which will be no-op. | |
| 1024 controller->Invalidate(); | |
| 1025 } else { | |
| 1026 ++it; | |
| 1027 } | |
| 1028 } | |
| 1029 } | |
| 1030 | |
| 1031 void MediaRouterMojoImpl::OnMediaControllerCreated( | |
| 1032 const MediaRoute::Id& route_id, | |
| 1033 bool success) { | |
| 1034 if (base::ContainsKey(route_controllers_, route_id) && !success) | |
|
imcheng
2017/03/28 01:18:38
Check !success first. Also you can combine the two
takumif
2017/03/29 02:34:49
Done.
| |
| 1035 route_controllers_.at(route_id)->Invalidate(); | |
|
imcheng
2017/03/28 01:18:38
Also remove the entry from the map to be safe? We
takumif
2017/03/29 02:34:49
Calling DetachRouteController() from Invalidate().
| |
| 1036 } | |
| 1037 | |
| 983 } // namespace media_router | 1038 } // namespace media_router |
| OLD | NEW |