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 CHROMEOS_NETWORK_HOST_RESOLVER_IMPL_CHROMEOS_H_
6#define CHROMEOS_NETWORK_HOST_RESOLVER_IMPL_CHROMEOS_H_
7
8#include "base/memory/ref_counted.h"
9#include "base/threading/thread_checker.h"
10#include "chromeos/chromeos_export.h"
11#include "net/dns/host_resolver_impl.h"
12
13namespace base {
14class MessageLoopProxy;
15}
16
17namespace chromeos {
18class NetworkStateHandler;
19}
20
21namespace chromeos {
22
23// HostResolverImplChromeOS overrides HostResolverImpl::Resolve in order to
24// provide the correct IP addresses for localhost using the chromeos
25// NetworkHandler interface. ('hostname' only returns 'localhost' on cros).
26
27class CHROMEOS_EXPORT HostResolverImplChromeOS : public net::HostResolverImpl {
28 public:
29  // ChromeOS specific implementation of HostResolver::CreateSystemResolver.
30  // Assumes NetworkHandler has been initialized.
31  // This is expected to be constructed on the same thread that Resolve() is
32  // called from, i.e. the IO thread, which is presumed to differ from the
33  // thread that NetworkStateHandler is called on, i.e. the UI thread.
34  static scoped_ptr<net::HostResolver> CreateSystemResolver(
35      const Options& options,
36      net::NetLog* net_log);
37
38  // Creates a host resolver instance for testing.
39  static scoped_ptr<net::HostResolver> CreateHostResolverForTest(
40      scoped_refptr<base::MessageLoopProxy> network_handler_message_loop,
41      NetworkStateHandler* network_state_handler);
42
43  virtual ~HostResolverImplChromeOS();
44
45  // HostResolverImpl
46  virtual int Resolve(const RequestInfo& info,
47                      net::RequestPriority priority,
48                      net::AddressList* addresses,
49                      const net::CompletionCallback& callback,
50                      RequestHandle* out_req,
51                      const net::BoundNetLog& source_net_log) OVERRIDE;
52
53 private:
54  friend class net::HostResolver;
55  class NetworkObserver;
56
57  HostResolverImplChromeOS(
58      scoped_refptr<base::MessageLoopProxy> network_handler_message_loop,
59      NetworkStateHandler* network_state_handler,
60      const Options& options,
61      net::NetLog* net_log);
62
63  void SetIPAddresses(const std::string& ipv4_address,
64                      const std::string& ipv6_address);
65
66  bool ResolveLocalIPAddress(const RequestInfo& info,
67                             net::AddressList* addresses);
68
69  std::string ipv4_address_;
70  std::string ipv6_address_;
71  base::ThreadChecker thread_checker_;
72  scoped_refptr<base::MessageLoopProxy> network_handler_message_loop_;
73  base::WeakPtrFactory<HostResolverImplChromeOS> weak_ptr_factory_;
74
75  DISALLOW_COPY_AND_ASSIGN(HostResolverImplChromeOS);
76};
77
78}  // namespace chromeos
79
80#endif  // CHROMEOS_NETWORK_HOST_RESOLVER_IMPL_CHROMEOS_H_
81