1 // Copyright 2015 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 "net/dns/mojo_host_resolver_impl.h"
7 #include "base/basictypes.h"
8 #include "base/stl_util.h"
9 #include "net/base/address_list.h"
10 #include "net/base/net_errors.h"
11 #include "net/dns/host_resolver.h"
12 #include "net/dns/mojo_host_type_converters.h"
16 // Handles host resolution for a single request and sends a response when done.
17 // Also detects connection errors for HostResolverRequestClient and cancels the
18 // outstanding resolve request. Owned by MojoHostResolverImpl.
19 class MojoHostResolverImpl::Job
{
21 Job(MojoHostResolverImpl
* resolver_service
,
22 net::HostResolver
* resolver
,
23 const net::HostResolver::RequestInfo
& request_info
,
24 const BoundNetLog
& net_log
,
25 interfaces::HostResolverRequestClientPtr client
);
31 // Completion callback for the HostResolver::Resolve request.
32 void OnResolveDone(int result
);
34 // Mojo error handler.
35 void OnConnectionError();
37 MojoHostResolverImpl
* resolver_service_
;
38 net::HostResolver
* resolver_
;
39 net::HostResolver::RequestInfo request_info_
;
40 const BoundNetLog net_log_
;
41 interfaces::HostResolverRequestClientPtr client_
;
42 net::HostResolver::RequestHandle handle_
;
44 base::ThreadChecker thread_checker_
;
47 MojoHostResolverImpl::MojoHostResolverImpl(net::HostResolver
* resolver
,
48 const BoundNetLog
& net_log
)
49 : resolver_(resolver
), net_log_(net_log
) {
52 MojoHostResolverImpl::~MojoHostResolverImpl() {
53 DCHECK(thread_checker_
.CalledOnValidThread());
54 STLDeleteElements(&pending_jobs_
);
57 void MojoHostResolverImpl::Resolve(
58 interfaces::HostResolverRequestInfoPtr request_info
,
59 interfaces::HostResolverRequestClientPtr client
) {
60 DCHECK(thread_checker_
.CalledOnValidThread());
61 Job
* job
= new Job(this, resolver_
,
62 request_info
->To
<net::HostResolver::RequestInfo
>(),
63 net_log_
, client
.Pass());
64 pending_jobs_
.insert(job
);
68 void MojoHostResolverImpl::DeleteJob(Job
* job
) {
69 DCHECK(thread_checker_
.CalledOnValidThread());
70 size_t num_erased
= pending_jobs_
.erase(job
);
75 MojoHostResolverImpl::Job::Job(
76 MojoHostResolverImpl
* resolver_service
,
77 net::HostResolver
* resolver
,
78 const net::HostResolver::RequestInfo
& request_info
,
79 const BoundNetLog
& net_log
,
80 interfaces::HostResolverRequestClientPtr client
)
81 : resolver_service_(resolver_service
),
83 request_info_(request_info
),
85 client_(client
.Pass()),
87 client_
.set_connection_error_handler(base::Bind(
88 &MojoHostResolverImpl::Job::OnConnectionError
, base::Unretained(this)));
91 void MojoHostResolverImpl::Job::Start() {
92 DVLOG(1) << "Resolve " << request_info_
.host_port_pair().ToString();
94 resolver_
->Resolve(request_info_
, DEFAULT_PRIORITY
, &result_
,
95 base::Bind(&MojoHostResolverImpl::Job::OnResolveDone
,
96 base::Unretained(this)),
99 if (result
!= ERR_IO_PENDING
)
100 OnResolveDone(result
);
103 MojoHostResolverImpl::Job::~Job() {
104 DCHECK(thread_checker_
.CalledOnValidThread());
106 resolver_
->CancelRequest(handle_
);
109 void MojoHostResolverImpl::Job::OnResolveDone(int result
) {
110 DCHECK(thread_checker_
.CalledOnValidThread());
112 DVLOG(1) << "Resolved " << request_info_
.host_port_pair().ToString()
113 << " with error " << result
<< " and " << result_
.size()
115 for (const auto& address
: result_
) {
116 DVLOG(1) << address
.ToString();
119 client_
->ReportResult(result
, interfaces::AddressList::From(result_
));
121 client_
->ReportResult(result
, nullptr);
123 resolver_service_
->DeleteJob(this);
126 void MojoHostResolverImpl::Job::OnConnectionError() {
127 DCHECK(thread_checker_
.CalledOnValidThread());
128 // |resolver_service_| should always outlive us.
129 DCHECK(resolver_service_
);
130 DVLOG(1) << "Connection error on request for "
131 << request_info_
.host_port_pair().ToString();
132 resolver_service_
->DeleteJob(this);