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