dns_probe_runner.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
14ee2ad04344446e610172a0e73949212923014dfSebastian Redl// Copyright 2013 The Chromium Authors. All rights reserved.
22cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// Use of this source code is governed by a BSD-style license that can be
32cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor// found in the LICENSE file.
42cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
52cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#ifndef CHROME_BROWSER_NET_DNS_PROBE_RUNNER_H_
62cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#define CHROME_BROWSER_NET_DNS_PROBE_RUNNER_H_
72cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
82cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/basictypes.h"
92cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/bind.h"
10a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl#include "base/memory/scoped_ptr.h"
112cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#include "base/memory/weak_ptr.h"
122cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
132cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregornamespace net {
147faa2ec03a7ef120ac165bb45b6c70a8b20c9f1cSebastian Redlclass DnsClient;
150eca89e9890db4d8336ce762a5b359a1d58ca02bArgyrios Kyrtzidisclass DnsResponse;
16e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregorclass DnsTransaction;
17e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor}
182cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
192cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregornamespace chrome_browser_net {
202cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
212a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall// Runs DNS probes using a single DnsClient and evaluates the responses.
2289eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis// (Currently requests A records for google.com and expects at least one IP
230b7489194f9f89fac39d57211c1e7953ae50251fDouglas Gregor// address in the response.)
247a1fad38256eb4c5129359be85ba1ea1678eb5c9John McCall// Used by DnsProbeService to probe the system and public DNS configurations.
252cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregorclass DnsProbeRunner {
26a1ee0c548b8aa4aaf93d1917e304e3da13171a08John McCall public:
276ab7cd853e9c15cf986a8a7c3db1f8d20e275409Sebastian Redl  static const char* kKnownGoodHostname;
287c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner
296a5a23f8e7fb65e028c8092bc1d1a1d9dfe2e9bcDouglas Gregor  // Used in histograms; add new entries at the bottom, and don't remove any.
307c5d24efcd2e505b5739f7def08dfe25ce59a1b2Chris Lattner  enum Result {
3183d63c78810556d26b62ac4cbae2eda6cdd2570cSteve Naroff    UNKNOWN,
3214f79002e58556798e86168c63e48d533287eda5Douglas Gregor    CORRECT,     // Response contains at least one A record.
3310e286aa8d39fb51a21412850265d9dae74613eeChris Lattner    INCORRECT,   // Response claimed success but included no A records.
343251ceb90b3fec68e86d6dcfa58836e20a7205c3Douglas Gregor    FAILING,     // Response included an error or was malformed.
3514f79002e58556798e86168c63e48d533287eda5Douglas Gregor    UNREACHABLE  // No response received (timeout, network unreachable, etc.).
36bd94500d3aa60092fb0f1e90f53fb0d03fa502a8Douglas Gregor  };
372bec0410d268779f601bd509e0302a500af7ac6aDouglas Gregor
38ab41e63821dc60ad144d0684df8d79a9eef86b75Douglas Gregor  DnsProbeRunner();
390a0d2b179085a52c10402feebeb6db8b4d96a140Douglas Gregor  ~DnsProbeRunner();
4017fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor
4117fc223395d51be582fc666bb6ea21bd1dff26dcDouglas Gregor  // Sets the DnsClient that will be used for DNS probes sent by this runner.
422596e429a61602312bdd149786045b8a90cd2d10Daniel Dunbar  // Must be called before RunProbe; can be called repeatedly, including during
432cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // a probe.  It will not affect an in-flight probe, if one is running.
44fbfd180495e7800975c6d9bdc6d24e706ef70e34Michael J. Spencer  void SetClient(scoped_ptr<net::DnsClient> client);
4514f79002e58556798e86168c63e48d533287eda5Douglas Gregor
4603013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer  // Starts a probe using the client specified with SetClient, which must have
47f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  // been called before RunProbe.  |callback| will be called asynchronously
483c304bd9ec2b4611572d4cbae9e1727bbecb5dc9Chris Lattner  // when the result is ready, even if it is ready synchronously.  Must not
49cfbf1c7536e016dc275139dd842d4a5f059a749fDouglas Gregor  // be called again until the callback is called, but may be called during the
50f62d43d2afe1960755a1b5813cae1e5983bcac1bDouglas Gregor  // callback.
512cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  void RunProbe(const base::Closure& callback);
528538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl
532cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // Returns true if a probe is running.  Guaranteed to return true after
54ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  // RunProbe returns, and false during and after the callback.
555f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  bool IsRunning() const;
565f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner
575f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  // Returns the result of the last probe.
586e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  Result result() const { return result_; }
59ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
606e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer private:
616e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  void OnTransactionComplete(net::DnsTransaction* transaction,
625f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                             int net_error,
635f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner                             const net::DnsResponse* response);
646e089c687cc2b914c46859ab7e46fe4c3c6b0afbBenjamin Kramer  void CallCallback();
65ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl
66ade5000c8763f4bec41f452d7efa3a9b2a6d4712Sebastian Redl  scoped_ptr<net::DnsClient> client_;
672cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
682cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // The callback passed to |RunProbe|.  Cleared right before calling the
692cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  // callback.
7012b1c7615d4f9a2edc544be499f895f16ac100edChris Lattner  base::Closure callback_;
712cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
723397c5570369f19b2d6c52e898f708d75ceede1fSebastian Redl  // The transaction started in |RunProbe| for the DNS probe.  Reset once the
73a4232eb646d89e7d52424bb42eb87d9061f39e63Sebastian Redl  // results have been examined.
7489eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  scoped_ptr<net::DnsTransaction> transaction_;
752cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
762cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor  Result result_;
772cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
788538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl  base::WeakPtrFactory<DnsProbeRunner> weak_factory_;
792cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
8089eaf3af92c72c0c1aae807644e39cabc461d685Argyrios Kyrtzidis  DISALLOW_COPY_AND_ASSIGN(DnsProbeRunner);
818538e8d43a3a9bd439c987c0de37bcbf035dd391Sebastian Redl};
822cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
832cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor}  // namespace chrome_browser_net
842cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor
852cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor#endif  // CHROME_BROWSER_NET_DNS_PROBE_RUNNER_H_
862cf2634ffdb4f7c8d46cef3f8e60a55993f1c57aDouglas Gregor