OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/privet_http_asynchronous_factory_impl.h
" | 5 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory_impl.h
" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "chrome/browser/local_discovery/endpoint_resolver.h" |
8 #include "base/command_line.h" | |
9 #include "base/debug/dump_without_crashing.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/local_discovery/privet_http_impl.h" | 8 #include "chrome/browser/local_discovery/privet_http_impl.h" |
12 #include "chrome/browser/local_discovery/service_discovery_shared_client.h" | |
13 #include "chrome/common/chrome_switches.h" | |
14 #include "net/base/net_util.h" | |
15 | 9 |
16 namespace local_discovery { | 10 namespace local_discovery { |
17 | 11 |
18 PrivetHTTPAsynchronousFactoryImpl::PrivetHTTPAsynchronousFactoryImpl( | 12 PrivetHTTPAsynchronousFactoryImpl::PrivetHTTPAsynchronousFactoryImpl( |
19 net::URLRequestContextGetter* request_context) | 13 net::URLRequestContextGetter* request_context) |
20 : request_context_(request_context) { | 14 : request_context_(request_context) { |
21 } | 15 } |
22 | 16 |
23 PrivetHTTPAsynchronousFactoryImpl::~PrivetHTTPAsynchronousFactoryImpl() { | 17 PrivetHTTPAsynchronousFactoryImpl::~PrivetHTTPAsynchronousFactoryImpl() { |
24 } | 18 } |
25 | 19 |
26 scoped_ptr<PrivetHTTPResolution> | 20 scoped_ptr<PrivetHTTPResolution> |
27 PrivetHTTPAsynchronousFactoryImpl::CreatePrivetHTTP( | 21 PrivetHTTPAsynchronousFactoryImpl::CreatePrivetHTTP( |
28 const std::string& service_name) { | 22 const std::string& service_name) { |
29 return scoped_ptr<PrivetHTTPResolution>( | 23 return scoped_ptr<PrivetHTTPResolution>( |
30 new ResolutionImpl(service_name, request_context_.get())); | 24 new ResolutionImpl(service_name, request_context_.get())); |
31 } | 25 } |
32 | 26 |
33 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolutionImpl( | 27 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolutionImpl( |
34 const std::string& service_name, | 28 const std::string& service_name, |
35 net::URLRequestContextGetter* request_context) | 29 net::URLRequestContextGetter* request_context) |
36 : name_(service_name), request_context_(request_context) { | 30 : name_(service_name), |
37 service_discovery_client_ = ServiceDiscoverySharedClient::GetInstance(); | 31 request_context_(request_context), |
38 } | 32 endpoint_resolver_(new EndpointResolver()) {} |
39 | 33 |
40 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() { | 34 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() { |
41 } | 35 } |
42 | 36 |
43 const std::string& | 37 const std::string& |
44 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() { | 38 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() { |
45 return name_; | 39 return name_; |
46 } | 40 } |
47 | 41 |
48 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start( | 42 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start( |
49 const ResultCallback& callback) { | 43 const ResultCallback& callback) { |
50 service_resolver_ = service_discovery_client_->CreateServiceResolver( | 44 endpoint_resolver_->Start(name_, |
51 name_, base::Bind(&ResolutionImpl::ServiceResolveComplete, | 45 base::Bind(&ResolutionImpl::ResolveComplete, |
52 base::Unretained(this), callback)); | 46 base::Unretained(this), callback)); |
53 service_resolver_->StartResolving(); | |
54 } | |
55 | |
56 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ServiceResolveComplete( | |
57 const ResultCallback& callback, | |
58 ServiceResolver::RequestStatus result, | |
59 const ServiceDescription& description) { | |
60 if (result != ServiceResolver::STATUS_SUCCESS) | |
61 return callback.Run(scoped_ptr<PrivetHTTPClient>()); | |
62 | |
63 Start(description.address, callback); | |
64 } | 47 } |
65 | 48 |
66 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start( | 49 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start( |
67 const net::HostPortPair& address, | 50 const net::HostPortPair& address, |
68 const ResultCallback& callback) { | 51 const ResultCallback& callback) { |
69 #if defined(OS_MACOSX) | 52 endpoint_resolver_->Start(address, |
70 net::IPAddressNumber ip_address; | 53 base::Bind(&ResolutionImpl::ResolveComplete, |
71 if (!net::ParseIPLiteralToNumber(address.host(), &ip_address)) { | 54 base::Unretained(this), callback)); |
72 NOTREACHED() << address.ToString(); | |
73 // Unexpected, but could be a reason for crbug.com/513505 | |
74 base::debug::DumpWithoutCrashing(); | |
75 return callback.Run(scoped_ptr<PrivetHTTPClient>()); | |
76 } | |
77 | |
78 // OSX already has IP there. | |
79 callback.Run(scoped_ptr<PrivetHTTPClient>(new PrivetHTTPClientImpl( | |
80 name_, net::HostPortPair::FromIPEndPoint( | |
81 net::IPEndPoint(ip_address, address.port())), | |
82 request_context_.get()))); | |
83 #else // OS_MACOSX | |
84 net::AddressFamily address_family = net::ADDRESS_FAMILY_UNSPECIFIED; | |
85 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
86 switches::kPrivetIPv6Only)) { | |
87 address_family = net::ADDRESS_FAMILY_IPV6; | |
88 } | |
89 | |
90 domain_resolver_ = service_discovery_client_->CreateLocalDomainResolver( | |
91 address.host(), address_family, | |
92 base::Bind(&ResolutionImpl::DomainResolveComplete, base::Unretained(this), | |
93 address.port(), callback)); | |
94 domain_resolver_->Start(); | |
95 #endif // OS_MACOSX | |
96 } | 55 } |
97 | 56 |
98 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::DomainResolveComplete( | 57 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolveComplete( |
99 uint16 port, | |
100 const ResultCallback& callback, | 58 const ResultCallback& callback, |
101 bool success, | 59 const net::IPEndPoint& endpoint) { |
102 const net::IPAddressNumber& address_ipv4, | 60 if (endpoint.address().empty()) |
103 const net::IPAddressNumber& address_ipv6) { | 61 return callback.Run(scoped_ptr<PrivetHTTPClient>()); |
104 if (!success) { | |
105 callback.Run(scoped_ptr<PrivetHTTPClient>()); | |
106 return; | |
107 } | |
108 | 62 |
109 net::IPAddressNumber address = address_ipv4; | 63 net::HostPortPair new_address = net::HostPortPair::FromIPEndPoint(endpoint); |
110 if (address.empty()) | |
111 address = address_ipv6; | |
112 | |
113 DCHECK(!address.empty()); | |
114 | |
115 net::HostPortPair new_address = | |
116 net::HostPortPair::FromIPEndPoint(net::IPEndPoint(address, port)); | |
117 callback.Run(scoped_ptr<PrivetHTTPClient>( | 64 callback.Run(scoped_ptr<PrivetHTTPClient>( |
118 new PrivetHTTPClientImpl(name_, new_address, request_context_.get()))); | 65 new PrivetHTTPClientImpl(name_, new_address, request_context_.get()))); |
119 } | 66 } |
120 | 67 |
121 } // namespace local_discovery | 68 } // namespace local_discovery |
OLD | NEW |