| 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 "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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "net/base/net_log.h" | 14 #include "base/values.h" |
| 15 #include "net/base/net_errors.h" | 15 #include "net/base/net_errors.h" |
| 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 17 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" | 17 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h" |
| 18 #include "net/proxy/proxy_script_fetcher.h" | 18 #include "net/proxy/proxy_script_fetcher.h" |
| 19 | 19 |
| 20 namespace net { | 20 namespace net { |
| 21 | 21 |
| 22 namespace { | 22 namespace { |
| 23 |
| 23 bool LooksLikePacScript(const string16& script) { | 24 bool LooksLikePacScript(const string16& script) { |
| 24 // Note: this is only an approximation! It may not always work correctly, | 25 // Note: this is only an approximation! It may not always work correctly, |
| 25 // however it is very likely that legitimate scripts have this exact string, | 26 // however it is very likely that legitimate scripts have this exact string, |
| 26 // since they must minimally define a function of this name. Conversely, a | 27 // since they must minimally define a function of this name. Conversely, a |
| 27 // file not containing the string is not likely to be a PAC script. | 28 // file not containing the string is not likely to be a PAC script. |
| 28 // | 29 // |
| 29 // An exact test would have to load the script in a javascript evaluator. | 30 // An exact test would have to load the script in a javascript evaluator. |
| 30 return script.find(ASCIIToUTF16("FindProxyForURL")) != string16::npos; | 31 return script.find(ASCIIToUTF16("FindProxyForURL")) != string16::npos; |
| 31 } | 32 } |
| 33 |
| 32 } | 34 } |
| 33 | 35 |
| 34 // This is the hard-coded location used by the DNS portion of web proxy | 36 // This is the hard-coded location used by the DNS portion of web proxy |
| 35 // auto-discovery. | 37 // auto-discovery. |
| 36 // | 38 // |
| 37 // Note that we not use DNS devolution to find the WPAD host, since that could | 39 // Note that we not use DNS devolution to find the WPAD host, since that could |
| 38 // be dangerous should our top level domain registry become out of date. | 40 // be dangerous should our top level domain registry become out of date. |
| 39 // | 41 // |
| 40 // Instead we directly resolve "wpad", and let the operating system apply the | 42 // Instead we directly resolve "wpad", and let the operating system apply the |
| 41 // DNS suffix search paths. This is the same approach taken by Firefox, and | 43 // DNS suffix search paths. This is the same approach taken by Firefox, and |
| 42 // compatibility hasn't been an issue. | 44 // compatibility hasn't been an issue. |
| 43 // | 45 // |
| 44 // For more details, also check out this comment: | 46 // For more details, also check out this comment: |
| 45 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 | 47 // http://code.google.com/p/chromium/issues/detail?id=18575#c20 |
| 46 static const char kWpadUrl[] = "http://wpad/wpad.dat"; | 48 static const char kWpadUrl[] = "http://wpad/wpad.dat"; |
| 47 | 49 |
| 50 Value* ProxyScriptDecider::PacSource::NetLogCallback( |
| 51 const GURL* effective_pac_url, |
| 52 NetLog::LogLevel /* log_level */) const { |
| 53 DictionaryValue* dict = new DictionaryValue(); |
| 54 std::string source; |
| 55 switch (type) { |
| 56 case PacSource::WPAD_DHCP: |
| 57 source = "WPAD DHCP"; |
| 58 break; |
| 59 case PacSource::WPAD_DNS: |
| 60 source = "WPAD DNS: "; |
| 61 source += effective_pac_url->possibly_invalid_spec(); |
| 62 break; |
| 63 case PacSource::CUSTOM: |
| 64 source = "Custom PAC URL: "; |
| 65 source += effective_pac_url->possibly_invalid_spec(); |
| 66 break; |
| 67 } |
| 68 dict->SetString("source", source); |
| 69 return dict; |
| 70 } |
| 71 |
| 48 ProxyScriptDecider::ProxyScriptDecider( | 72 ProxyScriptDecider::ProxyScriptDecider( |
| 49 ProxyScriptFetcher* proxy_script_fetcher, | 73 ProxyScriptFetcher* proxy_script_fetcher, |
| 50 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, | 74 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher, |
| 51 NetLog* net_log) | 75 NetLog* net_log) |
| 52 : resolver_(NULL), | 76 : resolver_(NULL), |
| 53 proxy_script_fetcher_(proxy_script_fetcher), | 77 proxy_script_fetcher_(proxy_script_fetcher), |
| 54 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), | 78 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher), |
| 55 current_pac_source_index_(0u), | 79 current_pac_source_index_(0u), |
| 56 pac_mandatory_(false), | 80 pac_mandatory_(false), |
| 57 next_state_(STATE_NONE), | 81 next_state_(STATE_NONE), |
| 58 net_log_(BoundNetLog::Make( | 82 net_log_(BoundNetLog::Make( |
| 59 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), | 83 net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)), |
| 60 fetch_pac_bytes_(false) { | 84 fetch_pac_bytes_(false) { |
| 61 } | 85 } |
| 62 | 86 |
| 63 ProxyScriptDecider::~ProxyScriptDecider() { | 87 ProxyScriptDecider::~ProxyScriptDecider() { |
| 64 if (next_state_ != STATE_NONE) | 88 if (next_state_ != STATE_NONE) |
| 65 Cancel(); | 89 Cancel(); |
| 66 } | 90 } |
| 67 | 91 |
| 68 int ProxyScriptDecider::Start( | 92 int ProxyScriptDecider::Start( |
| 69 const ProxyConfig& config, const base::TimeDelta wait_delay, | 93 const ProxyConfig& config, const base::TimeDelta wait_delay, |
| 70 bool fetch_pac_bytes, const CompletionCallback& callback) { | 94 bool fetch_pac_bytes, const CompletionCallback& callback) { |
| 71 DCHECK_EQ(STATE_NONE, next_state_); | 95 DCHECK_EQ(STATE_NONE, next_state_); |
| 72 DCHECK(!callback.is_null()); | 96 DCHECK(!callback.is_null()); |
| 73 DCHECK(config.HasAutomaticSettings()); | 97 DCHECK(config.HasAutomaticSettings()); |
| 74 | 98 |
| 75 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); | 99 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER); |
| 76 | 100 |
| 77 fetch_pac_bytes_ = fetch_pac_bytes; | 101 fetch_pac_bytes_ = fetch_pac_bytes; |
| 78 | 102 |
| 79 // Save the |wait_delay| as a non-negative value. | 103 // Save the |wait_delay| as a non-negative value. |
| 80 wait_delay_ = wait_delay; | 104 wait_delay_ = wait_delay; |
| 81 if (wait_delay_ < base::TimeDelta()) | 105 if (wait_delay_ < base::TimeDelta()) |
| 82 wait_delay_ = base::TimeDelta(); | 106 wait_delay_ = base::TimeDelta(); |
| 83 | 107 |
| 84 pac_mandatory_ = config.pac_mandatory(); | 108 pac_mandatory_ = config.pac_mandatory(); |
| 85 | 109 |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 int ProxyScriptDecider::DoWait() { | 204 int ProxyScriptDecider::DoWait() { |
| 181 next_state_ = STATE_WAIT_COMPLETE; | 205 next_state_ = STATE_WAIT_COMPLETE; |
| 182 | 206 |
| 183 // If no waiting is required, continue on to the next state. | 207 // If no waiting is required, continue on to the next state. |
| 184 if (wait_delay_.ToInternalValue() == 0) | 208 if (wait_delay_.ToInternalValue() == 0) |
| 185 return OK; | 209 return OK; |
| 186 | 210 |
| 187 // Otherwise wait the specified amount of time. | 211 // Otherwise wait the specified amount of time. |
| 188 wait_timer_.Start(FROM_HERE, wait_delay_, this, | 212 wait_timer_.Start(FROM_HERE, wait_delay_, this, |
| 189 &ProxyScriptDecider::OnWaitTimerFired); | 213 &ProxyScriptDecider::OnWaitTimerFired); |
| 190 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, NULL); | 214 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT); |
| 191 return ERR_IO_PENDING; | 215 return ERR_IO_PENDING; |
| 192 } | 216 } |
| 193 | 217 |
| 194 int ProxyScriptDecider::DoWaitComplete(int result) { | 218 int ProxyScriptDecider::DoWaitComplete(int result) { |
| 195 DCHECK_EQ(OK, result); | 219 DCHECK_EQ(OK, result); |
| 196 if (wait_delay_.ToInternalValue() != 0) { | 220 if (wait_delay_.ToInternalValue() != 0) { |
| 197 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, | 221 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT, |
| 198 result); | 222 result); |
| 199 } | 223 } |
| 200 next_state_ = GetStartState(); | 224 next_state_ = GetStartState(); |
| 201 return OK; | 225 return OK; |
| 202 } | 226 } |
| 203 | 227 |
| 204 int ProxyScriptDecider::DoFetchPacScript() { | 228 int ProxyScriptDecider::DoFetchPacScript() { |
| 205 DCHECK(fetch_pac_bytes_); | 229 DCHECK(fetch_pac_bytes_); |
| 206 | 230 |
| 207 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; | 231 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; |
| 208 | 232 |
| 209 const PacSource& pac_source = current_pac_source(); | 233 const PacSource& pac_source = current_pac_source(); |
| 210 | 234 |
| 211 GURL effective_pac_url; | 235 GURL effective_pac_url; |
| 212 NetLogStringParameter* log_parameter = | 236 DetermineURL(pac_source, &effective_pac_url); |
| 213 CreateNetLogParameterAndDetermineURL(pac_source, &effective_pac_url); | |
| 214 | 237 |
| 215 net_log_.BeginEvent( | 238 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, |
| 216 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, | 239 base::Bind(&PacSource::NetLogCallback, |
| 217 make_scoped_refptr(log_parameter)); | 240 base::Unretained(&pac_source), |
| 241 &effective_pac_url)); |
| 218 | 242 |
| 219 if (pac_source.type == PacSource::WPAD_DHCP) { | 243 if (pac_source.type == PacSource::WPAD_DHCP) { |
| 220 if (!dhcp_proxy_script_fetcher_) { | 244 if (!dhcp_proxy_script_fetcher_) { |
| 221 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); | 245 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); |
| 222 return ERR_UNEXPECTED; | 246 return ERR_UNEXPECTED; |
| 223 } | 247 } |
| 224 | 248 |
| 225 return dhcp_proxy_script_fetcher_->Fetch( | 249 return dhcp_proxy_script_fetcher_->Fetch( |
| 226 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, | 250 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion, |
| 227 base::Unretained(this))); | 251 base::Unretained(this))); |
| 228 } | 252 } |
| 229 | 253 |
| 230 if (!proxy_script_fetcher_) { | 254 if (!proxy_script_fetcher_) { |
| 231 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER, NULL); | 255 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER); |
| 232 return ERR_UNEXPECTED; | 256 return ERR_UNEXPECTED; |
| 233 } | 257 } |
| 234 | 258 |
| 235 return proxy_script_fetcher_->Fetch( | 259 return proxy_script_fetcher_->Fetch( |
| 236 effective_pac_url, &pac_script_, | 260 effective_pac_url, &pac_script_, |
| 237 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); | 261 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this))); |
| 238 } | 262 } |
| 239 | 263 |
| 240 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { | 264 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) { |
| 241 DCHECK(fetch_pac_bytes_); | 265 DCHECK(fetch_pac_bytes_); |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 | 339 |
| 316 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { | 340 if (current_pac_source_index_ + 1 >= pac_sources_.size()) { |
| 317 // Nothing left to fall back to. | 341 // Nothing left to fall back to. |
| 318 return error; | 342 return error; |
| 319 } | 343 } |
| 320 | 344 |
| 321 // Advance to next URL in our list. | 345 // Advance to next URL in our list. |
| 322 ++current_pac_source_index_; | 346 ++current_pac_source_index_; |
| 323 | 347 |
| 324 net_log_.AddEvent( | 348 net_log_.AddEvent( |
| 325 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE, NULL); | 349 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE); |
| 326 | 350 |
| 327 next_state_ = GetStartState(); | 351 next_state_ = GetStartState(); |
| 328 | 352 |
| 329 return OK; | 353 return OK; |
| 330 } | 354 } |
| 331 | 355 |
| 332 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const { | 356 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const { |
| 333 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT; | 357 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT; |
| 334 } | 358 } |
| 335 | 359 |
| 336 NetLogStringParameter* ProxyScriptDecider::CreateNetLogParameterAndDetermineURL( | 360 void ProxyScriptDecider::DetermineURL(const PacSource& pac_source, |
| 337 const PacSource& pac_source, | 361 GURL* effective_pac_url) { |
| 338 GURL* effective_pac_url) { | |
| 339 DCHECK(effective_pac_url); | 362 DCHECK(effective_pac_url); |
| 340 | 363 |
| 341 std::string source_field; | |
| 342 switch (pac_source.type) { | 364 switch (pac_source.type) { |
| 343 case PacSource::WPAD_DHCP: | 365 case PacSource::WPAD_DHCP: |
| 344 source_field = "WPAD DHCP"; | |
| 345 break; | 366 break; |
| 346 case PacSource::WPAD_DNS: | 367 case PacSource::WPAD_DNS: |
| 347 *effective_pac_url = GURL(kWpadUrl); | 368 *effective_pac_url = GURL(kWpadUrl); |
| 348 source_field = "WPAD DNS: "; | |
| 349 source_field += effective_pac_url->possibly_invalid_spec(); | |
| 350 break; | 369 break; |
| 351 case PacSource::CUSTOM: | 370 case PacSource::CUSTOM: |
| 352 *effective_pac_url = pac_source.url; | 371 *effective_pac_url = pac_source.url; |
| 353 source_field = "Custom PAC URL: "; | |
| 354 source_field += effective_pac_url->possibly_invalid_spec(); | |
| 355 break; | 372 break; |
| 356 } | 373 } |
| 357 return new NetLogStringParameter("source", source_field); | |
| 358 } | 374 } |
| 359 | 375 |
| 360 const ProxyScriptDecider::PacSource& | 376 const ProxyScriptDecider::PacSource& |
| 361 ProxyScriptDecider::current_pac_source() const { | 377 ProxyScriptDecider::current_pac_source() const { |
| 362 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); | 378 DCHECK_LT(current_pac_source_index_, pac_sources_.size()); |
| 363 return pac_sources_[current_pac_source_index_]; | 379 return pac_sources_[current_pac_source_index_]; |
| 364 } | 380 } |
| 365 | 381 |
| 366 void ProxyScriptDecider::OnWaitTimerFired() { | 382 void ProxyScriptDecider::OnWaitTimerFired() { |
| 367 OnIOCompletion(OK); | 383 OnIOCompletion(OK); |
| 368 } | 384 } |
| 369 | 385 |
| 370 void ProxyScriptDecider::DidComplete() { | 386 void ProxyScriptDecider::DidComplete() { |
| 371 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER, NULL); | 387 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER); |
| 372 } | 388 } |
| 373 | 389 |
| 374 void ProxyScriptDecider::Cancel() { | 390 void ProxyScriptDecider::Cancel() { |
| 375 DCHECK_NE(STATE_NONE, next_state_); | 391 DCHECK_NE(STATE_NONE, next_state_); |
| 376 | 392 |
| 377 net_log_.AddEvent(NetLog::TYPE_CANCELLED, NULL); | 393 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 378 | 394 |
| 379 switch (next_state_) { | 395 switch (next_state_) { |
| 380 case STATE_WAIT_COMPLETE: | 396 case STATE_WAIT_COMPLETE: |
| 381 wait_timer_.Stop(); | 397 wait_timer_.Stop(); |
| 382 break; | 398 break; |
| 383 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | 399 case STATE_FETCH_PAC_SCRIPT_COMPLETE: |
| 384 proxy_script_fetcher_->Cancel(); | 400 proxy_script_fetcher_->Cancel(); |
| 385 break; | 401 break; |
| 386 default: | 402 default: |
| 387 NOTREACHED(); | 403 NOTREACHED(); |
| 388 break; | 404 break; |
| 389 } | 405 } |
| 390 | 406 |
| 391 // This is safe to call in any state. | 407 // This is safe to call in any state. |
| 392 if (dhcp_proxy_script_fetcher_) | 408 if (dhcp_proxy_script_fetcher_) |
| 393 dhcp_proxy_script_fetcher_->Cancel(); | 409 dhcp_proxy_script_fetcher_->Cancel(); |
| 394 | 410 |
| 395 DidComplete(); | 411 DidComplete(); |
| 396 } | 412 } |
| 397 | 413 |
| 398 } // namespace net | 414 } // namespace net |
| OLD | NEW |