| 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/media_router_mojo_impl.h" | 5 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 MediaRouterMediaRoutesObserver(MediaRouterMojoImpl* router) | 70 MediaRouterMediaRoutesObserver(MediaRouterMojoImpl* router) |
| 71 : MediaRoutesObserver(router), | 71 : MediaRoutesObserver(router), |
| 72 router_(router) { | 72 router_(router) { |
| 73 DCHECK(router); | 73 DCHECK(router); |
| 74 } | 74 } |
| 75 | 75 |
| 76 MediaRouterMojoImpl::MediaRouterMediaRoutesObserver:: | 76 MediaRouterMojoImpl::MediaRouterMediaRoutesObserver:: |
| 77 ~MediaRouterMediaRoutesObserver() { | 77 ~MediaRouterMediaRoutesObserver() { |
| 78 } | 78 } |
| 79 | 79 |
| 80 MediaRouterMojoImpl::MediaRoutesQuery::MediaRoutesQuery() = default; |
| 81 |
| 82 MediaRouterMojoImpl::MediaRoutesQuery::~MediaRoutesQuery() = default; |
| 83 |
| 80 MediaRouterMojoImpl::MediaSinksQuery::MediaSinksQuery() = default; | 84 MediaRouterMojoImpl::MediaSinksQuery::MediaSinksQuery() = default; |
| 81 | 85 |
| 82 MediaRouterMojoImpl::MediaSinksQuery::~MediaSinksQuery() = default; | 86 MediaRouterMojoImpl::MediaSinksQuery::~MediaSinksQuery() = default; |
| 83 | 87 |
| 84 void MediaRouterMojoImpl::MediaRouterMediaRoutesObserver::OnRoutesUpdated( | 88 void MediaRouterMojoImpl::MediaRouterMediaRoutesObserver::OnRoutesUpdated( |
| 85 const std::vector<media_router::MediaRoute>& routes) { | 89 const std::vector<media_router::MediaRoute>& routes, |
| 90 const std::vector<media_router::MediaRoute::Id>& joinable_route_ids) { |
| 86 bool has_local_display_route = | 91 bool has_local_display_route = |
| 87 std::find_if(routes.begin(), routes.end(), | 92 std::find_if(routes.begin(), routes.end(), |
| 88 [](const media_router::MediaRoute& route) { | 93 [](const media_router::MediaRoute& route) { |
| 89 return route.is_local() && route.for_display(); | 94 return route.is_local() && route.for_display(); |
| 90 }) != routes.end(); | 95 }) != routes.end(); |
| 91 | 96 |
| 92 // |this| will be deleted in UpdateHasLocalDisplayRoute() if | 97 // |this| will be deleted in UpdateHasLocalDisplayRoute() if |
| 93 // |has_local_display_route| is false. Note that ObserverList supports | 98 // |has_local_display_route| is false. Note that ObserverList supports |
| 94 // removing an observer while iterating through it. | 99 // removing an observer while iterating through it. |
| 95 router_->UpdateHasLocalDisplayRoute(has_local_display_route); | 100 router_->UpdateHasLocalDisplayRoute(has_local_display_route); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 const Issue& issue_converted = issue.To<Issue>(); | 193 const Issue& issue_converted = issue.To<Issue>(); |
| 189 issue_manager_.AddIssue(issue_converted); | 194 issue_manager_.AddIssue(issue_converted); |
| 190 } | 195 } |
| 191 | 196 |
| 192 void MediaRouterMojoImpl::OnSinksReceived( | 197 void MediaRouterMojoImpl::OnSinksReceived( |
| 193 const mojo::String& media_source, | 198 const mojo::String& media_source, |
| 194 mojo::Array<interfaces::MediaSinkPtr> sinks) { | 199 mojo::Array<interfaces::MediaSinkPtr> sinks) { |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | 200 DCHECK(thread_checker_.CalledOnValidThread()); |
| 196 | 201 |
| 197 DVLOG_WITH_INSTANCE(1) << "OnSinksReceived"; | 202 DVLOG_WITH_INSTANCE(1) << "OnSinksReceived"; |
| 203 if (!HasSinksObservers(media_source)) { |
| 204 DVLOG_WITH_INSTANCE(1) |
| 205 << "Received sink list without any active observers: " << media_source; |
| 206 return; |
| 207 } |
| 208 |
| 198 std::vector<MediaSink> sinks_converted; | 209 std::vector<MediaSink> sinks_converted; |
| 199 sinks_converted.reserve(sinks.size()); | 210 sinks_converted.reserve(sinks.size()); |
| 200 | 211 |
| 201 for (size_t i = 0; i < sinks.size(); ++i) { | 212 for (size_t i = 0; i < sinks.size(); ++i) { |
| 202 sinks_converted.push_back(sinks[i].To<MediaSink>()); | 213 sinks_converted.push_back(sinks[i].To<MediaSink>()); |
| 203 } | 214 } |
| 204 | 215 |
| 205 auto it = sinks_queries_.find(media_source); | 216 auto sinks_query = sinks_queries_.get(media_source); |
| 206 if (it == sinks_queries_.end() || | 217 FOR_EACH_OBSERVER(MediaSinksObserver, sinks_query->observers, |
| 207 !(it->second->observers.might_have_observers())) { | 218 OnSinksReceived(sinks_converted)); |
| 208 DVLOG_WITH_INSTANCE(1) | |
| 209 << "Received sink list without any active observers: " << media_source; | |
| 210 } else { | |
| 211 FOR_EACH_OBSERVER(MediaSinksObserver, it->second->observers, | |
| 212 OnSinksReceived(sinks_converted)); | |
| 213 } | |
| 214 } | 219 } |
| 215 | 220 |
| 216 void MediaRouterMojoImpl::OnRoutesUpdated( | 221 void MediaRouterMojoImpl::OnRoutesUpdated( |
| 217 mojo::Array<interfaces::MediaRoutePtr> routes) { | 222 mojo::Array<interfaces::MediaRoutePtr> routes, |
| 223 const mojo::String& media_source, |
| 224 mojo::Array<mojo::String> joinable_route_ids) { |
| 218 DCHECK(thread_checker_.CalledOnValidThread()); | 225 DCHECK(thread_checker_.CalledOnValidThread()); |
| 219 | 226 |
| 220 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated"; | 227 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated"; |
| 228 auto it = routes_queries_.find(media_source); |
| 229 if (it == routes_queries_.end() || |
| 230 !(it->second->observers.might_have_observers())) { |
| 231 DVLOG_WITH_INSTANCE(1) |
| 232 << "Received route list without any active observers: " << media_source; |
| 233 return; |
| 234 } |
| 221 | 235 |
| 222 std::vector<MediaRoute> routes_converted; | 236 std::vector<MediaRoute> routes_converted; |
| 223 routes_converted.reserve(routes.size()); | 237 routes_converted.reserve(routes.size()); |
| 224 | 238 |
| 225 for (size_t i = 0; i < routes.size(); ++i) { | 239 for (size_t i = 0; i < routes.size(); ++i) |
| 226 routes_converted.push_back(routes[i].To<MediaRoute>()); | 240 routes_converted.push_back(routes[i].To<MediaRoute>()); |
| 227 } | |
| 228 | 241 |
| 229 FOR_EACH_OBSERVER(MediaRoutesObserver, routes_observers_, | 242 std::vector<MediaRoute::Id> joinable_routes_converted = |
| 230 OnRoutesUpdated(routes_converted)); | 243 joinable_route_ids.To<std::vector<std::string>>(); |
| 244 |
| 245 FOR_EACH_OBSERVER(MediaRoutesObserver, it->second->observers, |
| 246 OnRoutesUpdated(routes_converted, |
| 247 joinable_routes_converted)); |
| 231 } | 248 } |
| 232 | 249 |
| 233 void MediaRouterMojoImpl::RouteResponseReceived( | 250 void MediaRouterMojoImpl::RouteResponseReceived( |
| 234 const std::string& presentation_id, | 251 const std::string& presentation_id, |
| 235 const std::vector<MediaRouteResponseCallback>& callbacks, | 252 const std::vector<MediaRouteResponseCallback>& callbacks, |
| 236 interfaces::MediaRoutePtr media_route, | 253 interfaces::MediaRoutePtr media_route, |
| 237 const mojo::String& error_text) { | 254 const mojo::String& error_text) { |
| 238 scoped_ptr<MediaRoute> route; | 255 scoped_ptr<MediaRoute> route; |
| 239 std::string actual_presentation_id; | 256 std::string actual_presentation_id; |
| 240 std::string error; | 257 std::string error; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 } | 327 } |
| 311 | 328 |
| 312 SetWakeReason(MediaRouteProviderWakeReason::JOIN_ROUTE); | 329 SetWakeReason(MediaRouteProviderWakeReason::JOIN_ROUTE); |
| 313 int tab_id = SessionTabHelper::IdForTab(web_contents); | 330 int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 314 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, | 331 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, |
| 315 base::Unretained(this), source_id, presentation_id, | 332 base::Unretained(this), source_id, presentation_id, |
| 316 origin.is_empty() ? "" : origin.spec(), tab_id, | 333 origin.is_empty() ? "" : origin.spec(), tab_id, |
| 317 callbacks)); | 334 callbacks)); |
| 318 } | 335 } |
| 319 | 336 |
| 337 void MediaRouterMojoImpl::ConnectRouteByRouteId( |
| 338 const MediaSource::Id& source_id, |
| 339 const MediaRoute::Id& route_id, |
| 340 const GURL& origin, |
| 341 content::WebContents* web_contents, |
| 342 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 343 DCHECK(thread_checker_.CalledOnValidThread()); |
| 344 |
| 345 if (!origin.is_valid()) { |
| 346 DVLOG_WITH_INSTANCE(1) << "Invalid origin: " << origin; |
| 347 for (const MediaRouteResponseCallback& callback : callbacks) |
| 348 callback.Run(nullptr, "", "Invalid origin"); |
| 349 return; |
| 350 } |
| 351 |
| 352 SetWakeReason(MediaRouteProviderWakeReason::CONNECT_ROUTE_BY_ROUTE_ID); |
| 353 int tab_id = SessionTabHelper::IdForTab(web_contents); |
| 354 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoConnectRouteByRouteId, |
| 355 base::Unretained(this), source_id, route_id, |
| 356 origin.is_empty() ? "" : origin.spec(), tab_id, |
| 357 callbacks)); |
| 358 } |
| 359 |
| 320 void MediaRouterMojoImpl::TerminateRoute(const MediaRoute::Id& route_id) { | 360 void MediaRouterMojoImpl::TerminateRoute(const MediaRoute::Id& route_id) { |
| 321 DCHECK(thread_checker_.CalledOnValidThread()); | 361 DCHECK(thread_checker_.CalledOnValidThread()); |
| 322 | 362 |
| 323 SetWakeReason(MediaRouteProviderWakeReason::TERMINATE_ROUTE); | 363 SetWakeReason(MediaRouteProviderWakeReason::TERMINATE_ROUTE); |
| 324 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoTerminateRoute, | 364 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoTerminateRoute, |
| 325 base::Unretained(this), route_id)); | 365 base::Unretained(this), route_id)); |
| 326 } | 366 } |
| 327 | 367 |
| 328 void MediaRouterMojoImpl::DetachRoute(const MediaRoute::Id& route_id) { | 368 void MediaRouterMojoImpl::DetachRoute(const MediaRoute::Id& route_id) { |
| 329 DCHECK(thread_checker_.CalledOnValidThread()); | 369 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 base::Unretained(this), source_id)); | 466 base::Unretained(this), source_id)); |
| 427 } else { | 467 } else { |
| 428 sinks_queries_.erase(source_id); | 468 sinks_queries_.erase(source_id); |
| 429 } | 469 } |
| 430 } | 470 } |
| 431 } | 471 } |
| 432 | 472 |
| 433 void MediaRouterMojoImpl::RegisterMediaRoutesObserver( | 473 void MediaRouterMojoImpl::RegisterMediaRoutesObserver( |
| 434 MediaRoutesObserver* observer) { | 474 MediaRoutesObserver* observer) { |
| 435 DCHECK(thread_checker_.CalledOnValidThread()); | 475 DCHECK(thread_checker_.CalledOnValidThread()); |
| 436 DCHECK(!routes_observers_.HasObserver(observer)); | 476 const MediaSource::Id source_id = observer->source_id(); |
| 477 auto* routes_query = routes_queries_.get(source_id); |
| 478 if (!routes_query) { |
| 479 routes_query = new MediaRoutesQuery; |
| 480 routes_queries_.add(source_id, make_scoped_ptr(routes_query)); |
| 481 } else { |
| 482 DCHECK(!routes_query->observers.HasObserver(observer)); |
| 483 } |
| 437 | 484 |
| 485 routes_query->observers.AddObserver(observer); |
| 438 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES); | 486 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES); |
| 439 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaRoutes, | 487 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaRoutes, |
| 440 base::Unretained(this))); | 488 base::Unretained(this), source_id)); |
| 441 routes_observers_.AddObserver(observer); | |
| 442 } | 489 } |
| 443 | 490 |
| 444 void MediaRouterMojoImpl::UnregisterMediaRoutesObserver( | 491 void MediaRouterMojoImpl::UnregisterMediaRoutesObserver( |
| 445 MediaRoutesObserver* observer) { | 492 MediaRoutesObserver* observer) { |
| 446 if (!routes_observers_.HasObserver(observer)) | 493 const MediaSource::Id source_id = observer->source_id(); |
| 494 auto* routes_query = routes_queries_.get(source_id); |
| 495 if (!routes_query || !routes_query->observers.HasObserver(observer)) { |
| 447 return; | 496 return; |
| 497 } |
| 448 | 498 |
| 449 routes_observers_.RemoveObserver(observer); | 499 // If we are removing the final observer for the source, then stop |
| 450 if (!routes_observers_.might_have_observers()) { | 500 // observing routes for it. |
| 501 // might_have_observers() is reliable here on the assumption that this call |
| 502 // is not inside the ObserverList iteration. |
| 503 routes_query->observers.RemoveObserver(observer); |
| 504 if (!routes_query->observers.might_have_observers()) { |
| 451 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_ROUTES); | 505 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_ROUTES); |
| 452 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaRoutes, | 506 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaRoutes, |
| 453 base::Unretained(this))); | 507 base::Unretained(this), source_id)); |
| 454 } | 508 } |
| 455 } | 509 } |
| 456 | 510 |
| 457 void MediaRouterMojoImpl::RegisterIssuesObserver(IssuesObserver* observer) { | 511 void MediaRouterMojoImpl::RegisterIssuesObserver(IssuesObserver* observer) { |
| 458 DCHECK(thread_checker_.CalledOnValidThread()); | 512 DCHECK(thread_checker_.CalledOnValidThread()); |
| 459 issue_manager_.RegisterObserver(observer); | 513 issue_manager_.RegisterObserver(observer); |
| 460 } | 514 } |
| 461 | 515 |
| 462 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) { | 516 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) { |
| 463 DCHECK(thread_checker_.CalledOnValidThread()); | 517 DCHECK(thread_checker_.CalledOnValidThread()); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 547 int tab_id, | 601 int tab_id, |
| 548 const std::vector<MediaRouteResponseCallback>& callbacks) { | 602 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 549 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id | 603 DVLOG_WITH_INSTANCE(1) << "DoJoinRoute " << source_id |
| 550 << ", presentation ID: " << presentation_id; | 604 << ", presentation ID: " << presentation_id; |
| 551 media_route_provider_->JoinRoute( | 605 media_route_provider_->JoinRoute( |
| 552 source_id, presentation_id, origin, tab_id, | 606 source_id, presentation_id, origin, tab_id, |
| 553 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, | 607 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 554 base::Unretained(this), presentation_id, callbacks)); | 608 base::Unretained(this), presentation_id, callbacks)); |
| 555 } | 609 } |
| 556 | 610 |
| 611 void MediaRouterMojoImpl::DoConnectRouteByRouteId( |
| 612 const MediaSource::Id& source_id, |
| 613 const MediaRoute::Id& route_id, |
| 614 const std::string& origin, |
| 615 int tab_id, |
| 616 const std::vector<MediaRouteResponseCallback>& callbacks) { |
| 617 std::string presentation_id("mr_"); |
| 618 presentation_id += base::GenerateGUID(); |
| 619 DVLOG_WITH_INSTANCE(1) << "DoConnectRouteByRouteId " << source_id |
| 620 << ", route ID: " << route_id |
| 621 << ", presentation ID: " << presentation_id; |
| 622 media_route_provider_->ConnectRouteByRouteId( |
| 623 source_id, route_id, presentation_id, origin, tab_id, |
| 624 base::Bind(&MediaRouterMojoImpl::RouteResponseReceived, |
| 625 base::Unretained(this), presentation_id, callbacks)); |
| 626 } |
| 627 |
| 557 void MediaRouterMojoImpl::DoTerminateRoute(const MediaRoute::Id& route_id) { | 628 void MediaRouterMojoImpl::DoTerminateRoute(const MediaRoute::Id& route_id) { |
| 558 DVLOG_WITH_INSTANCE(1) << "DoTerminateRoute " << route_id; | 629 DVLOG_WITH_INSTANCE(1) << "DoTerminateRoute " << route_id; |
| 559 media_route_provider_->TerminateRoute(route_id); | 630 media_route_provider_->TerminateRoute(route_id); |
| 560 } | 631 } |
| 561 | 632 |
| 562 void MediaRouterMojoImpl::DoDetachRoute(const MediaRoute::Id& route_id) { | 633 void MediaRouterMojoImpl::DoDetachRoute(const MediaRoute::Id& route_id) { |
| 563 DVLOG_WITH_INSTANCE(1) << "DoDetachRoute " << route_id; | 634 DVLOG_WITH_INSTANCE(1) << "DoDetachRoute " << route_id; |
| 564 media_route_provider_->DetachRoute(route_id); | 635 media_route_provider_->DetachRoute(route_id); |
| 565 } | 636 } |
| 566 | 637 |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 692 } | 763 } |
| 693 | 764 |
| 694 void MediaRouterMojoImpl::DoStartObservingMediaSinks( | 765 void MediaRouterMojoImpl::DoStartObservingMediaSinks( |
| 695 const MediaSource::Id& source_id) { | 766 const MediaSource::Id& source_id) { |
| 696 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaSinks: " << source_id; | 767 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaSinks: " << source_id; |
| 697 // No need to call MRPM if there are no sinks available. | 768 // No need to call MRPM if there are no sinks available. |
| 698 if (availability_ == interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) | 769 if (availability_ == interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) |
| 699 return; | 770 return; |
| 700 | 771 |
| 701 // No need to call MRPM if all observers have been removed in the meantime. | 772 // No need to call MRPM if all observers have been removed in the meantime. |
| 702 auto* sinks_query = sinks_queries_.get(source_id); | 773 if (!HasSinksObservers(source_id)) |
| 703 if (!sinks_query || !sinks_query->observers.might_have_observers()) { | |
| 704 return; | 774 return; |
| 705 } | |
| 706 | 775 |
| 707 DVLOG_WITH_INSTANCE(1) << "MRPM.StartObservingMediaSinks: " << source_id; | 776 DVLOG_WITH_INSTANCE(1) << "MRPM.StartObservingMediaSinks: " << source_id; |
| 708 media_route_provider_->StartObservingMediaSinks(source_id); | 777 media_route_provider_->StartObservingMediaSinks(source_id); |
| 778 auto* sinks_query = sinks_queries_.get(source_id); |
| 709 sinks_query->is_active = true; | 779 sinks_query->is_active = true; |
| 710 } | 780 } |
| 711 | 781 |
| 712 void MediaRouterMojoImpl::DoStopObservingMediaSinks( | 782 void MediaRouterMojoImpl::DoStopObservingMediaSinks( |
| 713 const MediaSource::Id& source_id) { | 783 const MediaSource::Id& source_id) { |
| 714 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaSinks: " << source_id; | 784 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaSinks: " << source_id; |
| 715 | 785 |
| 716 auto* sinks_query = sinks_queries_.get(source_id); | 786 auto* sinks_query = sinks_queries_.get(source_id); |
| 717 // No need to call MRPM if observers have been added in the meantime, | 787 // No need to call MRPM if observers have been added in the meantime, |
| 718 // or StopObservingMediaSinks has already been called. | 788 // or StopObservingMediaSinks has already been called. |
| 719 if (!sinks_query || !sinks_query->is_active || | 789 if (!sinks_query || !sinks_query->is_active || |
| 720 sinks_query->observers.might_have_observers()) { | 790 sinks_query->observers.might_have_observers()) { |
| 721 return; | 791 return; |
| 722 } | 792 } |
| 723 | 793 |
| 724 DVLOG_WITH_INSTANCE(1) << "MRPM.StopObservingMediaSinks: " << source_id; | 794 DVLOG_WITH_INSTANCE(1) << "MRPM.StopObservingMediaSinks: " << source_id; |
| 725 media_route_provider_->StopObservingMediaSinks(source_id); | 795 media_route_provider_->StopObservingMediaSinks(source_id); |
| 726 sinks_queries_.erase(source_id); | 796 sinks_queries_.erase(source_id); |
| 727 } | 797 } |
| 728 | 798 |
| 729 void MediaRouterMojoImpl::DoStartObservingMediaRoutes() { | 799 void MediaRouterMojoImpl::DoStartObservingMediaRoutes( |
| 800 const MediaSource::Id& source_id) { |
| 730 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaRoutes"; | 801 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaRoutes"; |
| 731 media_route_provider_->StartObservingMediaRoutes(); | 802 |
| 803 // No need to call MRPM if all observers have been removed in the meantime. |
| 804 if (!HasRoutesObservers(source_id)) |
| 805 return; |
| 806 |
| 807 DVLOG_WITH_INSTANCE(1) << "MRPM.StartObservingMediaRoutes: " << source_id; |
| 808 media_route_provider_->StartObservingMediaRoutes(source_id); |
| 809 auto* routes_query = routes_queries_.get(source_id); |
| 810 routes_query->is_active = true; |
| 732 } | 811 } |
| 733 | 812 |
| 734 void MediaRouterMojoImpl::DoStopObservingMediaRoutes() { | 813 void MediaRouterMojoImpl::DoStopObservingMediaRoutes( |
| 814 const MediaSource::Id& source_id) { |
| 735 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaRoutes"; | 815 DVLOG_WITH_INSTANCE(1) << "DoStopObservingMediaRoutes"; |
| 736 media_route_provider_->StopObservingMediaRoutes(); | 816 |
| 817 // No need to call MRPM if observers have been added in the meantime, |
| 818 // or StopObservingMediaRoutes has already been called. |
| 819 auto* routes_query = routes_queries_.get(source_id); |
| 820 if (!routes_query || !routes_query->is_active || |
| 821 routes_query->observers.might_have_observers()) { |
| 822 return; |
| 823 } |
| 824 |
| 825 DVLOG_WITH_INSTANCE(1) << "MRPM.StopObservingMediaRoutes: " << source_id; |
| 826 media_route_provider_->StopObservingMediaRoutes(source_id); |
| 827 routes_queries_.erase(source_id); |
| 737 } | 828 } |
| 738 | 829 |
| 739 void MediaRouterMojoImpl::EnqueueTask(const base::Closure& closure) { | 830 void MediaRouterMojoImpl::EnqueueTask(const base::Closure& closure) { |
| 740 pending_requests_.push_back(closure); | 831 pending_requests_.push_back(closure); |
| 741 if (pending_requests_.size() > kMaxPendingRequests) { | 832 if (pending_requests_.size() > kMaxPendingRequests) { |
| 742 DLOG_WITH_INSTANCE(ERROR) << "Reached max queue size. Dropping oldest " | 833 DLOG_WITH_INSTANCE(ERROR) << "Reached max queue size. Dropping oldest " |
| 743 << "request."; | 834 << "request."; |
| 744 pending_requests_.pop_front(); | 835 pending_requests_.pop_front(); |
| 745 } | 836 } |
| 746 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length=" | 837 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length=" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 830 DCHECK(reason != MediaRouteProviderWakeReason::TOTAL_COUNT); | 921 DCHECK(reason != MediaRouteProviderWakeReason::TOTAL_COUNT); |
| 831 if (current_wake_reason_ == MediaRouteProviderWakeReason::TOTAL_COUNT) | 922 if (current_wake_reason_ == MediaRouteProviderWakeReason::TOTAL_COUNT) |
| 832 current_wake_reason_ = reason; | 923 current_wake_reason_ = reason; |
| 833 } | 924 } |
| 834 | 925 |
| 835 void MediaRouterMojoImpl::ClearWakeReason() { | 926 void MediaRouterMojoImpl::ClearWakeReason() { |
| 836 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); | 927 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); |
| 837 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; | 928 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; |
| 838 } | 929 } |
| 839 | 930 |
| 931 bool MediaRouterMojoImpl::HasRoutesObservers( |
| 932 const MediaSource::Id& source_id) const { |
| 933 auto* routes_query = routes_queries_.get(source_id); |
| 934 return routes_query && routes_query->observers.might_have_observers(); |
| 935 } |
| 936 |
| 937 bool MediaRouterMojoImpl::HasSinksObservers( |
| 938 const MediaSource::Id& source_id) const { |
| 939 auto* sinks_query = sinks_queries_.get(source_id); |
| 940 return sinks_query && sinks_query->observers.might_have_observers(); |
| 941 } |
| 942 |
| 840 } // namespace media_router | 943 } // namespace media_router |
| OLD | NEW |