Rename InputLatency::ScrollUpdate to Latency::ScrollUpdate
[chromium-blink-merge.git] / net / proxy / proxy_script_decider.cc
blobe7d8b170159a17dd11679294786028fa107f5c36
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
3 // found in the LICENSE file.
5 #include "net/proxy/proxy_script_decider.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/compiler_specific.h"
10 #include "base/format_macros.h"
11 #include "base/logging.h"
12 #include "base/metrics/histogram.h"
13 #include "base/profiler/scoped_tracker.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "net/base/net_errors.h"
18 #include "net/proxy/dhcp_proxy_script_fetcher.h"
19 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
20 #include "net/proxy/proxy_script_fetcher.h"
21 #include "net/url_request/url_request_context.h"
23 namespace net {
25 namespace {
27 bool LooksLikePacScript(const base::string16& script) {
28 // Note: this is only an approximation! It may not always work correctly,
29 // however it is very likely that legitimate scripts have this exact string,
30 // since they must minimally define a function of this name. Conversely, a
31 // file not containing the string is not likely to be a PAC script.
33 // An exact test would have to load the script in a javascript evaluator.
34 return script.find(base::ASCIIToUTF16("FindProxyForURL")) !=
35 base::string16::npos;
40 // This is the hard-coded location used by the DNS portion of web proxy
41 // auto-discovery.
43 // Note that we not use DNS devolution to find the WPAD host, since that could
44 // be dangerous should our top level domain registry become out of date.
46 // Instead we directly resolve "wpad", and let the operating system apply the
47 // DNS suffix search paths. This is the same approach taken by Firefox, and
48 // compatibility hasn't been an issue.
50 // For more details, also check out this comment:
51 // http://code.google.com/p/chromium/issues/detail?id=18575#c20
52 namespace {
53 const char kWpadUrl[] = "http://wpad/wpad.dat";
54 const int kQuickCheckDelayMs = 1000;
57 base::Value* ProxyScriptDecider::PacSource::NetLogCallback(
58 const GURL* effective_pac_url,
59 NetLogCaptureMode /* capture_mode */) const {
60 base::DictionaryValue* dict = new base::DictionaryValue();
61 std::string source;
62 switch (type) {
63 case PacSource::WPAD_DHCP:
64 source = "WPAD DHCP";
65 break;
66 case PacSource::WPAD_DNS:
67 source = "WPAD DNS: ";
68 source += effective_pac_url->possibly_invalid_spec();
69 break;
70 case PacSource::CUSTOM:
71 source = "Custom PAC URL: ";
72 source += effective_pac_url->possibly_invalid_spec();
73 break;
75 dict->SetString("source", source);
76 return dict;
79 ProxyScriptDecider::ProxyScriptDecider(
80 ProxyScriptFetcher* proxy_script_fetcher,
81 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher,
82 NetLog* net_log)
83 : proxy_script_fetcher_(proxy_script_fetcher),
84 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher),
85 current_pac_source_index_(0u),
86 pac_mandatory_(false),
87 next_state_(STATE_NONE),
88 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)),
89 fetch_pac_bytes_(false),
90 quick_check_enabled_(true) {
91 if (proxy_script_fetcher &&
92 proxy_script_fetcher->GetRequestContext() &&
93 proxy_script_fetcher->GetRequestContext()->host_resolver()) {
94 host_resolver_.reset(new SingleRequestHostResolver(
95 proxy_script_fetcher->GetRequestContext()->host_resolver()));
99 ProxyScriptDecider::~ProxyScriptDecider() {
100 if (next_state_ != STATE_NONE)
101 Cancel();
104 int ProxyScriptDecider::Start(
105 const ProxyConfig& config, const base::TimeDelta wait_delay,
106 bool fetch_pac_bytes, const CompletionCallback& callback) {
107 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
108 tracked_objects::ScopedTracker tracking_profile(
109 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 ProxyScriptDecider::Start"));
111 DCHECK_EQ(STATE_NONE, next_state_);
112 DCHECK(!callback.is_null());
113 DCHECK(config.HasAutomaticSettings());
115 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
117 fetch_pac_bytes_ = fetch_pac_bytes;
119 // Save the |wait_delay| as a non-negative value.
120 wait_delay_ = wait_delay;
121 if (wait_delay_ < base::TimeDelta())
122 wait_delay_ = base::TimeDelta();
124 pac_mandatory_ = config.pac_mandatory();
125 have_custom_pac_url_ = config.has_pac_url();
127 pac_sources_ = BuildPacSourcesFallbackList(config);
128 DCHECK(!pac_sources_.empty());
130 next_state_ = STATE_WAIT;
132 int rv = DoLoop(OK);
133 if (rv == ERR_IO_PENDING)
134 callback_ = callback;
135 else
136 DidComplete();
138 return rv;
141 const ProxyConfig& ProxyScriptDecider::effective_config() const {
142 DCHECK_EQ(STATE_NONE, next_state_);
143 return effective_config_;
146 // TODO(eroman): Return a const-pointer.
147 ProxyResolverScriptData* ProxyScriptDecider::script_data() const {
148 DCHECK_EQ(STATE_NONE, next_state_);
149 return script_data_.get();
152 // Initialize the fallback rules.
153 // (1) WPAD (DHCP).
154 // (2) WPAD (DNS).
155 // (3) Custom PAC URL.
156 ProxyScriptDecider::PacSourceList ProxyScriptDecider::
157 BuildPacSourcesFallbackList(
158 const ProxyConfig& config) const {
159 PacSourceList pac_sources;
160 if (config.auto_detect()) {
161 pac_sources.push_back(PacSource(PacSource::WPAD_DHCP, GURL(kWpadUrl)));
162 pac_sources.push_back(PacSource(PacSource::WPAD_DNS, GURL(kWpadUrl)));
164 if (config.has_pac_url())
165 pac_sources.push_back(PacSource(PacSource::CUSTOM, config.pac_url()));
166 return pac_sources;
169 void ProxyScriptDecider::OnIOCompletion(int result) {
170 DCHECK_NE(STATE_NONE, next_state_);
171 int rv = DoLoop(result);
172 if (rv != ERR_IO_PENDING) {
173 DidComplete();
174 DoCallback(rv);
178 int ProxyScriptDecider::DoLoop(int result) {
179 DCHECK_NE(next_state_, STATE_NONE);
180 int rv = result;
181 do {
182 State state = next_state_;
183 next_state_ = STATE_NONE;
184 switch (state) {
185 case STATE_WAIT:
186 DCHECK_EQ(OK, rv);
187 rv = DoWait();
188 break;
189 case STATE_WAIT_COMPLETE:
190 rv = DoWaitComplete(rv);
191 break;
192 case STATE_QUICK_CHECK:
193 DCHECK_EQ(OK, rv);
194 rv = DoQuickCheck();
195 break;
196 case STATE_QUICK_CHECK_COMPLETE:
197 rv = DoQuickCheckComplete(rv);
198 break;
199 case STATE_FETCH_PAC_SCRIPT:
200 DCHECK_EQ(OK, rv);
201 rv = DoFetchPacScript();
202 break;
203 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
204 rv = DoFetchPacScriptComplete(rv);
205 break;
206 case STATE_VERIFY_PAC_SCRIPT:
207 DCHECK_EQ(OK, rv);
208 rv = DoVerifyPacScript();
209 break;
210 case STATE_VERIFY_PAC_SCRIPT_COMPLETE:
211 rv = DoVerifyPacScriptComplete(rv);
212 break;
213 default:
214 NOTREACHED() << "bad state";
215 rv = ERR_UNEXPECTED;
216 break;
218 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
219 return rv;
222 void ProxyScriptDecider::DoCallback(int result) {
223 DCHECK_NE(ERR_IO_PENDING, result);
224 DCHECK(!callback_.is_null());
225 callback_.Run(result);
228 int ProxyScriptDecider::DoWait() {
229 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
230 tracked_objects::ScopedTracker tracking_profile(
231 FROM_HERE_WITH_EXPLICIT_FUNCTION("455942 ProxyScriptDecider::DoWait"));
233 next_state_ = STATE_WAIT_COMPLETE;
235 // If no waiting is required, continue on to the next state.
236 if (wait_delay_.ToInternalValue() == 0)
237 return OK;
239 // Otherwise wait the specified amount of time.
240 wait_timer_.Start(FROM_HERE, wait_delay_, this,
241 &ProxyScriptDecider::OnWaitTimerFired);
242 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT);
243 return ERR_IO_PENDING;
246 int ProxyScriptDecider::DoWaitComplete(int result) {
247 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
248 tracked_objects::ScopedTracker tracking_profile(
249 FROM_HERE_WITH_EXPLICIT_FUNCTION(
250 "455942 ProxyScriptDecider::DoWaitComplete"));
252 DCHECK_EQ(OK, result);
253 if (wait_delay_.ToInternalValue() != 0) {
254 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT,
255 result);
257 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS)
258 next_state_ = STATE_QUICK_CHECK;
259 else
260 next_state_ = GetStartState();
261 return OK;
264 int ProxyScriptDecider::DoQuickCheck() {
265 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
266 tracked_objects::ScopedTracker tracking_profile(
267 FROM_HERE_WITH_EXPLICIT_FUNCTION(
268 "455942 ProxyScriptDecider::DoQuickCheck"));
270 DCHECK(quick_check_enabled_);
271 if (host_resolver_.get() == NULL) {
272 // If we have no resolver, skip QuickCheck altogether.
273 next_state_ = GetStartState();
274 return OK;
277 quick_check_start_time_ = base::Time::Now();
278 std::string host = current_pac_source().url.host();
279 HostResolver::RequestInfo reqinfo(HostPortPair(host, 80));
280 reqinfo.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
281 CompletionCallback callback = base::Bind(
282 &ProxyScriptDecider::OnIOCompletion,
283 base::Unretained(this));
285 next_state_ = STATE_QUICK_CHECK_COMPLETE;
286 quick_check_timer_.Start(FROM_HERE,
287 base::TimeDelta::FromMilliseconds(
288 kQuickCheckDelayMs),
289 base::Bind(callback, ERR_NAME_NOT_RESOLVED));
291 // We use HIGHEST here because proxy decision blocks doing any other requests.
292 return host_resolver_->Resolve(reqinfo, HIGHEST, &wpad_addresses_,
293 callback, net_log_);
296 int ProxyScriptDecider::DoQuickCheckComplete(int result) {
297 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
298 tracked_objects::ScopedTracker tracking_profile(
299 FROM_HERE_WITH_EXPLICIT_FUNCTION(
300 "455942 ProxyScriptDecider::DoQuickCheckComplete"));
302 DCHECK(quick_check_enabled_);
303 base::TimeDelta delta = base::Time::Now() - quick_check_start_time_;
304 if (result == OK)
305 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta);
306 else
307 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta);
308 host_resolver_->Cancel();
309 quick_check_timer_.Stop();
310 if (result != OK)
311 return TryToFallbackPacSource(result);
312 next_state_ = GetStartState();
313 return result;
316 int ProxyScriptDecider::DoFetchPacScript() {
317 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
318 tracked_objects::ScopedTracker tracking_profile(
319 FROM_HERE_WITH_EXPLICIT_FUNCTION(
320 "455942 ProxyScriptDecider::DoFetchPacScript"));
322 DCHECK(fetch_pac_bytes_);
324 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE;
326 const PacSource& pac_source = current_pac_source();
328 GURL effective_pac_url;
329 DetermineURL(pac_source, &effective_pac_url);
331 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT,
332 base::Bind(&PacSource::NetLogCallback,
333 base::Unretained(&pac_source),
334 &effective_pac_url));
336 if (pac_source.type == PacSource::WPAD_DHCP) {
337 if (!dhcp_proxy_script_fetcher_) {
338 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
339 return ERR_UNEXPECTED;
342 return dhcp_proxy_script_fetcher_->Fetch(
343 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion,
344 base::Unretained(this)));
347 if (!proxy_script_fetcher_) {
348 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
349 return ERR_UNEXPECTED;
352 return proxy_script_fetcher_->Fetch(
353 effective_pac_url, &pac_script_,
354 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this)));
357 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) {
358 DCHECK(fetch_pac_bytes_);
360 net_log_.EndEventWithNetErrorCode(
361 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result);
362 if (result != OK)
363 return TryToFallbackPacSource(result);
365 next_state_ = STATE_VERIFY_PAC_SCRIPT;
366 return result;
369 int ProxyScriptDecider::DoVerifyPacScript() {
370 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
371 tracked_objects::ScopedTracker tracking_profile(
372 FROM_HERE_WITH_EXPLICIT_FUNCTION(
373 "455942 ProxyScriptDecider::DoVerifyPacScript"));
375 next_state_ = STATE_VERIFY_PAC_SCRIPT_COMPLETE;
377 // This is just a heuristic. Ideally we would try to parse the script.
378 if (fetch_pac_bytes_ && !LooksLikePacScript(pac_script_))
379 return ERR_PAC_SCRIPT_FAILED;
381 return OK;
384 int ProxyScriptDecider::DoVerifyPacScriptComplete(int result) {
385 // TODO(eroman): Remove ScopedTracker below once crbug.com/455942 is fixed.
386 tracked_objects::ScopedTracker tracking_profile(
387 FROM_HERE_WITH_EXPLICIT_FUNCTION(
388 "455942 ProxyScriptDecider::DoVerifyPacScriptComplete"));
390 if (result != OK)
391 return TryToFallbackPacSource(result);
393 const PacSource& pac_source = current_pac_source();
395 // Extract the current script data.
396 if (fetch_pac_bytes_) {
397 script_data_ = ProxyResolverScriptData::FromUTF16(pac_script_);
398 } else {
399 script_data_ = pac_source.type == PacSource::CUSTOM ?
400 ProxyResolverScriptData::FromURL(pac_source.url) :
401 ProxyResolverScriptData::ForAutoDetect();
404 // Let the caller know which automatic setting we ended up initializing the
405 // resolver for (there may have been multiple fallbacks to choose from.)
406 if (current_pac_source().type == PacSource::CUSTOM) {
407 effective_config_ =
408 ProxyConfig::CreateFromCustomPacURL(current_pac_source().url);
409 effective_config_.set_pac_mandatory(pac_mandatory_);
410 } else {
411 if (fetch_pac_bytes_) {
412 GURL auto_detected_url;
414 switch (current_pac_source().type) {
415 case PacSource::WPAD_DHCP:
416 auto_detected_url = dhcp_proxy_script_fetcher_->GetPacURL();
417 break;
419 case PacSource::WPAD_DNS:
420 auto_detected_url = GURL(kWpadUrl);
421 break;
423 default:
424 NOTREACHED();
427 effective_config_ =
428 ProxyConfig::CreateFromCustomPacURL(auto_detected_url);
429 } else {
430 // The resolver does its own resolution so we cannot know the
431 // URL. Just do the best we can and state that the configuration
432 // is to auto-detect proxy settings.
433 effective_config_ = ProxyConfig::CreateAutoDetect();
437 return OK;
440 int ProxyScriptDecider::TryToFallbackPacSource(int error) {
441 DCHECK_LT(error, 0);
443 if (current_pac_source_index_ + 1 >= pac_sources_.size()) {
444 // Nothing left to fall back to.
445 return error;
448 // Advance to next URL in our list.
449 ++current_pac_source_index_;
451 net_log_.AddEvent(
452 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE);
453 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS)
454 next_state_ = STATE_QUICK_CHECK;
455 else
456 next_state_ = GetStartState();
458 return OK;
461 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const {
462 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT;
465 void ProxyScriptDecider::DetermineURL(const PacSource& pac_source,
466 GURL* effective_pac_url) {
467 DCHECK(effective_pac_url);
469 switch (pac_source.type) {
470 case PacSource::WPAD_DHCP:
471 break;
472 case PacSource::WPAD_DNS:
473 *effective_pac_url = GURL(kWpadUrl);
474 break;
475 case PacSource::CUSTOM:
476 *effective_pac_url = pac_source.url;
477 break;
481 const ProxyScriptDecider::PacSource&
482 ProxyScriptDecider::current_pac_source() const {
483 DCHECK_LT(current_pac_source_index_, pac_sources_.size());
484 return pac_sources_[current_pac_source_index_];
487 void ProxyScriptDecider::OnWaitTimerFired() {
488 OnIOCompletion(OK);
491 void ProxyScriptDecider::DidComplete() {
492 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
495 void ProxyScriptDecider::Cancel() {
496 DCHECK_NE(STATE_NONE, next_state_);
498 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
500 switch (next_state_) {
501 case STATE_WAIT_COMPLETE:
502 wait_timer_.Stop();
503 break;
504 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
505 proxy_script_fetcher_->Cancel();
506 break;
507 default:
508 break;
511 // This is safe to call in any state.
512 if (dhcp_proxy_script_fetcher_)
513 dhcp_proxy_script_fetcher_->Cancel();
515 DidComplete();
518 } // namespace net