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 CHROME_BROWSER_EXTENSIONS_API_DNS_HOST_RESOLVER_WRAPPER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DNS_HOST_RESOLVER_WRAPPER_H_
8 #include "base/memory/singleton.h"
9 #include "net/dns/host_resolver.h"
11 namespace extensions {
13 // Used for testing. In production code, this class does nothing interesting.
14 // This class is a singleton that holds a pointer to a mock HostResolver, or
15 // else to NULL. API classes that need to resolve hostnames ask this class for
16 // the correct HostResolver to use, passing in the one that they want to use,
17 // thereby avoiding most lifetime issues, and it will reply with either that
18 // same one, or else the test version to use instead.
20 // This is a pretty complicated way to replace a single pointer with another.
21 // TODO(miket): make the previous statement obsolete.
22 class HostResolverWrapper {
24 static HostResolverWrapper* GetInstance();
26 // Given a pointer to a real host resolver, returns the same pointer or else
27 // a substitute MockHostResolver to use instead. If
28 // SetHostResolverForTesting() hasn't been called, then this method returns
29 // the supplied argument as its result.
30 net::HostResolver* GetHostResolver(net::HostResolver* real_resolver);
32 // Sets the MockHostResolver to return in GetHostResolver().
33 void SetHostResolverForTesting(net::HostResolver* mock_resolver);
36 HostResolverWrapper();
37 friend struct DefaultSingletonTraits<HostResolverWrapper>;
39 net::HostResolver* resolver_;
41 DISALLOW_COPY_AND_ASSIGN(HostResolverWrapper);
44 } // namespace extensions
46 #endif // CHROME_BROWSER_EXTENSIONS_API_DNS_HOST_RESOLVER_WRAPPER_H_