Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" | |
| 6 | |
| 7 #include "base/android/build_info.h" | |
| 8 #include "base/android/jni_android.h" | |
| 9 #include "base/android/jni_string.h" | |
| 10 #include "base/base64.h" | |
| 11 #include "base/bind.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/metrics/field_trial.h" | |
| 14 #include "base/metrics/histogram.h" | |
| 15 #include "base/prefs/pref_member.h" | |
| 16 #include "base/prefs/pref_service.h" | |
| 17 #include "base/strings/string_number_conversions.h" | |
| 18 #include "base/strings/string_util.h" | |
| 19 #include "base/strings/stringprintf.h" | |
| 20 #include "base/strings/utf_string_conversions.h" | |
| 21 #include "chrome/browser/browser_process.h" | |
| 22 #include "chrome/browser/prefs/proxy_prefs.h" | |
| 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
| 24 #include "chrome/browser/profiles/profile.h" | |
| 25 #include "chrome/browser/profiles/profile_manager.h" | |
| 26 #include "chrome/common/chrome_switches.h" | |
| 27 #include "chrome/common/pref_names.h" | |
| 28 #include "jni/DataReductionProxySettingsAndroid_jni.h" | |
| 29 #include "net/base/host_port_pair.h" | |
| 30 #include "net/base/load_flags.h" | |
| 31 #include "net/base/net_errors.h" | |
| 32 #include "net/url_request/url_fetcher.h" | |
| 33 #include "net/url_request/url_fetcher_delegate.h" | |
| 34 #include "net/url_request/url_request_status.h" | |
| 35 #include "url/gurl.h" | |
| 36 | |
| 37 using base::android::CheckException; | |
| 38 using base::android::ConvertJavaStringToUTF8; | |
| 39 using base::android::ConvertUTF8ToJavaString; | |
| 40 using base::android::ScopedJavaLocalRef; | |
| 41 using base::FieldTrialList; | |
| 42 using base::StringPrintf; | |
| 43 | |
| 44 | |
| 45 namespace { | |
| 46 | |
| 47 // The C++ definition of enum SpdyProxyAuthState defined in | |
| 48 // tools/histograms/histograms.xml. | |
| 49 // New values should be added at the end before |NUM_SPDY_PROXY_AUTH_STATE|. | |
| 50 enum { | |
| 51 CHROME_STARTUP, | |
| 52 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
| 53 SPDY_PROXY_AUTH_ON_BY_USER, | |
| 54 SPDY_PROXY_AUTH_OFF_BY_USER, | |
| 55 // Used by UMA histograms and should always be the last value. | |
| 56 NUM_SPDY_PROXY_AUTH_STATE | |
| 57 }; | |
| 58 | |
| 59 bool IsProxyOriginSetOnCommandLine() { | |
| 60 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 61 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 62 return true; | |
| 63 } | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 bool IsProxyAllowed() { | |
| 68 return IsProxyOriginSetOnCommandLine() || | |
| 69 (FieldTrialList::FindFullName("DataCompressionProxyRollout") == | |
|
mef
2013/09/19 20:21:44
nit: Probably should define those strings (especia
bengr
2013/09/19 20:31:25
Done for "Enabled". The others only appear once, s
| |
| 70 "Enabled"); | |
| 71 } | |
| 72 | |
| 73 bool IsProxyPromoAllowed() { | |
| 74 return IsProxyOriginSetOnCommandLine() || | |
| 75 (IsProxyAllowed() && | |
| 76 FieldTrialList::FindFullName("DataCompressionProxyPromoVisibility") == | |
| 77 "Enabled"); | |
| 78 } | |
| 79 | |
| 80 int64 GetInt64PrefValue(const ListValue& list_value, size_t index) { | |
| 81 int64 val = 0; | |
| 82 std::string pref_value; | |
| 83 bool rv = list_value.GetString(index, &pref_value); | |
| 84 DCHECK(rv); | |
| 85 if (rv) { | |
| 86 rv = base::StringToInt64(pref_value, &val); | |
| 87 DCHECK(rv); | |
| 88 } | |
| 89 return val; | |
| 90 } | |
| 91 | |
| 92 std::string GetProxyCheckURL(){ | |
| 93 if (!IsProxyAllowed()) | |
| 94 return std::string(); | |
| 95 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 96 if (command_line.HasSwitch(switches::kDataReductionProxyProbeURL)) { | |
| 97 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); | |
| 98 } | |
| 99 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) | |
| 100 return DATA_REDUCTION_PROXY_PROBE_URL; | |
| 101 #else | |
| 102 return std::string(); | |
| 103 #endif | |
| 104 } | |
| 105 | |
| 106 std::string GetDataReductionProxyOriginInternal() { | |
| 107 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 108 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 109 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); | |
| 110 } | |
| 111 #if defined(SPDY_PROXY_AUTH_ORIGIN) | |
| 112 return SPDY_PROXY_AUTH_ORIGIN; | |
| 113 #else | |
| 114 return std::string(); | |
| 115 #endif | |
| 116 } | |
| 117 | |
| 118 std::string GetDataReductionProxyOriginHostPort() { | |
| 119 std::string spdy_proxy = GetDataReductionProxyOriginInternal(); | |
| 120 if (spdy_proxy.empty()) { | |
| 121 DLOG(ERROR) << "A SPDY proxy has not been set."; | |
| 122 return spdy_proxy; | |
| 123 } | |
| 124 // Remove a trailing slash from the proxy string if one exists as well as | |
| 125 // leading HTTPS scheme. | |
| 126 return net::HostPortPair::FromURL(GURL(spdy_proxy)).ToString(); | |
| 127 } | |
| 128 | |
| 129 } // namespace | |
| 130 | |
| 131 | |
| 132 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( | |
| 133 JNIEnv* env, jobject obj) | |
| 134 : has_turned_on_(false), | |
| 135 has_turned_off_(false), | |
| 136 disabled_by_carrier_(false), | |
| 137 enabled_by_user_(false) { | |
| 138 } | |
| 139 | |
| 140 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() { | |
| 141 if (IsProxyAllowed()) | |
| 142 spdy_proxy_auth_enabled_.Destroy(); | |
| 143 } | |
| 144 | |
| 145 void DataReductionProxySettingsAndroid::InitPrefMembers() { | |
| 146 spdy_proxy_auth_enabled_.Init( | |
| 147 prefs::kSpdyProxyAuthEnabled, | |
| 148 GetOriginalProfilePrefs(), | |
| 149 base::Bind(&DataReductionProxySettingsAndroid::OnProxyEnabledPrefChange, | |
| 150 base::Unretained(this))); | |
| 151 } | |
| 152 | |
| 153 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings( | |
| 154 JNIEnv* env, | |
| 155 jobject obj) { | |
| 156 // Disable the proxy if it is not allowed to be used. | |
| 157 if (!IsProxyAllowed()) | |
| 158 return; | |
| 159 | |
| 160 InitPrefMembers(); | |
| 161 | |
| 162 AddDefaultProxyBypassRules(); | |
| 163 net::NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 164 | |
| 165 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 166 | |
| 167 // Setting the kEnableSpdyProxyAuth switch has the same effect as enabling | |
| 168 // the feature via settings, in that once set, the preference will be sticky | |
| 169 // across instances of Chrome. Disabling the feature can only be done through | |
| 170 // the settings menu. | |
| 171 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", CHROME_STARTUP, | |
| 172 NUM_SPDY_PROXY_AUTH_STATE); | |
| 173 if (spdy_proxy_auth_enabled_.GetValue() || | |
| 174 command_line->HasSwitch(switches::kEnableSpdyProxyAuth)) { | |
| 175 MaybeActivateDataReductionProxy(true); | |
| 176 } else { | |
| 177 LOG(WARNING) << "SPDY proxy OFF at startup."; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 void DataReductionProxySettingsAndroid::BypassHostPattern( | |
| 182 JNIEnv* env, jobject obj, jstring pattern) { | |
| 183 AddHostPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
| 184 } | |
| 185 void DataReductionProxySettingsAndroid::BypassURLPattern( | |
| 186 JNIEnv* env, jobject obj, jstring pattern) { | |
| 187 AddURLPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
| 188 } | |
| 189 | |
| 190 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyAllowed( | |
| 191 JNIEnv* env, jobject obj) { | |
| 192 return IsProxyAllowed(); | |
| 193 } | |
| 194 | |
| 195 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAllowed( | |
| 196 JNIEnv* env, jobject obj) { | |
| 197 return IsProxyPromoAllowed(); | |
| 198 } | |
| 199 | |
| 200 ScopedJavaLocalRef<jstring> | |
| 201 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin( | |
| 202 JNIEnv* env, jobject obj) { | |
| 203 return ConvertUTF8ToJavaString(env, GetDataReductionProxyOriginInternal()); | |
| 204 } | |
| 205 | |
| 206 ScopedJavaLocalRef<jstring> | |
| 207 DataReductionProxySettingsAndroid::GetDataReductionProxyAuth( | |
| 208 JNIEnv* env, jobject obj) { | |
| 209 if (!IsProxyAllowed()) | |
| 210 return ConvertUTF8ToJavaString(env, std::string()); | |
| 211 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 212 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 213 // If an origin is provided via a switch, then only consider the value | |
| 214 // that is provided by a switch. Do not use the preprocessor constant. | |
| 215 // Don't expose SPDY_PROXY_AUTH_VALUE to a proxy passed in via the command | |
| 216 // line. | |
| 217 if (command_line.HasSwitch(switches::kSpdyProxyAuthValue)) { | |
| 218 return ConvertUTF8ToJavaString( | |
| 219 env, | |
| 220 command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue)); | |
| 221 } | |
| 222 return ConvertUTF8ToJavaString(env, std::string()); | |
| 223 } | |
| 224 #if defined(SPDY_PROXY_AUTH_VALUE) | |
| 225 return ConvertUTF8ToJavaString(env, SPDY_PROXY_AUTH_VALUE); | |
| 226 #else | |
| 227 return ConvertUTF8ToJavaString(env, std::string()); | |
| 228 #endif | |
| 229 } | |
| 230 | |
| 231 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled( | |
| 232 JNIEnv* env, jobject obj) { | |
| 233 return spdy_proxy_auth_enabled_.GetValue(); | |
| 234 } | |
| 235 | |
| 236 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( | |
| 237 JNIEnv* env, jobject obj) { | |
| 238 return spdy_proxy_auth_enabled_.IsManaged(); | |
| 239 } | |
| 240 | |
| 241 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled( | |
| 242 JNIEnv* env, | |
| 243 jobject obj, | |
| 244 jboolean enabled) { | |
| 245 // Prevent configuring the proxy when it is not allowed to be used. | |
| 246 if (!IsProxyAllowed()) | |
| 247 return; | |
| 248 | |
| 249 spdy_proxy_auth_enabled_.SetValue(enabled); | |
| 250 OnProxyEnabledPrefChange(); | |
| 251 } | |
| 252 | |
| 253 jlong DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime( | |
| 254 JNIEnv* env, jobject obj) { | |
| 255 PrefService* local_state = GetLocalStatePrefs(); | |
| 256 int64 last_update_internal = | |
| 257 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
| 258 base::Time last_update = base::Time::FromInternalValue(last_update_internal); | |
| 259 return static_cast<int64>(last_update.ToJsTime()); | |
| 260 } | |
| 261 | |
| 262 base::android::ScopedJavaLocalRef<jobject> | |
| 263 DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv* env, | |
| 264 jobject obj) { | |
| 265 int64 original_content_length; | |
| 266 int64 received_content_length; | |
| 267 int64 last_update_internal; | |
| 268 GetContentLengthsInternal(spdyproxy::kNumDaysInHistorySummary, | |
| 269 &original_content_length, | |
| 270 &received_content_length, &last_update_internal); | |
| 271 | |
| 272 return Java_ContentLengths_create(env, | |
| 273 original_content_length, | |
| 274 received_content_length); | |
| 275 } | |
| 276 | |
| 277 ScopedJavaLocalRef<jlongArray> | |
| 278 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | |
| 279 JNIEnv* env, jobject obj) { | |
| 280 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | |
| 281 } | |
| 282 | |
| 283 ScopedJavaLocalRef<jlongArray> | |
| 284 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | |
| 285 JNIEnv* env, jobject obj) { | |
| 286 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | |
| 287 } | |
| 288 | |
| 289 // static | |
| 290 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) { | |
| 291 bool register_natives_impl_result = RegisterNativesImpl(env); | |
| 292 return register_natives_impl_result; | |
| 293 } | |
| 294 | |
| 295 void DataReductionProxySettingsAndroid::OnURLFetchComplete( | |
| 296 const net::URLFetcher* source) { | |
| 297 net::URLRequestStatus status = source->GetStatus(); | |
| 298 if (status.status() == net::URLRequestStatus::FAILED && | |
| 299 status.error() == net::ERR_INTERNET_DISCONNECTED) { | |
| 300 return; | |
| 301 } | |
| 302 | |
| 303 std::string response; | |
| 304 source->GetResponseAsString(&response); | |
| 305 | |
| 306 if ("OK" == response.substr(0, 2)) { | |
| 307 DVLOG(1) << "The data reduction proxy is not blocked."; | |
| 308 | |
| 309 if (enabled_by_user_ && disabled_by_carrier_) { | |
| 310 // The user enabled the proxy, but sometime previously in the session, | |
| 311 // the network operator had blocked the proxy. Now that the network | |
| 312 // operator is unblocking it, configure it to the user's desires. | |
| 313 SetProxyPac(true, false); | |
| 314 } | |
| 315 disabled_by_carrier_ = false; | |
| 316 return; | |
| 317 } | |
| 318 DVLOG(1) << "The data reduction proxy is blocked."; | |
| 319 | |
| 320 if (enabled_by_user_ && !disabled_by_carrier_) { | |
| 321 // Disable the proxy. | |
| 322 SetProxyPac(false, false); | |
| 323 } | |
| 324 disabled_by_carrier_ = true; | |
| 325 } | |
| 326 | |
| 327 void DataReductionProxySettingsAndroid::OnIPAddressChanged() { | |
| 328 if (enabled_by_user_) { | |
| 329 DCHECK(IsProxyAllowed()); | |
| 330 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 331 } | |
| 332 } | |
| 333 | |
| 334 void DataReductionProxySettingsAndroid::OnProxyEnabledPrefChange() { | |
| 335 if (!IsProxyAllowed()) | |
| 336 return; | |
| 337 MaybeActivateDataReductionProxy(false); | |
| 338 } | |
| 339 | |
| 340 void DataReductionProxySettingsAndroid::AddDefaultProxyBypassRules() { | |
| 341 // localhost | |
| 342 AddHostToBypass("localhost"); | |
| 343 AddHostPatternToBypass("localhost.*"); | |
| 344 AddHostToBypass("127.0.0.1"); | |
| 345 AddHostToBypass("::1"); | |
| 346 // TODO(bengr): revisit 192.168.*, 10.*, 172.16.0.0 - 172.31.255.255. The | |
| 347 // concern was that adding these and other rules would add to the processing | |
|
mef
2013/09/19 20:21:44
I think those must be added, otherwise how would i
bengr
2013/09/19 20:31:25
The data reduction proxy will issue a proxy-bypass
| |
| 348 // time. | |
| 349 | |
| 350 // TODO(bengr): See http://crbug.com/169959. For some reason the data | |
| 351 // reduction proxy is breaking the omnibox SearchProvider. Remove this rule | |
| 352 // when this is fixed. | |
| 353 AddURLPatternToBypass("http://www.google.com/complete/search*"); | |
| 354 | |
| 355 // Check for proxy availability | |
| 356 std::string proxy_check_url = GetProxyCheckURL(); | |
| 357 if (!proxy_check_url.empty()) { | |
| 358 AddURLPatternToBypass(GetProxyCheckURL()); | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 void DataReductionProxySettingsAndroid::AddURLPatternToBypass( | |
| 363 const std::string& pattern) { | |
| 364 AddPatternToBypass("url", pattern); | |
| 365 } | |
| 366 | |
| 367 void DataReductionProxySettingsAndroid::AddHostPatternToBypass( | |
| 368 const std::string& pattern) { | |
| 369 AddPatternToBypass("host", pattern); | |
| 370 } | |
| 371 | |
| 372 void DataReductionProxySettingsAndroid::AddPatternToBypass( | |
| 373 const std::string& url_or_host, | |
| 374 const std::string& pattern) { | |
| 375 bypass_rules_.push_back( | |
| 376 StringPrintf("shExpMatch(%s, \"%s\")", | |
| 377 url_or_host.c_str(), pattern.c_str())); | |
| 378 } | |
| 379 | |
| 380 void DataReductionProxySettingsAndroid::AddHostToBypass( | |
| 381 const std::string& host) { | |
| 382 bypass_rules_.push_back( | |
| 383 StringPrintf("host == \"%s\"", host.c_str())); | |
| 384 } | |
| 385 | |
| 386 PrefService* DataReductionProxySettingsAndroid::GetOriginalProfilePrefs() { | |
| 387 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
| 388 GetOriginalProfile()->GetPrefs(); | |
| 389 } | |
| 390 | |
| 391 PrefService* DataReductionProxySettingsAndroid::GetLocalStatePrefs() { | |
| 392 return g_browser_process->local_state(); | |
| 393 } | |
| 394 | |
| 395 void DataReductionProxySettingsAndroid::ResetDataReductionStatistics() { | |
| 396 PrefService* prefs = GetLocalStatePrefs(); | |
| 397 if (!prefs) | |
| 398 return; | |
| 399 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); | |
| 400 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); | |
| 401 original_update->Clear(); | |
| 402 received_update->Clear(); | |
| 403 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 404 original_update->AppendString(base::Int64ToString(0)); | |
| 405 received_update->AppendString(base::Int64ToString(0)); | |
| 406 } | |
| 407 } | |
| 408 | |
| 409 void DataReductionProxySettingsAndroid::MaybeActivateDataReductionProxy( | |
| 410 bool at_startup) { | |
| 411 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 412 | |
| 413 if (spdy_proxy_auth_enabled_.GetValue() && | |
| 414 !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { | |
| 415 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); | |
| 416 ResetDataReductionStatistics(); | |
| 417 } | |
| 418 | |
| 419 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); | |
| 420 | |
| 421 // Configure use of the data reduction proxy if it is enabled and the proxy | |
| 422 // origin is non-empty. | |
| 423 enabled_by_user_= | |
| 424 spdy_proxy_auth_enabled_.GetValue() && spdy_proxy_origin != ""; | |
|
mef
2013/09/19 20:21:44
!spdy_proxy_origin.empty()
bengr
2013/09/19 20:31:25
Done.
| |
| 425 SetProxyPac(enabled_by_user_ && !disabled_by_carrier_, at_startup); | |
| 426 | |
| 427 // Check if the proxy has been disabled explicitly by the carrier. | |
| 428 if (enabled_by_user_) | |
| 429 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 430 } | |
| 431 | |
| 432 void DataReductionProxySettingsAndroid::SetProxyPac(bool enable_spdy_proxy, | |
| 433 bool at_startup) { | |
| 434 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 435 DCHECK(prefs); | |
| 436 // Keys duplicated from proxy_config_dictionary.cc | |
| 437 // TODO(bengr): Move these to proxy_config_dictionary.h and reuse them here. | |
| 438 const char kProxyMode[] = "mode"; | |
| 439 const char kProxyPacURL[] = "pac_url"; | |
| 440 const char kAtStartup[] = "at startup"; | |
| 441 const char kByUser[] = "by user action"; | |
| 442 | |
| 443 DictionaryPrefUpdate update(prefs, prefs::kProxy); | |
| 444 DictionaryValue* dict = update.Get(); | |
| 445 if (enable_spdy_proxy) { | |
| 446 LOG(WARNING) << "SPDY proxy ON " << (at_startup ? kAtStartup : kByUser); | |
| 447 // Convert to a data URI and update the PAC settings. | |
| 448 std::string base64_pac; | |
| 449 base::Base64Encode(GetProxyPacScript(), &base64_pac); | |
| 450 | |
| 451 dict->SetString(kProxyPacURL, | |
| 452 "data:application/x-ns-proxy-autoconfig;base64," + | |
| 453 base64_pac); | |
| 454 dict->SetString(kProxyMode, | |
| 455 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | |
| 456 | |
| 457 if (at_startup) { | |
| 458 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 459 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
| 460 NUM_SPDY_PROXY_AUTH_STATE); | |
| 461 } else if (!has_turned_on_) { | |
| 462 // SPDY proxy auth is turned on by user action for the first time in | |
| 463 // this session. | |
| 464 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 465 SPDY_PROXY_AUTH_ON_BY_USER, | |
| 466 NUM_SPDY_PROXY_AUTH_STATE); | |
| 467 has_turned_on_ = true; | |
| 468 } | |
| 469 } else { | |
| 470 LOG(WARNING) << "SPDY proxy OFF " << (at_startup ? kAtStartup : kByUser); | |
| 471 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
| 472 dict->SetString(kProxyPacURL, ""); | |
| 473 | |
| 474 if (!at_startup && !has_turned_off_) { | |
| 475 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 476 SPDY_PROXY_AUTH_OFF_BY_USER, | |
| 477 NUM_SPDY_PROXY_AUTH_STATE); | |
| 478 has_turned_off_ = true; | |
| 479 } | |
| 480 } | |
| 481 } | |
| 482 | |
| 483 ScopedJavaLocalRef<jlongArray> | |
| 484 DataReductionProxySettingsAndroid::GetDailyContentLengths( | |
| 485 JNIEnv* env, const char* pref_name) { | |
| 486 jlongArray result = env->NewLongArray(spdyproxy::kNumDaysInHistory); | |
| 487 PrefService* local_state = GetLocalStatePrefs(); | |
| 488 if (!local_state) | |
| 489 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 490 | |
| 491 const ListValue* list_value = local_state->GetList(pref_name); | |
| 492 if (list_value->GetSize() != spdyproxy::kNumDaysInHistory) | |
| 493 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 494 | |
| 495 jlong jval[spdyproxy::kNumDaysInHistory]; | |
| 496 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 497 jval[i] = GetInt64PrefValue(*list_value, i); | |
| 498 } | |
| 499 env->SetLongArrayRegion(result, 0, spdyproxy::kNumDaysInHistory, jval); | |
| 500 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 501 } | |
| 502 | |
| 503 void DataReductionProxySettingsAndroid::GetContentLengthsInternal( | |
| 504 unsigned int days, | |
| 505 int64* original_content_length, | |
| 506 int64* received_content_length, | |
| 507 int64* last_update_time) { | |
| 508 DCHECK_LE(days, spdyproxy::kNumDaysInHistory); | |
| 509 PrefService* local_state = GetLocalStatePrefs(); | |
| 510 if (!local_state) { | |
| 511 *original_content_length = 0L; | |
| 512 *received_content_length = 0L; | |
| 513 *last_update_time = 0L; | |
| 514 return; | |
| 515 } | |
| 516 | |
| 517 const ListValue* original_list = | |
| 518 local_state->GetList(prefs::kDailyHttpOriginalContentLength); | |
| 519 const ListValue* received_list = | |
| 520 local_state->GetList(prefs::kDailyHttpReceivedContentLength); | |
| 521 | |
| 522 if (original_list->GetSize() != spdyproxy::kNumDaysInHistory || | |
| 523 received_list->GetSize() != spdyproxy::kNumDaysInHistory) { | |
| 524 *original_content_length = 0L; | |
| 525 *received_content_length = 0L; | |
| 526 *last_update_time = 0L; | |
| 527 return; | |
| 528 } | |
| 529 | |
| 530 int64 orig = 0L; | |
| 531 int64 recv = 0L; | |
| 532 // We include days from the end of the list going backwards. | |
| 533 for (unsigned int i = 0; i < days; ++i) { | |
| 534 int read_index = spdyproxy::kNumDaysInHistory - 1 - i; | |
| 535 std::string result; | |
| 536 orig += GetInt64PrefValue(*original_list, read_index); | |
| 537 recv += GetInt64PrefValue(*received_list, read_index); | |
| 538 } | |
| 539 *original_content_length = orig; | |
| 540 *received_content_length = recv; | |
| 541 *last_update_time = | |
| 542 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
| 543 } | |
| 544 | |
| 545 net::URLFetcher* DataReductionProxySettingsAndroid::GetURLFetcher() { | |
| 546 std::string url = GetProxyCheckURL(); | |
| 547 if (url.empty()) | |
| 548 return NULL; | |
| 549 net::URLFetcher* fetcher = net::URLFetcher::Create(GURL(url), | |
| 550 net::URLFetcher::GET, | |
| 551 this); | |
| 552 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
| 553 Profile* profile = g_browser_process->profile_manager()-> | |
| 554 GetDefaultProfile(); | |
| 555 fetcher->SetRequestContext(profile->GetRequestContext()); | |
| 556 // Configure to max_retries at most kMaxRetries times for 5xx errors. | |
| 557 static const int kMaxRetries = 5; | |
| 558 fetcher->SetMaxRetriesOn5xx(kMaxRetries); | |
| 559 return fetcher; | |
| 560 } | |
| 561 | |
| 562 void | |
| 563 DataReductionProxySettingsAndroid::ProbeWhetherDataReductionProxyIsAvailable() { | |
| 564 net::URLFetcher* fetcher = GetURLFetcher(); | |
| 565 if (!fetcher) | |
| 566 return; | |
| 567 fetcher_.reset(fetcher); | |
| 568 fetcher_->Start(); | |
| 569 } | |
| 570 | |
| 571 // TODO(bengr): Replace with our own ProxyResolver. | |
| 572 std::string DataReductionProxySettingsAndroid::GetProxyPacScript() { | |
| 573 std::string bypass_clause = "(" + JoinString(bypass_rules_, ") || (") + ")"; | |
| 574 | |
| 575 // We want to fall back to direct loading when the proxy is unavailable and | |
| 576 // only process HTTP traffic, so we concoct a PAC configuration | |
| 577 // accordingly. (With a statically configured proxy, proxy failures will | |
| 578 // simply result in a connection error presented to users.) | |
| 579 | |
| 580 std::string pac = "function FindProxyForURL(url, host) {" | |
| 581 " if (" + bypass_clause + ") {" | |
| 582 " return \"DIRECT\";" | |
| 583 " } " | |
| 584 " if (url.substring(0, 5) == \"http:\") {" | |
| 585 " return \"HTTPS " + GetDataReductionProxyOriginHostPort() + | |
| 586 "; DIRECT\";" | |
| 587 " }" | |
| 588 " return \"DIRECT\";" | |
| 589 "}"; | |
| 590 return pac; | |
| 591 } | |
| 592 | |
| 593 // Used by generated jni code. | |
| 594 static jint Init(JNIEnv* env, jobject obj) { | |
| 595 DataReductionProxySettingsAndroid* settings = | |
| 596 new DataReductionProxySettingsAndroid(env, obj); | |
| 597 return reinterpret_cast<jint>(settings); | |
| 598 } | |
| 599 | |
| 600 | |
| OLD | NEW |