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
3 // found in the LICENSE file.
5 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory.h"
8 #include "base/command_line.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/local_discovery/privet_http.h"
11 #include "chrome/browser/local_discovery/privet_http_impl.h"
12 #include "chrome/common/chrome_switches.h"
13 #include "chrome/common/local_discovery/service_discovery_client.h"
15 namespace local_discovery
{
19 std::string
IPAddressToHostString(const net::IPAddressNumber
& address
) {
20 std::string address_str
= net::IPAddressToString(address
);
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());
32 class PrivetHTTPAsynchronousFactoryImpl
: public PrivetHTTPAsynchronousFactory
{
34 PrivetHTTPAsynchronousFactoryImpl(
35 ServiceDiscoveryClient
* service_discovery_client
,
36 net::URLRequestContextGetter
* request_context
);
37 virtual ~PrivetHTTPAsynchronousFactoryImpl();
39 virtual scoped_ptr
<PrivetHTTPResolution
> CreatePrivetHTTP(
40 const std::string
& name
,
41 const net::HostPortPair
& address
,
42 const ResultCallback
& callback
) OVERRIDE
;
45 class ResolutionImpl
: public PrivetHTTPResolution
{
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();
54 virtual void Start() OVERRIDE
;
55 virtual const std::string
& GetName() OVERRIDE
;
57 void ResolveComplete(bool success
,
58 const net::IPAddressNumber
& address_ipv4
,
59 const net::IPAddressNumber
& address_ipv6
);
62 scoped_ptr
<LocalDomainResolver
> resolver_
;
63 net::HostPortPair hostport_
;
64 ResultCallback callback_
;
65 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
68 ServiceDiscoveryClient
* service_discovery_client_
;
69 scoped_refptr
<net::URLRequestContextGetter
> request_context_
;
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
) {
79 PrivetHTTPAsynchronousFactoryImpl::~PrivetHTTPAsynchronousFactoryImpl() {
83 scoped_ptr
<PrivetHTTPAsynchronousFactory
>
84 PrivetHTTPAsynchronousFactory::CreateInstance(
85 ServiceDiscoveryClient
* service_discovery_client
,
86 net::URLRequestContextGetter
* request_context
) {
87 return make_scoped_ptr
<PrivetHTTPAsynchronousFactory
>(
88 new PrivetHTTPAsynchronousFactoryImpl(service_discovery_client
,
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()));
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
;
112 if (CommandLine::ForCurrentProcess()->HasSwitch(
113 switches::kPrivetIPv6Only
)) {
114 address_family
= net::ADDRESS_FAMILY_IPV6
;
117 resolver_
= service_discovery_client
->CreateLocalDomainResolver(
118 address
.host(), address_family
,
119 base::Bind(&ResolutionImpl::ResolveComplete
, base::Unretained(this)));
122 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() {
125 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start() {
130 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() {
134 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolveComplete(
136 const net::IPAddressNumber
& address_ipv4
,
137 const net::IPAddressNumber
& address_ipv6
) {
139 callback_
.Run(scoped_ptr
<PrivetHTTPClient
>());
143 net::IPAddressNumber address
= address_ipv4
;
145 address
= address_ipv6
;
147 DCHECK(!address
.empty());
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())));
155 } // namespace local_discovery