Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: chrome/browser/media/router/mojo/media_router_mojo_impl.cc

Issue 2735493002: MediaRouter: Cache MediaRoutesObserver queries to prevent ext wake-ups (Closed)
Patch Set: Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 179 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
180 DVLOG_WITH_INSTANCE(1) << "OnSinksReceived"; 180 DVLOG_WITH_INSTANCE(1) << "OnSinksReceived";
181 auto it = sinks_queries_.find(media_source); 181 auto it = sinks_queries_.find(media_source);
182 if (it == sinks_queries_.end()) { 182 if (it == sinks_queries_.end()) {
183 DVLOG_WITH_INSTANCE(1) << "Received sink list without MediaSinksQuery."; 183 DVLOG_WITH_INSTANCE(1) << "Received sink list without MediaSinksQuery.";
184 return; 184 return;
185 } 185 }
186 186
187 auto* sinks_query = it->second.get(); 187 auto* sinks_query = it->second.get();
188 sinks_query->has_cached_result = true; 188 sinks_query->has_cached_result = true;
189 sinks_query->cached_sink_list = sinks;
189 sinks_query->origins = origins; 190 sinks_query->origins = origins;
190 sinks_query->cached_sink_list = sinks;
191 191
192 if (!sinks_query->observers.might_have_observers()) { 192 if (sinks_query->observers.might_have_observers()) {
193 DVLOG_WITH_INSTANCE(1)
194 << "Received sink list without any active observers: " << media_source;
195 } else {
196 for (auto& observer : sinks_query->observers) { 193 for (auto& observer : sinks_query->observers) {
197 observer.OnSinksUpdated(sinks_query->cached_sink_list, 194 observer.OnSinksUpdated(sinks_query->cached_sink_list,
198 sinks_query->origins); 195 sinks_query->origins);
199 } 196 }
197 } else {
198 DVLOG_WITH_INSTANCE(1)
199 << "Received sink list without any active observers: " << media_source;
200 } 200 }
201 } 201 }
202 202
203 void MediaRouterMojoImpl::OnRoutesUpdated( 203 void MediaRouterMojoImpl::OnRoutesUpdated(
204 const std::vector<MediaRoute>& routes, 204 const std::vector<MediaRoute>& routes,
205 const std::string& media_source, 205 const std::string& media_source,
206 const std::vector<std::string>& joinable_route_ids) { 206 const std::vector<std::string>& joinable_route_ids) {
207 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 207 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
208 208
209 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated"; 209 DVLOG_WITH_INSTANCE(1) << "OnRoutesUpdated";
210 auto it = routes_queries_.find(media_source); 210 auto it = routes_queries_.find(media_source);
211 if (it == routes_queries_.end() || 211 if (it == routes_queries_.end() ||
212 !(it->second->observers.might_have_observers())) { 212 !(it->second->observers.might_have_observers())) {
213 DVLOG_WITH_INSTANCE(1) 213 DVLOG_WITH_INSTANCE(1)
214 << "Received route list without any active observers: " << media_source; 214 << "Received route list without any active observers: " << media_source;
215 return; 215 return;
216 } 216 }
217 217
218 for (auto& observer : it->second->observers) 218 auto* routes_query = it->second.get();
219 observer.OnRoutesUpdated(routes, joinable_route_ids); 219 routes_query->has_cached_result = true;
220 routes_query->cached_route_list = routes;
221 routes_query->joinable_route_ids = joinable_route_ids;
222
223 if (routes_query->observers.might_have_observers()) {
224 for (auto& observer : routes_query->observers)
225 observer.OnRoutesUpdated(routes, joinable_route_ids);
226 } else {
227 DVLOG_WITH_INSTANCE(1)
228 << "Received routes update without any active observers: "
229 << media_source;
230 }
220 } 231 }
221 232
222 void MediaRouterMojoImpl::RouteResponseReceived( 233 void MediaRouterMojoImpl::RouteResponseReceived(
223 const std::string& presentation_id, 234 const std::string& presentation_id,
224 bool is_incognito, 235 bool is_incognito,
225 const std::vector<MediaRouteResponseCallback>& callbacks, 236 const std::vector<MediaRouteResponseCallback>& callbacks,
226 bool is_join, 237 bool is_join,
227 const base::Optional<MediaRoute>& media_route, 238 const base::Optional<MediaRoute>& media_route,
228 const base::Optional<std::string>& error_text, 239 const base::Optional<std::string>& error_text,
229 RouteRequestResult::ResultCode result_code) { 240 RouteRequestResult::ResultCode result_code) {
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
452 sinks_queries_.erase(source_id); 463 sinks_queries_.erase(source_id);
453 } 464 }
454 } 465 }
455 } 466 }
456 467
457 void MediaRouterMojoImpl::RegisterMediaRoutesObserver( 468 void MediaRouterMojoImpl::RegisterMediaRoutesObserver(
458 MediaRoutesObserver* observer) { 469 MediaRoutesObserver* observer) {
459 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 470 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
460 const MediaSource::Id source_id = observer->source_id(); 471 const MediaSource::Id source_id = observer->source_id();
461 auto& routes_query = routes_queries_[source_id]; 472 auto& routes_query = routes_queries_[source_id];
473 bool new_query = false;
Wez 2017/03/04 02:07:32 nit: is_new_query
miu 2017/03/04 02:24:39 Done.
462 if (!routes_query) { 474 if (!routes_query) {
475 new_query = true;
463 routes_query = base::MakeUnique<MediaRoutesQuery>(); 476 routes_query = base::MakeUnique<MediaRoutesQuery>();
464 } else { 477 } else {
465 DCHECK(!routes_query->observers.HasObserver(observer)); 478 DCHECK(!routes_query->observers.HasObserver(observer));
466 } 479 }
467 480
468 routes_query->observers.AddObserver(observer); 481 routes_query->observers.AddObserver(observer);
469 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES); 482 if (new_query) {
470 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaRoutes, 483 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES);
471 base::Unretained(this), source_id)); 484 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaRoutes,
485 base::Unretained(this), source_id));
486 // The MRPM will call MediaRouterMojoImpl::OnRoutesUpdated() soon, if there
487 // are any existing routes the new observer should be aware of.
488 } else if (routes_query->has_cached_result) {
489 // Return to the event loop before notifying of a cached route list because
490 // MediaRoutesObserver is calling this method from its constructor, and that
491 // must complete before invoking its virtual OnRoutesUpdated() method.
492 content::BrowserThread::PostTask(
493 content::BrowserThread::UI, FROM_HERE,
494 base::Bind(
495 &MediaRouterMojoImpl::NotifyOfExistingRoutesAfterRegistration,
496 weak_factory_.GetWeakPtr(), source_id, observer));
497 }
498 }
499
500 void MediaRouterMojoImpl::NotifyOfExistingRoutesAfterRegistration(
501 const MediaSource::Id& source_id,
502 MediaRoutesObserver* observer) const {
503 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
504
505 // Check that the route query still exists with a cached result, and that the
506 // observer is still registered. Otherwise, punt.
Wez 2017/03/04 02:07:32 nit: What does "punt" mean here? Perhaps rephrase
miu 2017/03/04 02:24:39 Done.
507 const auto it = routes_queries_.find(source_id);
508 if (it == routes_queries_.end() || !it->second->has_cached_result ||
509 !it->second->observers.HasObserver(observer)) {
510 return;
511 }
512
513 observer->OnRoutesUpdated(it->second->cached_route_list,
514 it->second->joinable_route_ids);
472 } 515 }
473 516
474 void MediaRouterMojoImpl::UnregisterMediaRoutesObserver( 517 void MediaRouterMojoImpl::UnregisterMediaRoutesObserver(
475 MediaRoutesObserver* observer) { 518 MediaRoutesObserver* observer) {
476 const MediaSource::Id source_id = observer->source_id(); 519 const MediaSource::Id source_id = observer->source_id();
477 auto it = routes_queries_.find(source_id); 520 auto it = routes_queries_.find(source_id);
478 if (it == routes_queries_.end() || 521 if (it == routes_queries_.end() ||
479 !it->second->observers.HasObserver(observer)) { 522 !it->second->observers.HasObserver(observer)) {
480 return; 523 return;
481 } 524 }
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 base::Unretained(this), source_id)); 972 base::Unretained(this), source_id));
930 } 973 }
931 974
932 void MediaRouterMojoImpl::DoUpdateMediaSinks( 975 void MediaRouterMojoImpl::DoUpdateMediaSinks(
933 const MediaSource::Id& source_id) { 976 const MediaSource::Id& source_id) {
934 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; 977 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id;
935 media_route_provider_->UpdateMediaSinks(source_id); 978 media_route_provider_->UpdateMediaSinks(source_id);
936 } 979 }
937 980
938 } // namespace media_router 981 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698