| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/media/router/discovery/dial/dial_media_sink_service.h" | |
| 6 | |
| 7 #include "chrome/browser/media/router/discovery/dial/dial_device_data.h" | |
| 8 #include "chrome/browser/profiles/profile.h" | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "net/url_request/url_request_context_getter.h" | |
| 11 | |
| 12 using content::BrowserThread; | |
| 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 { | |
| 20 | |
| 21 DialMediaSinkService::DialMediaSinkService( | |
| 22 const OnSinksDiscoveredCallback& callback, | |
| 23 net::URLRequestContextGetter* request_context) | |
| 24 : MediaSinkService(callback), request_context_(request_context) { | |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 26 DCHECK(request_context_); | |
| 27 } | |
| 28 | |
| 29 DialMediaSinkService::~DialMediaSinkService() { | |
| 30 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 31 Stop(); | |
| 32 } | |
| 33 | |
| 34 void DialMediaSinkService::Start() { | |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 36 if (dial_registry_) | |
| 37 return; | |
| 38 | |
| 39 dial_registry_ = DialRegistry::GetInstance(); | |
| 40 dial_registry_->RegisterObserver(this); | |
| 41 dial_registry_->OnListenerAdded(); | |
| 42 } | |
| 43 | |
| 44 void DialMediaSinkService::Stop() { | |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 46 if (!dial_registry_) | |
| 47 return; | |
| 48 | |
| 49 dial_registry_->OnListenerRemoved(); | |
| 50 dial_registry_->UnregisterObserver(this); | |
| 51 dial_registry_ = nullptr; | |
| 52 } | |
| 53 | |
| 54 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { | |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 56 if (!description_service_.get()) { | |
| 57 description_service_.reset(new DeviceDescriptionService( | |
| 58 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, | |
| 59 base::Unretained(this)), | |
| 60 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | |
| 61 base::Unretained(this)))); | |
| 62 } | |
| 63 return description_service_.get(); | |
| 64 } | |
| 65 | |
| 66 void DialMediaSinkService::SetDialRegistryForTest(DialRegistry* dial_registry) { | |
| 67 DCHECK(!dial_registry_); | |
| 68 dial_registry_ = dial_registry; | |
| 69 | |
| 70 DCHECK(dial_registry); | |
| 71 dial_registry_->RegisterObserver(this); | |
| 72 dial_registry_->OnListenerAdded(); | |
| 73 } | |
| 74 | |
| 75 void DialMediaSinkService::SetDescriptionServiceForTest( | |
| 76 std::unique_ptr<DeviceDescriptionService> description_service) { | |
| 77 DCHECK(!description_service_); | |
| 78 description_service_ = std::move(description_service); | |
| 79 } | |
| 80 | |
| 81 void DialMediaSinkService::SetTimerForTest(std::unique_ptr<base::Timer> timer) { | |
| 82 DCHECK(!finish_timer_); | |
| 83 finish_timer_ = std::move(timer); | |
| 84 } | |
| 85 | |
| 86 void DialMediaSinkService::OnDialDeviceEvent( | |
| 87 const DialRegistry::DeviceList& devices) { | |
| 88 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 89 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | |
| 90 << " devices"; | |
| 91 | |
| 92 StartTimer(); | |
| 93 | |
| 94 current_sinks_.clear(); | |
| 95 current_devices_ = devices; | |
| 96 | |
| 97 GetDescriptionService()->GetDeviceDescriptions(devices, | |
| 98 request_context_.get()); | |
| 99 } | |
| 100 | |
| 101 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | |
| 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 103 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | |
| 104 } | |
| 105 | |
| 106 void DialMediaSinkService::OnDeviceDescriptionAvailable( | |
| 107 const DialDeviceData& device_data, | |
| 108 const ParsedDialDeviceDescription& description_data) { | |
| 109 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 110 | |
| 111 if (!base::ContainsValue(current_devices_, device_data)) { | |
| 112 DVLOG(2) << "Device data not found in current device data list..."; | |
| 113 return; | |
| 114 } | |
| 115 | |
| 116 // When use this "sink" within browser, please note it will have a different | |
| 117 // ID when it is sent to the extension, because it derives a different sink ID | |
| 118 // using the given sink ID. | |
| 119 MediaSink sink(description_data.unique_id, description_data.friendly_name, | |
| 120 MediaSink::IconType::GENERIC); | |
| 121 DialSinkExtraData extra_data; | |
| 122 extra_data.app_url = description_data.app_url; | |
| 123 extra_data.model_name = description_data.model_name; | |
| 124 std::string ip_address = device_data.device_description_url().host(); | |
| 125 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { | |
| 126 DVLOG(1) << "Invalid ip_address: " << ip_address; | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | |
| 131 | |
| 132 // Start fetch timer again if device description comes back after | |
| 133 // |finish_timer_| fires. | |
| 134 if (!finish_timer_->IsRunning()) | |
| 135 StartTimer(); | |
| 136 } | |
| 137 | |
| 138 void DialMediaSinkService::OnDeviceDescriptionError( | |
| 139 const DialDeviceData& device, | |
| 140 const std::string& error_message) { | |
| 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 142 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | |
| 143 } | |
| 144 | |
| 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 | |
| OLD | NEW |