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. 4 5 #ifndef CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ 6 #define CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/bind.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/time/time.h" 14 #include "chrome/browser/net/dns_probe_runner.h" 15 #include "chrome/common/net/net_error_info.h" 16 #include "net/base/network_change_notifier.h" 17 18 namespace net { 19 class DnsClient; 20 struct DnsConfig; 21 } 22 23 namespace chrome_browser_net { 24 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 { 31 public: 32 typedef base::Callback<void(chrome_common_net::DnsProbeStatus result)> 33 ProbeCallback; 34 35 DnsProbeService(); 36 virtual ~DnsProbeService(); 37 38 virtual void ProbeDns(const ProbeCallback& callback); 39 40 // NetworkChangeNotifier::DNSObserver implementation: 41 virtual void OnDNSChanged() OVERRIDE; 42 43 void SetSystemClientForTesting(scoped_ptr<net::DnsClient> system_client); 44 void SetPublicClientForTesting(scoped_ptr<net::DnsClient> public_client); 45 void ClearCachedResultForTesting(); 46 47 private: 48 enum State { 49 STATE_NO_RESULT, 50 STATE_PROBE_RUNNING, 51 STATE_RESULT_CACHED, 52 }; 53 54 void SetSystemClientToCurrentConfig(); 55 void SetPublicClientToGooglePublicDns(); 56 57 // Starts a probe (runs system and public probes). 58 void StartProbes(); 59 void OnProbeComplete(); 60 // Calls all |pending_callbacks_| with the |cached_result_|. 61 void CallCallbacks(); 62 // Clears a cached probe result. 63 void ClearCachedResult(); 64 65 bool CachedResultIsExpired() const; 66 67 State state_; 68 std::vector<ProbeCallback> pending_callbacks_; 69 base::Time probe_start_time_; 70 chrome_common_net::DnsProbeStatus cached_result_; 71 72 // DnsProbeRunners for the system DNS configuration and a public DNS server. 73 DnsProbeRunner system_runner_; 74 DnsProbeRunner public_runner_; 75 76 DISALLOW_COPY_AND_ASSIGN(DnsProbeService); 77 }; 78 79 } // namespace chrome_browser_net 80 81 #endif // CHROME_BROWSER_NET_DNS_PROBE_SERVICE_H_ 82