Home | History | Annotate | Download | only in browser
      1 // Copyright 2014 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 COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_CONFIG_SERVICE_H_
      6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_CONFIG_SERVICE_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/callback.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "base/observer_list.h"
     16 #include "base/task_runner.h"
     17 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h"
     18 #include "net/proxy/proxy_config.h"
     19 #include "net/proxy/proxy_config_service.h"
     20 
     21 class PrefService;
     22 
     23 namespace net {
     24 class ProxyConfig;
     25 }
     26 
     27 namespace data_reduction_proxy {
     28 
     29 // A net::ProxyConfigService implementation that applies data reduction proxy
     30 // settings as overrides to the proxy configuration determined by a
     31 // baseline delegate ProxyConfigService.
     32 class DataReductionProxyConfigService
     33     : public net::ProxyConfigService,
     34       public net::ProxyConfigService::Observer {
     35  public:
     36   // Takes ownership of the passed |base_service|.
     37   DataReductionProxyConfigService(
     38       scoped_ptr<net::ProxyConfigService> base_service);
     39   virtual ~DataReductionProxyConfigService();
     40 
     41   // ProxyConfigService implementation:
     42   virtual void AddObserver(
     43       net::ProxyConfigService::Observer* observer) OVERRIDE;
     44   virtual void RemoveObserver(
     45       net::ProxyConfigService::Observer* observer) OVERRIDE;
     46   virtual ConfigAvailability GetLatestProxyConfig(
     47       net::ProxyConfig* config) OVERRIDE;
     48   virtual void OnLazyPoll() OVERRIDE;
     49 
     50   // Method on IO thread that receives the data reduction proxy settings pushed
     51   // from DataReductionProxyConfiguratorImpl.
     52   void UpdateProxyConfig(bool enabled,
     53                          const net::ProxyConfig& config);
     54 
     55  private:
     56   friend class DataReductionProxyConfigServiceTest;
     57 
     58   // ProxyConfigService::Observer implementation:
     59   virtual void OnProxyConfigChanged(const net::ProxyConfig& config,
     60                                     ConfigAvailability availability) OVERRIDE;
     61 
     62   // Makes sure that the observer registration with the base service is set up.
     63   void RegisterObserver();
     64 
     65   scoped_ptr<net::ProxyConfigService> base_service_;
     66   ObserverList<net::ProxyConfigService::Observer, true> observers_;
     67 
     68   // Configuration as defined by the data reduction proxy.
     69   net::ProxyConfig config_;
     70 
     71   // Flag that indicates that a PrefProxyConfigTracker needs to inform us
     72   // about a proxy configuration before we may return any configuration.
     73   bool config_read_pending_;
     74 
     75   // Indicates whether the base service registration is done.
     76   bool registered_observer_;
     77 
     78   // The data reduction proxy is enabled.
     79   bool enabled_;
     80 
     81   // Use of the data reduction proxy is restricted to HTTP proxying only.
     82   bool restricted_;
     83 
     84   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyConfigService);
     85 };
     86 
     87 // A data_reduction_proxy::DataReductionProxyConfigurator implementation that
     88 // tracks changes to the data reduction proxy configuration and notifies an
     89 // associated DataReductionProxyConfigService. Configuration changes include
     90 // adding URL and host patterns to bypass and enabling and disabling use of the
     91 // proxy.
     92 class DataReductionProxyConfigTracker : public DataReductionProxyConfigurator {
     93  public:
     94   DataReductionProxyConfigTracker(
     95       base::Callback<void(bool, const net::ProxyConfig&)> update_proxy_config,
     96       base::TaskRunner* task_runner);
     97   virtual ~DataReductionProxyConfigTracker();
     98 
     99   virtual void Enable(bool primary_restricted,
    100                       bool fallback_restricted,
    101                       const std::string& primary_origin,
    102                       const std::string& fallback_origin,
    103                       const std::string& ssl_origin) OVERRIDE;
    104   virtual void Disable() OVERRIDE;
    105   virtual void AddHostPatternToBypass(const std::string& pattern) OVERRIDE;
    106   virtual void AddURLPatternToBypass(const std::string& pattern) OVERRIDE;
    107 
    108  private:
    109   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigServiceTest,
    110                            TrackerEnable);
    111   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigServiceTest,
    112                            TrackerRestricted);
    113   FRIEND_TEST_ALL_PREFIXES(DataReductionProxyConfigServiceTest,
    114                            TrackerBypassList);
    115 
    116   void UpdateProxyConfigOnIOThread(bool enabled,
    117                                    const net::ProxyConfig& config);
    118 
    119   base::Callback<void(bool, const net::ProxyConfig&)> update_proxy_config_;
    120   std::vector<std::string> bypass_rules_;
    121   scoped_refptr<base::TaskRunner> task_runner_;
    122 
    123   DISALLOW_COPY_AND_ASSIGN(DataReductionProxyConfigTracker);
    124 };
    125 
    126 }  //  namespace data_reduction_proxy
    127 
    128 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_CONFIG_SERVICE_H_
    129