| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ | 5 #ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ |
| 6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ | 6 #define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_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 "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 16 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" | 16 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configura
tor.h" |
| 17 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h" | |
| 18 #include "net/base/network_change_notifier.h" | 17 #include "net/base/network_change_notifier.h" |
| 19 #include "net/url_request/url_fetcher_delegate.h" | 18 #include "net/url_request/url_fetcher_delegate.h" |
| 20 | 19 |
| 21 class PrefService; | 20 class PrefService; |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 class AuthChallengeInfo; | 23 class AuthChallengeInfo; |
| 25 class HostPortPair; | 24 class HostPortPair; |
| 26 class HttpAuthCache; | 25 class HttpAuthCache; |
| 27 class HttpNetworkSession; | 26 class HttpNetworkSession; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 // Central point for configuring the data reduction proxy. | 79 // Central point for configuring the data reduction proxy. |
| 81 // This object lives on the UI thread and all of its methods are expected to | 80 // This object lives on the UI thread and all of its methods are expected to |
| 82 // be called from there. | 81 // be called from there. |
| 83 // TODO(marq): Convert this to be a KeyedService with an | 82 // TODO(marq): Convert this to be a KeyedService with an |
| 84 // associated factory class, and refactor the Java call sites accordingly. | 83 // associated factory class, and refactor the Java call sites accordingly. |
| 85 class DataReductionProxySettings | 84 class DataReductionProxySettings |
| 86 : public net::URLFetcherDelegate, | 85 : public net::URLFetcherDelegate, |
| 87 public net::NetworkChangeNotifier::IPAddressObserver { | 86 public net::NetworkChangeNotifier::IPAddressObserver { |
| 88 public: | 87 public: |
| 89 typedef std::vector<long long> ContentLengthList; | 88 typedef std::vector<long long> ContentLengthList; |
| 89 // TODO(marq): Consider instead using a std::pair instead of a vector. |
| 90 typedef std::vector<GURL> DataReductionProxyList; |
| 90 | 91 |
| 92 // Returns true of the data reduction proxy origin is set on the command line. |
| 93 static bool IsProxyOriginSetOnCommandLine(); |
| 94 |
| 95 // Returns true if the data reduction proxy key is set on the command line. |
| 91 static bool IsProxyKeySetOnCommandLine(); | 96 static bool IsProxyKeySetOnCommandLine(); |
| 92 | 97 |
| 93 DataReductionProxySettings(DataReductionProxyParams* params); | 98 // Returns true if this application instance is part of the data reduction |
| 99 // proxy field trial, or if it a proxy origin is set in flags. This is a |
| 100 // convenience method for platforms like Chrome on Android and iOS, to |
| 101 // determine if the data reduction proxy is allowed. |
| 102 static bool IsIncludedInFieldTrialOrFlags(); |
| 103 |
| 104 static void SetAllowed(bool allowed); |
| 105 static void SetPromoAllowed(bool promo_allowed); |
| 106 |
| 107 DataReductionProxySettings(); |
| 94 virtual ~DataReductionProxySettings(); | 108 virtual ~DataReductionProxySettings(); |
| 95 | 109 |
| 96 DataReductionProxyParams* params() const { | 110 // Set and get the key to be used for data reduction proxy authentication. |
| 97 return params_.get(); | 111 void set_key(const std::string& key) { |
| 112 key_ = key; |
| 113 } |
| 114 |
| 115 const std::string& key() const { |
| 116 return key_; |
| 98 } | 117 } |
| 99 | 118 |
| 100 // Initializes the data reduction proxy with profile and local state prefs, | 119 // Initializes the data reduction proxy with profile and local state prefs, |
| 101 // and a |UrlRequestContextGetter| for canary probes. The caller must ensure | 120 // and a |UrlRequestContextGetter| for canary probes. The caller must ensure |
| 102 // that all parameters remain alive for the lifetime of the | 121 // that all parameters remain alive for the lifetime of the |
| 103 // |DataReductionProxySettings| instance. | 122 // |DataReductionProxySettings| instance. |
| 104 void InitDataReductionProxySettings( | 123 void InitDataReductionProxySettings( |
| 105 PrefService* prefs, | 124 PrefService* prefs, |
| 106 PrefService* local_state_prefs, | 125 PrefService* local_state_prefs, |
| 107 net::URLRequestContextGetter* url_request_context_getter); | 126 net::URLRequestContextGetter* url_request_context_getter); |
| 108 | 127 |
| 109 // Initializes the data reduction proxy with profile and local state prefs, | 128 // Initializes the data reduction proxy with profile and local state prefs, |
| 110 // a |UrlRequestContextGetter| for canary probes, and a proxy configurator. | 129 // a |UrlRequestContextGetter| for canary probes, and a proxy configurator. |
| 111 // The caller must ensure that all parameters remain alive for the lifetime of | 130 // The caller must ensure that all parameters remain alive for the lifetime of |
| 112 // the |DataReductionProxySettings| instance. | 131 // the |DataReductionProxySettings| instance. |
| 113 // TODO(marq): Remove when iOS supports the new interface above. | 132 // TODO(marq): Remove when iOS supports the new interface above. |
| 114 void InitDataReductionProxySettings( | 133 void InitDataReductionProxySettings( |
| 115 PrefService* prefs, | 134 PrefService* prefs, |
| 116 PrefService* local_state_prefs, | 135 PrefService* local_state_prefs, |
| 117 net::URLRequestContextGetter* url_request_context_getter, | 136 net::URLRequestContextGetter* url_request_context_getter, |
| 118 scoped_ptr<DataReductionProxyConfigurator> configurator); | 137 scoped_ptr<DataReductionProxyConfigurator> config); |
| 119 | 138 |
| 120 // Sets the logic the embedder uses to set the networking configuration that | 139 // Sets the logic the embedder uses to set the networking configuration that |
| 121 // causes traffic to be proxied. | 140 // causes traffic to be proxied. |
| 122 void SetProxyConfigurator( | 141 void SetProxyConfigurator( |
| 123 scoped_ptr<DataReductionProxyConfigurator> configurator); | 142 scoped_ptr<DataReductionProxyConfigurator> configurator); |
| 124 | 143 |
| 125 // If proxy authentication is compiled in, pre-cache authentication | 144 // If proxy authentication is compiled in, pre-cache an authentication |
| 126 // keys for all configured proxies in |session|. | 145 // |key| for all configured proxies in |session|. |
| 127 static void InitDataReductionProxySession( | 146 static void InitDataReductionProxySession(net::HttpNetworkSession* session, |
| 128 net::HttpNetworkSession* session, | 147 const std::string& key); |
| 129 const DataReductionProxyParams* params); | 148 |
| 149 // Returns true if the data reduction proxy is allowed to be used. This could |
| 150 // return false, for example, if this instance is not part of the field trial, |
| 151 // or if the proxy name is not configured via gyp. |
| 152 static bool IsDataReductionProxyAllowed(); |
| 153 |
| 154 // Returns true if a screen promoting the data reduction proxy is allowed to |
| 155 // be shown. Logic that decides when to show the promo should check its |
| 156 // availability. This would return false if not part of a separate field |
| 157 // trial that governs the use of the promotion. |
| 158 static bool IsDataReductionProxyPromoAllowed(); |
| 159 |
| 160 // Returns true if preconnect advisory hinting is enabled by command line |
| 161 // flag or Finch trial. |
| 162 static bool IsPreconnectHintingAllowed(); |
| 163 |
| 164 // Returns the URL of the data reduction proxy. |
| 165 static std::string GetDataReductionProxyOrigin(); |
| 166 |
| 167 // Returns the URL of the fallback data reduction proxy. |
| 168 static std::string GetDataReductionProxyFallback(); |
| 169 |
| 170 // Returns a vector of GURLs for all configured proxies. |
| 171 static DataReductionProxyList GetDataReductionProxies(); |
| 130 | 172 |
| 131 // Returns true if |auth_info| represents an authentication challenge from | 173 // Returns true if |auth_info| represents an authentication challenge from |
| 132 // a compatible, configured proxy. | 174 // a compatible, configured proxy. |
| 133 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info); | 175 static bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info); |
| 134 | 176 |
| 135 // Returns a UTF16 string suitable for use as an authentication token in | 177 // Returns a UTF16 string suitable for use as an authentication token in |
| 136 // response to the challenge represented by |auth_info|. If the token can't | 178 // response to the challenge represented by |auth_info|. If the token can't |
| 137 // be correctly generated for |auth_info|, returns an empty UTF16 string. | 179 // be correctly generated for |auth_info|, returns an empty UTF16 string. |
| 138 base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info); | 180 base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info); |
| 139 | 181 |
| 140 // Returns true if the proxy is enabled. | 182 // Returns true if the proxy is enabled. |
| 141 bool IsDataReductionProxyEnabled(); | 183 bool IsDataReductionProxyEnabled(); |
| 142 | 184 |
| 143 // Returns true if the alternative proxy is enabled. | |
| 144 bool IsDataReductionProxyAlternativeEnabled(); | |
| 145 | |
| 146 // Returns true if the proxy is managed by an adminstrator's policy. | 185 // Returns true if the proxy is managed by an adminstrator's policy. |
| 147 bool IsDataReductionProxyManaged(); | 186 bool IsDataReductionProxyManaged(); |
| 148 | 187 |
| 149 // Enables or disables the data reduction proxy. If a probe URL is available, | 188 // Enables or disables the data reduction proxy. If a probe URL is available, |
| 150 // and a probe request fails at some point, the proxy won't be used until a | 189 // and a probe request fails at some point, the proxy won't be used until a |
| 151 // probe succeeds. | 190 // probe succeeds. |
| 152 void SetDataReductionProxyEnabled(bool enabled); | 191 void SetDataReductionProxyEnabled(bool enabled); |
| 153 | 192 |
| 154 // Enables or disables the alternative data reduction proxy configuration. | 193 // If |allowed|, the fallback proxy will be included in the proxy |
| 155 void SetDataReductionProxyAlternativeEnabled(bool enabled); | 194 // configuration. |
| 195 void set_fallback_allowed(bool allowed) { |
| 196 fallback_allowed_ = allowed; |
| 197 } |
| 198 |
| 199 bool fallback_allowed() const { |
| 200 return fallback_allowed_; |
| 201 } |
| 156 | 202 |
| 157 // Returns the time in microseconds that the last update was made to the | 203 // Returns the time in microseconds that the last update was made to the |
| 158 // daily original and received content lengths. | 204 // daily original and received content lengths. |
| 159 int64 GetDataReductionLastUpdateTime(); | 205 int64 GetDataReductionLastUpdateTime(); |
| 160 | 206 |
| 161 // Returns a vector containing the total size of all HTTP content that was | 207 // Returns a vector containing the total size of all HTTP content that was |
| 162 // received over the last |kNumDaysInHistory| before any compression by the | 208 // received over the last |kNumDaysInHistory| before any compression by the |
| 163 // data reduction proxy. Each element in the vector contains one day of data. | 209 // data reduction proxy. Each element in the vector contains one day of data. |
| 164 ContentLengthList GetDailyOriginalContentLengths(); | 210 ContentLengthList GetDailyOriginalContentLengths(); |
| 165 | 211 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 179 virtual PrefService* GetOriginalProfilePrefs(); | 225 virtual PrefService* GetOriginalProfilePrefs(); |
| 180 virtual PrefService* GetLocalStatePrefs(); | 226 virtual PrefService* GetLocalStatePrefs(); |
| 181 | 227 |
| 182 void GetContentLengths(unsigned int days, | 228 void GetContentLengths(unsigned int days, |
| 183 int64* original_content_length, | 229 int64* original_content_length, |
| 184 int64* received_content_length, | 230 int64* received_content_length, |
| 185 int64* last_update_time); | 231 int64* last_update_time); |
| 186 ContentLengthList GetDailyContentLengths(const char* pref_name); | 232 ContentLengthList GetDailyContentLengths(const char* pref_name); |
| 187 | 233 |
| 188 // Sets the proxy configs, enabling or disabling the proxy according to | 234 // Sets the proxy configs, enabling or disabling the proxy according to |
| 189 // the value of |enabled| and |alternative_enabled|. Use the alternative | 235 // the value of |enabled|. If |restricted| is true, only enable the fallback |
| 190 // configuration only if |enabled| and |alternative_enabled| are true. If | 236 // proxy. |at_startup| is true when this method is called from |
| 191 // |restricted| is true, only enable the fallback proxy. |at_startup| is true | 237 // InitDataReductionProxySettings. |
| 192 // when this method is called from InitDataReductionProxySettings. | 238 virtual void SetProxyConfigs(bool enabled, bool restricted, bool at_startup); |
| 193 virtual void SetProxyConfigs(bool enabled, | |
| 194 bool alternative_enabled, | |
| 195 bool restricted, | |
| 196 bool at_startup); | |
| 197 | 239 |
| 198 // Metrics method. Subclasses should override if they wish to provide | 240 // Metrics methods. Subclasses should override if they wish to provide |
| 199 // alternatives. | 241 // alternate methods. |
| 200 virtual void RecordDataReductionInit(); | 242 virtual void RecordDataReductionInit(); |
| 201 | 243 |
| 202 virtual void AddDefaultProxyBypassRules(); | 244 virtual void AddDefaultProxyBypassRules(); |
| 203 | 245 |
| 204 // Writes a warning to the log that is used in backend processing of | 246 // Writes a warning to the log that is used in backend processing of |
| 205 // customer feedback. Virtual so tests can mock it for verification. | 247 // customer feedback. Virtual so tests can mock it for verification. |
| 206 virtual void LogProxyState(bool enabled, bool restricted, bool at_startup); | 248 virtual void LogProxyState(bool enabled, bool restricted, bool at_startup); |
| 207 | 249 |
| 208 // Virtualized for mocking | 250 // Virtualized for mocking |
| 209 virtual void RecordProbeURLFetchResult( | 251 virtual void RecordProbeURLFetchResult( |
| 210 data_reduction_proxy::ProbeURLFetchResult result); | 252 data_reduction_proxy::ProbeURLFetchResult result); |
| 211 virtual void RecordStartupState( | 253 virtual void RecordStartupState( |
| 212 data_reduction_proxy::ProxyStartupState state); | 254 data_reduction_proxy::ProxyStartupState state); |
| 213 | 255 |
| 214 DataReductionProxyConfigurator* configurator() { | 256 DataReductionProxyConfigurator* config() { |
| 215 return configurator_.get(); | 257 return config_.get(); |
| 216 } | 258 } |
| 217 | 259 |
| 218 // Reset params for tests. | |
| 219 void ResetParamsForTest(DataReductionProxyParams* params); | |
| 220 | |
| 221 private: | 260 private: |
| 222 friend class DataReductionProxySettingsTestBase; | 261 friend class DataReductionProxySettingsTestBase; |
| 223 friend class DataReductionProxySettingsTest; | 262 friend class DataReductionProxySettingsTest; |
| 224 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 263 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 225 TestAuthenticationInit); | 264 TestAuthenticationInit); |
| 226 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 265 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 227 TestAuthHashGeneration); | 266 TestAuthHashGeneration); |
| 228 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 267 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 229 TestAuthHashGenerationWithOriginSetViaSwitch); | 268 TestAuthHashGenerationWithOriginSetViaSwitch); |
| 230 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 269 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 242 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 281 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 243 TestOnProxyEnabledPrefChange); | 282 TestOnProxyEnabledPrefChange); |
| 244 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 283 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 245 TestInitDataReductionProxyOn); | 284 TestInitDataReductionProxyOn); |
| 246 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 285 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 247 TestInitDataReductionProxyOff); | 286 TestInitDataReductionProxyOff); |
| 248 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 287 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 249 TestBypassList); | 288 TestBypassList); |
| 250 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | 289 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, |
| 251 CheckInitMetricsWhenNotAllowed); | 290 CheckInitMetricsWhenNotAllowed); |
| 252 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, | |
| 253 TestSetProxyConfigs); | |
| 254 | 291 |
| 255 // NetworkChangeNotifier::IPAddressObserver: | 292 // NetworkChangeNotifier::IPAddressObserver: |
| 256 virtual void OnIPAddressChanged() OVERRIDE; | 293 virtual void OnIPAddressChanged() OVERRIDE; |
| 257 | 294 |
| 258 // Underlying implementation of InitDataReductionProxySession(), factored | 295 // Underlying implementation of InitDataReductionProxySession(), factored |
| 259 // out to be testable without creating a full HttpNetworkSession. | 296 // out to be testable without creating a full HttpNetworkSession. |
| 260 static void InitDataReductionAuthentication( | 297 static void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache, |
| 261 net::HttpAuthCache* auth_cache, | 298 const std::string& key); |
| 262 const DataReductionProxyParams* params); | |
| 263 | 299 |
| 264 void OnProxyEnabledPrefChange(); | 300 void OnProxyEnabledPrefChange(); |
| 265 void OnProxyAlternativeEnabledPrefChange(); | |
| 266 | 301 |
| 267 void ResetDataReductionStatistics(); | 302 void ResetDataReductionStatistics(); |
| 268 | 303 |
| 269 void MaybeActivateDataReductionProxy(bool at_startup); | 304 void MaybeActivateDataReductionProxy(bool at_startup); |
| 270 | 305 |
| 271 // Requests the proxy probe URL, if one is set. If unable to do so, disables | 306 // Requests the proxy probe URL, if one is set. If unable to do so, disables |
| 272 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe | 307 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe |
| 273 // failure. | 308 // failure. |
| 274 void ProbeWhetherDataReductionProxyIsAvailable(); | 309 void ProbeWhetherDataReductionProxyIsAvailable(); |
| 310 std::string GetProxyCheckURL(); |
| 275 | 311 |
| 276 // Returns a UTF16 string that's the hash of the configured authentication | 312 // Returns a UTF16 string that's the hash of the configured authentication |
| 277 // |key| and |salt|. Returns an empty UTF16 string if no key is configured or | 313 // |key| and |salt|. Returns an empty UTF16 string if no key is configured or |
| 278 // the data reduction proxy feature isn't available. | 314 // the data reduction proxy feature isn't available. |
| 279 static base::string16 AuthHashForSalt(int64 salt, | 315 static base::string16 AuthHashForSalt(int64 salt, const std::string& key); |
| 280 const std::string& key); | 316 |
| 317 static bool allowed_; |
| 318 static bool promo_allowed_; |
| 281 | 319 |
| 282 std::string key_; | 320 std::string key_; |
| 283 bool restricted_by_carrier_; | 321 bool restricted_by_carrier_; |
| 284 bool enabled_by_user_; | 322 bool enabled_by_user_; |
| 285 | 323 |
| 286 scoped_ptr<net::URLFetcher> fetcher_; | 324 scoped_ptr<net::URLFetcher> fetcher_; |
| 287 BooleanPrefMember spdy_proxy_auth_enabled_; | 325 BooleanPrefMember spdy_proxy_auth_enabled_; |
| 288 BooleanPrefMember data_reduction_proxy_alternative_enabled_; | |
| 289 | 326 |
| 290 PrefService* prefs_; | 327 PrefService* prefs_; |
| 291 PrefService* local_state_prefs_; | 328 PrefService* local_state_prefs_; |
| 292 | 329 |
| 293 net::URLRequestContextGetter* url_request_context_getter_; | 330 net::URLRequestContextGetter* url_request_context_getter_; |
| 294 | 331 |
| 295 scoped_ptr<DataReductionProxyConfigurator> configurator_; | 332 scoped_ptr<DataReductionProxyConfigurator> config_; |
| 296 | 333 |
| 297 base::ThreadChecker thread_checker_; | 334 base::ThreadChecker thread_checker_; |
| 298 | 335 |
| 299 scoped_ptr<DataReductionProxyParams> params_; | 336 bool fallback_allowed_; |
| 300 | 337 |
| 301 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); | 338 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); |
| 302 }; | 339 }; |
| 303 | 340 |
| 304 } // namespace data_reduction_proxy | 341 } // namespace data_reduction_proxy |
| 305 | 342 |
| 306 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS
_H_ | 343 #endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS
_H_ |
| OLD | NEW |