| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/mdns/dns_sd_registry.h" | 5 #include "chrome/browser/media/router/discovery/mdns/dns_sd_registry.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "chrome/browser/extensions/api/mdns/dns_sd_device_lister.h" | |
| 12 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" | 11 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" |
| 12 #include "chrome/browser/media/router/discovery/mdns/dns_sd_device_lister.h" |
| 13 #include "chrome/common/features.h" | 13 #include "chrome/common/features.h" |
| 14 | 14 |
| 15 using local_discovery::ServiceDiscoveryClient; | 15 using local_discovery::ServiceDiscoveryClient; |
| 16 using local_discovery::ServiceDiscoverySharedClient; | 16 using local_discovery::ServiceDiscoverySharedClient; |
| 17 | 17 |
| 18 namespace extensions { | 18 namespace media_router { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 // Predicate to test if two discovered services have the same service_name. | 21 // Predicate to test if two discovered services have the same service_name. |
| 22 class IsSameServiceName { | 22 class IsSameServiceName { |
| 23 public: | 23 public: |
| 24 explicit IsSameServiceName(const DnsSdService& service) : service_(service) {} | 24 explicit IsSameServiceName(const DnsSdService& service) : service_(service) {} |
| 25 bool operator()(const DnsSdService& other) const { | 25 bool operator()(const DnsSdService& other) const { |
| 26 return service_.service_name == other.service_name; | 26 return service_.service_name == other.service_name; |
| 27 } | 27 } |
| 28 | 28 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 43 | 43 |
| 44 bool DnsSdRegistry::ServiceTypeData::ListenerRemoved() { | 44 bool DnsSdRegistry::ServiceTypeData::ListenerRemoved() { |
| 45 return --ref_count == 0; | 45 return --ref_count == 0; |
| 46 } | 46 } |
| 47 | 47 |
| 48 int DnsSdRegistry::ServiceTypeData::GetListenerCount() { | 48 int DnsSdRegistry::ServiceTypeData::GetListenerCount() { |
| 49 return ref_count; | 49 return ref_count; |
| 50 } | 50 } |
| 51 | 51 |
| 52 bool DnsSdRegistry::ServiceTypeData::UpdateService( | 52 bool DnsSdRegistry::ServiceTypeData::UpdateService( |
| 53 bool added, const DnsSdService& service) { | 53 bool added, |
| 54 DnsSdRegistry::DnsSdServiceList::iterator it = | 54 const DnsSdService& service) { |
| 55 std::find_if(service_list_.begin(), | 55 DnsSdRegistry::DnsSdServiceList::iterator it = std::find_if( |
| 56 service_list_.end(), | 56 service_list_.begin(), service_list_.end(), IsSameServiceName(service)); |
| 57 IsSameServiceName(service)); | |
| 58 // Set to true when a service is updated in or added to the registry. | 57 // Set to true when a service is updated in or added to the registry. |
| 59 bool updated_or_added = added; | 58 bool updated_or_added = added; |
| 60 bool known = (it != service_list_.end()); | 59 bool known = (it != service_list_.end()); |
| 61 if (known) { | 60 if (known) { |
| 62 // If added == true, but we still found the service in our cache, then just | 61 // If added == true, but we still found the service in our cache, then just |
| 63 // update the existing entry, but this should not happen! | 62 // update the existing entry, but this should not happen! |
| 64 DCHECK(!added); | 63 DCHECK(!added); |
| 65 if (*it != service) { | 64 if (*it != service) { |
| 66 *it = service; | 65 *it = service; |
| 67 updated_or_added = true; | 66 updated_or_added = true; |
| 68 } | 67 } |
| 69 } else if (added) { | 68 } else if (added) { |
| 70 service_list_.push_back(service); | 69 service_list_.push_back(service); |
| 71 } | 70 } |
| 72 | 71 |
| 73 VLOG(1) << "UpdateService: " << service.service_name | 72 VLOG(1) << "UpdateService: " << service.service_name << ", added: " << added |
| 74 << ", added: " << added | 73 << ", known: " << known << ", updated or added: " << updated_or_added; |
| 75 << ", known: " << known | |
| 76 << ", updated or added: " << updated_or_added; | |
| 77 return updated_or_added; | 74 return updated_or_added; |
| 78 } | 75 } |
| 79 | 76 |
| 80 bool DnsSdRegistry::ServiceTypeData::RemoveService( | 77 bool DnsSdRegistry::ServiceTypeData::RemoveService( |
| 81 const std::string& service_name) { | 78 const std::string& service_name) { |
| 82 for (DnsSdRegistry::DnsSdServiceList::iterator it = service_list_.begin(); | 79 for (DnsSdRegistry::DnsSdServiceList::iterator it = service_list_.begin(); |
| 83 it != service_list_.end(); ++it) { | 80 it != service_list_.end(); ++it) { |
| 84 if ((*it).service_name == service_name) { | 81 if ((*it).service_name == service_name) { |
| 85 service_list_.erase(it); | 82 service_list_.erase(it); |
| 86 return true; | 83 return true; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 | 171 |
| 175 if (service_data_map_[service_type]->ListenerRemoved()) | 172 if (service_data_map_[service_type]->ListenerRemoved()) |
| 176 service_data_map_.erase(it); | 173 service_data_map_.erase(it); |
| 177 } | 174 } |
| 178 | 175 |
| 179 void DnsSdRegistry::ServiceChanged(const std::string& service_type, | 176 void DnsSdRegistry::ServiceChanged(const std::string& service_type, |
| 180 bool added, | 177 bool added, |
| 181 const DnsSdService& service) { | 178 const DnsSdService& service) { |
| 182 VLOG(1) << "ServiceChanged: service_type: " << service_type | 179 VLOG(1) << "ServiceChanged: service_type: " << service_type |
| 183 << ", known: " << IsRegistered(service_type) | 180 << ", known: " << IsRegistered(service_type) |
| 184 << ", service: " << service.service_name | 181 << ", service: " << service.service_name << ", added: " << added; |
| 185 << ", added: " << added; | |
| 186 if (!IsRegistered(service_type)) { | 182 if (!IsRegistered(service_type)) { |
| 187 return; | 183 return; |
| 188 } | 184 } |
| 189 | 185 |
| 190 bool is_updated = | 186 bool is_updated = |
| 191 service_data_map_[service_type]->UpdateService(added, service); | 187 service_data_map_[service_type]->UpdateService(added, service); |
| 192 VLOG(1) << "ServiceChanged: is_updated: " << is_updated; | 188 VLOG(1) << "ServiceChanged: is_updated: " << is_updated; |
| 193 | 189 |
| 194 if (is_updated) { | 190 if (is_updated) { |
| 195 DispatchApiEvent(service_type); | 191 DispatchApiEvent(service_type); |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 for (auto& observer : observers_) { | 228 for (auto& observer : observers_) { |
| 233 observer.OnDnsSdEvent(service_type, | 229 observer.OnDnsSdEvent(service_type, |
| 234 service_data_map_[service_type]->GetServiceList()); | 230 service_data_map_[service_type]->GetServiceList()); |
| 235 } | 231 } |
| 236 } | 232 } |
| 237 | 233 |
| 238 bool DnsSdRegistry::IsRegistered(const std::string& service_type) { | 234 bool DnsSdRegistry::IsRegistered(const std::string& service_type) { |
| 239 return service_data_map_.find(service_type) != service_data_map_.end(); | 235 return service_data_map_.find(service_type) != service_data_map_.end(); |
| 240 } | 236 } |
| 241 | 237 |
| 242 } // namespace extensions | 238 } // namespace media_router |
| OLD | NEW |