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