OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 // A DnsMaster object is instantiated once in the browser | 5 // A DnsMaster object is instantiated once in the browser |
6 // process, and manages asynchronous resolution of DNS hostnames. | 6 // process, and manages asynchronous resolution of DNS hostnames. |
7 // Most hostname lists are sent out by renderer processes, and | 7 // Most hostname lists are sent out by renderer processes, and |
8 // involve lists of hostnames that *might* be used in the near | 8 // involve lists of hostnames that *might* be used in the near |
9 // future by the browsing user. The goal of this class is to | 9 // future by the browsing user. The goal of this class is to |
10 // cause the underlying DNS structure to lookup a hostname before | 10 // cause the underlying DNS structure to lookup a hostname before |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "testing/gtest/include/gtest/gtest_prod.h" | 26 #include "testing/gtest/include/gtest/gtest_prod.h" |
27 | 27 |
28 namespace chrome_browser_net { | 28 namespace chrome_browser_net { |
29 | 29 |
30 typedef chrome_common_net::NameList NameList; | 30 typedef chrome_common_net::NameList NameList; |
31 typedef std::map<std::string, DnsHostInfo> Results; | 31 typedef std::map<std::string, DnsHostInfo> Results; |
32 | 32 |
33 class DnsMaster { | 33 class DnsMaster { |
34 public: | 34 public: |
35 // Specify how many concurrent (paralell) prefetches will be performed. | 35 // Specify how many concurrent (paralell) prefetches will be performed. |
36 explicit DnsMaster(size_t max_concurrent); | 36 DnsMaster(size_t max_concurrent); |
37 ~DnsMaster(); | 37 ~DnsMaster(); |
38 | 38 |
39 // Cancel pending requests and prevent new ones from being made. | 39 // Cancel pending requests and prevent new ones from being made. |
40 void Shutdown(); | 40 void Shutdown(); |
41 | 41 |
42 // In some circumstances, for privacy reasons, all results should be | 42 // In some circumstances, for privacy reasons, all results should be |
43 // discarded. This method gracefully handles that activity. | 43 // discarded. This method gracefully handles that activity. |
44 // Destroy all our internal state, which shows what names we've looked up, and | 44 // Destroy all our internal state, which shows what names we've looked up, and |
45 // how long each has taken, etc. etc. We also destroy records of suggesses | 45 // how long each has taken, etc. etc. We also destroy records of suggesses |
46 // (cache hits etc.). | 46 // (cache hits etc.). |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 // list, as constructed by SerializeReferrers(), and add all the identified | 86 // list, as constructed by SerializeReferrers(), and add all the identified |
87 // values into the current referrer list. | 87 // values into the current referrer list. |
88 void DeserializeReferrers(const ListValue& referral_list); | 88 void DeserializeReferrers(const ListValue& referral_list); |
89 | 89 |
90 private: | 90 private: |
91 FRIEND_TEST(DnsMasterTest, BenefitLookupTest); | 91 FRIEND_TEST(DnsMasterTest, BenefitLookupTest); |
92 FRIEND_TEST(DnsMasterTest, ShutdownWhenResolutionIsPendingTest); | 92 FRIEND_TEST(DnsMasterTest, ShutdownWhenResolutionIsPendingTest); |
93 FRIEND_TEST(DnsMasterTest, SingleLookupTest); | 93 FRIEND_TEST(DnsMasterTest, SingleLookupTest); |
94 FRIEND_TEST(DnsMasterTest, ConcurrentLookupTest); | 94 FRIEND_TEST(DnsMasterTest, ConcurrentLookupTest); |
95 FRIEND_TEST(DnsMasterTest, MassiveConcurrentLookupTest); | 95 FRIEND_TEST(DnsMasterTest, MassiveConcurrentLookupTest); |
96 FRIEND_TEST(DnsMasterTest, PriorityQueuePushPopTest); | |
97 FRIEND_TEST(DnsMasterTest, PriorityQueueReorderTest); | |
98 friend class WaitForResolutionHelper; // For testing. | 96 friend class WaitForResolutionHelper; // For testing. |
99 | 97 |
100 class LookupRequest; | 98 class LookupRequest; |
101 | 99 |
102 // A simple priority queue for handling host names. | |
103 // Some names that are queued up have |motivation| that requires very rapid | |
104 // handling. For example, a sub-resource name lookup MUST be done before the | |
105 // actual sub-resource is fetched. In contrast, a name that was speculatively | |
106 // noted in a page has to be resolved before the user "gets around to" | |
107 // clicking on a link. By tagging (with a motivation) each push we make into | |
108 // this FIFO queue, the queue can re-order the more important names to service | |
109 // them sooner (relative to some low priority background resolutions). | |
110 class HostNameQueue { | |
111 public: | |
112 HostNameQueue(); | |
113 ~HostNameQueue(); | |
114 void Push(const std::string& hostname, | |
115 DnsHostInfo::ResolutionMotivation motivation); | |
116 bool IsEmpty() const; | |
117 std::string Pop(); | |
118 | |
119 private: | |
120 // The names in the queue that should be serviced (popped) ASAP. | |
121 std::queue<std::string> rush_queue_; | |
122 // The names in the queue that should only be serviced when rush_queue is | |
123 // empty. | |
124 std::queue<std::string> background_queue_; | |
125 | |
126 DISALLOW_COPY_AND_ASSIGN(HostNameQueue); | |
127 }; | |
128 | |
129 // A map that is keyed with the hostnames that we've learned were the cause | 100 // A map that is keyed with the hostnames that we've learned were the cause |
130 // of loading additional hostnames. The list of additional hostnames in held | 101 // of loading additional hostnames. The list of additional hostnames in held |
131 // in a Referrer instance, which is found in this type. | 102 // in a Referrer instance, which is found in this type. |
132 typedef std::map<std::string, Referrer> Referrers; | 103 typedef std::map<std::string, Referrer> Referrers; |
133 | 104 |
134 // Only for testing. Returns true if hostname has been successfully resolved | 105 // Only for testing. Returns true if hostname has been successfully resolved |
135 // (name found). | 106 // (name found). |
136 bool WasFound(const std::string& hostname) { | 107 bool WasFound(const std::string& hostname) { |
137 AutoLock auto_lock(lock_); | 108 AutoLock auto_lock(lock_); |
138 return (results_.find(hostname) != results_.end()) && | 109 return (results_.find(hostname) != results_.end()) && |
(...skipping 16 matching lines...) Expand all Loading... |
155 void OnLookupFinished(LookupRequest* request, | 126 void OnLookupFinished(LookupRequest* request, |
156 const std::string& hostname, bool found); | 127 const std::string& hostname, bool found); |
157 | 128 |
158 // "PreLocked" means that the caller has already Acquired lock_ in the | 129 // "PreLocked" means that the caller has already Acquired lock_ in the |
159 // following method names. | 130 // following method names. |
160 // Queue hostname for resolution. If queueing was done, return the pointer | 131 // Queue hostname for resolution. If queueing was done, return the pointer |
161 // to the queued instance, otherwise return NULL. | 132 // to the queued instance, otherwise return NULL. |
162 DnsHostInfo* PreLockedResolve(const std::string& hostname, | 133 DnsHostInfo* PreLockedResolve(const std::string& hostname, |
163 DnsHostInfo::ResolutionMotivation motivation); | 134 DnsHostInfo::ResolutionMotivation motivation); |
164 | 135 |
165 // Check to see if too much queuing delay has been noted for the given info, | 136 // Take lookup requests from name_buffer_ and tell HostResolver |
166 // which indicates that there is "congestion" or growing delay in handling the | 137 // to look them up asynchronously, provided we don't exceed |
167 // resolution of names. Rather than letting this congestion potentially grow | 138 // concurrent resolution limit. |
168 // without bounds, we abandon our queued efforts at pre-resolutions in such a | |
169 // case. | |
170 // To do this, we will recycle |info|, as well as all queued items, back to | |
171 // the state they had before they were queued up. We can't do anything about | |
172 // the resolutions we've already sent off for processing on another thread, so | |
173 // we just let them complete. On a slow system, subject to congestion, this | |
174 // will greatly reduce the number of resolutions done, but it will assure that | |
175 // any resolutions that are done, are in a timely and hence potentially | |
176 // helpful manner. | |
177 bool PreLockedCongestionControlPerformed(DnsHostInfo* info); | |
178 | |
179 // Take lookup requests from work_queue_ and tell HostResolver to look them up | |
180 // asynchronously, provided we don't exceed concurrent resolution limit. | |
181 void PreLockedScheduleLookups(); | 139 void PreLockedScheduleLookups(); |
182 | 140 |
183 // Synchronize access to variables listed below. | 141 // Synchronize access to variables listed below. |
184 Lock lock_; | 142 Lock lock_; |
185 | 143 |
186 // work_queue_ holds a list of names we need to look up. | 144 // name_buffer_ holds a list of names we need to look up. |
187 HostNameQueue work_queue_; | 145 std::queue<std::string> name_buffer_; |
188 | 146 |
189 // results_ contains information for existing/prior prefetches. | 147 // results_ contains information for existing/prior prefetches. |
190 Results results_; | 148 Results results_; |
191 | 149 |
192 // For each hostname that we might navigate to (that we've "learned about") | 150 // For each hostname that we might navigate to (that we've "learned about") |
193 // we have a Referrer list. Each Referrer list has all hostnames we need to | 151 // we have a Referrer list. Each Referrer list has all hostnames we need to |
194 // pre-resolve when there is a navigation to the orginial hostname. | 152 // pre-resolve when there is a navigation to the orginial hostname. |
195 Referrers referrers_; | 153 Referrers referrers_; |
196 | 154 |
197 std::set<LookupRequest*> pending_lookups_; | 155 std::set<LookupRequest*> pending_lookups_; |
(...skipping 12 matching lines...) Expand all Loading... |
210 | 168 |
211 // The number of concurrent lookups currently allowed. | 169 // The number of concurrent lookups currently allowed. |
212 const size_t max_concurrent_lookups_; | 170 const size_t max_concurrent_lookups_; |
213 | 171 |
214 DISALLOW_COPY_AND_ASSIGN(DnsMaster); | 172 DISALLOW_COPY_AND_ASSIGN(DnsMaster); |
215 }; | 173 }; |
216 | 174 |
217 } // namespace chrome_browser_net | 175 } // namespace chrome_browser_net |
218 | 176 |
219 #endif // CHROME_BROWSER_NET_DNS_MASTER_H_ | 177 #endif // CHROME_BROWSER_NET_DNS_MASTER_H_ |
OLD | NEW |