1 // Copyright 2013 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 CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_
6 #define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/prefs/pref_member.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/url_request/url_fetcher_delegate.h"
21 class AuthChallengeInfo
;
24 class HttpNetworkSession
;
25 class HttpResponseHeaders
;
31 // The number of days of bandwidth usage statistics that are tracked.
32 const unsigned int kNumDaysInHistory
= 60;
34 // The number of days of bandwidth usage statistics that are presented.
35 const unsigned int kNumDaysInHistorySummary
= 30;
37 COMPILE_ASSERT(kNumDaysInHistorySummary
<= kNumDaysInHistory
,
38 DataReductionProxySettings_summary_too_long
);
40 // Values of the UMA DataReductionProxy.StartupState histogram.
41 // This enum must remain synchronized with DataReductionProxyStartupState
42 // in metrics/histograms/histograms.xml.
43 enum ProxyStartupState
{
44 PROXY_NOT_AVAILABLE
= 0,
47 PROXY_STARTUP_STATE_COUNT
,
50 // Values of the UMA DataReductionProxy.ProbeURL histogram.
51 // This enum must remain synchronized with
52 // DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml.
53 // TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so
54 // their names match the behavior they track.
55 enum ProbeURLFetchResult
{
56 // The probe failed because the internet was disconnected.
57 INTERNET_DISCONNECTED
= 0,
59 // The probe failed for any other reason, and as a result, the proxy was
61 FAILED_PROXY_DISABLED
,
63 // The probe failed, but the proxy was already restricted.
64 FAILED_PROXY_ALREADY_DISABLED
,
66 // THe probe succeeded, and as a result the proxy was restricted.
67 SUCCEEDED_PROXY_ENABLED
,
69 // The probe succeeded, but the proxy was already restricted.
70 SUCCEEDED_PROXY_ALREADY_ENABLED
,
72 // This must always be last.
73 PROBE_URL_FETCH_RESULT_COUNT
76 } // namespace spdyproxy
78 // Central point for configuring the data reduction proxy.
79 // This object lives on the UI thread and all of its methods are expected to
80 // be called from there.
81 // TODO(marq): Convert this to be a BrowserContextKeyedService with an
82 // associated factory class, and refactor the Java call sites accordingly.
83 class DataReductionProxySettings
84 : public net::URLFetcherDelegate
,
85 public net::NetworkChangeNotifier::IPAddressObserver
{
87 typedef std::vector
<long long> ContentLengthList
;
88 // TODO(marq): Consider instead using a std::pair instead of a vector.
89 typedef std::vector
<GURL
> DataReductionProxyList
;
91 DataReductionProxySettings();
92 virtual ~DataReductionProxySettings();
94 void InitDataReductionProxySettings();
96 // If proxy authentication is compiled in, pre-cache authentication
97 // keys for all configured proxies in |session|.
98 static void InitDataReductionProxySession(net::HttpNetworkSession
* session
);
100 // Add a host pattern to bypass. This should follow the same syntax used
101 // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix
102 // pattern, an IP literal, a CIDR block, or the magic string '<local>'.
103 // Bypass settings persist for the life of this object and are applied
104 // each time the proxy is enabled, but are not updated while it is enabled.
105 void AddHostPatternToBypass(const std::string
& pattern
);
107 // Add a URL pattern to bypass the proxy. The base implementation strips
108 // everything in |pattern| after the first single slash and then treats it
109 // as a hostname pattern. Subclasses may implement other semantics.
110 virtual void AddURLPatternToBypass(const std::string
& pattern
);
112 // Returns true if the data reduction proxy is allowed to be used on this
113 // instance of Chrome. This could return false, for example, if this instance
114 // is not part of the field trial, or if the proxy name is not configured
116 static bool IsDataReductionProxyAllowed();
118 // Returns true if a screen promoting the data reduction proxy is allowed to
119 // be shown. Logic that decides when to show the promo should check its
120 // availability. This would return false if not part of a separate field
121 // trial that governs the use of the promotion.
122 static bool IsDataReductionProxyPromoAllowed();
124 // Returns true if preconnect advisory hinting is enabled by command line
125 // flag or Finch trial.
126 static bool IsPreconnectHintingAllowed();
128 // Returns true if the Via header indicates that this request was fetched
129 // explicitly via the Chrome Proxy.
130 static bool WasFetchedViaProxy(const net::HttpResponseHeaders
* headers
);
132 // Returns the URL of the data reduction proxy.
133 static std::string
GetDataReductionProxyOrigin();
135 // Returns the URL of the fallback data reduction proxy.
136 static std::string
GetDataReductionProxyFallback();
138 // Returns a vector of GURLs for all configured proxies.
139 static DataReductionProxyList
GetDataReductionProxies();
141 // Returns true if |auth_info| represents an authentication challenge from
142 // a compatible, configured proxy.
143 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo
* auth_info
);
145 // Returns a UTF16 string suitable for use as an authentication token in
146 // response to the challenge represented by |auth_info|. If the token can't
147 // be correctly generated for |auth_info|, returns an empty UTF16 string.
148 base::string16
GetTokenForAuthChallenge(net::AuthChallengeInfo
* auth_info
);
150 // Returns true if the proxy is enabled.
151 bool IsDataReductionProxyEnabled();
153 // Returns true if the proxy is managed by an adminstrator's policy.
154 bool IsDataReductionProxyManaged();
156 // Enables or disables the data reduction proxy. If a probe URL is available,
157 // and a probe request fails at some point, the proxy won't be used until a
159 void SetDataReductionProxyEnabled(bool enabled
);
161 // Returns the time in microseconds that the last update was made to the
162 // daily original and received content lengths.
163 int64
GetDataReductionLastUpdateTime();
165 // Returns a vector containing the total size of all HTTP content that was
166 // received over the last |kNumDaysInHistory| before any compression by the
167 // data reduction proxy. Each element in the vector contains one day of data.
168 ContentLengthList
GetDailyOriginalContentLengths();
170 // Returns an vector containing the aggregate received HTTP content in the
171 // last |kNumDaysInHistory| days.
172 ContentLengthList
GetDailyReceivedContentLengths();
174 // net::URLFetcherDelegate:
175 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
178 void InitPrefMembers();
180 virtual net::URLFetcher
* GetURLFetcher();
182 // Virtualized for unit test support.
183 virtual PrefService
* GetOriginalProfilePrefs();
184 virtual PrefService
* GetLocalStatePrefs();
186 void GetContentLengths(unsigned int days
,
187 int64
* original_content_length
,
188 int64
* received_content_length
,
189 int64
* last_update_time
);
190 ContentLengthList
GetDailyContentLengths(const char* pref_name
);
192 // Sets the proxy configs, enabling or disabling the proxy according to
193 // the value of |enabled|. If |restricted| is true, only enable the fallback
194 // proxy. |at_startup| is true when this method is called from
195 // InitDataReductionProxySettings.
196 virtual void SetProxyConfigs(bool enabled
, bool restricted
, bool at_startup
);
198 // Metrics methods. Subclasses should override if they wish to provide
199 // alternate methods.
200 virtual void RecordDataReductionInit();
202 virtual void AddDefaultProxyBypassRules();
204 // Writes a warning to the log that is used in backend processing of
205 // customer feedback. Virtual so tests can mock it for verification.
206 virtual void LogProxyState(bool enabled
, bool restricted
, bool at_startup
);
208 // Accessor for unit tests.
209 std::vector
<std::string
> BypassRules() { return bypass_rules_
;}
211 // Virtualized for mocking
212 virtual void RecordProbeURLFetchResult(spdyproxy::ProbeURLFetchResult result
);
213 virtual void RecordStartupState(spdyproxy::ProxyStartupState state
);
216 friend class DataReductionProxySettingsTestBase
;
217 friend class DataReductionProxySettingsTest
;
218 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
219 TestAuthenticationInit
);
220 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
221 TestAuthHashGeneration
);
222 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
223 TestAuthHashGenerationWithOriginSetViaSwitch
);
224 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
225 TestResetDataReductionStatistics
);
226 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
227 TestIsProxyEnabledOrManaged
);
228 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
230 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
231 TestGetDailyContentLengths
);
232 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
233 TestMaybeActivateDataReductionProxy
);
234 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
235 TestOnIPAddressChanged
);
236 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
237 TestOnProxyEnabledPrefChange
);
238 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
239 TestInitDataReductionProxyOn
);
240 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
241 TestInitDataReductionProxyOff
);
242 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest
,
245 // NetworkChangeNotifier::IPAddressObserver:
246 virtual void OnIPAddressChanged() OVERRIDE
;
248 // Underlying implementation of InitDataReductionProxySession(), factored
249 // out to be testable without creating a full HttpNetworkSession.
250 static void InitDataReductionAuthentication(net::HttpAuthCache
* auth_cache
);
252 void OnProxyEnabledPrefChange();
254 void ResetDataReductionStatistics();
256 void MaybeActivateDataReductionProxy(bool at_startup
);
258 // Requests the proxy probe URL, if one is set. If unable to do so, disables
259 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
261 void ProbeWhetherDataReductionProxyIsAvailable();
262 std::string
GetProxyCheckURL();
264 // Returns a UTF16 string that's the hash of the configured authentication
265 // key and |salt|. Returns an empty UTF16 string if no key is configured or
266 // the data reduction proxy feature isn't available.
267 static base::string16
AuthHashForSalt(int64 salt
);
269 std::vector
<std::string
> bypass_rules_
;
271 bool restricted_by_carrier_
;
272 bool enabled_by_user_
;
274 scoped_ptr
<net::URLFetcher
> fetcher_
;
275 BooleanPrefMember spdy_proxy_auth_enabled_
;
277 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings
);
280 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_