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