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