OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ | 5 #ifndef CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ |
6 #define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ | 6 #define CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "base/prefs/pref_member.h" | 14 #include "base/prefs/pref_member.h" |
15 #include "net/base/network_change_notifier.h" | 15 #include "net/base/network_change_notifier.h" |
16 #include "net/url_request/url_fetcher_delegate.h" | 16 #include "net/url_request/url_fetcher_delegate.h" |
17 | 17 |
18 class PrefService; | 18 class PrefService; |
19 | 19 |
20 namespace net { | 20 namespace net { |
| 21 class AuthChallengeInfo; |
| 22 class HostPortPair; |
| 23 class HttpAuthCache; |
| 24 class HttpNetworkSession; |
21 class URLFetcher; | 25 class URLFetcher; |
22 } | 26 } |
23 | 27 |
24 namespace spdyproxy { | 28 namespace spdyproxy { |
25 | 29 |
26 // The number of days of bandwidth usage statistics that are tracked. | 30 // The number of days of bandwidth usage statistics that are tracked. |
27 const unsigned int kNumDaysInHistory = 60; | 31 const unsigned int kNumDaysInHistory = 60; |
28 | 32 |
29 // The number of days of bandwidth usage statistics that are presented. | 33 // The number of days of bandwidth usage statistics that are presented. |
30 const unsigned int kNumDaysInHistorySummary = 30; | 34 const unsigned int kNumDaysInHistorySummary = 30; |
31 | 35 |
32 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory, | 36 COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory, |
33 DataReductionProxySettings_summary_too_long); | 37 DataReductionProxySettings_summary_too_long); |
34 | 38 |
35 } // namespace spdyproxy | 39 } // namespace spdyproxy |
36 | 40 |
37 // Central point for configuring the data reduction proxy. | 41 // Central point for configuring the data reduction proxy. |
38 // This object lives on the UI thread and all of its methods are expected to | 42 // This object lives on the UI thread and all of its methods are expected to |
39 // be called from there. | 43 // be called from there. |
40 class DataReductionProxySettings | 44 class DataReductionProxySettings |
41 : public net::URLFetcherDelegate, | 45 : public net::URLFetcherDelegate, |
42 public net::NetworkChangeNotifier::IPAddressObserver { | 46 public net::NetworkChangeNotifier::IPAddressObserver { |
43 public: | 47 public: |
44 typedef std::vector<long long> ContentLengthList; | 48 typedef std::vector<long long> ContentLengthList; |
| 49 typedef std::vector<GURL> DataReductionProxyList; |
45 | 50 |
46 DataReductionProxySettings(); | 51 DataReductionProxySettings(); |
47 virtual ~DataReductionProxySettings(); | 52 virtual ~DataReductionProxySettings(); |
48 | 53 |
49 void InitDataReductionProxySettings(); | 54 void InitDataReductionProxySettings(); |
50 | 55 |
| 56 // If proxy authentication is compiled in, pre-cache authentication |
| 57 // keys for all configured proxies in |session|. |
| 58 void InitDataReductionProxySession(net::HttpNetworkSession* session); |
| 59 |
51 // Add a host pattern to bypass. This should follow the same syntax used | 60 // Add a host pattern to bypass. This should follow the same syntax used |
52 // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix | 61 // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix |
53 // pattern, an IP literal, a CIDR block, or the magic string '<local>'. | 62 // pattern, an IP literal, a CIDR block, or the magic string '<local>'. |
54 // Bypass settings persist for the life of this object and are applied | 63 // Bypass settings persist for the life of this object and are applied |
55 // each time the proxy is enabled, but are not updated while it is enabled. | 64 // each time the proxy is enabled, but are not updated while it is enabled. |
56 void AddHostPatternToBypass(const std::string& pattern); | 65 void AddHostPatternToBypass(const std::string& pattern); |
57 | 66 |
58 // Add a URL pattern to bypass the proxy. The base implementation strips | 67 // Add a URL pattern to bypass the proxy. The base implementation strips |
59 // everything in |pattern| after the first single slash and then treats it | 68 // everything in |pattern| after the first single slash and then treats it |
60 // as a hostname pattern. Subclasses may implement other semantics. | 69 // as a hostname pattern. Subclasses may implement other semantics. |
61 virtual void AddURLPatternToBypass(const std::string& pattern); | 70 virtual void AddURLPatternToBypass(const std::string& pattern); |
62 | 71 |
63 // Returns true if the data reduction proxy is allowed to be used on this | 72 // Returns true if the data reduction proxy is allowed to be used on this |
64 // instance of Chrome. This could return false, for example, if this instance | 73 // instance of Chrome. This could return false, for example, if this instance |
65 // is not part of the field trial, or if the proxy name is not configured | 74 // is not part of the field trial, or if the proxy name is not configured |
66 // via gyp. | 75 // via gyp. |
67 bool IsDataReductionProxyAllowed(); | 76 bool IsDataReductionProxyAllowed(); |
68 | 77 |
69 // Returns true if a screen promoting the data reduction proxy is allowed to | 78 // Returns true if a screen promoting the data reduction proxy is allowed to |
70 // be shown. Logic that decides when to show the promo should check its | 79 // be shown. Logic that decides when to show the promo should check its |
71 // availability. This would return false if not part of a separate field | 80 // availability. This would return false if not part of a separate field |
72 // trial that governs the use of the promotion. | 81 // trial that governs the use of the promotion. |
73 bool IsDataReductionProxyPromoAllowed(); | 82 bool IsDataReductionProxyPromoAllowed(); |
74 | 83 |
75 // Returns the URL of the data reduction proxy. | 84 // Returns the URL of the data reduction proxy. |
76 std::string GetDataReductionProxyOrigin(); | 85 std::string GetDataReductionProxyOrigin(); |
77 | 86 |
78 // Returns a configuration string for the proxy. | 87 // Returns the URL of the fallback data reduction proxy. |
79 std::string GetDataReductionProxyAuth(); | 88 std::string GetDataReductionProxyFallback(); |
| 89 |
| 90 // Returns a vector of HostPortPairs for all configured proxies. |
| 91 DataReductionProxyList GetDataReductionProxies(); |
| 92 |
| 93 // Returns true if |auth_info| represents an authentication challenge from |
| 94 // a compatible, configured proxy. |
| 95 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info); |
| 96 |
| 97 // Returns a UTF16 string suitable for use as an authentication token in |
| 98 // response to the challenge represented by |auth_info|. If the token can't |
| 99 // be correctly generated for |auth_info|, returns an empty UTF16 string. |
| 100 base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info); |
80 | 101 |
81 // Returns true if the proxy is enabled. | 102 // Returns true if the proxy is enabled. |
82 bool IsDataReductionProxyEnabled(); | 103 bool IsDataReductionProxyEnabled(); |
83 | 104 |
84 // Returns true if the proxy is managed by an adminstrator's policy. | 105 // Returns true if the proxy is managed by an adminstrator's policy. |
85 bool IsDataReductionProxyManaged(); | 106 bool IsDataReductionProxyManaged(); |
86 | 107 |
87 // Enables or disables the data reduction proxy. If a probe URL is available, | 108 // Enables or disables the data reduction proxy. If a probe URL is available, |
88 // and a probe request fails at some point, the proxy won't be used until a | 109 // and a probe request fails at some point, the proxy won't be used until a |
89 // probe succeeds. | 110 // probe succeeds. |
(...skipping 10 matching lines...) Expand all Loading... |
100 | 121 |
101 // Returns an vector containing the aggregate received HTTP content in the | 122 // Returns an vector containing the aggregate received HTTP content in the |
102 // last |kNumDaysInHistory| days. | 123 // last |kNumDaysInHistory| days. |
103 ContentLengthList GetDailyReceivedContentLengths(); | 124 ContentLengthList GetDailyReceivedContentLengths(); |
104 | 125 |
105 // net::URLFetcherDelegate: | 126 // net::URLFetcherDelegate: |
106 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 127 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
107 | 128 |
108 protected: | 129 protected: |
109 void InitPrefMembers(); | 130 void InitPrefMembers(); |
| 131 |
110 virtual net::URLFetcher* GetURLFetcher(); | 132 virtual net::URLFetcher* GetURLFetcher(); |
111 | 133 |
112 // Virtualized for unit test support. | 134 // Virtualized for unit test support. |
113 virtual PrefService* GetOriginalProfilePrefs(); | 135 virtual PrefService* GetOriginalProfilePrefs(); |
114 virtual PrefService* GetLocalStatePrefs(); | 136 virtual PrefService* GetLocalStatePrefs(); |
115 | 137 |
116 std::string GetDataReductionProxyOriginHostPort(); | |
117 | |
118 bool IsProxyOriginSetOnCommandLine(); | 138 bool IsProxyOriginSetOnCommandLine(); |
119 void GetContentLengths(unsigned int days, | 139 void GetContentLengths(unsigned int days, |
120 int64* original_content_length, | 140 int64* original_content_length, |
121 int64* received_content_length, | 141 int64* received_content_length, |
122 int64* last_update_time); | 142 int64* last_update_time); |
123 ContentLengthList GetDailyContentLengths(const char* pref_name); | 143 ContentLengthList GetDailyContentLengths(const char* pref_name); |
124 | 144 |
125 // Sets the proxy configs, enabling or disabling the proxy according to | 145 // Sets the proxy configs, enabling or disabling the proxy according to |
126 // the value of |enabled|. |at_startup| is true when this method is called | 146 // the value of |enabled|. |at_startup| is true when this method is called |
127 // from InitDataReductionProxySettings. | 147 // from InitDataReductionProxySettings. |
(...skipping 11 matching lines...) Expand all Loading... |
139 | 159 |
140 bool HasTurnedOn() { return has_turned_on_; } | 160 bool HasTurnedOn() { return has_turned_on_; } |
141 bool HasTurnedOff() { return has_turned_off_; } | 161 bool HasTurnedOff() { return has_turned_off_; } |
142 // Note that these flags may only be toggled to true, never back to false. | 162 // Note that these flags may only be toggled to true, never back to false. |
143 void SetHasTurnedOn() { has_turned_on_ = true; } | 163 void SetHasTurnedOn() { has_turned_on_ = true; } |
144 void SetHasTurnedOff() { has_turned_off_ = true; } | 164 void SetHasTurnedOff() { has_turned_off_ = true; } |
145 | 165 |
146 // Accessor for unit tests. | 166 // Accessor for unit tests. |
147 std::vector<std::string> BypassRules() { return bypass_rules_;} | 167 std::vector<std::string> BypassRules() { return bypass_rules_;} |
148 | 168 |
| 169 // Accessors for default hostnames, virtual for testing. |
| 170 virtual std::string GetDefaultProxyHost(); |
| 171 virtual std::string GetDefaultFallbackProxyHost(); |
| 172 |
149 private: | 173 private: |
150 friend class DataReductionProxySettingsTestBase; | 174 friend class DataReductionProxySettingsTestBase; |
151 friend class DataReductionProxySettingsTest; | 175 friend class DataReductionProxySettingsTest; |
152 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 176 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 177 TestAuthenticationInit); |
| 178 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 179 TestAuthHashGeneration); |
| 180 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 181 TestAuthHashGenerationWithOriginSetViaSwitch); |
| 182 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
153 TestResetDataReductionStatistics); | 183 TestResetDataReductionStatistics); |
154 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 184 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
155 TestIsProxyEnabledOrManaged); | 185 TestIsProxyEnabledOrManaged); |
156 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 186 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
157 TestContentLengths); | 187 TestContentLengths); |
158 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 188 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
159 TestGetDailyContentLengths); | 189 TestGetDailyContentLengths); |
160 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 190 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
161 TestMaybeActivateDataReductionProxy); | 191 TestMaybeActivateDataReductionProxy); |
162 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 192 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
163 TestOnIPAddressChanged); | 193 TestOnIPAddressChanged); |
164 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 194 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
165 TestOnProxyEnabledPrefChange); | 195 TestOnProxyEnabledPrefChange); |
166 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 196 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
167 TestInitDataReductionProxyOn); | 197 TestInitDataReductionProxyOn); |
168 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 198 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
169 TestInitDataReductionProxyOff); | 199 TestInitDataReductionProxyOff); |
170 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 200 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
171 TestBypassList); | 201 TestBypassList); |
172 | 202 |
173 // NetworkChangeNotifier::IPAddressObserver: | 203 // NetworkChangeNotifier::IPAddressObserver: |
174 virtual void OnIPAddressChanged() OVERRIDE; | 204 virtual void OnIPAddressChanged() OVERRIDE; |
175 | 205 |
| 206 // Underlying implementation of InitDataReductionProxySession(), factored |
| 207 // out to be testable without creating a full HttpNetworkSession. |
| 208 void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache); |
| 209 |
176 void OnProxyEnabledPrefChange(); | 210 void OnProxyEnabledPrefChange(); |
177 | 211 |
178 void ResetDataReductionStatistics(); | 212 void ResetDataReductionStatistics(); |
179 | 213 |
180 void MaybeActivateDataReductionProxy(bool at_startup); | 214 void MaybeActivateDataReductionProxy(bool at_startup); |
181 | 215 |
182 // Requests the proxy probe URL, if one is set. If unable to do so, disables | 216 // Requests the proxy probe URL, if one is set. If unable to do so, disables |
183 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe | 217 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe |
184 // failure. | 218 // failure. |
185 void ProbeWhetherDataReductionProxyIsAvailable(); | 219 void ProbeWhetherDataReductionProxyIsAvailable(); |
186 std::string GetProxyCheckURL(); | 220 std::string GetProxyCheckURL(); |
187 | 221 |
| 222 // Returns a UTF16 string that's the hash of the configured authentication |
| 223 // key and |salt|. Returns an empty UTF16 string if no key is configured or |
| 224 // the data reduction proxy feature isn't available. |
| 225 base::string16 AuthHashForSalt(int64 salt); |
| 226 |
188 std::vector<std::string> bypass_rules_; | 227 std::vector<std::string> bypass_rules_; |
189 | 228 |
190 // Indicate whether a user has turned on the data reduction proxy previously | 229 // Indicate whether a user has turned on the data reduction proxy previously |
191 // in this session. | 230 // in this session. |
192 bool has_turned_on_; | 231 bool has_turned_on_; |
193 | 232 |
194 // Indicate whether a user has turned off the data reduction proxy previously | 233 // Indicate whether a user has turned off the data reduction proxy previously |
195 // in this session. | 234 // in this session. |
196 bool has_turned_off_; | 235 bool has_turned_off_; |
197 | 236 |
198 bool disabled_by_carrier_; | 237 bool disabled_by_carrier_; |
199 bool enabled_by_user_; | 238 bool enabled_by_user_; |
200 | 239 |
201 scoped_ptr<net::URLFetcher> fetcher_; | 240 scoped_ptr<net::URLFetcher> fetcher_; |
202 BooleanPrefMember spdy_proxy_auth_enabled_; | 241 BooleanPrefMember spdy_proxy_auth_enabled_; |
203 | 242 |
204 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); | 243 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); |
205 }; | 244 }; |
206 | 245 |
207 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ | 246 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ |
OLD | NEW |