Chromium Code Reviews| 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_change_registrar.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| | |
|
mmenke
2013/09/17 20:25:25
nit: +period.
bengr
2013/09/18 22:15:26
Done.
| |
| 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 bool IsProxyOriginSetOnCommandLine() { | |
| 60 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 61 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 62 return true; | |
| 63 } | |
| 64 return false; | |
| 65 } | |
| 66 | |
| 67 bool IsProxyAllowed() { | |
| 68 return IsProxyOriginSetOnCommandLine() || | |
| 69 (FieldTrialList::FindFullName("DataCompressionProxyRollout") == | |
| 70 "Enabled"); | |
| 71 } | |
| 72 | |
| 73 bool IsProxyPromoAllowed() { | |
| 74 return IsProxyOriginSetOnCommandLine() || | |
| 75 (IsProxyAllowed() && | |
| 76 FieldTrialList::FindFullName("DataCompressionProxyPromoVisibility") == | |
| 77 "Enabled"); | |
| 78 } | |
| 79 | |
| 80 int64 GetInt64PrefValue(const ListValue& list_value, size_t index) { | |
| 81 int64 val = 0; | |
| 82 std::string pref_value; | |
| 83 bool rv = list_value.GetString(index, &pref_value); | |
| 84 DCHECK(rv); | |
| 85 if (rv) { | |
| 86 rv = base::StringToInt64(pref_value, &val); | |
| 87 DCHECK(rv); | |
| 88 } | |
| 89 return val; | |
| 90 } | |
| 91 | |
| 92 std::string GetProxyCheckURL(){ | |
| 93 if (!IsProxyAllowed()) | |
| 94 return std::string(); | |
| 95 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 96 if (command_line.HasSwitch(switches::kDataReductionProxyProbeURL)) { | |
| 97 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); | |
|
mmenke
2013/09/17 20:25:25
switches::kDataReductionProxyProbeURL?
bengr
2013/09/18 22:15:26
Done. (Sorry, I caught this one after uploading an
| |
| 98 } | |
| 99 #if defined(DATA_REDUCTION_PROXY_PROBE_URL) | |
| 100 return DATA_REDUCTION_PROXY_PROBE_URL; | |
| 101 #else | |
| 102 return std::string(); | |
| 103 #endif | |
| 104 } | |
| 105 | |
| 106 } // namespace | |
| 107 | |
| 108 | |
| 109 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( | |
| 110 JNIEnv* env, jobject obj) | |
| 111 : has_turned_on_(false), | |
| 112 has_turned_off_(false), | |
| 113 disabled_by_carrier_(false), | |
| 114 enabled_by_user_(false) { | |
| 115 } | |
| 116 | |
| 117 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() { | |
| 118 } | |
| 119 | |
| 120 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings( | |
| 121 JNIEnv* env, | |
| 122 jobject obj) { | |
| 123 // Disable the proxy if it is not allowed to be used. | |
| 124 if (!IsProxyAllowed()) | |
| 125 return; | |
| 126 | |
| 127 pref_change_registrar_.Init(GetOriginalProfilePrefs()); | |
| 128 pref_change_registrar_.Add( | |
| 129 prefs::kSpdyProxyAuthEnabled, | |
| 130 base::Bind(&DataReductionProxySettingsAndroid::OnProxyEnabledPrefChange, | |
| 131 base::Unretained(this))); | |
|
mmenke
2013/09/17 20:25:25
Rather than use both a registrar and a pref lookup
bengr
2013/09/18 22:15:26
Done.
| |
| 132 | |
| 133 AddDefaultProxyBypassRules(); | |
| 134 net::NetworkChangeNotifier::AddIPAddressObserver(this); | |
| 135 | |
| 136 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 137 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 138 bool spdy_proxy_enabled = prefs->GetBoolean(prefs::kSpdyProxyAuthEnabled); | |
| 139 | |
| 140 // Setting the kEnableSpdyProxyAuth switch has the same effect as enabling | |
| 141 // the feature via settings, in that once set, the preference will be sticky | |
| 142 // across instances of Chrome. Disabling the feature can only be done through | |
| 143 // the settings menu. | |
| 144 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", CHROME_STARTUP, | |
| 145 NUM_SPDY_PROXY_AUTH_STATE); | |
| 146 if (command_line->HasSwitch(switches::kEnableSpdyProxyAuth) || | |
| 147 spdy_proxy_enabled) { | |
|
mmenke
2013/09/17 20:25:25
nit: Checking spdy_proxy_enabled_ first is margin
bengr
2013/09/18 22:15:26
Done.
| |
| 148 MaybeActivateDataReductionProxy(true); | |
| 149 } else { | |
| 150 LOG(WARNING) << "SPDY proxy OFF at startup."; | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 void DataReductionProxySettingsAndroid::BypassHostPattern( | |
| 155 JNIEnv* env, jobject obj, jstring pattern) { | |
| 156 AddHostPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
| 157 } | |
| 158 void DataReductionProxySettingsAndroid::BypassURLPattern( | |
| 159 JNIEnv* env, jobject obj, jstring pattern) { | |
| 160 AddURLPatternToBypass(ConvertJavaStringToUTF8(env, pattern)); | |
| 161 } | |
| 162 | |
| 163 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyAllowed( | |
| 164 JNIEnv* env, jobject obj) { | |
| 165 return IsProxyAllowed(); | |
| 166 } | |
| 167 | |
| 168 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAllowed( | |
| 169 JNIEnv* env, jobject obj) { | |
| 170 return IsProxyPromoAllowed(); | |
| 171 } | |
| 172 | |
| 173 ScopedJavaLocalRef<jstring> | |
| 174 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin( | |
| 175 JNIEnv* env, jobject obj) { | |
| 176 return ConvertUTF8ToJavaString(env, GetDataReductionProxyOriginInternal()); | |
| 177 } | |
| 178 | |
| 179 ScopedJavaLocalRef<jstring> | |
| 180 DataReductionProxySettingsAndroid::GetDataReductionProxyAuth( | |
| 181 JNIEnv* env, jobject obj) { | |
| 182 if (!IsProxyAllowed()) | |
| 183 return ConvertUTF8ToJavaString(env, std::string()); | |
| 184 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 185 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 186 // If an origin is provided via a switch, then only consider the value | |
| 187 // that is provided by a switch. Do not use the preprocessor constant. | |
| 188 if (command_line.HasSwitch(switches::kSpdyProxyAuthValue)) { | |
| 189 return ConvertUTF8ToJavaString( | |
| 190 env, | |
| 191 command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthValue)); | |
| 192 } | |
| 193 return ConvertUTF8ToJavaString(env, std::string()); | |
|
mmenke
2013/09/17 20:25:25
Think this is worth a comment (Just something alon
bengr
2013/09/18 22:15:26
Done.
| |
| 194 } | |
| 195 #if defined(SPDY_PROXY_AUTH_VALUE) | |
| 196 return ConvertUTF8ToJavaString(env, SPDY_PROXY_AUTH_VALUE); | |
| 197 #else | |
| 198 return ConvertUTF8ToJavaString(env, std::string()); | |
| 199 #endif | |
| 200 } | |
| 201 | |
| 202 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled( | |
| 203 JNIEnv* env, jobject obj) { | |
| 204 return GetOriginalProfilePrefs()->GetBoolean(prefs::kSpdyProxyAuthEnabled); | |
| 205 } | |
| 206 | |
| 207 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( | |
| 208 JNIEnv* env, jobject obj) { | |
| 209 return GetOriginalProfilePrefs()->IsManagedPreference( | |
| 210 prefs::kSpdyProxyAuthEnabled); | |
| 211 } | |
| 212 | |
| 213 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled( | |
|
mmenke
2013/09/17 20:25:25
Are there already Java functions to set preference
bengr
2013/09/18 22:15:26
This is how Chrome for Android does things.
| |
| 214 JNIEnv* env, | |
| 215 jobject obj, | |
| 216 jboolean enabled) { | |
| 217 // Prevent configuring the proxy when it is not allowed to be used. | |
| 218 if (!IsProxyAllowed()) | |
| 219 return; | |
| 220 | |
| 221 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 222 prefs->SetBoolean(prefs::kSpdyProxyAuthEnabled, enabled); | |
| 223 } | |
| 224 | |
| 225 jlong DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime( | |
| 226 JNIEnv* env, jobject obj) { | |
| 227 PrefService* local_state = GetLocalStatePrefs(); | |
| 228 int64 last_update_internal = | |
| 229 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
| 230 base::Time last_update = base::Time::FromInternalValue(last_update_internal); | |
| 231 return static_cast<int64>(last_update.ToJsTime()); | |
| 232 } | |
| 233 | |
| 234 base::android::ScopedJavaLocalRef<jobject> | |
| 235 DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv* env, | |
| 236 jobject obj) { | |
| 237 int64 original_content_length; | |
| 238 int64 received_content_length; | |
| 239 int64 last_update_internal; | |
| 240 GetContentLengthsInternal(spdyproxy::kNumDaysInHistorySummary, | |
| 241 &original_content_length, | |
| 242 &received_content_length, &last_update_internal); | |
| 243 | |
| 244 return Java_ContentLengths_create(env, | |
| 245 original_content_length, | |
| 246 received_content_length); | |
| 247 } | |
| 248 | |
| 249 ScopedJavaLocalRef<jlongArray> | |
| 250 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | |
| 251 JNIEnv* env, jobject obj) { | |
| 252 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | |
| 253 } | |
| 254 | |
| 255 ScopedJavaLocalRef<jlongArray> | |
| 256 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | |
| 257 JNIEnv* env, jobject obj) { | |
| 258 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | |
| 259 } | |
| 260 | |
| 261 // static | |
| 262 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) { | |
| 263 bool register_natives_impl_result = RegisterNativesImpl(env); | |
| 264 return register_natives_impl_result; | |
| 265 } | |
| 266 | |
| 267 void DataReductionProxySettingsAndroid::OnURLFetchComplete( | |
| 268 const net::URLFetcher* source) { | |
| 269 net::URLRequestStatus status = source->GetStatus(); | |
| 270 if (status.status() == net::URLRequestStatus::FAILED && | |
| 271 status.error() == net::ERR_INTERNET_DISCONNECTED) { | |
| 272 return; | |
| 273 } | |
| 274 | |
| 275 std::string response; | |
| 276 source->GetResponseAsString(&response); | |
| 277 | |
| 278 if ("OK" == response.substr(0, 2)) { | |
| 279 DVLOG(1) << "The data reduction proxy is not blocked."; | |
| 280 | |
| 281 if (enabled_by_user_ && disabled_by_carrier_) { | |
| 282 // The user enabled the proxy, but sometime previously in the session, | |
| 283 // the network operator had blocked the proxy. Now that the network | |
| 284 // operator is unblocking it, configure it to the user's desires. | |
| 285 SetProxyPac(true, false); | |
| 286 } | |
| 287 disabled_by_carrier_ = false; | |
| 288 return; | |
| 289 } | |
| 290 DVLOG(1) << "The data reduction proxy is blocked."; | |
| 291 | |
| 292 if (enabled_by_user_ && !disabled_by_carrier_) { | |
| 293 // Disable the proxy. | |
| 294 SetProxyPac(false, false); | |
| 295 } | |
| 296 disabled_by_carrier_ = true; | |
| 297 } | |
| 298 | |
| 299 void DataReductionProxySettingsAndroid::OnIPAddressChanged() { | |
| 300 if (enabled_by_user_) | |
|
mmenke
2013/09/17 20:25:25
DCHECK(IsProxyAllowed())? We should maintain some
bengr
2013/09/18 22:15:26
Done.
| |
| 301 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 302 } | |
| 303 | |
| 304 void DataReductionProxySettingsAndroid::OnProxyEnabledPrefChange() { | |
|
mmenke
2013/09/17 20:25:25
Should we just exit of not Allowed?
mmenke
2013/09/17 21:51:51
Err.... Or "kEnableSpdyProxyAuth" is set - either
bengr
2013/09/18 22:15:26
Done.
bengr
2013/09/18 22:15:26
One reason to separate is for the UI. I.e., we sho
| |
| 305 MaybeActivateDataReductionProxy(false); | |
| 306 } | |
| 307 | |
| 308 void DataReductionProxySettingsAndroid::AddDefaultProxyBypassRules() { | |
| 309 // localhost | |
| 310 AddHostToBypass("localhost"); | |
| 311 AddHostPatternToBypass("localhost.*"); | |
| 312 AddHostToBypass("127.0.0.1"); | |
| 313 AddHostToBypass("[::1]"); | |
|
mmenke
2013/09/17 20:25:25
I believe you can use about:net-internals#events.
bengr
2013/09/18 22:15:26
Done.
| |
| 314 // TODO(bengr): revisit 192.168.*, 10.*, 172.16.0.0 - 172.31.255.255. The | |
| 315 // concern was that adding these and other rules would add to the processing | |
| 316 // time. | |
| 317 | |
| 318 // TODO(bengr): See http://crbug.com/169959. For some reason the data | |
| 319 // reduction proxy is breaking the omnibox SearchProvider. Remove this rule | |
| 320 // when this is fixed. | |
| 321 AddURLPatternToBypass("http://www.google.com/complete/search*"); | |
| 322 | |
| 323 // Check for proxy availability | |
| 324 std::string proxy_check_url = GetProxyCheckURL(); | |
| 325 if (!proxy_check_url.empty()) { | |
| 326 AddURLPatternToBypass(GetProxyCheckURL()); | |
| 327 } | |
| 328 } | |
| 329 | |
| 330 void DataReductionProxySettingsAndroid::AddURLPatternToBypass( | |
| 331 const std::string& pattern) { | |
| 332 AddPatternToBypass("url", pattern); | |
| 333 } | |
| 334 | |
| 335 void DataReductionProxySettingsAndroid::AddHostPatternToBypass( | |
| 336 const std::string& pattern) { | |
| 337 AddPatternToBypass("host", pattern); | |
| 338 } | |
| 339 | |
| 340 void DataReductionProxySettingsAndroid::AddPatternToBypass( | |
| 341 const std::string& url_or_host, | |
| 342 const std::string& pattern) { | |
| 343 bypass_rules_.push_back( | |
| 344 StringPrintf("shExpMatch(%s, \"%s\")", | |
| 345 url_or_host.c_str(), pattern.c_str())); | |
| 346 } | |
| 347 | |
| 348 void DataReductionProxySettingsAndroid::AddHostToBypass( | |
| 349 const std::string& host) { | |
| 350 bypass_rules_.push_back( | |
| 351 StringPrintf("host == \"%s\"", host.c_str())); | |
| 352 } | |
| 353 | |
| 354 PrefService* DataReductionProxySettingsAndroid::GetOriginalProfilePrefs() { | |
| 355 return g_browser_process->profile_manager()->GetDefaultProfile()-> | |
| 356 GetOriginalProfile()->GetPrefs(); | |
| 357 } | |
| 358 | |
| 359 PrefService* DataReductionProxySettingsAndroid::GetLocalStatePrefs() { | |
| 360 return g_browser_process->local_state(); | |
| 361 } | |
| 362 | |
| 363 std::string | |
| 364 DataReductionProxySettingsAndroid::GetDataReductionProxyOriginInternal() { | |
|
mmenke
2013/09/17 20:25:25
Looks like this function and the next one can be p
bengr
2013/09/18 22:15:26
Done.
| |
| 365 if (!IsProxyAllowed()) | |
| 366 return std::string(); | |
|
mmenke
2013/09/17 20:25:25
Is this default behavior needed? Doesn't seem lik
bengr
2013/09/18 22:15:26
Done.
| |
| 367 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 368 if (command_line.HasSwitch(switches::kSpdyProxyAuthOrigin)) { | |
| 369 return command_line.GetSwitchValueASCII(switches::kSpdyProxyAuthOrigin); | |
| 370 } | |
| 371 #if defined(SPDY_PROXY_AUTH_ORIGIN) | |
| 372 return SPDY_PROXY_AUTH_ORIGIN; | |
| 373 #else | |
| 374 return std::string(); | |
| 375 #endif | |
| 376 } | |
| 377 | |
| 378 std::string | |
| 379 DataReductionProxySettingsAndroid::GetDataReductionProxyOriginHostPort() { | |
| 380 std::string spdy_proxy = GetDataReductionProxyOriginInternal(); | |
| 381 if (spdy_proxy.empty()) { | |
| 382 DLOG(ERROR) << "A SPDY proxy has not been set."; | |
| 383 return spdy_proxy; | |
| 384 } | |
| 385 // Remove a trailing slash from the proxy string if one exists as well as | |
| 386 // leading HTTPS scheme. | |
| 387 return net::HostPortPair::FromURL(GURL(spdy_proxy)).ToString(); | |
| 388 } | |
| 389 | |
| 390 void DataReductionProxySettingsAndroid::ResetDataReductionStatistics() { | |
| 391 PrefService* prefs = GetLocalStatePrefs(); | |
| 392 if (!prefs) | |
| 393 return; | |
| 394 ListPrefUpdate original_update(prefs, prefs::kDailyHttpOriginalContentLength); | |
| 395 ListPrefUpdate received_update(prefs, prefs::kDailyHttpReceivedContentLength); | |
| 396 original_update->Clear(); | |
| 397 received_update->Clear(); | |
| 398 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 399 original_update->AppendString(base::Int64ToString(0)); | |
| 400 received_update->AppendString(base::Int64ToString(0)); | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 void DataReductionProxySettingsAndroid::MaybeActivateDataReductionProxy( | |
| 405 bool at_startup) { | |
| 406 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 407 bool enabled = prefs->GetBoolean(prefs::kSpdyProxyAuthEnabled); | |
| 408 | |
| 409 if (enabled && !prefs->GetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore)) { | |
| 410 prefs->SetBoolean(prefs::kSpdyProxyAuthWasEnabledBefore, true); | |
| 411 ResetDataReductionStatistics(); | |
| 412 } | |
| 413 | |
| 414 std::string spdy_proxy_origin = GetDataReductionProxyOriginHostPort(); | |
| 415 | |
| 416 // Configure use of the data reduction proxy if it is enabled and the proxy | |
| 417 // origin is non-empty. | |
| 418 enabled_by_user_= enabled && spdy_proxy_origin != ""; | |
| 419 SetProxyPac(enabled_by_user_ && !disabled_by_carrier_, at_startup); | |
| 420 | |
| 421 // Check if the proxy has been disabled explicitly by the carrier. | |
| 422 if (enabled_by_user_) | |
| 423 ProbeWhetherDataReductionProxyIsAvailable(); | |
| 424 } | |
| 425 | |
| 426 void DataReductionProxySettingsAndroid::SetProxyPac(bool enable_spdy_proxy, | |
| 427 bool at_startup) { | |
| 428 PrefService* prefs = GetOriginalProfilePrefs(); | |
| 429 DCHECK(prefs); | |
| 430 // Keys duplicated from proxy_config_dictionary.cc | |
| 431 // TODO(bengr): Move these to proxy_config_dictionary.h and reuse them here. | |
| 432 const char kProxyMode[] = "mode"; | |
| 433 const char kProxyPacURL[] = "pac_url"; | |
| 434 const char kAtStartup[] = "at startup"; | |
| 435 const char kByUser[] = "by user action"; | |
| 436 | |
| 437 DictionaryPrefUpdate update(prefs, prefs::kProxy); | |
| 438 DictionaryValue* dict = update.Get(); | |
| 439 if (enable_spdy_proxy) { | |
| 440 LOG(WARNING) << "SPDY proxy ON " << (at_startup ? kAtStartup : kByUser); | |
| 441 // Convert to a data URI and update the PAC settings. | |
| 442 std::string base64_pac; | |
| 443 base::Base64Encode(GetProxyPacScript(), &base64_pac); | |
| 444 | |
| 445 dict->SetString(kProxyPacURL, | |
| 446 "data:application/x-ns-proxy-autoconfig;base64," + | |
| 447 base64_pac); | |
| 448 dict->SetString(kProxyMode, | |
| 449 ProxyModeToString(ProxyPrefs::MODE_PAC_SCRIPT)); | |
| 450 | |
| 451 if (at_startup) { | |
| 452 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 453 SPDY_PROXY_AUTH_ON_AT_STARTUP, | |
| 454 NUM_SPDY_PROXY_AUTH_STATE); | |
| 455 } else if (!has_turned_on_) { | |
| 456 // SPDY proxy auth is turned on by user action for the first time in | |
| 457 // this session. | |
| 458 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 459 SPDY_PROXY_AUTH_ON_BY_USER, | |
| 460 NUM_SPDY_PROXY_AUTH_STATE); | |
| 461 has_turned_on_ = true; | |
| 462 } | |
| 463 } else { | |
| 464 LOG(WARNING) << "SPDY proxy OFF " << (at_startup ? kAtStartup : kByUser); | |
| 465 dict->SetString(kProxyMode, ProxyModeToString(ProxyPrefs::MODE_SYSTEM)); | |
| 466 dict->SetString(kProxyPacURL, ""); | |
| 467 | |
| 468 if (!at_startup && !has_turned_off_) { | |
| 469 UMA_HISTOGRAM_ENUMERATION("SpdyProxyAuth.State", | |
| 470 SPDY_PROXY_AUTH_OFF_BY_USER, | |
| 471 NUM_SPDY_PROXY_AUTH_STATE); | |
| 472 has_turned_off_ = true; | |
| 473 } | |
| 474 } | |
| 475 } | |
| 476 | |
| 477 ScopedJavaLocalRef<jlongArray> | |
| 478 DataReductionProxySettingsAndroid::GetDailyContentLengths( | |
| 479 JNIEnv* env, const char* pref_name) { | |
| 480 jlongArray result = env->NewLongArray(spdyproxy::kNumDaysInHistory); | |
| 481 PrefService* local_state = GetLocalStatePrefs(); | |
| 482 if (!local_state) | |
| 483 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 484 | |
| 485 const ListValue* list_value = local_state->GetList(pref_name); | |
| 486 if (list_value->GetSize() != spdyproxy::kNumDaysInHistory) | |
| 487 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 488 | |
| 489 jlong jval[spdyproxy::kNumDaysInHistory]; | |
| 490 for (size_t i = 0; i < spdyproxy::kNumDaysInHistory; ++i) { | |
| 491 jval[i] = GetInt64PrefValue(*list_value, i); | |
| 492 } | |
| 493 env->SetLongArrayRegion(result, 0, spdyproxy::kNumDaysInHistory, jval); | |
| 494 return ScopedJavaLocalRef<jlongArray>(env, result); | |
| 495 } | |
| 496 | |
| 497 void DataReductionProxySettingsAndroid::GetContentLengthsInternal( | |
| 498 unsigned int days, | |
| 499 int64* original_content_length, | |
| 500 int64* received_content_length, | |
| 501 int64* last_update_time) { | |
| 502 DCHECK_LE(days, spdyproxy::kNumDaysInHistory); | |
| 503 PrefService* local_state = GetLocalStatePrefs(); | |
| 504 if (!local_state) { | |
| 505 *original_content_length = 0L; | |
| 506 *received_content_length = 0L; | |
| 507 *last_update_time = 0L; | |
| 508 return; | |
| 509 } | |
| 510 | |
| 511 const ListValue* original_list = | |
| 512 local_state->GetList(prefs::kDailyHttpOriginalContentLength); | |
| 513 const ListValue* received_list = | |
| 514 local_state->GetList(prefs::kDailyHttpReceivedContentLength); | |
| 515 int64 last_update_internal = | |
| 516 local_state->GetInt64(prefs::kDailyHttpContentLengthLastUpdateDate); | |
|
mmenke
2013/09/17 20:25:25
Seems like we don't get anything from having the t
bengr
2013/09/18 22:15:26
Done.
| |
| 517 | |
| 518 if (original_list->GetSize() != spdyproxy::kNumDaysInHistory || | |
| 519 received_list->GetSize() != spdyproxy::kNumDaysInHistory) { | |
| 520 *original_content_length = 0L; | |
| 521 *received_content_length = 0L; | |
| 522 *last_update_time = 0L; | |
| 523 return; | |
| 524 } | |
| 525 | |
| 526 int64 orig = 0L; | |
| 527 int64 recv = 0L; | |
| 528 // We include days from the end of the list going backwards. | |
| 529 for (unsigned int i = 0; i < days; ++i) { | |
| 530 int read_index = spdyproxy::kNumDaysInHistory - 1 - i; | |
| 531 std::string result; | |
| 532 orig += GetInt64PrefValue(*original_list, read_index); | |
| 533 recv += GetInt64PrefValue(*received_list, read_index); | |
| 534 } | |
| 535 *original_content_length = orig; | |
| 536 *received_content_length = recv; | |
| 537 *last_update_time = last_update_internal; | |
| 538 } | |
| 539 | |
| 540 net::URLFetcher* DataReductionProxySettingsAndroid::GetURLFetcher() { | |
| 541 std::string url = GetProxyCheckURL(); | |
| 542 if (url.empty()) | |
| 543 return NULL; | |
| 544 fetcher_.reset(net::URLFetcher::Create(0, GURL(url), | |
|
mmenke
2013/09/17 20:25:25
Since you aren't using TestUrlFetcherFactory, you
bengr
2013/09/18 22:15:26
Done.
| |
| 545 net::URLFetcher::GET, this)); | |
| 546 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE); | |
| 547 Profile* profile = g_browser_process->profile_manager()-> | |
| 548 GetDefaultProfile(); | |
| 549 fetcher_->SetRequestContext(profile->GetRequestContext()); | |
| 550 // Configure to max_retries at most kMaxRetries times for 5xx errors. | |
| 551 static const int kMaxRetries = 5; | |
| 552 fetcher_->SetMaxRetriesOn5xx(kMaxRetries); | |
| 553 return fetcher_.get(); | |
| 554 } | |
| 555 | |
| 556 void | |
| 557 DataReductionProxySettingsAndroid::ProbeWhetherDataReductionProxyIsAvailable() { | |
| 558 net::URLFetcher* fetcher = GetURLFetcher(); | |
|
mmenke
2013/09/17 20:25:25
optional: I think it would be much cleaner to cha
bengr
2013/09/18 22:15:26
Done.
| |
| 559 if (fetcher) | |
|
mmenke
2013/09/17 20:25:25
Since this can't happen, I think it's best not to
bengr
2013/09/18 22:15:26
This could happen if the proxy check url is empty.
| |
| 560 fetcher->Start(); | |
| 561 } | |
| 562 | |
| 563 // TODO(bengr): Replace with our own ProxyResolver. | |
| 564 std::string DataReductionProxySettingsAndroid::GetProxyPacScript() { | |
| 565 std::string bypass_clause = "(" + JoinString(bypass_rules_, ") || (") + ")"; | |
| 566 | |
| 567 // We want to fall back to direct loading when the proxy is unavailable and | |
| 568 // only process HTTP traffic, so we concoct a PAC configuration | |
| 569 // accordingly. (With a statically configured proxy, proxy failures will | |
| 570 // simply result in a connection error presented to users.) | |
| 571 | |
| 572 std::string pac = "function FindProxyForURL(url, host) {" | |
| 573 " alert(\"Hello World\");" | |
|
mmenke
2013/09/17 20:25:25
I assume this was just for testing?
bengr
2013/09/18 22:15:26
Done.
| |
| 574 " if (" + bypass_clause + ") {" | |
| 575 " return \"DIRECT\";" | |
| 576 " } " | |
| 577 " if (url.substring(0, 5) == \"http:\") {" | |
|
mmenke
2013/09/17 20:25:25
I thought media bypassed flywheel? Or is that don
bengr
2013/09/18 22:15:26
Media is bypassed using a proxy-bypass signal from
| |
| 578 " return \"HTTPS " + GetDataReductionProxyOriginHostPort() + | |
| 579 "; DIRECT\";" | |
| 580 " }" | |
| 581 " return \"DIRECT\";" | |
| 582 "}"; | |
| 583 return pac; | |
| 584 } | |
| 585 | |
| 586 // Used by generated jni code. | |
| 587 static jint Init(JNIEnv* env, jobject obj) { | |
| 588 DataReductionProxySettingsAndroid* settings = | |
| 589 new DataReductionProxySettingsAndroid(env, obj); | |
| 590 return reinterpret_cast<jint>(settings); | |
| 591 } | |
| 592 | |
| 593 | |
| OLD | NEW |