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_service.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/logging.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/message_loop/message_loop.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/strings/string_util.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/values.h"
19 #include "net/base/completion_callback.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_log.h"
22 #include "net/base/net_util.h"
23 #include "net/proxy/dhcp_proxy_script_fetcher.h"
24 #include "net/proxy/multi_threaded_proxy_resolver.h"
25 #include "net/proxy/network_delegate_error_observer.h"
26 #include "net/proxy/proxy_config_service_fixed.h"
27 #include "net/proxy/proxy_resolver.h"
28 #include "net/proxy/proxy_script_decider.h"
29 #include "net/proxy/proxy_script_fetcher.h"
30 #include "net/url_request/url_request_context.h"
34 #include "net/proxy/proxy_config_service_win.h"
35 #include "net/proxy/proxy_resolver_winhttp.h"
37 #include "net/proxy/proxy_config_service_ios.h"
38 #include "net/proxy/proxy_resolver_mac.h"
39 #elif defined(OS_MACOSX)
40 #include "net/proxy/proxy_config_service_mac.h"
41 #include "net/proxy/proxy_resolver_mac.h"
42 #elif defined(OS_LINUX) && !defined(OS_CHROMEOS)
43 #include "net/proxy/proxy_config_service_linux.h"
44 #elif defined(OS_ANDROID)
45 #include "net/proxy/proxy_config_service_android.h"
48 #if defined(SPDY_PROXY_AUTH_ORIGIN)
49 #include "base/metrics/histogram.h"
52 using base::TimeDelta;
53 using base::TimeTicks;
59 // When the IP address changes we don't immediately re-run proxy auto-config.
60 // Instead, we wait for |kDelayAfterNetworkChangesMs| before
61 // attempting to re-valuate proxy auto-config.
63 // During this time window, any resolve requests sent to the ProxyService will
64 // be queued. Once we have waited the required amount of them, the proxy
65 // auto-config step will be run, and the queued requests resumed.
67 // The reason we play this game is that our signal for detecting network
68 // changes (NetworkChangeNotifier) may fire *before* the system's networking
69 // dependencies are fully configured. This is a problem since it means if
70 // we were to run proxy auto-config right away, it could fail due to spurious
71 // DNS failures. (see http://crbug.com/50779 for more details.)
73 // By adding the wait window, we give things a better chance to get properly
74 // set up. Network failures can happen at any time though, so we additionally
75 // poll the PAC script for changes, which will allow us to recover from these
77 const int64 kDelayAfterNetworkChangesMs = 2000;
79 // This is the default policy for polling the PAC script.
81 // In response to a failure, the poll intervals are:
82 // 0: 8 seconds (scheduled on timer)
87 // In response to a success, the poll intervals are:
90 // Only the 8 second poll is scheduled on a timer, the rest happen in response
91 // to network activity (and hence will take longer than the written time).
93 // Explanation for these values:
95 // TODO(eroman): These values are somewhat arbitrary, and need to be tuned
96 // using some histograms data. Trying to be conservative so as not to break
97 // existing setups when deployed. A simple exponential retry scheme would be
98 // more elegant, but places more load on server.
100 // The motivation for trying quickly after failures (8 seconds) is to recover
101 // from spurious network failures, which are common after the IP address has
102 // just changed (like DNS failing to resolve). The next 32 second boundary is
103 // to try and catch other VPN weirdness which anecdotally I have seen take
104 // 10+ seconds for some users.
106 // The motivation for re-trying after a success is to check for possible
107 // content changes to the script, or to the WPAD auto-discovery results. We are
108 // not very aggressive with these checks so as to minimize the risk of
109 // overloading existing PAC setups. Moreover it is unlikely that PAC scripts
110 // change very frequently in existing setups. More research is needed to
111 // motivate what safe values are here, and what other user agents do.
113 // Comparison to other browsers:
115 // In Firefox the PAC URL is re-tried on failures according to
116 // network.proxy.autoconfig_retry_interval_min and
117 // network.proxy.autoconfig_retry_interval_max. The defaults are 5 seconds and
118 // 5 minutes respectively. It doubles the interval at each attempt.
120 // TODO(eroman): Figure out what Internet Explorer does.
121 class DefaultPollPolicy : public ProxyService::PacPollPolicy {
123 DefaultPollPolicy() {}
125 virtual Mode GetNextDelay(int initial_error,
126 TimeDelta current_delay,
127 TimeDelta* next_delay) const OVERRIDE {
128 if (initial_error != OK) {
129 // Re-try policy for failures.
130 const int kDelay1Seconds = 8;
131 const int kDelay2Seconds = 32;
132 const int kDelay3Seconds = 2 * 60; // 2 minutes
133 const int kDelay4Seconds = 4 * 60 * 60; // 4 Hours
136 if (current_delay < TimeDelta()) {
137 *next_delay = TimeDelta::FromSeconds(kDelay1Seconds);
138 return MODE_USE_TIMER;
140 switch (current_delay.InSeconds()) {
142 *next_delay = TimeDelta::FromSeconds(kDelay2Seconds);
143 return MODE_START_AFTER_ACTIVITY;
145 *next_delay = TimeDelta::FromSeconds(kDelay3Seconds);
146 return MODE_START_AFTER_ACTIVITY;
148 *next_delay = TimeDelta::FromSeconds(kDelay4Seconds);
149 return MODE_START_AFTER_ACTIVITY;
152 // Re-try policy for succeses.
153 *next_delay = TimeDelta::FromHours(12);
154 return MODE_START_AFTER_ACTIVITY;
159 DISALLOW_COPY_AND_ASSIGN(DefaultPollPolicy);
162 // Config getter that always returns direct settings.
163 class ProxyConfigServiceDirect : public ProxyConfigService {
165 // ProxyConfigService implementation:
166 virtual void AddObserver(Observer* observer) OVERRIDE {}
167 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
168 virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* config)
170 *config = ProxyConfig::CreateDirect();
171 config->set_source(PROXY_CONFIG_SOURCE_UNKNOWN);
176 // Proxy resolver that fails every time.
177 class ProxyResolverNull : public ProxyResolver {
179 ProxyResolverNull() : ProxyResolver(false /*expects_pac_bytes*/) {}
181 // ProxyResolver implementation.
182 virtual int GetProxyForURL(const GURL& url,
184 const CompletionCallback& callback,
185 RequestHandle* request,
186 const BoundNetLog& net_log) OVERRIDE {
187 return ERR_NOT_IMPLEMENTED;
190 virtual void CancelRequest(RequestHandle request) OVERRIDE {
194 virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
196 return LOAD_STATE_IDLE;
199 virtual void CancelSetPacScript() OVERRIDE {
203 virtual int SetPacScript(
204 const scoped_refptr<ProxyResolverScriptData>& /*script_data*/,
205 const CompletionCallback& /*callback*/) OVERRIDE {
206 return ERR_NOT_IMPLEMENTED;
210 // ProxyResolver that simulates a PAC script which returns
211 // |pac_string| for every single URL.
212 class ProxyResolverFromPacString : public ProxyResolver {
214 explicit ProxyResolverFromPacString(const std::string& pac_string)
215 : ProxyResolver(false /*expects_pac_bytes*/),
216 pac_string_(pac_string) {}
218 virtual int GetProxyForURL(const GURL& url,
220 const CompletionCallback& callback,
221 RequestHandle* request,
222 const BoundNetLog& net_log) OVERRIDE {
223 results->UsePacString(pac_string_);
227 virtual void CancelRequest(RequestHandle request) OVERRIDE {
231 virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
233 return LOAD_STATE_IDLE;
236 virtual void CancelSetPacScript() OVERRIDE {
240 virtual int SetPacScript(
241 const scoped_refptr<ProxyResolverScriptData>& pac_script,
242 const CompletionCallback& callback) OVERRIDE {
247 const std::string pac_string_;
250 // Creates ProxyResolvers using a platform-specific implementation.
251 class ProxyResolverFactoryForSystem : public ProxyResolverFactory {
253 ProxyResolverFactoryForSystem()
254 : ProxyResolverFactory(false /*expects_pac_bytes*/) {}
256 virtual ProxyResolver* CreateProxyResolver() OVERRIDE {
257 DCHECK(IsSupported());
259 return new ProxyResolverWinHttp();
260 #elif defined(OS_MACOSX)
261 return new ProxyResolverMac();
268 static bool IsSupported() {
269 #if defined(OS_WIN) || defined(OS_MACOSX)
277 // Returns NetLog parameters describing a proxy configuration change.
278 base::Value* NetLogProxyConfigChangedCallback(
279 const ProxyConfig* old_config,
280 const ProxyConfig* new_config,
281 NetLog::LogLevel /* log_level */) {
282 base::DictionaryValue* dict = new base::DictionaryValue();
283 // The "old_config" is optional -- the first notification will not have
284 // any "previous" configuration.
285 if (old_config->is_valid())
286 dict->Set("old_config", old_config->ToValue());
287 dict->Set("new_config", new_config->ToValue());
291 base::Value* NetLogBadProxyListCallback(const ProxyRetryInfoMap* retry_info,
292 NetLog::LogLevel /* log_level */) {
293 base::DictionaryValue* dict = new base::DictionaryValue();
294 base::ListValue* list = new base::ListValue();
296 for (ProxyRetryInfoMap::const_iterator iter = retry_info->begin();
297 iter != retry_info->end(); ++iter) {
298 list->Append(new base::StringValue(iter->first));
300 dict->Set("bad_proxy_list", list);
304 // Returns NetLog parameters on a successfuly proxy resolution.
305 base::Value* NetLogFinishedResolvingProxyCallback(
307 NetLog::LogLevel /* log_level */) {
308 base::DictionaryValue* dict = new base::DictionaryValue();
309 dict->SetString("pac_string", result->ToPacString());
313 #if defined(OS_CHROMEOS)
314 class UnsetProxyConfigService : public ProxyConfigService {
316 UnsetProxyConfigService() {}
317 virtual ~UnsetProxyConfigService() {}
319 virtual void AddObserver(Observer* observer) OVERRIDE {}
320 virtual void RemoveObserver(Observer* observer) OVERRIDE {}
321 virtual ConfigAvailability GetLatestProxyConfig(
322 ProxyConfig* config) OVERRIDE {
330 // ProxyService::InitProxyResolver --------------------------------------------
332 // This glues together two asynchronous steps:
333 // (1) ProxyScriptDecider -- try to fetch/validate a sequence of PAC scripts
334 // to figure out what we should configure against.
335 // (2) Feed the fetched PAC script into the ProxyResolver.
337 // InitProxyResolver is a single-use class which encapsulates cancellation as
338 // part of its destructor. Start() or StartSkipDecider() should be called just
339 // once. The instance can be destroyed at any time, and the request will be
342 class ProxyService::InitProxyResolver {
345 : proxy_resolver_(NULL),
346 next_state_(STATE_NONE) {
349 ~InitProxyResolver() {
350 // Note that the destruction of ProxyScriptDecider will automatically cancel
351 // any outstanding work.
352 if (next_state_ == STATE_SET_PAC_SCRIPT_COMPLETE) {
353 proxy_resolver_->CancelSetPacScript();
357 // Begins initializing the proxy resolver; calls |callback| when done.
358 int Start(ProxyResolver* proxy_resolver,
359 ProxyScriptFetcher* proxy_script_fetcher,
360 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher,
362 const ProxyConfig& config,
363 TimeDelta wait_delay,
364 const CompletionCallback& callback) {
365 DCHECK_EQ(STATE_NONE, next_state_);
366 proxy_resolver_ = proxy_resolver;
368 decider_.reset(new ProxyScriptDecider(
369 proxy_script_fetcher, dhcp_proxy_script_fetcher, net_log));
371 wait_delay_ = wait_delay;
372 callback_ = callback;
374 next_state_ = STATE_DECIDE_PROXY_SCRIPT;
378 // Similar to Start(), however it skips the ProxyScriptDecider stage. Instead
379 // |effective_config|, |decider_result| and |script_data| will be used as the
380 // inputs for initializing the ProxyResolver.
381 int StartSkipDecider(ProxyResolver* proxy_resolver,
382 const ProxyConfig& effective_config,
384 ProxyResolverScriptData* script_data,
385 const CompletionCallback& callback) {
386 DCHECK_EQ(STATE_NONE, next_state_);
387 proxy_resolver_ = proxy_resolver;
389 effective_config_ = effective_config;
390 script_data_ = script_data;
391 callback_ = callback;
393 if (decider_result != OK)
394 return decider_result;
396 next_state_ = STATE_SET_PAC_SCRIPT;
400 // Returns the proxy configuration that was selected by ProxyScriptDecider.
401 // Should only be called upon completion of the initialization.
402 const ProxyConfig& effective_config() const {
403 DCHECK_EQ(STATE_NONE, next_state_);
404 return effective_config_;
407 // Returns the PAC script data that was selected by ProxyScriptDecider.
408 // Should only be called upon completion of the initialization.
409 ProxyResolverScriptData* script_data() {
410 DCHECK_EQ(STATE_NONE, next_state_);
411 return script_data_.get();
414 LoadState GetLoadState() const {
415 if (next_state_ == STATE_DECIDE_PROXY_SCRIPT_COMPLETE) {
416 // In addition to downloading, this state may also include the stall time
417 // after network change events (kDelayAfterNetworkChangesMs).
418 return LOAD_STATE_DOWNLOADING_PROXY_SCRIPT;
420 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
426 STATE_DECIDE_PROXY_SCRIPT,
427 STATE_DECIDE_PROXY_SCRIPT_COMPLETE,
428 STATE_SET_PAC_SCRIPT,
429 STATE_SET_PAC_SCRIPT_COMPLETE,
432 int DoLoop(int result) {
433 DCHECK_NE(next_state_, STATE_NONE);
436 State state = next_state_;
437 next_state_ = STATE_NONE;
439 case STATE_DECIDE_PROXY_SCRIPT:
441 rv = DoDecideProxyScript();
443 case STATE_DECIDE_PROXY_SCRIPT_COMPLETE:
444 rv = DoDecideProxyScriptComplete(rv);
446 case STATE_SET_PAC_SCRIPT:
448 rv = DoSetPacScript();
450 case STATE_SET_PAC_SCRIPT_COMPLETE:
451 rv = DoSetPacScriptComplete(rv);
454 NOTREACHED() << "bad state: " << state;
458 } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
462 int DoDecideProxyScript() {
463 next_state_ = STATE_DECIDE_PROXY_SCRIPT_COMPLETE;
465 return decider_->Start(
466 config_, wait_delay_, proxy_resolver_->expects_pac_bytes(),
467 base::Bind(&InitProxyResolver::OnIOCompletion, base::Unretained(this)));
470 int DoDecideProxyScriptComplete(int result) {
474 effective_config_ = decider_->effective_config();
475 script_data_ = decider_->script_data();
477 next_state_ = STATE_SET_PAC_SCRIPT;
481 int DoSetPacScript() {
482 DCHECK(script_data_.get());
483 // TODO(eroman): Should log this latency to the NetLog.
484 next_state_ = STATE_SET_PAC_SCRIPT_COMPLETE;
485 return proxy_resolver_->SetPacScript(
487 base::Bind(&InitProxyResolver::OnIOCompletion, base::Unretained(this)));
490 int DoSetPacScriptComplete(int result) {
494 void OnIOCompletion(int result) {
495 DCHECK_NE(STATE_NONE, next_state_);
496 int rv = DoLoop(result);
497 if (rv != ERR_IO_PENDING)
501 void DoCallback(int result) {
502 DCHECK_NE(ERR_IO_PENDING, result);
503 callback_.Run(result);
507 ProxyConfig effective_config_;
508 scoped_refptr<ProxyResolverScriptData> script_data_;
509 TimeDelta wait_delay_;
510 scoped_ptr<ProxyScriptDecider> decider_;
511 ProxyResolver* proxy_resolver_;
512 CompletionCallback callback_;
515 DISALLOW_COPY_AND_ASSIGN(InitProxyResolver);
518 // ProxyService::ProxyScriptDeciderPoller -------------------------------------
520 // This helper class encapsulates the logic to schedule and run periodic
521 // background checks to see if the PAC script (or effective proxy configuration)
522 // has changed. If a change is detected, then the caller will be notified via
523 // the ChangeCallback.
524 class ProxyService::ProxyScriptDeciderPoller {
526 typedef base::Callback<void(int, ProxyResolverScriptData*,
527 const ProxyConfig&)> ChangeCallback;
529 // Builds a poller helper, and starts polling for updates. Whenever a change
530 // is observed, |callback| will be invoked with the details.
532 // |config| specifies the (unresolved) proxy configuration to poll.
533 // |proxy_resolver_expects_pac_bytes| the type of proxy resolver we expect
534 // to use the resulting script data with
535 // (so it can choose the right format).
536 // |proxy_script_fetcher| this pointer must remain alive throughout our
537 // lifetime. It is the dependency that will be used
538 // for downloading proxy scripts.
539 // |dhcp_proxy_script_fetcher| similar to |proxy_script_fetcher|, but for
540 // the DHCP dependency.
541 // |init_net_error| This is the initial network error (possibly success)
542 // encountered by the first PAC fetch attempt. We use it
543 // to schedule updates more aggressively if the initial
544 // fetch resulted in an error.
545 // |init_script_data| the initial script data from the PAC fetch attempt.
546 // This is the baseline used to determine when the
547 // script's contents have changed.
548 // |net_log| the NetLog to log progress into.
549 ProxyScriptDeciderPoller(ChangeCallback callback,
550 const ProxyConfig& config,
551 bool proxy_resolver_expects_pac_bytes,
552 ProxyScriptFetcher* proxy_script_fetcher,
553 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher,
555 ProxyResolverScriptData* init_script_data,
557 : weak_factory_(this),
558 change_callback_(callback),
560 proxy_resolver_expects_pac_bytes_(proxy_resolver_expects_pac_bytes),
561 proxy_script_fetcher_(proxy_script_fetcher),
562 dhcp_proxy_script_fetcher_(dhcp_proxy_script_fetcher),
563 last_error_(init_net_error),
564 last_script_data_(init_script_data),
565 last_poll_time_(TimeTicks::Now()) {
566 // Set the initial poll delay.
567 next_poll_mode_ = poll_policy()->GetNextDelay(
568 last_error_, TimeDelta::FromSeconds(-1), &next_poll_delay_);
569 TryToStartNextPoll(false);
573 // We have just been notified of network activity. Use this opportunity to
574 // see if we can start our next poll.
575 TryToStartNextPoll(true);
578 static const PacPollPolicy* set_policy(const PacPollPolicy* policy) {
579 const PacPollPolicy* prev = poll_policy_;
580 poll_policy_ = policy;
585 // Returns the effective poll policy (the one injected by unit-tests, or the
587 const PacPollPolicy* poll_policy() {
590 return &default_poll_policy_;
593 void StartPollTimer() {
594 DCHECK(!decider_.get());
596 base::MessageLoop::current()->PostDelayedTask(
598 base::Bind(&ProxyScriptDeciderPoller::DoPoll,
599 weak_factory_.GetWeakPtr()),
603 void TryToStartNextPoll(bool triggered_by_activity) {
604 switch (next_poll_mode_) {
605 case PacPollPolicy::MODE_USE_TIMER:
606 if (!triggered_by_activity)
610 case PacPollPolicy::MODE_START_AFTER_ACTIVITY:
611 if (triggered_by_activity && !decider_.get()) {
612 TimeDelta elapsed_time = TimeTicks::Now() - last_poll_time_;
613 if (elapsed_time >= next_poll_delay_)
621 last_poll_time_ = TimeTicks::Now();
623 // Start the proxy script decider to see if anything has changed.
624 // TODO(eroman): Pass a proper NetLog rather than NULL.
625 decider_.reset(new ProxyScriptDecider(
626 proxy_script_fetcher_, dhcp_proxy_script_fetcher_, NULL));
627 int result = decider_->Start(
628 config_, TimeDelta(), proxy_resolver_expects_pac_bytes_,
629 base::Bind(&ProxyScriptDeciderPoller::OnProxyScriptDeciderCompleted,
630 base::Unretained(this)));
632 if (result != ERR_IO_PENDING)
633 OnProxyScriptDeciderCompleted(result);
636 void OnProxyScriptDeciderCompleted(int result) {
637 if (HasScriptDataChanged(result, decider_->script_data())) {
638 // Something has changed, we must notify the ProxyService so it can
639 // re-initialize its ProxyResolver. Note that we post a notification task
640 // rather than calling it directly -- this is done to avoid an ugly
641 // destruction sequence, since |this| might be destroyed as a result of
643 base::MessageLoop::current()->PostTask(
645 base::Bind(&ProxyScriptDeciderPoller::NotifyProxyServiceOfChange,
646 weak_factory_.GetWeakPtr(),
648 make_scoped_refptr(decider_->script_data()),
649 decider_->effective_config()));
655 // Decide when the next poll should take place, and possibly start the
657 next_poll_mode_ = poll_policy()->GetNextDelay(
658 last_error_, next_poll_delay_, &next_poll_delay_);
659 TryToStartNextPoll(false);
662 bool HasScriptDataChanged(int result, ProxyResolverScriptData* script_data) {
663 if (result != last_error_) {
664 // Something changed -- it was failing before and now it succeeded, or
665 // conversely it succeeded before and now it failed. Or it failed in
666 // both cases, however the specific failure error codes differ.
671 // If it failed last time and failed again with the same error code this
672 // time, then nothing has actually changed.
676 // Otherwise if it succeeded both this time and last time, we need to look
677 // closer and see if we ended up downloading different content for the PAC
679 return !script_data->Equals(last_script_data_.get());
682 void NotifyProxyServiceOfChange(
684 const scoped_refptr<ProxyResolverScriptData>& script_data,
685 const ProxyConfig& effective_config) {
686 // Note that |this| may be deleted after calling into the ProxyService.
687 change_callback_.Run(result, script_data.get(), effective_config);
690 base::WeakPtrFactory<ProxyScriptDeciderPoller> weak_factory_;
692 ChangeCallback change_callback_;
694 bool proxy_resolver_expects_pac_bytes_;
695 ProxyScriptFetcher* proxy_script_fetcher_;
696 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher_;
699 scoped_refptr<ProxyResolverScriptData> last_script_data_;
701 scoped_ptr<ProxyScriptDecider> decider_;
702 TimeDelta next_poll_delay_;
703 PacPollPolicy::Mode next_poll_mode_;
705 TimeTicks last_poll_time_;
707 // Polling policy injected by unit-tests. Otherwise this is NULL and the
708 // default policy will be used.
709 static const PacPollPolicy* poll_policy_;
711 const DefaultPollPolicy default_poll_policy_;
713 DISALLOW_COPY_AND_ASSIGN(ProxyScriptDeciderPoller);
717 const ProxyService::PacPollPolicy*
718 ProxyService::ProxyScriptDeciderPoller::poll_policy_ = NULL;
720 // ProxyService::PacRequest ---------------------------------------------------
722 class ProxyService::PacRequest
723 : public base::RefCounted<ProxyService::PacRequest> {
725 PacRequest(ProxyService* service,
728 const net::CompletionCallback& user_callback,
729 const BoundNetLog& net_log)
731 user_callback_(user_callback),
735 config_id_(ProxyConfig::kInvalidConfigID),
736 config_source_(PROXY_CONFIG_SOURCE_UNKNOWN),
738 DCHECK(!user_callback.is_null());
741 // Starts the resolve proxy request.
743 DCHECK(!was_cancelled());
744 DCHECK(!is_started());
746 DCHECK(service_->config_.is_valid());
748 config_id_ = service_->config_.id();
749 config_source_ = service_->config_.source();
750 proxy_resolve_start_time_ = TimeTicks::Now();
752 return resolver()->GetProxyForURL(
754 base::Bind(&PacRequest::QueryComplete, base::Unretained(this)),
755 &resolve_job_, net_log_);
758 bool is_started() const {
759 // Note that !! casts to bool. (VS gives a warning otherwise).
760 return !!resolve_job_;
763 void StartAndCompleteCheckingForSynchronous() {
764 int rv = service_->TryToCompleteSynchronously(url_, results_);
765 if (rv == ERR_IO_PENDING)
767 if (rv != ERR_IO_PENDING)
771 void CancelResolveJob() {
772 DCHECK(is_started());
773 // The request may already be running in the resolver.
774 resolver()->CancelRequest(resolve_job_);
776 DCHECK(!is_started());
780 net_log_.AddEvent(NetLog::TYPE_CANCELLED);
785 // Mark as cancelled, to prevent accessing this again later.
787 user_callback_.Reset();
790 net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE);
793 // Returns true if Cancel() has been called.
794 bool was_cancelled() const {
795 return user_callback_.is_null();
798 // Helper to call after ProxyResolver completion (both synchronous and
799 // asynchronous). Fixes up the result that is to be returned to user.
800 int QueryDidComplete(int result_code) {
801 DCHECK(!was_cancelled());
803 // Note that DidFinishResolvingProxy might modify |results_|.
804 int rv = service_->DidFinishResolvingProxy(results_, result_code, net_log_);
806 // Make a note in the results which configuration was in use at the
807 // time of the resolve.
808 results_->config_id_ = config_id_;
809 results_->config_source_ = config_source_;
810 results_->did_use_pac_script_ = true;
811 results_->proxy_resolve_start_time_ = proxy_resolve_start_time_;
812 results_->proxy_resolve_end_time_ = TimeTicks::Now();
814 // Reset the state associated with in-progress-resolve.
816 config_id_ = ProxyConfig::kInvalidConfigID;
817 config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN;
822 BoundNetLog* net_log() { return &net_log_; }
824 LoadState GetLoadState() const {
826 return resolver()->GetLoadState(resolve_job_);
827 return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
831 friend class base::RefCounted<ProxyService::PacRequest>;
835 // Callback for when the ProxyResolver request has completed.
836 void QueryComplete(int result_code) {
837 result_code = QueryDidComplete(result_code);
839 // Remove this completed PacRequest from the service's pending list.
840 /// (which will probably cause deletion of |this|).
841 if (!user_callback_.is_null()) {
842 net::CompletionCallback callback = user_callback_;
843 service_->RemovePendingRequest(this);
844 callback.Run(result_code);
848 ProxyResolver* resolver() const { return service_->resolver_.get(); }
850 // Note that we don't hold a reference to the ProxyService. Outstanding
851 // requests are cancelled during ~ProxyService, so this is guaranteed
852 // to be valid throughout our lifetime.
853 ProxyService* service_;
854 net::CompletionCallback user_callback_;
857 ProxyResolver::RequestHandle resolve_job_;
858 ProxyConfig::ID config_id_; // The config id when the resolve was started.
859 ProxyConfigSource config_source_; // The source of proxy settings.
860 BoundNetLog net_log_;
861 // Time when the PAC is started. Cached here since resetting ProxyInfo also
862 // clears the proxy times.
863 TimeTicks proxy_resolve_start_time_;
866 // ProxyService ---------------------------------------------------------------
868 ProxyService::ProxyService(ProxyConfigService* config_service,
869 ProxyResolver* resolver,
871 : resolver_(resolver),
873 current_state_(STATE_NONE) ,
875 stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds(
876 kDelayAfterNetworkChangesMs)) {
877 NetworkChangeNotifier::AddIPAddressObserver(this);
878 NetworkChangeNotifier::AddDNSObserver(this);
879 ResetConfigService(config_service);
883 ProxyService* ProxyService::CreateUsingSystemProxyResolver(
884 ProxyConfigService* proxy_config_service,
885 size_t num_pac_threads,
887 DCHECK(proxy_config_service);
889 if (!ProxyResolverFactoryForSystem::IsSupported()) {
890 LOG(WARNING) << "PAC support disabled because there is no "
891 "system implementation";
892 return CreateWithoutProxyResolver(proxy_config_service, net_log);
895 if (num_pac_threads == 0)
896 num_pac_threads = kDefaultNumPacThreads;
898 ProxyResolver* proxy_resolver = new MultiThreadedProxyResolver(
899 new ProxyResolverFactoryForSystem(), num_pac_threads);
901 return new ProxyService(proxy_config_service, proxy_resolver, net_log);
905 ProxyService* ProxyService::CreateWithoutProxyResolver(
906 ProxyConfigService* proxy_config_service,
908 return new ProxyService(proxy_config_service,
909 new ProxyResolverNull(),
914 ProxyService* ProxyService::CreateFixed(const ProxyConfig& pc) {
915 // TODO(eroman): This isn't quite right, won't work if |pc| specifies
917 return CreateUsingSystemProxyResolver(new ProxyConfigServiceFixed(pc),
922 ProxyService* ProxyService::CreateFixed(const std::string& proxy) {
923 net::ProxyConfig proxy_config;
924 proxy_config.proxy_rules().ParseFromString(proxy);
925 return ProxyService::CreateFixed(proxy_config);
929 ProxyService* ProxyService::CreateDirect() {
930 return CreateDirectWithNetLog(NULL);
933 ProxyService* ProxyService::CreateDirectWithNetLog(NetLog* net_log) {
934 // Use direct connections.
935 return new ProxyService(new ProxyConfigServiceDirect, new ProxyResolverNull,
940 ProxyService* ProxyService::CreateFixedFromPacResult(
941 const std::string& pac_string) {
943 // We need the settings to contain an "automatic" setting, otherwise the
944 // ProxyResolver dependency we give it will never be used.
945 scoped_ptr<ProxyConfigService> proxy_config_service(
946 new ProxyConfigServiceFixed(ProxyConfig::CreateAutoDetect()));
948 scoped_ptr<ProxyResolver> proxy_resolver(
949 new ProxyResolverFromPacString(pac_string));
951 return new ProxyService(proxy_config_service.release(),
952 proxy_resolver.release(),
956 int ProxyService::ResolveProxy(const GURL& raw_url,
958 const net::CompletionCallback& callback,
959 PacRequest** pac_request,
960 const BoundNetLog& net_log) {
961 DCHECK(CalledOnValidThread());
962 DCHECK(!callback.is_null());
964 net_log.BeginEvent(NetLog::TYPE_PROXY_SERVICE);
966 // Notify our polling-based dependencies that a resolve is taking place.
967 // This way they can schedule their polls in response to network activity.
968 config_service_->OnLazyPoll();
969 if (script_poller_.get())
970 script_poller_->OnLazyPoll();
972 if (current_state_ == STATE_NONE)
973 ApplyProxyConfigIfAvailable();
975 // Strip away any reference fragments and the username/password, as they
976 // are not relevant to proxy resolution.
977 GURL url = SimplifyUrlForRequest(raw_url);
979 // Check if the request can be completed right away. (This is the case when
980 // using a direct connection for example).
981 int rv = TryToCompleteSynchronously(url, result);
982 if (rv != ERR_IO_PENDING)
983 return DidFinishResolvingProxy(result, rv, net_log);
985 scoped_refptr<PacRequest> req(
986 new PacRequest(this, url, result, callback, net_log));
988 if (current_state_ == STATE_READY) {
989 // Start the resolve request.
991 if (rv != ERR_IO_PENDING)
992 return req->QueryDidComplete(rv);
994 req->net_log()->BeginEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
997 DCHECK_EQ(ERR_IO_PENDING, rv);
998 DCHECK(!ContainsPendingRequest(req.get()));
999 pending_requests_.push_back(req);
1001 // Completion will be notified through |callback|, unless the caller cancels
1002 // the request using |pac_request|.
1004 *pac_request = req.get();
1005 return rv; // ERR_IO_PENDING
1008 int ProxyService::TryToCompleteSynchronously(const GURL& url,
1009 ProxyInfo* result) {
1010 DCHECK_NE(STATE_NONE, current_state_);
1012 if (current_state_ != STATE_READY)
1013 return ERR_IO_PENDING; // Still initializing.
1015 DCHECK_NE(config_.id(), ProxyConfig::kInvalidConfigID);
1017 // If it was impossible to fetch or parse the PAC script, we cannot complete
1018 // the request here and bail out.
1019 if (permanent_error_ != OK)
1020 return permanent_error_;
1022 if (config_.HasAutomaticSettings())
1023 return ERR_IO_PENDING; // Must submit the request to the proxy resolver.
1025 // Use the manual proxy settings.
1026 config_.proxy_rules().Apply(url, result);
1027 result->config_source_ = config_.source();
1028 result->config_id_ = config_.id();
1032 ProxyService::~ProxyService() {
1033 NetworkChangeNotifier::RemoveIPAddressObserver(this);
1034 NetworkChangeNotifier::RemoveDNSObserver(this);
1035 config_service_->RemoveObserver(this);
1037 // Cancel any inprogress requests.
1038 for (PendingRequests::iterator it = pending_requests_.begin();
1039 it != pending_requests_.end();
1045 void ProxyService::SuspendAllPendingRequests() {
1046 for (PendingRequests::iterator it = pending_requests_.begin();
1047 it != pending_requests_.end();
1049 PacRequest* req = it->get();
1050 if (req->is_started()) {
1051 req->CancelResolveJob();
1053 req->net_log()->BeginEvent(
1054 NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1059 void ProxyService::SetReady() {
1060 DCHECK(!init_proxy_resolver_.get());
1061 current_state_ = STATE_READY;
1063 // Make a copy in case |this| is deleted during the synchronous completion
1064 // of one of the requests. If |this| is deleted then all of the PacRequest
1065 // instances will be Cancel()-ed.
1066 PendingRequests pending_copy = pending_requests_;
1068 for (PendingRequests::iterator it = pending_copy.begin();
1069 it != pending_copy.end();
1071 PacRequest* req = it->get();
1072 if (!req->is_started() && !req->was_cancelled()) {
1073 req->net_log()->EndEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1075 // Note that we re-check for synchronous completion, in case we are
1076 // no longer using a ProxyResolver (can happen if we fell-back to manual).
1077 req->StartAndCompleteCheckingForSynchronous();
1082 void ProxyService::ApplyProxyConfigIfAvailable() {
1083 DCHECK_EQ(STATE_NONE, current_state_);
1085 config_service_->OnLazyPoll();
1087 // If we have already fetched the configuration, start applying it.
1088 if (fetched_config_.is_valid()) {
1089 InitializeUsingLastFetchedConfig();
1093 // Otherwise we need to first fetch the configuration.
1094 current_state_ = STATE_WAITING_FOR_PROXY_CONFIG;
1096 // Retrieve the current proxy configuration from the ProxyConfigService.
1097 // If a configuration is not available yet, we will get called back later
1098 // by our ProxyConfigService::Observer once it changes.
1100 ProxyConfigService::ConfigAvailability availability =
1101 config_service_->GetLatestProxyConfig(&config);
1102 if (availability != ProxyConfigService::CONFIG_PENDING)
1103 OnProxyConfigChanged(config, availability);
1106 void ProxyService::OnInitProxyResolverComplete(int result) {
1107 DCHECK_EQ(STATE_WAITING_FOR_INIT_PROXY_RESOLVER, current_state_);
1108 DCHECK(init_proxy_resolver_.get());
1109 DCHECK(fetched_config_.HasAutomaticSettings());
1110 config_ = init_proxy_resolver_->effective_config();
1112 // At this point we have decided which proxy settings to use (i.e. which PAC
1113 // script if any). We start up a background poller to periodically revisit
1114 // this decision. If the contents of the PAC script change, or if the
1115 // result of proxy auto-discovery changes, this poller will notice it and
1116 // will trigger a re-initialization using the newly discovered PAC.
1117 script_poller_.reset(new ProxyScriptDeciderPoller(
1118 base::Bind(&ProxyService::InitializeUsingDecidedConfig,
1119 base::Unretained(this)),
1121 resolver_->expects_pac_bytes(),
1122 proxy_script_fetcher_.get(),
1123 dhcp_proxy_script_fetcher_.get(),
1125 init_proxy_resolver_->script_data(),
1128 init_proxy_resolver_.reset();
1131 if (fetched_config_.pac_mandatory()) {
1132 VLOG(1) << "Failed configuring with mandatory PAC script, blocking all "
1134 config_ = fetched_config_;
1135 result = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1137 VLOG(1) << "Failed configuring with PAC script, falling-back to manual "
1139 config_ = fetched_config_;
1140 config_.ClearAutomaticSettings();
1144 permanent_error_ = result;
1146 // TODO(eroman): Make this ID unique in the case where configuration changed
1147 // due to ProxyScriptDeciderPoller.
1148 config_.set_id(fetched_config_.id());
1149 config_.set_source(fetched_config_.source());
1151 // Resume any requests which we had to defer until the PAC script was
1156 int ProxyService::ReconsiderProxyAfterError(const GURL& url,
1158 const CompletionCallback& callback,
1159 PacRequest** pac_request,
1160 const BoundNetLog& net_log) {
1161 DCHECK(CalledOnValidThread());
1163 // Check to see if we have a new config since ResolveProxy was called. We
1164 // want to re-run ResolveProxy in two cases: 1) we have a new config, or 2) a
1165 // direct connection failed and we never tried the current config.
1167 bool re_resolve = result->config_id_ != config_.id();
1170 // If we have a new config or the config was never tried, we delete the
1171 // list of bad proxies and we try again.
1172 proxy_retry_info_.clear();
1173 return ResolveProxy(url, result, callback, pac_request, net_log);
1176 #if defined(SPDY_PROXY_AUTH_ORIGIN)
1177 if (result->proxy_server().isDataReductionProxy()) {
1178 RecordDataReductionProxyBypassInfo(
1179 true, result->proxy_server(), ERROR_BYPASS);
1180 } else if (result->proxy_server().isDataReductionProxyFallback()) {
1181 RecordDataReductionProxyBypassInfo(
1182 false, result->proxy_server(), ERROR_BYPASS);
1186 // We don't have new proxy settings to try, try to fallback to the next proxy
1188 bool did_fallback = result->Fallback(net_log);
1190 // Return synchronous failure if there is nothing left to fall-back to.
1191 // TODO(eroman): This is a yucky API, clean it up.
1192 return did_fallback ? OK : ERR_FAILED;
1195 bool ProxyService::MarkProxyAsBad(const ProxyInfo& result,
1196 base::TimeDelta retry_delay,
1197 const BoundNetLog& net_log) {
1198 result.proxy_list_.UpdateRetryInfoOnFallback(&proxy_retry_info_, retry_delay,
1200 return result.proxy_list_.HasUntriedProxies(proxy_retry_info_);
1203 void ProxyService::ReportSuccess(const ProxyInfo& result) {
1204 DCHECK(CalledOnValidThread());
1206 const ProxyRetryInfoMap& new_retry_info = result.proxy_retry_info();
1207 if (new_retry_info.empty())
1210 for (ProxyRetryInfoMap::const_iterator iter = new_retry_info.begin();
1211 iter != new_retry_info.end(); ++iter) {
1212 ProxyRetryInfoMap::iterator existing = proxy_retry_info_.find(iter->first);
1213 if (existing == proxy_retry_info_.end())
1214 proxy_retry_info_[iter->first] = iter->second;
1215 else if (existing->second.bad_until < iter->second.bad_until)
1216 existing->second.bad_until = iter->second.bad_until;
1219 net_log_->AddGlobalEntry(
1220 NetLog::TYPE_BAD_PROXY_LIST_REPORTED,
1221 base::Bind(&NetLogBadProxyListCallback, &new_retry_info));
1225 void ProxyService::CancelPacRequest(PacRequest* req) {
1226 DCHECK(CalledOnValidThread());
1229 RemovePendingRequest(req);
1232 LoadState ProxyService::GetLoadState(const PacRequest* req) const {
1234 if (current_state_ == STATE_WAITING_FOR_INIT_PROXY_RESOLVER)
1235 return init_proxy_resolver_->GetLoadState();
1236 return req->GetLoadState();
1239 bool ProxyService::ContainsPendingRequest(PacRequest* req) {
1240 PendingRequests::iterator it = std::find(
1241 pending_requests_.begin(), pending_requests_.end(), req);
1242 return pending_requests_.end() != it;
1245 void ProxyService::RemovePendingRequest(PacRequest* req) {
1246 DCHECK(ContainsPendingRequest(req));
1247 PendingRequests::iterator it = std::find(
1248 pending_requests_.begin(), pending_requests_.end(), req);
1249 pending_requests_.erase(it);
1252 int ProxyService::DidFinishResolvingProxy(ProxyInfo* result,
1254 const BoundNetLog& net_log) {
1255 // Log the result of the proxy resolution.
1256 if (result_code == OK) {
1257 // When logging all events is enabled, dump the proxy list.
1258 if (net_log.IsLoggingAllEvents()) {
1260 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST,
1261 base::Bind(&NetLogFinishedResolvingProxyCallback, result));
1263 result->DeprioritizeBadProxies(proxy_retry_info_);
1265 net_log.AddEventWithNetErrorCode(
1266 NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, result_code);
1268 if (!config_.pac_mandatory()) {
1269 // Fall-back to direct when the proxy resolver fails. This corresponds
1270 // with a javascript runtime error in the PAC script.
1272 // This implicit fall-back to direct matches Firefox 3.5 and
1273 // Internet Explorer 8. For more information, see:
1275 // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
1276 result->UseDirect();
1279 result_code = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1283 net_log.EndEvent(NetLog::TYPE_PROXY_SERVICE);
1287 void ProxyService::SetProxyScriptFetchers(
1288 ProxyScriptFetcher* proxy_script_fetcher,
1289 DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher) {
1290 DCHECK(CalledOnValidThread());
1291 State previous_state = ResetProxyConfig(false);
1292 proxy_script_fetcher_.reset(proxy_script_fetcher);
1293 dhcp_proxy_script_fetcher_.reset(dhcp_proxy_script_fetcher);
1294 if (previous_state != STATE_NONE)
1295 ApplyProxyConfigIfAvailable();
1298 ProxyScriptFetcher* ProxyService::GetProxyScriptFetcher() const {
1299 DCHECK(CalledOnValidThread());
1300 return proxy_script_fetcher_.get();
1303 ProxyService::State ProxyService::ResetProxyConfig(bool reset_fetched_config) {
1304 DCHECK(CalledOnValidThread());
1305 State previous_state = current_state_;
1307 permanent_error_ = OK;
1308 proxy_retry_info_.clear();
1309 script_poller_.reset();
1310 init_proxy_resolver_.reset();
1311 SuspendAllPendingRequests();
1312 config_ = ProxyConfig();
1313 if (reset_fetched_config)
1314 fetched_config_ = ProxyConfig();
1315 current_state_ = STATE_NONE;
1317 return previous_state;
1320 void ProxyService::ResetConfigService(
1321 ProxyConfigService* new_proxy_config_service) {
1322 DCHECK(CalledOnValidThread());
1323 State previous_state = ResetProxyConfig(true);
1325 // Release the old configuration service.
1326 if (config_service_.get())
1327 config_service_->RemoveObserver(this);
1329 // Set the new configuration service.
1330 config_service_.reset(new_proxy_config_service);
1331 config_service_->AddObserver(this);
1333 if (previous_state != STATE_NONE)
1334 ApplyProxyConfigIfAvailable();
1337 void ProxyService::PurgeMemory() {
1338 DCHECK(CalledOnValidThread());
1339 if (resolver_.get())
1340 resolver_->PurgeMemory();
1343 void ProxyService::ForceReloadProxyConfig() {
1344 DCHECK(CalledOnValidThread());
1345 ResetProxyConfig(false);
1346 ApplyProxyConfigIfAvailable();
1350 ProxyConfigService* ProxyService::CreateSystemProxyConfigService(
1351 base::SingleThreadTaskRunner* io_thread_task_runner,
1352 base::MessageLoop* file_loop) {
1354 return new ProxyConfigServiceWin();
1355 #elif defined(OS_IOS)
1356 return new ProxyConfigServiceIOS();
1357 #elif defined(OS_MACOSX)
1358 return new ProxyConfigServiceMac(io_thread_task_runner);
1359 #elif defined(OS_CHROMEOS)
1360 LOG(ERROR) << "ProxyConfigService for ChromeOS should be created in "
1361 << "profile_io_data.cc::CreateProxyConfigService and this should "
1362 << "be used only for examples.";
1363 return new UnsetProxyConfigService;
1364 #elif defined(OS_LINUX)
1365 ProxyConfigServiceLinux* linux_config_service =
1366 new ProxyConfigServiceLinux();
1368 // Assume we got called on the thread that runs the default glib
1369 // main loop, so the current thread is where we should be running
1370 // gconf calls from.
1371 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner =
1372 base::ThreadTaskRunnerHandle::Get();
1374 // The file loop should be a MessageLoopForIO on Linux.
1375 DCHECK_EQ(base::MessageLoop::TYPE_IO, file_loop->type());
1377 // Synchronously fetch the current proxy config (since we are
1378 // running on glib_default_loop). Additionally register for
1379 // notifications (delivered in either |glib_default_loop| or
1380 // |file_loop|) to keep us updated when the proxy config changes.
1381 linux_config_service->SetupAndFetchInitialConfig(
1382 glib_thread_task_runner.get(),
1383 io_thread_task_runner,
1384 static_cast<base::MessageLoopForIO*>(file_loop));
1386 return linux_config_service;
1387 #elif defined(OS_ANDROID)
1388 return new ProxyConfigServiceAndroid(
1389 io_thread_task_runner,
1390 base::MessageLoop::current()->message_loop_proxy());
1392 LOG(WARNING) << "Failed to choose a system proxy settings fetcher "
1393 "for this platform.";
1394 return new ProxyConfigServiceDirect();
1399 const ProxyService::PacPollPolicy* ProxyService::set_pac_script_poll_policy(
1400 const PacPollPolicy* policy) {
1401 return ProxyScriptDeciderPoller::set_policy(policy);
1405 scoped_ptr<ProxyService::PacPollPolicy>
1406 ProxyService::CreateDefaultPacPollPolicy() {
1407 return scoped_ptr<PacPollPolicy>(new DefaultPollPolicy());
1410 #if defined(SPDY_PROXY_AUTH_ORIGIN)
1411 void ProxyService::RecordDataReductionProxyBypassInfo(
1413 const ProxyServer& proxy_server,
1414 DataReductionProxyBypassEventType bypass_type) const {
1415 // Only record UMA if the proxy isn't already on the retry list.
1416 if (proxy_retry_info_.find(proxy_server.ToURI()) != proxy_retry_info_.end())
1420 UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoPrimary",
1421 bypass_type, BYPASS_EVENT_TYPE_MAX);
1423 UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoFallback",
1424 bypass_type, BYPASS_EVENT_TYPE_MAX);
1427 #endif // defined(SPDY_PROXY_AUTH_ORIGIN)
1429 void ProxyService::OnProxyConfigChanged(
1430 const ProxyConfig& config,
1431 ProxyConfigService::ConfigAvailability availability) {
1432 // Retrieve the current proxy configuration from the ProxyConfigService.
1433 // If a configuration is not available yet, we will get called back later
1434 // by our ProxyConfigService::Observer once it changes.
1435 ProxyConfig effective_config;
1436 switch (availability) {
1437 case ProxyConfigService::CONFIG_PENDING:
1438 // ProxyConfigService implementors should never pass CONFIG_PENDING.
1439 NOTREACHED() << "Proxy config change with CONFIG_PENDING availability!";
1441 case ProxyConfigService::CONFIG_VALID:
1442 effective_config = config;
1444 case ProxyConfigService::CONFIG_UNSET:
1445 effective_config = ProxyConfig::CreateDirect();
1449 // Emit the proxy settings change to the NetLog stream.
1451 net_log_->AddGlobalEntry(
1452 net::NetLog::TYPE_PROXY_CONFIG_CHANGED,
1453 base::Bind(&NetLogProxyConfigChangedCallback,
1454 &fetched_config_, &effective_config));
1457 // Set the new configuration as the most recently fetched one.
1458 fetched_config_ = effective_config;
1459 fetched_config_.set_id(1); // Needed for a later DCHECK of is_valid().
1461 InitializeUsingLastFetchedConfig();
1464 void ProxyService::InitializeUsingLastFetchedConfig() {
1465 ResetProxyConfig(false);
1467 DCHECK(fetched_config_.is_valid());
1469 // Increment the ID to reflect that the config has changed.
1470 fetched_config_.set_id(next_config_id_++);
1472 if (!fetched_config_.HasAutomaticSettings()) {
1473 config_ = fetched_config_;
1478 // Start downloading + testing the PAC scripts for this new configuration.
1479 current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1481 // If we changed networks recently, we should delay running proxy auto-config.
1482 TimeDelta wait_delay =
1483 stall_proxy_autoconfig_until_ - TimeTicks::Now();
1485 init_proxy_resolver_.reset(new InitProxyResolver());
1486 int rv = init_proxy_resolver_->Start(
1488 proxy_script_fetcher_.get(),
1489 dhcp_proxy_script_fetcher_.get(),
1493 base::Bind(&ProxyService::OnInitProxyResolverComplete,
1494 base::Unretained(this)));
1496 if (rv != ERR_IO_PENDING)
1497 OnInitProxyResolverComplete(rv);
1500 void ProxyService::InitializeUsingDecidedConfig(
1502 ProxyResolverScriptData* script_data,
1503 const ProxyConfig& effective_config) {
1504 DCHECK(fetched_config_.is_valid());
1505 DCHECK(fetched_config_.HasAutomaticSettings());
1507 ResetProxyConfig(false);
1509 current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1511 init_proxy_resolver_.reset(new InitProxyResolver());
1512 int rv = init_proxy_resolver_->StartSkipDecider(
1517 base::Bind(&ProxyService::OnInitProxyResolverComplete,
1518 base::Unretained(this)));
1520 if (rv != ERR_IO_PENDING)
1521 OnInitProxyResolverComplete(rv);
1524 void ProxyService::OnIPAddressChanged() {
1525 // See the comment block by |kDelayAfterNetworkChangesMs| for info.
1526 stall_proxy_autoconfig_until_ =
1527 TimeTicks::Now() + stall_proxy_auto_config_delay_;
1529 State previous_state = ResetProxyConfig(false);
1530 if (previous_state != STATE_NONE)
1531 ApplyProxyConfigIfAvailable();
1534 void ProxyService::OnDNSChanged() {
1535 OnIPAddressChanged();
1538 SyncProxyServiceHelper::SyncProxyServiceHelper(
1539 base::MessageLoop* io_message_loop,
1540 ProxyService* proxy_service)
1541 : io_message_loop_(io_message_loop),
1542 proxy_service_(proxy_service),
1543 event_(false, false),
1544 callback_(base::Bind(&SyncProxyServiceHelper::OnCompletion,
1545 base::Unretained(this))) {
1546 DCHECK(io_message_loop_ != base::MessageLoop::current());
1549 int SyncProxyServiceHelper::ResolveProxy(const GURL& url,
1550 ProxyInfo* proxy_info,
1551 const BoundNetLog& net_log) {
1552 DCHECK(io_message_loop_ != base::MessageLoop::current());
1554 io_message_loop_->PostTask(
1556 base::Bind(&SyncProxyServiceHelper::StartAsyncResolve, this, url,
1561 if (result_ == net::OK) {
1562 *proxy_info = proxy_info_;
1567 int SyncProxyServiceHelper::ReconsiderProxyAfterError(
1568 const GURL& url, ProxyInfo* proxy_info, const BoundNetLog& net_log) {
1569 DCHECK(io_message_loop_ != base::MessageLoop::current());
1571 io_message_loop_->PostTask(
1573 base::Bind(&SyncProxyServiceHelper::StartAsyncReconsider, this, url,
1578 if (result_ == net::OK) {
1579 *proxy_info = proxy_info_;
1584 SyncProxyServiceHelper::~SyncProxyServiceHelper() {}
1586 void SyncProxyServiceHelper::StartAsyncResolve(const GURL& url,
1587 const BoundNetLog& net_log) {
1588 result_ = proxy_service_->ResolveProxy(
1589 url, &proxy_info_, callback_, NULL, net_log);
1590 if (result_ != net::ERR_IO_PENDING) {
1591 OnCompletion(result_);
1595 void SyncProxyServiceHelper::StartAsyncReconsider(const GURL& url,
1596 const BoundNetLog& net_log) {
1597 result_ = proxy_service_->ReconsiderProxyAfterError(
1598 url, &proxy_info_, callback_, NULL, net_log);
1599 if (result_ != net::ERR_IO_PENDING) {
1600 OnCompletion(result_);
1604 void SyncProxyServiceHelper::OnCompletion(int rv) {