- add sources.
[platform/framework/web/crosswalk.git] / src / net / proxy / proxy_service.cc
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.
4
5 #include "net/proxy/proxy_service.h"
6
7 #include <algorithm>
8
9 #include "base/bind.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"
31 #include "url/gurl.h"
32
33 #if defined(OS_WIN)
34 #include "net/proxy/proxy_config_service_win.h"
35 #include "net/proxy/proxy_resolver_winhttp.h"
36 #elif defined(OS_IOS)
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"
46 #endif
47
48 #if defined(SPDY_PROXY_AUTH_ORIGIN)
49 #include "base/metrics/histogram.h"
50 #endif
51
52 using base::TimeDelta;
53 using base::TimeTicks;
54
55 namespace net {
56
57 namespace {
58
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.
62 //
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.
66 //
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.)
72 //
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
76 // sorts of problems.
77 const int64 kDelayAfterNetworkChangesMs = 2000;
78
79 // This is the default policy for polling the PAC script.
80 //
81 // In response to a failure, the poll intervals are:
82 //    0: 8 seconds  (scheduled on timer)
83 //    1: 32 seconds
84 //    2: 2 minutes
85 //    3+: 4 hours
86 //
87 // In response to a success, the poll intervals are:
88 //    0+: 12 hours
89 //
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).
92 //
93 // Explanation for these values:
94 //
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.
99 //
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.
105 //
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.
112 //
113 // Comparison to other browsers:
114 //
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.
119 //
120 // TODO(eroman): Figure out what Internet Explorer does.
121 class DefaultPollPolicy : public ProxyService::PacPollPolicy {
122  public:
123   DefaultPollPolicy() {}
124
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
134
135       // Initial poll.
136       if (current_delay < TimeDelta()) {
137         *next_delay = TimeDelta::FromSeconds(kDelay1Seconds);
138         return MODE_USE_TIMER;
139       }
140       switch (current_delay.InSeconds()) {
141         case kDelay1Seconds:
142           *next_delay = TimeDelta::FromSeconds(kDelay2Seconds);
143           return MODE_START_AFTER_ACTIVITY;
144         case kDelay2Seconds:
145           *next_delay = TimeDelta::FromSeconds(kDelay3Seconds);
146           return MODE_START_AFTER_ACTIVITY;
147         default:
148           *next_delay = TimeDelta::FromSeconds(kDelay4Seconds);
149           return MODE_START_AFTER_ACTIVITY;
150       }
151     } else {
152       // Re-try policy for succeses.
153       *next_delay = TimeDelta::FromHours(12);
154       return MODE_START_AFTER_ACTIVITY;
155     }
156   }
157
158  private:
159   DISALLOW_COPY_AND_ASSIGN(DefaultPollPolicy);
160 };
161
162 // Config getter that always returns direct settings.
163 class ProxyConfigServiceDirect : public ProxyConfigService {
164  public:
165   // ProxyConfigService implementation:
166   virtual void AddObserver(Observer* observer) OVERRIDE {}
167   virtual void RemoveObserver(Observer* observer) OVERRIDE {}
168   virtual ConfigAvailability GetLatestProxyConfig(ProxyConfig* config)
169       OVERRIDE {
170     *config = ProxyConfig::CreateDirect();
171     config->set_source(PROXY_CONFIG_SOURCE_UNKNOWN);
172     return CONFIG_VALID;
173   }
174 };
175
176 // Proxy resolver that fails every time.
177 class ProxyResolverNull : public ProxyResolver {
178  public:
179   ProxyResolverNull() : ProxyResolver(false /*expects_pac_bytes*/) {}
180
181   // ProxyResolver implementation.
182   virtual int GetProxyForURL(const GURL& url,
183                              ProxyInfo* results,
184                              const CompletionCallback& callback,
185                              RequestHandle* request,
186                              const BoundNetLog& net_log) OVERRIDE {
187     return ERR_NOT_IMPLEMENTED;
188   }
189
190   virtual void CancelRequest(RequestHandle request) OVERRIDE {
191     NOTREACHED();
192   }
193
194   virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
195     NOTREACHED();
196     return LOAD_STATE_IDLE;
197   }
198
199   virtual void CancelSetPacScript() OVERRIDE {
200     NOTREACHED();
201   }
202
203   virtual int SetPacScript(
204       const scoped_refptr<ProxyResolverScriptData>& /*script_data*/,
205       const CompletionCallback& /*callback*/) OVERRIDE {
206     return ERR_NOT_IMPLEMENTED;
207   }
208 };
209
210 // ProxyResolver that simulates a PAC script which returns
211 // |pac_string| for every single URL.
212 class ProxyResolverFromPacString : public ProxyResolver {
213  public:
214   explicit ProxyResolverFromPacString(const std::string& pac_string)
215       : ProxyResolver(false /*expects_pac_bytes*/),
216         pac_string_(pac_string) {}
217
218   virtual int GetProxyForURL(const GURL& url,
219                              ProxyInfo* results,
220                              const CompletionCallback& callback,
221                              RequestHandle* request,
222                              const BoundNetLog& net_log) OVERRIDE {
223     results->UsePacString(pac_string_);
224     return OK;
225   }
226
227   virtual void CancelRequest(RequestHandle request) OVERRIDE {
228     NOTREACHED();
229   }
230
231   virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
232     NOTREACHED();
233     return LOAD_STATE_IDLE;
234   }
235
236   virtual void CancelSetPacScript() OVERRIDE {
237     NOTREACHED();
238   }
239
240   virtual int SetPacScript(
241       const scoped_refptr<ProxyResolverScriptData>& pac_script,
242       const CompletionCallback& callback) OVERRIDE {
243     return OK;
244   }
245
246  private:
247   const std::string pac_string_;
248 };
249
250 // Creates ProxyResolvers using a platform-specific implementation.
251 class ProxyResolverFactoryForSystem : public ProxyResolverFactory {
252  public:
253   ProxyResolverFactoryForSystem()
254       : ProxyResolverFactory(false /*expects_pac_bytes*/) {}
255
256   virtual ProxyResolver* CreateProxyResolver() OVERRIDE {
257     DCHECK(IsSupported());
258 #if defined(OS_WIN)
259     return new ProxyResolverWinHttp();
260 #elif defined(OS_MACOSX)
261     return new ProxyResolverMac();
262 #else
263     NOTREACHED();
264     return NULL;
265 #endif
266   }
267
268   static bool IsSupported() {
269 #if defined(OS_WIN) || defined(OS_MACOSX)
270     return true;
271 #else
272     return false;
273 #endif
274   }
275 };
276
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());
288   return dict;
289 }
290
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();
295
296   for (ProxyRetryInfoMap::const_iterator iter = retry_info->begin();
297        iter != retry_info->end(); ++iter) {
298     list->Append(new base::StringValue(iter->first));
299   }
300   dict->Set("bad_proxy_list", list);
301   return dict;
302 }
303
304 // Returns NetLog parameters on a successfuly proxy resolution.
305 base::Value* NetLogFinishedResolvingProxyCallback(
306     ProxyInfo* result,
307     NetLog::LogLevel /* log_level */) {
308   base::DictionaryValue* dict = new base::DictionaryValue();
309   dict->SetString("pac_string", result->ToPacString());
310   return dict;
311 }
312
313 #if defined(OS_CHROMEOS)
314 class UnsetProxyConfigService : public ProxyConfigService {
315  public:
316   UnsetProxyConfigService() {}
317   virtual ~UnsetProxyConfigService() {}
318
319   virtual void AddObserver(Observer* observer) OVERRIDE {}
320   virtual void RemoveObserver(Observer* observer) OVERRIDE {}
321   virtual ConfigAvailability GetLatestProxyConfig(
322       ProxyConfig* config) OVERRIDE {
323     return CONFIG_UNSET;
324   }
325 };
326 #endif
327
328 }  // namespace
329
330 // ProxyService::InitProxyResolver --------------------------------------------
331
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.
336 //
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
340 // cancelled.
341
342 class ProxyService::InitProxyResolver {
343  public:
344   InitProxyResolver()
345       : proxy_resolver_(NULL),
346         next_state_(STATE_NONE) {
347   }
348
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();
354     }
355   }
356
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,
361             NetLog* net_log,
362             const ProxyConfig& config,
363             TimeDelta wait_delay,
364             const CompletionCallback& callback) {
365     DCHECK_EQ(STATE_NONE, next_state_);
366     proxy_resolver_ = proxy_resolver;
367
368     decider_.reset(new ProxyScriptDecider(
369         proxy_script_fetcher, dhcp_proxy_script_fetcher, net_log));
370     config_ = config;
371     wait_delay_ = wait_delay;
372     callback_ = callback;
373
374     next_state_ = STATE_DECIDE_PROXY_SCRIPT;
375     return DoLoop(OK);
376   }
377
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,
383                        int decider_result,
384                        ProxyResolverScriptData* script_data,
385                        const CompletionCallback& callback) {
386     DCHECK_EQ(STATE_NONE, next_state_);
387     proxy_resolver_ = proxy_resolver;
388
389     effective_config_ = effective_config;
390     script_data_ = script_data;
391     callback_ = callback;
392
393     if (decider_result != OK)
394       return decider_result;
395
396     next_state_ = STATE_SET_PAC_SCRIPT;
397     return DoLoop(OK);
398   }
399
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_;
405   }
406
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();
412   }
413
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;
419     }
420     return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
421   }
422
423  private:
424   enum State {
425     STATE_NONE,
426     STATE_DECIDE_PROXY_SCRIPT,
427     STATE_DECIDE_PROXY_SCRIPT_COMPLETE,
428     STATE_SET_PAC_SCRIPT,
429     STATE_SET_PAC_SCRIPT_COMPLETE,
430   };
431
432   int DoLoop(int result) {
433     DCHECK_NE(next_state_, STATE_NONE);
434     int rv = result;
435     do {
436       State state = next_state_;
437       next_state_ = STATE_NONE;
438       switch (state) {
439         case STATE_DECIDE_PROXY_SCRIPT:
440           DCHECK_EQ(OK, rv);
441           rv = DoDecideProxyScript();
442           break;
443         case STATE_DECIDE_PROXY_SCRIPT_COMPLETE:
444           rv = DoDecideProxyScriptComplete(rv);
445           break;
446         case STATE_SET_PAC_SCRIPT:
447           DCHECK_EQ(OK, rv);
448           rv = DoSetPacScript();
449           break;
450         case STATE_SET_PAC_SCRIPT_COMPLETE:
451           rv = DoSetPacScriptComplete(rv);
452           break;
453         default:
454           NOTREACHED() << "bad state: " << state;
455           rv = ERR_UNEXPECTED;
456           break;
457       }
458     } while (rv != ERR_IO_PENDING && next_state_ != STATE_NONE);
459     return rv;
460   }
461
462   int DoDecideProxyScript() {
463     next_state_ = STATE_DECIDE_PROXY_SCRIPT_COMPLETE;
464
465     return decider_->Start(
466         config_, wait_delay_, proxy_resolver_->expects_pac_bytes(),
467         base::Bind(&InitProxyResolver::OnIOCompletion, base::Unretained(this)));
468   }
469
470   int DoDecideProxyScriptComplete(int result) {
471     if (result != OK)
472       return result;
473
474     effective_config_ = decider_->effective_config();
475     script_data_ = decider_->script_data();
476
477     next_state_ = STATE_SET_PAC_SCRIPT;
478     return OK;
479   }
480
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(
486         script_data_,
487         base::Bind(&InitProxyResolver::OnIOCompletion, base::Unretained(this)));
488   }
489
490   int DoSetPacScriptComplete(int result) {
491     return result;
492   }
493
494   void OnIOCompletion(int result) {
495     DCHECK_NE(STATE_NONE, next_state_);
496     int rv = DoLoop(result);
497     if (rv != ERR_IO_PENDING)
498       DoCallback(rv);
499   }
500
501   void DoCallback(int result) {
502     DCHECK_NE(ERR_IO_PENDING, result);
503     callback_.Run(result);
504   }
505
506   ProxyConfig config_;
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_;
513   State next_state_;
514
515   DISALLOW_COPY_AND_ASSIGN(InitProxyResolver);
516 };
517
518 // ProxyService::ProxyScriptDeciderPoller -------------------------------------
519
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 {
525  public:
526   typedef base::Callback<void(int, ProxyResolverScriptData*,
527                               const ProxyConfig&)> ChangeCallback;
528
529   // Builds a poller helper, and starts polling for updates. Whenever a change
530   // is observed, |callback| will be invoked with the details.
531   //
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,
554                            int init_net_error,
555                            ProxyResolverScriptData* init_script_data,
556                            NetLog* net_log)
557       : weak_factory_(this),
558         change_callback_(callback),
559         config_(config),
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);
570   }
571
572   void OnLazyPoll() {
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);
576   }
577
578   static const PacPollPolicy* set_policy(const PacPollPolicy* policy) {
579     const PacPollPolicy* prev = poll_policy_;
580     poll_policy_ = policy;
581     return prev;
582   }
583
584  private:
585   // Returns the effective poll policy (the one injected by unit-tests, or the
586   // default).
587   const PacPollPolicy* poll_policy() {
588     if (poll_policy_)
589       return poll_policy_;
590     return &default_poll_policy_;
591   }
592
593   void StartPollTimer() {
594     DCHECK(!decider_.get());
595
596     base::MessageLoop::current()->PostDelayedTask(
597         FROM_HERE,
598         base::Bind(&ProxyScriptDeciderPoller::DoPoll,
599                    weak_factory_.GetWeakPtr()),
600         next_poll_delay_);
601   }
602
603   void TryToStartNextPoll(bool triggered_by_activity) {
604     switch (next_poll_mode_) {
605       case PacPollPolicy::MODE_USE_TIMER:
606         if (!triggered_by_activity)
607           StartPollTimer();
608         break;
609
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_)
614             DoPoll();
615         }
616         break;
617     }
618   }
619
620   void DoPoll() {
621     last_poll_time_ = TimeTicks::Now();
622
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)));
631
632     if (result != ERR_IO_PENDING)
633       OnProxyScriptDeciderCompleted(result);
634   }
635
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
642       // the notification.
643       base::MessageLoop::current()->PostTask(
644           FROM_HERE,
645           base::Bind(&ProxyScriptDeciderPoller::NotifyProxyServiceOfChange,
646                      weak_factory_.GetWeakPtr(),
647                      result,
648                      make_scoped_refptr(decider_->script_data()),
649                      decider_->effective_config()));
650       return;
651     }
652
653     decider_.reset();
654
655     // Decide when the next poll should take place, and possibly start the
656     // next timer.
657     next_poll_mode_ = poll_policy()->GetNextDelay(
658         last_error_, next_poll_delay_, &next_poll_delay_);
659     TryToStartNextPoll(false);
660   }
661
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.
667       return true;
668     }
669
670     if (result != OK) {
671       // If it failed last time and failed again with the same error code this
672       // time, then nothing has actually changed.
673       return false;
674     }
675
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
678     // script.
679     return !script_data->Equals(last_script_data_.get());
680   }
681
682   void NotifyProxyServiceOfChange(
683       int result,
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);
688   }
689
690   base::WeakPtrFactory<ProxyScriptDeciderPoller> weak_factory_;
691
692   ChangeCallback change_callback_;
693   ProxyConfig config_;
694   bool proxy_resolver_expects_pac_bytes_;
695   ProxyScriptFetcher* proxy_script_fetcher_;
696   DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher_;
697
698   int last_error_;
699   scoped_refptr<ProxyResolverScriptData> last_script_data_;
700
701   scoped_ptr<ProxyScriptDecider> decider_;
702   TimeDelta next_poll_delay_;
703   PacPollPolicy::Mode next_poll_mode_;
704
705   TimeTicks last_poll_time_;
706
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_;
710
711   const DefaultPollPolicy default_poll_policy_;
712
713   DISALLOW_COPY_AND_ASSIGN(ProxyScriptDeciderPoller);
714 };
715
716 // static
717 const ProxyService::PacPollPolicy*
718     ProxyService::ProxyScriptDeciderPoller::poll_policy_ = NULL;
719
720 // ProxyService::PacRequest ---------------------------------------------------
721
722 class ProxyService::PacRequest
723     : public base::RefCounted<ProxyService::PacRequest> {
724  public:
725     PacRequest(ProxyService* service,
726                const GURL& url,
727                ProxyInfo* results,
728                const net::CompletionCallback& user_callback,
729                const BoundNetLog& net_log)
730       : service_(service),
731         user_callback_(user_callback),
732         results_(results),
733         url_(url),
734         resolve_job_(NULL),
735         config_id_(ProxyConfig::kInvalidConfigID),
736         config_source_(PROXY_CONFIG_SOURCE_UNKNOWN),
737         net_log_(net_log) {
738     DCHECK(!user_callback.is_null());
739   }
740
741   // Starts the resolve proxy request.
742   int Start() {
743     DCHECK(!was_cancelled());
744     DCHECK(!is_started());
745
746     DCHECK(service_->config_.is_valid());
747
748     config_id_ = service_->config_.id();
749     config_source_ = service_->config_.source();
750     proxy_resolve_start_time_ = TimeTicks::Now();
751
752     return resolver()->GetProxyForURL(
753         url_, results_,
754         base::Bind(&PacRequest::QueryComplete, base::Unretained(this)),
755         &resolve_job_, net_log_);
756   }
757
758   bool is_started() const {
759     // Note that !! casts to bool. (VS gives a warning otherwise).
760     return !!resolve_job_;
761   }
762
763   void StartAndCompleteCheckingForSynchronous() {
764     int rv = service_->TryToCompleteSynchronously(url_, results_);
765     if (rv == ERR_IO_PENDING)
766       rv = Start();
767     if (rv != ERR_IO_PENDING)
768       QueryComplete(rv);
769   }
770
771   void CancelResolveJob() {
772     DCHECK(is_started());
773     // The request may already be running in the resolver.
774     resolver()->CancelRequest(resolve_job_);
775     resolve_job_ = NULL;
776     DCHECK(!is_started());
777   }
778
779   void Cancel() {
780     net_log_.AddEvent(NetLog::TYPE_CANCELLED);
781
782     if (is_started())
783       CancelResolveJob();
784
785     // Mark as cancelled, to prevent accessing this again later.
786     service_ = NULL;
787     user_callback_.Reset();
788     results_ = NULL;
789
790     net_log_.EndEvent(NetLog::TYPE_PROXY_SERVICE);
791   }
792
793   // Returns true if Cancel() has been called.
794   bool was_cancelled() const {
795     return user_callback_.is_null();
796   }
797
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());
802
803     // Note that DidFinishResolvingProxy might modify |results_|.
804     int rv = service_->DidFinishResolvingProxy(results_, result_code, net_log_);
805
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();
813
814     // Reset the state associated with in-progress-resolve.
815     resolve_job_ = NULL;
816     config_id_ = ProxyConfig::kInvalidConfigID;
817     config_source_ = PROXY_CONFIG_SOURCE_UNKNOWN;
818
819     return rv;
820   }
821
822   BoundNetLog* net_log() { return &net_log_; }
823
824   LoadState GetLoadState() const {
825     if (is_started())
826       return resolver()->GetLoadState(resolve_job_);
827     return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
828   }
829
830  private:
831   friend class base::RefCounted<ProxyService::PacRequest>;
832
833   ~PacRequest() {}
834
835   // Callback for when the ProxyResolver request has completed.
836   void QueryComplete(int result_code) {
837     result_code = QueryDidComplete(result_code);
838
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);
845     }
846   }
847
848   ProxyResolver* resolver() const { return service_->resolver_.get(); }
849
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_;
855   ProxyInfo* results_;
856   GURL url_;
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_;
864 };
865
866 // ProxyService ---------------------------------------------------------------
867
868 ProxyService::ProxyService(ProxyConfigService* config_service,
869                            ProxyResolver* resolver,
870                            NetLog* net_log)
871     : resolver_(resolver),
872       next_config_id_(1),
873       current_state_(STATE_NONE) ,
874       net_log_(net_log),
875       stall_proxy_auto_config_delay_(TimeDelta::FromMilliseconds(
876           kDelayAfterNetworkChangesMs)) {
877   NetworkChangeNotifier::AddIPAddressObserver(this);
878   NetworkChangeNotifier::AddDNSObserver(this);
879   ResetConfigService(config_service);
880 }
881
882 // static
883 ProxyService* ProxyService::CreateUsingSystemProxyResolver(
884     ProxyConfigService* proxy_config_service,
885     size_t num_pac_threads,
886     NetLog* net_log) {
887   DCHECK(proxy_config_service);
888
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);
893   }
894
895   if (num_pac_threads == 0)
896     num_pac_threads = kDefaultNumPacThreads;
897
898   ProxyResolver* proxy_resolver = new MultiThreadedProxyResolver(
899       new ProxyResolverFactoryForSystem(), num_pac_threads);
900
901   return new ProxyService(proxy_config_service, proxy_resolver, net_log);
902 }
903
904 // static
905 ProxyService* ProxyService::CreateWithoutProxyResolver(
906     ProxyConfigService* proxy_config_service,
907     NetLog* net_log) {
908   return new ProxyService(proxy_config_service,
909                           new ProxyResolverNull(),
910                           net_log);
911 }
912
913 // static
914 ProxyService* ProxyService::CreateFixed(const ProxyConfig& pc) {
915   // TODO(eroman): This isn't quite right, won't work if |pc| specifies
916   //               a PAC script.
917   return CreateUsingSystemProxyResolver(new ProxyConfigServiceFixed(pc),
918                                         0, NULL);
919 }
920
921 // static
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);
926 }
927
928 // static
929 ProxyService* ProxyService::CreateDirect() {
930   return CreateDirectWithNetLog(NULL);
931 }
932
933 ProxyService* ProxyService::CreateDirectWithNetLog(NetLog* net_log) {
934   // Use direct connections.
935   return new ProxyService(new ProxyConfigServiceDirect, new ProxyResolverNull,
936                           net_log);
937 }
938
939 // static
940 ProxyService* ProxyService::CreateFixedFromPacResult(
941     const std::string& pac_string) {
942
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()));
947
948   scoped_ptr<ProxyResolver> proxy_resolver(
949       new ProxyResolverFromPacString(pac_string));
950
951   return new ProxyService(proxy_config_service.release(),
952                           proxy_resolver.release(),
953                           NULL);
954 }
955
956 int ProxyService::ResolveProxy(const GURL& raw_url,
957                                ProxyInfo* result,
958                                const net::CompletionCallback& callback,
959                                PacRequest** pac_request,
960                                const BoundNetLog& net_log) {
961   DCHECK(CalledOnValidThread());
962   DCHECK(!callback.is_null());
963
964   net_log.BeginEvent(NetLog::TYPE_PROXY_SERVICE);
965
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();
971
972   if (current_state_ == STATE_NONE)
973     ApplyProxyConfigIfAvailable();
974
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);
978
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);
984
985   scoped_refptr<PacRequest> req(
986       new PacRequest(this, url, result, callback, net_log));
987
988   if (current_state_ == STATE_READY) {
989     // Start the resolve request.
990     rv = req->Start();
991     if (rv != ERR_IO_PENDING)
992       return req->QueryDidComplete(rv);
993   } else {
994     req->net_log()->BeginEvent(NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
995   }
996
997   DCHECK_EQ(ERR_IO_PENDING, rv);
998   DCHECK(!ContainsPendingRequest(req.get()));
999   pending_requests_.push_back(req);
1000
1001   // Completion will be notified through |callback|, unless the caller cancels
1002   // the request using |pac_request|.
1003   if (pac_request)
1004     *pac_request = req.get();
1005   return rv;  // ERR_IO_PENDING
1006 }
1007
1008 int ProxyService::TryToCompleteSynchronously(const GURL& url,
1009                                              ProxyInfo* result) {
1010   DCHECK_NE(STATE_NONE, current_state_);
1011
1012   if (current_state_ != STATE_READY)
1013     return ERR_IO_PENDING;  // Still initializing.
1014
1015   DCHECK_NE(config_.id(), ProxyConfig::kInvalidConfigID);
1016
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_;
1021
1022   if (config_.HasAutomaticSettings())
1023     return ERR_IO_PENDING;  // Must submit the request to the proxy resolver.
1024
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();
1029   return OK;
1030 }
1031
1032 ProxyService::~ProxyService() {
1033   NetworkChangeNotifier::RemoveIPAddressObserver(this);
1034   NetworkChangeNotifier::RemoveDNSObserver(this);
1035   config_service_->RemoveObserver(this);
1036
1037   // Cancel any inprogress requests.
1038   for (PendingRequests::iterator it = pending_requests_.begin();
1039        it != pending_requests_.end();
1040        ++it) {
1041     (*it)->Cancel();
1042   }
1043 }
1044
1045 void ProxyService::SuspendAllPendingRequests() {
1046   for (PendingRequests::iterator it = pending_requests_.begin();
1047        it != pending_requests_.end();
1048        ++it) {
1049     PacRequest* req = it->get();
1050     if (req->is_started()) {
1051       req->CancelResolveJob();
1052
1053       req->net_log()->BeginEvent(
1054           NetLog::TYPE_PROXY_SERVICE_WAITING_FOR_INIT_PAC);
1055     }
1056   }
1057 }
1058
1059 void ProxyService::SetReady() {
1060   DCHECK(!init_proxy_resolver_.get());
1061   current_state_ = STATE_READY;
1062
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_;
1067
1068   for (PendingRequests::iterator it = pending_copy.begin();
1069        it != pending_copy.end();
1070        ++it) {
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);
1074
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();
1078     }
1079   }
1080 }
1081
1082 void ProxyService::ApplyProxyConfigIfAvailable() {
1083   DCHECK_EQ(STATE_NONE, current_state_);
1084
1085   config_service_->OnLazyPoll();
1086
1087   // If we have already fetched the configuration, start applying it.
1088   if (fetched_config_.is_valid()) {
1089     InitializeUsingLastFetchedConfig();
1090     return;
1091   }
1092
1093   // Otherwise we need to first fetch the configuration.
1094   current_state_ = STATE_WAITING_FOR_PROXY_CONFIG;
1095
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.
1099   ProxyConfig config;
1100   ProxyConfigService::ConfigAvailability availability =
1101       config_service_->GetLatestProxyConfig(&config);
1102   if (availability != ProxyConfigService::CONFIG_PENDING)
1103     OnProxyConfigChanged(config, availability);
1104 }
1105
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();
1111
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)),
1120       fetched_config_,
1121       resolver_->expects_pac_bytes(),
1122       proxy_script_fetcher_.get(),
1123       dhcp_proxy_script_fetcher_.get(),
1124       result,
1125       init_proxy_resolver_->script_data(),
1126       NULL));
1127
1128   init_proxy_resolver_.reset();
1129
1130   if (result != OK) {
1131     if (fetched_config_.pac_mandatory()) {
1132       VLOG(1) << "Failed configuring with mandatory PAC script, blocking all "
1133                  "traffic.";
1134       config_ = fetched_config_;
1135       result = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1136     } else {
1137       VLOG(1) << "Failed configuring with PAC script, falling-back to manual "
1138                  "proxy servers.";
1139       config_ = fetched_config_;
1140       config_.ClearAutomaticSettings();
1141       result = OK;
1142     }
1143   }
1144   permanent_error_ = result;
1145
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());
1150
1151   // Resume any requests which we had to defer until the PAC script was
1152   // downloaded.
1153   SetReady();
1154 }
1155
1156 int ProxyService::ReconsiderProxyAfterError(const GURL& url,
1157                                             ProxyInfo* result,
1158                                             const CompletionCallback& callback,
1159                                             PacRequest** pac_request,
1160                                             const BoundNetLog& net_log) {
1161   DCHECK(CalledOnValidThread());
1162
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.
1166
1167   bool re_resolve = result->config_id_ != config_.id();
1168
1169   if (re_resolve) {
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);
1174   }
1175
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);
1183   }
1184 #endif
1185
1186   // We don't have new proxy settings to try, try to fallback to the next proxy
1187   // in the list.
1188   bool did_fallback = result->Fallback(net_log);
1189
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;
1193 }
1194
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,
1199                                                net_log);
1200   return result.proxy_list_.HasUntriedProxies(proxy_retry_info_);
1201 }
1202
1203 void ProxyService::ReportSuccess(const ProxyInfo& result) {
1204   DCHECK(CalledOnValidThread());
1205
1206   const ProxyRetryInfoMap& new_retry_info = result.proxy_retry_info();
1207   if (new_retry_info.empty())
1208     return;
1209
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;
1217   }
1218   if (net_log_) {
1219     net_log_->AddGlobalEntry(
1220         NetLog::TYPE_BAD_PROXY_LIST_REPORTED,
1221         base::Bind(&NetLogBadProxyListCallback, &new_retry_info));
1222   }
1223 }
1224
1225 void ProxyService::CancelPacRequest(PacRequest* req) {
1226   DCHECK(CalledOnValidThread());
1227   DCHECK(req);
1228   req->Cancel();
1229   RemovePendingRequest(req);
1230 }
1231
1232 LoadState ProxyService::GetLoadState(const PacRequest* req) const {
1233   CHECK(req);
1234   if (current_state_ == STATE_WAITING_FOR_INIT_PROXY_RESOLVER)
1235     return init_proxy_resolver_->GetLoadState();
1236   return req->GetLoadState();
1237 }
1238
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;
1243 }
1244
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);
1250 }
1251
1252 int ProxyService::DidFinishResolvingProxy(ProxyInfo* result,
1253                                           int result_code,
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()) {
1259       net_log.AddEvent(
1260           NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST,
1261           base::Bind(&NetLogFinishedResolvingProxyCallback, result));
1262     }
1263     result->DeprioritizeBadProxies(proxy_retry_info_);
1264   } else {
1265     net_log.AddEventWithNetErrorCode(
1266         NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, result_code);
1267
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.
1271       //
1272       // This implicit fall-back to direct matches Firefox 3.5 and
1273       // Internet Explorer 8. For more information, see:
1274       //
1275       // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
1276       result->UseDirect();
1277       result_code = OK;
1278     } else {
1279       result_code = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1280     }
1281   }
1282
1283   net_log.EndEvent(NetLog::TYPE_PROXY_SERVICE);
1284   return result_code;
1285 }
1286
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();
1296 }
1297
1298 ProxyScriptFetcher* ProxyService::GetProxyScriptFetcher() const {
1299   DCHECK(CalledOnValidThread());
1300   return proxy_script_fetcher_.get();
1301 }
1302
1303 ProxyService::State ProxyService::ResetProxyConfig(bool reset_fetched_config) {
1304   DCHECK(CalledOnValidThread());
1305   State previous_state = current_state_;
1306
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;
1316
1317   return previous_state;
1318 }
1319
1320 void ProxyService::ResetConfigService(
1321     ProxyConfigService* new_proxy_config_service) {
1322   DCHECK(CalledOnValidThread());
1323   State previous_state = ResetProxyConfig(true);
1324
1325   // Release the old configuration service.
1326   if (config_service_.get())
1327     config_service_->RemoveObserver(this);
1328
1329   // Set the new configuration service.
1330   config_service_.reset(new_proxy_config_service);
1331   config_service_->AddObserver(this);
1332
1333   if (previous_state != STATE_NONE)
1334     ApplyProxyConfigIfAvailable();
1335 }
1336
1337 void ProxyService::PurgeMemory() {
1338   DCHECK(CalledOnValidThread());
1339   if (resolver_.get())
1340     resolver_->PurgeMemory();
1341 }
1342
1343 void ProxyService::ForceReloadProxyConfig() {
1344   DCHECK(CalledOnValidThread());
1345   ResetProxyConfig(false);
1346   ApplyProxyConfigIfAvailable();
1347 }
1348
1349 // static
1350 ProxyConfigService* ProxyService::CreateSystemProxyConfigService(
1351     base::SingleThreadTaskRunner* io_thread_task_runner,
1352     base::MessageLoop* file_loop) {
1353 #if defined(OS_WIN)
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();
1367
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();
1373
1374   // The file loop should be a MessageLoopForIO on Linux.
1375   DCHECK_EQ(base::MessageLoop::TYPE_IO, file_loop->type());
1376
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));
1385
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());
1391 #else
1392   LOG(WARNING) << "Failed to choose a system proxy settings fetcher "
1393                   "for this platform.";
1394   return new ProxyConfigServiceDirect();
1395 #endif
1396 }
1397
1398 // static
1399 const ProxyService::PacPollPolicy* ProxyService::set_pac_script_poll_policy(
1400     const PacPollPolicy* policy) {
1401   return ProxyScriptDeciderPoller::set_policy(policy);
1402 }
1403
1404 // static
1405 scoped_ptr<ProxyService::PacPollPolicy>
1406   ProxyService::CreateDefaultPacPollPolicy() {
1407   return scoped_ptr<PacPollPolicy>(new DefaultPollPolicy());
1408 }
1409
1410 #if defined(SPDY_PROXY_AUTH_ORIGIN)
1411 void ProxyService::RecordDataReductionProxyBypassInfo(
1412     bool is_primary,
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())
1417     return;
1418
1419   if (is_primary) {
1420     UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoPrimary",
1421                               bypass_type, BYPASS_EVENT_TYPE_MAX);
1422   } else {
1423     UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoFallback",
1424                               bypass_type, BYPASS_EVENT_TYPE_MAX);
1425   }
1426 }
1427 #endif  // defined(SPDY_PROXY_AUTH_ORIGIN)
1428
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!";
1440       return;
1441     case ProxyConfigService::CONFIG_VALID:
1442       effective_config = config;
1443       break;
1444     case ProxyConfigService::CONFIG_UNSET:
1445       effective_config = ProxyConfig::CreateDirect();
1446       break;
1447   }
1448
1449   // Emit the proxy settings change to the NetLog stream.
1450   if (net_log_) {
1451     net_log_->AddGlobalEntry(
1452         net::NetLog::TYPE_PROXY_CONFIG_CHANGED,
1453         base::Bind(&NetLogProxyConfigChangedCallback,
1454                    &fetched_config_, &effective_config));
1455   }
1456
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().
1460
1461   InitializeUsingLastFetchedConfig();
1462 }
1463
1464 void ProxyService::InitializeUsingLastFetchedConfig() {
1465   ResetProxyConfig(false);
1466
1467   DCHECK(fetched_config_.is_valid());
1468
1469   // Increment the ID to reflect that the config has changed.
1470   fetched_config_.set_id(next_config_id_++);
1471
1472   if (!fetched_config_.HasAutomaticSettings()) {
1473     config_ = fetched_config_;
1474     SetReady();
1475     return;
1476   }
1477
1478   // Start downloading + testing the PAC scripts for this new configuration.
1479   current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1480
1481   // If we changed networks recently, we should delay running proxy auto-config.
1482   TimeDelta wait_delay =
1483       stall_proxy_autoconfig_until_ - TimeTicks::Now();
1484
1485   init_proxy_resolver_.reset(new InitProxyResolver());
1486   int rv = init_proxy_resolver_->Start(
1487       resolver_.get(),
1488       proxy_script_fetcher_.get(),
1489       dhcp_proxy_script_fetcher_.get(),
1490       net_log_,
1491       fetched_config_,
1492       wait_delay,
1493       base::Bind(&ProxyService::OnInitProxyResolverComplete,
1494                  base::Unretained(this)));
1495
1496   if (rv != ERR_IO_PENDING)
1497     OnInitProxyResolverComplete(rv);
1498 }
1499
1500 void ProxyService::InitializeUsingDecidedConfig(
1501     int decider_result,
1502     ProxyResolverScriptData* script_data,
1503     const ProxyConfig& effective_config) {
1504   DCHECK(fetched_config_.is_valid());
1505   DCHECK(fetched_config_.HasAutomaticSettings());
1506
1507   ResetProxyConfig(false);
1508
1509   current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1510
1511   init_proxy_resolver_.reset(new InitProxyResolver());
1512   int rv = init_proxy_resolver_->StartSkipDecider(
1513       resolver_.get(),
1514       effective_config,
1515       decider_result,
1516       script_data,
1517       base::Bind(&ProxyService::OnInitProxyResolverComplete,
1518                  base::Unretained(this)));
1519
1520   if (rv != ERR_IO_PENDING)
1521     OnInitProxyResolverComplete(rv);
1522 }
1523
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_;
1528
1529   State previous_state = ResetProxyConfig(false);
1530   if (previous_state != STATE_NONE)
1531     ApplyProxyConfigIfAvailable();
1532 }
1533
1534 void ProxyService::OnDNSChanged() {
1535   OnIPAddressChanged();
1536 }
1537
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());
1547 }
1548
1549 int SyncProxyServiceHelper::ResolveProxy(const GURL& url,
1550                                          ProxyInfo* proxy_info,
1551                                          const BoundNetLog& net_log) {
1552   DCHECK(io_message_loop_ != base::MessageLoop::current());
1553
1554   io_message_loop_->PostTask(
1555       FROM_HERE,
1556       base::Bind(&SyncProxyServiceHelper::StartAsyncResolve, this, url,
1557                  net_log));
1558
1559   event_.Wait();
1560
1561   if (result_ == net::OK) {
1562     *proxy_info = proxy_info_;
1563   }
1564   return result_;
1565 }
1566
1567 int SyncProxyServiceHelper::ReconsiderProxyAfterError(
1568     const GURL& url, ProxyInfo* proxy_info, const BoundNetLog& net_log) {
1569   DCHECK(io_message_loop_ != base::MessageLoop::current());
1570
1571   io_message_loop_->PostTask(
1572       FROM_HERE,
1573       base::Bind(&SyncProxyServiceHelper::StartAsyncReconsider, this, url,
1574                  net_log));
1575
1576   event_.Wait();
1577
1578   if (result_ == net::OK) {
1579     *proxy_info = proxy_info_;
1580   }
1581   return result_;
1582 }
1583
1584 SyncProxyServiceHelper::~SyncProxyServiceHelper() {}
1585
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_);
1592   }
1593 }
1594
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_);
1601   }
1602 }
1603
1604 void SyncProxyServiceHelper::OnCompletion(int rv) {
1605   result_ = rv;
1606   event_.Signal();
1607 }
1608
1609 }  // namespace net