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 ExpireResultForTesting(); |
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 enum ProbeType { | |
55 SYSTEM, | |
56 PUBLIC | |
57 }; | |
58 | |
59 void SetSystemClientToCurrentConfig(); | |
60 void SetPublicClientToGooglePublicDns(); | |
61 | |
62 // Sets the DnsClients used by |system_runner_| and |public_runner_|. | |
63 void SetSystemClient(scoped_ptr<net::DnsClient> system_client); | |
64 void SetPublicClient(scoped_ptr<net::DnsClient> public_client); | |
65 | |
66 // Starts a probe (runs system and public probes). | |
47 void StartProbes(); | 67 void StartProbes(); |
48 void OnProbesComplete(); | 68 void OnProbeComplete(ProbeType type, DnsProbeRunner::Result result); |
69 // Examines |system_result_| and |public_result_| and returns an overall | |
70 // result for why a DNS query may have failed. | |
71 chrome_common_net::DnsProbeStatus EvaluateResults() const; | |
72 // Histograms the results of a just-finished probe. | |
73 void HistogramProbes() const; | |
74 // Calls all |pending_callbacks_| with the |cached_result_|. | |
49 void CallCallbacks(); | 75 void CallCallbacks(); |
76 // Clears a cached probe result. | |
77 void ExpireResult(); | |
szym
2013/06/13 20:50:48
nit: Suggest to name it ClearResult, FlushResult,
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Done.
| |
50 | 78 |
51 void OnProbeJobComplete(DnsProbeJob* job, DnsProbeJob::Result result); | 79 bool ResultsExpired() const; |
52 chrome_common_net::DnsProbeResult EvaluateResults() const; | |
53 void HistogramProbes() const; | |
54 | 80 |
55 // These are expected to be overridden by tests to return mock jobs. | 81 State state_; |
56 virtual scoped_ptr<DnsProbeJob> CreateSystemProbeJob( | 82 std::vector<ProbeCallback> pending_callbacks_; |
57 const DnsProbeJob::CallbackType& job_callback); | 83 base::Time probe_start_time_; |
58 virtual scoped_ptr<DnsProbeJob> CreatePublicProbeJob( | 84 chrome_common_net::DnsProbeStatus cached_result_; |
59 const DnsProbeJob::CallbackType& job_callback); | |
60 | 85 |
61 scoped_ptr<DnsProbeJob> CreateProbeJob( | 86 // DnsProbeRunners for the system DNS configuration and a public DNS server. |
62 const net::DnsConfig& dns_config, | 87 DnsProbeRunner system_runner_; |
63 const DnsProbeJob::CallbackType& job_callback); | 88 DnsProbeRunner public_runner_; |
64 void GetSystemDnsConfig(net::DnsConfig* config); | 89 // Results most recently returned by |system_runner_| and |public_runner_|. |
65 void GetPublicDnsConfig(net::DnsConfig* config); | 90 DnsProbeRunner::Result system_result_; |
66 bool ResultsExpired(); | 91 DnsProbeRunner::Result public_result_; |
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 | 92 |
84 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); | 93 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); |
85 }; | 94 }; |
86 | 95 |
87 } // namespace chrome_browser_net | 96 } // namespace chrome_browser_net |
88 | 97 |
89 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ | 98 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ |
OLD | NEW |