1// Copyright 2014 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 EXTENSIONS_BROWSER_API_DNS_HOST_RESOLVER_WRAPPER_H_ 6#define EXTENSIONS_BROWSER_API_DNS_HOST_RESOLVER_WRAPPER_H_ 7 8#include "base/memory/singleton.h" 9 10namespace content { 11class ResourceContext; 12} 13 14namespace net { 15class HostResolver; 16} 17 18namespace extensions { 19 20// Used for testing. In production code, this class does nothing interesting. 21// This class is a singleton that holds a pointer to a mock HostResolver, or 22// else to NULL. API classes that need to resolve hostnames ask this class for 23// the correct HostResolver to use, passing in the one that they want to use, 24// thereby avoiding most lifetime issues, and it will reply with either that 25// same one, or else the test version to use instead. 26// 27// This is a pretty complicated way to replace a single pointer with another. 28// TODO(miket): make the previous statement obsolete. 29class HostResolverWrapper { 30 public: 31 static HostResolverWrapper* GetInstance(); 32 33 // Given a pointer to a ResourceContext, returns its HostResolver if 34 // SetHostResolverForTesting() hasn't been called, or else a 35 // a substitute MockHostResolver to use instead. 36 net::HostResolver* GetHostResolver(content::ResourceContext* context); 37 38 // Sets the MockHostResolver to return in GetHostResolver(). 39 void SetHostResolverForTesting(net::HostResolver* mock_resolver); 40 41 private: 42 HostResolverWrapper(); 43 friend struct DefaultSingletonTraits<HostResolverWrapper>; 44 45 net::HostResolver* resolver_; 46 47 DISALLOW_COPY_AND_ASSIGN(HostResolverWrapper); 48}; 49 50} // namespace extensions 51 52#endif // EXTENSIONS_BROWSER_API_DNS_HOST_RESOLVER_WRAPPER_H_ 53