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 23 matching lines...) Expand all Loading... |
34 | 34 |
35 using SinkAvailability = interfaces::MediaRouter::SinkAvailability; | 35 using SinkAvailability = interfaces::MediaRouter::SinkAvailability; |
36 | 36 |
37 namespace { | 37 namespace { |
38 | 38 |
39 scoped_ptr<content::PresentationSessionMessage> | 39 scoped_ptr<content::PresentationSessionMessage> |
40 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { | 40 ConvertToPresentationSessionMessage(interfaces::RouteMessagePtr input) { |
41 DCHECK(!input.is_null()); | 41 DCHECK(!input.is_null()); |
42 scoped_ptr<content::PresentationSessionMessage> output; | 42 scoped_ptr<content::PresentationSessionMessage> output; |
43 switch (input->type) { | 43 switch (input->type) { |
44 case interfaces::RouteMessage::Type::TYPE_TEXT: { | 44 case interfaces::RouteMessage::Type::TEXT: { |
45 DCHECK(!input->message.is_null()); | 45 DCHECK(!input->message.is_null()); |
46 DCHECK(input->data.is_null()); | 46 DCHECK(input->data.is_null()); |
47 output.reset(new content::PresentationSessionMessage( | 47 output.reset(new content::PresentationSessionMessage( |
48 content::PresentationMessageType::TEXT)); | 48 content::PresentationMessageType::TEXT)); |
49 input->message.Swap(&output->message); | 49 input->message.Swap(&output->message); |
50 return output; | 50 return output; |
51 } | 51 } |
52 case interfaces::RouteMessage::Type::TYPE_BINARY: { | 52 case interfaces::RouteMessage::Type::BINARY: { |
53 DCHECK(!input->data.is_null()); | 53 DCHECK(!input->data.is_null()); |
54 DCHECK(input->message.is_null()); | 54 DCHECK(input->message.is_null()); |
55 output.reset(new content::PresentationSessionMessage( | 55 output.reset(new content::PresentationSessionMessage( |
56 content::PresentationMessageType::ARRAY_BUFFER)); | 56 content::PresentationMessageType::ARRAY_BUFFER)); |
57 output->data.reset(new std::vector<uint8_t>); | 57 output->data.reset(new std::vector<uint8_t>); |
58 input->data.Swap(output->data.get()); | 58 input->data.Swap(output->data.get()); |
59 return output; | 59 return output; |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
(...skipping 30 matching lines...) Expand all Loading... |
93 // |has_local_display_route| is false. Note that ObserverList supports | 93 // |has_local_display_route| is false. Note that ObserverList supports |
94 // removing an observer while iterating through it. | 94 // removing an observer while iterating through it. |
95 router_->UpdateHasLocalDisplayRoute(has_local_display_route); | 95 router_->UpdateHasLocalDisplayRoute(has_local_display_route); |
96 } | 96 } |
97 | 97 |
98 MediaRouterMojoImpl::MediaRouterMojoImpl( | 98 MediaRouterMojoImpl::MediaRouterMojoImpl( |
99 extensions::EventPageTracker* event_page_tracker) | 99 extensions::EventPageTracker* event_page_tracker) |
100 : event_page_tracker_(event_page_tracker), | 100 : event_page_tracker_(event_page_tracker), |
101 instance_id_(base::GenerateGUID()), | 101 instance_id_(base::GenerateGUID()), |
102 has_local_display_route_(false), | 102 has_local_display_route_(false), |
103 availability_(interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE), | 103 availability_(interfaces::MediaRouter::SinkAvailability::UNAVAILABLE), |
104 wakeup_attempt_count_(0), | 104 wakeup_attempt_count_(0), |
105 current_wake_reason_(MediaRouteProviderWakeReason::TOTAL_COUNT), | 105 current_wake_reason_(MediaRouteProviderWakeReason::TOTAL_COUNT), |
106 weak_factory_(this) { | 106 weak_factory_(this) { |
107 DCHECK(event_page_tracker_); | 107 DCHECK(event_page_tracker_); |
108 } | 108 } |
109 | 109 |
110 MediaRouterMojoImpl::~MediaRouterMojoImpl() { | 110 MediaRouterMojoImpl::~MediaRouterMojoImpl() { |
111 DCHECK(thread_checker_.CalledOnValidThread()); | 111 DCHECK(thread_checker_.CalledOnValidThread()); |
112 | 112 |
113 // Make sure |routes_observer_| is destroyed first, because it triggers | 113 // Make sure |routes_observer_| is destroyed first, because it triggers |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
380 } else { | 380 } else { |
381 DCHECK(!sinks_query->observers.HasObserver(observer)); | 381 DCHECK(!sinks_query->observers.HasObserver(observer)); |
382 } | 382 } |
383 | 383 |
384 // We need to call DoStartObservingMediaSinks every time an observer is | 384 // We need to call DoStartObservingMediaSinks every time an observer is |
385 // added to ensure the observer will be notified with a fresh set of results. | 385 // added to ensure the observer will be notified with a fresh set of results. |
386 // The exception is if it is known that no sinks are available, then there is | 386 // The exception is if it is known that no sinks are available, then there is |
387 // no need to call to MRPM. | 387 // no need to call to MRPM. |
388 // TODO(imcheng): Implement caching. (crbug.com/492451) | 388 // TODO(imcheng): Implement caching. (crbug.com/492451) |
389 sinks_query->observers.AddObserver(observer); | 389 sinks_query->observers.AddObserver(observer); |
390 if (availability_ != interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) { | 390 if (availability_ != interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) { |
391 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_SINKS); | 391 SetWakeReason(MediaRouteProviderWakeReason::START_OBSERVING_MEDIA_SINKS); |
392 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, | 392 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, |
393 base::Unretained(this), source_id)); | 393 base::Unretained(this), source_id)); |
394 } else { | 394 } else { |
395 observer->OnSinksReceived(std::vector<MediaSink>()); | 395 observer->OnSinksReceived(std::vector<MediaSink>()); |
396 } | 396 } |
397 | 397 |
398 return true; | 398 return true; |
399 } | 399 } |
400 | 400 |
(...skipping 10 matching lines...) Expand all Loading... |
411 // If we are removing the final observer for the source, then stop | 411 // If we are removing the final observer for the source, then stop |
412 // observing sinks for it. | 412 // observing sinks for it. |
413 // might_have_observers() is reliable here on the assumption that this call | 413 // might_have_observers() is reliable here on the assumption that this call |
414 // is not inside the ObserverList iteration. | 414 // is not inside the ObserverList iteration. |
415 sinks_query->observers.RemoveObserver(observer); | 415 sinks_query->observers.RemoveObserver(observer); |
416 if (!sinks_query->observers.might_have_observers()) { | 416 if (!sinks_query->observers.might_have_observers()) { |
417 // Only ask MRPM to stop observing media sinks if the availability is not | 417 // Only ask MRPM to stop observing media sinks if the availability is not |
418 // UNAVAILABLE. | 418 // UNAVAILABLE. |
419 // Otherwise, the MRPM would have discarded the queries already. | 419 // Otherwise, the MRPM would have discarded the queries already. |
420 if (availability_ != | 420 if (availability_ != |
421 interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) { | 421 interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) { |
422 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_SINKS); | 422 SetWakeReason(MediaRouteProviderWakeReason::STOP_OBSERVING_MEDIA_SINKS); |
423 // The |sinks_queries_| entry will be removed in the immediate or deferred | 423 // The |sinks_queries_| entry will be removed in the immediate or deferred |
424 // |DoStopObservingMediaSinks| call. | 424 // |DoStopObservingMediaSinks| call. |
425 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaSinks, | 425 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStopObservingMediaSinks, |
426 base::Unretained(this), source_id)); | 426 base::Unretained(this), source_id)); |
427 } else { | 427 } else { |
428 sinks_queries_.erase(source_id); | 428 sinks_queries_.erase(source_id); |
429 } | 429 } |
430 } | 430 } |
431 } | 431 } |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
654 SinkAvailability availability) { | 654 SinkAvailability availability) { |
655 if (!interfaces::MediaRouter::SinkAvailability_IsValidValue(availability)) { | 655 if (!interfaces::MediaRouter::SinkAvailability_IsValidValue(availability)) { |
656 DLOG(WARNING) << "Unknown SinkAvailability value " << availability; | 656 DLOG(WARNING) << "Unknown SinkAvailability value " << availability; |
657 return; | 657 return; |
658 } | 658 } |
659 | 659 |
660 if (availability_ == availability) | 660 if (availability_ == availability) |
661 return; | 661 return; |
662 | 662 |
663 availability_ = availability; | 663 availability_ = availability; |
664 if (availability_ == interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) { | 664 if (availability_ == interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) { |
665 // Sinks are no longer available. MRPM has already removed all sink queries. | 665 // Sinks are no longer available. MRPM has already removed all sink queries. |
666 for (auto& source_and_query : sinks_queries_) | 666 for (auto& source_and_query : sinks_queries_) |
667 source_and_query.second->is_active = false; | 667 source_and_query.second->is_active = false; |
668 } else { | 668 } else { |
669 // Sinks are now available. Tell MRPM to start all sink queries again. | 669 // Sinks are now available. Tell MRPM to start all sink queries again. |
670 for (const auto& source_and_query : sinks_queries_) { | 670 for (const auto& source_and_query : sinks_queries_) { |
671 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, | 671 RunOrDefer(base::Bind(&MediaRouterMojoImpl::DoStartObservingMediaSinks, |
672 base::Unretained(this), source_and_query.first)); | 672 base::Unretained(this), source_and_query.first)); |
673 } | 673 } |
674 } | 674 } |
(...skipping 13 matching lines...) Expand all Loading... |
688 } | 688 } |
689 | 689 |
690 bool MediaRouterMojoImpl::HasLocalDisplayRoute() const { | 690 bool MediaRouterMojoImpl::HasLocalDisplayRoute() const { |
691 return has_local_display_route_; | 691 return has_local_display_route_; |
692 } | 692 } |
693 | 693 |
694 void MediaRouterMojoImpl::DoStartObservingMediaSinks( | 694 void MediaRouterMojoImpl::DoStartObservingMediaSinks( |
695 const MediaSource::Id& source_id) { | 695 const MediaSource::Id& source_id) { |
696 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaSinks: " << source_id; | 696 DVLOG_WITH_INSTANCE(1) << "DoStartObservingMediaSinks: " << source_id; |
697 // No need to call MRPM if there are no sinks available. | 697 // No need to call MRPM if there are no sinks available. |
698 if (availability_ == interfaces::MediaRouter::SINK_AVAILABILITY_UNAVAILABLE) | 698 if (availability_ == interfaces::MediaRouter::SinkAvailability::UNAVAILABLE) |
699 return; | 699 return; |
700 | 700 |
701 // No need to call MRPM if all observers have been removed in the meantime. | 701 // No need to call MRPM if all observers have been removed in the meantime. |
702 auto* sinks_query = sinks_queries_.get(source_id); | 702 auto* sinks_query = sinks_queries_.get(source_id); |
703 if (!sinks_query || !sinks_query->observers.might_have_observers()) { | 703 if (!sinks_query || !sinks_query->observers.might_have_observers()) { |
704 return; | 704 return; |
705 } | 705 } |
706 | 706 |
707 DVLOG_WITH_INSTANCE(1) << "MRPM.StartObservingMediaSinks: " << source_id; | 707 DVLOG_WITH_INSTANCE(1) << "MRPM.StartObservingMediaSinks: " << source_id; |
708 media_route_provider_->StartObservingMediaSinks(source_id); | 708 media_route_provider_->StartObservingMediaSinks(source_id); |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
831 if (current_wake_reason_ == MediaRouteProviderWakeReason::TOTAL_COUNT) | 831 if (current_wake_reason_ == MediaRouteProviderWakeReason::TOTAL_COUNT) |
832 current_wake_reason_ = reason; | 832 current_wake_reason_ = reason; |
833 } | 833 } |
834 | 834 |
835 void MediaRouterMojoImpl::ClearWakeReason() { | 835 void MediaRouterMojoImpl::ClearWakeReason() { |
836 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); | 836 DCHECK(current_wake_reason_ != MediaRouteProviderWakeReason::TOTAL_COUNT); |
837 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; | 837 current_wake_reason_ = MediaRouteProviderWakeReason::TOTAL_COUNT; |
838 } | 838 } |
839 | 839 |
840 } // namespace media_router | 840 } // namespace media_router |
OLD | NEW |