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/privet_http_asynchronous_factory.h" | 5 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #if defined(OS_MACOSX) |
8 #include "base/command_line.h" | 8 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory_mac.h" |
9 #include "base/strings/stringprintf.h" | 9 #else |
10 #include "chrome/browser/local_discovery/privet_http.h" | 10 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory_impl.h
" |
11 #include "chrome/browser/local_discovery/privet_http_impl.h" | 11 #endif |
12 #include "chrome/common/chrome_switches.h" | |
13 #include "chrome/common/local_discovery/service_discovery_client.h" | |
14 | 12 |
15 namespace local_discovery { | 13 namespace local_discovery { |
16 | 14 |
17 namespace { | |
18 | |
19 std::string IPAddressToHostString(const net::IPAddressNumber& address) { | |
20 std::string address_str = net::IPAddressToString(address); | |
21 | |
22 // IPv6 addresses need to be surrounded by brackets. | |
23 if (address.size() == net::kIPv6AddressSize) { | |
24 address_str = base::StringPrintf("[%s]", address_str.c_str()); | |
25 } | |
26 | |
27 return address_str; | |
28 } | |
29 | |
30 } // namespace | |
31 | |
32 class PrivetHTTPAsynchronousFactoryImpl : public PrivetHTTPAsynchronousFactory { | |
33 public: | |
34 PrivetHTTPAsynchronousFactoryImpl( | |
35 ServiceDiscoveryClient* service_discovery_client, | |
36 net::URLRequestContextGetter* request_context); | |
37 virtual ~PrivetHTTPAsynchronousFactoryImpl(); | |
38 | |
39 virtual scoped_ptr<PrivetHTTPResolution> CreatePrivetHTTP( | |
40 const std::string& name, | |
41 const net::HostPortPair& address, | |
42 const ResultCallback& callback) OVERRIDE; | |
43 | |
44 private: | |
45 class ResolutionImpl : public PrivetHTTPResolution { | |
46 public: | |
47 ResolutionImpl(const std::string& name, | |
48 const net::HostPortPair& address, | |
49 const ResultCallback& callback, | |
50 ServiceDiscoveryClient* service_discovery_client, | |
51 net::URLRequestContextGetter* request_context); | |
52 virtual ~ResolutionImpl(); | |
53 | |
54 virtual void Start() OVERRIDE; | |
55 virtual const std::string& GetName() OVERRIDE; | |
56 private: | |
57 void ResolveComplete(bool success, | |
58 const net::IPAddressNumber& address_ipv4, | |
59 const net::IPAddressNumber& address_ipv6); | |
60 | |
61 std::string name_; | |
62 scoped_ptr<LocalDomainResolver> resolver_; | |
63 net::HostPortPair hostport_; | |
64 ResultCallback callback_; | |
65 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
66 }; | |
67 | |
68 ServiceDiscoveryClient* service_discovery_client_; | |
69 scoped_refptr<net::URLRequestContextGetter> request_context_; | |
70 }; | |
71 | |
72 PrivetHTTPAsynchronousFactoryImpl::PrivetHTTPAsynchronousFactoryImpl( | |
73 ServiceDiscoveryClient* service_discovery_client, | |
74 net::URLRequestContextGetter* request_context) | |
75 : service_discovery_client_(service_discovery_client), | |
76 request_context_(request_context) { | |
77 } | |
78 | |
79 PrivetHTTPAsynchronousFactoryImpl::~PrivetHTTPAsynchronousFactoryImpl() { | |
80 } | |
81 | |
82 // static | 15 // static |
83 scoped_ptr<PrivetHTTPAsynchronousFactory> | 16 scoped_ptr<PrivetHTTPAsynchronousFactory> |
84 PrivetHTTPAsynchronousFactory::CreateInstance( | 17 PrivetHTTPAsynchronousFactory::CreateInstance( |
85 ServiceDiscoveryClient* service_discovery_client, | 18 ServiceDiscoveryClient* service_discovery_client, |
86 net::URLRequestContextGetter* request_context) { | 19 net::URLRequestContextGetter* request_context) { |
| 20 #if defined(OS_MACOSX) |
| 21 return make_scoped_ptr<PrivetHTTPAsynchronousFactory>( |
| 22 new PrivetHTTPAsynchronousFactoryMac(request_context)); |
| 23 |
| 24 #else |
87 return make_scoped_ptr<PrivetHTTPAsynchronousFactory>( | 25 return make_scoped_ptr<PrivetHTTPAsynchronousFactory>( |
88 new PrivetHTTPAsynchronousFactoryImpl(service_discovery_client, | 26 new PrivetHTTPAsynchronousFactoryImpl(service_discovery_client, |
89 request_context)); | 27 request_context)); |
90 } | 28 #endif |
91 | |
92 scoped_ptr<PrivetHTTPResolution> | |
93 PrivetHTTPAsynchronousFactoryImpl::CreatePrivetHTTP( | |
94 const std::string& name, | |
95 const net::HostPortPair& address, | |
96 const ResultCallback& callback) { | |
97 return scoped_ptr<PrivetHTTPResolution>( | |
98 new ResolutionImpl(name, address, callback, service_discovery_client_, | |
99 request_context_.get())); | |
100 } | |
101 | |
102 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolutionImpl( | |
103 const std::string& name, | |
104 const net::HostPortPair& address, | |
105 const ResultCallback& callback, | |
106 ServiceDiscoveryClient* service_discovery_client, | |
107 net::URLRequestContextGetter* request_context) | |
108 : name_(name), hostport_(address), callback_(callback), | |
109 request_context_(request_context) { | |
110 net::AddressFamily address_family = net::ADDRESS_FAMILY_UNSPECIFIED; | |
111 | |
112 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
113 switches::kPrivetIPv6Only)) { | |
114 address_family = net::ADDRESS_FAMILY_IPV6; | |
115 } | |
116 | |
117 resolver_ = service_discovery_client->CreateLocalDomainResolver( | |
118 address.host(), address_family, | |
119 base::Bind(&ResolutionImpl::ResolveComplete, base::Unretained(this))); | |
120 } | |
121 | |
122 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() { | |
123 } | |
124 | |
125 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start() { | |
126 resolver_->Start(); | |
127 } | |
128 | |
129 const std::string& | |
130 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() { | |
131 return name_; | |
132 } | |
133 | |
134 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolveComplete( | |
135 bool success, | |
136 const net::IPAddressNumber& address_ipv4, | |
137 const net::IPAddressNumber& address_ipv6) { | |
138 if (!success) { | |
139 callback_.Run(scoped_ptr<PrivetHTTPClient>()); | |
140 return; | |
141 } | |
142 | |
143 net::IPAddressNumber address = address_ipv4; | |
144 if (address.empty()) | |
145 address = address_ipv6; | |
146 | |
147 DCHECK(!address.empty()); | |
148 | |
149 net::HostPortPair new_address = net::HostPortPair( | |
150 IPAddressToHostString(address), hostport_.port()); | |
151 callback_.Run(scoped_ptr<PrivetHTTPClient>( | |
152 new PrivetHTTPClientImpl(name_, new_address, request_context_.get()))); | |
153 } | 29 } |
154 | 30 |
155 } // namespace local_discovery | 31 } // namespace local_discovery |
OLD | NEW |