1 // Copyright (c) 2012 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 #ifndef NET_DNS_HOST_RESOLVER_PROC_H_
6 #define NET_DNS_HOST_RESOLVER_PROC_H_
10 #include "base/memory/ref_counted.h"
11 #include "net/base/address_family.h"
12 #include "net/base/net_export.h"
18 // Interface for a getaddrinfo()-like procedure. This is used by unit-tests
19 // to control the underlying resolutions in HostResolverImpl. HostResolverProcs
20 // can be chained together; they fallback to the next procedure in the chain
21 // by calling ResolveUsingPrevious().
23 // Note that implementations of HostResolverProc *MUST BE THREADSAFE*, since
24 // the HostResolver implementation using them can be multi-threaded.
25 class NET_EXPORT HostResolverProc
26 : public base::RefCountedThreadSafe
<HostResolverProc
> {
28 explicit HostResolverProc(HostResolverProc
* previous
);
30 // Resolves |host| to an address list, restricting the results to addresses
31 // in |address_family|. If successful returns OK and fills |addrlist| with
32 // a list of socket addresses. Otherwise returns a network error code, and
33 // fills |os_error| with a more specific error if it was non-NULL.
34 virtual int Resolve(const std::string
& host
,
35 AddressFamily address_family
,
36 HostResolverFlags host_resolver_flags
,
37 AddressList
* addrlist
,
41 friend class base::RefCountedThreadSafe
<HostResolverProc
>;
43 virtual ~HostResolverProc();
45 // Asks the fallback procedure (if set) to do the resolve.
46 int ResolveUsingPrevious(const std::string
& host
,
47 AddressFamily address_family
,
48 HostResolverFlags host_resolver_flags
,
49 AddressList
* addrlist
,
53 friend class HostResolverImpl
;
54 friend class MockHostResolverBase
;
55 friend class ScopedDefaultHostResolverProc
;
57 // Sets the previous procedure in the chain. Aborts if this would result in a
59 void SetPreviousProc(HostResolverProc
* proc
);
61 // Sets the last procedure in the chain, i.e. appends |proc| to the end of the
62 // current chain. Aborts if this would result in a cycle.
63 void SetLastProc(HostResolverProc
* proc
);
65 // Returns the last procedure in the chain starting at |proc|. Will return
66 // NULL iff |proc| is NULL.
67 static HostResolverProc
* GetLastProc(HostResolverProc
* proc
);
69 // Sets the default host resolver procedure that is used by HostResolverImpl.
70 // This can be used through ScopedDefaultHostResolverProc to set a catch-all
71 // DNS block in unit-tests (individual tests should use MockHostResolver to
72 // prevent hitting the network).
73 static HostResolverProc
* SetDefault(HostResolverProc
* proc
);
74 static HostResolverProc
* GetDefault();
76 scoped_refptr
<HostResolverProc
> previous_proc_
;
77 static HostResolverProc
* default_proc_
;
79 DISALLOW_COPY_AND_ASSIGN(HostResolverProc
);
82 // Resolves |host| to an address list, using the system's default host resolver.
83 // (i.e. this calls out to getaddrinfo()). If successful returns OK and fills
84 // |addrlist| with a list of socket addresses. Otherwise returns a
85 // network error code, and fills |os_error| with a more specific error if it
87 NET_EXPORT_PRIVATE
int SystemHostResolverCall(
88 const std::string
& host
,
89 AddressFamily address_family
,
90 HostResolverFlags host_resolver_flags
,
91 AddressList
* addrlist
,
94 // Wraps call to SystemHostResolverCall as an instance of HostResolverProc.
95 class NET_EXPORT_PRIVATE SystemHostResolverProc
: public HostResolverProc
{
97 SystemHostResolverProc();
98 virtual int Resolve(const std::string
& hostname
,
99 AddressFamily address_family
,
100 HostResolverFlags host_resolver_flags
,
101 AddressList
* addr_list
,
102 int* os_error
) OVERRIDE
;
104 virtual ~SystemHostResolverProc();
106 DISALLOW_COPY_AND_ASSIGN(SystemHostResolverProc
);
111 #endif // NET_DNS_HOST_RESOLVER_PROC_H_