OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 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 #include "chrome/browser/net/dns_probe_runner.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 #include "net/base/address_list.h" | |
10 #include "net/base/ip_endpoint.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "net/base/net_log.h" | |
13 #include "net/base/net_util.h" | |
14 #include "net/base/network_change_notifier.h" | |
15 #include "net/dns/dns_client.h" | |
16 #include "net/dns/dns_protocol.h" | |
17 #include "net/dns/dns_response.h" | |
18 #include "net/dns/dns_transaction.h" | |
19 | |
20 using base::TimeDelta; | |
21 using content::BrowserThread; | |
22 using net::AddressList; | |
23 using net::BoundNetLog; | |
24 using net::DnsClient; | |
25 using net::DnsResponse; | |
26 using net::DnsTransaction; | |
27 using net::IPAddressNumber; | |
28 using net::IPEndPoint; | |
29 using net::NetLog; | |
30 using net::NetworkChangeNotifier; | |
31 using net::ParseIPLiteralToNumber; | |
32 | |
33 namespace chrome_browser_net { | |
34 | |
35 namespace { | |
36 | |
37 const char* kKnownGoodHostname = "google.com"; | |
38 | |
39 DnsProbeRunner::Result EvaluateResponse( | |
40 int net_error, | |
41 const DnsResponse* response) { | |
42 switch (net_error) { | |
43 case net::OK: | |
szym
2013/06/13 20:50:48
nit: indent
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Done.
| |
44 break; | |
45 | |
46 // ERR_NAME_NOT_RESOLVED maps to NXDOMAIN, which means the server is working | |
47 // but gave us a wrong answer. | |
48 case net::ERR_NAME_NOT_RESOLVED: | |
49 return DnsProbeRunner::INCORRECT; | |
50 | |
51 // These results mean we heard *something* from the DNS server, but it was | |
52 // unsuccessful (SERVFAIL) or malformed. | |
53 case net::ERR_DNS_MALFORMED_RESPONSE: | |
54 case net::ERR_DNS_SERVER_REQUIRES_TCP: // Shouldn't happen; DnsTransaction | |
55 // will retry with TCP. | |
56 case net::ERR_DNS_SERVER_FAILED: | |
57 case net::ERR_DNS_SORT_ERROR: // Can only happen if the server responds. | |
58 return DnsProbeRunner::FAILING; | |
59 | |
60 // Any other error means we never reached the DNS server in the first place. | |
61 case net::ERR_DNS_TIMED_OUT: | |
62 default: | |
63 // Something else happened, probably at a network level. | |
64 return DnsProbeRunner::UNREACHABLE; | |
65 } | |
66 | |
67 AddressList addr_list; | |
68 TimeDelta ttl; | |
69 DnsResponse::Result result = response->ParseToAddressList(&addr_list, &ttl); | |
70 | |
71 if (result != DnsResponse::DNS_PARSE_OK) { | |
72 return DnsProbeRunner::FAILING; | |
73 } | |
74 | |
75 if (addr_list.empty()) { | |
76 return DnsProbeRunner::INCORRECT; | |
77 } | |
78 | |
79 return DnsProbeRunner::CORRECT; | |
80 } | |
81 | |
82 } // namespace | |
83 | |
84 DnsProbeRunner::DnsProbeRunner() : weak_factory_(this) {} | |
85 | |
86 DnsProbeRunner::~DnsProbeRunner() {} | |
87 | |
88 void DnsProbeRunner::RunProbe(const ProbeCallback& callback) { | |
89 DCHECK(client_.get()); | |
90 DCHECK(!IsRunning()); | |
91 DCHECK(!callback.is_null()); | |
92 | |
93 callback_ = callback; | |
94 transaction_ = client_->GetTransactionFactory()->CreateTransaction( | |
95 kKnownGoodHostname, | |
96 net::dns_protocol::kTypeA, | |
97 base::Bind(&DnsProbeRunner::OnTransactionComplete, | |
98 weak_factory_.GetWeakPtr()), | |
99 BoundNetLog()); | |
100 | |
101 int rv = transaction_->Start(); | |
102 if (rv != net::ERR_IO_PENDING) | |
103 OnTransactionComplete(transaction_.get(), rv, NULL); | |
104 } | |
105 | |
106 void DnsProbeRunner::SetClient(scoped_ptr<net::DnsClient> client) { | |
107 client_ = client.Pass(); | |
108 } | |
109 | |
110 bool DnsProbeRunner::IsRunning() const { | |
111 return !callback_.is_null(); | |
112 } | |
113 | |
114 void DnsProbeRunner::OnTransactionComplete( | |
115 DnsTransaction* transaction, | |
116 int net_error, | |
117 const DnsResponse* response) { | |
118 DCHECK(IsRunning()); | |
119 DCHECK_EQ(transaction_.get(), transaction); | |
120 | |
121 const Result result = EvaluateResponse(net_error, response); | |
122 transaction_.reset(); | |
123 | |
124 BrowserThread::PostTask( | |
125 BrowserThread::IO, | |
126 FROM_HERE, | |
127 base::Bind(&DnsProbeRunner::CallCallback, | |
128 weak_factory_.GetWeakPtr(), | |
129 result)); | |
130 } | |
131 | |
132 void DnsProbeRunner::CallCallback(Result result) { | |
133 const ProbeCallback callback = callback_; | |
134 callback_ = ProbeCallback(); | |
szym
2013/06/13 20:50:48
Suggest: callback_.Reset()
Deprecated (see juliatuttle)
2013/06/18 19:36:37
Done.
| |
135 callback.Run(result); | |
136 } | |
137 | |
138 } // namespace chrome_browser_net | |
OLD | NEW |