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