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 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
|
mef
2013/09/24 16:31:17
const CommandLine& command_line = *CommandLine::Fo
bengr
2013/09/24 21:13:34
Done.
| |
| 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) { | |
|
mef
2013/09/24 16:31:17
Does JNI allow marking methods as 'const'?
If yes
bengr
2013/09/24 21:13:34
No. (Or at least not the way chromium uses it.)
| |
| 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()); | |
|
mef
2013/09/24 16:31:17
Suggest simplifying:
std::string spdy_proxy_auth_
bengr
2013/09/24 21:13:34
I'm going to leave this one as is. In general, I l
| |
| 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, &last_update_internal); | |
|
mef
2013/09/24 16:31:17
nit: move &last_update_internal); to new line?
bengr
2013/09/24 21:13:34
Done.
| |
| 272 | |
| 273 return Java_ContentLengths_create(env, | |
| 274 original_content_length, | |
| 275 received_content_length); | |
| 276 } | |
| 277 | |
| 278 ScopedJavaLocalRef<jlongArray> | |
| 279 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | |
| 280 JNIEnv* env, jobject obj) { | |
| 281 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | |
| 282 } | |
| 283 | |
| 284 ScopedJavaLocalRef<jlongArray> | |
| 285 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | |
| 286 JNIEnv* env, jobject obj) { | |
| 287 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | |
| 288 } | |
| 289 | |
| 290 // static | |
| 291 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) { | |
| 292 bool register_natives_impl_result = RegisterNativesImpl(env); | |
| 293 return register_natives_impl_result; | |
| 294 } | |
| 295 | |
| 296 void DataReductionProxySettingsAndroid::OnURLFetchComplete( | |
| 297 const net::URLFetcher* source) { | |
| 298 net::URLRequestStatus status = source->GetStatus(); | |
| 299 if (status.status() == net::URLRequestStatus::FAILED && | |
| 300 status.error() == net::ERR_INTERNET_DISCONNECTED) { | |
| 301 return; | |
| 302 } | |
| 303 | |
| 304 std::string response; | |
| 305 source->GetResponseAsString(&response); | |
| 306 | |
| 307 if ("OK" == response.substr(0, 2)) { | |
| 308 DVLOG(1) << "The data reduction proxy is not blocked."; | |
| 309 | |
| 310 if (enabled_by_user_ && disabled_by_carrier_) { | |
| 311 // The user enabled the proxy, but sometime previously in the session, | |
| 312 // the network operator had blocked the proxy. Now that the network | |
| 313 // operator is unblocking it, configure it to the user's desires. | |
| 314 SetProxyPac(true, false); | |
| 315 } | |
| 316 disabled_by_carrier_ = false; | |
| 317 return; | |
| 318 } | |
| 319 DVLOG(1) << "The data reduction proxy is blocked."; | |
| 320 | |
| 321 if (enabled_by_user_ && !disabled_by_carrier_) { | |
| 322 // Disable the proxy. | |
| 323 SetProxyPac(false, false); | |
| 324 } | |
| 325 disabled_by_carrier_ = true; | |
| 326 } | |
| 327 | |
| 328 void DataReductionProxySettingsAndroid::OnIPAddressChanged() { | |
| 329 if (enabled_by_user_) { | |
| 330 DCHECK(IsProxyAllowed()); | |
| 331 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 332 } | |
| 333 } | |
| 334 | |
| 335 void DataReductionProxySettingsAndroid::OnProxyEnabledPrefChange() { | |
| 336 if (!IsProxyAllowed()) | |
| 337 return; | |
| 338 MaybeActivateDataReductionProxy(false); | |
| 339 } | |
| 340 | |
| 341 void DataReductionProxySettingsAndroid::AddDefaultProxyBypassRules() { | |
| 342 // localhost | |
| 343 AddHostToBypass("localhost"); | |
| 344 AddHostPatternToBypass("localhost.*"); | |
| 345 AddHostToBypass("127.0.0.1"); | |
| 346 AddHostToBypass("::1"); | |
| 347 // TODO(bengr): revisit 192.168.*, 10.*, 172.16.0.0 - 172.31.255.255. The | |
| 348 // concern was that adding these and other rules would add to the processing | |
| 349 // time. | |
| 350 | |
| 351 // TODO(bengr): See http://crbug.com/169959. For some reason the data | |
| 352 // reduction proxy is breaking the omnibox SearchProvider. Remove this rule | |
| 353 // when this is fixed. | |
| 354 AddURLPatternToBypass("http://www.google.com/complete/search*"); | |
| 355 | |
| 356 // Check for proxy availability | |
| 357 std::string proxy_check_url = GetProxyCheckURL(); | |
| 358 if (!proxy_check_url.empty()) { | |
| 359 AddURLPatternToBypass(GetProxyCheckURL()); | |
| 360 } | |
| 361 } | |
| 362 | |
| 363 void DataReductionProxySettingsAndroid::AddURLPatternToBypass( | |
| 364 const std::string& pattern) { | |
| 365 AddPatternToBypass("url", pattern); | |
| 366 } | |
| 367 | |
| 368 void DataReductionProxySettingsAndroid::AddHostPatternToBypass( | |
| 369 const std::string& pattern) { | |
| 370 AddPatternToBypass("host", pattern); | |
| 371 } | |
| 372 | |
| 373 void DataReductionProxySettingsAndroid::AddPatternToBypass( | |
| 374 const std::string& url_or_host, | |
| 375 const std::string& pattern) { | |
| 376 bypass_rules_.push_back( | |
| 377 StringPrintf("shExpMatch(%s, \"%s\")", | |
| 378 url_or_host.c_str(), pattern.c_str())); | |
| 379 } | |
| 380 | |
| 381 void DataReductionProxySettingsAndroid::AddHostToBypass( | |
| 382 const std::string& host) { | |
| 383 bypass_rules_.push_back( | |
| 384 StringPrintf("host == \"%s\"", host.c_str())); | |
| 385 } | |
| 386 | |
| 387 PrefService* DataReductionProxySettingsAndroid::GetOriginalProfilePrefs() { | |
| 388 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
| 389 GetOriginalProfile()->GetPrefs(); | |
| 390 } | |
| 391 | |
| 392 PrefService* DataReductionProxySettingsAndroid::GetLocalStatePrefs() { | |
| 393 return g_browser_process->local_state(); | |
| 394 } | |
| 395 | |
| 396 void DataReductionProxySettingsAndroid::ResetDataReductionStatistics() { | |
| 397 PrefService* prefs = GetLocalStatePrefs(); | |
| 398 if (!prefs) | |
| 399 return; | |
| 400 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); | |
| 401 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); | |
| 402 original_update->Clear(); | |
| 403 received_update->Clear(); | |
| 404 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 405 original_update->AppendString(base::Int64ToString(0)); | |
| 406 received_update->AppendString(base::Int64ToString(0)); | |
| 407 } | |
| 408 } | |
| 409 | |
| 410 void DataReductionProxySettingsAndroid::MaybeActivateDataReductionProxy( | |
| 411 bool at_startup) { | |
| 412 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 413 | |
| 414 if (spdy_proxy_auth_enabled_.GetValue() && | |
| 415 !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { | |
| 416 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); | |
| 417 ResetDataReductionStatistics(); | |
| 418 } | |
| 419 | |
| 420 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); | |
| 421 | |
| 422 // Configure use of the data reduction proxy if it is enabled and the proxy | |
| 423 // origin is non-empty. | |
| 424 enabled_by_user_= | |
| 425 spdy_proxy_auth_enabled_.GetValue() && !spdy_proxy_origin.empty(); | |
| 426 SetProxyPac(enabled_by_user_ && !disabled_by_carrier_, at_startup); | |
| 427 | |
| 428 // Check if the proxy has been disabled explicitly by the carrier. | |
| 429 if (enabled_by_user_) | |
| 430 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 431 } | |
| 432 | |
| 433 void DataReductionProxySettingsAndroid::SetProxyPac(bool enable_spdy_proxy, | |
| 434 bool at_startup) { | |
| 435 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 436 DCHECK(prefs); | |
| 437 // Keys duplicated from proxy_config_dictionary.cc | |
| 438 // TODO(bengr): Move these to proxy_config_dictionary.h and reuse them here. | |
| 439 const char kProxyMode[] = "mode"; | |
| 440 const char kProxyPacURL[] = "pac_url"; | |
| 441 const char kAtStartup[] = "at startup"; | |
| 442 const char kByUser[] = "by user action"; | |
| 443 | |
| 444 DictionaryPrefUpdate update(prefs, prefs::kProxy); | |
| 445 DictionaryValue* dict = update.Get(); | |
| 446 if (enable_spdy_proxy) { | |
| 447 LOG(WARNING) << "SPDY proxy ON " << (at_startup ? kAtStartup : kByUser); | |
| 448 // Convert to a data URI and update the PAC settings. | |
| 449 std::string base64_pac; | |
| 450 base::Base64Encode(GetProxyPacScript(), &base64_pac); | |
| 451 | |
| 452 dict->SetString(kProxyPacURL, | |
| 453 "data:application/x-ns-proxy-autoconfig;base64," + | |
| 454 base64_pac); | |
| 455 dict->SetString(kProxyMode, | |
| 456 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | |
| 457 | |
| 458 if (at_startup) { | |
| 459 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 460 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
| 461 NUM_SPDY_PROXY_AUTH_STATE); | |
| 462 } else if (!has_turned_on_) { | |
| 463 // SPDY proxy auth is turned on by user action for the first time in | |
| 464 // this session. | |
| 465 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 466 SPDY_PROXY_AUTH_ON_BY_USER, | |
| 467 NUM_SPDY_PROXY_AUTH_STATE); | |
| 468 has_turned_on_ = true; | |
| 469 } | |
| 470 } else { | |
| 471 LOG(WARNING) << "SPDY proxy OFF " << (at_startup ? kAtStartup : kByUser); | |
| 472 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
| 473 dict->SetString(kProxyPacURL, ""); | |
| 474 | |
| 475 if (!at_startup && !has_turned_off_) { | |
| 476 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 477 SPDY_PROXY_AUTH_OFF_BY_USER, | |
| 478 NUM_SPDY_PROXY_AUTH_STATE); | |
| 479 has_turned_off_ = true; | |
| 480 } | |
| 481 } | |
| 482 } | |
| 483 | |
| 484 ScopedJavaLocalRef<jlongArray> | |
| 485 DataReductionProxySettingsAndroid::GetDailyContentLengths( | |
| 486 JNIEnv* env, const char* pref_name) { | |
| 487 jlongArray result = env->NewLongArray(spdyproxy::kNumDaysInHistory); | |
| 488 PrefService* local_state = GetLocalStatePrefs(); | |
| 489 if (!local_state) | |
| 490 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 491 | |
| 492 const ListValue* list_value = local_state->GetList(pref_name); | |
| 493 if (list_value->GetSize() != spdyproxy::kNumDaysInHistory) | |
| 494 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 495 | |
| 496 jlong jval[spdyproxy::kNumDaysInHistory]; | |
| 497 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 498 jval[i] = GetInt64PrefValue(*list_value, i); | |
| 499 } | |
| 500 env->SetLongArrayRegion(result, 0, spdyproxy::kNumDaysInHistory, jval); | |
| 501 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 502 } | |
| 503 | |
| 504 void DataReductionProxySettingsAndroid::GetContentLengthsInternal( | |
| 505 unsigned int days, | |
| 506 int64* original_content_length, | |
| 507 int64* received_content_length, | |
| 508 int64* last_update_time) { | |
| 509 DCHECK_LE(days, spdyproxy::kNumDaysInHistory); | |
| 510 PrefService* local_state = GetLocalStatePrefs(); | |
| 511 if (!local_state) { | |
| 512 *original_content_length = 0L; | |
| 513 *received_content_length = 0L; | |
| 514 *last_update_time = 0L; | |
| 515 return; | |
| 516 } | |
| 517 | |
| 518 const ListValue* original_list = | |
| 519 local_state->GetList(prefs::kDailyHttpOriginalContentLength); | |
| 520 const ListValue* received_list = | |
| 521 local_state->GetList(prefs::kDailyHttpReceivedContentLength); | |
| 522 | |
| 523 if (original_list->GetSize() != spdyproxy::kNumDaysInHistory || | |
| 524 received_list->GetSize() != spdyproxy::kNumDaysInHistory) { | |
| 525 *original_content_length = 0L; | |
| 526 *received_content_length = 0L; | |
| 527 *last_update_time = 0L; | |
| 528 return; | |
| 529 } | |
| 530 | |
| 531 int64 orig = 0L; | |
| 532 int64 recv = 0L; | |
| 533 // We include days from the end of the list going backwards. | |
| 534 for (unsigned int i = 0; i < days; ++i) { | |
|
mef
2013/09/24 16:31:17
BUG? What if days > kNumDaysInHistory?
bengr
2013/09/24 21:13:34
There's a DCHECK at the top of the function to cat
| |
| 535 int read_index = spdyproxy::kNumDaysInHistory - 1 - i; | |
|
mef
2013/09/24 16:31:17
i is unsigned, read_index is signed...
Maybe loop
bengr
2013/09/24 21:13:34
Done.
| |
| 536 std::string result; | |
|
mef
2013/09/24 16:31:17
unused?
bengr
2013/09/24 21:13:34
Done.
| |
| 537 orig += GetInt64PrefValue(*original_list, read_index); | |
| 538 recv += GetInt64PrefValue(*received_list, read_index); | |
| 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. | |
| 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_, ") || (") + ")"; | |
|
mef
2013/09/24 16:31:17
Can bypass_rules be influenced by user?
If so, th
bengr
2013/09/24 21:13:34
No. I don't believe that the bypass rules can be i
| |
| 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 | |
| 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 |