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

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

Issue 2728543009: [Media Router] Custom Controls 2 - add MediaRouter::GetRouteController() (Closed)
Patch Set: Combine CreateMRController and SetMRStatusObserver Created 3 years, 8 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
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
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 27 matching lines...) Expand all
270 const MediaSink::Id& sink_id, 272 const MediaSink::Id& sink_id,
271 const url::Origin& origin, 273 const url::Origin& origin,
272 content::WebContents* web_contents, 274 content::WebContents* web_contents,
273 const std::vector<MediaRouteResponseCallback>& callbacks, 275 const std::vector<MediaRouteResponseCallback>& callbacks,
274 base::TimeDelta timeout, 276 base::TimeDelta timeout,
275 bool incognito) { 277 bool incognito) {
276 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 278 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
277 279
278 SetWakeReason(MediaRouteProviderWakeReason::CREATE_ROUTE); 280 SetWakeReason(MediaRouteProviderWakeReason::CREATE_ROUTE);
279 int tab_id = SessionTabHelper::IdForTab(web_contents); 281 int tab_id = SessionTabHelper::IdForTab(web_contents);
280 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoCreateRoute, 282 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoCreateRoute,
281 base::Unretained(this), source_id, sink_id, origin, 283 base::Unretained(this), source_id, sink_id, origin,
282 tab_id, callbacks, timeout, incognito)); 284 tab_id, callbacks, timeout, incognito));
283 } 285 }
284 286
285 void MediaRouterMojoImpl::JoinRoute( 287 void MediaRouterMojoImpl::JoinRoute(
286 const MediaSource::Id& source_id, 288 const MediaSource::Id& source_id,
287 const std::string& presentation_id, 289 const std::string& presentation_id,
288 const url::Origin& origin, 290 const url::Origin& origin,
289 content::WebContents* web_contents, 291 content::WebContents* web_contents,
290 const std::vector<MediaRouteResponseCallback>& callbacks, 292 const std::vector<MediaRouteResponseCallback>& callbacks,
291 base::TimeDelta timeout, 293 base::TimeDelta timeout,
292 bool incognito) { 294 bool incognito) {
293 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 295 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
294 296
295 if (!HasJoinableRoute()) { 297 if (!HasJoinableRoute()) {
296 DVLOG_WITH_INSTANCE(1) << "No joinable routes"; 298 DVLOG_WITH_INSTANCE(1) << "No joinable routes";
297 std::unique_ptr<RouteRequestResult> result = RouteRequestResult::FromError( 299 std::unique_ptr<RouteRequestResult> result = RouteRequestResult::FromError(
298 "Route not found", RouteRequestResult::ROUTE_NOT_FOUND); 300 "Route not found", RouteRequestResult::ROUTE_NOT_FOUND);
299 MediaRouterMojoMetrics::RecordJoinRouteResultCode(result->result_code()); 301 MediaRouterMojoMetrics::RecordJoinRouteResultCode(result->result_code());
300 RunRouteRequestCallbacks(std::move(result), callbacks); 302 RunRouteRequestCallbacks(std::move(result), callbacks);
301 return; 303 return;
302 } 304 }
303 305
304 SetWakeReason(MediaRouteProviderWakeReason::JOIN_ROUTE); 306 SetWakeReason(MediaRouteProviderWakeReason::JOIN_ROUTE);
305 int tab_id = SessionTabHelper::IdForTab(web_contents); 307 int tab_id = SessionTabHelper::IdForTab(web_contents);
306 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoJoinRoute, 308 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoJoinRoute,
307 base::Unretained(this), source_id, presentation_id, 309 base::Unretained(this), source_id, presentation_id,
308 origin, tab_id, callbacks, timeout, incognito)); 310 origin, tab_id, callbacks, timeout, incognito));
309 } 311 }
310 312
311 void MediaRouterMojoImpl::ConnectRouteByRouteId( 313 void MediaRouterMojoImpl::ConnectRouteByRouteId(
312 const MediaSource::Id& source_id, 314 const MediaSource::Id& source_id,
313 const MediaRoute::Id& route_id, 315 const MediaRoute::Id& route_id,
314 const url::Origin& origin, 316 const url::Origin& origin,
315 content::WebContents* web_contents, 317 content::WebContents* web_contents,
316 const std::vector<MediaRouteResponseCallback>& callbacks, 318 const std::vector<MediaRouteResponseCallback>& callbacks,
317 base::TimeDelta timeout, 319 base::TimeDelta timeout,
318 bool incognito) { 320 bool incognito) {
319 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 321 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
320 322
321 SetWakeReason(MediaRouteProviderWakeReason::CONNECT_ROUTE_BY_ROUTE_ID); 323 SetWakeReason(MediaRouteProviderWakeReason::CONNECT_ROUTE_BY_ROUTE_ID);
322 int tab_id = SessionTabHelper::IdForTab(web_contents); 324 int tab_id = SessionTabHelper::IdForTab(web_contents);
323 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoConnectRouteByRouteId, 325 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoConnectRouteByRouteId,
324 base::Unretained(this), source_id, route_id, origin, 326 base::Unretained(this), source_id, route_id, origin,
325 tab_id, callbacks, timeout, incognito)); 327 tab_id, callbacks, timeout, incognito));
326 } 328 }
327 329
328 void MediaRouterMojoImpl::TerminateRoute(const MediaRoute::Id& route_id) { 330 void MediaRouterMojoImpl::TerminateRoute(const MediaRoute::Id& route_id) {
329 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 331 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
330 DVLOG(2) << "TerminateRoute " << route_id; 332 DVLOG(2) << "TerminateRoute " << route_id;
331 SetWakeReason(MediaRouteProviderWakeReason::TERMINATE_ROUTE); 333 SetWakeReason(MediaRouteProviderWakeReason::TERMINATE_ROUTE);
332 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoTerminateRoute, 334 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoTerminateRoute,
333 base::Unretained(this), route_id)); 335 base::Unretained(this), route_id));
334 } 336 }
335 337
336 void MediaRouterMojoImpl::DetachRoute(const MediaRoute::Id& route_id) { 338 void MediaRouterMojoImpl::DetachRoute(const MediaRoute::Id& route_id) {
337 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 339 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
338 340
339 SetWakeReason(MediaRouteProviderWakeReason::DETACH_ROUTE); 341 SetWakeReason(MediaRouteProviderWakeReason::DETACH_ROUTE);
340 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoDetachRoute, 342 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoDetachRoute,
341 base::Unretained(this), route_id)); 343 base::Unretained(this), route_id));
342 } 344 }
343 345
344 void MediaRouterMojoImpl::SendRouteMessage( 346 void MediaRouterMojoImpl::SendRouteMessage(
345 const MediaRoute::Id& route_id, 347 const MediaRoute::Id& route_id,
346 const std::string& message, 348 const std::string& message,
347 const SendRouteMessageCallback& callback) { 349 const SendRouteMessageCallback& callback) {
348 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 350 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
349 351
350 SetWakeReason(MediaRouteProviderWakeReason::SEND_SESSION_MESSAGE); 352 SetWakeReason(MediaRouteProviderWakeReason::SEND_SESSION_MESSAGE);
351 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSendSessionMessage, 353 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoSendSessionMessage,
352 base::Unretained(this), route_id, message, callback)); 354 base::Unretained(this), route_id, message,
355 callback));
353 } 356 }
354 357
355 void MediaRouterMojoImpl::SendRouteBinaryMessage( 358 void MediaRouterMojoImpl::SendRouteBinaryMessage(
356 const MediaRoute::Id& route_id, 359 const MediaRoute::Id& route_id,
357 std::unique_ptr<std::vector<uint8_t>> data, 360 std::unique_ptr<std::vector<uint8_t>> data,
358 const SendRouteMessageCallback& callback) { 361 const SendRouteMessageCallback& callback) {
359 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 362 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
360 363
361 SetWakeReason(MediaRouteProviderWakeReason::SEND_SESSION_BINARY_MESSAGE); 364 SetWakeReason(MediaRouteProviderWakeReason::SEND_SESSION_BINARY_MESSAGE);
362 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSendSessionBinaryMessage, 365 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoSendSessionBinaryMessage,
363 base::Unretained(this), route_id, 366 base::Unretained(this), route_id,
364 base::Passed(std::move(data)), callback)); 367 base::Passed(std::move(data)), callback));
365 } 368 }
366 369
367 void MediaRouterMojoImpl::AddIssue(const IssueInfo& issue_info) { 370 void MediaRouterMojoImpl::AddIssue(const IssueInfo& issue_info) {
368 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 371 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
369 issue_manager_.AddIssue(issue_info); 372 issue_manager_.AddIssue(issue_info);
370 } 373 }
371 374
372 void MediaRouterMojoImpl::ClearIssue(const Issue::Id& issue_id) { 375 void MediaRouterMojoImpl::ClearIssue(const Issue::Id& issue_id) {
373 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 376 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
374 issue_manager_.ClearIssue(issue_id); 377 issue_manager_.ClearIssue(issue_id);
(...skipping 11 matching lines...) Expand all
386 389
387 void MediaRouterMojoImpl::SearchSinks( 390 void MediaRouterMojoImpl::SearchSinks(
388 const MediaSink::Id& sink_id, 391 const MediaSink::Id& sink_id,
389 const MediaSource::Id& source_id, 392 const MediaSource::Id& source_id,
390 const std::string& search_input, 393 const std::string& search_input,
391 const std::string& domain, 394 const std::string& domain,
392 const MediaSinkSearchResponseCallback& sink_callback) { 395 const MediaSinkSearchResponseCallback& sink_callback) {
393 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 396 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
394 397
395 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS); 398 SetWakeReason(MediaRouteProviderWakeReason::SEARCH_SINKS);
396 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoSearchSinks, 399 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoSearchSinks,
397 base::Unretained(this), sink_id, source_id, 400 base::Unretained(this), sink_id, source_id,
398 search_input, domain, sink_callback)); 401 search_input, domain, sink_callback));
402 }
403
404 scoped_refptr<MediaRouteController> MediaRouterMojoImpl::GetRouteController(
405 const MediaRoute::Id& route_id) {
406 if (!IsRouteKnown(route_id))
407 return nullptr;
408
409 auto it = route_controllers_.find(route_id);
410 if (it != route_controllers_.end())
411 return scoped_refptr<MediaRouteController>(it->second);
412
413 mojom::MediaControllerPtr mojo_media_controller;
414 mojom::MediaControllerRequest mojo_media_controller_request =
415 mojo::MakeRequest(&mojo_media_controller);
416 scoped_refptr<MediaRouteController> route_controller =
417 new MediaRouteController(route_id, std::move(mojo_media_controller),
418 this);
419
420 SetWakeReason(MediaRouteProviderWakeReason::CREATE_MEDIA_ROUTE_CONTROLLER);
421 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoCreateMediaRouteController,
422 base::Unretained(this), route_id,
423 std::move(mojo_media_controller_request),
424 route_controller->BindObserverPtr()));
425 route_controllers_.emplace(route_id, route_controller.get());
426 return route_controller;
399 } 427 }
400 428
401 void MediaRouterMojoImpl::ProvideSinks( 429 void MediaRouterMojoImpl::ProvideSinks(
402 const std::string& provider_name, 430 const std::string& provider_name,
403 const std::vector<MediaSinkInternal>& sinks) { 431 const std::vector<MediaSinkInternal>& sinks) {
404 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 432 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
405 433
406 SetWakeReason(MediaRouteProviderWakeReason::PROVIDE_SINKS); 434 SetWakeReason(MediaRouteProviderWakeReason::PROVIDE_SINKS);
407 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoProvideSinks, 435 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoProvideSinks,
408 base::Unretained(this), provider_name, sinks)); 436 base::Unretained(this), provider_name, sinks));
(...skipping 18 matching lines...) Expand all
427 // If sink availability is UNAVAILABLE, then there is no need to call MRPM. 455 // If sink availability is UNAVAILABLE, then there is no need to call MRPM.
428 // |observer| can be immediately notified with an empty list. 456 // |observer| can be immediately notified with an empty list.
429 sinks_query->observers.AddObserver(observer); 457 sinks_query->observers.AddObserver(observer);
430 if (availability_ == mojom::MediaRouter::SinkAvailability::UNAVAILABLE) { 458 if (availability_ == mojom::MediaRouter::SinkAvailability::UNAVAILABLE) {
431 observer->OnSinksUpdated(std::vector<MediaSink>(), 459 observer->OnSinksUpdated(std::vector<MediaSink>(),
432 std::vector<url::Origin>()); 460 std::vector<url::Origin>());
433 } else { 461 } else {
434 // Need to call MRPM to start observing sinks if the query is new. 462 // Need to call MRPM to start observing sinks if the query is new.
435 if (is_new_query) { 463 if (is_new_query) {
436 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_SINKS); 464 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_SINKS);
437 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, 465 RunOrDefer(
438 base::Unretained(this), source_id)); 466 base::BindOnce(&MediaRouterMojoImpl::DoStartObservingMediaSinks,
467 base::Unretained(this), source_id));
439 } else if (sinks_query->cached_sink_list) { 468 } else if (sinks_query->cached_sink_list) {
440 observer->OnSinksUpdated(*sinks_query->cached_sink_list, 469 observer->OnSinksUpdated(*sinks_query->cached_sink_list,
441 sinks_query->origins); 470 sinks_query->origins);
442 } 471 }
443 } 472 }
444 return true; 473 return true;
445 } 474 }
446 475
447 void MediaRouterMojoImpl::UnregisterMediaSinksObserver( 476 void MediaRouterMojoImpl::UnregisterMediaSinksObserver(
448 MediaSinksObserver* observer) { 477 MediaSinksObserver* observer) {
(...skipping 13 matching lines...) Expand all
462 it->second->observers.RemoveObserver(observer); 491 it->second->observers.RemoveObserver(observer);
463 if (!it->second->observers.might_have_observers()) { 492 if (!it->second->observers.might_have_observers()) {
464 // Only ask MRPM to stop observing media sinks if the availability is not 493 // Only ask MRPM to stop observing media sinks if the availability is not
465 // UNAVAILABLE. 494 // UNAVAILABLE.
466 // Otherwise, the MRPM would have discarded the queries already. 495 // Otherwise, the MRPM would have discarded the queries already.
467 if (availability_ != 496 if (availability_ !=
468 mojom::MediaRouter::SinkAvailability::UNAVAILABLE) { 497 mojom::MediaRouter::SinkAvailability::UNAVAILABLE) {
469 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_SINKS); 498 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_SINKS);
470 // The |sinks_queries_| entry will be removed in the immediate or deferred 499 // The |sinks_queries_| entry will be removed in the immediate or deferred
471 // |DoStopObservingMediaSinks| call. 500 // |DoStopObservingMediaSinks| call.
472 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaSinks, 501 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoStopObservingMediaSinks,
473 base::Unretained(this), source_id)); 502 base::Unretained(this), source_id));
474 } else { 503 } else {
475 sinks_queries_.erase(source_id); 504 sinks_queries_.erase(source_id);
476 } 505 }
477 } 506 }
478 } 507 }
479 508
480 void MediaRouterMojoImpl::RegisterMediaRoutesObserver( 509 void MediaRouterMojoImpl::RegisterMediaRoutesObserver(
481 MediaRoutesObserver* observer) { 510 MediaRoutesObserver* observer) {
482 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 511 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
483 const MediaSource::Id source_id = observer->source_id(); 512 const MediaSource::Id source_id = observer->source_id();
484 auto& routes_query = routes_queries_[source_id]; 513 auto& routes_query = routes_queries_[source_id];
485 bool is_new_query = false; 514 bool is_new_query = false;
486 if (!routes_query) { 515 if (!routes_query) {
487 is_new_query = true; 516 is_new_query = true;
488 routes_query = base::MakeUnique<MediaRoutesQuery>(); 517 routes_query = base::MakeUnique<MediaRoutesQuery>();
489 } else { 518 } else {
490 DCHECK(!routes_query->observers.HasObserver(observer)); 519 DCHECK(!routes_query->observers.HasObserver(observer));
491 } 520 }
492 521
493 routes_query->observers.AddObserver(observer); 522 routes_query->observers.AddObserver(observer);
494 if (is_new_query) { 523 if (is_new_query) {
495 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES); 524 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_ROUTES);
496 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaRoutes, 525 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoStartObservingMediaRoutes,
497 base::Unretained(this), source_id)); 526 base::Unretained(this), source_id));
498 // The MRPM will call MediaRouterMojoImpl::OnRoutesUpdated() soon, if there 527 // The MRPM will call MediaRouterMojoImpl::OnRoutesUpdated() soon, if there
499 // are any existing routes the new observer should be aware of. 528 // are any existing routes the new observer should be aware of.
500 } else if (routes_query->cached_route_list) { 529 } else if (routes_query->cached_route_list) {
501 // Return to the event loop before notifying of a cached route list because 530 // Return to the event loop before notifying of a cached route list because
502 // MediaRoutesObserver is calling this method from its constructor, and that 531 // MediaRoutesObserver is calling this method from its constructor, and that
503 // must complete before invoking its virtual OnRoutesUpdated() method. 532 // must complete before invoking its virtual OnRoutesUpdated() method.
504 content::BrowserThread::PostTask( 533 content::BrowserThread::PostTask(
505 content::BrowserThread::UI, FROM_HERE, 534 content::BrowserThread::UI, FROM_HERE,
506 base::Bind(&MediaRouterMojoImpl::NotifyOfExistingRoutesIfRegistered, 535 base::Bind(&MediaRouterMojoImpl::NotifyOfExistingRoutesIfRegistered,
507 weak_factory_.GetWeakPtr(), source_id, observer)); 536 weak_factory_.GetWeakPtr(), source_id, observer));
(...skipping 27 matching lines...) Expand all
535 return; 564 return;
536 } 565 }
537 566
538 // If we are removing the final observer for the source, then stop 567 // If we are removing the final observer for the source, then stop
539 // observing routes for it. 568 // observing routes for it.
540 // might_have_observers() is reliable here on the assumption that this call 569 // might_have_observers() is reliable here on the assumption that this call
541 // is not inside the ObserverList iteration. 570 // is not inside the ObserverList iteration.
542 it->second->observers.RemoveObserver(observer); 571 it->second->observers.RemoveObserver(observer);
543 if (!it->second->observers.might_have_observers()) { 572 if (!it->second->observers.might_have_observers()) {
544 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_ROUTES); 573 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_ROUTES);
545 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaRoutes, 574 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoStopObservingMediaRoutes,
546 base::Unretained(this), source_id)); 575 base::Unretained(this), source_id));
547 } 576 }
548 } 577 }
549 578
550 void MediaRouterMojoImpl::RegisterIssuesObserver(IssuesObserver* observer) { 579 void MediaRouterMojoImpl::RegisterIssuesObserver(IssuesObserver* observer) {
551 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 580 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
552 issue_manager_.RegisterObserver(observer); 581 issue_manager_.RegisterObserver(observer);
553 } 582 }
554 583
555 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) { 584 void MediaRouterMojoImpl::UnregisterIssuesObserver(IssuesObserver* observer) {
556 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 585 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
589 const MediaRoute::Id& route_id = observer->route_id(); 618 const MediaRoute::Id& route_id = observer->route_id();
590 auto it = message_observers_.find(route_id); 619 auto it = message_observers_.find(route_id);
591 if (it == message_observers_.end() || !it->second->HasObserver(observer)) 620 if (it == message_observers_.end() || !it->second->HasObserver(observer))
592 return; 621 return;
593 622
594 it->second->RemoveObserver(observer); 623 it->second->RemoveObserver(observer);
595 if (!it->second->might_have_observers()) { 624 if (!it->second->might_have_observers()) {
596 message_observers_.erase(route_id); 625 message_observers_.erase(route_id);
597 SetWakeReason( 626 SetWakeReason(
598 MediaRouteProviderWakeReason::STOP_LISTENING_FOR_ROUTE_MESSAGES); 627 MediaRouteProviderWakeReason::STOP_LISTENING_FOR_ROUTE_MESSAGES);
599 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopListeningForRouteMessages, 628 RunOrDefer(
600 base::Unretained(this), route_id)); 629 base::BindOnce(&MediaRouterMojoImpl::DoStopListeningForRouteMessages,
630 base::Unretained(this), route_id));
601 } 631 }
602 } 632 }
603 633
634 void MediaRouterMojoImpl::DetachRouteController(
635 const MediaRoute::Id& route_id,
636 MediaRouteController* controller) {
637 auto it = route_controllers_.find(route_id);
638 if (it != route_controllers_.end() && it->second == controller)
639 route_controllers_.erase(it);
640 }
641
604 void MediaRouterMojoImpl::DoCreateRoute( 642 void MediaRouterMojoImpl::DoCreateRoute(
605 const MediaSource::Id& source_id, 643 const MediaSource::Id& source_id,
606 const MediaSink::Id& sink_id, 644 const MediaSink::Id& sink_id,
607 const url::Origin& origin, 645 const url::Origin& origin,
608 int tab_id, 646 int tab_id,
609 const std::vector<MediaRouteResponseCallback>& callbacks, 647 const std::vector<MediaRouteResponseCallback>& callbacks,
610 base::TimeDelta timeout, 648 base::TimeDelta timeout,
611 bool incognito) { 649 bool incognito) {
612 std::string presentation_id = MediaRouterBase::CreatePresentationId(); 650 std::string presentation_id = MediaRouterBase::CreatePresentationId();
613 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id 651 DVLOG_WITH_INSTANCE(1) << "DoCreateRoute " << source_id << "=>" << sink_id
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
712 sink_id, source_id, std::move(sink_search_criteria), sink_callback); 750 sink_id, source_id, std::move(sink_search_criteria), sink_callback);
713 } 751 }
714 752
715 void MediaRouterMojoImpl::DoProvideSinks( 753 void MediaRouterMojoImpl::DoProvideSinks(
716 const std::string& provider_name, 754 const std::string& provider_name,
717 const std::vector<MediaSinkInternal>& sinks) { 755 const std::vector<MediaSinkInternal>& sinks) {
718 DVLOG_WITH_INSTANCE(1) << "DoProvideSinks"; 756 DVLOG_WITH_INSTANCE(1) << "DoProvideSinks";
719 media_route_provider_->ProvideSinks(provider_name, sinks); 757 media_route_provider_->ProvideSinks(provider_name, sinks);
720 } 758 }
721 759
760 void MediaRouterMojoImpl::DoCreateMediaRouteController(
761 const MediaRoute::Id& route_id,
762 mojom::MediaControllerRequest mojo_media_controller_request,
763 mojom::MediaStatusObserverPtr mojo_observer) {
764 DVLOG_WITH_INSTANCE(1) << "DoCreateMediaRouteController";
765 if (!mojo_media_controller_request.is_pending() || !mojo_observer.is_bound())
766 return;
767
768 media_route_provider_->CreateMediaRouteController(
769 route_id, std::move(mojo_media_controller_request),
770 std::move(mojo_observer),
771 base::Bind(&MediaRouterMojoImpl::OnMediaControllerCreated,
772 base::Unretained(this), route_id));
773 }
774
722 void MediaRouterMojoImpl::OnRouteMessagesReceived( 775 void MediaRouterMojoImpl::OnRouteMessagesReceived(
723 const std::string& route_id, 776 const std::string& route_id,
724 const std::vector<RouteMessage>& messages) { 777 const std::vector<RouteMessage>& messages) {
725 DVLOG_WITH_INSTANCE(1) << "OnRouteMessagesReceived"; 778 DVLOG_WITH_INSTANCE(1) << "OnRouteMessagesReceived";
726 779
727 if (messages.empty()) 780 if (messages.empty())
728 return; 781 return;
729 782
730 auto it = message_observers_.find(route_id); 783 auto it = message_observers_.find(route_id);
731 if (it == message_observers_.end()) { 784 if (it == message_observers_.end()) {
(...skipping 14 matching lines...) Expand all
746 // Sinks are no longer available. MRPM has already removed all sink queries. 799 // Sinks are no longer available. MRPM has already removed all sink queries.
747 for (auto& source_and_query : sinks_queries_) { 800 for (auto& source_and_query : sinks_queries_) {
748 const auto& query = source_and_query.second; 801 const auto& query = source_and_query.second;
749 query->is_active = false; 802 query->is_active = false;
750 query->cached_sink_list = base::nullopt; 803 query->cached_sink_list = base::nullopt;
751 query->origins.clear(); 804 query->origins.clear();
752 } 805 }
753 } else { 806 } else {
754 // Sinks are now available. Tell MRPM to start all sink queries again. 807 // Sinks are now available. Tell MRPM to start all sink queries again.
755 for (const auto& source_and_query : sinks_queries_) { 808 for (const auto& source_and_query : sinks_queries_) {
756 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, 809 RunOrDefer(
757 base::Unretained(this), source_and_query.first)); 810 base::BindOnce(&MediaRouterMojoImpl::DoStartObservingMediaSinks,
811 base::Unretained(this), source_and_query.first));
758 } 812 }
759 } 813 }
760 } 814 }
761 815
762 void MediaRouterMojoImpl::OnPresentationConnectionStateChanged( 816 void MediaRouterMojoImpl::OnPresentationConnectionStateChanged(
763 const std::string& route_id, 817 const std::string& route_id,
764 content::PresentationConnectionState state) { 818 content::PresentationConnectionState state) {
765 NotifyPresentationConnectionStateChange(route_id, state); 819 NotifyPresentationConnectionStateChange(route_id, state);
766 } 820 }
767 821
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 if (it == routes_queries_.end() || !it->second->is_active || 897 if (it == routes_queries_.end() || !it->second->is_active ||
844 it->second->observers.might_have_observers()) { 898 it->second->observers.might_have_observers()) {
845 return; 899 return;
846 } 900 }
847 901
848 DVLOG_WITH_INSTANCE(1) << "MRPM.StopObservingMediaRoutes: " << source_id; 902 DVLOG_WITH_INSTANCE(1) << "MRPM.StopObservingMediaRoutes: " << source_id;
849 media_route_provider_->StopObservingMediaRoutes(source_id); 903 media_route_provider_->StopObservingMediaRoutes(source_id);
850 routes_queries_.erase(source_id); 904 routes_queries_.erase(source_id);
851 } 905 }
852 906
853 void MediaRouterMojoImpl::EnqueueTask(const base::Closure& closure) { 907 void MediaRouterMojoImpl::EnqueueTask(base::OnceClosure closure) {
854 pending_requests_.push_back(closure); 908 pending_requests_.push_back(std::move(closure));
855 if (pending_requests_.size() > kMaxPendingRequests) { 909 if (pending_requests_.size() > kMaxPendingRequests) {
856 DLOG_WITH_INSTANCE(ERROR) << "Reached max queue size. Dropping oldest " 910 DLOG_WITH_INSTANCE(ERROR) << "Reached max queue size. Dropping oldest "
857 << "request."; 911 << "request.";
858 pending_requests_.pop_front(); 912 pending_requests_.pop_front();
859 } 913 }
860 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length=" 914 DVLOG_WITH_INSTANCE(2) << "EnqueueTask (queue-length="
861 << pending_requests_.size() << ")"; 915 << pending_requests_.size() << ")";
862 } 916 }
863 917
864 void MediaRouterMojoImpl::RunOrDefer(const base::Closure& request) { 918 void MediaRouterMojoImpl::RunOrDefer(base::OnceClosure request) {
865 DCHECK(event_page_tracker_); 919 DCHECK(event_page_tracker_);
866 920
867 if (media_route_provider_extension_id_.empty()) { 921 if (media_route_provider_extension_id_.empty()) {
868 DVLOG_WITH_INSTANCE(1) << "Extension ID not known yet."; 922 DVLOG_WITH_INSTANCE(1) << "Extension ID not known yet.";
869 EnqueueTask(request); 923 EnqueueTask(std::move(request));
870 } else if (event_page_tracker_->IsEventPageSuspended( 924 } else if (event_page_tracker_->IsEventPageSuspended(
871 media_route_provider_extension_id_)) { 925 media_route_provider_extension_id_)) {
872 DVLOG_WITH_INSTANCE(1) << "Waking event page."; 926 DVLOG_WITH_INSTANCE(1) << "Waking event page.";
873 EnqueueTask(request); 927 EnqueueTask(std::move(request));
874 AttemptWakeEventPage(); 928 AttemptWakeEventPage();
875 media_route_provider_.reset(); 929 media_route_provider_.reset();
876 } else if (!media_route_provider_) { 930 } else if (!media_route_provider_) {
877 DVLOG_WITH_INSTANCE(1) << "Extension is awake, awaiting ProvideMediaRouter " 931 DVLOG_WITH_INSTANCE(1) << "Extension is awake, awaiting ProvideMediaRouter "
878 " to be called."; 932 " to be called.";
879 EnqueueTask(request); 933 EnqueueTask(std::move(request));
880 } else { 934 } else {
881 request.Run(); 935 std::move(request).Run();
882 } 936 }
883 } 937 }
884 938
885 void MediaRouterMojoImpl::AttemptWakeEventPage() { 939 void MediaRouterMojoImpl::AttemptWakeEventPage() {
886 ++wakeup_attempt_count_; 940 ++wakeup_attempt_count_;
887 if (wakeup_attempt_count_ > kMaxWakeupAttemptCount) { 941 if (wakeup_attempt_count_ > kMaxWakeupAttemptCount) {
888 DLOG_WITH_INSTANCE(ERROR) << "Attempted too many times to wake up event " 942 DLOG_WITH_INSTANCE(ERROR) << "Attempted too many times to wake up event "
889 << "page."; 943 << "page.";
890 DrainPendingRequests(); 944 DrainPendingRequests();
891 wakeup_attempt_count_ = 0; 945 wakeup_attempt_count_ = 0;
(...skipping 15 matching lines...) Expand all
907 DLOG_WITH_INSTANCE(ERROR) << "Failed to schedule a wakeup for event page."; 961 DLOG_WITH_INSTANCE(ERROR) << "Failed to schedule a wakeup for event page.";
908 } 962 }
909 } 963 }
910 964
911 void MediaRouterMojoImpl::ExecutePendingRequests() { 965 void MediaRouterMojoImpl::ExecutePendingRequests() {
912 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 966 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
913 DCHECK(media_route_provider_); 967 DCHECK(media_route_provider_);
914 DCHECK(event_page_tracker_); 968 DCHECK(event_page_tracker_);
915 DCHECK(!media_route_provider_extension_id_.empty()); 969 DCHECK(!media_route_provider_extension_id_.empty());
916 970
917 for (const auto& next_request : pending_requests_) 971 for (auto& next_request : pending_requests_)
918 next_request.Run(); 972 std::move(next_request).Run();
919 973
920 pending_requests_.clear(); 974 pending_requests_.clear();
921 } 975 }
922 976
923 void MediaRouterMojoImpl::EventPageWakeComplete(bool success) { 977 void MediaRouterMojoImpl::EventPageWakeComplete(bool success) {
924 if (success) { 978 if (success) {
925 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeReason( 979 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeReason(
926 current_wake_reason_); 980 current_wake_reason_);
927 ClearWakeReason(); 981 ClearWakeReason();
928 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeup( 982 MediaRouterMojoMetrics::RecordMediaRouteProviderWakeup(
(...skipping 27 matching lines...) Expand all
956 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); 1010 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT);
957 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; 1011 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT;
958 } 1012 }
959 1013
960 #if defined(OS_WIN) 1014 #if defined(OS_WIN)
961 void MediaRouterMojoImpl::EnsureMdnsDiscoveryEnabled() { 1015 void MediaRouterMojoImpl::EnsureMdnsDiscoveryEnabled() {
962 if (is_mdns_enabled_) 1016 if (is_mdns_enabled_)
963 return; 1017 return;
964 1018
965 SetWakeReason(MediaRouteProviderWakeReason::ENABLE_MDNS_DISCOVERY); 1019 SetWakeReason(MediaRouteProviderWakeReason::ENABLE_MDNS_DISCOVERY);
966 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoEnsureMdnsDiscoveryEnabled, 1020 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoEnsureMdnsDiscoveryEnabled,
967 base::Unretained(this))); 1021 base::Unretained(this)));
968 should_enable_mdns_discovery_ = true; 1022 should_enable_mdns_discovery_ = true;
969 } 1023 }
970 1024
971 void MediaRouterMojoImpl::DoEnsureMdnsDiscoveryEnabled() { 1025 void MediaRouterMojoImpl::DoEnsureMdnsDiscoveryEnabled() {
972 DVLOG_WITH_INSTANCE(1) << "DoEnsureMdnsDiscoveryEnabled"; 1026 DVLOG_WITH_INSTANCE(1) << "DoEnsureMdnsDiscoveryEnabled";
973 if (!is_mdns_enabled_) { 1027 if (!is_mdns_enabled_) {
974 media_route_provider_->EnableMdnsDiscovery(); 1028 media_route_provider_->EnableMdnsDiscovery();
975 is_mdns_enabled_ = true; 1029 is_mdns_enabled_ = true;
976 } 1030 }
977 } 1031 }
978 1032
979 void MediaRouterMojoImpl::OnFirewallCheckComplete( 1033 void MediaRouterMojoImpl::OnFirewallCheckComplete(
980 bool firewall_can_use_local_ports) { 1034 bool firewall_can_use_local_ports) {
981 if (firewall_can_use_local_ports) 1035 if (firewall_can_use_local_ports)
982 EnsureMdnsDiscoveryEnabled(); 1036 EnsureMdnsDiscoveryEnabled();
983 } 1037 }
984 #endif 1038 #endif
985 1039
986 void MediaRouterMojoImpl::UpdateMediaSinks( 1040 void MediaRouterMojoImpl::UpdateMediaSinks(
987 const MediaSource::Id& source_id) { 1041 const MediaSource::Id& source_id) {
988 SetWakeReason(MediaRouteProviderWakeReason::UPDATE_MEDIA_SINKS); 1042 SetWakeReason(MediaRouteProviderWakeReason::UPDATE_MEDIA_SINKS);
989 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoUpdateMediaSinks, 1043 RunOrDefer(base::BindOnce(&MediaRouterMojoImpl::DoUpdateMediaSinks,
990 base::Unretained(this), source_id)); 1044 base::Unretained(this), source_id));
991 } 1045 }
992 1046
993 void MediaRouterMojoImpl::DoUpdateMediaSinks( 1047 void MediaRouterMojoImpl::DoUpdateMediaSinks(
994 const MediaSource::Id& source_id) { 1048 const MediaSource::Id& source_id) {
995 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks" << source_id; 1049 DVLOG_WITH_INSTANCE(1) << "DoUpdateMediaSinks: " << source_id;
996 media_route_provider_->UpdateMediaSinks(source_id); 1050 media_route_provider_->UpdateMediaSinks(source_id);
997 } 1051 }
998 1052
1053 void MediaRouterMojoImpl::RemoveInvalidRouteControllers(
1054 const std::vector<MediaRoute>& routes) {
1055 auto it = route_controllers_.begin();
1056 while (it != route_controllers_.end()) {
1057 if (IsRouteKnown(it->first)) {
1058 ++it;
1059 } else {
1060 it->second->Invalidate();
1061 it = route_controllers_.erase(it);
1062 }
1063 }
1064 }
1065
1066 void MediaRouterMojoImpl::OnMediaControllerCreated(
1067 const MediaRoute::Id& route_id,
1068 bool success) {
1069 // TODO(takumif): Record success/failure with UMA.
1070 DVLOG_WITH_INSTANCE(1) << "OnMediaControllerCreated: " << route_id
1071 << (success ? " was successful." : " failed.");
1072 }
1073
999 } // namespace media_router 1074 } // namespace media_router
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698