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/host_port_pair.h" | |
28 #include "net/base/load_flags.h" | |
29 #include "net/base/net_errors.h" | |
30 #include "net/url_request/url_fetcher.h" | |
31 #include "net/url_request/url_fetcher_delegate.h" | |
32 #include "net/url_request/url_request_status.h" | |
33 #include "url/gurl.h" | |
34 | |
35 using base::android::CheckException; | |
36 using base::android::ConvertJavaStringToUTF8; | |
37 using base::android::ConvertUTF8ToJavaString; | |
38 using base::android::ScopedJavaLocalRef; | |
39 using base::FieldTrialList; | |
40 using base::StringPrintf; | |
41 | |
42 | |
43 namespace { | |
44 | |
45 // The C++ definition of enum SpdyProxyAuthState defined in | |
46 // tools/histograms/histograms.xml. | |
47 // New values should be added at the end before |NUM_SPDY_PROXY_AUTH_STATE| | |
48 enum { | |
49 CHROME_STARTUP, | |
50 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
51 SPDY_PROXY_AUTH_ON_BY_USER, | |
52 SPDY_PROXY_AUTH_OFF_BY_USER, | |
53 // Used by UMA histograms and should always be the last value. | |
54 NUM_SPDY_PROXY_AUTH_STATE | |
55 }; | |
56 | |
57 bool IsProxyAvailable() { | |
58 return (FieldTrialList::FindFullName("DataCompressionProxyRollout") == | |
59 "Enabled"); | |
60 } | |
61 | |
62 bool IsProxyPromoAvailable() { | |
63 return (IsProxyAvailable() && | |
64 FieldTrialList::FindFullName("DataCompressionProxyPromoVisibility") == | |
65 "Enabled"); | |
66 } | |
67 | |
68 int64 GetInt64PrefValue(const ListValue& list_value, size_t index) { | |
69 int64 val = 0; | |
70 std::string pref_value; | |
71 bool rv = list_value.GetString(index, &pref_value); | |
72 DCHECK(rv); | |
73 if (rv) { | |
74 rv = base::StringToInt64(pref_value, &val); | |
75 DCHECK(rv); | |
76 } | |
77 return val; | |
78 } | |
79 | |
80 std::string GetProxyCheckURL() { | |
81 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) | |
82 return DATA_REDUCTION_PROXY_PROBE_URL; | |
83 #else | |
84 return std::string(); | |
85 #endif | |
86 } | |
87 | |
88 } // namespace | |
89 | |
90 | |
91 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( | |
92 JNIEnv* env, jobject obj) | |
93 : has_turned_on_(false), | |
94 has_turned_off_(false), | |
95 disabled_by_carrier_(false), | |
96 enabled_by_user_(false), | |
97 profile_prefs_for_testing_(NULL), | |
98 local_state_prefs_for_testing_(NULL) { | |
99 } | |
100 | |
101 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() { | |
102 } | |
103 | |
104 void DataReductionProxySettingsAndroid::BypassHostPattern( | |
105 JNIEnv* env, jobject obj, jstring pattern) { | |
106 AddHostPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
107 } | |
108 void DataReductionProxySettingsAndroid::BypassURLPattern( | |
109 JNIEnv* env, jobject obj, jstring pattern) { | |
110 AddURLPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
111 } | |
112 | |
113 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyAvailable( | |
114 JNIEnv* env, jobject obj) { | |
115 return IsProxyAvailable(); | |
116 } | |
117 | |
118 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAvailable( | |
119 JNIEnv* env, jobject obj) { | |
120 return IsProxyPromoAvailable(); | |
121 } | |
122 | |
123 std::string DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin() { | |
124 if (!IsProxyAvailable()) | |
125 return std::string(); | |
126 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
127 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
128 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); | |
129 } | |
130 #if defined(SPDY_PROXY_AUTH_ORIGIN) | |
131 return SPDY_PROXY_AUTH_ORIGIN; | |
132 #else | |
133 return std::string(); | |
134 #endif | |
135 } | |
136 | |
137 ScopedJavaLocalRef<jstring> | |
138 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin( | |
139 JNIEnv* env, jobject obj) { | |
140 return ConvertUTF8ToJavaString(env, GetDataReductionProxyOrigin()); | |
141 } | |
142 | |
143 ScopedJavaLocalRef<jstring> | |
144 DataReductionProxySettingsAndroid::GetDataReductionProxyAuth( | |
145 JNIEnv* env, jobject obj) { | |
146 if (!IsProxyAvailable()) | |
147 return ConvertUTF8ToJavaString(env, std::string()); | |
148 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
149 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
150 // If an origin is provided via a switch, then only consider the value | |
151 // that is provided by a switch. Do not use the preprocessor constant. | |
152 if (command_line.HasSwitch(switches::kSpdyProxyAuthValue)) { | |
153 return ConvertUTF8ToJavaString( | |
154 env, | |
155 command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue)); | |
156 } | |
157 } | |
158 #if defined(SPDY_PROXY_AUTH_VALUE) | |
159 return ConvertUTF8ToJavaString(env, SPDY_PROXY_AUTH_VALUE); | |
160 #else | |
161 return ConvertUTF8ToJavaString(env, std::string()); | |
162 #endif | |
163 } | |
164 | |
165 jlong DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime( | |
166 JNIEnv* env, jobject obj) { | |
167 PrefService* local_state = GetLocalStatePrefs(); | |
168 int64 last_update_internal = | |
169 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
170 base::Time last_update = base::Time::FromInternalValue(last_update_internal); | |
171 return static_cast<int64>(last_update.ToJsTime()); | |
172 } | |
173 | |
174 base::android::ScopedJavaLocalRef<jobject> | |
175 DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv* env, | |
176 jobject obj) { | |
177 int64 original_content_length; | |
178 int64 received_content_length; | |
179 int64 last_update_internal; | |
180 GetContentLengths(spdyproxy::kNumDaysInHistorySummary, | |
181 &original_content_length, | |
182 &received_content_length, &last_update_internal); | |
183 | |
184 return Java_ContentLengths_create(env, | |
185 original_content_length, | |
186 received_content_length); | |
187 } | |
188 | |
189 void DataReductionProxySettingsAndroid::AddDefaultProxyBypassRules() { | |
190 // localhost | |
191 AddHostToBypass("localhost"); | |
192 AddHostPatternToBypass("localhost.*"); | |
193 AddHostToBypass("127.0.0.1"); | |
194 // TODO(bengr): revisit 192.168.*, 10.*, 172.16.0.0 - 172.31.255.255. The | |
195 // concern was that adding these and other rules would add to the processing | |
196 // time. | |
197 | |
198 // TODO(bengr): See http://crbug.com/169959. For some reason the data | |
199 // reduction proxy is breaking the omnibox SearchProvider. Remove this rule | |
200 // when this is fixed. | |
201 AddURLPatternToBypass("http://www.google.com/complete/search*"); | |
202 | |
203 // Check for proxy availability | |
204 std::string proxy_check_url = GetProxyCheckURL(); | |
205 if (!proxy_check_url.empty()) { | |
206 AddURLPatternToBypass(GetProxyCheckURL()); | |
207 } | |
208 } | |
209 | |
210 void DataReductionProxySettingsAndroid::AddURLPatternToBypass( | |
211 const std::string& pattern) { | |
212 AddPatternToBypass("url", pattern); | |
213 } | |
214 | |
215 void DataReductionProxySettingsAndroid::AddHostPatternToBypass( | |
216 const std::string& pattern) { | |
217 AddPatternToBypass("host", pattern); | |
218 } | |
219 | |
220 void DataReductionProxySettingsAndroid::AddPatternToBypass( | |
221 const std::string& url_or_host, | |
222 const std::string& pattern) { | |
223 bypass_rules_.push_back( | |
224 StringPrintf("shExpMatch(%s, \"%s\")", | |
225 url_or_host.c_str(), pattern.c_str())); | |
226 } | |
227 | |
228 void DataReductionProxySettingsAndroid::AddHostToBypass( | |
229 const std::string& host) { | |
230 bypass_rules_.push_back( | |
231 StringPrintf("host == \"%s\"", host.c_str())); | |
232 } | |
233 | |
234 // TODO(bengr): Replace with our own ProxyResolver. | |
235 std::string DataReductionProxySettingsAndroid::GetProxyPacScript() { | |
236 std::string bypass_clause = "(" + JoinString(bypass_rules_, ") || (") + ")"; | |
237 | |
238 // We want to fall back to direct loading when the proxy is unavailable and | |
239 // only process HTTP traffic, so we concoct a PAC configuration | |
240 // accordingly. (With a statically configured proxy, proxy failures will | |
241 // simply result in a connection error presented to users.) | |
242 | |
243 std::string pac = "function FindProxyForURL(url, host) {" | |
244 " if (" + bypass_clause + ") {" | |
245 " return \"DIRECT\";" | |
246 " } " | |
247 " if (url.substring(0, 5) == \"http:\") {" | |
248 " return \"HTTPS " + GetDataReductionProxyOriginHostPort() + | |
249 "; DIRECT\";" | |
250 " }" | |
251 " return \"DIRECT\";" | |
252 "}"; | |
253 return pac; | |
254 } | |
255 | |
256 PrefService* DataReductionProxySettingsAndroid::OriginalProfilePrefs() { | |
257 if (profile_prefs_for_testing_) | |
258 return profile_prefs_for_testing_; | |
259 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
260 GetOriginalProfile()->GetPrefs(); | |
261 } | |
262 | |
263 PrefService* DataReductionProxySettingsAndroid::GetLocalStatePrefs() { | |
264 if (local_state_prefs_for_testing_) | |
265 return local_state_prefs_for_testing_; | |
266 return g_browser_process->local_state(); | |
267 } | |
268 | |
269 void DataReductionProxySettingsAndroid::set_profile_prefs_for_testing( | |
270 PrefService* pref_service) { | |
271 profile_prefs_for_testing_ = pref_service; | |
272 } | |
273 | |
274 void DataReductionProxySettingsAndroid::set_local_state_prefs_for_testing( | |
275 PrefService* local_state) { | |
276 local_state_prefs_for_testing_ = local_state; | |
277 } | |
278 | |
279 std::string | |
280 DataReductionProxySettingsAndroid::GetDataReductionProxyOriginHostPort() { | |
281 std::string spdy_proxy = GetDataReductionProxyOrigin(); | |
282 if (spdy_proxy.empty()) { | |
283 DLOG(ERROR) << "A SPDY proxy has not been set."; | |
284 return spdy_proxy; | |
285 } | |
286 // Remove a trailing slash from the proxy string if one exists as well as | |
287 // leading HTTPS scheme. | |
288 return net::HostPortPair::FromURL(GURL(spdy_proxy)).ToString(); | |
289 } | |
290 | |
291 void DataReductionProxySettingsAndroid::ResetDataReductionStatistics() { | |
292 PrefService* prefs = GetLocalStatePrefs(); | |
293 if (!prefs) | |
294 return; | |
295 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); | |
296 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); | |
297 original_update->Clear(); | |
298 received_update->Clear(); | |
299 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
300 original_update->AppendString(base::Int64ToString(0)); | |
301 received_update->AppendString(base::Int64ToString(0)); | |
302 } | |
303 } | |
304 | |
305 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings( | |
306 JNIEnv* env, | |
307 jobject obj) { | |
308 // Disable the proxy is it's not meant to be available. | |
309 if (!IsProxyAvailable()) | |
310 return; | |
311 | |
312 AddDefaultProxyBypassRules(); | |
313 net::NetworkChangeNotifier::AddIPAddressObserver(this); | |
314 | |
315 PrefService* prefs = OriginalProfilePrefs(); | |
316 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
317 bool spdy_proxy_enabled = prefs->GetBoolean(prefs::kSpdyProxyAuthEnabled); | |
318 | |
319 // Setting the kEnableSpdyProxyAuth switch has the same effect as enabling | |
320 // the feature via settings, in that once set, the preference will be sticky | |
321 // across instances of Chrome. Disabling the feature can only be done through | |
322 // the settings menu. | |
323 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", CHROME_STARTUP, | |
324 NUM_SPDY_PROXY_AUTH_STATE); | |
325 if (command_line->HasSwitch(switches::kEnableSpdyProxyAuth) || | |
326 spdy_proxy_enabled) { | |
327 SetDataReductionProxyEnabled(NULL, NULL, true); | |
328 } else { | |
329 LOG(WARNING) << "SPDY proxy OFF at startup."; | |
330 } | |
331 } | |
332 | |
333 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled( | |
334 JNIEnv* env, jobject obj) { | |
335 return OriginalProfilePrefs()->GetBoolean(prefs::kSpdyProxyAuthEnabled); | |
336 } | |
337 | |
338 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( | |
339 JNIEnv* env, jobject obj) { | |
340 return OriginalProfilePrefs()->IsManagedPreference( | |
341 prefs::kSpdyProxyAuthEnabled); | |
342 } | |
343 | |
344 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled( | |
345 JNIEnv* env, | |
346 jobject obj, | |
347 jboolean enabled) { | |
348 // Prevent configuring the proxy when it is unavailable. | |
349 if (!IsProxyAvailable()) | |
350 return; | |
351 | |
352 // Check if the proxy has been disabled explicitly by the carrier. | |
353 CheckDataReductionProxyIsAvailable(GetProxyCheckURL()); | |
354 | |
355 PrefService* prefs = OriginalProfilePrefs(); | |
356 | |
357 prefs->SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled); | |
358 | |
359 if (enabled && !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { | |
360 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); | |
361 ResetDataReductionStatistics(); | |
362 } | |
363 | |
364 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); | |
365 | |
366 // Configure use of the data reduction proxy if it is enabled and the proxy | |
367 // origin is non-empty. | |
368 enabled_by_user_= enabled && spdy_proxy_origin != ""; | |
369 SetProxyPac(enabled_by_user_, !env); | |
370 } | |
371 | |
372 void DataReductionProxySettingsAndroid::SetProxyPac(bool enable_spdy_proxy, | |
373 bool at_startup) { | |
374 PrefService* prefs = OriginalProfilePrefs(); | |
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 const std::string& url) { | |
429 if (url.empty()) | |
430 return; | |
431 fetcher_.reset(net::URLFetcher::Create(0, GURL(url), | |
432 net::URLFetcher::GET, this)); | |
433 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
434 Profile* profile = g_browser_process->profile_manager()-> | |
435 GetDefaultProfile(); | |
436 fetcher_->SetRequestContext(profile->GetRequestContext()); | |
437 // Configure to max_retries at most kMaxRetries times for 5xx errors. | |
438 static const int kMaxRetries = 5; | |
439 fetcher_->SetMaxRetriesOn5xx(kMaxRetries); | |
440 fetcher_->Start(); | |
441 } | |
442 | |
443 void DataReductionProxySettingsAndroid::OnURLFetchComplete( | |
444 const net::URLFetcher* source) { | |
445 net::URLRequestStatus status = source->GetStatus(); | |
446 if (status.status() == net::URLRequestStatus::FAILED && | |
447 status.error() == net::ERR_INTERNET_DISCONNECTED) { | |
448 return; | |
449 } | |
450 | |
451 std::string response; | |
452 source->GetResponseAsString(&response); | |
453 | |
454 if ("OK" == response.substr(0, 2)) { | |
455 DVLOG(1) << "The data reduction proxy is not blocked."; | |
456 | |
457 if (enabled_by_user_ && disabled_by_carrier_) | |
458 // The user enabled the proxy, but sometime previously in the session, | |
459 // the network operator had blocked the proxy. Now that the network | |
460 // operator is unblocking it, configure it to the user's desires. | |
461 SetProxyPac(true, false); | |
mmenke
2013/09/10 19:54:06
nit: Use braces on multi-line conditional bodies.
bengr
2013/09/11 22:01:02
Done.
| |
462 disabled_by_carrier_ = false; | |
463 return; | |
464 } | |
465 DVLOG(1) << "The data reduction proxy is blocked."; | |
466 | |
467 if (enabled_by_user_ && !disabled_by_carrier_) | |
468 // Disable the proxy. | |
469 SetProxyPac(false, false); | |
mmenke
2013/09/10 19:54:06
nit: Braces on multi-line conditional bodies.
bengr
2013/09/11 22:01:02
Done.
| |
470 disabled_by_carrier_ = true; | |
471 } | |
472 | |
473 ScopedJavaLocalRef<jlongArray> | |
474 DataReductionProxySettingsAndroid::GetDailyContentLengths( | |
475 JNIEnv* env, const char* pref_name) { | |
476 jlongArray result = env->NewLongArray(spdyproxy::kNumDaysInHistory); | |
477 PrefService* local_state = GetLocalStatePrefs(); | |
478 if (!local_state) | |
479 return ScopedJavaLocalRef<jlongArray>(env, result); | |
480 | |
481 const ListValue* list_value = local_state->GetList(pref_name); | |
482 if (list_value->GetSize() != spdyproxy::kNumDaysInHistory) | |
483 return ScopedJavaLocalRef<jlongArray>(env, result); | |
484 | |
485 jlong jval[spdyproxy::kNumDaysInHistory]; | |
486 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
487 jval[i] = GetInt64PrefValue(*list_value, i); | |
488 } | |
489 env->SetLongArrayRegion(result, 0, spdyproxy::kNumDaysInHistory, jval); | |
490 return ScopedJavaLocalRef<jlongArray>(env, result); | |
491 } | |
492 | |
493 ScopedJavaLocalRef<jlongArray> | |
494 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | |
495 JNIEnv* env, jobject obj) { | |
496 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | |
497 } | |
498 | |
499 ScopedJavaLocalRef<jlongArray> | |
500 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | |
501 JNIEnv* env, jobject obj) { | |
502 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | |
503 } | |
504 | |
505 void DataReductionProxySettingsAndroid::GetContentLengths( | |
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 int64 last_update_internal = | |
524 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
525 | |
526 if (original_list->GetSize() != spdyproxy::kNumDaysInHistory || | |
527 received_list->GetSize() != spdyproxy::kNumDaysInHistory) { | |
528 *original_content_length = 0L; | |
529 *received_content_length = 0L; | |
530 *last_update_time = 0L; | |
531 return; | |
532 } | |
533 | |
534 int64 orig = 0L; | |
535 int64 recv = 0L; | |
536 // We include days from the end of the list going backwards. | |
537 for (unsigned int i = 0; i < days; ++i) { | |
538 int read_index = spdyproxy::kNumDaysInHistory - 1 - i; | |
539 std::string result; | |
540 orig += GetInt64PrefValue(*original_list, read_index); | |
541 recv += GetInt64PrefValue(*received_list, read_index); | |
542 } | |
543 *original_content_length = orig; | |
544 *received_content_length = recv; | |
545 *last_update_time = last_update_internal; | |
546 } | |
547 | |
548 // Used by generated jni code. | |
549 static jint Init(JNIEnv* env, jobject obj) { | |
550 DataReductionProxySettingsAndroid* settings = | |
551 new DataReductionProxySettingsAndroid(env, obj); | |
552 return reinterpret_cast<jint>(settings); | |
553 } | |
554 | |
555 // static | |
556 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) { | |
557 bool register_natives_impl_result = RegisterNativesImpl(env); | |
558 return register_natives_impl_result; | |
559 } | |
OLD | NEW |