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.
4
5#ifndef NET_DNS_SINGLE_REQUEST_HOST_RESOLVER_H_
6#define NET_DNS_SINGLE_REQUEST_HOST_RESOLVER_H_
7
8#include "base/basictypes.h"
9
10#include "net/base/completion_callback.h"
11#include "net/base/net_export.h"
12#include "net/base/request_priority.h"
13#include "net/dns/host_resolver.h"
14
15namespace net {
16
17class AddressList;
18class BoundNetLog;
19
20// This class represents the task of resolving a hostname (or IP address
21// literal) to an AddressList object.  It wraps HostResolver to resolve only a
22// single hostname at a time and cancels this request when going out of scope.
23class NET_EXPORT SingleRequestHostResolver {
24 public:
25  // |resolver| must remain valid for the lifetime of |this|.
26  explicit SingleRequestHostResolver(HostResolver* resolver);
27
28  // If a completion callback is pending when the resolver is destroyed, the
29  // host resolution is cancelled, and the completion callback will not be
30  // called.
31  ~SingleRequestHostResolver();
32
33  // Resolves the given hostname (or IP address literal), filling out the
34  // |addresses| object upon success. See HostResolver::Resolve() for details.
35  int Resolve(const HostResolver::RequestInfo& info,
36              RequestPriority priority,
37              AddressList* addresses,
38              const CompletionCallback& callback,
39              const BoundNetLog& net_log);
40
41  // Cancels the in-progress request, if any. This prevents the callback
42  // from being invoked. Resolve() can be called again after cancelling.
43  void Cancel();
44
45 private:
46  // Callback for when the request to |resolver_| completes, so we dispatch
47  // to the user's callback.
48  void OnResolveCompletion(int result);
49
50  // The actual host resolver that will handle the request.
51  HostResolver* const resolver_;
52
53  // The current request (if any).
54  HostResolver::RequestHandle cur_request_;
55  CompletionCallback cur_request_callback_;
56
57  // Completion callback for when request to |resolver_| completes.
58  CompletionCallback callback_;
59
60  DISALLOW_COPY_AND_ASSIGN(SingleRequestHostResolver);
61};
62
63}  // namespace net
64
65#endif  // NET_DNS_SINGLE_REQUEST_HOST_RESOLVER_H_
66