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. |
| 44 // TODO(marq): Convert this to be a BrowserContextKeyedService with an |
| 45 // associated factory class, and refactor the Java call sites accordingly. |
40 class DataReductionProxySettings | 46 class DataReductionProxySettings |
41 : public net::URLFetcherDelegate, | 47 : public net::URLFetcherDelegate, |
42 public net::NetworkChangeNotifier::IPAddressObserver { | 48 public net::NetworkChangeNotifier::IPAddressObserver { |
43 public: | 49 public: |
44 typedef std::vector<long long> ContentLengthList; | 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; |
45 | 53 |
46 DataReductionProxySettings(); | 54 DataReductionProxySettings(); |
47 virtual ~DataReductionProxySettings(); | 55 virtual ~DataReductionProxySettings(); |
48 | 56 |
49 void InitDataReductionProxySettings(); | 57 void InitDataReductionProxySettings(); |
50 | 58 |
| 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); |
| 62 |
51 // Add a host pattern to bypass. This should follow the same syntax used | 63 // 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 | 64 // in net::ProxyBypassRules; that is, a hostname pattern, a hostname suffix |
53 // pattern, an IP literal, a CIDR block, or the magic string '<local>'. | 65 // 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 | 66 // 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. | 67 // each time the proxy is enabled, but are not updated while it is enabled. |
56 void AddHostPatternToBypass(const std::string& pattern); | 68 void AddHostPatternToBypass(const std::string& pattern); |
57 | 69 |
58 // Add a URL pattern to bypass the proxy. The base implementation strips | 70 // 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 | 71 // everything in |pattern| after the first single slash and then treats it |
60 // as a hostname pattern. Subclasses may implement other semantics. | 72 // as a hostname pattern. Subclasses may implement other semantics. |
61 virtual void AddURLPatternToBypass(const std::string& pattern); | 73 virtual void AddURLPatternToBypass(const std::string& pattern); |
62 | 74 |
63 // Returns true if the data reduction proxy is allowed to be used on this | 75 // 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 | 76 // 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 | 77 // is not part of the field trial, or if the proxy name is not configured |
66 // via gyp. | 78 // via gyp. |
67 bool IsDataReductionProxyAllowed(); | 79 static bool IsDataReductionProxyAllowed(); |
68 | 80 |
69 // Returns true if a screen promoting the data reduction proxy is allowed to | 81 // 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 | 82 // 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 | 83 // availability. This would return false if not part of a separate field |
72 // trial that governs the use of the promotion. | 84 // trial that governs the use of the promotion. |
73 bool IsDataReductionProxyPromoAllowed(); | 85 static bool IsDataReductionProxyPromoAllowed(); |
74 | 86 |
75 // Returns the URL of the data reduction proxy. | 87 // Returns the URL of the data reduction proxy. |
76 std::string GetDataReductionProxyOrigin(); | 88 static std::string GetDataReductionProxyOrigin(); |
77 | 89 |
78 // Returns a configuration string for the proxy. | 90 // Returns the URL of the fallback data reduction proxy. |
79 std::string GetDataReductionProxyAuth(); | 91 static std::string GetDataReductionProxyFallback(); |
| 92 |
| 93 // Returns a vector of GURLs for all configured proxies. |
| 94 static DataReductionProxyList GetDataReductionProxies(); |
| 95 |
| 96 // Returns true if |auth_info| represents an authentication challenge from |
| 97 // a compatible, configured proxy. |
| 98 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info); |
| 99 |
| 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); |
80 | 104 |
81 // Returns true if the proxy is enabled. | 105 // Returns true if the proxy is enabled. |
82 bool IsDataReductionProxyEnabled(); | 106 bool IsDataReductionProxyEnabled(); |
83 | 107 |
84 // Returns true if the proxy is managed by an adminstrator's policy. | 108 // Returns true if the proxy is managed by an adminstrator's policy. |
85 bool IsDataReductionProxyManaged(); | 109 bool IsDataReductionProxyManaged(); |
86 | 110 |
87 // Enables or disables the data reduction proxy. If a probe URL is available, | 111 // 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 | 112 // and a probe request fails at some point, the proxy won't be used until a |
89 // probe succeeds. | 113 // probe succeeds. |
(...skipping 10 matching lines...) Expand all Loading... |
100 | 124 |
101 // Returns an vector containing the aggregate received HTTP content in the | 125 // Returns an vector containing the aggregate received HTTP content in the |
102 // last |kNumDaysInHistory| days. | 126 // last |kNumDaysInHistory| days. |
103 ContentLengthList GetDailyReceivedContentLengths(); | 127 ContentLengthList GetDailyReceivedContentLengths(); |
104 | 128 |
105 // net::URLFetcherDelegate: | 129 // net::URLFetcherDelegate: |
106 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 130 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
107 | 131 |
108 protected: | 132 protected: |
109 void InitPrefMembers(); | 133 void InitPrefMembers(); |
| 134 |
110 virtual net::URLFetcher* GetURLFetcher(); | 135 virtual net::URLFetcher* GetURLFetcher(); |
111 | 136 |
112 // Virtualized for unit test support. | 137 // Virtualized for unit test support. |
113 virtual PrefService* GetOriginalProfilePrefs(); | 138 virtual PrefService* GetOriginalProfilePrefs(); |
114 virtual PrefService* GetLocalStatePrefs(); | 139 virtual PrefService* GetLocalStatePrefs(); |
115 | 140 |
116 std::string GetDataReductionProxyOriginHostPort(); | |
117 | |
118 bool IsProxyOriginSetOnCommandLine(); | |
119 void GetContentLengths(unsigned int days, | 141 void GetContentLengths(unsigned int days, |
120 int64* original_content_length, | 142 int64* original_content_length, |
121 int64* received_content_length, | 143 int64* received_content_length, |
122 int64* last_update_time); | 144 int64* last_update_time); |
123 ContentLengthList GetDailyContentLengths(const char* pref_name); | 145 ContentLengthList GetDailyContentLengths(const char* pref_name); |
124 | 146 |
125 // Sets the proxy configs, enabling or disabling the proxy according to | 147 // 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 | 148 // the value of |enabled|. |at_startup| is true when this method is called |
127 // from InitDataReductionProxySettings. | 149 // from InitDataReductionProxySettings. |
128 virtual void SetProxyConfigs(bool enabled, bool at_startup); | 150 virtual void SetProxyConfigs(bool enabled, bool at_startup); |
129 | 151 |
130 // Metrics methods. Subclasses should override if they wish to provide | 152 // Metrics methods. Subclasses should override if they wish to provide |
131 // alternate methods. | 153 // alternate methods. |
132 virtual void RecordDataReductionInit(); | 154 virtual void RecordDataReductionInit(); |
133 | 155 |
134 virtual void AddDefaultProxyBypassRules(); | 156 virtual void AddDefaultProxyBypassRules(); |
135 | 157 |
136 // Writes a warning to the log that is used in backend processing of | 158 // Writes a warning to the log that is used in backend processing of |
137 // customer feedback. | 159 // customer feedback. Virtual so tests can mock it for verification. |
138 void LogProxyState(bool enabled, bool at_startup); | 160 virtual void LogProxyState(bool enabled, bool at_startup); |
139 | 161 |
140 bool HasTurnedOn() { return has_turned_on_; } | 162 bool HasTurnedOn() { return has_turned_on_; } |
141 bool HasTurnedOff() { return has_turned_off_; } | 163 bool HasTurnedOff() { return has_turned_off_; } |
142 // Note that these flags may only be toggled to true, never back to false. | 164 // Note that these flags may only be toggled to true, never back to false. |
143 void SetHasTurnedOn() { has_turned_on_ = true; } | 165 void SetHasTurnedOn() { has_turned_on_ = true; } |
144 void SetHasTurnedOff() { has_turned_off_ = true; } | 166 void SetHasTurnedOff() { has_turned_off_ = true; } |
145 | 167 |
146 // Accessor for unit tests. | 168 // Accessor for unit tests. |
147 std::vector<std::string> BypassRules() { return bypass_rules_;} | 169 std::vector<std::string> BypassRules() { return bypass_rules_;} |
148 | 170 |
149 private: | 171 private: |
150 friend class DataReductionProxySettingsTestBase; | 172 friend class DataReductionProxySettingsTestBase; |
151 friend class DataReductionProxySettingsTest; | 173 friend class DataReductionProxySettingsTest; |
152 FRIEND_TEST_ALL_PREFIXES(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, |
153 TestResetDataReductionStatistics); | 181 TestResetDataReductionStatistics); |
154 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 182 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
155 TestIsProxyEnabledOrManaged); | 183 TestIsProxyEnabledOrManaged); |
156 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 184 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
157 TestContentLengths); | 185 TestContentLengths); |
158 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 186 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
159 TestGetDailyContentLengths); | 187 TestGetDailyContentLengths); |
160 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 188 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
161 TestMaybeActivateDataReductionProxy); | 189 TestMaybeActivateDataReductionProxy); |
162 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 190 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
163 TestOnIPAddressChanged); | 191 TestOnIPAddressChanged); |
164 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 192 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
165 TestOnProxyEnabledPrefChange); | 193 TestOnProxyEnabledPrefChange); |
166 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 194 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
167 TestInitDataReductionProxyOn); | 195 TestInitDataReductionProxyOn); |
168 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 196 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
169 TestInitDataReductionProxyOff); | 197 TestInitDataReductionProxyOff); |
170 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 198 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
171 TestBypassList); | 199 TestBypassList); |
172 | 200 |
173 // NetworkChangeNotifier::IPAddressObserver: | 201 // NetworkChangeNotifier::IPAddressObserver: |
174 virtual void OnIPAddressChanged() OVERRIDE; | 202 virtual void OnIPAddressChanged() OVERRIDE; |
175 | 203 |
| 204 // Underlying implementation of InitDataReductionProxySession(), factored |
| 205 // out to be testable without creating a full HttpNetworkSession. |
| 206 static void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache); |
| 207 |
176 void OnProxyEnabledPrefChange(); | 208 void OnProxyEnabledPrefChange(); |
177 | 209 |
178 void ResetDataReductionStatistics(); | 210 void ResetDataReductionStatistics(); |
179 | 211 |
180 void MaybeActivateDataReductionProxy(bool at_startup); | 212 void MaybeActivateDataReductionProxy(bool at_startup); |
181 | 213 |
182 // Requests the proxy probe URL, if one is set. If unable to do so, disables | 214 // 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 | 215 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe |
184 // failure. | 216 // failure. |
185 void ProbeWhetherDataReductionProxyIsAvailable(); | 217 void ProbeWhetherDataReductionProxyIsAvailable(); |
186 std::string GetProxyCheckURL(); | 218 std::string GetProxyCheckURL(); |
187 | 219 |
| 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); |
| 224 |
188 std::vector<std::string> bypass_rules_; | 225 std::vector<std::string> bypass_rules_; |
189 | 226 |
190 // Indicate whether a user has turned on the data reduction proxy previously | 227 // Indicate whether a user has turned on the data reduction proxy previously |
191 // in this session. | 228 // in this session. |
192 bool has_turned_on_; | 229 bool has_turned_on_; |
193 | 230 |
194 // Indicate whether a user has turned off the data reduction proxy previously | 231 // Indicate whether a user has turned off the data reduction proxy previously |
195 // in this session. | 232 // in this session. |
196 bool has_turned_off_; | 233 bool has_turned_off_; |
197 | 234 |
198 bool disabled_by_carrier_; | 235 bool disabled_by_carrier_; |
199 bool enabled_by_user_; | 236 bool enabled_by_user_; |
200 | 237 |
201 scoped_ptr<net::URLFetcher> fetcher_; | 238 scoped_ptr<net::URLFetcher> fetcher_; |
202 BooleanPrefMember spdy_proxy_auth_enabled_; | 239 BooleanPrefMember spdy_proxy_auth_enabled_; |
203 | 240 |
204 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); | 241 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); |
205 }; | 242 }; |
206 | 243 |
207 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ | 244 #endif // CHROME_BROWSER_NET_SPDYPROXY_DATA_REDUCTION_PROXY_SETTINGS_H_ |
OLD | NEW |