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