Chromium Code Reviews| 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 | |
| 11 using content::BrowserThread; | |
| 12 | |
| 13 namespace { | |
| 14 // Time interval when media sink service sends sinks to MRP. | |
| 15 const int kFetchCompleteTimeoutSecs = 3; | |
| 16 } | |
| 17 | |
| 18 namespace media_router { | |
| 19 | |
| 20 DialMediaSinkService::DialMediaSinkService( | |
| 21 const OnSinksDiscoveredCallback& callback, | |
| 22 content::BrowserContext* browser_context) | |
| 23 : MediaSinkService(callback), browser_context_(browser_context) { | |
| 24 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 25 auto* profile = Profile::FromBrowserContext(browser_context_); | |
| 26 request_context_ = profile->GetRequestContext(); | |
| 27 | |
| 28 DCHECK(request_context_); | |
| 29 } | |
| 30 | |
| 31 DialMediaSinkService::~DialMediaSinkService() {} | |
| 32 | |
| 33 void DialMediaSinkService::Start() { | |
|
imcheng
2017/04/25 01:40:12
The threading model isn't clear to me; we should c
zhaobin
2017/04/26 18:50:05
Will do PostTask in MediaRouter. Will make DialMed
| |
| 34 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 35 dial_registry()->RegisterObserver(this); | |
| 36 dial_registry()->DiscoverNow(); | |
| 37 } | |
| 38 | |
| 39 void DialMediaSinkService::Stop() { | |
|
imcheng
2017/04/25 01:40:13
Is the plan to have MediaRouter call Stop() when s
mark a. foltz
2017/04/25 21:06:26
I would assume the MR would call Start/Stop depend
zhaobin
2017/04/26 18:50:05
Figure out where to call Stop in https://coderevie
| |
| 40 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 41 dial_registry()->UnregisterObserver(this); | |
| 42 } | |
| 43 | |
| 44 void DialMediaSinkService::AddSinkQuery(MediaSinksObserver* observer) { | |
| 45 // TODO(crbug.com/687375): Implement DIAL sink query. | |
| 46 NOTIMPLEMENTED(); | |
| 47 } | |
| 48 | |
| 49 void DialMediaSinkService::RemoveSinkQuery(MediaSinksObserver* observer) { | |
| 50 // TODO(crbug.com/687375): Implement DIAL sink query. | |
| 51 NOTIMPLEMENTED(); | |
| 52 } | |
| 53 | |
| 54 DialRegistry* DialMediaSinkService::dial_registry() { | |
|
mark a. foltz
2017/04/25 21:06:26
Does this need to be exposed through the API - wha
zhaobin
2017/04/26 18:50:05
For unit tests to override this function...
| |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 56 return DialRegistry::GetInstance(); | |
| 57 } | |
| 58 | |
| 59 DeviceDescriptionService* DialMediaSinkService::GetDescriptionService() { | |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 61 if (!description_service_.get()) { | |
| 62 description_service_.reset(new DeviceDescriptionService( | |
| 63 base::Bind(&DialMediaSinkService::OnDeviceDescriptionAvailable, | |
| 64 base::Unretained(this)), | |
| 65 base::Bind(&DialMediaSinkService::OnDeviceDescriptionError, | |
| 66 base::Unretained(this)))); | |
| 67 } | |
| 68 return description_service_.get(); | |
| 69 } | |
| 70 | |
| 71 void DialMediaSinkService::OnDialDeviceEvent( | |
| 72 const DialRegistry::DeviceList& devices) { | |
| 73 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 74 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() | |
| 75 << " devices"; | |
| 76 | |
| 77 // Add a finish timer. | |
| 78 finish_timer_.reset(new base::OneShotTimer()); | |
| 79 base::TimeDelta finish_delay = | |
| 80 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | |
| 81 finish_timer_->Start(FROM_HERE, finish_delay, this, | |
| 82 &DialMediaSinkService::OnFetchCompleted); | |
| 83 | |
| 84 current_sinks_.clear(); | |
| 85 GetDescriptionService()->GetDeviceDescriptions(devices, request_context_); | |
| 86 } | |
| 87 | |
| 88 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { | |
| 89 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 90 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); | |
| 91 } | |
| 92 | |
| 93 void DialMediaSinkService::OnDeviceDescriptionAvailable( | |
| 94 const DialDeviceData& device_data, | |
| 95 const ParsedDialDeviceDescription& description_data) { | |
| 96 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 97 | |
| 98 MediaSink sink(description_data.unique_id, description_data.friendly_name, | |
| 99 MediaSink::IconType::GENERIC); | |
| 100 DialSinkExtraData extra_data; | |
| 101 extra_data.app_url = description_data.app_url; | |
| 102 extra_data.model_name = description_data.model_name; | |
| 103 std::string ip_address = device_data.device_description_url().host(); | |
| 104 if (!extra_data.ip_address.AssignFromIPLiteral(ip_address)) { | |
| 105 DVLOG(1) << "Invalid ip_address: " << ip_address; | |
| 106 return; | |
| 107 } | |
| 108 | |
| 109 current_sinks_.insert(MediaSinkInternal(sink, extra_data)); | |
| 110 | |
| 111 if (finish_timer_) | |
| 112 return; | |
|
imcheng
2017/04/26 22:52:15
IIUC, it seems safe to reset the 3s timer even if
| |
| 113 | |
| 114 // Start fetch timer again if device description comes back after | |
| 115 // |finish_timer_| fires. | |
| 116 base::TimeDelta finish_delay = | |
| 117 base::TimeDelta::FromSeconds(kFetchCompleteTimeoutSecs); | |
|
imcheng
2017/04/25 01:40:12
Seems like DelayTimer is better suited for this: h
zhaobin
2017/04/26 18:50:05
I think we may want to fire the first timer event
imcheng
2017/04/26 22:52:16
What you are doing here is to set a 3s timer initi
| |
| 118 finish_timer_.reset(new base::OneShotTimer()); | |
| 119 finish_timer_->Start(FROM_HERE, finish_delay, this, | |
| 120 &DialMediaSinkService::OnFetchCompleted); | |
| 121 } | |
| 122 | |
| 123 void DialMediaSinkService::OnDeviceDescriptionError( | |
| 124 const DialDeviceData& device, | |
| 125 const std::string& error_message) { | |
| 126 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 127 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; | |
| 128 } | |
| 129 | |
| 130 void DialMediaSinkService::OnFetchCompleted() { | |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 132 DCHECK(!sink_discovery_callback_.is_null()); | |
| 133 | |
| 134 finish_timer_.reset(); | |
| 135 | |
| 136 auto sinks = current_sinks_; | |
| 137 if (sinks == mrp_sinks_) { | |
| 138 DVLOG(2) << "No update to sink list."; | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 DVLOG(2) << "Send sinks to media router, [size]: " << sinks.size(); | |
| 143 sink_discovery_callback_.Run( | |
|
imcheng
2017/04/25 01:40:12
We should be invoking the callback on the UI threa
zhaobin
2017/04/26 18:50:05
Will do that in MediaRouter...
| |
| 144 std::vector<MediaSinkInternal>(sinks.begin(), sinks.end())); | |
| 145 mrp_sinks_ = std::move(sinks); | |
| 146 } | |
| 147 | |
| 148 } // namespace media_router | |
| OLD | NEW |