data_reduction_proxy_settings.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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 3// found in the LICENSE file. 4 5#ifndef COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ 6#define COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ 7 8#include <vector> 9 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 "base/threading/thread_checker.h" 16#include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h" 17#include "net/base/network_change_notifier.h" 18#include "net/url_request/url_fetcher_delegate.h" 19 20class PrefService; 21 22namespace net { 23class AuthChallengeInfo; 24class HostPortPair; 25class HttpAuthCache; 26class HttpNetworkSession; 27class HttpResponseHeaders; 28class URLFetcher; 29class URLRequestContextGetter; 30} 31 32namespace data_reduction_proxy { 33 34// The number of days of bandwidth usage statistics that are tracked. 35const unsigned int kNumDaysInHistory = 60; 36 37// The number of days of bandwidth usage statistics that are presented. 38const unsigned int kNumDaysInHistorySummary = 30; 39 40COMPILE_ASSERT(kNumDaysInHistorySummary <= kNumDaysInHistory, 41 DataReductionProxySettings_summary_too_long); 42 43 // Values of the UMA DataReductionProxy.StartupState histogram. 44 // This enum must remain synchronized with DataReductionProxyStartupState 45 // in metrics/histograms/histograms.xml. 46 enum ProxyStartupState { 47 PROXY_NOT_AVAILABLE = 0, 48 PROXY_DISABLED, 49 PROXY_ENABLED, 50 PROXY_STARTUP_STATE_COUNT, 51 }; 52 53 // Values of the UMA DataReductionProxy.ProbeURL histogram. 54 // This enum must remain synchronized with 55 // DataReductionProxyProbeURLFetchResult in metrics/histograms/histograms.xml. 56 // TODO(marq): Rename these histogram buckets with s/DISABLED/RESTRICTED/, so 57 // their names match the behavior they track. 58 enum ProbeURLFetchResult { 59 // The probe failed because the Internet was disconnected. 60 INTERNET_DISCONNECTED = 0, 61 62 // The probe failed for any other reason, and as a result, the proxy was 63 // disabled. 64 FAILED_PROXY_DISABLED, 65 66 // The probe failed, but the proxy was already restricted. 67 FAILED_PROXY_ALREADY_DISABLED, 68 69 // THe probe succeeded, and as a result the proxy was restricted. 70 SUCCEEDED_PROXY_ENABLED, 71 72 // The probe succeeded, but the proxy was already restricted. 73 SUCCEEDED_PROXY_ALREADY_ENABLED, 74 75 // This must always be last. 76 PROBE_URL_FETCH_RESULT_COUNT 77 }; 78 79// Central point for configuring the data reduction proxy. 80// This object lives on the UI thread and all of its methods are expected to 81// be called from there. 82// TODO(marq): Convert this to be a KeyedService with an 83// associated factory class, and refactor the Java call sites accordingly. 84class DataReductionProxySettings 85 : public net::URLFetcherDelegate, 86 public net::NetworkChangeNotifier::IPAddressObserver { 87 public: 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; 91 92 DataReductionProxySettings(); 93 virtual ~DataReductionProxySettings(); 94 95 // Initializes the data reduction proxy with profile and local state prefs, 96 // and a |UrlRequestContextGetter| for canary probes. The caller must ensure 97 // that all parameters remain alive for the lifetime of the 98 // |DataReductionProxySettings| instance. 99 void InitDataReductionProxySettings( 100 PrefService* prefs, 101 PrefService* local_state_prefs, 102 net::URLRequestContextGetter* url_request_context_getter, 103 scoped_ptr<DataReductionProxyConfigurator> config); 104 105 // If proxy authentication is compiled in, pre-cache authentication 106 // keys for all configured proxies in |session|. 107 static void InitDataReductionProxySession(net::HttpNetworkSession* session); 108 109 // Returns true if the data reduction proxy is allowed to be used on this 110 // instance of Chrome. This could return false, for example, if this instance 111 // is not part of the field trial, or if the proxy name is not configured 112 // via gyp. 113 static bool IsDataReductionProxyAllowed(); 114 115 // Returns true if a screen promoting the data reduction proxy is allowed to 116 // be shown. Logic that decides when to show the promo should check its 117 // availability. This would return false if not part of a separate field 118 // trial that governs the use of the promotion. 119 static bool IsDataReductionProxyPromoAllowed(); 120 121 // Returns true if preconnect advisory hinting is enabled by command line 122 // flag or Finch trial. 123 static bool IsPreconnectHintingAllowed(); 124 125 // Returns the URL of the data reduction proxy. 126 static std::string GetDataReductionProxyOrigin(); 127 128 // Returns the URL of the fallback data reduction proxy. 129 static std::string GetDataReductionProxyFallback(); 130 131 // Returns a vector of GURLs for all configured proxies. 132 static DataReductionProxyList GetDataReductionProxies(); 133 134 // Returns true if |auth_info| represents an authentication challenge from 135 // a compatible, configured proxy. 136 bool IsAcceptableAuthChallenge(net::AuthChallengeInfo* auth_info); 137 138 // Returns a UTF16 string suitable for use as an authentication token in 139 // response to the challenge represented by |auth_info|. If the token can't 140 // be correctly generated for |auth_info|, returns an empty UTF16 string. 141 base::string16 GetTokenForAuthChallenge(net::AuthChallengeInfo* auth_info); 142 143 // Returns true if the proxy is enabled. 144 bool IsDataReductionProxyEnabled(); 145 146 // Returns true if the proxy is managed by an adminstrator's policy. 147 bool IsDataReductionProxyManaged(); 148 149 // 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 151 // probe succeeds. 152 void SetDataReductionProxyEnabled(bool enabled); 153 154 // Returns the time in microseconds that the last update was made to the 155 // daily original and received content lengths. 156 int64 GetDataReductionLastUpdateTime(); 157 158 // Returns a vector containing the total size of all HTTP content that was 159 // received over the last |kNumDaysInHistory| before any compression by the 160 // data reduction proxy. Each element in the vector contains one day of data. 161 ContentLengthList GetDailyOriginalContentLengths(); 162 163 // Returns an vector containing the aggregate received HTTP content in the 164 // last |kNumDaysInHistory| days. 165 ContentLengthList GetDailyReceivedContentLengths(); 166 167 // net::URLFetcherDelegate: 168 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 169 170 protected: 171 void InitPrefMembers(); 172 173 virtual net::URLFetcher* GetURLFetcher(); 174 175 // Virtualized for unit test support. 176 virtual PrefService* GetOriginalProfilePrefs(); 177 virtual PrefService* GetLocalStatePrefs(); 178 179 void GetContentLengths(unsigned int days, 180 int64* original_content_length, 181 int64* received_content_length, 182 int64* last_update_time); 183 ContentLengthList GetDailyContentLengths(const char* pref_name); 184 185 // Sets the proxy configs, enabling or disabling the proxy according to 186 // the value of |enabled|. If |restricted| is true, only enable the fallback 187 // proxy. |at_startup| is true when this method is called from 188 // InitDataReductionProxySettings. 189 virtual void SetProxyConfigs(bool enabled, bool restricted, bool at_startup); 190 191 // Metrics methods. Subclasses should override if they wish to provide 192 // alternate methods. 193 virtual void RecordDataReductionInit(); 194 195 virtual void AddDefaultProxyBypassRules(); 196 197 // Writes a warning to the log that is used in backend processing of 198 // customer feedback. Virtual so tests can mock it for verification. 199 virtual void LogProxyState(bool enabled, bool restricted, bool at_startup); 200 201 // Virtualized for mocking 202 virtual void RecordProbeURLFetchResult( 203 data_reduction_proxy::ProbeURLFetchResult result); 204 virtual void RecordStartupState( 205 data_reduction_proxy::ProxyStartupState state); 206 207 DataReductionProxyConfigurator* config() { 208 return config_.get(); 209 } 210 211 private: 212 friend class DataReductionProxySettingsTestBase; 213 friend class DataReductionProxySettingsTest; 214 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 215 TestAuthenticationInit); 216 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 217 TestAuthHashGeneration); 218 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 219 TestAuthHashGenerationWithOriginSetViaSwitch); 220 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 221 TestResetDataReductionStatistics); 222 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 223 TestIsProxyEnabledOrManaged); 224 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 225 TestContentLengths); 226 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 227 TestGetDailyContentLengths); 228 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 229 TestMaybeActivateDataReductionProxy); 230 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 231 TestOnIPAddressChanged); 232 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 233 TestOnProxyEnabledPrefChange); 234 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 235 TestInitDataReductionProxyOn); 236 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 237 TestInitDataReductionProxyOff); 238 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 239 TestBypassList); 240 FRIEND_TEST_ALL_PREFIXES(DataReductionProxySettingsTest, 241 CheckInitMetricsWhenNotAllowed); 242 243 // NetworkChangeNotifier::IPAddressObserver: 244 virtual void OnIPAddressChanged() OVERRIDE; 245 246 // Underlying implementation of InitDataReductionProxySession(), factored 247 // out to be testable without creating a full HttpNetworkSession. 248 static void InitDataReductionAuthentication(net::HttpAuthCache* auth_cache); 249 250 void OnProxyEnabledPrefChange(); 251 252 void ResetDataReductionStatistics(); 253 254 void MaybeActivateDataReductionProxy(bool at_startup); 255 256 // Requests the proxy probe URL, if one is set. If unable to do so, disables 257 // the proxy, if enabled. Otherwise enables the proxy if disabled by a probe 258 // failure. 259 void ProbeWhetherDataReductionProxyIsAvailable(); 260 std::string GetProxyCheckURL(); 261 262 // Returns a UTF16 string that's the hash of the configured authentication 263 // key and |salt|. Returns an empty UTF16 string if no key is configured or 264 // the data reduction proxy feature isn't available. 265 static base::string16 AuthHashForSalt(int64 salt); 266 267 bool restricted_by_carrier_; 268 bool enabled_by_user_; 269 270 scoped_ptr<net::URLFetcher> fetcher_; 271 BooleanPrefMember spdy_proxy_auth_enabled_; 272 273 PrefService* prefs_; 274 PrefService* local_state_prefs_; 275 276 net::URLRequestContextGetter* url_request_context_getter_; 277 278 scoped_ptr<DataReductionProxyConfigurator> config_; 279 280 base::ThreadChecker thread_checker_; 281 282 DISALLOW_COPY_AND_ASSIGN(DataReductionProxySettings); 283}; 284 285} // namespace data_reduction_proxy 286 287#endif // COMPONENTS_DATA_REDUCTION_PROXY_BROWSER_DATA_REDUCTION_PROXY_SETTINGS_H_ 288