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/dial_media_sink_service.h" |
| 6 |
| 7 #include "chrome/browser/extensions/api/dial/device_description_fetcher.h" |
| 8 #include "chrome/browser/extensions/api/dial/dial_api.h" |
| 9 #include "chrome/browser/extensions/api/dial/dial_api_factory.h" |
| 10 #include "chrome/browser/extensions/api/dial/dial_device_data.h" |
| 11 #include "chrome/browser/media/router/media_sinks_observer.h" |
| 12 #include "chrome/browser/media/router/mojo/media_router_mojo_impl.h" |
| 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "content/public/browser/browser_thread.h" |
| 15 |
| 16 using base::Time; |
| 17 using base::TimeDelta; |
| 18 using content::BrowserThread; |
| 19 |
| 20 namespace media_router { |
| 21 |
| 22 DialMediaSinkService::DialMediaSinkService( |
| 23 const OnSinksDiscoveredCallback& callback, |
| 24 content::BrowserContext* browser_context) |
| 25 : MediaSinkService(callback), |
| 26 dial_registry_(nullptr), |
| 27 browser_context_(browser_context) { |
| 28 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 29 auto* profile = Profile::FromBrowserContext(browser_context_); |
| 30 dial_api_ = |
| 31 extensions::DialAPIFactory::GetInstance()->GetForBrowserContext(profile); |
| 32 request_context_ = profile->GetRequestContext(); |
| 33 |
| 34 DCHECK(dial_api_); |
| 35 DCHECK(request_context_); |
| 36 } |
| 37 |
| 38 DialMediaSinkService::~DialMediaSinkService() { |
| 39 content::BrowserThread::PostTask( |
| 40 content::BrowserThread::IO, FROM_HERE, |
| 41 base::Bind(&DialRegistry::UnregisterObserver, |
| 42 base::Unretained(dial_registry_), this)); |
| 43 } |
| 44 |
| 45 void DialMediaSinkService::Start() { |
| 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 47 dial_registry()->RegisterObserver(this); |
| 48 } |
| 49 |
| 50 void DialMediaSinkService::AddSinkQuery(MediaSinksObserver* observer) { |
| 51 NOTIMPLEMENTED(); |
| 52 } |
| 53 |
| 54 void DialMediaSinkService::RemoveSinkQuery(MediaSinksObserver* observer) { |
| 55 NOTIMPLEMENTED(); |
| 56 } |
| 57 |
| 58 DialMediaSinkService::DialRegistry* DialMediaSinkService::dial_registry() { |
| 59 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 60 dial_registry_ = dial_api_->dial_registry(); |
| 61 return dial_registry_; |
| 62 } |
| 63 |
| 64 DeviceDescriptionService* DialMediaSinkService::description_service() { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 66 if (!description_service_.get()) { |
| 67 description_service_.reset(new DeviceDescriptionService(this)); |
| 68 } |
| 69 return description_service_.get(); |
| 70 } |
| 71 |
| 72 void DialMediaSinkService::OnDialDeviceEvent( |
| 73 const DialRegistry::DeviceList& devices) { |
| 74 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 75 DVLOG(2) << "DialMediaSinkService::OnDialDeviceEvent found " << devices.size() |
| 76 << " devices"; |
| 77 |
| 78 std::set<std::string> device_labels; |
| 79 for (const auto& device : devices) |
| 80 device_labels.insert(device.label()); |
| 81 |
| 82 // Abort previous resolution. Always follow the latest device list. |
| 83 for (const auto& pending_label : pending_device_labels_) { |
| 84 if (device_labels.find(pending_label) == device_labels.end()) |
| 85 description_service()->MayStopDeviceDescriptionFetching(pending_label); |
| 86 } |
| 87 |
| 88 pending_device_labels_ = device_labels; |
| 89 dial_sinks_.clear(); |
| 90 |
| 91 for (const auto& device : devices) { |
| 92 DialDeviceDescription description; |
| 93 if (description_service()->GetDeviceDescription(device, request_context_, |
| 94 &description)) |
| 95 OnDeviceDescriptionAvailable(device.label(), description); |
| 96 } |
| 97 } |
| 98 |
| 99 void DialMediaSinkService::OnDialError(DialRegistry::DialErrorCode type) { |
| 100 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 101 DVLOG(2) << "OnDialError [DialErrorCode]: " << static_cast<int>(type); |
| 102 } |
| 103 |
| 104 void DialMediaSinkService::OnDeviceDescriptionAvailable( |
| 105 const std::string& device_id, |
| 106 const DialDeviceDescription& description) { |
| 107 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 108 dial_sinks_.insert(std::make_pair( |
| 109 description.unique_id, |
| 110 base::MakeUnique<MediaSink>(description.unique_id, |
| 111 description.friendly_name, MediaSink::CAST))); |
| 112 OnDeviceDescription(device_id); |
| 113 } |
| 114 |
| 115 void DialMediaSinkService::OnDeviceDescriptionFetchError( |
| 116 const std::string& device_label, |
| 117 const std::string& error_message) { |
| 118 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 119 DVLOG(2) << "OnDescriptionFetchesError [message]: " << error_message; |
| 120 OnDeviceDescription(device_label); |
| 121 } |
| 122 |
| 123 void DialMediaSinkService::OnDeviceDescription( |
| 124 const std::string& device_label) { |
| 125 pending_device_labels_.erase(device_label); |
| 126 if (pending_device_labels_.empty()) |
| 127 FetchCompleted(); |
| 128 } |
| 129 |
| 130 void DialMediaSinkService::FetchCompleted() { |
| 131 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 132 DCHECK(!sink_discovery_callback_.is_null()); |
| 133 |
| 134 std::vector<MediaSink> sinks; |
| 135 for (auto& it : dial_sinks_) |
| 136 sinks.push_back(*it.second); |
| 137 |
| 138 sink_discovery_callback_.Run(sinks); |
| 139 dial_sinks_.clear(); |
| 140 } |
| 141 |
| 142 } // namespace media_router |
OLD | NEW |