Home | History | Annotate | Download | only in proxy
      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 #ifndef NET_PROXY_PROXY_SERVICE_H_
      6 #define NET_PROXY_PROXY_SERVICE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "base/memory/ref_counted.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "base/synchronization/waitable_event.h"
     15 #include "base/threading/non_thread_safe.h"
     16 #include "net/base/completion_callback.h"
     17 #include "net/base/load_states.h"
     18 #include "net/base/net_export.h"
     19 #include "net/base/net_log.h"
     20 #include "net/base/network_change_notifier.h"
     21 #include "net/proxy/proxy_config_service.h"
     22 #include "net/proxy/proxy_info.h"
     23 #include "net/proxy/proxy_server.h"
     24 
     25 class GURL;
     26 
     27 namespace base {
     28 class MessageLoop;
     29 class SingleThreadTaskRunner;
     30 }  // namespace base
     31 
     32 namespace net {
     33 
     34 class DhcpProxyScriptFetcher;
     35 class HostResolver;
     36 class NetworkDelegate;
     37 class ProxyResolver;
     38 class ProxyResolverScriptData;
     39 class ProxyScriptDecider;
     40 class ProxyScriptFetcher;
     41 
     42 // This class can be used to resolve the proxy server to use when loading a
     43 // HTTP(S) URL.  It uses the given ProxyResolver to handle the actual proxy
     44 // resolution.  See ProxyResolverV8 for example.
     45 class NET_EXPORT ProxyService : public NetworkChangeNotifier::IPAddressObserver,
     46                                 public NetworkChangeNotifier::DNSObserver,
     47                                 public ProxyConfigService::Observer,
     48                                 NON_EXPORTED_BASE(public base::NonThreadSafe) {
     49  public:
     50   static const size_t kDefaultNumPacThreads = 4;
     51 
     52   // This interface defines the set of policies for when to poll the PAC
     53   // script for changes.
     54   //
     55   // The polling policy decides what the next poll delay should be in
     56   // milliseconds. It also decides how to wait for this delay -- either
     57   // by starting a timer to do the poll at exactly |next_delay_ms|
     58   // (MODE_USE_TIMER) or by waiting for the first network request issued after
     59   // |next_delay_ms| (MODE_START_AFTER_ACTIVITY).
     60   //
     61   // The timer method is more precise and guarantees that polling happens when
     62   // it was requested. However it has the disadvantage of causing spurious CPU
     63   // and network activity. It is a reasonable choice to use for short poll
     64   // intervals which only happen a couple times.
     65   //
     66   // However for repeated timers this will prevent the browser from going
     67   // idle. MODE_START_AFTER_ACTIVITY solves this problem by only polling in
     68   // direct response to network activity. The drawback to
     69   // MODE_START_AFTER_ACTIVITY is since the poll is initiated only after the
     70   // request is received, the first couple requests initiated after a long
     71   // period of inactivity will likely see a stale version of the PAC script
     72   // until the background polling gets a chance to update things.
     73   class NET_EXPORT_PRIVATE PacPollPolicy {
     74    public:
     75     enum Mode {
     76       MODE_USE_TIMER,
     77       MODE_START_AFTER_ACTIVITY,
     78     };
     79 
     80     virtual ~PacPollPolicy() {}
     81 
     82     // Decides the next poll delay. |current_delay| is the delay used
     83     // by the preceding poll, or a negative TimeDelta value if determining
     84     // the delay for the initial poll. |initial_error| is the network error
     85     // code that the last PAC fetch (or WPAD initialization) failed with,
     86     // or OK if it completed successfully. Implementations must set
     87     // |next_delay| to a non-negative value.
     88     virtual Mode GetNextDelay(int initial_error,
     89                               base::TimeDelta current_delay,
     90                               base::TimeDelta* next_delay) const = 0;
     91   };
     92 
     93   // The instance takes ownership of |config_service| and |resolver|.
     94   // |net_log| is a possibly NULL destination to send log events to. It must
     95   // remain alive for the lifetime of this ProxyService.
     96   ProxyService(ProxyConfigService* config_service,
     97                ProxyResolver* resolver,
     98                NetLog* net_log);
     99 
    100   virtual ~ProxyService();
    101 
    102   // Used internally to handle PAC queries.
    103   // TODO(eroman): consider naming this simply "Request".
    104   class PacRequest;
    105 
    106   // Returns ERR_IO_PENDING if the proxy information could not be provided
    107   // synchronously, to indicate that the result will be available when the
    108   // callback is run.  The callback is run on the thread that calls
    109   // ResolveProxy.
    110   //
    111   // The caller is responsible for ensuring that |results| and |callback|
    112   // remain valid until the callback is run or until |pac_request| is cancelled
    113   // via CancelPacRequest.  |pac_request| is only valid while the completion
    114   // callback is still pending. NULL can be passed for |pac_request| if
    115   // the caller will not need to cancel the request.
    116   //
    117   // We use the three possible proxy access types in the following order,
    118   // doing fallback if one doesn't work.  See "pac_script_decider.h"
    119   // for the specifics.
    120   //   1.  WPAD auto-detection
    121   //   2.  PAC URL
    122   //   3.  named proxy
    123   //
    124   // Profiling information for the request is saved to |net_log| if non-NULL.
    125   int ResolveProxy(const GURL& url,
    126                    ProxyInfo* results,
    127                    const net::CompletionCallback& callback,
    128                    PacRequest** pac_request,
    129                    const BoundNetLog& net_log);
    130 
    131   // This method is called after a failure to connect or resolve a host name.
    132   // It gives the proxy service an opportunity to reconsider the proxy to use.
    133   // The |results| parameter contains the results returned by an earlier call
    134   // to ResolveProxy.  The semantics of this call are otherwise similar to
    135   // ResolveProxy.
    136   //
    137   // NULL can be passed for |pac_request| if the caller will not need to
    138   // cancel the request.
    139   //
    140   // Returns ERR_FAILED if there is not another proxy config to try.
    141   //
    142   // Profiling information for the request is saved to |net_log| if non-NULL.
    143   int ReconsiderProxyAfterError(const GURL& url,
    144                                 ProxyInfo* results,
    145                                 const CompletionCallback& callback,
    146                                 PacRequest** pac_request,
    147                                 const BoundNetLog& net_log);
    148 
    149   // Explicitly trigger proxy fallback for the given |results| by updating our
    150   // list of bad proxies to include the first entry of |results|. Returns true
    151   // if there will be at least one proxy remaining in the list after fallback
    152   // and false otherwise.
    153   bool MarkProxyAsBad(const ProxyInfo& results, const BoundNetLog& net_log);
    154 
    155   // Called to report that the last proxy connection succeeded.  If |proxy_info|
    156   // has a non empty proxy_retry_info map, the proxies that have been tried (and
    157   // failed) for this request will be marked as bad.
    158   void ReportSuccess(const ProxyInfo& proxy_info);
    159 
    160   // Call this method with a non-null |pac_request| to cancel the PAC request.
    161   void CancelPacRequest(PacRequest* pac_request);
    162 
    163   // Returns the LoadState for this |pac_request| which must be non-NULL.
    164   LoadState GetLoadState(const PacRequest* pac_request) const;
    165 
    166   // Sets the ProxyScriptFetcher and DhcpProxyScriptFetcher dependencies. This
    167   // is needed if the ProxyResolver is of type ProxyResolverWithoutFetch.
    168   // ProxyService takes ownership of both objects.
    169   void SetProxyScriptFetchers(
    170       ProxyScriptFetcher* proxy_script_fetcher,
    171       DhcpProxyScriptFetcher* dhcp_proxy_script_fetcher);
    172   ProxyScriptFetcher* GetProxyScriptFetcher() const;
    173 
    174   // Tells this ProxyService to start using a new ProxyConfigService to
    175   // retrieve its ProxyConfig from. The new ProxyConfigService will immediately
    176   // be queried for new config info which will be used for all subsequent
    177   // ResolveProxy calls. ProxyService takes ownership of
    178   // |new_proxy_config_service|.
    179   void ResetConfigService(ProxyConfigService* new_proxy_config_service);
    180 
    181   // Tells the resolver to purge any memory it does not need.
    182   void PurgeMemory();
    183 
    184 
    185   // Returns the last configuration fetched from ProxyConfigService.
    186   const ProxyConfig& fetched_config() {
    187     return fetched_config_;
    188   }
    189 
    190   // Returns the current configuration being used by ProxyConfigService.
    191   const ProxyConfig& config() {
    192     return config_;
    193   }
    194 
    195   // Returns the map of proxies which have been marked as "bad".
    196   const ProxyRetryInfoMap& proxy_retry_info() const {
    197     return proxy_retry_info_;
    198   }
    199 
    200   // Clears the list of bad proxy servers that has been cached.
    201   void ClearBadProxiesCache() {
    202     proxy_retry_info_.clear();
    203   }
    204 
    205   // Forces refetching the proxy configuration, and applying it.
    206   // This re-does everything from fetching the system configuration,
    207   // to downloading and testing the PAC files.
    208   void ForceReloadProxyConfig();
    209 
    210   // Same as CreateProxyServiceUsingV8ProxyResolver, except it uses system
    211   // libraries for evaluating the PAC script if available, otherwise skips
    212   // proxy autoconfig.
    213   static ProxyService* CreateUsingSystemProxyResolver(
    214       ProxyConfigService* proxy_config_service,
    215       size_t num_pac_threads,
    216       NetLog* net_log);
    217 
    218   // Creates a ProxyService without support for proxy autoconfig.
    219   static ProxyService* CreateWithoutProxyResolver(
    220       ProxyConfigService* proxy_config_service,
    221       NetLog* net_log);
    222 
    223   // Convenience methods that creates a proxy service using the
    224   // specified fixed settings.
    225   static ProxyService* CreateFixed(const ProxyConfig& pc);
    226   static ProxyService* CreateFixed(const std::string& proxy);
    227 
    228   // Creates a proxy service that uses a DIRECT connection for all requests.
    229   static ProxyService* CreateDirect();
    230   // |net_log|'s lifetime must exceed ProxyService.
    231   static ProxyService* CreateDirectWithNetLog(NetLog* net_log);
    232 
    233   // This method is used by tests to create a ProxyService that returns a
    234   // hardcoded proxy fallback list (|pac_string|) for every URL.
    235   //
    236   // |pac_string| is a list of proxy servers, in the format that a PAC script
    237   // would return it. For example, "PROXY foobar:99; SOCKS fml:2; DIRECT"
    238   static ProxyService* CreateFixedFromPacResult(const std::string& pac_string);
    239 
    240   // Creates a config service appropriate for this platform that fetches the
    241   // system proxy settings.
    242   static ProxyConfigService* CreateSystemProxyConfigService(
    243       base::SingleThreadTaskRunner* io_thread_task_runner,
    244       base::MessageLoop* file_loop);
    245 
    246   // This method should only be used by unit tests.
    247   void set_stall_proxy_auto_config_delay(base::TimeDelta delay) {
    248     stall_proxy_auto_config_delay_ = delay;
    249   }
    250 
    251   // This method should only be used by unit tests. Returns the previously
    252   // active policy.
    253   static const PacPollPolicy* set_pac_script_poll_policy(
    254       const PacPollPolicy* policy);
    255 
    256   // This method should only be used by unit tests. Creates an instance
    257   // of the default internal PacPollPolicy used by ProxyService.
    258   static scoped_ptr<PacPollPolicy> CreateDefaultPacPollPolicy();
    259 
    260  private:
    261   FRIEND_TEST_ALL_PREFIXES(ProxyServiceTest, UpdateConfigAfterFailedAutodetect);
    262   FRIEND_TEST_ALL_PREFIXES(ProxyServiceTest, UpdateConfigFromPACToDirect);
    263   friend class PacRequest;
    264   class InitProxyResolver;
    265   class ProxyScriptDeciderPoller;
    266 
    267   // TODO(eroman): change this to a std::set. Note that this requires updating
    268   // some tests in proxy_service_unittest.cc such as:
    269   //   ProxyServiceTest.InitialPACScriptDownload
    270   // which expects requests to finish in the order they were added.
    271   typedef std::vector<scoped_refptr<PacRequest> > PendingRequests;
    272 
    273   enum State {
    274     STATE_NONE,
    275     STATE_WAITING_FOR_PROXY_CONFIG,
    276     STATE_WAITING_FOR_INIT_PROXY_RESOLVER,
    277     STATE_READY,
    278   };
    279 
    280   // Resets all the variables associated with the current proxy configuration,
    281   // and rewinds the current state to |STATE_NONE|. Returns the previous value
    282   // of |current_state_|.  If |reset_fetched_config| is true then
    283   // |fetched_config_| will also be reset, otherwise it will be left as-is.
    284   // Resetting it means that we will have to re-fetch the configuration from
    285   // the ProxyConfigService later.
    286   State ResetProxyConfig(bool reset_fetched_config);
    287 
    288   // Retrieves the current proxy configuration from the ProxyConfigService, and
    289   // starts initializing for it.
    290   void ApplyProxyConfigIfAvailable();
    291 
    292   // Callback for when the proxy resolver has been initialized with a
    293   // PAC script.
    294   void OnInitProxyResolverComplete(int result);
    295 
    296   // Returns ERR_IO_PENDING if the request cannot be completed synchronously.
    297   // Otherwise it fills |result| with the proxy information for |url|.
    298   // Completing synchronously means we don't need to query ProxyResolver.
    299   int TryToCompleteSynchronously(const GURL& url, ProxyInfo* result);
    300 
    301   // Cancels all of the requests sent to the ProxyResolver. These will be
    302   // restarted when calling SetReady().
    303   void SuspendAllPendingRequests();
    304 
    305   // Advances the current state to |STATE_READY|, and resumes any pending
    306   // requests which had been stalled waiting for initialization to complete.
    307   void SetReady();
    308 
    309   // Returns true if |pending_requests_| contains |req|.
    310   bool ContainsPendingRequest(PacRequest* req);
    311 
    312   // Removes |req| from the list of pending requests.
    313   void RemovePendingRequest(PacRequest* req);
    314 
    315   // Called when proxy resolution has completed (either synchronously or
    316   // asynchronously). Handles logging the result, and cleaning out
    317   // bad entries from the results list.
    318   int DidFinishResolvingProxy(ProxyInfo* result,
    319                               int result_code,
    320                               const BoundNetLog& net_log);
    321 
    322   // Start initialization using |fetched_config_|.
    323   void InitializeUsingLastFetchedConfig();
    324 
    325   // Start the initialization skipping past the "decision" phase.
    326   void InitializeUsingDecidedConfig(
    327       int decider_result,
    328       ProxyResolverScriptData* script_data,
    329       const ProxyConfig& effective_config);
    330 
    331   // NetworkChangeNotifier::IPAddressObserver
    332   // When this is called, we re-fetch PAC scripts and re-run WPAD.
    333   virtual void OnIPAddressChanged() OVERRIDE;
    334 
    335   // NetworkChangeNotifier::DNSObserver
    336   // We respond as above.
    337   virtual void OnDNSChanged() OVERRIDE;
    338 
    339   // ProxyConfigService::Observer
    340   virtual void OnProxyConfigChanged(
    341       const ProxyConfig& config,
    342       ProxyConfigService::ConfigAvailability availability) OVERRIDE;
    343 
    344   scoped_ptr<ProxyConfigService> config_service_;
    345   scoped_ptr<ProxyResolver> resolver_;
    346 
    347   // We store the proxy configuration that was last fetched from the
    348   // ProxyConfigService, as well as the resulting "effective" configuration.
    349   // The effective configuration is what we condense the original fetched
    350   // settings to after testing the various automatic settings (auto-detect
    351   // and custom PAC url).
    352   ProxyConfig fetched_config_;
    353   ProxyConfig config_;
    354 
    355   // Increasing ID to give to the next ProxyConfig that we set.
    356   int next_config_id_;
    357 
    358   // The time when the proxy configuration was last read from the system.
    359   base::TimeTicks config_last_update_time_;
    360 
    361   // Map of the known bad proxies and the information about the retry time.
    362   ProxyRetryInfoMap proxy_retry_info_;
    363 
    364   // Set of pending/inprogress requests.
    365   PendingRequests pending_requests_;
    366 
    367   // The fetcher to use when downloading PAC scripts for the ProxyResolver.
    368   // This dependency can be NULL if our ProxyResolver has no need for
    369   // external PAC script fetching.
    370   scoped_ptr<ProxyScriptFetcher> proxy_script_fetcher_;
    371 
    372   // The fetcher to use when attempting to download the most appropriate PAC
    373   // script configured in DHCP, if any. Can be NULL if the ProxyResolver has
    374   // no need for DHCP PAC script fetching.
    375   scoped_ptr<DhcpProxyScriptFetcher> dhcp_proxy_script_fetcher_;
    376 
    377   // Helper to download the PAC script (wpad + custom) and apply fallback rules.
    378   //
    379   // Note that the declaration is important here: |proxy_script_fetcher_| and
    380   // |proxy_resolver_| must outlive |init_proxy_resolver_|.
    381   scoped_ptr<InitProxyResolver> init_proxy_resolver_;
    382 
    383   // Helper to poll the PAC script for changes.
    384   scoped_ptr<ProxyScriptDeciderPoller> script_poller_;
    385 
    386   State current_state_;
    387 
    388   // Either OK or an ERR_* value indicating that a permanent error (e.g.
    389   // failed to fetch the PAC script) prevents proxy resolution.
    390   int permanent_error_;
    391 
    392   // This is the log where any events generated by |init_proxy_resolver_| are
    393   // sent to.
    394   NetLog* net_log_;
    395 
    396   // The earliest time at which we should run any proxy auto-config. (Used to
    397   // stall re-configuration following an IP address change).
    398   base::TimeTicks stall_proxy_autoconfig_until_;
    399 
    400   // The amount of time to stall requests following IP address changes.
    401   base::TimeDelta stall_proxy_auto_config_delay_;
    402 
    403   DISALLOW_COPY_AND_ASSIGN(ProxyService);
    404 };
    405 
    406 // Wrapper for invoking methods on a ProxyService synchronously.
    407 class NET_EXPORT SyncProxyServiceHelper
    408     : public base::RefCountedThreadSafe<SyncProxyServiceHelper> {
    409  public:
    410   SyncProxyServiceHelper(base::MessageLoop* io_message_loop,
    411                          ProxyService* proxy_service);
    412 
    413   int ResolveProxy(const GURL& url,
    414                    ProxyInfo* proxy_info,
    415                    const BoundNetLog& net_log);
    416   int ReconsiderProxyAfterError(const GURL& url,
    417                                 ProxyInfo* proxy_info,
    418                                 const BoundNetLog& net_log);
    419 
    420  private:
    421   friend class base::RefCountedThreadSafe<SyncProxyServiceHelper>;
    422 
    423   virtual ~SyncProxyServiceHelper();
    424 
    425   void StartAsyncResolve(const GURL& url, const BoundNetLog& net_log);
    426   void StartAsyncReconsider(const GURL& url, const BoundNetLog& net_log);
    427 
    428   void OnCompletion(int result);
    429 
    430   base::MessageLoop* io_message_loop_;
    431   ProxyService* proxy_service_;
    432 
    433   base::WaitableEvent event_;
    434   CompletionCallback callback_;
    435   ProxyInfo proxy_info_;
    436   int result_;
    437 };
    438 
    439 }  // namespace net
    440 
    441 #endif  // NET_PROXY_PROXY_SERVICE_H_
    442