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