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.
5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_
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"
27 class HttpNetworkSession
;
28 class HttpResponseHeaders
;
30 class URLRequestContextGetter
;
33 namespace data_reduction_proxy
{
35 // The number of days of bandwidth usage statistics that are tracked.
36 const unsigned int kNumDaysInHistory
= 60;
38 // The number of days of bandwidth usage statistics that are presented.
39 const unsigned int kNumDaysInHistorySummary
= 30;
41 COMPILE_ASSERT(kNumDaysInHistorySummary
<= kNumDaysInHistory
,
42 DataReductionProxySettings_summary_too_long
);
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,
51 PROXY_STARTUP_STATE_COUNT
,
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,
63 // The probe failed for any other reason, and as a result, the proxy was
65 FAILED_PROXY_DISABLED
,
67 // The probe failed, but the proxy was already restricted.
68 FAILED_PROXY_ALREADY_DISABLED
,
70 // The probe succeeded, and as a result the proxy was restricted.
71 SUCCEEDED_PROXY_ENABLED
,
73 // The probe succeeded, but the proxy was already restricted.
74 SUCCEEDED_PROXY_ALREADY_ENABLED
,
76 // This must always be last.
77 PROBE_URL_FETCH_RESULT_COUNT
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
{
89 typedef std::vector
<long long> ContentLengthList
;
91 static bool IsProxyKeySetOnCommandLine();
93 DataReductionProxySettings(DataReductionProxyParams
* params
);
94 virtual ~DataReductionProxySettings();
96 DataReductionProxyParams
* params() const {
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(
106 PrefService
* local_state_prefs
,
107 net::URLRequestContextGetter
* url_request_context_getter
);
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(
116 PrefService
* local_state_prefs
,
117 net::URLRequestContextGetter
* url_request_context_getter
,
118 DataReductionProxyConfigurator
* configurator
);
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
);
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
);
130 // Returns true if the proxy is enabled.
131 bool IsDataReductionProxyEnabled();
133 // Returns true if the alternative proxy is enabled.
134 bool IsDataReductionProxyAlternativeEnabled() const;
136 // Returns true if the proxy is managed by an adminstrator's policy.
137 bool IsDataReductionProxyManaged();
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
142 void SetDataReductionProxyEnabled(bool enabled
);
144 // Enables or disables the alternative data reduction proxy configuration.
145 void SetDataReductionProxyAlternativeEnabled(bool enabled
);
147 // Returns the time in microseconds that the last update was made to the
148 // daily original and received content lengths.
149 int64
GetDataReductionLastUpdateTime();
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();
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
);
163 // Records that the data reduction proxy is unreachable or not.
164 void SetUnreachable(bool unreachable
);
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();
171 // Returns an vector containing the aggregate received HTTP content in the
172 // last |kNumDaysInHistory| days.
173 ContentLengthList
GetDailyReceivedContentLengths();
175 ContentLengthList
GetDailyContentLengths(const char* pref_name
);
177 // net::URLFetcherDelegate:
178 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
181 void InitPrefMembers();
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();
187 // Returns a fetcher to warm up the connection to the data reduction proxy.
188 // Virtual for testing.
189 virtual net::URLFetcher
* GetURLFetcherForWarmup();
191 // Virtualized for unit test support.
192 virtual PrefService
* GetOriginalProfilePrefs();
193 virtual PrefService
* GetLocalStatePrefs();
195 // Sets the proxy configs, enabling or disabling the proxy according to
196 // the value of |enabled| and |alternative_enabled|. Use the alternative
197 // configuration only if |enabled| and |alternative_enabled| are true. If
198 // |restricted| is true, only enable the fallback proxy. |at_startup| is true
199 // when this method is called from InitDataReductionProxySettings.
200 virtual void SetProxyConfigs(bool enabled
,
201 bool alternative_enabled
,
205 // Metrics method. Subclasses should override if they wish to provide
207 virtual void RecordDataReductionInit();
209 virtual void AddDefaultProxyBypassRules();
211 // Writes a warning to the log that is used in backend processing of
212 // customer feedback. Virtual so tests can mock it for verification.
213 virtual void LogProxyState(bool enabled
, bool restricted
, bool at_startup
);
215 // Virtualized for mocking. Records UMA containing the result of requesting
217 virtual void RecordProbeURLFetchResult(
218 data_reduction_proxy::ProbeURLFetchResult result
);
220 // Virtualized for mocking. Records UMA specifying whether the proxy was
221 // enabled or disabled at startup.
222 virtual void RecordStartupState(
223 data_reduction_proxy::ProxyStartupState state
);
225 // Virtualized for mocking. Returns the list of network interfaces in use.
226 virtual void GetNetworkList(net::NetworkInterfaceList
* interfaces
,
229 DataReductionProxyConfigurator
* configurator() {
230 return configurator_
;
233 // Reset params for tests.
234 void ResetParamsForTest(DataReductionProxyParams
* params
);
237 friend class DataReductionProxySettingsTestBase
;
238 friend class DataReductionProxySettingsTest
;
239 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
240 TestAuthenticationInit
);
241 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
242 TestAuthHashGeneration
);
243 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
244 TestAuthHashGenerationWithOriginSetViaSwitch
);
245 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
246 TestResetDataReductionStatistics
);
247 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
248 TestIsProxyEnabledOrManaged
);
249 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
251 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
252 TestGetDailyContentLengths
);
253 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
254 TestMaybeActivateDataReductionProxy
);
255 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
256 TestOnIPAddressChanged
);
257 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
258 TestOnProxyEnabledPrefChange
);
259 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
260 TestInitDataReductionProxyOn
);
261 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
262 TestInitDataReductionProxyOff
);
263 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
265 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
266 CheckInitMetricsWhenNotAllowed
);
267 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
268 TestSetProxyConfigs
);
269 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
270 TestSetProxyConfigsHoldback
);
272 // NetworkChangeNotifier::IPAddressObserver:
273 virtual void OnIPAddressChanged() OVERRIDE
;
275 void OnProxyEnabledPrefChange();
276 void OnProxyAlternativeEnabledPrefChange();
278 void ResetDataReductionStatistics();
280 void MaybeActivateDataReductionProxy(bool at_startup
);
282 // Requests the proxy probe URL, if one is set. If unable to do so, disables
283 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
285 void ProbeWhetherDataReductionProxyIsAvailable();
287 // Warms the connection to the data reduction proxy.
288 void WarmProxyConnection();
290 // Disables use of the data reduction proxy on VPNs. Returns true if the
291 // data reduction proxy has been disabled.
294 // Generic method to get a URL fetcher.
295 net::URLFetcher
* GetBaseURLFetcher(const GURL
& gurl
, int load_flags
);
298 bool restricted_by_carrier_
;
299 bool enabled_by_user_
;
300 bool disabled_on_vpn_
;
303 scoped_ptr
<net::URLFetcher
> fetcher_
;
304 scoped_ptr
<net::URLFetcher
> warmup_fetcher_
;
306 BooleanPrefMember spdy_proxy_auth_enabled_
;
307 BooleanPrefMember data_reduction_proxy_alternative_enabled_
;
310 PrefService
* local_state_prefs_
;
312 net::URLRequestContextGetter
* url_request_context_getter_
;
314 base::Callback
<void(bool)> on_data_reduction_proxy_enabled_
;
316 DataReductionProxyConfigurator
* configurator_
;
318 base::ThreadChecker thread_checker_
;
320 scoped_ptr
<DataReductionProxyParams
> params_
;
322 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings
);
325 } // namespace data_reduction_proxy
327 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_