| 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/utility/local_discovery/service_discovery_message_handler.h" | 5 #include "chrome/utility/local_discovery/service_discovery_message_handler.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "chrome/common/local_discovery/local_discovery_messages.h" | 10 #include "chrome/common/local_discovery/local_discovery_messages.h" |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 } | 123 } |
| 124 | 124 |
| 125 void SendLocalDomainResolved(uint64 id, bool success, | 125 void SendLocalDomainResolved(uint64 id, bool success, |
| 126 const net::IPAddressNumber& address_ipv4, | 126 const net::IPAddressNumber& address_ipv4, |
| 127 const net::IPAddressNumber& address_ipv6) { | 127 const net::IPAddressNumber& address_ipv6) { |
| 128 content::UtilityThread::Get()->Send( | 128 content::UtilityThread::Get()->Send( |
| 129 new LocalDiscoveryHostMsg_LocalDomainResolverCallback( | 129 new LocalDiscoveryHostMsg_LocalDomainResolverCallback( |
| 130 id, success, address_ipv4, address_ipv6)); | 130 id, success, address_ipv4, address_ipv6)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 |
| 134 std::string WatcherUpdateToString(ServiceWatcher::UpdateType update) { |
| 135 switch (update) { |
| 136 case ServiceWatcher::UPDATE_ADDED: |
| 137 return "UPDATE_ADDED"; |
| 138 case ServiceWatcher::UPDATE_CHANGED: |
| 139 return "UPDATE_CHANGED"; |
| 140 case ServiceWatcher::UPDATE_REMOVED: |
| 141 return "UPDATE_REMOVED"; |
| 142 case ServiceWatcher::UPDATE_INVALIDATED: |
| 143 return "UPDATE_INVALIDATED"; |
| 144 } |
| 145 return "Unknown Update"; |
| 146 } |
| 147 |
| 148 std::string ResolverStatusToString(ServiceResolver::RequestStatus status) { |
| 149 switch (status) { |
| 150 case ServiceResolver::STATUS_SUCCESS: |
| 151 return "STATUS_SUCESS"; |
| 152 case ServiceResolver::STATUS_REQUEST_TIMEOUT: |
| 153 return "STATUS_REQUEST_TIMEOUT"; |
| 154 case ServiceResolver::STATUS_KNOWN_NONEXISTENT: |
| 155 return "STATUS_KNOWN_NONEXISTENT"; |
| 156 } |
| 157 return "Unknown Status"; |
| 158 } |
| 159 |
| 133 } // namespace | 160 } // namespace |
| 134 | 161 |
| 135 ServiceDiscoveryMessageHandler::ServiceDiscoveryMessageHandler() { | 162 ServiceDiscoveryMessageHandler::ServiceDiscoveryMessageHandler() { |
| 136 } | 163 } |
| 137 | 164 |
| 138 ServiceDiscoveryMessageHandler::~ServiceDiscoveryMessageHandler() { | 165 ServiceDiscoveryMessageHandler::~ServiceDiscoveryMessageHandler() { |
| 139 DCHECK(!discovery_thread_); | 166 DCHECK(!discovery_thread_); |
| 140 } | 167 } |
| 141 | 168 |
| 142 void ServiceDiscoveryMessageHandler::PreSandboxStartup() { | 169 void ServiceDiscoveryMessageHandler::PreSandboxStartup() { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 void ServiceDiscoveryMessageHandler::OnDestroyLocalDomainResolver(uint64 id) { | 288 void ServiceDiscoveryMessageHandler::OnDestroyLocalDomainResolver(uint64 id) { |
| 262 PostTask(FROM_HERE, | 289 PostTask(FROM_HERE, |
| 263 base::Bind( | 290 base::Bind( |
| 264 &ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver, | 291 &ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver, |
| 265 base::Unretained(this), id)); | 292 base::Unretained(this), id)); |
| 266 } | 293 } |
| 267 | 294 |
| 268 void ServiceDiscoveryMessageHandler::StartWatcher( | 295 void ServiceDiscoveryMessageHandler::StartWatcher( |
| 269 uint64 id, | 296 uint64 id, |
| 270 const std::string& service_type) { | 297 const std::string& service_type) { |
| 298 VLOG(1) << "StartWatcher with id " << id; |
| 271 if (!service_discovery_client_) | 299 if (!service_discovery_client_) |
| 272 return; | 300 return; |
| 273 DCHECK(!ContainsKey(service_watchers_, id)); | 301 DCHECK(!ContainsKey(service_watchers_, id)); |
| 274 scoped_ptr<ServiceWatcher> watcher( | 302 scoped_ptr<ServiceWatcher> watcher( |
| 275 service_discovery_client_->CreateServiceWatcher( | 303 service_discovery_client_->CreateServiceWatcher( |
| 276 service_type, | 304 service_type, |
| 277 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceUpdated, | 305 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceUpdated, |
| 278 base::Unretained(this), id))); | 306 base::Unretained(this), id))); |
| 279 watcher->Start(); | 307 watcher->Start(); |
| 280 service_watchers_[id].reset(watcher.release()); | 308 service_watchers_[id].reset(watcher.release()); |
| 281 } | 309 } |
| 282 | 310 |
| 283 void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id, | 311 void ServiceDiscoveryMessageHandler::DiscoverServices(uint64 id, |
| 284 bool force_update) { | 312 bool force_update) { |
| 313 VLOG(1) << "DiscoverServices with id " << id; |
| 285 if (!service_discovery_client_) | 314 if (!service_discovery_client_) |
| 286 return; | 315 return; |
| 287 DCHECK(ContainsKey(service_watchers_, id)); | 316 DCHECK(ContainsKey(service_watchers_, id)); |
| 288 service_watchers_[id]->DiscoverNewServices(force_update); | 317 service_watchers_[id]->DiscoverNewServices(force_update); |
| 289 } | 318 } |
| 290 | 319 |
| 291 void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id) { | 320 void ServiceDiscoveryMessageHandler::DestroyWatcher(uint64 id) { |
| 321 VLOG(1) << "DestoryWatcher with id " << id; |
| 292 if (!service_discovery_client_) | 322 if (!service_discovery_client_) |
| 293 return; | 323 return; |
| 294 service_watchers_.erase(id); | 324 service_watchers_.erase(id); |
| 295 } | 325 } |
| 296 | 326 |
| 297 void ServiceDiscoveryMessageHandler::ResolveService( | 327 void ServiceDiscoveryMessageHandler::ResolveService( |
| 298 uint64 id, | 328 uint64 id, |
| 299 const std::string& service_name) { | 329 const std::string& service_name) { |
| 330 VLOG(1) << "ResolveService with id " << id; |
| 300 if (!service_discovery_client_) | 331 if (!service_discovery_client_) |
| 301 return; | 332 return; |
| 302 DCHECK(!ContainsKey(service_resolvers_, id)); | 333 DCHECK(!ContainsKey(service_resolvers_, id)); |
| 303 scoped_ptr<ServiceResolver> resolver( | 334 scoped_ptr<ServiceResolver> resolver( |
| 304 service_discovery_client_->CreateServiceResolver( | 335 service_discovery_client_->CreateServiceResolver( |
| 305 service_name, | 336 service_name, |
| 306 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceResolved, | 337 base::Bind(&ServiceDiscoveryMessageHandler::OnServiceResolved, |
| 307 base::Unretained(this), id))); | 338 base::Unretained(this), id))); |
| 308 resolver->StartResolving(); | 339 resolver->StartResolving(); |
| 309 service_resolvers_[id].reset(resolver.release()); | 340 service_resolvers_[id].reset(resolver.release()); |
| 310 } | 341 } |
| 311 | 342 |
| 312 void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id) { | 343 void ServiceDiscoveryMessageHandler::DestroyResolver(uint64 id) { |
| 344 VLOG(1) << "DestroyResolver with id " << id; |
| 313 if (!service_discovery_client_) | 345 if (!service_discovery_client_) |
| 314 return; | 346 return; |
| 315 service_resolvers_.erase(id); | 347 service_resolvers_.erase(id); |
| 316 } | 348 } |
| 317 | 349 |
| 318 void ServiceDiscoveryMessageHandler::ResolveLocalDomain( | 350 void ServiceDiscoveryMessageHandler::ResolveLocalDomain( |
| 319 uint64 id, | 351 uint64 id, |
| 320 const std::string& domain, | 352 const std::string& domain, |
| 321 net::AddressFamily address_family) { | 353 net::AddressFamily address_family) { |
| 354 VLOG(1) << "ResolveLocalDomain with id " << id; |
| 322 if (!service_discovery_client_) | 355 if (!service_discovery_client_) |
| 323 return; | 356 return; |
| 324 DCHECK(!ContainsKey(local_domain_resolvers_, id)); | 357 DCHECK(!ContainsKey(local_domain_resolvers_, id)); |
| 325 scoped_ptr<LocalDomainResolver> resolver( | 358 scoped_ptr<LocalDomainResolver> resolver( |
| 326 service_discovery_client_->CreateLocalDomainResolver( | 359 service_discovery_client_->CreateLocalDomainResolver( |
| 327 domain, address_family, | 360 domain, address_family, |
| 328 base::Bind(&ServiceDiscoveryMessageHandler::OnLocalDomainResolved, | 361 base::Bind(&ServiceDiscoveryMessageHandler::OnLocalDomainResolved, |
| 329 base::Unretained(this), id))); | 362 base::Unretained(this), id))); |
| 330 resolver->Start(); | 363 resolver->Start(); |
| 331 local_domain_resolvers_[id].reset(resolver.release()); | 364 local_domain_resolvers_[id].reset(resolver.release()); |
| 332 } | 365 } |
| 333 | 366 |
| 334 void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id) { | 367 void ServiceDiscoveryMessageHandler::DestroyLocalDomainResolver(uint64 id) { |
| 368 VLOG(1) << "DestroyLocalDomainResolver with id " << id; |
| 335 if (!service_discovery_client_) | 369 if (!service_discovery_client_) |
| 336 return; | 370 return; |
| 337 local_domain_resolvers_.erase(id); | 371 local_domain_resolvers_.erase(id); |
| 338 } | 372 } |
| 339 | 373 |
| 340 void ServiceDiscoveryMessageHandler::ShutdownLocalDiscovery() { | 374 void ServiceDiscoveryMessageHandler::ShutdownLocalDiscovery() { |
| 375 VLOG(1) << "ShutdownLocalDiscovery"; |
| 341 discovery_task_runner_->PostTask( | 376 discovery_task_runner_->PostTask( |
| 342 FROM_HERE, | 377 FROM_HERE, |
| 343 base::Bind(&ServiceDiscoveryMessageHandler::ShutdownOnIOThread, | 378 base::Bind(&ServiceDiscoveryMessageHandler::ShutdownOnIOThread, |
| 344 base::Unretained(this))); | 379 base::Unretained(this))); |
| 345 | 380 |
| 346 // This will wait for message loop to drain, so ShutdownOnIOThread will | 381 // This will wait for message loop to drain, so ShutdownOnIOThread will |
| 347 // definitely be called. | 382 // definitely be called. |
| 348 discovery_thread_.reset(); | 383 discovery_thread_.reset(); |
| 349 } | 384 } |
| 350 | 385 |
| 351 void ServiceDiscoveryMessageHandler::ShutdownOnIOThread() { | 386 void ServiceDiscoveryMessageHandler::ShutdownOnIOThread() { |
| 352 service_watchers_.clear(); | 387 service_watchers_.clear(); |
| 353 service_resolvers_.clear(); | 388 service_resolvers_.clear(); |
| 354 local_domain_resolvers_.clear(); | 389 local_domain_resolvers_.clear(); |
| 355 | 390 |
| 356 service_discovery_client_.reset(); | 391 service_discovery_client_.reset(); |
| 357 mdns_client_.reset(); | 392 mdns_client_.reset(); |
| 358 } | 393 } |
| 359 | 394 |
| 360 void ServiceDiscoveryMessageHandler::OnServiceUpdated( | 395 void ServiceDiscoveryMessageHandler::OnServiceUpdated( |
| 361 uint64 id, | 396 uint64 id, |
| 362 ServiceWatcher::UpdateType update, | 397 ServiceWatcher::UpdateType update, |
| 363 const std::string& name) { | 398 const std::string& name) { |
| 399 VLOG(1) << "OnServiceUpdated with id " << id |
| 400 << WatcherUpdateToString(update); |
| 364 DCHECK(service_discovery_client_); | 401 DCHECK(service_discovery_client_); |
| 365 utility_task_runner_->PostTask(FROM_HERE, | 402 utility_task_runner_->PostTask(FROM_HERE, |
| 366 base::Bind(&SendServiceUpdated, id, update, name)); | 403 base::Bind(&SendServiceUpdated, id, update, name)); |
| 367 } | 404 } |
| 368 | 405 |
| 369 void ServiceDiscoveryMessageHandler::OnServiceResolved( | 406 void ServiceDiscoveryMessageHandler::OnServiceResolved( |
| 370 uint64 id, | 407 uint64 id, |
| 371 ServiceResolver::RequestStatus status, | 408 ServiceResolver::RequestStatus status, |
| 372 const ServiceDescription& description) { | 409 const ServiceDescription& description) { |
| 410 VLOG(1) << "OnServiceResolved with id " << id << " and status " |
| 411 << ResolverStatusToString(status); |
| 412 |
| 373 DCHECK(service_discovery_client_); | 413 DCHECK(service_discovery_client_); |
| 374 utility_task_runner_->PostTask(FROM_HERE, | 414 utility_task_runner_->PostTask(FROM_HERE, |
| 375 base::Bind(&SendServiceResolved, id, status, description)); | 415 base::Bind(&SendServiceResolved, id, status, description)); |
| 376 } | 416 } |
| 377 | 417 |
| 378 void ServiceDiscoveryMessageHandler::OnLocalDomainResolved( | 418 void ServiceDiscoveryMessageHandler::OnLocalDomainResolved( |
| 379 uint64 id, | 419 uint64 id, |
| 380 bool success, | 420 bool success, |
| 381 const net::IPAddressNumber& address_ipv4, | 421 const net::IPAddressNumber& address_ipv4, |
| 382 const net::IPAddressNumber& address_ipv6) { | 422 const net::IPAddressNumber& address_ipv6) { |
| 423 VLOG(1) << "OnLocalDomainResolved with id " << id; |
| 424 |
| 425 if (!address_ipv4.empty()) |
| 426 VLOG(1) << "Local comain callback has valid ipv4 address with id " << id; |
| 427 if (!address_ipv6.empty()) |
| 428 VLOG(1) << "Local comain callback has valid ipv6 address with id " << id; |
| 429 |
| 383 DCHECK(service_discovery_client_); | 430 DCHECK(service_discovery_client_); |
| 384 utility_task_runner_->PostTask(FROM_HERE, base::Bind(&SendLocalDomainResolved, | 431 utility_task_runner_->PostTask(FROM_HERE, base::Bind(&SendLocalDomainResolved, |
| 385 id, success, | 432 id, success, |
| 386 address_ipv4, | 433 address_ipv4, |
| 387 address_ipv6)); | 434 address_ipv6)); |
| 388 } | 435 } |
| 389 | 436 |
| 390 | 437 |
| 391 } // namespace local_discovery | 438 } // namespace local_discovery |
| OLD | NEW |