| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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" |
| 8 #include "base/bind_helpers.h" |
| 7 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 8 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 11 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 12 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
| 13 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 14 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 15 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" | 17 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" |
| 16 #include "net/proxy/proxy_script_fetcher.h" | 18 #include "net/proxy/proxy_script_fetcher.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 42 // For more details, also check out this comment: | 44 // For more details, also check out this comment: |
| 43 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 | 45 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 |
| 44 static const char kWpadUrl[] = "http://wpad/wpad.dat"; | 46 static const char kWpadUrl[] = "http://wpad/wpad.dat"; |
| 45 | 47 |
| 46 ProxyScriptDecider::ProxyScriptDecider( | 48 ProxyScriptDecider::ProxyScriptDecider( |
| 47 ProxyScriptFetcher* proxy_script_fetcher, | 49 ProxyScriptFetcher* proxy_script_fetcher, |
| 48 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | 50 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, |
| 49 NetLog* net_log) | 51 NetLog* net_log) |
| 50 : proxy_script_fetcher_(proxy_script_fetcher), | 52 : proxy_script_fetcher_(proxy_script_fetcher), |
| 51 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), | 53 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), |
| 52 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | |
| 53 this, &ProxyScriptDecider::OnIOCompletion)), | |
| 54 user_callback_(NULL), | |
| 55 current_pac_source_index_(0u), | 54 current_pac_source_index_(0u), |
| 56 pac_mandatory_(false), | 55 pac_mandatory_(false), |
| 57 next_state_(STATE_NONE), | 56 next_state_(STATE_NONE), |
| 58 net_log_(BoundNetLog::Make( | 57 net_log_(BoundNetLog::Make( |
| 59 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), | 58 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), |
| 60 fetch_pac_bytes_(false) { | 59 fetch_pac_bytes_(false) { |
| 61 } | 60 } |
| 62 | 61 |
| 63 ProxyScriptDecider::~ProxyScriptDecider() { | 62 ProxyScriptDecider::~ProxyScriptDecider() { |
| 64 if (next_state_ != STATE_NONE) | 63 if (next_state_ != STATE_NONE) |
| 65 Cancel(); | 64 Cancel(); |
| 66 } | 65 } |
| 67 | 66 |
| 68 int ProxyScriptDecider::Start(const ProxyConfig& config, | 67 int ProxyScriptDecider::Start( |
| 69 const base::TimeDelta wait_delay, | 68 const ProxyConfig& config, const base::TimeDelta wait_delay, |
| 70 bool fetch_pac_bytes, | 69 bool fetch_pac_bytes, const CompletionCallback& callback) { |
| 71 OldCompletionCallback* callback) { | |
| 72 DCHECK_EQ(STATE_NONE, next_state_); | 70 DCHECK_EQ(STATE_NONE, next_state_); |
| 73 DCHECK(callback); | 71 DCHECK(!callback.is_null()); |
| 74 DCHECK(config.HasAutomaticSettings()); | 72 DCHECK(config.HasAutomaticSettings()); |
| 75 | 73 |
| 76 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); | 74 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); |
| 77 | 75 |
| 78 fetch_pac_bytes_ = fetch_pac_bytes; | 76 fetch_pac_bytes_ = fetch_pac_bytes; |
| 79 | 77 |
| 80 // Save the |wait_delay| as a non-negative value. | 78 // Save the |wait_delay| as a non-negative value. |
| 81 wait_delay_ = wait_delay; | 79 wait_delay_ = wait_delay; |
| 82 if (wait_delay_ < base::TimeDelta()) | 80 if (wait_delay_ < base::TimeDelta()) |
| 83 wait_delay_ = base::TimeDelta(); | 81 wait_delay_ = base::TimeDelta(); |
| 84 | 82 |
| 85 pac_mandatory_ = config.pac_mandatory(); | 83 pac_mandatory_ = config.pac_mandatory(); |
| 86 | 84 |
| 87 pac_sources_ = BuildPacSourcesFallbackList(config); | 85 pac_sources_ = BuildPacSourcesFallbackList(config); |
| 88 DCHECK(!pac_sources_.empty()); | 86 DCHECK(!pac_sources_.empty()); |
| 89 | 87 |
| 90 next_state_ = STATE_WAIT; | 88 next_state_ = STATE_WAIT; |
| 91 | 89 |
| 92 int rv = DoLoop(OK); | 90 int rv = DoLoop(OK); |
| 93 if (rv == ERR_IO_PENDING) | 91 if (rv == ERR_IO_PENDING) |
| 94 user_callback_ = callback; | 92 callback_ = callback; |
| 95 else | 93 else |
| 96 DidComplete(); | 94 DidComplete(); |
| 97 | 95 |
| 98 return rv; | 96 return rv; |
| 99 } | 97 } |
| 100 | 98 |
| 101 const ProxyConfig& ProxyScriptDecider::effective_config() const { | 99 const ProxyConfig& ProxyScriptDecider::effective_config() const { |
| 102 DCHECK_EQ(STATE_NONE, next_state_); | 100 DCHECK_EQ(STATE_NONE, next_state_); |
| 103 return effective_config_; | 101 return effective_config_; |
| 104 } | 102 } |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 NOTREACHED() << "bad state"; | 165 NOTREACHED() << "bad state"; |
| 168 rv = ERR_UNEXPECTED; | 166 rv = ERR_UNEXPECTED; |
| 169 break; | 167 break; |
| 170 } | 168 } |
| 171 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); | 169 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE); |
| 172 return rv; | 170 return rv; |
| 173 } | 171 } |
| 174 | 172 |
| 175 void ProxyScriptDecider::DoCallback(int result) { | 173 void ProxyScriptDecider::DoCallback(int result) { |
| 176 DCHECK_NE(ERR_IO_PENDING, result); | 174 DCHECK_NE(ERR_IO_PENDING, result); |
| 177 DCHECK(user_callback_); | 175 DCHECK(!callback_.is_null()); |
| 178 user_callback_->Run(result); | 176 callback_.Run(result); |
| 179 } | 177 } |
| 180 | 178 |
| 181 int ProxyScriptDecider::DoWait() { | 179 int ProxyScriptDecider::DoWait() { |
| 182 next_state_ = STATE_WAIT_COMPLETE; | 180 next_state_ = STATE_WAIT_COMPLETE; |
| 183 | 181 |
| 184 // If no waiting is required, continue on to the next state. | 182 // If no waiting is required, continue on to the next state. |
| 185 if (wait_delay_.ToInternalValue() == 0) | 183 if (wait_delay_.ToInternalValue() == 0) |
| 186 return OK; | 184 return OK; |
| 187 | 185 |
| 188 // Otherwise wait the specified amount of time. | 186 // Otherwise wait the specified amount of time. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 216 net_log_.BeginEvent( | 214 net_log_.BeginEvent( |
| 217 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, | 215 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, |
| 218 make_scoped_refptr(log_parameter)); | 216 make_scoped_refptr(log_parameter)); |
| 219 | 217 |
| 220 if (pac_source.type == PacSource::WPAD_DHCP) { | 218 if (pac_source.type == PacSource::WPAD_DHCP) { |
| 221 if (!dhcp_proxy_script_fetcher_) { | 219 if (!dhcp_proxy_script_fetcher_) { |
| 222 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); | 220 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); |
| 223 return ERR_UNEXPECTED; | 221 return ERR_UNEXPECTED; |
| 224 } | 222 } |
| 225 | 223 |
| 226 return dhcp_proxy_script_fetcher_->Fetch(&pac_script_, &io_callback_); | 224 return dhcp_proxy_script_fetcher_->Fetch( |
| 225 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, |
| 226 base::Unretained(this))); |
| 227 } | 227 } |
| 228 | 228 |
| 229 if (!proxy_script_fetcher_) { | 229 if (!proxy_script_fetcher_) { |
| 230 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); | 230 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); |
| 231 return ERR_UNEXPECTED; | 231 return ERR_UNEXPECTED; |
| 232 } | 232 } |
| 233 | 233 |
| 234 return proxy_script_fetcher_->Fetch( | 234 return proxy_script_fetcher_->Fetch( |
| 235 effective_pac_url, &pac_script_, &io_callback_); | 235 effective_pac_url, &pac_script_, |
| 236 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); |
| 236 } | 237 } |
| 237 | 238 |
| 238 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { | 239 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { |
| 239 DCHECK(fetch_pac_bytes_); | 240 DCHECK(fetch_pac_bytes_); |
| 240 | 241 |
| 241 net_log_.EndEventWithNetErrorCode( | 242 net_log_.EndEventWithNetErrorCode( |
| 242 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result); | 243 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result); |
| 243 if (result != OK) | 244 if (result != OK) |
| 244 return TryToFallbackPacSource(result); | 245 return TryToFallbackPacSource(result); |
| 245 | 246 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 387 } | 388 } |
| 388 | 389 |
| 389 // This is safe to call in any state. | 390 // This is safe to call in any state. |
| 390 if (dhcp_proxy_script_fetcher_) | 391 if (dhcp_proxy_script_fetcher_) |
| 391 dhcp_proxy_script_fetcher_->Cancel(); | 392 dhcp_proxy_script_fetcher_->Cancel(); |
| 392 | 393 |
| 393 DidComplete(); | 394 DidComplete(); |
| 394 } | 395 } |
| 395 | 396 |
| 396 } // namespace net | 397 } // namespace net |
| OLD | NEW |