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/extensions/api/mdns/dns_sd_registry.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "chrome/browser/extensions/api/mdns/dns_sd_device_lister.h" | 8 #include "chrome/browser/extensions/api/mdns/dns_sd_device_lister.h" |
9 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" | 9 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" |
10 | 10 |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 } | 46 } |
47 | 47 |
48 bool DnsSdRegistry::ServiceTypeData::UpdateService( | 48 bool DnsSdRegistry::ServiceTypeData::UpdateService( |
49 bool added, const DnsSdService& service) { | 49 bool added, const DnsSdService& service) { |
50 DnsSdRegistry::DnsSdServiceList::iterator it = | 50 DnsSdRegistry::DnsSdServiceList::iterator it = |
51 std::find_if(service_list_.begin(), | 51 std::find_if(service_list_.begin(), |
52 service_list_.end(), | 52 service_list_.end(), |
53 IsSameServiceName(service)); | 53 IsSameServiceName(service)); |
54 // Set to true when a service is updated in or added to the registry. | 54 // Set to true when a service is updated in or added to the registry. |
55 bool updated_or_added = added; | 55 bool updated_or_added = added; |
56 if (it != service_list_.end()) { | 56 bool known = (it != service_list_.end()); |
| 57 if (known) { |
57 // If added == true, but we still found the service in our cache, then just | 58 // If added == true, but we still found the service in our cache, then just |
58 // update the existing entry, but this should not happen! | 59 // update the existing entry, but this should not happen! |
59 DCHECK(!added); | 60 DCHECK(!added); |
60 if (*it != service) { | 61 if (*it != service) { |
61 *it = service; | 62 *it = service; |
62 updated_or_added = true; | 63 updated_or_added = true; |
63 } | 64 } |
64 } else if (added) { | 65 } else if (added) { |
65 service_list_.push_back(service); | 66 service_list_.push_back(service); |
66 } | 67 } |
| 68 |
| 69 VLOG(1) << "UpdateService: " << service.service_name |
| 70 << ", added: " << added |
| 71 << ", known: " << known |
| 72 << ", updated or added: " << updated_or_added; |
67 return updated_or_added; | 73 return updated_or_added; |
68 }; | 74 }; |
69 | 75 |
70 bool DnsSdRegistry::ServiceTypeData::RemoveService( | 76 bool DnsSdRegistry::ServiceTypeData::RemoveService( |
71 const std::string& service_name) { | 77 const std::string& service_name) { |
72 for (DnsSdRegistry::DnsSdServiceList::iterator it = service_list_.begin(); | 78 for (DnsSdRegistry::DnsSdServiceList::iterator it = service_list_.begin(); |
73 it != service_list_.end(); ++it) { | 79 it != service_list_.end(); ++it) { |
74 if ((*it).service_name == service_name) { | 80 if ((*it).service_name == service_name) { |
75 service_list_.erase(it); | 81 service_list_.erase(it); |
76 return true; | 82 return true; |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 } | 119 } |
114 | 120 |
115 DnsSdDeviceLister* DnsSdRegistry::CreateDnsSdDeviceLister( | 121 DnsSdDeviceLister* DnsSdRegistry::CreateDnsSdDeviceLister( |
116 DnsSdDelegate* delegate, | 122 DnsSdDelegate* delegate, |
117 const std::string& service_type, | 123 const std::string& service_type, |
118 local_discovery::ServiceDiscoverySharedClient* discovery_client) { | 124 local_discovery::ServiceDiscoverySharedClient* discovery_client) { |
119 return new DnsSdDeviceLister(discovery_client, delegate, service_type); | 125 return new DnsSdDeviceLister(discovery_client, delegate, service_type); |
120 } | 126 } |
121 | 127 |
122 void DnsSdRegistry::RegisterDnsSdListener(std::string service_type) { | 128 void DnsSdRegistry::RegisterDnsSdListener(std::string service_type) { |
| 129 VLOG(1) << "RegisterDnsSdListener: " << service_type |
| 130 << ", registered: " << IsRegistered(service_type); |
123 if (service_type.empty()) | 131 if (service_type.empty()) |
124 return; | 132 return; |
125 | 133 |
126 if (IsRegistered(service_type)) { | 134 if (IsRegistered(service_type)) { |
127 service_data_map_[service_type]->ListenerAdded(); | 135 service_data_map_[service_type]->ListenerAdded(); |
128 DispatchApiEvent(service_type); | 136 DispatchApiEvent(service_type); |
129 return; | 137 return; |
130 } | 138 } |
131 | 139 |
132 scoped_ptr<DnsSdDeviceLister> dns_sd_device_lister(CreateDnsSdDeviceLister( | 140 scoped_ptr<DnsSdDeviceLister> dns_sd_device_lister(CreateDnsSdDeviceLister( |
133 this, service_type, service_discovery_client_)); | 141 this, service_type, service_discovery_client_)); |
134 dns_sd_device_lister->Discover(false); | 142 dns_sd_device_lister->Discover(false); |
135 linked_ptr<ServiceTypeData> service_type_data( | 143 linked_ptr<ServiceTypeData> service_type_data( |
136 new ServiceTypeData(dns_sd_device_lister.Pass())); | 144 new ServiceTypeData(dns_sd_device_lister.Pass())); |
137 service_data_map_[service_type] = service_type_data; | 145 service_data_map_[service_type] = service_type_data; |
138 DispatchApiEvent(service_type); | 146 DispatchApiEvent(service_type); |
139 } | 147 } |
140 | 148 |
141 void DnsSdRegistry::UnregisterDnsSdListener(std::string service_type) { | 149 void DnsSdRegistry::UnregisterDnsSdListener(std::string service_type) { |
| 150 VLOG(1) << "UnregisterDnsSdListener: " << service_type; |
142 DnsSdRegistry::DnsSdServiceTypeDataMap::iterator it = | 151 DnsSdRegistry::DnsSdServiceTypeDataMap::iterator it = |
143 service_data_map_.find(service_type); | 152 service_data_map_.find(service_type); |
144 if (it == service_data_map_.end()) | 153 if (it == service_data_map_.end()) |
145 return; | 154 return; |
146 | 155 |
147 if (service_data_map_[service_type]->ListenerRemoved()) | 156 if (service_data_map_[service_type]->ListenerRemoved()) |
148 service_data_map_.erase(it); | 157 service_data_map_.erase(it); |
149 } | 158 } |
150 | 159 |
151 void DnsSdRegistry::ServiceChanged(const std::string& service_type, | 160 void DnsSdRegistry::ServiceChanged(const std::string& service_type, |
152 bool added, | 161 bool added, |
153 const DnsSdService& service) { | 162 const DnsSdService& service) { |
154 if (!IsRegistered(service_type)) | 163 VLOG(1) << "ServiceChanged: service_type: " << service_type |
| 164 << ", known: " << IsRegistered(service_type) |
| 165 << ", service: " << service.service_name |
| 166 << ", added: " << added; |
| 167 if (!IsRegistered(service_type)) { |
155 return; | 168 return; |
| 169 } |
156 | 170 |
157 VLOG(1) << "Service changed: " << service.service_name; | 171 bool is_updated = |
158 if (service_data_map_[service_type]->UpdateService(added, service)) { | 172 service_data_map_[service_type]->UpdateService(added, service); |
| 173 VLOG(1) << "ServiceChanged: is_updated: " << is_updated; |
| 174 |
| 175 if (is_updated) { |
159 DispatchApiEvent(service_type); | 176 DispatchApiEvent(service_type); |
160 } else { | |
161 VLOG(1) << "Failed to find existing service to update: " | |
162 << service.service_name; | |
163 } | 177 } |
164 } | 178 } |
165 | 179 |
166 void DnsSdRegistry::ServiceRemoved(const std::string& service_type, | 180 void DnsSdRegistry::ServiceRemoved(const std::string& service_type, |
167 const std::string& service_name) { | 181 const std::string& service_name) { |
168 if (!IsRegistered(service_type)) | 182 VLOG(1) << "ServiceRemoved: service_type: " << service_type |
| 183 << ", known: " << IsRegistered(service_type) |
| 184 << ", service: " << service_name; |
| 185 if (!IsRegistered(service_type)) { |
169 return; | 186 return; |
| 187 } |
170 | 188 |
171 VLOG(1) << "Removing service: " << service_name; | 189 bool is_removed = |
172 if (service_data_map_[service_type]->RemoveService(service_name)) { | 190 service_data_map_[service_type]->RemoveService(service_name); |
| 191 VLOG(1) << "ServiceRemoved: is_removed: " << is_removed; |
| 192 |
| 193 if (is_removed) |
173 DispatchApiEvent(service_type); | 194 DispatchApiEvent(service_type); |
174 } else { | |
175 VLOG(1) << "Failed to remove service: " << service_name; | |
176 } | |
177 } | 195 } |
178 | 196 |
179 void DnsSdRegistry::ServicesFlushed(const std::string& service_type) { | 197 void DnsSdRegistry::ServicesFlushed(const std::string& service_type) { |
180 if (!IsRegistered(service_type)) | 198 VLOG(1) << "ServicesFlushed: service_type: " << service_type |
| 199 << ", known: " << IsRegistered(service_type); |
| 200 if (!IsRegistered(service_type)) { |
181 return; | 201 return; |
| 202 } |
182 | 203 |
183 if (service_data_map_[service_type]->ClearServices()) | 204 bool is_cleared = service_data_map_[service_type]->ClearServices(); |
| 205 VLOG(1) << "ServicesFlushed: is_cleared: " << is_cleared; |
| 206 |
| 207 if (is_cleared) |
184 DispatchApiEvent(service_type); | 208 DispatchApiEvent(service_type); |
185 } | 209 } |
186 | 210 |
187 void DnsSdRegistry::DispatchApiEvent(const std::string& service_type) { | 211 void DnsSdRegistry::DispatchApiEvent(const std::string& service_type) { |
188 // TODO(justinlin): Make this MaybeDispatchApiEvent instead and dispatch if a | 212 // TODO(justinlin): Make this MaybeDispatchApiEvent instead and dispatch if a |
189 // dirty bit is set. | 213 // dirty bit is set. |
| 214 VLOG(1) << "DispatchApiEvent: service_type: " << service_type; |
190 FOR_EACH_OBSERVER(DnsSdObserver, observers_, OnDnsSdEvent( | 215 FOR_EACH_OBSERVER(DnsSdObserver, observers_, OnDnsSdEvent( |
191 service_type, service_data_map_[service_type]->GetServiceList())); | 216 service_type, service_data_map_[service_type]->GetServiceList())); |
192 } | 217 } |
193 | 218 |
194 bool DnsSdRegistry::IsRegistered(const std::string& service_type) { | 219 bool DnsSdRegistry::IsRegistered(const std::string& service_type) { |
195 return service_data_map_.find(service_type) != service_data_map_.end(); | 220 return service_data_map_.find(service_type) != service_data_map_.end(); |
196 } | 221 } |
197 | 222 |
198 } // namespace extensions | 223 } // namespace extensions |
OLD | NEW |