| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/discovery/dial/dial_media_sink_service.h" | 5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service_imp
l.h" |
| 6 | 6 |
| 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "net/url_request/url_request_context_getter.h" | 10 #include "net/url_request/url_request_context_getter.h" |
| 11 | 11 |
| 12 using content::BrowserThread; | 12 using content::BrowserThread; |
| 13 | 13 |
| 14 namespace { | |
| 15 // Time interval when media sink service sends sinks to MRP. | |
| 16 const int kFetchCompleteTimeoutSecs = 3; | |
| 17 } | |
| 18 | |
| 19 namespace media_router { | 14 namespace media_router { |
| 20 | 15 |
| 21 DialMediaSinkService::DialMediaSinkService( | 16 DialMediaSinkServiceImpl::DialMediaSinkServiceImpl( |
| 22 const OnSinksDiscoveredCallback& callback, | 17 const OnSinksDiscoveredCallback& callback, |
| 23 net::URLRequestContextGetter* request_context) | 18 net::URLRequestContextGetter* request_context) |
| 24 : MediaSinkService(callback), request_context_(request_context) { | 19 : MediaSinkServiceBase(callback), request_context_(request_context) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 20 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 DCHECK(request_context_); | 21 DCHECK(request_context_); |
| 27 } | 22 } |
| 28 | 23 |
| 29 DialMediaSinkService::~DialMediaSinkService() { | 24 DialMediaSinkServiceImpl::~DialMediaSinkServiceImpl() { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 31 Stop(); | 26 Stop(); |
| 32 } | 27 } |
| 33 | 28 |
| 34 void DialMediaSinkService::Start() { | 29 void DialMediaSinkServiceImpl::Start() { |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 36 if (dial_registry_) | 31 if (dial_registry_) |
| 37 return; | 32 return; |
| 38 | 33 |
| 39 dial_registry_ = DialRegistry::GetInstance(); | 34 dial_registry_ = DialRegistry::GetInstance(); |
| 40 dial_registry_->RegisterObserver(this); | 35 dial_registry_->RegisterObserver(this); |
| 41 dial_registry_->OnListenerAdded(); | 36 dial_registry_->OnListenerAdded(); |
| 42 } | 37 } |
| 43 | 38 |
| 44 void DialMediaSinkService::Stop() { | 39 void DialMediaSinkServiceImpl::Stop() { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 46 if (!dial_registry_) | 41 if (!dial_registry_) |
| 47 return; | 42 return; |
| 48 | 43 |
| 49 dial_registry_->OnListenerRemoved(); | 44 dial_registry_->OnListenerRemoved(); |
| 50 dial_registry_->UnregisterObserver(this); | 45 dial_registry_->UnregisterObserver(this); |
| 51 dial_registry_ = nullptr; | 46 dial_registry_ = nullptr; |
| 52 } | 47 } |
| 53 | 48 |
| 54 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { | 49 DeviceDescriptionService* DialMediaSinkServiceImpl::GetDescriptionService() { |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 56 if (!description_service_.get()) { | 51 if (!description_service_.get()) { |
| 57 description_service_.reset(new DeviceDescriptionService( | 52 description_service_.reset(new DeviceDescriptionService( |
| 58 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, | 53 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionAvailable, |
| 59 base::Unretained(this)), | 54 base::Unretained(this)), |
| 60 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | 55 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionError, |
| 61 base::Unretained(this)))); | 56 base::Unretained(this)))); |
| 62 } | 57 } |
| 63 return description_service_.get(); | 58 return description_service_.get(); |
| 64 } | 59 } |
| 65 | 60 |
| 66 void DialMediaSinkService::SetDialRegistryForTest(DialRegistry* dial_registry) { | 61 void DialMediaSinkServiceImpl::SetDialRegistryForTest( |
| 62 DialRegistry* dial_registry) { |
| 67 DCHECK(!dial_registry_); | 63 DCHECK(!dial_registry_); |
| 68 dial_registry_ = dial_registry; | 64 dial_registry_ = dial_registry; |
| 69 | 65 |
| 70 DCHECK(dial_registry); | 66 DCHECK(dial_registry); |
| 71 dial_registry_->RegisterObserver(this); | 67 dial_registry_->RegisterObserver(this); |
| 72 dial_registry_->OnListenerAdded(); | 68 dial_registry_->OnListenerAdded(); |
| 73 } | 69 } |
| 74 | 70 |
| 75 void DialMediaSinkService::SetDescriptionServiceForTest( | 71 void DialMediaSinkServiceImpl::SetDescriptionServiceForTest( |
| 76 std::unique_ptr<DeviceDescriptionService> description_service) { | 72 std::unique_ptr<DeviceDescriptionService> description_service) { |
| 77 DCHECK(!description_service_); | 73 DCHECK(!description_service_); |
| 78 description_service_ = std::move(description_service); | 74 description_service_ = std::move(description_service); |
| 79 } | 75 } |
| 80 | 76 |
| 81 void DialMediaSinkService::SetTimerForTest(std::unique_ptr<base::Timer> timer) { | 77 void DialMediaSinkServiceImpl::OnDialDeviceEvent( |
| 82 DCHECK(!finish_timer_); | |
| 83 finish_timer_ = std::move(timer); | |
| 84 } | |
| 85 | |
| 86 void DialMediaSinkService::OnDialDeviceEvent( | |
| 87 const DialRegistry::DeviceList& devices) { | 78 const DialRegistry::DeviceList& devices) { |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 89 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | 80 DVLOG(2) << "DialMediaSinkServiceImpl::OnDialDeviceEvent found " |
| 90 << " devices"; | 81 << devices.size() << " devices"; |
| 91 | 82 |
| 83 // Timer starts in |OnDialDeviceEvent()|, and expires 3 seconds later. If |
| 84 // |OnDeviceDescriptionAvailable()| is called after |finish_timer_| expires, |
| 85 // |finish_timer_| is restarted. |
| 92 StartTimer(); | 86 StartTimer(); |
| 93 | 87 |
| 94 current_sinks_.clear(); | 88 current_sinks_.clear(); |
| 95 current_devices_ = devices; | 89 current_devices_ = devices; |
| 96 | 90 |
| 97 GetDescriptionService()->GetDeviceDescriptions(devices, | 91 GetDescriptionService()->GetDeviceDescriptions(devices, |
| 98 request_context_.get()); | 92 request_context_.get()); |
| 99 } | 93 } |
| 100 | 94 |
| 101 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | 95 void DialMediaSinkServiceImpl::OnDialError(DialRegistry::DialErrorCode type) { |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 103 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | 97 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); |
| 104 } | 98 } |
| 105 | 99 |
| 106 void DialMediaSinkService::OnDeviceDescriptionAvailable( | 100 void DialMediaSinkServiceImpl::OnDeviceDescriptionAvailable( |
| 107 const DialDeviceData& device_data, | 101 const DialDeviceData& device_data, |
| 108 const ParsedDialDeviceDescription& description_data) { | 102 const ParsedDialDeviceDescription& description_data) { |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 103 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 110 | 104 |
| 111 if (!base::ContainsValue(current_devices_, device_data)) { | 105 if (!base::ContainsValue(current_devices_, device_data)) { |
| 112 DVLOG(2) << "Device data not found in current device data list..."; | 106 DVLOG(2) << "Device data not found in current device data list..."; |
| 113 return; | 107 return; |
| 114 } | 108 } |
| 115 | 109 |
| 116 // When use this "sink" within browser, please note it will have a different | 110 // When use this "sink" within browser, please note it will have a different |
| (...skipping 11 matching lines...) Expand all Loading... |
| 128 } | 122 } |
| 129 | 123 |
| 130 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | 124 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); |
| 131 | 125 |
| 132 // Start fetch timer again if device description comes back after | 126 // Start fetch timer again if device description comes back after |
| 133 // |finish_timer_| fires. | 127 // |finish_timer_| fires. |
| 134 if (!finish_timer_->IsRunning()) | 128 if (!finish_timer_->IsRunning()) |
| 135 StartTimer(); | 129 StartTimer(); |
| 136 } | 130 } |
| 137 | 131 |
| 138 void DialMediaSinkService::OnDeviceDescriptionError( | 132 void DialMediaSinkServiceImpl::OnDeviceDescriptionError( |
| 139 const DialDeviceData& device, | 133 const DialDeviceData& device, |
| 140 const std::string& error_message) { | 134 const std::string& error_message) { |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 142 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | 136 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; |
| 143 } | 137 } |
| 144 | 138 |
| 145 void DialMediaSinkService::OnFetchCompleted() { | |
| 146 DCHECK(!sink_discovery_callback_.is_null()); | |
| 147 | |
| 148 if (current_sinks_ == mrp_sinks_) { | |
| 149 DVLOG(2) << "No update to sink list."; | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 DVLOG(2) << "Send sinks to media router, [size]: " << current_sinks_.size(); | |
| 154 sink_discovery_callback_.Run(std::vector<MediaSinkInternal>( | |
| 155 current_sinks_.begin(), current_sinks_.end())); | |
| 156 mrp_sinks_ = current_sinks_; | |
| 157 } | |
| 158 | |
| 159 void DialMediaSinkService::StartTimer() { | |
| 160 // Create a finish timer. | |
| 161 if (!finish_timer_) | |
| 162 finish_timer_.reset(new base::OneShotTimer()); | |
| 163 | |
| 164 base::TimeDelta finish_delay = | |
| 165 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | |
| 166 finish_timer_->Start(FROM_HERE, finish_delay, | |
| 167 base::Bind(&DialMediaSinkService::OnFetchCompleted, | |
| 168 base::Unretained(this))); | |
| 169 } | |
| 170 | |
| 171 } // namespace media_router | 139 } // namespace media_router |
| OLD | NEW |