Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: net/proxy/dhcp_proxy_script_fetcher_win.cc

Issue 266243004: Clang format slam. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 #include "net/proxy/dhcp_proxy_script_fetcher_win.h" 5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "base/threading/sequenced_worker_pool.h" 10 #include "base/threading/sequenced_worker_pool.h"
(...skipping 27 matching lines...) Expand all
38 // grow it to 6 or fewer threads) so setting the limit lower would not 38 // grow it to 6 or fewer threads) so setting the limit lower would not
39 // improve performance or memory usage on those systems. 39 // improve performance or memory usage on those systems.
40 const int kMaxDhcpLookupThreads = 12; 40 const int kMaxDhcpLookupThreads = 12;
41 41
42 // How long to wait at maximum after we get results (a PAC file or 42 // How long to wait at maximum after we get results (a PAC file or
43 // knowledge that no PAC file is configured) from whichever network 43 // knowledge that no PAC file is configured) from whichever network
44 // adapter finishes first. 44 // adapter finishes first.
45 const int kMaxWaitAfterFirstResultMs = 400; 45 const int kMaxWaitAfterFirstResultMs = 400;
46 46
47 const int kGetAdaptersAddressesErrors[] = { 47 const int kGetAdaptersAddressesErrors[] = {
48 ERROR_ADDRESS_NOT_ASSOCIATED, 48 ERROR_ADDRESS_NOT_ASSOCIATED, ERROR_BUFFER_OVERFLOW,
49 ERROR_BUFFER_OVERFLOW, 49 ERROR_INVALID_PARAMETER, ERROR_NOT_ENOUGH_MEMORY,
50 ERROR_INVALID_PARAMETER, 50 ERROR_NO_DATA,
51 ERROR_NOT_ENOUGH_MEMORY,
52 ERROR_NO_DATA,
53 }; 51 };
54 52
55 } // namespace 53 } // namespace
56 54
57 namespace net { 55 namespace net {
58 56
59 DhcpProxyScriptFetcherWin::DhcpProxyScriptFetcherWin( 57 DhcpProxyScriptFetcherWin::DhcpProxyScriptFetcherWin(
60 URLRequestContext* url_request_context) 58 URLRequestContext* url_request_context)
61 : state_(STATE_START), 59 : state_(STATE_START),
62 num_pending_fetchers_(0), 60 num_pending_fetchers_(0),
63 destination_string_(NULL), 61 destination_string_(NULL),
64 url_request_context_(url_request_context) { 62 url_request_context_(url_request_context) {
65 DCHECK(url_request_context_); 63 DCHECK(url_request_context_);
66 64
67 worker_pool_ = new base::SequencedWorkerPool(kMaxDhcpLookupThreads, 65 worker_pool_ =
68 "PacDhcpLookup"); 66 new base::SequencedWorkerPool(kMaxDhcpLookupThreads, "PacDhcpLookup");
69 } 67 }
70 68
71 DhcpProxyScriptFetcherWin::~DhcpProxyScriptFetcherWin() { 69 DhcpProxyScriptFetcherWin::~DhcpProxyScriptFetcherWin() {
72 // Count as user-initiated if we are not yet in STATE_DONE. 70 // Count as user-initiated if we are not yet in STATE_DONE.
73 Cancel(); 71 Cancel();
74 72
75 worker_pool_->Shutdown(); 73 worker_pool_->Shutdown();
76 } 74 }
77 75
78 int DhcpProxyScriptFetcherWin::Fetch(base::string16* utf16_text, 76 int DhcpProxyScriptFetcherWin::Fetch(base::string16* utf16_text,
79 const CompletionCallback& callback) { 77 const CompletionCallback& callback) {
80 DCHECK(CalledOnValidThread()); 78 DCHECK(CalledOnValidThread());
81 if (state_ != STATE_START && state_ != STATE_DONE) { 79 if (state_ != STATE_START && state_ != STATE_DONE) {
82 NOTREACHED(); 80 NOTREACHED();
83 return ERR_UNEXPECTED; 81 return ERR_UNEXPECTED;
84 } 82 }
85 83
86 fetch_start_time_ = base::TimeTicks::Now(); 84 fetch_start_time_ = base::TimeTicks::Now();
87 85
88 state_ = STATE_WAIT_ADAPTERS; 86 state_ = STATE_WAIT_ADAPTERS;
89 callback_ = callback; 87 callback_ = callback;
90 destination_string_ = utf16_text; 88 destination_string_ = utf16_text;
91 89
92 last_query_ = ImplCreateAdapterQuery(); 90 last_query_ = ImplCreateAdapterQuery();
93 GetTaskRunner()->PostTaskAndReply( 91 GetTaskRunner()->PostTaskAndReply(
94 FROM_HERE, 92 FROM_HERE,
95 base::Bind( 93 base::Bind(
96 &DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames, 94 &DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames,
97 last_query_.get()), 95 last_query_.get()),
98 base::Bind( 96 base::Bind(&DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone,
99 &DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone, 97 AsWeakPtr(),
100 AsWeakPtr(), 98 last_query_));
101 last_query_));
102 99
103 return ERR_IO_PENDING; 100 return ERR_IO_PENDING;
104 } 101 }
105 102
106 void DhcpProxyScriptFetcherWin::Cancel() { 103 void DhcpProxyScriptFetcherWin::Cancel() {
107 DCHECK(CalledOnValidThread()); 104 DCHECK(CalledOnValidThread());
108 105
109 if (state_ != STATE_DONE) { 106 if (state_ != STATE_DONE) {
110 // We only count this stat if the cancel was explicitly initiated by 107 // We only count this stat if the cancel was explicitly initiated by
111 // our client, and if we weren't already in STATE_DONE. 108 // our client, and if we weren't already in STATE_DONE.
112 UMA_HISTOGRAM_TIMES("Net.DhcpWpadCancelTime", 109 UMA_HISTOGRAM_TIMES("Net.DhcpWpadCancelTime",
113 base::TimeTicks::Now() - fetch_start_time_); 110 base::TimeTicks::Now() - fetch_start_time_);
114 } 111 }
115 112
116 CancelImpl(); 113 CancelImpl();
117 } 114 }
118 115
119 void DhcpProxyScriptFetcherWin::CancelImpl() { 116 void DhcpProxyScriptFetcherWin::CancelImpl() {
120 DCHECK(CalledOnValidThread()); 117 DCHECK(CalledOnValidThread());
121 118
122 if (state_ != STATE_DONE) { 119 if (state_ != STATE_DONE) {
123 callback_.Reset(); 120 callback_.Reset();
124 wait_timer_.Stop(); 121 wait_timer_.Stop();
125 state_ = STATE_DONE; 122 state_ = STATE_DONE;
126 123
127 for (FetcherVector::iterator it = fetchers_.begin(); 124 for (FetcherVector::iterator it = fetchers_.begin(); it != fetchers_.end();
128 it != fetchers_.end();
129 ++it) { 125 ++it) {
130 (*it)->Cancel(); 126 (*it)->Cancel();
131 } 127 }
132 128
133 fetchers_.clear(); 129 fetchers_.clear();
134 } 130 }
135 } 131 }
136 132
137 void DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone( 133 void DhcpProxyScriptFetcherWin::OnGetCandidateAdapterNamesDone(
138 scoped_refptr<AdapterQuery> query) { 134 scoped_refptr<AdapterQuery> query) {
(...skipping 19 matching lines...) Expand all
158 154
159 if (adapter_names.empty()) { 155 if (adapter_names.empty()) {
160 TransitionToDone(); 156 TransitionToDone();
161 return; 157 return;
162 } 158 }
163 159
164 for (std::set<std::string>::const_iterator it = adapter_names.begin(); 160 for (std::set<std::string>::const_iterator it = adapter_names.begin();
165 it != adapter_names.end(); 161 it != adapter_names.end();
166 ++it) { 162 ++it) {
167 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher()); 163 DhcpProxyScriptAdapterFetcher* fetcher(ImplCreateAdapterFetcher());
168 fetcher->Fetch( 164 fetcher->Fetch(*it,
169 *it, base::Bind(&DhcpProxyScriptFetcherWin::OnFetcherDone, 165 base::Bind(&DhcpProxyScriptFetcherWin::OnFetcherDone,
170 base::Unretained(this))); 166 base::Unretained(this)));
171 fetchers_.push_back(fetcher); 167 fetchers_.push_back(fetcher);
172 } 168 }
173 num_pending_fetchers_ = fetchers_.size(); 169 num_pending_fetchers_ = fetchers_.size();
174 } 170 }
175 171
176 std::string DhcpProxyScriptFetcherWin::GetFetcherName() const { 172 std::string DhcpProxyScriptFetcherWin::GetFetcherName() const {
177 DCHECK(CalledOnValidThread()); 173 DCHECK(CalledOnValidThread());
178 return "win"; 174 return "win";
179 } 175 }
180 176
181 const GURL& DhcpProxyScriptFetcherWin::GetPacURL() const { 177 const GURL& DhcpProxyScriptFetcherWin::GetPacURL() const {
182 DCHECK(CalledOnValidThread()); 178 DCHECK(CalledOnValidThread());
183 DCHECK_EQ(state_, STATE_DONE); 179 DCHECK_EQ(state_, STATE_DONE);
184 180
185 return pac_url_; 181 return pac_url_;
186 } 182 }
187 183
188 void DhcpProxyScriptFetcherWin::OnFetcherDone(int result) { 184 void DhcpProxyScriptFetcherWin::OnFetcherDone(int result) {
189 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS); 185 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS);
190 186
191 if (--num_pending_fetchers_ == 0) { 187 if (--num_pending_fetchers_ == 0) {
192 TransitionToDone(); 188 TransitionToDone();
193 return; 189 return;
194 } 190 }
195 191
196 // If the only pending adapters are those less preferred than one 192 // If the only pending adapters are those less preferred than one
197 // with a valid PAC script, we do not need to wait any longer. 193 // with a valid PAC script, we do not need to wait any longer.
198 for (FetcherVector::iterator it = fetchers_.begin(); 194 for (FetcherVector::iterator it = fetchers_.begin(); it != fetchers_.end();
199 it != fetchers_.end();
200 ++it) { 195 ++it) {
201 bool did_finish = (*it)->DidFinish(); 196 bool did_finish = (*it)->DidFinish();
202 int result = (*it)->GetResult(); 197 int result = (*it)->GetResult();
203 if (did_finish && result == OK) { 198 if (did_finish && result == OK) {
204 TransitionToDone(); 199 TransitionToDone();
205 return; 200 return;
206 } 201 }
207 if (!did_finish || result != ERR_PAC_NOT_IN_DHCP) { 202 if (!did_finish || result != ERR_PAC_NOT_IN_DHCP) {
208 break; 203 break;
209 } 204 }
210 } 205 }
211 206
212 // Once we have a single result, we set a maximum on how long to wait 207 // Once we have a single result, we set a maximum on how long to wait
213 // for the rest of the results. 208 // for the rest of the results.
214 if (state_ == STATE_NO_RESULTS) { 209 if (state_ == STATE_NO_RESULTS) {
215 state_ = STATE_SOME_RESULTS; 210 state_ = STATE_SOME_RESULTS;
216 wait_timer_.Start(FROM_HERE, 211 wait_timer_.Start(FROM_HERE,
217 ImplGetMaxWait(), this, &DhcpProxyScriptFetcherWin::OnWaitTimer); 212 ImplGetMaxWait(),
213 this,
214 &DhcpProxyScriptFetcherWin::OnWaitTimer);
218 } 215 }
219 } 216 }
220 217
221 void DhcpProxyScriptFetcherWin::OnWaitTimer() { 218 void DhcpProxyScriptFetcherWin::OnWaitTimer() {
222 DCHECK_EQ(state_, STATE_SOME_RESULTS); 219 DCHECK_EQ(state_, STATE_SOME_RESULTS);
223 220
224 // These are intended to help us understand whether our timeout may 221 // These are intended to help us understand whether our timeout may
225 // be too aggressive or not aggressive enough. 222 // be too aggressive or not aggressive enough.
226 UMA_HISTOGRAM_COUNTS_100("Net.DhcpWpadNumAdaptersAtWaitTimer", 223 UMA_HISTOGRAM_COUNTS_100("Net.DhcpWpadNumAdaptersAtWaitTimer",
227 fetchers_.size()); 224 fetchers_.size());
228 UMA_HISTOGRAM_COUNTS_100("Net.DhcpWpadNumPendingAdaptersAtWaitTimer", 225 UMA_HISTOGRAM_COUNTS_100("Net.DhcpWpadNumPendingAdaptersAtWaitTimer",
229 num_pending_fetchers_); 226 num_pending_fetchers_);
230 227
231 TransitionToDone(); 228 TransitionToDone();
232 } 229 }
233 230
234 void DhcpProxyScriptFetcherWin::TransitionToDone() { 231 void DhcpProxyScriptFetcherWin::TransitionToDone() {
235 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS); 232 DCHECK(state_ == STATE_NO_RESULTS || state_ == STATE_SOME_RESULTS);
236 233
237 int result = ERR_PAC_NOT_IN_DHCP; // Default if no fetchers. 234 int result = ERR_PAC_NOT_IN_DHCP; // Default if no fetchers.
238 if (!fetchers_.empty()) { 235 if (!fetchers_.empty()) {
239 // Scan twice for the result; once through the whole list for success, 236 // Scan twice for the result; once through the whole list for success,
240 // then if no success, return result for most preferred network adapter, 237 // then if no success, return result for most preferred network adapter,
241 // preferring "real" network errors to the ERR_PAC_NOT_IN_DHCP error. 238 // preferring "real" network errors to the ERR_PAC_NOT_IN_DHCP error.
242 // Default to ERR_ABORTED if no fetcher completed. 239 // Default to ERR_ABORTED if no fetcher completed.
243 result = ERR_ABORTED; 240 result = ERR_ABORTED;
244 for (FetcherVector::iterator it = fetchers_.begin(); 241 for (FetcherVector::iterator it = fetchers_.begin(); it != fetchers_.end();
245 it != fetchers_.end();
246 ++it) { 242 ++it) {
247 if ((*it)->DidFinish() && (*it)->GetResult() == OK) { 243 if ((*it)->DidFinish() && (*it)->GetResult() == OK) {
248 result = OK; 244 result = OK;
249 *destination_string_ = (*it)->GetPacScript(); 245 *destination_string_ = (*it)->GetPacScript();
250 pac_url_ = (*it)->GetPacURL(); 246 pac_url_ = (*it)->GetPacURL();
251 break; 247 break;
252 } 248 }
253 } 249 }
254 if (result != OK) { 250 if (result != OK) {
255 destination_string_->clear(); 251 destination_string_->clear();
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const { 287 URLRequestContext* DhcpProxyScriptFetcherWin::url_request_context() const {
292 return url_request_context_; 288 return url_request_context_;
293 } 289 }
294 290
295 scoped_refptr<base::TaskRunner> DhcpProxyScriptFetcherWin::GetTaskRunner() { 291 scoped_refptr<base::TaskRunner> DhcpProxyScriptFetcherWin::GetTaskRunner() {
296 return worker_pool_->GetTaskRunnerWithShutdownBehavior( 292 return worker_pool_->GetTaskRunnerWithShutdownBehavior(
297 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN); 293 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN);
298 } 294 }
299 295
300 DhcpProxyScriptAdapterFetcher* 296 DhcpProxyScriptAdapterFetcher*
301 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() { 297 DhcpProxyScriptFetcherWin::ImplCreateAdapterFetcher() {
302 return new DhcpProxyScriptAdapterFetcher(url_request_context_, 298 return new DhcpProxyScriptAdapterFetcher(url_request_context_,
303 GetTaskRunner()); 299 GetTaskRunner());
304 } 300 }
305 301
306 DhcpProxyScriptFetcherWin::AdapterQuery* 302 DhcpProxyScriptFetcherWin::AdapterQuery*
307 DhcpProxyScriptFetcherWin::ImplCreateAdapterQuery() { 303 DhcpProxyScriptFetcherWin::ImplCreateAdapterQuery() {
308 return new AdapterQuery(); 304 return new AdapterQuery();
309 } 305 }
310 306
311 base::TimeDelta DhcpProxyScriptFetcherWin::ImplGetMaxWait() { 307 base::TimeDelta DhcpProxyScriptFetcherWin::ImplGetMaxWait() {
312 return base::TimeDelta::FromMilliseconds(kMaxWaitAfterFirstResultMs); 308 return base::TimeDelta::FromMilliseconds(kMaxWaitAfterFirstResultMs);
313 } 309 }
314 310
315 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames( 311 bool DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(
316 std::set<std::string>* adapter_names) { 312 std::set<std::string>* adapter_names) {
317 DCHECK(adapter_names); 313 DCHECK(adapter_names);
318 adapter_names->clear(); 314 adapter_names->clear();
319 315
320 // The GetAdaptersAddresses MSDN page recommends using a size of 15000 to 316 // The GetAdaptersAddresses MSDN page recommends using a size of 15000 to
321 // avoid reallocation. 317 // avoid reallocation.
322 ULONG adapters_size = 15000; 318 ULONG adapters_size = 15000;
323 scoped_ptr<IP_ADAPTER_ADDRESSES, base::FreeDeleter> adapters; 319 scoped_ptr<IP_ADAPTER_ADDRESSES, base::FreeDeleter> adapters;
324 ULONG error = ERROR_SUCCESS; 320 ULONG error = ERROR_SUCCESS;
325 int num_tries = 0; 321 int num_tries = 0;
326 322
327 base::ElapsedTimer time_api_access; 323 base::ElapsedTimer time_api_access;
328 do { 324 do {
329 adapters.reset(static_cast<IP_ADAPTER_ADDRESSES*>(malloc(adapters_size))); 325 adapters.reset(static_cast<IP_ADAPTER_ADDRESSES*>(malloc(adapters_size)));
330 // Return only unicast addresses, and skip information we do not need. 326 // Return only unicast addresses, and skip information we do not need.
331 error = GetAdaptersAddresses(AF_UNSPEC, 327 error = GetAdaptersAddresses(
332 GAA_FLAG_SKIP_ANYCAST | 328 AF_UNSPEC,
333 GAA_FLAG_SKIP_MULTICAST | 329 GAA_FLAG_SKIP_ANYCAST | GAA_FLAG_SKIP_MULTICAST |
334 GAA_FLAG_SKIP_DNS_SERVER | 330 GAA_FLAG_SKIP_DNS_SERVER | GAA_FLAG_SKIP_FRIENDLY_NAME,
335 GAA_FLAG_SKIP_FRIENDLY_NAME, 331 NULL,
336 NULL, 332 adapters.get(),
337 adapters.get(), 333 &adapters_size);
338 &adapters_size);
339 ++num_tries; 334 ++num_tries;
340 } while (error == ERROR_BUFFER_OVERFLOW && num_tries <= 3); 335 } while (error == ERROR_BUFFER_OVERFLOW && num_tries <= 3);
341 336
342 // This is primarily to validate our belief that the GetAdaptersAddresses API 337 // This is primarily to validate our belief that the GetAdaptersAddresses API
343 // function is fast enough to call synchronously from the network thread. 338 // function is fast enough to call synchronously from the network thread.
344 UMA_HISTOGRAM_TIMES("Net.DhcpWpadGetAdaptersAddressesTime", 339 UMA_HISTOGRAM_TIMES("Net.DhcpWpadGetAdaptersAddressesTime",
345 time_api_access.Elapsed()); 340 time_api_access.Elapsed());
346 341
347 if (error != ERROR_SUCCESS) { 342 if (error != ERROR_SUCCESS) {
348 UMA_HISTOGRAM_CUSTOM_ENUMERATION( 343 UMA_HISTOGRAM_CUSTOM_ENUMERATION(
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 } 376 }
382 377
383 DhcpProxyScriptFetcherWin::AdapterQuery::~AdapterQuery() { 378 DhcpProxyScriptFetcherWin::AdapterQuery::~AdapterQuery() {
384 } 379 }
385 380
386 void DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames() { 381 void DhcpProxyScriptFetcherWin::AdapterQuery::GetCandidateAdapterNames() {
387 ImplGetCandidateAdapterNames(&adapter_names_); 382 ImplGetCandidateAdapterNames(&adapter_names_);
388 } 383 }
389 384
390 const std::set<std::string>& 385 const std::set<std::string>&
391 DhcpProxyScriptFetcherWin::AdapterQuery::adapter_names() const { 386 DhcpProxyScriptFetcherWin::AdapterQuery::adapter_names() const {
392 return adapter_names_; 387 return adapter_names_;
393 } 388 }
394 389
395 bool DhcpProxyScriptFetcherWin::AdapterQuery::ImplGetCandidateAdapterNames( 390 bool DhcpProxyScriptFetcherWin::AdapterQuery::ImplGetCandidateAdapterNames(
396 std::set<std::string>* adapter_names) { 391 std::set<std::string>* adapter_names) {
397 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names); 392 return DhcpProxyScriptFetcherWin::GetCandidateAdapterNames(adapter_names);
398 } 393 }
399 394
400 } // namespace net 395 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698