1 // Copyright (c) 2011 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/single_request_host_resolver.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "net/base/net_errors.h"
15 SingleRequestHostResolver::SingleRequestHostResolver(HostResolver
* resolver
)
16 : resolver_(resolver
),
19 base::Bind(&SingleRequestHostResolver::OnResolveCompletion
,
20 base::Unretained(this))) {
21 DCHECK(resolver_
!= NULL
);
24 SingleRequestHostResolver::~SingleRequestHostResolver() {
28 int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo
& info
,
29 RequestPriority priority
,
30 AddressList
* addresses
,
31 const CompletionCallback
& callback
,
32 const BoundNetLog
& net_log
) {
34 DCHECK_EQ(false, callback
.is_null());
35 DCHECK(cur_request_callback_
.is_null()) << "resolver already in use";
37 HostResolver::RequestHandle request
= NULL
;
39 // We need to be notified of completion before |callback| is called, so that
40 // we can clear out |cur_request_*|.
41 CompletionCallback transient_callback
=
42 callback
.is_null() ? CompletionCallback() : callback_
;
44 int rv
= resolver_
->Resolve(
45 info
, priority
, addresses
, transient_callback
, &request
, net_log
);
47 if (rv
== ERR_IO_PENDING
) {
48 DCHECK_EQ(false, callback
.is_null());
49 // Cleared in OnResolveCompletion().
50 cur_request_
= request
;
51 cur_request_callback_
= callback
;
57 void SingleRequestHostResolver::Cancel() {
58 if (!cur_request_callback_
.is_null()) {
59 resolver_
->CancelRequest(cur_request_
);
61 cur_request_callback_
.Reset();
65 void SingleRequestHostResolver::OnResolveCompletion(int result
) {
67 DCHECK_EQ(false, cur_request_callback_
.is_null());
69 CompletionCallback callback
= cur_request_callback_
;
71 // Clear the outstanding request information.
73 cur_request_callback_
.Reset();
75 // Call the user's original callback.