[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / local_discovery / privet_http_asynchronous_factory_impl.cc
blob80d207c99e675ab41be33a55e201fa9e403ba39c
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"
7 #include "base/bind.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 {
15 namespace {
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());
25 return address_str;
28 } // namespace
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,
45 address,
46 callback,
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)
57 : name_(name),
58 hostport_(address),
59 callback_(callback),
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(
68 address.host(),
69 address_family,
70 base::Bind(&ResolutionImpl::ResolveComplete, base::Unretained(this)));
73 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::~ResolutionImpl() {}
75 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::Start() {
76 resolver_->Start();
79 const std::string&
80 PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::GetName() {
81 return name_;
84 void PrivetHTTPAsynchronousFactoryImpl::ResolutionImpl::ResolveComplete(
85 bool success,
86 const net::IPAddressNumber& address_ipv4,
87 const net::IPAddressNumber& address_ipv6) {
88 if (!success) {
89 callback_.Run(scoped_ptr<PrivetHTTPClient>());
90 return;
93 net::IPAddressNumber address = address_ipv4;
94 if (address.empty())
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