Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / components / data_reduction_proxy / browser / data_reduction_proxy_settings.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_SETTINGS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/prefs/pref_member.h"
16 #include "base/threading/thread_checker.h"
17 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h"
18 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
19 #include "net/base/net_util.h"
20 #include "net/base/network_change_notifier.h"
21 #include "net/url_request/url_fetcher_delegate.h"
22
23 class PrefService;
24
25 namespace net {
26 class HostPortPair;
27 class HttpNetworkSession;
28 class HttpResponseHeaders;
29 class URLFetcher;
30 class URLRequestContextGetter;
31 }
32
33 namespace data_reduction_proxy {
34
35 // The number of days of bandwidth usage statistics that are tracked.
36 const unsigned int kNumDaysInHistory = 60;
37
38 // The number of days of bandwidth usage statistics that are presented.
39 const unsigned int kNumDaysInHistorySummary = 30;
40
41 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory,
42                DataReductionProxySettings_summary_too_long);
43
44 // Values of the UMA DataReductionProxy.StartupState histogram.
45 // This enum must remain synchronized with DataReductionProxyStartupState
46 // in metrics/histograms/histograms.xml.
47 enum ProxyStartupState {
48   PROXY_NOT_AVAILABLE = 0,
49   PROXY_DISABLED,
50   PROXY_ENABLED,
51   PROXY_STARTUP_STATE_COUNT,
52 };
53
54 // Values of the UMA DataReductionProxy.ProbeURL histogram.
55 // This enum must remain synchronized with
56 // DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml.
57 // TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so
58 //     their names match the behavior they track.
59 enum ProbeURLFetchResult {
60   // The probe failed because the Internet was disconnected.
61   INTERNET_DISCONNECTED = 0,
62
63   // The probe failed for any other reason, and as a result, the proxy was
64   // disabled.
65   FAILED_PROXY_DISABLED,
66
67   // The probe failed, but the proxy was already restricted.
68   FAILED_PROXY_ALREADY_DISABLED,
69
70   // The probe succeeded, and as a result the proxy was restricted.
71   SUCCEEDED_PROXY_ENABLED,
72
73   // The probe succeeded, but the proxy was already restricted.
74   SUCCEEDED_PROXY_ALREADY_ENABLED,
75
76   // This must always be last.
77   PROBE_URL_FETCH_RESULT_COUNT
78 };
79
80 // Central point for configuring the data reduction proxy.
81 // This object lives on the UI thread and all of its methods are expected to
82 // be called from there.
83 // TODO(marq): Convert this to be a KeyedService with an
84 // associated factory class, and refactor the Java call sites accordingly.
85 class DataReductionProxySettings
86     : public net::URLFetcherDelegate,
87       public net::NetworkChangeNotifier::IPAddressObserver {
88  public:
89   typedef std::vector<long long> ContentLengthList;
90
91   static bool IsProxyKeySetOnCommandLine();
92
93   DataReductionProxySettings(DataReductionProxyParams* params);
94   virtual ~DataReductionProxySettings();
95
96   DataReductionProxyParams* params() const {
97     return params_.get();
98   }
99
100   // Initializes the data reduction proxy with profile and local state prefs,
101   // and a |UrlRequestContextGetter| for canary probes. The caller must ensure
102   // that all parameters remain alive for the lifetime of the
103   // |DataReductionProxySettings| instance.
104   void InitDataReductionProxySettings(
105       PrefService* prefs,
106       PrefService* local_state_prefs,
107       net::URLRequestContextGetter* url_request_context_getter);
108
109   // Initializes the data reduction proxy with profile and local state prefs,
110   // a |UrlRequestContextGetter| for canary probes, and a proxy configurator.
111   // The caller must ensure that all parameters remain alive for the lifetime of
112   // the |DataReductionProxySettings| instance.
113   // TODO(marq): Remove when iOS supports the new interface above.
114   void InitDataReductionProxySettings(
115       PrefService* prefs,
116       PrefService* local_state_prefs,
117       net::URLRequestContextGetter* url_request_context_getter,
118       DataReductionProxyConfigurator* configurator);
119
120   // Sets the |on_data_reduction_proxy_enabled_| callback and runs to register
121   // the DataReductionProxyEnabled synthetic field trial.
122   void SetOnDataReductionEnabledCallback(
123       const base::Callback<void(bool)>& on_data_reduction_proxy_enabled);
124
125   // Sets the logic the embedder uses to set the networking configuration that
126   // causes traffic to be proxied.
127   void SetProxyConfigurator(
128       DataReductionProxyConfigurator* configurator);
129
130   // Returns true if the proxy is enabled.
131   bool IsDataReductionProxyEnabled();
132
133   // Returns true if the alternative proxy is enabled.
134   bool IsDataReductionProxyAlternativeEnabled() const;
135
136   // Returns true if the proxy is managed by an adminstrator's policy.
137   bool IsDataReductionProxyManaged();
138
139   // Enables or disables the data reduction proxy. If a probe URL is available,
140   // and a probe request fails at some point, the proxy won't be used until a
141   // probe succeeds.
142   void SetDataReductionProxyEnabled(bool enabled);
143
144   // Enables or disables the alternative data reduction proxy configuration.
145   void SetDataReductionProxyAlternativeEnabled(bool enabled);
146
147   // Returns the time in microseconds that the last update was made to the
148   // daily original and received content lengths.
149   int64 GetDataReductionLastUpdateTime();
150
151   // Returns a vector containing the total size of all HTTP content that was
152   // received over the last |kNumDaysInHistory| before any compression by the
153   // data reduction proxy. Each element in the vector contains one day of data.
154   ContentLengthList GetDailyOriginalContentLengths();
155
156   // Returns aggregate received and original content lengths over the specified
157   // number of days, as well as the time these stats were last updated.
158   void GetContentLengths(unsigned int days,
159                          int64* original_content_length,
160                          int64* received_content_length,
161                          int64* last_update_time);
162
163   // Records that the data reduction proxy is unreachable or not.
164   void SetUnreachable(bool unreachable);
165
166   // Returns whether the data reduction proxy is unreachable. Returns true
167   // if no request has successfully completed through proxy, even though atleast
168   // some of them should have.
169   bool IsDataReductionProxyUnreachable();
170
171   // Returns an vector containing the aggregate received HTTP content in the
172   // last |kNumDaysInHistory| days.
173   ContentLengthList GetDailyReceivedContentLengths();
174
175   ContentLengthList GetDailyContentLengths(const char* pref_name);
176
177   // net::URLFetcherDelegate:
178   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
179
180  protected:
181   void InitPrefMembers();
182
183   // Returns a fetcher for the probe to check if OK for the proxy to use SPDY.
184   // Virtual for testing.
185   virtual net::URLFetcher* GetURLFetcherForAvailabilityCheck();
186
187   // Virtualized for unit test support.
188   virtual PrefService* GetOriginalProfilePrefs();
189   virtual PrefService* GetLocalStatePrefs();
190
191   // Sets the proxy configs, enabling or disabling the proxy according to
192   // the value of |enabled| and |alternative_enabled|. Use the alternative
193   // configuration only if |enabled| and |alternative_enabled| are true. If
194   // |restricted| is true, only enable the fallback proxy. |at_startup| is true
195   // when this method is called from InitDataReductionProxySettings.
196   virtual void SetProxyConfigs(bool enabled,
197                                bool alternative_enabled,
198                                bool restricted,
199                                bool at_startup);
200
201   // Metrics method. Subclasses should override if they wish to provide
202   // alternatives.
203   virtual void RecordDataReductionInit();
204
205   virtual void AddDefaultProxyBypassRules();
206
207   // Writes a warning to the log that is used in backend processing of
208   // customer feedback. Virtual so tests can mock it for verification.
209   virtual void LogProxyState(bool enabled, bool restricted, bool at_startup);
210
211   // Virtualized for mocking. Records UMA containing the result of requesting
212   // the probe URL.
213   virtual void RecordProbeURLFetchResult(
214       data_reduction_proxy::ProbeURLFetchResult result);
215
216   // Virtualized for mocking. Records UMA specifying whether the proxy was
217   // enabled or disabled at startup.
218   virtual void RecordStartupState(
219       data_reduction_proxy::ProxyStartupState state);
220
221   // Virtualized for mocking. Returns the list of network interfaces in use.
222   virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
223                               int policy);
224
225   DataReductionProxyConfigurator* configurator() {
226     return configurator_;
227   }
228
229   // Reset params for tests.
230   void ResetParamsForTest(DataReductionProxyParams* params);
231
232  private:
233   friend class DataReductionProxySettingsTestBase;
234   friend class DataReductionProxySettingsTest;
235   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
236                            TestAuthenticationInit);
237   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
238                            TestAuthHashGeneration);
239   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
240                            TestAuthHashGenerationWithOriginSetViaSwitch);
241   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
242                            TestResetDataReductionStatistics);
243   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
244                            TestIsProxyEnabledOrManaged);
245   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
246                            TestContentLengths);
247   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
248                            TestGetDailyContentLengths);
249   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
250                            TestMaybeActivateDataReductionProxy);
251   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
252                            TestOnIPAddressChanged);
253   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
254                            TestOnProxyEnabledPrefChange);
255   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
256                            TestInitDataReductionProxyOn);
257   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
258                            TestInitDataReductionProxyOff);
259   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
260                            TestBypassList);
261   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
262                            CheckInitMetricsWhenNotAllowed);
263   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
264                            TestSetProxyConfigs);
265   FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
266                            TestSetProxyConfigsHoldback);
267
268   // NetworkChangeNotifier::IPAddressObserver:
269   virtual void OnIPAddressChanged() OVERRIDE;
270
271   void OnProxyEnabledPrefChange();
272   void OnProxyAlternativeEnabledPrefChange();
273
274   void ResetDataReductionStatistics();
275
276   void MaybeActivateDataReductionProxy(bool at_startup);
277
278   // Requests the proxy probe URL, if one is set.  If unable to do so, disables
279   // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
280   // failure.
281   void ProbeWhetherDataReductionProxyIsAvailable();
282
283   // Disables use of the data reduction proxy on VPNs. Returns true if the
284   // data reduction proxy has been disabled.
285   bool DisableIfVPN();
286
287   // Generic method to get a URL fetcher.
288   net::URLFetcher* GetBaseURLFetcher(const GURL& gurl, int load_flags);
289
290   std::string key_;
291   bool restricted_by_carrier_;
292   bool enabled_by_user_;
293   bool disabled_on_vpn_;
294   bool unreachable_;
295
296   scoped_ptr<net::URLFetcher> fetcher_;
297
298   BooleanPrefMember spdy_proxy_auth_enabled_;
299   BooleanPrefMember data_reduction_proxy_alternative_enabled_;
300
301   PrefService* prefs_;
302   PrefService* local_state_prefs_;
303
304   net::URLRequestContextGetter* url_request_context_getter_;
305
306   base::Callback<void(bool)> on_data_reduction_proxy_enabled_;
307
308   DataReductionProxyConfigurator* configurator_;
309
310   base::ThreadChecker thread_checker_;
311
312   scoped_ptr<DataReductionProxyParams> params_;
313
314   DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings);
315 };
316
317 }  // namespace data_reduction_proxy
318
319 #endif  // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_