Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / proxy / proxy_script_decider.cc
blob3d830fb84db764f601f9e7a184d9f1f336434089
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_macros.h"
13 #include "base/strings/string_util.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "net/base/net_errors.h"
17 #include "net/proxy/dhcp_proxy_script_fetcher.h"
18 #include "net/proxy/dhcp_proxy_script_fetcher_factory.h"
19 #include "net/proxy/proxy_script_fetcher.h"
20 #include "net/url_request/url_request_context.h"
22 namespace net {
24 namespace {
26 bool LooksLikePacScript(const base::string16& script) {
27 // Note: this is only an approximation! It may not always work correctly,
28 // however it is very likely that legitimate scripts have this exact string,
29 // since they must minimally define a function of this name. Conversely, a
30 // file not containing the string is not likely to be a PAC script.
32 // An exact test would have to load the script in a javascript evaluator.
33 return script.find(base::ASCIIToUTF16("FindProxyForURL")) !=
34 base::string16::npos;
39 // This is the hard-coded location used by the DNS portion of web proxy
40 // auto-discovery.
42 // Note that we not use DNS devolution to find the WPAD host, since that could
43 // be dangerous should our top level domain registry become out of date.
45 // Instead we directly resolve "wpad", and let the operating system apply the
46 // DNS suffix search paths. This is the same approach taken by Firefox, and
47 // compatibility hasn't been an issue.
49 // For more details, also check out this comment:
50 // http://code.google.com/p/chromium/issues/detail?id=18575#c20
51 namespace {
52 const char kWpadUrl[] = "http://wpad/wpad.dat";
53 const int kQuickCheckDelayMs = 1000;
56 scoped_ptr<base::Value> ProxyScriptDecider::PacSource::NetLogCallback(
57 const GURL* effective_pac_url,
58 NetLogCaptureMode /* capture_mode */) const {
59 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
60 std::string source;
61 switch (type) {
62 case PacSource::WPAD_DHCP:
63 source = "WPAD DHCP";
64 break;
65 case PacSource::WPAD_DNS:
66 source = "WPAD DNS: ";
67 source += effective_pac_url->possibly_invalid_spec();
68 break;
69 case PacSource::CUSTOM:
70 source = "Custom PAC URL: ";
71 source += effective_pac_url->possibly_invalid_spec();
72 break;
74 dict->SetString("source", source);
75 return dict.Pass();
78 ProxyScriptDecider::ProxyScriptDecider(
79 ProxyScriptFetcher* proxy_script_fetcher,
80 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher,
81 NetLog* net_log)
82 : proxy_script_fetcher_(proxy_script_fetcher),
83 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher),
84 current_pac_source_index_(0u),
85 pac_mandatory_(false),
86 next_state_(STATE_NONE),
87 net_log_(BoundNetLog::Make(net_log, NetLog::SOURCE_PROXY_SCRIPT_DECIDER)),
88 fetch_pac_bytes_(false),
89 quick_check_enabled_(true) {
90 if (proxy_script_fetcher &&
91 proxy_script_fetcher->GetRequestContext() &&
92 proxy_script_fetcher->GetRequestContext()->host_resolver()) {
93 host_resolver_.reset(new SingleRequestHostResolver(
94 proxy_script_fetcher->GetRequestContext()->host_resolver()));
98 ProxyScriptDecider::~ProxyScriptDecider() {
99 if (next_state_ != STATE_NONE)
100 Cancel();
103 int ProxyScriptDecider::Start(
104 const ProxyConfig& config, const base::TimeDelta wait_delay,
105 bool fetch_pac_bytes, const CompletionCallback& callback) {
106 DCHECK_EQ(STATE_NONE, next_state_);
107 DCHECK(!callback.is_null());
108 DCHECK(config.HasAutomaticSettings());
110 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
112 fetch_pac_bytes_ = fetch_pac_bytes;
114 // Save the |wait_delay| as a non-negative value.
115 wait_delay_ = wait_delay;
116 if (wait_delay_ < base::TimeDelta())
117 wait_delay_ = base::TimeDelta();
119 pac_mandatory_ = config.pac_mandatory();
120 have_custom_pac_url_ = config.has_pac_url();
122 pac_sources_ = BuildPacSourcesFallbackList(config);
123 DCHECK(!pac_sources_.empty());
125 next_state_ = STATE_WAIT;
127 int rv = DoLoop(OK);
128 if (rv == ERR_IO_PENDING)
129 callback_ = callback;
130 else
131 DidComplete();
133 return rv;
136 const ProxyConfig& ProxyScriptDecider::effective_config() const {
137 DCHECK_EQ(STATE_NONE, next_state_);
138 return effective_config_;
141 // TODO(eroman): Return a const-pointer.
142 ProxyResolverScriptData* ProxyScriptDecider::script_data() const {
143 DCHECK_EQ(STATE_NONE, next_state_);
144 return script_data_.get();
147 // Initialize the fallback rules.
148 // (1) WPAD (DHCP).
149 // (2) WPAD (DNS).
150 // (3) Custom PAC URL.
151 ProxyScriptDecider::PacSourceList ProxyScriptDecider::
152 BuildPacSourcesFallbackList(
153 const ProxyConfig& config) const {
154 PacSourceList pac_sources;
155 if (config.auto_detect()) {
156 pac_sources.push_back(PacSource(PacSource::WPAD_DHCP, GURL(kWpadUrl)));
157 pac_sources.push_back(PacSource(PacSource::WPAD_DNS, GURL(kWpadUrl)));
159 if (config.has_pac_url())
160 pac_sources.push_back(PacSource(PacSource::CUSTOM, config.pac_url()));
161 return pac_sources;
164 void ProxyScriptDecider::OnIOCompletion(int result) {
165 DCHECK_NE(STATE_NONE, next_state_);
166 int rv = DoLoop(result);
167 if (rv != ERR_IO_PENDING) {
168 DidComplete();
169 DoCallback(rv);
173 int ProxyScriptDecider::DoLoop(int result) {
174 DCHECK_NE(next_state_, STATE_NONE);
175 int rv = result;
176 do {
177 State state = next_state_;
178 next_state_ = STATE_NONE;
179 switch (state) {
180 case STATE_WAIT:
181 DCHECK_EQ(OK, rv);
182 rv = DoWait();
183 break;
184 case STATE_WAIT_COMPLETE:
185 rv = DoWaitComplete(rv);
186 break;
187 case STATE_QUICK_CHECK:
188 DCHECK_EQ(OK, rv);
189 rv = DoQuickCheck();
190 break;
191 case STATE_QUICK_CHECK_COMPLETE:
192 rv = DoQuickCheckComplete(rv);
193 break;
194 case STATE_FETCH_PAC_SCRIPT:
195 DCHECK_EQ(OK, rv);
196 rv = DoFetchPacScript();
197 break;
198 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
199 rv = DoFetchPacScriptComplete(rv);
200 break;
201 case STATE_VERIFY_PAC_SCRIPT:
202 DCHECK_EQ(OK, rv);
203 rv = DoVerifyPacScript();
204 break;
205 case STATE_VERIFY_PAC_SCRIPT_COMPLETE:
206 rv = DoVerifyPacScriptComplete(rv);
207 break;
208 default:
209 NOTREACHED() << "bad state";
210 rv = ERR_UNEXPECTED;
211 break;
213 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
214 return rv;
217 void ProxyScriptDecider::DoCallback(int result) {
218 DCHECK_NE(ERR_IO_PENDING, result);
219 DCHECK(!callback_.is_null());
220 callback_.Run(result);
223 int ProxyScriptDecider::DoWait() {
224 next_state_ = STATE_WAIT_COMPLETE;
226 // If no waiting is required, continue on to the next state.
227 if (wait_delay_.ToInternalValue() == 0)
228 return OK;
230 // Otherwise wait the specified amount of time.
231 wait_timer_.Start(FROM_HERE, wait_delay_, this,
232 &ProxyScriptDecider::OnWaitTimerFired);
233 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT);
234 return ERR_IO_PENDING;
237 int ProxyScriptDecider::DoWaitComplete(int result) {
238 DCHECK_EQ(OK, result);
239 if (wait_delay_.ToInternalValue() != 0) {
240 net_log_.EndEventWithNetErrorCode(NetLog::TYPE_PROXY_SCRIPT_DECIDER_WAIT,
241 result);
243 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS)
244 next_state_ = STATE_QUICK_CHECK;
245 else
246 next_state_ = GetStartState();
247 return OK;
250 int ProxyScriptDecider::DoQuickCheck() {
251 DCHECK(quick_check_enabled_);
252 if (host_resolver_.get() == NULL) {
253 // If we have no resolver, skip QuickCheck altogether.
254 next_state_ = GetStartState();
255 return OK;
258 quick_check_start_time_ = base::Time::Now();
259 std::string host = current_pac_source().url.host();
260 HostResolver::RequestInfo reqinfo(HostPortPair(host, 80));
261 reqinfo.set_host_resolver_flags(HOST_RESOLVER_SYSTEM_ONLY);
262 CompletionCallback callback = base::Bind(
263 &ProxyScriptDecider::OnIOCompletion,
264 base::Unretained(this));
266 next_state_ = STATE_QUICK_CHECK_COMPLETE;
267 quick_check_timer_.Start(FROM_HERE,
268 base::TimeDelta::FromMilliseconds(
269 kQuickCheckDelayMs),
270 base::Bind(callback, ERR_NAME_NOT_RESOLVED));
272 // We use HIGHEST here because proxy decision blocks doing any other requests.
273 return host_resolver_->Resolve(reqinfo, HIGHEST, &wpad_addresses_,
274 callback, net_log_);
277 int ProxyScriptDecider::DoQuickCheckComplete(int result) {
278 DCHECK(quick_check_enabled_);
279 base::TimeDelta delta = base::Time::Now() - quick_check_start_time_;
280 if (result == OK)
281 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckSuccess", delta);
282 else
283 UMA_HISTOGRAM_TIMES("Net.WpadQuickCheckFailure", delta);
284 host_resolver_->Cancel();
285 quick_check_timer_.Stop();
286 if (result != OK)
287 return TryToFallbackPacSource(result);
288 next_state_ = GetStartState();
289 return result;
292 int ProxyScriptDecider::DoFetchPacScript() {
293 DCHECK(fetch_pac_bytes_);
295 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE;
297 const PacSource& pac_source = current_pac_source();
299 GURL effective_pac_url;
300 DetermineURL(pac_source, &effective_pac_url);
302 net_log_.BeginEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT,
303 base::Bind(&PacSource::NetLogCallback,
304 base::Unretained(&pac_source),
305 &effective_pac_url));
307 if (pac_source.type == PacSource::WPAD_DHCP) {
308 if (!dhcp_proxy_script_fetcher_) {
309 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
310 return ERR_UNEXPECTED;
313 return dhcp_proxy_script_fetcher_->Fetch(
314 &pac_script_, base::Bind(&ProxyScriptDecider::OnIOCompletion,
315 base::Unretained(this)));
318 if (!proxy_script_fetcher_) {
319 net_log_.AddEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER_HAS_NO_FETCHER);
320 return ERR_UNEXPECTED;
323 return proxy_script_fetcher_->Fetch(
324 effective_pac_url, &pac_script_,
325 base::Bind(&ProxyScriptDecider::OnIOCompletion, base::Unretained(this)));
328 int ProxyScriptDecider::DoFetchPacScriptComplete(int result) {
329 DCHECK(fetch_pac_bytes_);
331 net_log_.EndEventWithNetErrorCode(
332 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FETCH_PAC_SCRIPT, result);
333 if (result != OK)
334 return TryToFallbackPacSource(result);
336 next_state_ = STATE_VERIFY_PAC_SCRIPT;
337 return result;
340 int ProxyScriptDecider::DoVerifyPacScript() {
341 next_state_ = STATE_VERIFY_PAC_SCRIPT_COMPLETE;
343 // This is just a heuristic. Ideally we would try to parse the script.
344 if (fetch_pac_bytes_ && !LooksLikePacScript(pac_script_))
345 return ERR_PAC_SCRIPT_FAILED;
347 return OK;
350 int ProxyScriptDecider::DoVerifyPacScriptComplete(int result) {
351 if (result != OK)
352 return TryToFallbackPacSource(result);
354 const PacSource& pac_source = current_pac_source();
356 // Extract the current script data.
357 if (fetch_pac_bytes_) {
358 script_data_ = ProxyResolverScriptData::FromUTF16(pac_script_);
359 } else {
360 script_data_ = pac_source.type == PacSource::CUSTOM ?
361 ProxyResolverScriptData::FromURL(pac_source.url) :
362 ProxyResolverScriptData::ForAutoDetect();
365 // Let the caller know which automatic setting we ended up initializing the
366 // resolver for (there may have been multiple fallbacks to choose from.)
367 if (current_pac_source().type == PacSource::CUSTOM) {
368 effective_config_ =
369 ProxyConfig::CreateFromCustomPacURL(current_pac_source().url);
370 effective_config_.set_pac_mandatory(pac_mandatory_);
371 } else {
372 if (fetch_pac_bytes_) {
373 GURL auto_detected_url;
375 switch (current_pac_source().type) {
376 case PacSource::WPAD_DHCP:
377 auto_detected_url = dhcp_proxy_script_fetcher_->GetPacURL();
378 break;
380 case PacSource::WPAD_DNS:
381 auto_detected_url = GURL(kWpadUrl);
382 break;
384 default:
385 NOTREACHED();
388 effective_config_ =
389 ProxyConfig::CreateFromCustomPacURL(auto_detected_url);
390 } else {
391 // The resolver does its own resolution so we cannot know the
392 // URL. Just do the best we can and state that the configuration
393 // is to auto-detect proxy settings.
394 effective_config_ = ProxyConfig::CreateAutoDetect();
398 return OK;
401 int ProxyScriptDecider::TryToFallbackPacSource(int error) {
402 DCHECK_LT(error, 0);
404 if (current_pac_source_index_ + 1 >= pac_sources_.size()) {
405 // Nothing left to fall back to.
406 return error;
409 // Advance to next URL in our list.
410 ++current_pac_source_index_;
412 net_log_.AddEvent(
413 NetLog::TYPE_PROXY_SCRIPT_DECIDER_FALLING_BACK_TO_NEXT_PAC_SOURCE);
414 if (quick_check_enabled_ && current_pac_source().type == PacSource::WPAD_DNS)
415 next_state_ = STATE_QUICK_CHECK;
416 else
417 next_state_ = GetStartState();
419 return OK;
422 ProxyScriptDecider::State ProxyScriptDecider::GetStartState() const {
423 return fetch_pac_bytes_ ? STATE_FETCH_PAC_SCRIPT : STATE_VERIFY_PAC_SCRIPT;
426 void ProxyScriptDecider::DetermineURL(const PacSource& pac_source,
427 GURL* effective_pac_url) {
428 DCHECK(effective_pac_url);
430 switch (pac_source.type) {
431 case PacSource::WPAD_DHCP:
432 break;
433 case PacSource::WPAD_DNS:
434 *effective_pac_url = GURL(kWpadUrl);
435 break;
436 case PacSource::CUSTOM:
437 *effective_pac_url = pac_source.url;
438 break;
442 const ProxyScriptDecider::PacSource&
443 ProxyScriptDecider::current_pac_source() const {
444 DCHECK_LT(current_pac_source_index_, pac_sources_.size());
445 return pac_sources_[current_pac_source_index_];
448 void ProxyScriptDecider::OnWaitTimerFired() {
449 OnIOCompletion(OK);
452 void ProxyScriptDecider::DidComplete() {
453 net_log_.EndEvent(NetLog::TYPE_PROXY_SCRIPT_DECIDER);
456 void ProxyScriptDecider::Cancel() {
457 DCHECK_NE(STATE_NONE, next_state_);
459 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
461 switch (next_state_) {
462 case STATE_WAIT_COMPLETE:
463 wait_timer_.Stop();
464 break;
465 case STATE_FETCH_PAC_SCRIPT_COMPLETE:
466 proxy_script_fetcher_->Cancel();
467 break;
468 default:
469 break;
472 // This is safe to call in any state.
473 if (dhcp_proxy_script_fetcher_)
474 dhcp_proxy_script_fetcher_->Cancel();
476 DidComplete();
479 } // namespace net