| 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_imp
l.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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 DialMediaSinkServiceImpl::~DialMediaSinkServiceImpl() { | 24 DialMediaSinkServiceImpl::~DialMediaSinkServiceImpl() { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 Stop(); | 26 Stop(); |
| 27 } | 27 } |
| 28 | 28 |
| 29 void DialMediaSinkServiceImpl::Start() { | 29 void DialMediaSinkServiceImpl::Start() { |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 31 if (dial_registry_) | 31 if (dial_registry_) |
| 32 return; | 32 return; |
| 33 | 33 |
| 34 dial_registry_ = DialRegistry::GetInstance(); | 34 dial_registry_ = |
| 35 test_dial_registry_ ? test_dial_registry_ : DialRegistry::GetInstance(); |
| 35 dial_registry_->RegisterObserver(this); | 36 dial_registry_->RegisterObserver(this); |
| 36 dial_registry_->OnListenerAdded(); | 37 dial_registry_->OnListenerAdded(); |
| 38 MediaSinkServiceBase::StartTimer(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 void DialMediaSinkServiceImpl::Stop() { | 41 void DialMediaSinkServiceImpl::Stop() { |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 42 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 41 if (!dial_registry_) | 43 if (!dial_registry_) |
| 42 return; | 44 return; |
| 43 | 45 |
| 44 dial_registry_->OnListenerRemoved(); | 46 dial_registry_->OnListenerRemoved(); |
| 45 dial_registry_->UnregisterObserver(this); | 47 dial_registry_->UnregisterObserver(this); |
| 46 dial_registry_ = nullptr; | 48 dial_registry_ = nullptr; |
| 49 MediaSinkServiceBase::StopTimer(); |
| 47 } | 50 } |
| 48 | 51 |
| 49 DeviceDescriptionService* DialMediaSinkServiceImpl::GetDescriptionService() { | 52 DeviceDescriptionService* DialMediaSinkServiceImpl::GetDescriptionService() { |
| 50 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 53 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 51 if (!description_service_.get()) { | 54 if (!description_service_.get()) { |
| 52 description_service_.reset(new DeviceDescriptionService( | 55 description_service_.reset(new DeviceDescriptionService( |
| 53 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionAvailable, | 56 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionAvailable, |
| 54 base::Unretained(this)), | 57 base::Unretained(this)), |
| 55 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionError, | 58 base::Bind(&DialMediaSinkServiceImpl::OnDeviceDescriptionError, |
| 56 base::Unretained(this)))); | 59 base::Unretained(this)))); |
| 57 } | 60 } |
| 58 return description_service_.get(); | 61 return description_service_.get(); |
| 59 } | 62 } |
| 60 | 63 |
| 61 void DialMediaSinkServiceImpl::SetDialRegistryForTest( | 64 void DialMediaSinkServiceImpl::SetDialRegistryForTest( |
| 62 DialRegistry* dial_registry) { | 65 DialRegistry* dial_registry) { |
| 63 DCHECK(!dial_registry_); | 66 DCHECK(!test_dial_registry_); |
| 64 dial_registry_ = dial_registry; | 67 test_dial_registry_ = dial_registry; |
| 65 | |
| 66 DCHECK(dial_registry); | |
| 67 dial_registry_->RegisterObserver(this); | |
| 68 dial_registry_->OnListenerAdded(); | |
| 69 } | 68 } |
| 70 | 69 |
| 71 void DialMediaSinkServiceImpl::SetDescriptionServiceForTest( | 70 void DialMediaSinkServiceImpl::SetDescriptionServiceForTest( |
| 72 std::unique_ptr<DeviceDescriptionService> description_service) { | 71 std::unique_ptr<DeviceDescriptionService> description_service) { |
| 73 DCHECK(!description_service_); | 72 DCHECK(!description_service_); |
| 74 description_service_ = std::move(description_service); | 73 description_service_ = std::move(description_service); |
| 75 } | 74 } |
| 76 | 75 |
| 77 void DialMediaSinkServiceImpl::OnDialDeviceEvent( | 76 void DialMediaSinkServiceImpl::OnDialDeviceEvent( |
| 78 const DialRegistry::DeviceList& devices) { | 77 const DialRegistry::DeviceList& devices) { |
| 79 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 78 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 80 DVLOG(2) << "DialMediaSinkServiceImpl::OnDialDeviceEvent found " | 79 DVLOG(2) << "DialMediaSinkServiceImpl::OnDialDeviceEvent found " |
| 81 << devices.size() << " devices"; | 80 << devices.size() << " devices"; |
| 82 | 81 |
| 83 // Timer starts in |OnDialDeviceEvent()|, and expires 3 seconds later. If | |
| 84 // |OnDeviceDescriptionAvailable()| is called after |finish_timer_| expires, | |
| 85 // |finish_timer_| is restarted. | |
| 86 StartTimer(); | |
| 87 | |
| 88 current_sinks_.clear(); | 82 current_sinks_.clear(); |
| 89 current_devices_ = devices; | 83 current_devices_ = devices; |
| 90 | 84 |
| 91 GetDescriptionService()->GetDeviceDescriptions(devices, | 85 GetDescriptionService()->GetDeviceDescriptions(devices, |
| 92 request_context_.get()); | 86 request_context_.get()); |
| 93 } | 87 } |
| 94 | 88 |
| 95 void DialMediaSinkServiceImpl::OnDialError(DialRegistry::DialErrorCode type) { | 89 void DialMediaSinkServiceImpl::OnDialError(DialRegistry::DialErrorCode type) { |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 97 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | 91 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 118 std::string ip_address = device_data.device_description_url().host(); | 112 std::string ip_address = device_data.device_description_url().host(); |
| 119 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { | 113 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { |
| 120 DVLOG(1) << "Invalid ip_address: " << ip_address; | 114 DVLOG(1) << "Invalid ip_address: " << ip_address; |
| 121 return; | 115 return; |
| 122 } | 116 } |
| 123 | 117 |
| 124 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | 118 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); |
| 125 | 119 |
| 126 // Start fetch timer again if device description comes back after | 120 // Start fetch timer again if device description comes back after |
| 127 // |finish_timer_| fires. | 121 // |finish_timer_| fires. |
| 128 if (!finish_timer_->IsRunning()) | 122 MediaSinkServiceBase::RestartTimer(); |
| 129 StartTimer(); | |
| 130 } | 123 } |
| 131 | 124 |
| 132 void DialMediaSinkServiceImpl::OnDeviceDescriptionError( | 125 void DialMediaSinkServiceImpl::OnDeviceDescriptionError( |
| 133 const DialDeviceData& device, | 126 const DialDeviceData& device, |
| 134 const std::string& error_message) { | 127 const std::string& error_message) { |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 136 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | 129 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; |
| 137 } | 130 } |
| 138 | 131 |
| 139 } // namespace media_router | 132 } // namespace media_router |
| OLD | NEW |