OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ |
6 #define CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ | 6 #define CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/bind.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/time.h" | 13 #include "base/time.h" |
13 #include "chrome/browser/net/dns_probe_job.h" | 14 #include "chrome/browser/net/dns_probe_runner.h" |
14 #include "chrome/common/net/net_error_info.h" | 15 #include "chrome/common/net/net_error_info.h" |
15 #include "net/base/network_change_notifier.h" | 16 #include "net/base/network_change_notifier.h" |
16 | 17 |
17 namespace net { | 18 namespace net { |
| 19 class DnsClient; |
18 struct DnsConfig; | 20 struct DnsConfig; |
19 } | 21 } |
20 | 22 |
21 namespace chrome_browser_net { | 23 namespace chrome_browser_net { |
22 | 24 |
23 class DnsProbeService : public net::NetworkChangeNotifier::IPAddressObserver { | 25 // Probes the system and public DNS servers to determine the (probable) cause |
| 26 // of a recent DNS-related page load error. Coalesces multiple probe requests |
| 27 // (perhaps from multiple tabs) and caches the results. |
| 28 // |
| 29 // Uses a single DNS attempt per config, and doesn't randomize source ports. |
| 30 class DnsProbeService : public net::NetworkChangeNotifier::DNSObserver { |
24 public: | 31 public: |
25 typedef base::Callback<void(chrome_common_net::DnsProbeResult result)> | 32 typedef base::Callback<void(chrome_common_net::DnsProbeStatus result)> |
26 CallbackType; | 33 ProbeCallback; |
27 | 34 |
28 DnsProbeService(); | 35 DnsProbeService(); |
29 virtual ~DnsProbeService(); | 36 virtual ~DnsProbeService(); |
30 | 37 |
31 void ProbeDns(const CallbackType& callback); | 38 void ProbeDns(const ProbeCallback& callback); |
32 | 39 |
33 // NetworkChangeNotifier::IPAddressObserver implementation: | 40 // NetworkChangeNotifier::DNSObserver implementation: |
34 virtual void OnIPAddressChanged() OVERRIDE; | 41 virtual void OnDNSChanged() OVERRIDE; |
35 | 42 |
36 protected: | 43 void SetSystemClientForTesting(scoped_ptr<net::DnsClient> system_client); |
37 // This can be called by tests to pretend the cached reuslt has expired. | 44 void SetPublicClientForTesting(scoped_ptr<net::DnsClient> public_client); |
38 void ExpireResults(); | 45 void ClearCachedResultForTesting(); |
39 | 46 |
40 private: | 47 private: |
41 enum State { | 48 enum State { |
42 STATE_NO_RESULTS, | 49 STATE_NO_RESULTS, |
43 STATE_PROBE_RUNNING, | 50 STATE_PROBE_RUNNING, |
44 STATE_RESULTS_CACHED, | 51 STATE_RESULTS_CACHED, |
45 }; | 52 }; |
46 | 53 |
| 54 void SetSystemClientToCurrentConfig(); |
| 55 void SetPublicClientToGooglePublicDns(); |
| 56 |
| 57 // Starts a probe (runs system and public probes). |
47 void StartProbes(); | 58 void StartProbes(); |
48 void OnProbesComplete(); | 59 void OnProbeComplete(); |
| 60 // Calls all |pending_callbacks_| with the |cached_result_|. |
49 void CallCallbacks(); | 61 void CallCallbacks(); |
| 62 // Clears a cached probe result. |
| 63 void ClearCachedResult(); |
50 | 64 |
51 void OnProbeJobComplete(DnsProbeJob* job, DnsProbeJob::Result result); | 65 bool CachedResultIsExpired() const; |
52 chrome_common_net::DnsProbeResult EvaluateResults() const; | |
53 void HistogramProbes() const; | |
54 | 66 |
55 // These are expected to be overridden by tests to return mock jobs. | 67 State state_; |
56 virtual scoped_ptr<DnsProbeJob> CreateSystemProbeJob( | 68 std::vector<ProbeCallback> pending_callbacks_; |
57 const DnsProbeJob::CallbackType& job_callback); | 69 base::Time probe_start_time_; |
58 virtual scoped_ptr<DnsProbeJob> CreatePublicProbeJob( | 70 chrome_common_net::DnsProbeStatus cached_result_; |
59 const DnsProbeJob::CallbackType& job_callback); | |
60 | 71 |
61 scoped_ptr<DnsProbeJob> CreateProbeJob( | 72 // DnsProbeRunners for the system DNS configuration and a public DNS server. |
62 const net::DnsConfig& dns_config, | 73 DnsProbeRunner system_runner_; |
63 const DnsProbeJob::CallbackType& job_callback); | 74 DnsProbeRunner public_runner_; |
64 void GetSystemDnsConfig(net::DnsConfig* config); | |
65 void GetPublicDnsConfig(net::DnsConfig* config); | |
66 bool ResultsExpired(); | |
67 | |
68 scoped_ptr<DnsProbeJob> system_job_; | |
69 scoped_ptr<DnsProbeJob> public_job_; | |
70 DnsProbeJob::Result system_result_; | |
71 DnsProbeJob::Result public_result_; | |
72 std::vector<CallbackType> callbacks_; | |
73 State state_; | |
74 chrome_common_net::DnsProbeResult result_; | |
75 base::Time probe_start_time_; | |
76 // How many DNS request attempts the probe jobs will make before giving up | |
77 // (Overrides the attempts field in the system DnsConfig.) | |
78 const int dns_attempts_; | |
79 // How many nameservers the system config has. | |
80 int system_nameserver_count_; | |
81 // Whether the only system nameserver is 127.0.0.1. | |
82 bool system_is_localhost_; | |
83 | 75 |
84 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); | 76 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); |
85 }; | 77 }; |
86 | 78 |
87 } // namespace chrome_browser_net | 79 } // namespace chrome_browser_net |
88 | 80 |
89 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ | 81 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ |
OLD | NEW |