| OLD | NEW |
| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram_macros.h" | 14 #include "base/metrics/histogram_macros.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/log/net_log_event_type.h" |
| 20 #include "net/log/net_log_source_type.h" |
| 19 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 21 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 20 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" | 22 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" |
| 21 #include "net/proxy/proxy_script_fetcher.h" | 23 #include "net/proxy/proxy_script_fetcher.h" |
| 22 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
| 23 | 25 |
| 24 namespace net { | 26 namespace net { |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| 27 | 29 |
| 28 bool LooksLikePacScript(const base::string16& script) { | 30 bool LooksLikePacScript(const base::string16& script) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 | 81 |
| 80 ProxyScriptDecider::ProxyScriptDecider( | 82 ProxyScriptDecider::ProxyScriptDecider( |
| 81 ProxyScriptFetcher* proxy_script_fetcher, | 83 ProxyScriptFetcher* proxy_script_fetcher, |
| 82 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | 84 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, |
| 83 NetLog* net_log) | 85 NetLog* net_log) |
| 84 : proxy_script_fetcher_(proxy_script_fetcher), | 86 : proxy_script_fetcher_(proxy_script_fetcher), |
| 85 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), | 87 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), |
| 86 current_pac_source_index_(0u), | 88 current_pac_source_index_(0u), |
| 87 pac_mandatory_(false), | 89 pac_mandatory_(false), |
| 88 next_state_(STATE_NONE), | 90 next_state_(STATE_NONE), |
| 89 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), | 91 net_log_( |
| 92 BoundNetLog::Make(net_log, NetLogSourceType::PROXY_SCRIPT_DECIDER)), |
| 90 fetch_pac_bytes_(false), | 93 fetch_pac_bytes_(false), |
| 91 quick_check_enabled_(true), | 94 quick_check_enabled_(true), |
| 92 host_resolver_(nullptr) { | 95 host_resolver_(nullptr) { |
| 93 if (proxy_script_fetcher && | 96 if (proxy_script_fetcher && |
| 94 proxy_script_fetcher->GetRequestContext() && | 97 proxy_script_fetcher->GetRequestContext() && |
| 95 proxy_script_fetcher->GetRequestContext()->host_resolver()) { | 98 proxy_script_fetcher->GetRequestContext()->host_resolver()) { |
| 96 host_resolver_ = proxy_script_fetcher->GetRequestContext()->host_resolver(); | 99 host_resolver_ = proxy_script_fetcher->GetRequestContext()->host_resolver(); |
| 97 } | 100 } |
| 98 } | 101 } |
| 99 | 102 |
| 100 ProxyScriptDecider::~ProxyScriptDecider() { | 103 ProxyScriptDecider::~ProxyScriptDecider() { |
| 101 if (next_state_ != STATE_NONE) | 104 if (next_state_ != STATE_NONE) |
| 102 Cancel(); | 105 Cancel(); |
| 103 } | 106 } |
| 104 | 107 |
| 105 int ProxyScriptDecider::Start( | 108 int ProxyScriptDecider::Start( |
| 106 const ProxyConfig& config, const base::TimeDelta wait_delay, | 109 const ProxyConfig& config, const base::TimeDelta wait_delay, |
| 107 bool fetch_pac_bytes, const CompletionCallback& callback) { | 110 bool fetch_pac_bytes, const CompletionCallback& callback) { |
| 108 DCHECK_EQ(STATE_NONE, next_state_); | 111 DCHECK_EQ(STATE_NONE, next_state_); |
| 109 DCHECK(!callback.is_null()); | 112 DCHECK(!callback.is_null()); |
| 110 DCHECK(config.HasAutomaticSettings()); | 113 DCHECK(config.HasAutomaticSettings()); |
| 111 | 114 |
| 112 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER); | 115 net_log_.BeginEvent(NetLogEventType::PROXY_SCRIPT_DECIDER); |
| 113 | 116 |
| 114 fetch_pac_bytes_ = fetch_pac_bytes; | 117 fetch_pac_bytes_ = fetch_pac_bytes; |
| 115 | 118 |
| 116 // Save the |wait_delay| as a non-negative value. | 119 // Save the |wait_delay| as a non-negative value. |
| 117 wait_delay_ = wait_delay; | 120 wait_delay_ = wait_delay; |
| 118 if (wait_delay_ < base::TimeDelta()) | 121 if (wait_delay_ < base::TimeDelta()) |
| 119 wait_delay_ = base::TimeDelta(); | 122 wait_delay_ = base::TimeDelta(); |
| 120 | 123 |
| 121 pac_mandatory_ = config.pac_mandatory(); | 124 pac_mandatory_ = config.pac_mandatory(); |
| 122 have_custom_pac_url_ = config.has_pac_url(); | 125 have_custom_pac_url_ = config.has_pac_url(); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 int ProxyScriptDecider::DoWait() { | 228 int ProxyScriptDecider::DoWait() { |
| 226 next_state_ = STATE_WAIT_COMPLETE; | 229 next_state_ = STATE_WAIT_COMPLETE; |
| 227 | 230 |
| 228 // If no waiting is required, continue on to the next state. | 231 // If no waiting is required, continue on to the next state. |
| 229 if (wait_delay_.ToInternalValue() == 0) | 232 if (wait_delay_.ToInternalValue() == 0) |
| 230 return OK; | 233 return OK; |
| 231 | 234 |
| 232 // Otherwise wait the specified amount of time. | 235 // Otherwise wait the specified amount of time. |
| 233 wait_timer_.Start(FROM_HERE, wait_delay_, this, | 236 wait_timer_.Start(FROM_HERE, wait_delay_, this, |
| 234 &ProxyScriptDecider::OnWaitTimerFired); | 237 &ProxyScriptDecider::OnWaitTimerFired); |
| 235 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT); | 238 net_log_.BeginEvent(NetLogEventType::PROXY_SCRIPT_DECIDER_WAIT); |
| 236 return ERR_IO_PENDING; | 239 return ERR_IO_PENDING; |
| 237 } | 240 } |
| 238 | 241 |
| 239 int ProxyScriptDecider::DoWaitComplete(int result) { | 242 int ProxyScriptDecider::DoWaitComplete(int result) { |
| 240 DCHECK_EQ(OK, result); | 243 DCHECK_EQ(OK, result); |
| 241 if (wait_delay_.ToInternalValue() != 0) { | 244 if (wait_delay_.ToInternalValue() != 0) { |
| 242 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, | 245 net_log_.EndEventWithNetErrorCode( |
| 243 result); | 246 NetLogEventType::PROXY_SCRIPT_DECIDER_WAIT, result); |
| 244 } | 247 } |
| 245 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS) | 248 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS) |
| 246 next_state_ = STATE_QUICK_CHECK; | 249 next_state_ = STATE_QUICK_CHECK; |
| 247 else | 250 else |
| 248 next_state_ = GetStartState(); | 251 next_state_ = GetStartState(); |
| 249 return OK; | 252 return OK; |
| 250 } | 253 } |
| 251 | 254 |
| 252 int ProxyScriptDecider::DoQuickCheck() { | 255 int ProxyScriptDecider::DoQuickCheck() { |
| 253 DCHECK(quick_check_enabled_); | 256 DCHECK(quick_check_enabled_); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 int ProxyScriptDecider::DoFetchPacScript() { | 297 int ProxyScriptDecider::DoFetchPacScript() { |
| 295 DCHECK(fetch_pac_bytes_); | 298 DCHECK(fetch_pac_bytes_); |
| 296 | 299 |
| 297 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; | 300 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; |
| 298 | 301 |
| 299 const PacSource& pac_source = current_pac_source(); | 302 const PacSource& pac_source = current_pac_source(); |
| 300 | 303 |
| 301 GURL effective_pac_url; | 304 GURL effective_pac_url; |
| 302 DetermineURL(pac_source, &effective_pac_url); | 305 DetermineURL(pac_source, &effective_pac_url); |
| 303 | 306 |
| 304 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, | 307 net_log_.BeginEvent( |
| 305 base::Bind(&PacSource::NetLogCallback, | 308 NetLogEventType::PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, |
| 306 base::Unretained(&pac_source), | 309 base::Bind(&PacSource::NetLogCallback, base::Unretained(&pac_source), |
| 307 &effective_pac_url)); | 310 &effective_pac_url)); |
| 308 | 311 |
| 309 if (pac_source.type == PacSource::WPAD_DHCP) { | 312 if (pac_source.type == PacSource::WPAD_DHCP) { |
| 310 if (!dhcp_proxy_script_fetcher_) { | 313 if (!dhcp_proxy_script_fetcher_) { |
| 311 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); | 314 net_log_.AddEvent(NetLogEventType::PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); |
| 312 return ERR_UNEXPECTED; | 315 return ERR_UNEXPECTED; |
| 313 } | 316 } |
| 314 | 317 |
| 315 return dhcp_proxy_script_fetcher_->Fetch( | 318 return dhcp_proxy_script_fetcher_->Fetch( |
| 316 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, | 319 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, |
| 317 base::Unretained(this))); | 320 base::Unretained(this))); |
| 318 } | 321 } |
| 319 | 322 |
| 320 if (!proxy_script_fetcher_) { | 323 if (!proxy_script_fetcher_) { |
| 321 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); | 324 net_log_.AddEvent(NetLogEventType::PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); |
| 322 return ERR_UNEXPECTED; | 325 return ERR_UNEXPECTED; |
| 323 } | 326 } |
| 324 | 327 |
| 325 return proxy_script_fetcher_->Fetch( | 328 return proxy_script_fetcher_->Fetch( |
| 326 effective_pac_url, &pac_script_, | 329 effective_pac_url, &pac_script_, |
| 327 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); | 330 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); |
| 328 } | 331 } |
| 329 | 332 |
| 330 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { | 333 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { |
| 331 DCHECK(fetch_pac_bytes_); | 334 DCHECK(fetch_pac_bytes_); |
| 332 | 335 |
| 333 net_log_.EndEventWithNetErrorCode( | 336 net_log_.EndEventWithNetErrorCode( |
| 334 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result); | 337 NetLogEventType::PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result); |
| 335 if (result != OK) | 338 if (result != OK) |
| 336 return TryToFallbackPacSource(result); | 339 return TryToFallbackPacSource(result); |
| 337 | 340 |
| 338 next_state_ = STATE_VERIFY_PAC_SCRIPT; | 341 next_state_ = STATE_VERIFY_PAC_SCRIPT; |
| 339 return result; | 342 return result; |
| 340 } | 343 } |
| 341 | 344 |
| 342 int ProxyScriptDecider::DoVerifyPacScript() { | 345 int ProxyScriptDecider::DoVerifyPacScript() { |
| 343 next_state_ = STATE_VERIFY_PAC_SCRIPT_COMPLETE; | 346 next_state_ = STATE_VERIFY_PAC_SCRIPT_COMPLETE; |
| 344 | 347 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 | 408 |
| 406 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { | 409 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { |
| 407 // Nothing left to fall back to. | 410 // Nothing left to fall back to. |
| 408 return error; | 411 return error; |
| 409 } | 412 } |
| 410 | 413 |
| 411 // Advance to next URL in our list. | 414 // Advance to next URL in our list. |
| 412 ++current_pac_source_index_; | 415 ++current_pac_source_index_; |
| 413 | 416 |
| 414 net_log_.AddEvent( | 417 net_log_.AddEvent( |
| 415 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE); | 418 NetLogEventType::PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE); |
| 416 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS) | 419 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS) |
| 417 next_state_ = STATE_QUICK_CHECK; | 420 next_state_ = STATE_QUICK_CHECK; |
| 418 else | 421 else |
| 419 next_state_ = GetStartState(); | 422 next_state_ = GetStartState(); |
| 420 | 423 |
| 421 return OK; | 424 return OK; |
| 422 } | 425 } |
| 423 | 426 |
| 424 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const { | 427 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const { |
| 425 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT; | 428 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 445 ProxyScriptDecider::current_pac_source() const { | 448 ProxyScriptDecider::current_pac_source() const { |
| 446 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); | 449 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); |
| 447 return pac_sources_[current_pac_source_index_]; | 450 return pac_sources_[current_pac_source_index_]; |
| 448 } | 451 } |
| 449 | 452 |
| 450 void ProxyScriptDecider::OnWaitTimerFired() { | 453 void ProxyScriptDecider::OnWaitTimerFired() { |
| 451 OnIOCompletion(OK); | 454 OnIOCompletion(OK); |
| 452 } | 455 } |
| 453 | 456 |
| 454 void ProxyScriptDecider::DidComplete() { | 457 void ProxyScriptDecider::DidComplete() { |
| 455 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER); | 458 net_log_.EndEvent(NetLogEventType::PROXY_SCRIPT_DECIDER); |
| 456 } | 459 } |
| 457 | 460 |
| 458 void ProxyScriptDecider::Cancel() { | 461 void ProxyScriptDecider::Cancel() { |
| 459 DCHECK_NE(STATE_NONE, next_state_); | 462 DCHECK_NE(STATE_NONE, next_state_); |
| 460 | 463 |
| 461 net_log_.AddEvent(NetLog::TYPE_CANCELLED); | 464 net_log_.AddEvent(NetLogEventType::CANCELLED); |
| 462 | 465 |
| 463 switch (next_state_) { | 466 switch (next_state_) { |
| 464 case STATE_WAIT_COMPLETE: | 467 case STATE_WAIT_COMPLETE: |
| 465 wait_timer_.Stop(); | 468 wait_timer_.Stop(); |
| 466 break; | 469 break; |
| 467 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | 470 case STATE_FETCH_PAC_SCRIPT_COMPLETE: |
| 468 proxy_script_fetcher_->Cancel(); | 471 proxy_script_fetcher_->Cancel(); |
| 469 break; | 472 break; |
| 470 default: | 473 default: |
| 471 break; | 474 break; |
| 472 } | 475 } |
| 473 | 476 |
| 474 // This is safe to call in any state. | 477 // This is safe to call in any state. |
| 475 if (dhcp_proxy_script_fetcher_) | 478 if (dhcp_proxy_script_fetcher_) |
| 476 dhcp_proxy_script_fetcher_->Cancel(); | 479 dhcp_proxy_script_fetcher_->Cancel(); |
| 477 | 480 |
| 478 DidComplete(); | 481 DidComplete(); |
| 479 } | 482 } |
| 480 | 483 |
| 481 } // namespace net | 484 } // namespace net |
| OLD | NEW |