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
3 // found in the LICENSE file.
5 #include "chrome/browser/local_discovery/privet_http_asynchronous_factory_impl.h"
8 #include "base/command_line.h"
9 #include "base/strings/stringprintf.h"
10 #include "chrome/browser/local_discovery/privet_http_impl.h"
11 #include "chrome/common/chrome_switches.h"
13 namespace local_discovery
{
17 std::string
IPAddressToHostString(const net::IPAddressNumber
& address
) {
18 std::string address_str
= net::IPAddressToString(address
);
20 // IPv6 addresses need to be surrounded by brackets.
21 if (address
.size() == net::kIPv6AddressSize
) {
22 address_str
= base::StringPrintf("[%s]", address_str
.c_str());
30 PrivetHTTPAsynchronousFactoryImpl::PrivetHTTPAsynchronousFactoryImpl(
31 ServiceDiscoveryClient
* service_discovery_client
,
32 net::URLRequestContextGetter
* request_context
)
33 : service_discovery_client_(service_discovery_client
),
34 request_context_(request_context
) {}
36 PrivetHTTPAsynchronousFactoryImpl::~PrivetHTTPAsynchronousFactoryImpl() {}
38 scoped_ptr
<PrivetHTTPResolution
>
39 PrivetHTTPAsynchronousFactoryImpl::CreatePrivetHTTP(
40 const std::string
& name
,
41 const net::HostPortPair
& address
,
42 const ResultCallback
& callback
) {
43 return scoped_ptr
<PrivetHTTPResolution
>(
44 new ResolutionImpl(name
,
47 service_discovery_client_
,
48 request_context_
.get()));
51 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolutionImpl(
52 const std::string
& name
,
53 const net::HostPortPair
& address
,
54 const ResultCallback
& callback
,
55 ServiceDiscoveryClient
* service_discovery_client
,
56 net::URLRequestContextGetter
* request_context
)
60 request_context_(request_context
) {
61 net::AddressFamily address_family
= net::ADDRESS_FAMILY_UNSPECIFIED
;
63 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kPrivetIPv6Only
)) {
64 address_family
= net::ADDRESS_FAMILY_IPV6
;
67 resolver_
= service_discovery_client
->CreateLocalDomainResolver(
70 base::Bind(&ResolutionImpl::ResolveComplete
, base::Unretained(this)));
73 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() {}
75 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start() {
80 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() {
84 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolveComplete(
86 const net::IPAddressNumber
& address_ipv4
,
87 const net::IPAddressNumber
& address_ipv6
) {
89 callback_
.Run(scoped_ptr
<PrivetHTTPClient
>());
93 net::IPAddressNumber address
= address_ipv4
;
95 address
= address_ipv6
;
97 DCHECK(!address
.empty());
99 net::HostPortPair new_address
=
100 net::HostPortPair(IPAddressToHostString(address
), hostport_
.port());
101 callback_
.Run(scoped_ptr
<PrivetHTTPClient
>(
102 new PrivetHTTPClientImpl(name_
, new_address
, request_context_
.get())));
105 } // namespace local_discovery