| 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" | |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 14 #include "chrome/browser/net/dns_probe_runner.h" | 13 #include "chrome/browser/net/dns_probe_job.h" |
| 15 #include "chrome/common/net/net_error_info.h" | 14 #include "chrome/common/net/net_error_info.h" |
| 16 #include "net/base/network_change_notifier.h" | 15 #include "net/base/network_change_notifier.h" |
| 17 | 16 |
| 18 namespace net { | 17 namespace net { |
| 19 class DnsClient; | |
| 20 struct DnsConfig; | 18 struct DnsConfig; |
| 21 } | 19 } |
| 22 | 20 |
| 23 namespace chrome_browser_net { | 21 namespace chrome_browser_net { |
| 24 | 22 |
| 25 // Probes the system and public DNS servers to determine the (probable) cause | 23 class DnsProbeService : public net::NetworkChangeNotifier::IPAddressObserver { |
| 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 { | |
| 31 public: | 24 public: |
| 32 typedef base::Callback<void(chrome_common_net::DnsProbeStatus result)> | 25 typedef base::Callback<void(chrome_common_net::DnsProbeResult result)> |
| 33 ProbeCallback; | 26 CallbackType; |
| 34 | 27 |
| 35 DnsProbeService(); | 28 DnsProbeService(); |
| 36 virtual ~DnsProbeService(); | 29 virtual ~DnsProbeService(); |
| 37 | 30 |
| 38 virtual void ProbeDns(const ProbeCallback& callback); | 31 void ProbeDns(const CallbackType& callback); |
| 39 | 32 |
| 40 // NetworkChangeNotifier::DNSObserver implementation: | 33 // NetworkChangeNotifier::IPAddressObserver implementation: |
| 41 virtual void OnDNSChanged() OVERRIDE; | 34 virtual void OnIPAddressChanged() OVERRIDE; |
| 42 | 35 |
| 43 void SetSystemClientForTesting(scoped_ptr<net::DnsClient> system_client); | 36 protected: |
| 44 void SetPublicClientForTesting(scoped_ptr<net::DnsClient> public_client); | 37 // This can be called by tests to pretend the cached reuslt has expired. |
| 45 void ClearCachedResultForTesting(); | 38 void ExpireResults(); |
| 46 | 39 |
| 47 private: | 40 private: |
| 48 enum State { | 41 enum State { |
| 49 STATE_NO_RESULT, | 42 STATE_NO_RESULTS, |
| 50 STATE_PROBE_RUNNING, | 43 STATE_PROBE_RUNNING, |
| 51 STATE_RESULT_CACHED, | 44 STATE_RESULTS_CACHED, |
| 52 }; | 45 }; |
| 53 | 46 |
| 54 void SetSystemClientToCurrentConfig(); | 47 void StartProbes(); |
| 55 void SetPublicClientToGooglePublicDns(); | 48 void OnProbesComplete(); |
| 49 void CallCallbacks(); |
| 56 | 50 |
| 57 // Starts a probe (runs system and public probes). | 51 void OnProbeJobComplete(DnsProbeJob* job, DnsProbeJob::Result result); |
| 58 void StartProbes(); | 52 chrome_common_net::DnsProbeResult EvaluateResults() const; |
| 59 void OnProbeComplete(); | 53 void HistogramProbes() const; |
| 60 // Calls all |pending_callbacks_| with the |cached_result_|. | |
| 61 void CallCallbacks(); | |
| 62 // Clears a cached probe result. | |
| 63 void ClearCachedResult(); | |
| 64 | 54 |
| 65 bool CachedResultIsExpired() const; | 55 // These are expected to be overridden by tests to return mock jobs. |
| 56 virtual scoped_ptr<DnsProbeJob> CreateSystemProbeJob( |
| 57 const DnsProbeJob::CallbackType& job_callback); |
| 58 virtual scoped_ptr<DnsProbeJob> CreatePublicProbeJob( |
| 59 const DnsProbeJob::CallbackType& job_callback); |
| 66 | 60 |
| 61 scoped_ptr<DnsProbeJob> CreateProbeJob( |
| 62 const net::DnsConfig& dns_config, |
| 63 const DnsProbeJob::CallbackType& job_callback); |
| 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_; |
| 67 State state_; | 73 State state_; |
| 68 std::vector<ProbeCallback> pending_callbacks_; | 74 chrome_common_net::DnsProbeResult result_; |
| 69 base::Time probe_start_time_; | 75 base::Time probe_start_time_; |
| 70 chrome_common_net::DnsProbeStatus cached_result_; | 76 // How many DNS request attempts the probe jobs will make before giving up |
| 71 | 77 // (Overrides the attempts field in the system DnsConfig.) |
| 72 // DnsProbeRunners for the system DNS configuration and a public DNS server. | 78 const int dns_attempts_; |
| 73 DnsProbeRunner system_runner_; | 79 // How many nameservers the system config has. |
| 74 DnsProbeRunner public_runner_; | 80 int system_nameserver_count_; |
| 81 // Whether the only system nameserver is 127.0.0.1. |
| 82 bool system_is_localhost_; |
| 75 | 83 |
| 76 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); | 84 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); |
| 77 }; | 85 }; |
| 78 | 86 |
| 79 } // namespace chrome_browser_net | 87 } // namespace chrome_browser_net |
| 80 | 88 |
| 81 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ | 89 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ |
| OLD | NEW |