Cast: Skip receiver log messages with time delta that can't be encoded.
[chromium-blink-merge.git] / net / dns / dns_config_service.h
blob7386e601790fe80e78b7f815a49efbed2728ace2
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_DNS_CONFIG_SERVICE_H_
6 #define NET_DNS_DNS_CONFIG_SERVICE_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/threading/non_thread_safe.h"
15 #include "base/time/time.h"
16 #include "base/timer/timer.h"
17 // Needed on shared build with MSVS2010 to avoid multiple definitions of
18 // std::vector<IPEndPoint>.
19 #include "net/base/address_list.h"
20 #include "net/base/ip_endpoint.h" // win requires size of IPEndPoint
21 #include "net/base/net_export.h"
22 #include "net/dns/dns_hosts.h"
24 namespace base {
25 class Value;
28 namespace net {
30 // Always use 1 second timeout (followed by binary exponential backoff).
31 // TODO(szym): Remove code which reads timeout from system.
32 const unsigned kDnsTimeoutSeconds = 1;
34 // DnsConfig stores configuration of the system resolver.
35 struct NET_EXPORT_PRIVATE DnsConfig {
36 DnsConfig();
37 virtual ~DnsConfig();
39 bool Equals(const DnsConfig& d) const;
41 bool EqualsIgnoreHosts(const DnsConfig& d) const;
43 void CopyIgnoreHosts(const DnsConfig& src);
45 // Returns a Value representation of |this|. Caller takes ownership of the
46 // returned Value. For performance reasons, the Value only contains the
47 // number of hosts rather than the full list.
48 base::Value* ToValue() const;
50 bool IsValid() const {
51 return !nameservers.empty();
54 // List of name server addresses.
55 std::vector<IPEndPoint> nameservers;
56 // Suffix search list; used on first lookup when number of dots in given name
57 // is less than |ndots|.
58 std::vector<std::string> search;
60 DnsHosts hosts;
62 // True if there are options set in the system configuration that are not yet
63 // supported by DnsClient.
64 bool unhandled_options;
66 // AppendToMultiLabelName: is suffix search performed for multi-label names?
67 // True, except on Windows where it can be configured.
68 bool append_to_multi_label_name;
70 // Indicates that source port randomization is required. This uses additional
71 // resources on some platforms.
72 bool randomize_ports;
74 // Resolver options; see man resolv.conf.
76 // Minimum number of dots before global resolution precedes |search|.
77 int ndots;
78 // Time between retransmissions, see res_state.retrans.
79 base::TimeDelta timeout;
80 // Maximum number of attempts, see res_state.retry.
81 int attempts;
82 // Round robin entries in |nameservers| for subsequent requests.
83 bool rotate;
84 // Enable EDNS0 extensions.
85 bool edns0;
87 // Indicates system configuration uses local IPv6 connectivity, e.g.,
88 // DirectAccess. This is exposed for HostResolver to skip IPv6 probes,
89 // as it may cause them to return incorrect results.
90 bool use_local_ipv6;
94 // Service for reading system DNS settings, on demand or when signalled by
95 // internal watchers and NetworkChangeNotifier.
96 class NET_EXPORT_PRIVATE DnsConfigService
97 : NON_EXPORTED_BASE(public base::NonThreadSafe) {
98 public:
99 // Callback interface for the client, called on the same thread as
100 // ReadConfig() and WatchConfig().
101 typedef base::Callback<void(const DnsConfig& config)> CallbackType;
103 // Creates the platform-specific DnsConfigService.
104 static scoped_ptr<DnsConfigService> CreateSystemService();
106 DnsConfigService();
107 virtual ~DnsConfigService();
109 // Attempts to read the configuration. Will run |callback| when succeeded.
110 // Can be called at most once.
111 void ReadConfig(const CallbackType& callback);
113 // Registers systems watchers. Will attempt to read config after watch starts,
114 // but only if watchers started successfully. Will run |callback| iff config
115 // changes from last call or has to be withdrawn. Can be called at most once.
116 // Might require MessageLoopForIO.
117 void WatchConfig(const CallbackType& callback);
119 protected:
120 enum WatchStatus {
121 DNS_CONFIG_WATCH_STARTED = 0,
122 DNS_CONFIG_WATCH_FAILED_TO_START_CONFIG,
123 DNS_CONFIG_WATCH_FAILED_TO_START_HOSTS,
124 DNS_CONFIG_WATCH_FAILED_CONFIG,
125 DNS_CONFIG_WATCH_FAILED_HOSTS,
126 DNS_CONFIG_WATCH_MAX,
129 // Immediately attempts to read the current configuration.
130 virtual void ReadNow() = 0;
131 // Registers system watchers. Returns true iff succeeds.
132 virtual bool StartWatching() = 0;
134 // Called when the current config (except hosts) has changed.
135 void InvalidateConfig();
136 // Called when the current hosts have changed.
137 void InvalidateHosts();
139 // Called with new config. |config|.hosts is ignored.
140 void OnConfigRead(const DnsConfig& config);
141 // Called with new hosts. Rest of the config is assumed unchanged.
142 void OnHostsRead(const DnsHosts& hosts);
144 void set_watch_failed(bool value) { watch_failed_ = value; }
146 private:
147 // The timer counts from the last Invalidate* until complete config is read.
148 void StartTimer();
149 void OnTimeout();
150 // Called when the config becomes complete. Stops the timer.
151 void OnCompleteConfig();
153 CallbackType callback_;
155 DnsConfig dns_config_;
157 // True if any of the necessary watchers failed. In that case, the service
158 // will communicate changes via OnTimeout, but will only send empty DnsConfig.
159 bool watch_failed_;
160 // True after On*Read, before Invalidate*. Tells if the config is complete.
161 bool have_config_;
162 bool have_hosts_;
163 // True if receiver needs to be updated when the config becomes complete.
164 bool need_update_;
165 // True if the last config sent was empty (instead of |dns_config_|).
166 // Set when |timer_| expires.
167 bool last_sent_empty_;
169 // Initialized and updated on Invalidate* call.
170 base::TimeTicks last_invalidate_config_time_;
171 base::TimeTicks last_invalidate_hosts_time_;
172 // Initialized and updated when |timer_| expires.
173 base::TimeTicks last_sent_empty_time_;
175 // Started in Invalidate*, cleared in On*Read.
176 base::OneShotTimer<DnsConfigService> timer_;
178 DISALLOW_COPY_AND_ASSIGN(DnsConfigService);
181 } // namespace net
183 #endif // NET_DNS_DNS_CONFIG_SERVICE_H_