Fix destruction order of PromoResourceService and NTPResourceCache
[chromium-blink-merge.git] / net / dns / single_request_host_resolver.cc
blob2c4b3aa19b9aaf9e193cafac1bb427fc883ff9a0
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"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/logging.h"
11 #include "base/profiler/scoped_tracker.h"
12 #include "net/base/net_errors.h"
14 namespace net {
16 SingleRequestHostResolver::SingleRequestHostResolver(HostResolver* resolver)
17 : resolver_(resolver),
18 cur_request_(NULL),
19 callback_(
20 base::Bind(&SingleRequestHostResolver::OnResolveCompletion,
21 base::Unretained(this))) {
22 DCHECK(resolver_ != NULL);
25 SingleRequestHostResolver::~SingleRequestHostResolver() {
26 Cancel();
29 int SingleRequestHostResolver::Resolve(const HostResolver::RequestInfo& info,
30 RequestPriority priority,
31 AddressList* addresses,
32 const CompletionCallback& callback,
33 const BoundNetLog& net_log) {
34 DCHECK(addresses);
35 DCHECK_EQ(false, callback.is_null());
36 DCHECK(cur_request_callback_.is_null()) << "resolver already in use";
38 HostResolver::RequestHandle request = NULL;
40 // We need to be notified of completion before |callback| is called, so that
41 // we can clear out |cur_request_*|.
42 CompletionCallback transient_callback =
43 callback.is_null() ? CompletionCallback() : callback_;
45 int rv = resolver_->Resolve(
46 info, priority, addresses, transient_callback, &request, net_log);
48 if (rv == ERR_IO_PENDING) {
49 DCHECK_EQ(false, callback.is_null());
50 // Cleared in OnResolveCompletion().
51 cur_request_ = request;
52 cur_request_callback_ = callback;
55 return rv;
58 void SingleRequestHostResolver::Cancel() {
59 if (!cur_request_callback_.is_null()) {
60 resolver_->CancelRequest(cur_request_);
61 cur_request_ = NULL;
62 cur_request_callback_.Reset();
66 void SingleRequestHostResolver::OnResolveCompletion(int result) {
67 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
68 tracked_objects::ScopedTracker tracking_profile(
69 FROM_HERE_WITH_EXPLICIT_FUNCTION(
70 "436634 SingleRequestHostResolver::OnResolveCompletion"));
72 DCHECK(cur_request_);
73 DCHECK_EQ(false, cur_request_callback_.is_null());
75 CompletionCallback callback = cur_request_callback_;
77 // Clear the outstanding request information.
78 cur_request_ = NULL;
79 cur_request_callback_.Reset();
81 // Call the user's original callback.
82 callback.Run(result);
85 } // namespace net