Chromium Code Reviews| 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.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 { | 14 namespace { |
| 15 // Time interval when media sink service sends sinks to MRP. | 15 // Time interval when media sink service sends sinks to MRP. |
| 16 const int kFetchCompleteTimeoutSecs = 3; | 16 const int kFetchCompleteTimeoutSecs = 3; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace media_router { | 19 namespace media_router { |
| 20 | 20 |
| 21 DialMediaSinkService::DialMediaSinkService( | 21 DialMediaSinkService::DialMediaSinkService( |
| 22 const OnSinksDiscoveredCallback& callback, | 22 const OnSinksDiscoveredCallback& callback, |
| 23 net::URLRequestContextGetter* request_context) | 23 net::URLRequestContextGetter* request_context) |
| 24 : MediaSinkService(callback), request_context_(request_context) { | 24 : MediaSinkService(callback), request_context_(request_context) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 25 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 26 DCHECK(request_context_); | 26 DCHECK(request_context_); |
| 27 } | 27 } |
| 28 | 28 |
| 29 DialMediaSinkService::~DialMediaSinkService() {} | 29 DialMediaSinkService::~DialMediaSinkService() {} |
|
mark a. foltz
2017/05/09 18:21:13
There needs to be some guarantees that the followi
zhaobin
2017/05/10 18:17:34
Marked DialMediaSinkService as content::BrowserThr
| |
| 30 | 30 |
| 31 void DialMediaSinkService::Start() { | 31 void DialMediaSinkService::Start() { |
| 32 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 32 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 33 dial_registry()->RegisterObserver(this); | 33 content::BrowserThread::PostTask( |
| 34 dial_registry()->StartPeriodicDiscovery(); | 34 content::BrowserThread::IO, FROM_HERE, |
| 35 base::Bind(&DialMediaSinkService::StartOnIOThread, this)); | |
| 35 } | 36 } |
| 36 | 37 |
| 37 void DialMediaSinkService::Stop() { | 38 void DialMediaSinkService::Stop() { |
| 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 39 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 39 dial_registry()->UnregisterObserver(this); | 40 content::BrowserThread::PostTask( |
| 40 } | 41 content::BrowserThread::IO, FROM_HERE, |
| 41 | 42 base::Bind(&DialMediaSinkService::StopOnIOThread, this)); |
| 42 DialRegistry* DialMediaSinkService::dial_registry() { | |
| 43 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 44 return DialRegistry::GetInstance(); | |
| 45 } | 43 } |
| 46 | 44 |
| 47 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { | 45 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { |
| 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 49 if (!description_service_.get()) { | 47 if (!description_service_.get()) { |
| 50 description_service_.reset(new DeviceDescriptionService( | 48 description_service_.reset(new DeviceDescriptionService( |
| 51 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, | 49 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, |
| 52 base::Unretained(this)), | 50 base::Unretained(this)), |
| 53 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | 51 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, |
| 54 base::Unretained(this)))); | 52 base::Unretained(this)))); |
| 55 } | 53 } |
| 56 return description_service_.get(); | 54 return description_service_.get(); |
| 57 } | 55 } |
| 58 | 56 |
| 57 void DialMediaSinkService::SetDialRegistryForTest(DialRegistry* dial_registry) { | |
| 58 dial_registry_ = dial_registry; | |
| 59 } | |
| 60 | |
| 61 void DialMediaSinkService::SetDescriptionServiceForTest( | |
| 62 std::unique_ptr<DeviceDescriptionService> description_service) { | |
| 63 description_service_ = std::move(description_service); | |
| 64 } | |
| 65 | |
| 66 void DialMediaSinkService::SetTimerForTest(std::unique_ptr<base::Timer> timer) { | |
| 67 finish_timer_ = std::move(timer); | |
| 68 } | |
| 69 | |
| 70 void DialMediaSinkService::StartOnIOThread() { | |
| 71 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 72 // Create a finish timer. | |
| 73 if (!finish_timer_) | |
| 74 finish_timer_.reset(new base::OneShotTimer()); | |
| 75 | |
| 76 if (!dial_registry_) | |
| 77 dial_registry_ = DialRegistry::GetInstance(); | |
| 78 | |
| 79 dial_registry_->RegisterObserver(this); | |
| 80 dial_registry_->OnListenerAdded(); | |
| 81 } | |
| 82 | |
| 83 void DialMediaSinkService::StopOnIOThread() { | |
| 84 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 85 if (!dial_registry_) | |
|
Kevin M
2017/05/09 17:32:06
Assuming the service was Start()ed, then is it pos
zhaobin
2017/05/10 18:17:34
Will change this to DCHECK. DCHECK(xxx) should be
| |
| 86 return; | |
| 87 | |
| 88 dial_registry_->OnListenerRemoved(); | |
| 89 dial_registry_->UnregisterObserver(this); | |
| 90 } | |
| 91 | |
| 92 void DialMediaSinkService::OnFetchCompletedOnUIThread() { | |
| 93 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 94 if (current_sinks_ == mrp_sinks_) { | |
| 95 DVLOG(2) << "No update to sink list."; | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 auto sinks = current_sinks_; | |
| 100 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); | |
| 101 sink_discovery_callback_.Run( | |
| 102 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); | |
| 103 mrp_sinks_ = std::move(sinks); | |
| 104 } | |
| 105 | |
| 59 void DialMediaSinkService::OnDialDeviceEvent( | 106 void DialMediaSinkService::OnDialDeviceEvent( |
| 60 const DialRegistry::DeviceList& devices) { | 107 const DialRegistry::DeviceList& devices) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 62 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | 109 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() |
| 63 << " devices"; | 110 << " devices"; |
| 64 | 111 |
| 65 // Add a finish timer. | 112 DCHECK(finish_timer_); |
| 66 finish_timer_.reset(new base::OneShotTimer()); | |
| 67 base::TimeDelta finish_delay = | 113 base::TimeDelta finish_delay = |
|
Kevin M
2017/05/09 17:32:05
Suggestion: refactor these calls into a method "Re
Kevin M
2017/05/09 17:32:06
This code resets the timer for every DIAL event, s
zhaobin
2017/05/10 18:17:34
Done.
zhaobin
2017/05/10 18:17:34
Yes. DIAL event is not for the *last* record. It r
| |
| 68 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | 114 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); |
| 69 finish_timer_->Start(FROM_HERE, finish_delay, this, | 115 finish_timer_->Start(FROM_HERE, finish_delay, |
| 70 &DialMediaSinkService::OnFetchCompleted); | 116 base::Bind(&DialMediaSinkService::OnFetchCompleted, |
| 117 base::Unretained(this))); | |
| 71 | 118 |
| 72 current_sinks_.clear(); | 119 current_sinks_.clear(); |
|
mark a. foltz
2017/05/09 18:21:13
This is being called on the IO thread, but current
zhaobin
2017/05/10 18:17:34
Created a delegate to handle thread hopping. DialM
| |
| 73 current_devices_ = devices; | 120 current_devices_ = devices; |
| 74 | 121 |
| 75 GetDescriptionService()->GetDeviceDescriptions(devices, | 122 GetDescriptionService()->GetDeviceDescriptions(devices, |
| 76 request_context_.get()); | 123 request_context_.get()); |
| 77 } | 124 } |
| 78 | 125 |
| 79 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | 126 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { |
| 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 127 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 81 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | 128 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); |
| 82 } | 129 } |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 98 MediaSink::IconType::GENERIC); | 145 MediaSink::IconType::GENERIC); |
| 99 DialSinkExtraData extra_data; | 146 DialSinkExtraData extra_data; |
| 100 extra_data.app_url = description_data.app_url; | 147 extra_data.app_url = description_data.app_url; |
| 101 extra_data.model_name = description_data.model_name; | 148 extra_data.model_name = description_data.model_name; |
| 102 std::string ip_address = device_data.device_description_url().host(); | 149 std::string ip_address = device_data.device_description_url().host(); |
| 103 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { | 150 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { |
| 104 DVLOG(1) << "Invalid ip_address: " << ip_address; | 151 DVLOG(1) << "Invalid ip_address: " << ip_address; |
| 105 return; | 152 return; |
| 106 } | 153 } |
| 107 | 154 |
| 108 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | 155 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); |
|
mark a. foltz
2017/05/09 18:21:13
Ditto. This class could be redesigned to pass the
zhaobin
2017/05/10 18:17:34
Done.
| |
| 109 | 156 |
| 110 if (finish_timer_) | 157 DCHECK(finish_timer_); |
| 158 if (finish_timer_->IsRunning()) | |
| 111 return; | 159 return; |
| 112 | 160 |
| 113 // Start fetch timer again if device description comes back after | 161 // Start fetch timer again if device description comes back after |
| 114 // |finish_timer_| fires. | 162 // |finish_timer_| fires. |
| 115 base::TimeDelta finish_delay = | 163 base::TimeDelta finish_delay = |
| 116 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | 164 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); |
| 117 finish_timer_.reset(new base::OneShotTimer()); | 165 finish_timer_->Start(FROM_HERE, finish_delay, |
| 118 finish_timer_->Start(FROM_HERE, finish_delay, this, | 166 base::Bind(&DialMediaSinkService::OnFetchCompleted, |
| 119 &DialMediaSinkService::OnFetchCompleted); | 167 base::Unretained(this))); |
| 120 } | 168 } |
| 121 | 169 |
| 122 void DialMediaSinkService::OnDeviceDescriptionError( | 170 void DialMediaSinkService::OnDeviceDescriptionError( |
| 123 const DialDeviceData& device, | 171 const DialDeviceData& device, |
| 124 const std::string& error_message) { | 172 const std::string& error_message) { |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 173 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 126 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | 174 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; |
| 127 } | 175 } |
| 128 | 176 |
| 129 void DialMediaSinkService::OnFetchCompleted() { | 177 void DialMediaSinkService::OnFetchCompleted() { |
| 130 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 131 DCHECK(!sink_discovery_callback_.is_null()); | 179 DCHECK(!sink_discovery_callback_.is_null()); |
| 132 | 180 |
| 133 finish_timer_.reset(); | 181 content::BrowserThread::PostTask( |
| 134 | 182 content::BrowserThread::UI, FROM_HERE, |
| 135 auto sinks = current_sinks_; | 183 base::Bind(&DialMediaSinkService::OnFetchCompletedOnUIThread, this)); |
| 136 if (sinks == mrp_sinks_) { | |
| 137 DVLOG(2) << "No update to sink list."; | |
| 138 return; | |
| 139 } | |
| 140 | |
| 141 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); | |
| 142 sink_discovery_callback_.Run( | |
| 143 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); | |
| 144 mrp_sinks_ = std::move(sinks); | |
| 145 } | 184 } |
| 146 | 185 |
| 147 } // namespace media_router | 186 } // namespace media_router |
| OLD | NEW |