OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 NET_DNS_ASYNC_HOST_RESOLVER_H_ | 5 #ifndef NET_DNS_ASYNC_HOST_RESOLVER_H_ |
6 #define NET_DNS_ASYNC_HOST_RESOLVER_H_ | 6 #define NET_DNS_ASYNC_HOST_RESOLVER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <list> | 9 #include <list> |
10 #include <map> | 10 #include <map> |
11 #include <string> | |
12 #include <utility> | |
13 | 11 |
14 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
15 #include "net/base/address_family.h" | 13 #include "net/base/address_family.h" |
16 #include "net/base/host_cache.h" | 14 #include "net/base/host_cache.h" |
17 #include "net/base/host_resolver.h" | 15 #include "net/base/host_resolver.h" |
18 #include "net/base/ip_endpoint.h" | 16 #include "net/base/ip_endpoint.h" |
19 #include "net/base/net_log.h" | 17 #include "net/base/net_log.h" |
20 #include "net/dns/dns_client.h" | 18 #include "net/base/rand_callback.h" |
| 19 #include "net/dns/dns_transaction.h" |
21 | 20 |
22 namespace net { | 21 namespace net { |
23 | 22 |
| 23 class ClientSocketFactory; |
| 24 |
24 class NET_EXPORT AsyncHostResolver | 25 class NET_EXPORT AsyncHostResolver |
25 : public HostResolver, | 26 : public HostResolver, |
| 27 public DnsTransaction::Delegate, |
26 NON_EXPORTED_BASE(public base::NonThreadSafe) { | 28 NON_EXPORTED_BASE(public base::NonThreadSafe) { |
27 public: | 29 public: |
28 AsyncHostResolver(size_t max_dns_requests, | 30 AsyncHostResolver(const IPEndPoint& dns_server, |
29 size_t max_pending_requests, | 31 size_t max_transactions, |
| 32 size_t max_pending_requests_, |
| 33 const RandIntCallback& rand_int, |
30 HostCache* cache, | 34 HostCache* cache, |
31 DnsClient* client, | 35 ClientSocketFactory* factory, |
32 NetLog* net_log); | 36 NetLog* net_log); |
33 virtual ~AsyncHostResolver(); | 37 virtual ~AsyncHostResolver(); |
34 | 38 |
35 // HostResolver interface | 39 // HostResolver interface |
36 virtual int Resolve(const RequestInfo& info, | 40 virtual int Resolve(const RequestInfo& info, |
37 AddressList* addresses, | 41 AddressList* addresses, |
38 const CompletionCallback& callback, | 42 const CompletionCallback& callback, |
39 RequestHandle* out_req, | 43 RequestHandle* out_req, |
40 const BoundNetLog& source_net_log) OVERRIDE; | 44 const BoundNetLog& source_net_log) OVERRIDE; |
41 virtual int ResolveFromCache(const RequestInfo& info, | 45 virtual int ResolveFromCache(const RequestInfo& info, |
42 AddressList* addresses, | 46 AddressList* addresses, |
43 const BoundNetLog& source_net_log) OVERRIDE; | 47 const BoundNetLog& source_net_log) OVERRIDE; |
44 virtual void CancelRequest(RequestHandle req_handle) OVERRIDE; | 48 virtual void CancelRequest(RequestHandle req_handle) OVERRIDE; |
45 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; | 49 virtual void SetDefaultAddressFamily(AddressFamily address_family) OVERRIDE; |
46 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; | 50 virtual AddressFamily GetDefaultAddressFamily() const OVERRIDE; |
47 virtual HostCache* GetHostCache() OVERRIDE; | 51 virtual HostCache* GetHostCache() OVERRIDE; |
48 | 52 |
49 void OnDnsRequestComplete(DnsClient::Request* request, | 53 // DnsTransaction::Delegate interface |
50 int result, | 54 virtual void OnTransactionComplete( |
51 const DnsResponse* transaction); | 55 int result, |
| 56 const DnsTransaction* transaction, |
| 57 const IPAddressList& ip_addresses) OVERRIDE; |
52 | 58 |
53 private: | 59 private: |
54 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, QueuedLookup); | 60 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, QueuedLookup); |
55 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, CancelPendingLookup); | 61 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, CancelPendingLookup); |
56 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, | 62 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, |
57 ResolverDestructionCancelsLookups); | 63 ResolverDestructionCancelsLookups); |
58 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, | 64 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, |
59 OverflowQueueWithLowPriorityLookup); | 65 OverflowQueueWithLowPriorityLookup); |
60 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, | 66 FRIEND_TEST_ALL_PREFIXES(AsyncHostResolverTest, |
61 OverflowQueueWithHighPriorityLookup); | 67 OverflowQueueWithHighPriorityLookup); |
62 | 68 |
63 class Request; | 69 class Request; |
64 | 70 |
65 typedef std::pair<std::string, uint16> Key; | 71 typedef DnsTransaction::Key Key; |
66 typedef std::list<Request*> RequestList; | 72 typedef std::list<Request*> RequestList; |
67 typedef std::list<const DnsClient::Request*> DnsRequestList; | 73 typedef std::list<const DnsTransaction*> TransactionList; |
68 typedef std::map<Key, RequestList> KeyRequestListMap; | 74 typedef std::map<Key, RequestList> KeyRequestListMap; |
69 | 75 |
70 // Create a new request for the incoming Resolve() call. | 76 // Create a new request for the incoming Resolve() call. |
71 Request* CreateNewRequest(const RequestInfo& info, | 77 Request* CreateNewRequest(const RequestInfo& info, |
72 const CompletionCallback& callback, | 78 const CompletionCallback& callback, |
73 AddressList* addresses, | 79 AddressList* addresses, |
74 const BoundNetLog& source_net_log); | 80 const BoundNetLog& source_net_log); |
75 | 81 |
76 // Called when a request has just been started. | 82 // Called when a request has just been started. |
77 void OnStart(Request* request); | 83 void OnStart(Request* request); |
78 | 84 |
79 // Called when a request has just completed (before its callback is run). | 85 // Called when a request has just completed (before its callback is run). |
80 void OnFinish(Request* request, int result); | 86 void OnFinish(Request* request, int result); |
81 | 87 |
82 // Called when a request has been cancelled. | 88 // Called when a request has been cancelled. |
83 void OnCancel(Request* request); | 89 void OnCancel(Request* request); |
84 | 90 |
85 // If there is an in-progress transaction for Request->key(), this will | 91 // If there is an in-progress transaction for Request->key(), this will |
86 // attach |request| to the respective list. | 92 // attach |request| to the respective list. |
87 bool AttachToRequestList(Request* request); | 93 bool AttachToRequestList(Request* request); |
88 | 94 |
89 // Will start a new DNS request for |request|, will insert a new key in | 95 // Will start a new transaction for |request|, will insert a new key in |
90 // |requestlist_map_| and append |request| to the respective list. | 96 // |requestlist_map_| and append |request| to the respective list. |
91 int StartNewDnsRequestFor(Request* request); | 97 int StartNewTransactionFor(Request* request); |
92 | 98 |
93 // Will enqueue |request| in |pending_requests_|. | 99 // Will enqueue |request| in |pending_requests_|. |
94 int Enqueue(Request* request); | 100 int Enqueue(Request* request); |
95 | 101 |
96 // A helper used by Enqueue to insert |request| into |pending_requests_|. | 102 // A helper used by Enqueue to insert |request| into |pending_requests_|. |
97 Request* Insert(Request* request); | 103 Request* Insert(Request* request); |
98 | 104 |
99 // Returns the number of pending requests. | 105 // Returns the number of pending requests. |
100 size_t GetNumPending() const; | 106 size_t GetNumPending() const; |
101 | 107 |
102 // Removes and returns a pointer to the lowest/highest priority request | 108 // Removes and returns a pointer to the lowest/highest priority request |
103 // from |pending_requests_|. | 109 // from |pending_requests_|. |
104 Request* RemoveLowest(); | 110 Request* RemoveLowest(); |
105 Request* RemoveHighest(); | 111 Request* RemoveHighest(); |
106 | 112 |
107 // Once a transaction has completed, called to start a new transaction if | 113 // Once a transaction has completed, called to start a new transaction if |
108 // there are pending requests. | 114 // there are pending requests. |
109 void ProcessPending(); | 115 void ProcessPending(); |
110 | 116 |
111 // Maximum number of concurrent DNS requests. | 117 // Maximum number of concurrent transactions. |
112 size_t max_dns_requests_; | 118 size_t max_transactions_; |
113 | 119 |
114 // List of current DNS requests. | 120 // List of current transactions. |
115 DnsRequestList dns_requests_; | 121 TransactionList transactions_; |
116 | 122 |
117 // A map from Key to a list of requests waiting for the Key to resolve. | 123 // A map from Key to a list of requests waiting for the Key to resolve. |
118 KeyRequestListMap requestlist_map_; | 124 KeyRequestListMap requestlist_map_; |
119 | 125 |
120 // Maximum number of pending requests. | 126 // Maximum number of pending requests. |
121 size_t max_pending_requests_; | 127 size_t max_pending_requests_; |
122 | 128 |
123 // Queues based on priority for putting pending requests. | 129 // Queues based on priority for putting pending requests. |
124 RequestList pending_requests_[NUM_PRIORITIES]; | 130 RequestList pending_requests_[NUM_PRIORITIES]; |
125 | 131 |
| 132 // DNS server to which queries will be setn. |
| 133 IPEndPoint dns_server_; |
| 134 |
| 135 // Callback to be passed to DnsTransaction for generating DNS query ids. |
| 136 RandIntCallback rand_int_cb_; |
| 137 |
126 // Cache of host resolution results. | 138 // Cache of host resolution results. |
127 scoped_ptr<HostCache> cache_; | 139 scoped_ptr<HostCache> cache_; |
128 | 140 |
129 DnsClient* client_; | 141 // Also passed to DnsTransaction; it's a dependency injection to aid |
| 142 // testing, outside of unit tests, its value is always NULL. |
| 143 ClientSocketFactory* factory_; |
130 | 144 |
131 NetLog* net_log_; | 145 NetLog* net_log_; |
132 | 146 |
133 DISALLOW_COPY_AND_ASSIGN(AsyncHostResolver); | 147 DISALLOW_COPY_AND_ASSIGN(AsyncHostResolver); |
134 }; | 148 }; |
135 | 149 |
136 } // namespace net | 150 } // namespace net |
137 | 151 |
138 #endif // NET_DNS_ASYNC_HOST_RESOLVER_H_ | 152 #endif // NET_DNS_ASYNC_HOST_RESOLVER_H_ |
OLD | NEW |