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_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_
6 #define COMPONENTS_DATA_REDUCTION_PROXY_CORE_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/core/browser/data_reduction_proxy_configurator.h"
18 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_statistics_prefs.h"
19 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_params.h"
20 #include "net/base/net_util.h"
21 #include "net/base/network_change_notifier.h"
22 #include "net/url_request/url_fetcher_delegate.h"
28 class HttpNetworkSession;
29 class HttpResponseHeaders;
31 class URLRequestContextGetter;
34 namespace data_reduction_proxy {
36 // The number of days of bandwidth usage statistics that are tracked.
37 const unsigned int kNumDaysInHistory = 60;
39 // The number of days of bandwidth usage statistics that are presented.
40 const unsigned int kNumDaysInHistorySummary = 30;
42 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory,
43 DataReductionProxySettings_summary_too_long);
45 // Values of the UMA DataReductionProxy.StartupState histogram.
46 // This enum must remain synchronized with DataReductionProxyStartupState
47 // in metrics/histograms/histograms.xml.
48 enum ProxyStartupState {
49 PROXY_NOT_AVAILABLE = 0,
52 PROXY_STARTUP_STATE_COUNT,
55 // Values of the UMA DataReductionProxy.ProbeURL histogram.
56 // This enum must remain synchronized with
57 // DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml.
58 // TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so
59 // their names match the behavior they track.
60 enum ProbeURLFetchResult {
61 // The probe failed because the Internet was disconnected.
62 INTERNET_DISCONNECTED = 0,
64 // The probe failed for any other reason, and as a result, the proxy was
66 FAILED_PROXY_DISABLED,
68 // The probe failed, but the proxy was already restricted.
69 FAILED_PROXY_ALREADY_DISABLED,
71 // The probe succeeded, and as a result the proxy was restricted.
72 SUCCEEDED_PROXY_ENABLED,
74 // The probe succeeded, but the proxy was already restricted.
75 SUCCEEDED_PROXY_ALREADY_ENABLED,
77 // This must always be last.
78 PROBE_URL_FETCH_RESULT_COUNT
81 // Central point for configuring the data reduction proxy.
82 // This object lives on the UI thread and all of its methods are expected to
83 // be called from there.
84 // TODO(marq): Convert this to be a KeyedService with an
85 // associated factory class, and refactor the Java call sites accordingly.
86 class DataReductionProxySettings
87 : public net::URLFetcherDelegate,
88 public net::NetworkChangeNotifier::IPAddressObserver {
90 typedef std::vector<long long> ContentLengthList;
92 static bool IsProxyKeySetOnCommandLine();
94 DataReductionProxySettings(DataReductionProxyParams* params);
95 ~DataReductionProxySettings() override;
97 DataReductionProxyParams* params() const {
101 // Initializes the data reduction proxy with profile and local state prefs,
102 // and a |UrlRequestContextGetter| for canary probes. The caller must ensure
103 // that all parameters remain alive for the lifetime of the
104 // |DataReductionProxySettings| instance.
105 void InitDataReductionProxySettings(
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 net::URLRequestContextGetter* url_request_context_getter,
117 DataReductionProxyConfigurator* configurator);
119 // Sets the |statistics_prefs_| to be used for data reduction proxy pref reads
121 void SetDataReductionProxyStatisticsPrefs(
122 DataReductionProxyStatisticsPrefs* statistics_prefs);
124 // Sets the |on_data_reduction_proxy_enabled_| callback and runs to register
125 // the DataReductionProxyEnabled synthetic field trial.
126 void SetOnDataReductionEnabledCallback(
127 const base::Callback<void(bool)>& on_data_reduction_proxy_enabled);
129 // Sets the logic the embedder uses to set the networking configuration that
130 // causes traffic to be proxied.
131 void SetProxyConfigurator(
132 DataReductionProxyConfigurator* configurator);
134 // Returns true if the proxy is enabled.
135 bool IsDataReductionProxyEnabled();
137 // Returns true if the alternative proxy is enabled.
138 bool IsDataReductionProxyAlternativeEnabled() const;
140 // Returns true if the proxy is managed by an adminstrator's policy.
141 bool IsDataReductionProxyManaged();
143 // Enables or disables the data reduction proxy. If a probe URL is available,
144 // and a probe request fails at some point, the proxy won't be used until a
146 void SetDataReductionProxyEnabled(bool enabled);
148 // Enables or disables the alternative data reduction proxy configuration.
149 void SetDataReductionProxyAlternativeEnabled(bool enabled);
151 // Returns the time in microseconds that the last update was made to the
152 // daily original and received content lengths.
153 int64 GetDataReductionLastUpdateTime();
155 // Returns a vector containing the total size of all HTTP content that was
156 // received over the last |kNumDaysInHistory| before any compression by the
157 // data reduction proxy. Each element in the vector contains one day of data.
158 ContentLengthList GetDailyOriginalContentLengths();
160 // Returns aggregate received and original content lengths over the specified
161 // number of days, as well as the time these stats were last updated.
162 void GetContentLengths(unsigned int days,
163 int64* original_content_length,
164 int64* received_content_length,
165 int64* last_update_time);
167 // Records that the data reduction proxy is unreachable or not.
168 void SetUnreachable(bool unreachable);
170 // Returns whether the data reduction proxy is unreachable. Returns true
171 // if no request has successfully completed through proxy, even though atleast
172 // some of them should have.
173 bool IsDataReductionProxyUnreachable();
175 // Returns an vector containing the aggregate received HTTP content in the
176 // last |kNumDaysInHistory| days.
177 ContentLengthList GetDailyReceivedContentLengths();
179 ContentLengthList GetDailyContentLengths(const char* pref_name);
181 // net::URLFetcherDelegate:
182 void OnURLFetchComplete(const net::URLFetcher* source) override;
184 // Configures data reduction proxy and makes a request to the probe URL to
185 // determine server availability. |at_startup| is true when this method is
186 // called in response to creating or loading a new profile.
187 void MaybeActivateDataReductionProxy(bool at_startup);
190 void InitPrefMembers();
192 // Returns a fetcher for the probe to check if OK for the proxy to use SPDY.
193 // Virtual for testing.
194 virtual net::URLFetcher* GetURLFetcherForAvailabilityCheck();
196 // Virtualized for unit test support.
197 virtual PrefService* GetOriginalProfilePrefs();
199 // Sets the proxy configs, enabling or disabling the proxy according to
200 // the value of |enabled| and |alternative_enabled|. Use the alternative
201 // configuration only if |enabled| and |alternative_enabled| are true. If
202 // |restricted| is true, only enable the fallback proxy. |at_startup| is true
203 // when this method is called from InitDataReductionProxySettings.
204 virtual void SetProxyConfigs(bool enabled,
205 bool alternative_enabled,
209 // Metrics method. Subclasses should override if they wish to provide
211 virtual void RecordDataReductionInit();
213 virtual void AddDefaultProxyBypassRules();
215 // Writes a warning to the log that is used in backend processing of
216 // customer feedback. Virtual so tests can mock it for verification.
217 virtual void LogProxyState(bool enabled, bool restricted, bool at_startup);
219 // Virtualized for mocking. Records UMA containing the result of requesting
221 virtual void RecordProbeURLFetchResult(
222 data_reduction_proxy::ProbeURLFetchResult result);
224 // Virtualized for mocking. Records UMA specifying whether the proxy was
225 // enabled or disabled at startup.
226 virtual void RecordStartupState(
227 data_reduction_proxy::ProxyStartupState state);
229 // Virtualized for mocking. Returns the list of network interfaces in use.
230 virtual void GetNetworkList(net::NetworkInterfaceList* interfaces,
233 DataReductionProxyConfigurator* configurator() {
234 return configurator_;
237 // Reset params for tests.
238 void ResetParamsForTest(DataReductionProxyParams* params);
241 friend class DataReductionProxySettingsTestBase;
242 friend class DataReductionProxySettingsTest;
243 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
244 TestAuthenticationInit);
245 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
246 TestAuthHashGeneration);
247 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
248 TestAuthHashGenerationWithOriginSetViaSwitch);
249 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
250 TestResetDataReductionStatistics);
251 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
252 TestIsProxyEnabledOrManaged);
253 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
255 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
256 TestGetDailyContentLengths);
257 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
258 TestMaybeActivateDataReductionProxy);
259 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
260 TestOnIPAddressChanged);
261 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
262 TestOnProxyEnabledPrefChange);
263 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
264 TestInitDataReductionProxyOn);
265 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
266 TestInitDataReductionProxyOff);
267 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
269 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
270 CheckInitMetricsWhenNotAllowed);
271 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
272 TestSetProxyConfigs);
273 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
274 TestSetProxyConfigsHoldback);
276 // NetworkChangeNotifier::IPAddressObserver:
277 void OnIPAddressChanged() override;
279 void OnProxyEnabledPrefChange();
280 void OnProxyAlternativeEnabledPrefChange();
282 void ResetDataReductionStatistics();
284 // Requests the proxy probe URL, if one is set. If unable to do so, disables
285 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
287 void ProbeWhetherDataReductionProxyIsAvailable();
289 // Disables use of the data reduction proxy on VPNs. Returns true if the
290 // data reduction proxy has been disabled.
293 // Generic method to get a URL fetcher.
294 net::URLFetcher* GetBaseURLFetcher(const GURL& gurl, int load_flags);
297 bool restricted_by_carrier_;
298 bool enabled_by_user_;
299 bool disabled_on_vpn_;
302 scoped_ptr<net::URLFetcher> fetcher_;
304 BooleanPrefMember spdy_proxy_auth_enabled_;
305 BooleanPrefMember data_reduction_proxy_alternative_enabled_;
308 DataReductionProxyStatisticsPrefs* statistics_prefs_;
310 net::URLRequestContextGetter* url_request_context_getter_;
312 base::Callback<void(bool)> on_data_reduction_proxy_enabled_;
314 DataReductionProxyConfigurator* configurator_;
316 base::ThreadChecker thread_checker_;
318 scoped_ptr<DataReductionProxyParams> params_;
320 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings);
323 } // namespace data_reduction_proxy
325 #endif // COMPONENTS_DATA_REDUCTION_PROXY_CORE_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_