| 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/local_discovery/service_discovery_client_mac.h" | 5 #include "chrome/browser/local_discovery/service_discovery_client_mac.h" |
| 6 | 6 |
| 7 #import <arpa/inet.h> | 7 #import <arpa/inet.h> |
| 8 #import <Foundation/Foundation.h> | 8 #import <Foundation/Foundation.h> |
| 9 #import <net/if_dl.h> | 9 #import <net/if_dl.h> |
| 10 | 10 |
| 11 #include "base/debug/dump_without_crashing.h" |
| 11 #include "base/memory/singleton.h" | 12 #include "base/memory/singleton.h" |
| 12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 16 #include "net/base/ip_address_number.h" | 17 #include "net/base/ip_address_number.h" |
| 18 #include "net/base/ip_endpoint.h" |
| 17 | 19 |
| 18 using local_discovery::ServiceWatcherImplMac; | 20 using local_discovery::ServiceWatcherImplMac; |
| 19 using local_discovery::ServiceResolverImplMac; | 21 using local_discovery::ServiceResolverImplMac; |
| 20 | 22 |
| 21 @interface NetServiceBrowserDelegate | 23 @interface NetServiceBrowserDelegate |
| 22 : NSObject<NSNetServiceBrowserDelegate, NSNetServiceDelegate> { | 24 : NSObject<NSNetServiceBrowserDelegate, NSNetServiceDelegate> { |
| 23 @private | 25 @private |
| 24 ServiceWatcherImplMac::NetServiceBrowserContainer* container_; // weak. | 26 ServiceWatcherImplMac::NetServiceBrowserContainer* container_; // weak. |
| 25 base::scoped_nsobject<NSMutableArray> services_; | 27 base::scoped_nsobject<NSMutableArray> services_; |
| 26 } | 28 } |
| (...skipping 300 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 [service_ resolveWithTimeout:kResolveTimeout]; | 329 [service_ resolveWithTimeout:kResolveTimeout]; |
| 328 | 330 |
| 329 VLOG(1) << "ServiceResolverImplMac::NetServiceContainer::" | 331 VLOG(1) << "ServiceResolverImplMac::NetServiceContainer::" |
| 330 << "StartResolvingOnDiscoveryThread: " << service_name_ | 332 << "StartResolvingOnDiscoveryThread: " << service_name_ |
| 331 << ", instance: " << instance << ", type: " << type | 333 << ", instance: " << instance << ", type: " << type |
| 332 << ", domain: " << domain; | 334 << ", domain: " << domain; |
| 333 } | 335 } |
| 334 | 336 |
| 335 void ServiceResolverImplMac::NetServiceContainer::OnResolveUpdate( | 337 void ServiceResolverImplMac::NetServiceContainer::OnResolveUpdate( |
| 336 RequestStatus status) { | 338 RequestStatus status) { |
| 337 if (status == STATUS_SUCCESS) { | 339 if (status != STATUS_SUCCESS) { |
| 338 service_description_.service_name = service_name_; | |
| 339 | |
| 340 for (NSData* address in [service_ addresses]) { | |
| 341 const void* bytes = [address bytes]; | |
| 342 // TODO(justinlin): Handle IPv6 addresses? | |
| 343 if (static_cast<const sockaddr*>(bytes)->sa_family == AF_INET) { | |
| 344 const sockaddr_in* sock = static_cast<const sockaddr_in*>(bytes); | |
| 345 char addr[INET_ADDRSTRLEN]; | |
| 346 inet_ntop(AF_INET, &sock->sin_addr, addr, INET_ADDRSTRLEN); | |
| 347 service_description_.address = | |
| 348 net::HostPortPair(addr, ntohs(sock->sin_port)); | |
| 349 net::ParseIPLiteralToNumber(addr, &service_description_.ip_address); | |
| 350 break; | |
| 351 } | |
| 352 } | |
| 353 | |
| 354 ParseTxtRecord([service_ TXTRecordData], &service_description_.metadata); | |
| 355 | |
| 356 // TODO(justinlin): Implement last_seen. | |
| 357 service_description_.last_seen = base::Time::Now(); | |
| 358 callback_runner_->PostTask( | |
| 359 FROM_HERE, base::Bind(callback_, status, service_description_)); | |
| 360 } else { | |
| 361 callback_runner_->PostTask( | 340 callback_runner_->PostTask( |
| 362 FROM_HERE, base::Bind(callback_, status, ServiceDescription())); | 341 FROM_HERE, base::Bind(callback_, status, ServiceDescription())); |
| 342 return; |
| 363 } | 343 } |
| 344 |
| 345 service_description_.service_name = service_name_; |
| 346 |
| 347 for (NSData* address in [service_ addresses]) { |
| 348 const void* bytes = [address bytes]; |
| 349 int length = [address length]; |
| 350 const sockaddr* socket = static_cast<const sockaddr*>(bytes); |
| 351 net::IPEndPoint end_point; |
| 352 if (end_point.FromSockAddr(socket, length)) { |
| 353 service_description_.address = |
| 354 net::HostPortPair::FromIPEndPoint(end_point); |
| 355 break; |
| 356 } |
| 357 } |
| 358 |
| 359 if (service_description_.address.host().empty()) { |
| 360 NOTREACHED() << service_name_; |
| 361 // Unexpected, but could be a reason for crbug.com/513505 |
| 362 base::debug::DumpWithoutCrashing(); |
| 363 callback_runner_->PostTask( |
| 364 FROM_HERE, |
| 365 base::Bind(callback_, STATUS_KNOWN_NONEXISTENT, ServiceDescription())); |
| 366 return; |
| 367 } |
| 368 |
| 369 ParseTxtRecord([service_ TXTRecordData], &service_description_.metadata); |
| 370 |
| 371 // TODO(justinlin): Implement last_seen. |
| 372 service_description_.last_seen = base::Time::Now(); |
| 373 callback_runner_->PostTask( |
| 374 FROM_HERE, base::Bind(callback_, status, service_description_)); |
| 364 } | 375 } |
| 365 | 376 |
| 366 void ServiceResolverImplMac::NetServiceContainer::SetServiceForTesting( | 377 void ServiceResolverImplMac::NetServiceContainer::SetServiceForTesting( |
| 367 base::scoped_nsobject<NSNetService> service) { | 378 base::scoped_nsobject<NSNetService> service) { |
| 368 service_ = service; | 379 service_ = service; |
| 369 } | 380 } |
| 370 | 381 |
| 371 ServiceResolverImplMac::ServiceResolverImplMac( | 382 ServiceResolverImplMac::ServiceResolverImplMac( |
| 372 const std::string& service_name, | 383 const std::string& service_name, |
| 373 const ServiceResolver::ResolveCompleteCallback& callback, | 384 const ServiceResolver::ResolveCompleteCallback& callback, |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 container_->OnResolveUpdate(local_discovery::ServiceResolver::STATUS_SUCCESS); | 482 container_->OnResolveUpdate(local_discovery::ServiceResolver::STATUS_SUCCESS); |
| 472 } | 483 } |
| 473 | 484 |
| 474 - (void)netService:(NSNetService *)sender | 485 - (void)netService:(NSNetService *)sender |
| 475 didNotResolve:(NSDictionary *)errorDict { | 486 didNotResolve:(NSDictionary *)errorDict { |
| 476 container_->OnResolveUpdate( | 487 container_->OnResolveUpdate( |
| 477 local_discovery::ServiceResolver::STATUS_REQUEST_TIMEOUT); | 488 local_discovery::ServiceResolver::STATUS_REQUEST_TIMEOUT); |
| 478 } | 489 } |
| 479 | 490 |
| 480 @end | 491 @end |
| OLD | NEW |