• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
DefaultPollPolicy()123   DefaultPollPolicy() {}
124 
GetNextDelay(int initial_error,TimeDelta current_delay,TimeDelta * next_delay) const125   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:
AddObserver(Observer * observer)166   virtual void AddObserver(Observer* observer) OVERRIDE {}
RemoveObserver(Observer * observer)167   virtual void RemoveObserver(Observer* observer) OVERRIDE {}
GetLatestProxyConfig(ProxyConfig * config)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:
ProxyResolverNull()179   ProxyResolverNull() : ProxyResolver(false /*expects_pac_bytes*/) {}
180 
181   // ProxyResolver implementation.
GetProxyForURL(const GURL & url,ProxyInfo * results,const CompletionCallback & callback,RequestHandle * request,const BoundNetLog & net_log)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 
CancelRequest(RequestHandle request)190   virtual void CancelRequest(RequestHandle request) OVERRIDE {
191     NOTREACHED();
192   }
193 
GetLoadState(RequestHandle request) const194   virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
195     NOTREACHED();
196     return LOAD_STATE_IDLE;
197   }
198 
CancelSetPacScript()199   virtual void CancelSetPacScript() OVERRIDE {
200     NOTREACHED();
201   }
202 
SetPacScript(const scoped_refptr<ProxyResolverScriptData> &,const CompletionCallback &)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:
ProxyResolverFromPacString(const std::string & pac_string)214   explicit ProxyResolverFromPacString(const std::string& pac_string)
215       : ProxyResolver(false /*expects_pac_bytes*/),
216         pac_string_(pac_string) {}
217 
GetProxyForURL(const GURL & url,ProxyInfo * results,const CompletionCallback & callback,RequestHandle * request,const BoundNetLog & net_log)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 
CancelRequest(RequestHandle request)227   virtual void CancelRequest(RequestHandle request) OVERRIDE {
228     NOTREACHED();
229   }
230 
GetLoadState(RequestHandle request) const231   virtual LoadState GetLoadState(RequestHandle request) const OVERRIDE {
232     NOTREACHED();
233     return LOAD_STATE_IDLE;
234   }
235 
CancelSetPacScript()236   virtual void CancelSetPacScript() OVERRIDE {
237     NOTREACHED();
238   }
239 
SetPacScript(const scoped_refptr<ProxyResolverScriptData> & pac_script,const CompletionCallback & callback)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:
ProxyResolverFactoryForSystem()253   ProxyResolverFactoryForSystem()
254       : ProxyResolverFactory(false /*expects_pac_bytes*/) {}
255 
CreateProxyResolver()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 
IsSupported()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.
NetLogProxyConfigChangedCallback(const ProxyConfig * old_config,const ProxyConfig * new_config,NetLog::LogLevel)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 
NetLogBadProxyListCallback(const ProxyRetryInfoMap * retry_info,NetLog::LogLevel)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.
NetLogFinishedResolvingProxyCallback(ProxyInfo * result,NetLog::LogLevel)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:
UnsetProxyConfigService()316   UnsetProxyConfigService() {}
~UnsetProxyConfigService()317   virtual ~UnsetProxyConfigService() {}
318 
AddObserver(Observer * observer)319   virtual void AddObserver(Observer* observer) OVERRIDE {}
RemoveObserver(Observer * observer)320   virtual void RemoveObserver(Observer* observer) OVERRIDE {}
GetLatestProxyConfig(ProxyConfig * config)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:
InitProxyResolver()344   InitProxyResolver()
345       : proxy_resolver_(NULL),
346         next_state_(STATE_NONE) {
347   }
348 
~InitProxyResolver()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.
Start(ProxyResolver * proxy_resolver,ProxyScriptFetcher * proxy_script_fetcher,DhcpProxyScriptFetcher * dhcp_proxy_script_fetcher,NetLog * net_log,const ProxyConfig & config,TimeDelta wait_delay,const CompletionCallback & callback)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.
StartSkipDecider(ProxyResolver * proxy_resolver,const ProxyConfig & effective_config,int decider_result,ProxyResolverScriptData * script_data,const CompletionCallback & callback)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.
effective_config() const402   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.
script_data()409   ProxyResolverScriptData* script_data() {
410     DCHECK_EQ(STATE_NONE, next_state_);
411     return script_data_.get();
412   }
413 
GetLoadState() const414   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 
DoLoop(int result)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 
DoDecideProxyScript()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 
DoDecideProxyScriptComplete(int result)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 
DoSetPacScript()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 
DoSetPacScriptComplete(int result)490   int DoSetPacScriptComplete(int result) {
491     return result;
492   }
493 
OnIOCompletion(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)
498       DoCallback(rv);
499   }
500 
DoCallback(int result)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.
ProxyScriptDeciderPoller(ChangeCallback callback,const ProxyConfig & config,bool proxy_resolver_expects_pac_bytes,ProxyScriptFetcher * proxy_script_fetcher,DhcpProxyScriptFetcher * dhcp_proxy_script_fetcher,int init_net_error,ProxyResolverScriptData * init_script_data,NetLog * net_log)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 
OnLazyPoll()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 
set_policy(const PacPollPolicy * policy)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).
poll_policy()587   const PacPollPolicy* poll_policy() {
588     if (poll_policy_)
589       return poll_policy_;
590     return &default_poll_policy_;
591   }
592 
StartPollTimer()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 
TryToStartNextPoll(bool triggered_by_activity)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 
DoPoll()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 
OnProxyScriptDeciderCompleted(int 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
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 
HasScriptDataChanged(int result,ProxyResolverScriptData * script_data)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 
NotifyProxyServiceOfChange(int result,const scoped_refptr<ProxyResolverScriptData> & script_data,const ProxyConfig & effective_config)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:
PacRequest(ProxyService * service,const GURL & url,ProxyInfo * results,const net::CompletionCallback & user_callback,const BoundNetLog & net_log)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.
Start()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 
is_started() const758   bool is_started() const {
759     // Note that !! casts to bool. (VS gives a warning otherwise).
760     return !!resolve_job_;
761   }
762 
StartAndCompleteCheckingForSynchronous()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 
CancelResolveJob()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 
Cancel()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.
was_cancelled() const794   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.
QueryDidComplete(int result_code)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 
net_log()822   BoundNetLog* net_log() { return &net_log_; }
823 
GetLoadState() const824   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 
~PacRequest()833   ~PacRequest() {}
834 
835   // Callback for when the ProxyResolver request has completed.
QueryComplete(int result_code)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 
resolver() const848   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 
ProxyService(ProxyConfigService * config_service,ProxyResolver * resolver,NetLog * net_log)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
CreateUsingSystemProxyResolver(ProxyConfigService * proxy_config_service,size_t num_pac_threads,NetLog * net_log)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
CreateWithoutProxyResolver(ProxyConfigService * proxy_config_service,NetLog * net_log)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
CreateFixed(const ProxyConfig & pc)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
CreateFixed(const std::string & proxy)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
CreateDirect()929 ProxyService* ProxyService::CreateDirect() {
930   return CreateDirectWithNetLog(NULL);
931 }
932 
CreateDirectWithNetLog(NetLog * net_log)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
CreateFixedFromPacResult(const std::string & pac_string)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 
ResolveProxy(const GURL & raw_url,ProxyInfo * result,const net::CompletionCallback & callback,PacRequest ** pac_request,const BoundNetLog & net_log)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 
TryToCompleteSynchronously(const GURL & url,ProxyInfo * result)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 
~ProxyService()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 
SuspendAllPendingRequests()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 
SetReady()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 
ApplyProxyConfigIfAvailable()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 
OnInitProxyResolverComplete(int result)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 
ReconsiderProxyAfterError(const GURL & url,ProxyInfo * result,const CompletionCallback & callback,PacRequest ** pac_request,const BoundNetLog & net_log)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 
MarkProxiesAsBad(const ProxyInfo & result,base::TimeDelta retry_delay,const ProxyServer & another_bad_proxy,const BoundNetLog & net_log)1195 bool ProxyService::MarkProxiesAsBad(
1196     const ProxyInfo& result,
1197     base::TimeDelta retry_delay,
1198     const ProxyServer& another_bad_proxy,
1199     const BoundNetLog& net_log) {
1200   result.proxy_list_.UpdateRetryInfoOnFallback(&proxy_retry_info_, retry_delay,
1201                                                another_bad_proxy,
1202                                                net_log);
1203   return result.proxy_list_.HasUntriedProxies(proxy_retry_info_);
1204 }
1205 
ReportSuccess(const ProxyInfo & result)1206 void ProxyService::ReportSuccess(const ProxyInfo& result) {
1207   DCHECK(CalledOnValidThread());
1208 
1209   const ProxyRetryInfoMap& new_retry_info = result.proxy_retry_info();
1210   if (new_retry_info.empty())
1211     return;
1212 
1213   for (ProxyRetryInfoMap::const_iterator iter = new_retry_info.begin();
1214        iter != new_retry_info.end(); ++iter) {
1215     ProxyRetryInfoMap::iterator existing = proxy_retry_info_.find(iter->first);
1216     if (existing == proxy_retry_info_.end())
1217       proxy_retry_info_[iter->first] = iter->second;
1218     else if (existing->second.bad_until < iter->second.bad_until)
1219       existing->second.bad_until = iter->second.bad_until;
1220   }
1221   if (net_log_) {
1222     net_log_->AddGlobalEntry(
1223         NetLog::TYPE_BAD_PROXY_LIST_REPORTED,
1224         base::Bind(&NetLogBadProxyListCallback, &new_retry_info));
1225   }
1226 }
1227 
CancelPacRequest(PacRequest * req)1228 void ProxyService::CancelPacRequest(PacRequest* req) {
1229   DCHECK(CalledOnValidThread());
1230   DCHECK(req);
1231   req->Cancel();
1232   RemovePendingRequest(req);
1233 }
1234 
GetLoadState(const PacRequest * req) const1235 LoadState ProxyService::GetLoadState(const PacRequest* req) const {
1236   CHECK(req);
1237   if (current_state_ == STATE_WAITING_FOR_INIT_PROXY_RESOLVER)
1238     return init_proxy_resolver_->GetLoadState();
1239   return req->GetLoadState();
1240 }
1241 
ContainsPendingRequest(PacRequest * req)1242 bool ProxyService::ContainsPendingRequest(PacRequest* req) {
1243   PendingRequests::iterator it = std::find(
1244       pending_requests_.begin(), pending_requests_.end(), req);
1245   return pending_requests_.end() != it;
1246 }
1247 
RemovePendingRequest(PacRequest * req)1248 void ProxyService::RemovePendingRequest(PacRequest* req) {
1249   DCHECK(ContainsPendingRequest(req));
1250   PendingRequests::iterator it = std::find(
1251       pending_requests_.begin(), pending_requests_.end(), req);
1252   pending_requests_.erase(it);
1253 }
1254 
DidFinishResolvingProxy(ProxyInfo * result,int result_code,const BoundNetLog & net_log)1255 int ProxyService::DidFinishResolvingProxy(ProxyInfo* result,
1256                                           int result_code,
1257                                           const BoundNetLog& net_log) {
1258   // Log the result of the proxy resolution.
1259   if (result_code == OK) {
1260     // When logging all events is enabled, dump the proxy list.
1261     if (net_log.IsLoggingAllEvents()) {
1262       net_log.AddEvent(
1263           NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST,
1264           base::Bind(&NetLogFinishedResolvingProxyCallback, result));
1265     }
1266     result->DeprioritizeBadProxies(proxy_retry_info_);
1267   } else {
1268     net_log.AddEventWithNetErrorCode(
1269         NetLog::TYPE_PROXY_SERVICE_RESOLVED_PROXY_LIST, result_code);
1270 
1271     if (!config_.pac_mandatory()) {
1272       // Fall-back to direct when the proxy resolver fails. This corresponds
1273       // with a javascript runtime error in the PAC script.
1274       //
1275       // This implicit fall-back to direct matches Firefox 3.5 and
1276       // Internet Explorer 8. For more information, see:
1277       //
1278       // http://www.chromium.org/developers/design-documents/proxy-settings-fallback
1279       result->UseDirect();
1280       result_code = OK;
1281     } else {
1282       result_code = ERR_MANDATORY_PROXY_CONFIGURATION_FAILED;
1283     }
1284   }
1285 
1286   net_log.EndEvent(NetLog::TYPE_PROXY_SERVICE);
1287   return result_code;
1288 }
1289 
SetProxyScriptFetchers(ProxyScriptFetcher * proxy_script_fetcher,DhcpProxyScriptFetcher * dhcp_proxy_script_fetcher)1290 void ProxyService::SetProxyScriptFetchers(
1291     ProxyScriptFetcher* proxy_script_fetcher,
1292     DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher) {
1293   DCHECK(CalledOnValidThread());
1294   State previous_state = ResetProxyConfig(false);
1295   proxy_script_fetcher_.reset(proxy_script_fetcher);
1296   dhcp_proxy_script_fetcher_.reset(dhcp_proxy_script_fetcher);
1297   if (previous_state != STATE_NONE)
1298     ApplyProxyConfigIfAvailable();
1299 }
1300 
GetProxyScriptFetcher() const1301 ProxyScriptFetcher* ProxyService::GetProxyScriptFetcher() const {
1302   DCHECK(CalledOnValidThread());
1303   return proxy_script_fetcher_.get();
1304 }
1305 
ResetProxyConfig(bool reset_fetched_config)1306 ProxyService::State ProxyService::ResetProxyConfig(bool reset_fetched_config) {
1307   DCHECK(CalledOnValidThread());
1308   State previous_state = current_state_;
1309 
1310   permanent_error_ = OK;
1311   proxy_retry_info_.clear();
1312   script_poller_.reset();
1313   init_proxy_resolver_.reset();
1314   SuspendAllPendingRequests();
1315   config_ = ProxyConfig();
1316   if (reset_fetched_config)
1317     fetched_config_ = ProxyConfig();
1318   current_state_ = STATE_NONE;
1319 
1320   return previous_state;
1321 }
1322 
ResetConfigService(ProxyConfigService * new_proxy_config_service)1323 void ProxyService::ResetConfigService(
1324     ProxyConfigService* new_proxy_config_service) {
1325   DCHECK(CalledOnValidThread());
1326   State previous_state = ResetProxyConfig(true);
1327 
1328   // Release the old configuration service.
1329   if (config_service_.get())
1330     config_service_->RemoveObserver(this);
1331 
1332   // Set the new configuration service.
1333   config_service_.reset(new_proxy_config_service);
1334   config_service_->AddObserver(this);
1335 
1336   if (previous_state != STATE_NONE)
1337     ApplyProxyConfigIfAvailable();
1338 }
1339 
PurgeMemory()1340 void ProxyService::PurgeMemory() {
1341   DCHECK(CalledOnValidThread());
1342   if (resolver_.get())
1343     resolver_->PurgeMemory();
1344 }
1345 
ForceReloadProxyConfig()1346 void ProxyService::ForceReloadProxyConfig() {
1347   DCHECK(CalledOnValidThread());
1348   ResetProxyConfig(false);
1349   ApplyProxyConfigIfAvailable();
1350 }
1351 
1352 // static
CreateSystemProxyConfigService(base::SingleThreadTaskRunner * io_thread_task_runner,base::MessageLoop * file_loop)1353 ProxyConfigService* ProxyService::CreateSystemProxyConfigService(
1354     base::SingleThreadTaskRunner* io_thread_task_runner,
1355     base::MessageLoop* file_loop) {
1356 #if defined(OS_WIN)
1357   return new ProxyConfigServiceWin();
1358 #elif defined(OS_IOS)
1359   return new ProxyConfigServiceIOS();
1360 #elif defined(OS_MACOSX)
1361   return new ProxyConfigServiceMac(io_thread_task_runner);
1362 #elif defined(OS_CHROMEOS)
1363   LOG(ERROR) << "ProxyConfigService for ChromeOS should be created in "
1364              << "profile_io_data.cc::CreateProxyConfigService and this should "
1365              << "be used only for examples.";
1366   return new UnsetProxyConfigService;
1367 #elif defined(OS_LINUX)
1368   ProxyConfigServiceLinux* linux_config_service =
1369       new ProxyConfigServiceLinux();
1370 
1371   // Assume we got called on the thread that runs the default glib
1372   // main loop, so the current thread is where we should be running
1373   // gconf calls from.
1374   scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner =
1375       base::ThreadTaskRunnerHandle::Get();
1376 
1377   // The file loop should be a MessageLoopForIO on Linux.
1378   DCHECK_EQ(base::MessageLoop::TYPE_IO, file_loop->type());
1379 
1380   // Synchronously fetch the current proxy config (since we are
1381   // running on glib_default_loop). Additionally register for
1382   // notifications (delivered in either |glib_default_loop| or
1383   // |file_loop|) to keep us updated when the proxy config changes.
1384   linux_config_service->SetupAndFetchInitialConfig(
1385       glib_thread_task_runner.get(),
1386       io_thread_task_runner,
1387       static_cast<base::MessageLoopForIO*>(file_loop));
1388 
1389   return linux_config_service;
1390 #elif defined(OS_ANDROID)
1391   return new ProxyConfigServiceAndroid(
1392       io_thread_task_runner,
1393       base::MessageLoop::current()->message_loop_proxy());
1394 #else
1395   LOG(WARNING) << "Failed to choose a system proxy settings fetcher "
1396                   "for this platform.";
1397   return new ProxyConfigServiceDirect();
1398 #endif
1399 }
1400 
1401 // static
set_pac_script_poll_policy(const PacPollPolicy * policy)1402 const ProxyService::PacPollPolicy* ProxyService::set_pac_script_poll_policy(
1403     const PacPollPolicy* policy) {
1404   return ProxyScriptDeciderPoller::set_policy(policy);
1405 }
1406 
1407 // static
1408 scoped_ptr<ProxyService::PacPollPolicy>
CreateDefaultPacPollPolicy()1409   ProxyService::CreateDefaultPacPollPolicy() {
1410   return scoped_ptr<PacPollPolicy>(new DefaultPollPolicy());
1411 }
1412 
1413 #if defined(SPDY_PROXY_AUTH_ORIGIN)
RecordDataReductionProxyBypassInfo(bool is_primary,const ProxyServer & proxy_server,DataReductionProxyBypassEventType bypass_type) const1414 void ProxyService::RecordDataReductionProxyBypassInfo(
1415     bool is_primary,
1416     const ProxyServer& proxy_server,
1417     DataReductionProxyBypassEventType bypass_type) const {
1418   // Only record UMA if the proxy isn't already on the retry list.
1419   if (proxy_retry_info_.find(proxy_server.ToURI()) != proxy_retry_info_.end())
1420     return;
1421 
1422   if (is_primary) {
1423     UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoPrimary",
1424                               bypass_type, BYPASS_EVENT_TYPE_MAX);
1425   } else {
1426     UMA_HISTOGRAM_ENUMERATION("DataReductionProxy.BypassInfoFallback",
1427                               bypass_type, BYPASS_EVENT_TYPE_MAX);
1428   }
1429 }
1430 #endif  // defined(SPDY_PROXY_AUTH_ORIGIN)
1431 
OnProxyConfigChanged(const ProxyConfig & config,ProxyConfigService::ConfigAvailability availability)1432 void ProxyService::OnProxyConfigChanged(
1433     const ProxyConfig& config,
1434     ProxyConfigService::ConfigAvailability availability) {
1435   // Retrieve the current proxy configuration from the ProxyConfigService.
1436   // If a configuration is not available yet, we will get called back later
1437   // by our ProxyConfigService::Observer once it changes.
1438   ProxyConfig effective_config;
1439   switch (availability) {
1440     case ProxyConfigService::CONFIG_PENDING:
1441       // ProxyConfigService implementors should never pass CONFIG_PENDING.
1442       NOTREACHED() << "Proxy config change with CONFIG_PENDING availability!";
1443       return;
1444     case ProxyConfigService::CONFIG_VALID:
1445       effective_config = config;
1446       break;
1447     case ProxyConfigService::CONFIG_UNSET:
1448       effective_config = ProxyConfig::CreateDirect();
1449       break;
1450   }
1451 
1452   // Emit the proxy settings change to the NetLog stream.
1453   if (net_log_) {
1454     net_log_->AddGlobalEntry(
1455         net::NetLog::TYPE_PROXY_CONFIG_CHANGED,
1456         base::Bind(&NetLogProxyConfigChangedCallback,
1457                    &fetched_config_, &effective_config));
1458   }
1459 
1460   // Set the new configuration as the most recently fetched one.
1461   fetched_config_ = effective_config;
1462   fetched_config_.set_id(1);  // Needed for a later DCHECK of is_valid().
1463 
1464   InitializeUsingLastFetchedConfig();
1465 }
1466 
InitializeUsingLastFetchedConfig()1467 void ProxyService::InitializeUsingLastFetchedConfig() {
1468   ResetProxyConfig(false);
1469 
1470   DCHECK(fetched_config_.is_valid());
1471 
1472   // Increment the ID to reflect that the config has changed.
1473   fetched_config_.set_id(next_config_id_++);
1474 
1475   if (!fetched_config_.HasAutomaticSettings()) {
1476     config_ = fetched_config_;
1477     SetReady();
1478     return;
1479   }
1480 
1481   // Start downloading + testing the PAC scripts for this new configuration.
1482   current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1483 
1484   // If we changed networks recently, we should delay running proxy auto-config.
1485   TimeDelta wait_delay =
1486       stall_proxy_autoconfig_until_ - TimeTicks::Now();
1487 
1488   init_proxy_resolver_.reset(new InitProxyResolver());
1489   int rv = init_proxy_resolver_->Start(
1490       resolver_.get(),
1491       proxy_script_fetcher_.get(),
1492       dhcp_proxy_script_fetcher_.get(),
1493       net_log_,
1494       fetched_config_,
1495       wait_delay,
1496       base::Bind(&ProxyService::OnInitProxyResolverComplete,
1497                  base::Unretained(this)));
1498 
1499   if (rv != ERR_IO_PENDING)
1500     OnInitProxyResolverComplete(rv);
1501 }
1502 
InitializeUsingDecidedConfig(int decider_result,ProxyResolverScriptData * script_data,const ProxyConfig & effective_config)1503 void ProxyService::InitializeUsingDecidedConfig(
1504     int decider_result,
1505     ProxyResolverScriptData* script_data,
1506     const ProxyConfig& effective_config) {
1507   DCHECK(fetched_config_.is_valid());
1508   DCHECK(fetched_config_.HasAutomaticSettings());
1509 
1510   ResetProxyConfig(false);
1511 
1512   current_state_ = STATE_WAITING_FOR_INIT_PROXY_RESOLVER;
1513 
1514   init_proxy_resolver_.reset(new InitProxyResolver());
1515   int rv = init_proxy_resolver_->StartSkipDecider(
1516       resolver_.get(),
1517       effective_config,
1518       decider_result,
1519       script_data,
1520       base::Bind(&ProxyService::OnInitProxyResolverComplete,
1521                  base::Unretained(this)));
1522 
1523   if (rv != ERR_IO_PENDING)
1524     OnInitProxyResolverComplete(rv);
1525 }
1526 
OnIPAddressChanged()1527 void ProxyService::OnIPAddressChanged() {
1528   // See the comment block by |kDelayAfterNetworkChangesMs| for info.
1529   stall_proxy_autoconfig_until_ =
1530       TimeTicks::Now() + stall_proxy_auto_config_delay_;
1531 
1532   State previous_state = ResetProxyConfig(false);
1533   if (previous_state != STATE_NONE)
1534     ApplyProxyConfigIfAvailable();
1535 }
1536 
OnDNSChanged()1537 void ProxyService::OnDNSChanged() {
1538   OnIPAddressChanged();
1539 }
1540 
SyncProxyServiceHelper(base::MessageLoop * io_message_loop,ProxyService * proxy_service)1541 SyncProxyServiceHelper::SyncProxyServiceHelper(
1542     base::MessageLoop* io_message_loop,
1543     ProxyService* proxy_service)
1544     : io_message_loop_(io_message_loop),
1545       proxy_service_(proxy_service),
1546       event_(false, false),
1547       callback_(base::Bind(&SyncProxyServiceHelper::OnCompletion,
1548                            base::Unretained(this))) {
1549   DCHECK(io_message_loop_ != base::MessageLoop::current());
1550 }
1551 
ResolveProxy(const GURL & url,ProxyInfo * proxy_info,const BoundNetLog & net_log)1552 int SyncProxyServiceHelper::ResolveProxy(const GURL& url,
1553                                          ProxyInfo* proxy_info,
1554                                          const BoundNetLog& net_log) {
1555   DCHECK(io_message_loop_ != base::MessageLoop::current());
1556 
1557   io_message_loop_->PostTask(
1558       FROM_HERE,
1559       base::Bind(&SyncProxyServiceHelper::StartAsyncResolve, this, url,
1560                  net_log));
1561 
1562   event_.Wait();
1563 
1564   if (result_ == net::OK) {
1565     *proxy_info = proxy_info_;
1566   }
1567   return result_;
1568 }
1569 
ReconsiderProxyAfterError(const GURL & url,ProxyInfo * proxy_info,const BoundNetLog & net_log)1570 int SyncProxyServiceHelper::ReconsiderProxyAfterError(
1571     const GURL& url, ProxyInfo* proxy_info, const BoundNetLog& net_log) {
1572   DCHECK(io_message_loop_ != base::MessageLoop::current());
1573 
1574   io_message_loop_->PostTask(
1575       FROM_HERE,
1576       base::Bind(&SyncProxyServiceHelper::StartAsyncReconsider, this, url,
1577                  net_log));
1578 
1579   event_.Wait();
1580 
1581   if (result_ == net::OK) {
1582     *proxy_info = proxy_info_;
1583   }
1584   return result_;
1585 }
1586 
~SyncProxyServiceHelper()1587 SyncProxyServiceHelper::~SyncProxyServiceHelper() {}
1588 
StartAsyncResolve(const GURL & url,const BoundNetLog & net_log)1589 void SyncProxyServiceHelper::StartAsyncResolve(const GURL& url,
1590                                                const BoundNetLog& net_log) {
1591   result_ = proxy_service_->ResolveProxy(
1592       url, &proxy_info_, callback_, NULL, net_log);
1593   if (result_ != net::ERR_IO_PENDING) {
1594     OnCompletion(result_);
1595   }
1596 }
1597 
StartAsyncReconsider(const GURL & url,const BoundNetLog & net_log)1598 void SyncProxyServiceHelper::StartAsyncReconsider(const GURL& url,
1599                                                   const BoundNetLog& net_log) {
1600   result_ = proxy_service_->ReconsiderProxyAfterError(
1601       url, &proxy_info_, callback_, NULL, net_log);
1602   if (result_ != net::ERR_IO_PENDING) {
1603     OnCompletion(result_);
1604   }
1605 }
1606 
OnCompletion(int rv)1607 void SyncProxyServiceHelper::OnCompletion(int rv) {
1608   result_ = rv;
1609   event_.Signal();
1610 }
1611 
1612 }  // namespace net
1613