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;
30 // The number of days of bandwidth usage statistics that are tracked.
31 const unsigned int kNumDaysInHistory = 60;
33 // The number of days of bandwidth usage statistics that are presented.
34 const unsigned int kNumDaysInHistorySummary = 30;
36 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory,
37 DataReductionProxySettings_summary_too_long);
39 } // namespace spdyproxy
41 // Central point for configuring the data reduction proxy.
42 // This object lives on the UI thread and all of its methods are expected to
43 // be called from there.
44 // TODO(marq): Convert this to be a BrowserContextKeyedService with an
45 // associated factory class, and refactor the Java call sites accordingly.
46 class DataReductionProxySettings
47 : public net::URLFetcherDelegate,
48 public net::NetworkChangeNotifier::IPAddressObserver {
50 typedef std::vector<long long> ContentLengthList;
51 // TODO(marq): Consider instead using a std::pair instead of a vector.
52 typedef std::vector<GURL> DataReductionProxyList;
54 DataReductionProxySettings();
55 virtual ~DataReductionProxySettings();
57 void InitDataReductionProxySettings();
59 // If proxy authentication is compiled in, pre-cache authentication
60 // keys for all configured proxies in |session|.
61 static void InitDataReductionProxySession(net::HttpNetworkSession* session);
63 // Add a host pattern to bypass. This should follow the same syntax used
64 // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix
65 // pattern, an IP literal, a CIDR block, or the magic string '<local>'.
66 // Bypass settings persist for the life of this object and are applied
67 // each time the proxy is enabled, but are not updated while it is enabled.
68 void AddHostPatternToBypass(const std::string& pattern);
70 // Add a URL pattern to bypass the proxy. The base implementation strips
71 // everything in |pattern| after the first single slash and then treats it
72 // as a hostname pattern. Subclasses may implement other semantics.
73 virtual void AddURLPatternToBypass(const std::string& pattern);
75 // Returns true if the data reduction proxy is allowed to be used on this
76 // instance of Chrome. This could return false, for example, if this instance
77 // is not part of the field trial, or if the proxy name is not configured
79 static bool IsDataReductionProxyAllowed();
81 // Returns true if a screen promoting the data reduction proxy is allowed to
82 // be shown. Logic that decides when to show the promo should check its
83 // availability. This would return false if not part of a separate field
84 // trial that governs the use of the promotion.
85 static bool IsDataReductionProxyPromoAllowed();
87 // Returns the URL of the data reduction proxy.
88 static std::string GetDataReductionProxyOrigin();
90 // Returns the URL of the fallback data reduction proxy.
91 static std::string GetDataReductionProxyFallback();
93 // Returns a vector of GURLs for all configured proxies.
94 static DataReductionProxyList GetDataReductionProxies();
96 // Returns true if |auth_info| represents an authentication challenge from
97 // a compatible, configured proxy.
98 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info);
100 // Returns a UTF16 string suitable for use as an authentication token in
101 // response to the challenge represented by |auth_info|. If the token can't
102 // be correctly generated for |auth_info|, returns an empty UTF16 string.
103 base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info);
105 // Returns true if the proxy is enabled.
106 bool IsDataReductionProxyEnabled();
108 // Returns true if the proxy is managed by an adminstrator's policy.
109 bool IsDataReductionProxyManaged();
111 // Enables or disables the data reduction proxy. If a probe URL is available,
112 // and a probe request fails at some point, the proxy won't be used until a
114 void SetDataReductionProxyEnabled(bool enabled);
116 // Returns the time in microseconds that the last update was made to the
117 // daily original and received content lengths.
118 int64 GetDataReductionLastUpdateTime();
120 // Returns a vector containing the total size of all HTTP content that was
121 // received over the last |kNumDaysInHistory| before any compression by the
122 // data reduction proxy. Each element in the vector contains one day of data.
123 ContentLengthList GetDailyOriginalContentLengths();
125 // Returns an vector containing the aggregate received HTTP content in the
126 // last |kNumDaysInHistory| days.
127 ContentLengthList GetDailyReceivedContentLengths();
129 // net::URLFetcherDelegate:
130 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
133 void InitPrefMembers();
135 virtual net::URLFetcher* GetURLFetcher();
137 // Virtualized for unit test support.
138 virtual PrefService* GetOriginalProfilePrefs();
139 virtual PrefService* GetLocalStatePrefs();
141 void GetContentLengths(unsigned int days,
142 int64* original_content_length,
143 int64* received_content_length,
144 int64* last_update_time);
145 ContentLengthList GetDailyContentLengths(const char* pref_name);
147 // Sets the proxy configs, enabling or disabling the proxy according to
148 // the value of |enabled|. |at_startup| is true when this method is called
149 // from InitDataReductionProxySettings.
150 virtual void SetProxyConfigs(bool enabled, bool at_startup);
152 // Metrics methods. Subclasses should override if they wish to provide
153 // alternate methods.
154 virtual void RecordDataReductionInit();
156 virtual void AddDefaultProxyBypassRules();
158 // Writes a warning to the log that is used in backend processing of
159 // customer feedback. Virtual so tests can mock it for verification.
160 virtual void LogProxyState(bool enabled, bool at_startup);
162 bool HasTurnedOn() { return has_turned_on_; }
163 bool HasTurnedOff() { return has_turned_off_; }
164 // Note that these flags may only be toggled to true, never back to false.
165 void SetHasTurnedOn() { has_turned_on_ = true; }
166 void SetHasTurnedOff() { has_turned_off_ = true; }
168 // Accessor for unit tests.
169 std::vector<std::string> BypassRules() { return bypass_rules_;}
172 friend class DataReductionProxySettingsTestBase;
173 friend class DataReductionProxySettingsTest;
174 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
175 TestAuthenticationInit);
176 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
177 TestAuthHashGeneration);
178 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
179 TestAuthHashGenerationWithOriginSetViaSwitch);
180 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
181 TestResetDataReductionStatistics);
182 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
183 TestIsProxyEnabledOrManaged);
184 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
186 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
187 TestGetDailyContentLengths);
188 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
189 TestMaybeActivateDataReductionProxy);
190 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
191 TestOnIPAddressChanged);
192 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
193 TestOnProxyEnabledPrefChange);
194 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
195 TestInitDataReductionProxyOn);
196 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
197 TestInitDataReductionProxyOff);
198 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest,
201 // NetworkChangeNotifier::IPAddressObserver:
202 virtual void OnIPAddressChanged() OVERRIDE;
204 // Underlying implementation of InitDataReductionProxySession(), factored
205 // out to be testable without creating a full HttpNetworkSession.
206 static void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache);
208 void OnProxyEnabledPrefChange();
210 void ResetDataReductionStatistics();
212 void MaybeActivateDataReductionProxy(bool at_startup);
214 // Requests the proxy probe URL, if one is set. If unable to do so, disables
215 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe
217 void ProbeWhetherDataReductionProxyIsAvailable();
218 std::string GetProxyCheckURL();
220 // Returns a UTF16 string that's the hash of the configured authentication
221 // key and |salt|. Returns an empty UTF16 string if no key is configured or
222 // the data reduction proxy feature isn't available.
223 static base::string16 AuthHashForSalt(int64 salt);
225 std::vector<std::string> bypass_rules_;
227 // Indicate whether a user has turned on the data reduction proxy previously
231 // Indicate whether a user has turned off the data reduction proxy previously
233 bool has_turned_off_;
235 bool disabled_by_carrier_;
236 bool enabled_by_user_;
238 scoped_ptr<net::URLFetcher> fetcher_;
239 BooleanPrefMember spdy_proxy_auth_enabled_;
241 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings);
244 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_