roll skia to 4276
[chromium-blink-merge.git] / net / base / host_resolver_proc.h
blob7a54d01b9e6d8960f889ad8fb3b474a0efd388ff
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_BASE_HOST_RESOLVER_PROC_H_
6 #define NET_BASE_HOST_RESOLVER_PROC_H_
7 #pragma once
9 #include <string>
11 #include "base/memory/ref_counted.h"
12 #include "net/base/address_family.h"
13 #include "net/base/net_export.h"
15 namespace net {
17 class AddressList;
19 // Interface for a getaddrinfo()-like procedure. This is used by unit-tests
20 // to control the underlying resolutions in HostResolverImpl. HostResolverProcs
21 // can be chained together; they fallback to the next procedure in the chain
22 // by calling ResolveUsingPrevious().
24 // Note that implementations of HostResolverProc *MUST BE THREADSAFE*, since
25 // the HostResolver implementation using them can be multi-threaded.
26 class NET_EXPORT HostResolverProc
27 : public base::RefCountedThreadSafe<HostResolverProc> {
28 public:
29 explicit HostResolverProc(HostResolverProc* previous);
31 // Resolves |host| to an address list, restricting the results to addresses
32 // in |address_family|. If successful returns OK and fills |addrlist| with
33 // a list of socket addresses. Otherwise returns a network error code, and
34 // fills |os_error| with a more specific error if it was non-NULL.
35 virtual int Resolve(const std::string& host,
36 AddressFamily address_family,
37 HostResolverFlags host_resolver_flags,
38 AddressList* addrlist,
39 int* os_error) = 0;
41 protected:
42 friend class base::RefCountedThreadSafe<HostResolverProc>;
44 virtual ~HostResolverProc();
46 // Asks the fallback procedure (if set) to do the resolve.
47 int ResolveUsingPrevious(const std::string& host,
48 AddressFamily address_family,
49 HostResolverFlags host_resolver_flags,
50 AddressList* addrlist,
51 int* os_error);
53 private:
54 friend class HostResolverImpl;
55 friend class MockHostResolverBase;
56 friend class ScopedDefaultHostResolverProc;
58 // Sets the previous procedure in the chain. Aborts if this would result in a
59 // cycle.
60 void SetPreviousProc(HostResolverProc* proc);
62 // Sets the last procedure in the chain, i.e. appends |proc| to the end of the
63 // current chain. Aborts if this would result in a cycle.
64 void SetLastProc(HostResolverProc* proc);
66 // Returns the last procedure in the chain starting at |proc|. Will return
67 // NULL iff |proc| is NULL.
68 static HostResolverProc* GetLastProc(HostResolverProc* proc);
70 // Sets the default host resolver procedure that is used by HostResolverImpl.
71 // This can be used through ScopedDefaultHostResolverProc to set a catch-all
72 // DNS block in unit-tests (individual tests should use MockHostResolver to
73 // prevent hitting the network).
74 static HostResolverProc* SetDefault(HostResolverProc* proc);
75 static HostResolverProc* GetDefault();
77 scoped_refptr<HostResolverProc> previous_proc_;
78 static HostResolverProc* default_proc_;
80 DISALLOW_COPY_AND_ASSIGN(HostResolverProc);
83 // Resolves |host| to an address list, using the system's default host resolver.
84 // (i.e. this calls out to getaddrinfo()). If successful returns OK and fills
85 // |addrlist| with a list of socket addresses. Otherwise returns a
86 // network error code, and fills |os_error| with a more specific error if it
87 // was non-NULL.
88 NET_EXPORT_PRIVATE int SystemHostResolverProc(
89 const std::string& host,
90 AddressFamily address_family,
91 HostResolverFlags host_resolver_flags,
92 AddressList* addrlist,
93 int* os_error);
95 } // namespace net
97 #endif // NET_BASE_HOST_RESOLVER_PROC_H_