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 | |
mmenke
2013/09/05 21:34:33
nit: Capitalize C++?
bengr
2013/09/10 00:56:09
Done.
| |
44 // tools/histograms/histograms.xml. | |
mmenke
2013/09/05 21:34:33
Think it's worth mentioning that new values should
bengr
2013/09/10 00:56:09
Done.
| |
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"; | |
mmenke
2013/09/05 21:34:33
Switches should be defined in src/chrome/common/ch
bengr
2013/09/10 00:56:09
Done.
| |
56 | |
57 const unsigned int kNumDaysInHistory = 60; | |
mmenke
2013/09/09 15:40:05
I don't think it's a good idea to have the same co
bengr
2013/09/10 00:56:09
I fixed for here and the tests. I'll fix for the d
| |
58 const unsigned int kNumDaysInHistorySummary = 30; | |
mmenke
2013/09/09 16:01:07
Should have a compile assert that this is less tha
bengr
2013/09/10 00:56:09
Done.
| |
59 | |
60 } // namespace | |
61 | |
62 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( | |
63 JNIEnv* env, jobject obj) : | |
mmenke
2013/09/05 21:34:33
The ":" should go in the next line, and all follow
bengr
2013/09/10 00:56:09
Done.
| |
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( | |
mmenke
2013/09/05 21:34:33
Confused about why this is different from GetDataR
bengr
2013/09/10 00:56:09
It actually doesn't matter because the switch is m
| |
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)); | |
mmenke
2013/09/05 21:34:33
Think this indentation violates the style guide -
bengr
2013/09/10 00:56:09
Done.
| |
94 } | |
95 #if defined(SPDY_PROXY_AUTH_ORIGIN) | |
96 return ConvertUTF8ToJavaString(env, SPDY_PROXY_AUTH_ORIGIN); | |
97 #endif | |
98 return ConvertUTF8ToJavaString(env, std::string()); | |
mmenke
2013/09/09 15:40:05
Suggest replacing the #endif with an #else, to emp
bengr
2013/09/10 00:56:09
Done.
| |
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)); | |
mmenke
2013/09/05 21:34:33
Again, this seems to violate the style guide.
bengr
2013/09/10 00:56:09
Done.
| |
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) { | |
mmenke
2013/09/09 15:40:05
nit: Fix indent.
bengr
2013/09/10 00:56:09
Done.
| |
133 int64 original_content_length; | |
134 int64 received_content_length; | |
135 int64 last_update_internal; | |
136 GetContentLengths(kNumDaysInHistorySummary, &original_content_length, | |
mmenke
2013/09/09 15:40:05
Wonder about this constant being defined in this f
bengr
2013/09/10 00:56:09
Done.
| |
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"); | |
mmenke
2013/09/05 21:34:33
This rule seems to be a subset of the next one.
bengr
2013/09/10 00:56:09
The next one uses shExpMatch underneath, which afa
mmenke
2013/09/10 16:17:29
You're right. The fact that shExpMatch is written
| |
147 AddHostPatternToBypass("localhost.*"); | |
148 AddHostToBypass("127.0.0.1"); | |
mmenke
2013/09/05 21:34:33
What about 192.168.*? 10.*? 172.16.0.0 - 172.31.2
bengr
2013/09/10 00:56:09
I think we didn't bypass these because we were wor
| |
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*"); | |
mmenke
2013/09/05 21:34:33
Why URLPattern here, but URLSubstring above? Havi
bengr
2013/09/10 00:56:09
Done.
| |
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"); | |
mmenke
2013/09/09 15:40:05
I think this return statement could be formatted t
bengr
2013/09/10 00:56:09
Done.
| |
210 } | |
211 | |
212 bool DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAvailable() { | |
213 return (IsDataReductionProxyAvailable() && base::FieldTrialList::FindFullName( | |
214 "DataCompressionProxyPromoVisibility") == "Enabled"); | |
mmenke
2013/09/09 15:40:05
Again, think this could be formatted a bit better.
bengr
2013/09/10 00:56:09
Actually, I'm at a loss for how to format this one
| |
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) {" | |
mmenke
2013/09/05 21:34:33
I'm not an expert, but do we really need a PAC? S
bengr
2013/09/10 00:56:09
This code is what we use currently in stable and s
| |
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 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( | |
344 JNIEnv* env, jobject obj) { | |
345 return GetPrefService()->IsManagedPreference(prefs::kSpdyProxyAuthEnabled); | |
346 } | |
347 | |
348 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled( | |
349 JNIEnv* env, | |
350 jobject obj, | |
351 jboolean enabled) { | |
352 | |
mmenke
2013/09/09 15:40:05
nit: Remove blank line.
bengr
2013/09/10 00:56:09
Done.
| |
353 // Disable the proxy is it's not meant to be available. | |
mmenke
2013/09/09 15:40:05
This comment doesn't seem to be accurate. We don'
bengr
2013/09/10 00:56:09
Done.
| |
354 if (!IsDataReductionProxyAvailable()) return; | |
mmenke
2013/09/09 15:40:05
return should be on its own line.
bengr
2013/09/10 00:56:09
Done.
| |
355 | |
356 // Check if the proxy has been disabled explicitly by the carrier. | |
357 CheckDataReductionProxyIsAvailable(GetProxyCheckURL()); | |
358 | |
359 PrefService* prefs = GetPrefService(); | |
360 | |
361 prefs->SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled); | |
362 | |
363 if (enabled && !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { | |
364 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); | |
365 ResetDataReductionStatistics(); | |
366 } | |
367 | |
368 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); | |
369 | |
370 // Configure use of the data reduction proxy if it is enabled and the proxy | |
371 // origin is non-empty. | |
372 enabled_by_user_= enabled && spdy_proxy_origin != ""; | |
373 SetProxyPac(enabled_by_user_, !env); | |
374 | |
375 } | |
376 | |
377 void DataReductionProxySettingsAndroid::SetProxyPac(bool enable_spdy_proxy, | |
378 bool at_startup) { | |
mmenke
2013/09/09 15:40:05
nit: Fix indent
mmenke
2013/09/09 15:40:05
nit: Fix indent.
bengr
2013/09/10 00:56:09
Done.
bengr
2013/09/10 00:56:09
Done.
| |
379 PrefService* prefs = GetPrefService(); | |
380 DCHECK(prefs); | |
381 // Keys duplicated from proxy_config_dictionary.cc | |
382 // TODO(bengr): Move these to proxy_config_dictionary.h and reuse them here. | |
383 const char kProxyMode[] = "mode"; | |
384 const char kProxyPacURL[] = "pac_url"; | |
385 const char kAtStartup[] = "at startup"; | |
386 const char kByUser[] = "by user action"; | |
387 | |
388 DictionaryPrefUpdate update(prefs, prefs::kProxy); | |
389 DictionaryValue* dict = update.Get(); | |
390 if (enable_spdy_proxy) { | |
391 LOG(WARNING) << "SPDY proxy ON " << (at_startup ? kAtStartup : kByUser); | |
392 // Convert to a data URI and update the PAC settings. | |
393 std::string base64_pac; | |
394 base::Base64Encode(GetProxyPacScript(), &base64_pac); | |
395 | |
396 dict->SetString(kProxyPacURL, | |
397 "data:application/x-ns-proxy-autoconfig;base64," + | |
398 base64_pac); | |
399 dict->SetString(kProxyMode, | |
400 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | |
401 | |
402 if (at_startup) { | |
403 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
404 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
405 NUM_SPDY_PROXY_AUTH_STATE); | |
406 } else if (!has_turned_on_) { | |
407 // SPDY proxy auth is turned on by user action for the first time in | |
408 // this session. | |
409 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
410 SPDY_PROXY_AUTH_ON_BY_USER, | |
411 NUM_SPDY_PROXY_AUTH_STATE); | |
412 has_turned_on_ = true; | |
413 } | |
414 } else { | |
415 LOG(WARNING) << "SPDY proxy OFF " << (at_startup ? kAtStartup : kByUser); | |
416 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
417 dict->SetString(kProxyPacURL, ""); | |
418 | |
419 if (!at_startup && !has_turned_off_) { | |
420 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
421 SPDY_PROXY_AUTH_OFF_BY_USER, | |
422 NUM_SPDY_PROXY_AUTH_STATE); | |
423 has_turned_off_ = true; | |
424 } | |
425 } | |
426 } | |
427 | |
428 void DataReductionProxySettingsAndroid::OnIPAddressChanged() { | |
429 CheckDataReductionProxyIsAvailable(GetProxyCheckURL()); | |
430 } | |
431 | |
432 void DataReductionProxySettingsAndroid::CheckDataReductionProxyIsAvailable( | |
433 std::string url) { | |
mmenke
2013/09/09 15:40:05
This should be a const std::string&, no?
bengr
2013/09/10 00:56:09
Done.
| |
434 fetcher_.reset(net::URLFetcher::Create(0, GURL(url), | |
435 net::URLFetcher::GET, this)); | |
436 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE | | |
437 net::LOAD_IGNORE_ALL_CERT_ERRORS); | |
mmenke
2013/09/09 15:40:05
Why are we ignoring cert errors?
bengr
2013/09/10 00:56:09
Done.
| |
438 Profile* profile = g_browser_process->profile_manager()-> | |
439 GetDefaultProfile(); | |
440 fetcher_->SetRequestContext(profile->GetRequestContext()); | |
441 // Configure to max_retries at most kMaxRetries times for 5xx errors. | |
442 static const int kMaxRetries = 5; | |
443 fetcher_->SetMaxRetriesOn5xx(kMaxRetries); | |
444 fetcher_->Start(); | |
445 } | |
446 | |
447 void DataReductionProxySettingsAndroid::OnURLFetchComplete( | |
448 const net::URLFetcher* source) { | |
449 net::URLRequestStatus status = source->GetStatus(); | |
450 if (status.status() == net::URLRequestStatus::FAILED && | |
451 status.error() == net::ERR_INTERNET_DISCONNECTED) { | |
452 return; | |
453 } | |
454 | |
455 std::string response; | |
456 source->GetResponseAsString(&response); | |
457 | |
458 if ("OK" == response.substr(0, 2)) { | |
459 DVLOG(1) << "The data reduction proxy is not blocked."; | |
460 | |
461 // The user enabled the proxy, but sometime previously in the session, | |
462 // the carrier had disabled the proxy. Now that the carrier is enabling | |
463 // it, configure it to the user's desires. | |
464 if (enabled_by_user_ && disabled_by_carrier_) | |
465 SetProxyPac(true, false); | |
466 disabled_by_carrier_ = false; | |
467 return; | |
468 } | |
469 DVLOG(1) << "The data reduction proxy is blocked."; | |
470 // Disable the proxy. | |
471 if (enabled_by_user_ && !disabled_by_carrier_) | |
472 SetProxyPac(false, false); | |
473 disabled_by_carrier_ = true; | |
474 } | |
475 | |
476 std::string DataReductionProxySettingsAndroid::GetProxyCheckURL() { | |
477 std::string origin = "http://" + GetDataReductionProxyOriginHostPort(); | |
478 GURL gurl(origin); | |
479 return gurl.scheme() + "://enabled." + gurl.host() + gurl.path() + "connect"; | |
mmenke
2013/09/09 15:40:05
There's a class call GURL::Replacements, or someth
bengr
2013/09/10 00:56:09
Done.
| |
480 } | |
481 | |
482 ScopedJavaLocalRef<jlongArray> | |
483 DataReductionProxySettingsAndroid::GetDailyContentLengths( | |
484 JNIEnv* env, const char* pref_name) { | |
485 jlongArray result = env->NewLongArray(kNumDaysInHistory); | |
mmenke
2013/09/09 16:01:07
nit: Fix indent.
bengr
2013/09/10 00:56:09
Done.
| |
486 PrefService* local_state = GetLocalState(); | |
487 if (!local_state) | |
488 return ScopedJavaLocalRef<jlongArray>(env, result); | |
489 | |
490 const ListValue* list_value = local_state->GetList(pref_name); | |
mmenke
2013/09/09 16:01:07
This guaranteed to exist and be a list?
bengr
2013/09/10 00:56:09
Yes. We'll hit a NOTREACHED if the pref isn't regi
| |
491 if (list_value->GetSize() != kNumDaysInHistory) | |
492 return ScopedJavaLocalRef<jlongArray>(env, result); | |
493 | |
494 jlong jval[kNumDaysInHistory]; | |
495 for (size_t i = 0; i < kNumDaysInHistory; ++i) { | |
496 int64 val = 0; | |
497 std::string pref_value; | |
498 bool rv = list_value->GetString(i, &pref_value); | |
499 DCHECK(rv); | |
500 if (rv) { | |
501 rv = base::StringToInt64(pref_value, &val); | |
502 DCHECK(rv); | |
503 } | |
mmenke
2013/09/09 16:01:07
Suggest just making the body of this loop a functi
bengr
2013/09/10 00:56:09
Done.
| |
504 jval[i] = val; | |
505 } | |
506 env->SetLongArrayRegion(result, 0, kNumDaysInHistory, jval); | |
507 return ScopedJavaLocalRef<jlongArray>(env, result); | |
508 } | |
509 | |
510 ScopedJavaLocalRef<jlongArray> | |
511 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | |
512 JNIEnv* env, jobject obj) { | |
513 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | |
514 } | |
515 | |
516 ScopedJavaLocalRef<jlongArray> | |
517 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | |
518 JNIEnv* env, jobject obj) { | |
519 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | |
520 } | |
521 | |
522 void DataReductionProxySettingsAndroid::GetContentLengths( | |
523 int days, | |
524 int64* original_content_length, | |
525 int64* received_content_length, | |
526 int64* last_update_time) { | |
mmenke
2013/09/09 16:01:07
Should DCHECK that days is no more than kDaysInHsi
bengr
2013/09/10 00:56:09
Done.
| |
527 PrefService* local_state = GetLocalState(); | |
528 if (!local_state) { | |
529 *original_content_length = 0L; | |
530 *received_content_length = 0L; | |
531 *last_update_time = 0L; | |
532 return; | |
533 } | |
534 | |
535 const ListValue* original_list = | |
536 local_state->GetList(prefs::kDailyHttpOriginalContentLength); | |
537 const ListValue* received_list = | |
538 local_state->GetList(prefs::kDailyHttpReceivedContentLength); | |
539 int64 last_update_internal = | |
540 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
541 | |
542 if (original_list->GetSize() != kNumDaysInHistory || | |
543 received_list->GetSize() != kNumDaysInHistory) { | |
mmenke
2013/09/09 16:01:07
Are we guaranteed these will exists and be lists?
bengr
2013/09/10 00:56:09
Yes. We'll hit a NOTREACHED if the pref isn't regi
| |
544 *original_content_length = 0L; | |
545 *received_content_length = 0L; | |
546 *last_update_time = 0L; | |
547 return; | |
548 } | |
549 | |
550 int64 orig = 0L; | |
551 int64 recv = 0L; | |
552 DCHECK_GE(static_cast<int>(kNumDaysInHistory), days); | |
553 DCHECK_LE(0, days); | |
554 // We include days from the end of the list going backwards. | |
555 for (int i = 0; i < days; ++i) { | |
556 int read_index = kNumDaysInHistory - 1 - i; | |
557 std::string result("0"); | |
mmenke
2013/09/09 16:01:07
Initialization to 0 doesn't seem to serve any purp
bengr
2013/09/10 00:56:09
Done.
| |
558 int64 val; | |
559 if (original_list->GetString(read_index, &result)) { | |
560 if (base::StringToInt64(result, &val)) | |
561 orig +=val; | |
mmenke
2013/09/09 16:01:07
nit: Add space.
bengr
2013/09/10 00:56:09
Done.
| |
562 else | |
563 DCHECK(false); | |
mmenke
2013/09/09 16:01:07
These should be NOTREACHED().
bengr
2013/09/10 00:56:09
Done.
| |
564 } else { | |
565 DCHECK(false); | |
566 } | |
567 if (received_list->GetString(read_index, &result)) { | |
568 if (base::StringToInt64(result, &val)) | |
569 recv +=val; | |
mmenke
2013/09/09 16:01:07
nit: Add space.
bengr
2013/09/10 00:56:09
Done.
| |
570 else | |
571 DCHECK(false); | |
572 } else { | |
573 DCHECK(false); | |
574 } | |
575 } | |
576 *original_content_length = orig; | |
577 *received_content_length = recv; | |
578 *last_update_time = last_update_internal; | |
579 } | |
580 | |
581 static jint Init(JNIEnv* env, jobject obj) { | |
mmenke
2013/09/09 16:01:07
This does seem to be used anywere.
bengr
2013/09/10 00:56:09
This is used by generated jni code. I've added a c
| |
582 DataReductionProxySettingsAndroid* settings = | |
583 new DataReductionProxySettingsAndroid(env, obj); | |
584 return reinterpret_cast<jint>(settings); | |
585 } | |
586 | |
587 // static | |
588 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) { | |
589 bool register_natives_impl_result = RegisterNativesImpl(env); | |
590 return register_natives_impl_result; | |
591 } | |
OLD | NEW |