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