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

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

Issue 10534132: NetLogEventParameter to Callback refactoring 7. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Update unit test Created 8 years, 6 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/proxy_script_decider.h" 5 #include "net/proxy/proxy_script_decider.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/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/format_macros.h" 10 #include "base/format_macros.h"
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
65 Cancel(); 65 Cancel();
66 } 66 }
67 67
68 int ProxyScriptDecider::Start( 68 int ProxyScriptDecider::Start(
69 const ProxyConfig& config, const base::TimeDelta wait_delay, 69 const ProxyConfig& config, const base::TimeDelta wait_delay,
70 bool fetch_pac_bytes, const CompletionCallback& callback) { 70 bool fetch_pac_bytes, const CompletionCallback& callback) {
71 DCHECK_EQ(STATE_NONE, next_state_); 71 DCHECK_EQ(STATE_NONE, next_state_);
72 DCHECK(!callback.is_null()); 72 DCHECK(!callback.is_null());
73 DCHECK(config.HasAutomaticSettings()); 73 DCHECK(config.HasAutomaticSettings());
74 74
75 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); 75 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
76 76
77 fetch_pac_bytes_ = fetch_pac_bytes; 77 fetch_pac_bytes_ = fetch_pac_bytes;
78 78
79 // Save the |wait_delay| as a non-negative value. 79 // Save the |wait_delay| as a non-negative value.
80 wait_delay_ = wait_delay; 80 wait_delay_ = wait_delay;
81 if (wait_delay_ < base::TimeDelta()) 81 if (wait_delay_ < base::TimeDelta())
82 wait_delay_ = base::TimeDelta(); 82 wait_delay_ = base::TimeDelta();
83 83
84 pac_mandatory_ = config.pac_mandatory(); 84 pac_mandatory_ = config.pac_mandatory();
85 85
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 int ProxyScriptDecider::DoWait() { 180 int ProxyScriptDecider::DoWait() {
181 next_state_ = STATE_WAIT_COMPLETE; 181 next_state_ = STATE_WAIT_COMPLETE;
182 182
183 // If no waiting is required, continue on to the next state. 183 // If no waiting is required, continue on to the next state.
184 if (wait_delay_.ToInternalValue() == 0) 184 if (wait_delay_.ToInternalValue() == 0)
185 return OK; 185 return OK;
186 186
187 // Otherwise wait the specified amount of time. 187 // Otherwise wait the specified amount of time.
188 wait_timer_.Start(FROM_HERE, wait_delay_, this, 188 wait_timer_.Start(FROM_HERE, wait_delay_, this,
189 &ProxyScriptDecider::OnWaitTimerFired); 189 &ProxyScriptDecider::OnWaitTimerFired);
190 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, NULL); 190 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT);
191 return ERR_IO_PENDING; 191 return ERR_IO_PENDING;
192 } 192 }
193 193
194 int ProxyScriptDecider::DoWaitComplete(int result) { 194 int ProxyScriptDecider::DoWaitComplete(int result) {
195 DCHECK_EQ(OK, result); 195 DCHECK_EQ(OK, result);
196 if (wait_delay_.ToInternalValue() != 0) { 196 if (wait_delay_.ToInternalValue() != 0) {
197 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, 197 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT,
198 result); 198 result);
199 } 199 }
200 next_state_ = GetStartState(); 200 next_state_ = GetStartState();
201 return OK; 201 return OK;
202 } 202 }
203 203
204 int ProxyScriptDecider::DoFetchPacScript() { 204 int ProxyScriptDecider::DoFetchPacScript() {
205 DCHECK(fetch_pac_bytes_); 205 DCHECK(fetch_pac_bytes_);
206 206
207 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; 207 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE;
208 208
209 const PacSource& pac_source = current_pac_source(); 209 const PacSource& pac_source = current_pac_source();
210 210
211 GURL effective_pac_url; 211 GURL effective_pac_url;
212 NetLogStringParameter* log_parameter = 212 std::string source_string;
213 CreateNetLogParameterAndDetermineURL(pac_source, &effective_pac_url); 213 CreateSourceStringAndDetermineURL(pac_source, &effective_pac_url,
eroman 2012/06/13 18:55:33 Can this string construction be internalized into
mmenke 2012/06/13 20:27:58 Done. This does add a little ugliness (Have to du
214 &source_string);
214 215
215 net_log_.BeginEvent( 216 net_log_.BeginEvent(
216 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, 217 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT,
217 make_scoped_refptr(log_parameter)); 218 NetLog::StringCallback("source", &source_string));
218 219
219 if (pac_source.type == PacSource::WPAD_DHCP) { 220 if (pac_source.type == PacSource::WPAD_DHCP) {
220 if (!dhcp_proxy_script_fetcher_) { 221 if (!dhcp_proxy_script_fetcher_) {
221 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); 222 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
222 return ERR_UNEXPECTED; 223 return ERR_UNEXPECTED;
223 } 224 }
224 225
225 return dhcp_proxy_script_fetcher_->Fetch( 226 return dhcp_proxy_script_fetcher_->Fetch(
226 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, 227 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion,
227 base::Unretained(this))); 228 base::Unretained(this)));
228 } 229 }
229 230
230 if (!proxy_script_fetcher_) { 231 if (!proxy_script_fetcher_) {
231 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); 232 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
232 return ERR_UNEXPECTED; 233 return ERR_UNEXPECTED;
233 } 234 }
234 235
235 return proxy_script_fetcher_->Fetch( 236 return proxy_script_fetcher_->Fetch(
236 effective_pac_url, &pac_script_, 237 effective_pac_url, &pac_script_,
237 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); 238 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this)));
238 } 239 }
239 240
240 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { 241 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) {
241 DCHECK(fetch_pac_bytes_); 242 DCHECK(fetch_pac_bytes_);
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 316
316 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { 317 if (current_pac_source_index_ + 1 >= pac_sources_.size()) {
317 // Nothing left to fall back to. 318 // Nothing left to fall back to.
318 return error; 319 return error;
319 } 320 }
320 321
321 // Advance to next URL in our list. 322 // Advance to next URL in our list.
322 ++current_pac_source_index_; 323 ++current_pac_source_index_;
323 324
324 net_log_.AddEvent( 325 net_log_.AddEvent(
325 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, NULL); 326 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE);
326 327
327 next_state_ = GetStartState(); 328 next_state_ = GetStartState();
328 329
329 return OK; 330 return OK;
330 } 331 }
331 332
332 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const { 333 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const {
333 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT; 334 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT;
334 } 335 }
335 336
336 NetLogStringParameter* ProxyScriptDecider::CreateNetLogParameterAndDetermineURL( 337 void ProxyScriptDecider::CreateSourceStringAndDetermineURL(
337 const PacSource& pac_source, 338 const PacSource& pac_source,
338 GURL* effective_pac_url) { 339 GURL* effective_pac_url,
340 std::string* source_string) {
339 DCHECK(effective_pac_url); 341 DCHECK(effective_pac_url);
340 342
341 std::string source_field; 343 std::string source_field;
342 switch (pac_source.type) { 344 switch (pac_source.type) {
343 case PacSource::WPAD_DHCP: 345 case PacSource::WPAD_DHCP:
344 source_field = "WPAD DHCP"; 346 *source_string = "WPAD DHCP";
345 break; 347 break;
346 case PacSource::WPAD_DNS: 348 case PacSource::WPAD_DNS:
347 *effective_pac_url = GURL(kWpadUrl); 349 *effective_pac_url = GURL(kWpadUrl);
348 source_field = "WPAD DNS: "; 350 *source_string = "WPAD DNS: ";
349 source_field += effective_pac_url->possibly_invalid_spec(); 351 *source_string += effective_pac_url->possibly_invalid_spec();
350 break; 352 break;
351 case PacSource::CUSTOM: 353 case PacSource::CUSTOM:
352 *effective_pac_url = pac_source.url; 354 *effective_pac_url = pac_source.url;
353 source_field = "Custom PAC URL: "; 355 *source_string = "Custom PAC URL: ";
354 source_field += effective_pac_url->possibly_invalid_spec(); 356 *source_string += effective_pac_url->possibly_invalid_spec();
355 break; 357 break;
356 } 358 }
357 return new NetLogStringParameter("source", source_field);
358 } 359 }
359 360
360 const ProxyScriptDecider::PacSource& 361 const ProxyScriptDecider::PacSource&
361 ProxyScriptDecider::current_pac_source() const { 362 ProxyScriptDecider::current_pac_source() const {
362 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); 363 DCHECK_LT(current_pac_source_index_, pac_sources_.size());
363 return pac_sources_[current_pac_source_index_]; 364 return pac_sources_[current_pac_source_index_];
364 } 365 }
365 366
366 void ProxyScriptDecider::OnWaitTimerFired() { 367 void ProxyScriptDecider::OnWaitTimerFired() {
367 OnIOCompletion(OK); 368 OnIOCompletion(OK);
368 } 369 }
369 370
370 void ProxyScriptDecider::DidComplete() { 371 void ProxyScriptDecider::DidComplete() {
371 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); 372 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
372 } 373 }
373 374
374 void ProxyScriptDecider::Cancel() { 375 void ProxyScriptDecider::Cancel() {
375 DCHECK_NE(STATE_NONE, next_state_); 376 DCHECK_NE(STATE_NONE, next_state_);
376 377
377 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); 378 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
378 379
379 switch (next_state_) { 380 switch (next_state_) {
380 case STATE_WAIT_COMPLETE: 381 case STATE_WAIT_COMPLETE:
381 wait_timer_.Stop(); 382 wait_timer_.Stop();
382 break; 383 break;
383 case STATE_FETCH_PAC_SCRIPT_COMPLETE: 384 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
384 proxy_script_fetcher_->Cancel(); 385 proxy_script_fetcher_->Cancel();
385 break; 386 break;
386 default: 387 default:
387 NOTREACHED(); 388 NOTREACHED();
388 break; 389 break;
389 } 390 }
390 391
391 // This is safe to call in any state. 392 // This is safe to call in any state.
392 if (dhcp_proxy_script_fetcher_) 393 if (dhcp_proxy_script_fetcher_)
393 dhcp_proxy_script_fetcher_->Cancel(); 394 dhcp_proxy_script_fetcher_->Cancel();
394 395
395 DidComplete(); 396 DidComplete();
396 } 397 }
397 398
398 } // namespace net 399 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698