| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" | 5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h" |
| 6 | 6 |
| 7 #include "base/android/build_info.h" | 7 #include "base/android/build_info.h" |
| 8 #include "base/android/jni_android.h" | 8 #include "base/android/jni_android.h" |
| 9 #include "base/android/jni_string.h" | 9 #include "base/android/jni_string.h" |
| 10 #include "base/base64.h" | 10 #include "base/base64.h" |
| 11 #include "base/bind.h" | 11 #include "base/bind.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/metrics/field_trial.h" | |
| 14 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 15 #include "base/prefs/pref_member.h" | 14 #include "base/prefs/pref_member.h" |
| 16 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 17 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_util.h" | 17 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 20 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 21 #include "chrome/browser/browser_process.h" | 20 #include "chrome/browser/browser_process.h" |
| 22 #include "chrome/browser/prefs/proxy_prefs.h" | 21 #include "chrome/browser/prefs/proxy_prefs.h" |
| 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 22 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 24 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 25 #include "chrome/browser/profiles/profile_manager.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
| 26 #include "chrome/common/chrome_switches.h" | 25 #include "chrome/common/chrome_switches.h" |
| 27 #include "chrome/common/pref_names.h" | 26 #include "chrome/common/pref_names.h" |
| 28 #include "jni/DataReductionProxySettings_jni.h" | 27 #include "jni/DataReductionProxySettings_jni.h" |
| 28 #include "net/base/auth.h" |
| 29 #include "net/base/host_port_pair.h" | 29 #include "net/base/host_port_pair.h" |
| 30 #include "net/base/load_flags.h" | 30 #include "net/base/load_flags.h" |
| 31 #include "net/base/net_errors.h" | 31 #include "net/base/net_errors.h" |
| 32 #include "net/url_request/url_fetcher.h" | 32 #include "net/url_request/url_fetcher.h" |
| 33 #include "net/url_request/url_fetcher_delegate.h" | 33 #include "net/url_request/url_fetcher_delegate.h" |
| 34 #include "net/url_request/url_request_status.h" | 34 #include "net/url_request/url_request_status.h" |
| 35 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 36 | 36 |
| 37 using base::android::CheckException; | 37 using base::android::CheckException; |
| 38 using base::android::ConvertJavaStringToUTF8; | 38 using base::android::ConvertJavaStringToUTF8; |
| 39 using base::android::ConvertUTF16ToJavaString; |
| 39 using base::android::ConvertUTF8ToJavaString; | 40 using base::android::ConvertUTF8ToJavaString; |
| 40 using base::android::ScopedJavaLocalRef; | 41 using base::android::ScopedJavaLocalRef; |
| 41 using base::FieldTrialList; | |
| 42 using base::StringPrintf; | 42 using base::StringPrintf; |
| 43 | 43 |
| 44 namespace { | 44 namespace { |
| 45 | 45 |
| 46 // The C++ definition of enum SpdyProxyAuthState defined in | 46 // The C++ definition of enum SpdyProxyAuthState defined in |
| 47 // tools/histograms/histograms.xml. | 47 // tools/histograms/histograms.xml. |
| 48 // New values should be added at the end before |NUM_SPDY_PROXY_AUTH_STATE|. | 48 // New values should be added at the end before |NUM_SPDY_PROXY_AUTH_STATE|. |
| 49 enum { | 49 enum { |
| 50 CHROME_STARTUP, | 50 CHROME_STARTUP, |
| 51 SPDY_PROXY_AUTH_ON_AT_STARTUP, | 51 SPDY_PROXY_AUTH_ON_AT_STARTUP, |
| 52 SPDY_PROXY_AUTH_ON_BY_USER, | 52 SPDY_PROXY_AUTH_ON_BY_USER, |
| 53 SPDY_PROXY_AUTH_OFF_BY_USER, | 53 SPDY_PROXY_AUTH_OFF_BY_USER, |
| 54 // Used by UMA histograms and should always be the last value. | 54 // Used by UMA histograms and should always be the last value. |
| 55 NUM_SPDY_PROXY_AUTH_STATE | 55 NUM_SPDY_PROXY_AUTH_STATE |
| 56 }; | 56 }; |
| 57 | 57 |
| 58 // Generates a PAC proxy string component, including trailing semicolon and |
| 59 // space, for |origin|. Eg: |
| 60 // "http://foo.com/" -> "HTTP foo.com:80; " |
| 61 // "https://bar.com:10443" -> "HTTPS bar.coom:10443; " |
| 62 // The returned strings are suitable for concatenating into a PAC string. |
| 63 // If |origin| is empty, returns an empty string. |
| 64 std::string ProtocolAndHostForPACString(const std::string& origin) { |
| 65 if (origin.empty()) { |
| 66 return std::string(); |
| 67 } |
| 68 GURL url = GURL(origin); |
| 69 std::string protocol = url.SchemeIsSecure() ? "HTTPS " : "HTTP "; |
| 70 return protocol + net::HostPortPair::FromURL(url).ToString() + "; "; |
| 71 } |
| 72 |
| 58 } // namespace | 73 } // namespace |
| 59 | 74 |
| 60 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( | 75 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid( |
| 61 JNIEnv* env, jobject obj): DataReductionProxySettings() { | 76 JNIEnv* env, jobject obj): DataReductionProxySettings() { |
| 62 } | 77 } |
| 63 | 78 |
| 64 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() {} | 79 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() {} |
| 65 | 80 |
| 66 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings( | 81 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings( |
| 67 JNIEnv* env, | 82 JNIEnv* env, |
| (...skipping 27 matching lines...) Expand all Loading... |
| 95 return DataReductionProxySettings::IsDataReductionProxyPromoAllowed(); | 110 return DataReductionProxySettings::IsDataReductionProxyPromoAllowed(); |
| 96 } | 111 } |
| 97 | 112 |
| 98 ScopedJavaLocalRef<jstring> | 113 ScopedJavaLocalRef<jstring> |
| 99 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin( | 114 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin( |
| 100 JNIEnv* env, jobject obj) { | 115 JNIEnv* env, jobject obj) { |
| 101 return ConvertUTF8ToJavaString( | 116 return ConvertUTF8ToJavaString( |
| 102 env, DataReductionProxySettings::GetDataReductionProxyOrigin()); | 117 env, DataReductionProxySettings::GetDataReductionProxyOrigin()); |
| 103 } | 118 } |
| 104 | 119 |
| 105 ScopedJavaLocalRef<jstring> | |
| 106 DataReductionProxySettingsAndroid::GetDataReductionProxyAuth( | |
| 107 JNIEnv* env, jobject obj) { | |
| 108 return ConvertUTF8ToJavaString( | |
| 109 env, DataReductionProxySettings::GetDataReductionProxyAuth()); | |
| 110 } | |
| 111 | |
| 112 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled( | 120 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled( |
| 113 JNIEnv* env, jobject obj) { | 121 JNIEnv* env, jobject obj) { |
| 114 return DataReductionProxySettings::IsDataReductionProxyEnabled(); | 122 return DataReductionProxySettings::IsDataReductionProxyEnabled(); |
| 115 } | 123 } |
| 116 | 124 |
| 117 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( | 125 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged( |
| 118 JNIEnv* env, jobject obj) { | 126 JNIEnv* env, jobject obj) { |
| 119 return DataReductionProxySettings::IsDataReductionProxyManaged(); | 127 return DataReductionProxySettings::IsDataReductionProxyManaged(); |
| 120 } | 128 } |
| 121 | 129 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 141 spdyproxy::kNumDaysInHistorySummary, | 149 spdyproxy::kNumDaysInHistorySummary, |
| 142 &original_content_length, | 150 &original_content_length, |
| 143 &received_content_length, | 151 &received_content_length, |
| 144 &last_update_internal); | 152 &last_update_internal); |
| 145 | 153 |
| 146 return Java_ContentLengths_create(env, | 154 return Java_ContentLengths_create(env, |
| 147 original_content_length, | 155 original_content_length, |
| 148 received_content_length); | 156 received_content_length); |
| 149 } | 157 } |
| 150 | 158 |
| 159 jboolean DataReductionProxySettingsAndroid::IsAcceptableAuthChallenge( |
| 160 JNIEnv* env, |
| 161 jobject obj, |
| 162 jstring host, |
| 163 jstring realm) { |
| 164 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); |
| 165 auth_info->realm = ConvertJavaStringToUTF8(env, realm); |
| 166 auth_info->challenger = |
| 167 net::HostPortPair::FromString(ConvertJavaStringToUTF8(env, host)); |
| 168 return DataReductionProxySettings::IsAcceptableAuthChallenge(auth_info.get()); |
| 169 } |
| 170 |
| 171 ScopedJavaLocalRef<jstring> |
| 172 DataReductionProxySettingsAndroid::GetTokenForAuthChallenge(JNIEnv* env, |
| 173 jobject obj, |
| 174 jstring host, |
| 175 jstring realm) { |
| 176 scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo); |
| 177 auth_info->realm = ConvertJavaStringToUTF8(env, realm); |
| 178 auth_info->challenger = |
| 179 net::HostPortPair::FromString(ConvertJavaStringToUTF8(env, host)); |
| 180 |
| 181 // If an empty string != null in Java, then here we should test for the |
| 182 // token being empty and return a java null. |
| 183 return ConvertUTF16ToJavaString(env, |
| 184 DataReductionProxySettings::GetTokenForAuthChallenge(auth_info.get())); |
| 185 } |
| 186 |
| 151 ScopedJavaLocalRef<jlongArray> | 187 ScopedJavaLocalRef<jlongArray> |
| 152 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( | 188 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths( |
| 153 JNIEnv* env, jobject obj) { | 189 JNIEnv* env, jobject obj) { |
| 154 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); | 190 return GetDailyContentLengths(env, prefs::kDailyHttpOriginalContentLength); |
| 155 } | 191 } |
| 156 | 192 |
| 157 ScopedJavaLocalRef<jlongArray> | 193 ScopedJavaLocalRef<jlongArray> |
| 158 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( | 194 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths( |
| 159 JNIEnv* env, jobject obj) { | 195 JNIEnv* env, jobject obj) { |
| 160 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); | 196 return GetDailyContentLengths(env, prefs::kDailyHttpReceivedContentLength); |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 | 289 |
| 254 // TODO(bengr): Replace with our own ProxyResolver. | 290 // TODO(bengr): Replace with our own ProxyResolver. |
| 255 std::string DataReductionProxySettingsAndroid::GetProxyPacScript() { | 291 std::string DataReductionProxySettingsAndroid::GetProxyPacScript() { |
| 256 // Compose the PAC-only bypass code; these will be URL patterns that | 292 // Compose the PAC-only bypass code; these will be URL patterns that |
| 257 // are matched by regular expression. Host bypasses are handled outside | 293 // are matched by regular expression. Host bypasses are handled outside |
| 258 // of the PAC file using the regular proxy bypass list configs. | 294 // of the PAC file using the regular proxy bypass list configs. |
| 259 std::string bypass_clause = | 295 std::string bypass_clause = |
| 260 "(" + JoinString(pac_bypass_rules_, ") || (") + ")"; | 296 "(" + JoinString(pac_bypass_rules_, ") || (") + ")"; |
| 261 | 297 |
| 262 // Generate a proxy PAC that falls back to direct loading when the proxy is | 298 // Generate a proxy PAC that falls back to direct loading when the proxy is |
| 263 // unavailable and only process HTTP traffic. (With a statically configured | 299 // unavailable and only process HTTP traffic. |
| 264 // proxy, proxy failures will simply result in a connection error presented to | |
| 265 // users.) | |
| 266 | 300 |
| 267 std::string proxy_host = | 301 std::string proxy_host = ProtocolAndHostForPACString( |
| 268 DataReductionProxySettings::GetDataReductionProxyOriginHostPort(); | 302 DataReductionProxySettings::GetDataReductionProxyOrigin()); |
| 303 std::string fallback_host = ProtocolAndHostForPACString( |
| 304 DataReductionProxySettings::GetDataReductionProxyFallback()); |
| 269 std::string pac = "function FindProxyForURL(url, host) {" | 305 std::string pac = "function FindProxyForURL(url, host) {" |
| 270 " if (" + bypass_clause + ") {" | 306 " if (" + bypass_clause + ") {" |
| 271 " return 'DIRECT';" | 307 " return 'DIRECT';" |
| 272 " } " | 308 " } " |
| 273 " if (url.substring(0, 5) == 'http:') {" | 309 " if (url.substring(0, 5) == 'http:') {" |
| 274 " return 'HTTPS " + proxy_host + | 310 " return '" + proxy_host + fallback_host + "DIRECT';" |
| 275 "; DIRECT';" | |
| 276 " }" | 311 " }" |
| 277 " return 'DIRECT';" | 312 " return 'DIRECT';" |
| 278 "}"; | 313 "}"; |
| 279 return pac; | 314 return pac; |
| 280 } | 315 } |
| 281 | 316 |
| 282 // Used by generated jni code. | 317 // Used by generated jni code. |
| 283 static jint Init(JNIEnv* env, jobject obj) { | 318 static jint Init(JNIEnv* env, jobject obj) { |
| 284 DataReductionProxySettingsAndroid* settings = | 319 DataReductionProxySettingsAndroid* settings = |
| 285 new DataReductionProxySettingsAndroid(env, obj); | 320 new DataReductionProxySettingsAndroid(env, obj); |
| 286 return reinterpret_cast<jint>(settings); | 321 return reinterpret_cast<jint>(settings); |
| 287 } | 322 } |
| OLD | NEW |