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