Home | History | Annotate | Download | only in spdyproxy
      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/bind.h"
     11 #include "base/command_line.h"
     12 #include "base/memory/scoped_ptr.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/utf_string_conversions.h"
     19 #include "chrome/browser/browser_process.h"
     20 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
     21 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_factory_android.h"
     22 #include "chrome/browser/prefs/proxy_prefs.h"
     23 #include "chrome/browser/profiles/profile.h"
     24 #include "chrome/browser/profiles/profile_manager.h"
     25 #include "chrome/common/chrome_switches.h"
     26 #include "chrome/common/pref_names.h"
     27 #include "components/data_reduction_proxy/browser/data_reduction_proxy_configurator.h"
     28 #include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
     29 #include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
     30 #include "jni/DataReductionProxySettings_jni.h"
     31 #include "net/base/auth.h"
     32 #include "net/base/host_port_pair.h"
     33 #include "net/base/load_flags.h"
     34 #include "net/base/net_errors.h"
     35 #include "net/url_request/url_fetcher.h"
     36 #include "net/url_request/url_fetcher_delegate.h"
     37 #include "net/url_request/url_request_status.h"
     38 
     39 using base::android::CheckException;
     40 using base::android::ConvertJavaStringToUTF8;
     41 using base::android::ConvertUTF16ToJavaString;
     42 using base::android::ConvertUTF8ToJavaString;
     43 using base::android::ScopedJavaLocalRef;
     44 using data_reduction_proxy::DataReductionProxyParams;
     45 using data_reduction_proxy::DataReductionProxySettings;
     46 
     47 namespace {
     48 
     49 // The C++ definition of enum SpdyProxyAuthState defined in
     50 // tools/histograms/histograms.xml.
     51 // New values should be added at the end before |NUM_SPDY_PROXY_AUTH_STATE|.
     52 enum {
     53   CHROME_STARTUP,
     54   SPDY_PROXY_AUTH_ON_AT_STARTUP,
     55   SPDY_PROXY_AUTH_ON_BY_USER,
     56   SPDY_PROXY_AUTH_OFF_BY_USER,
     57   // Used by UMA histograms and should always be the last value.
     58   NUM_SPDY_PROXY_AUTH_STATE
     59 };
     60 
     61 }  // namespace
     62 
     63 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid(
     64     data_reduction_proxy::DataReductionProxyParams* params)
     65     : DataReductionProxySettings(params) {
     66 }
     67 
     68 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() {
     69 }
     70 
     71 void DataReductionProxySettingsAndroid::InitDataReductionProxySettings(
     72     Profile* profile) {
     73   DCHECK(profile);
     74   PrefService* prefs = profile->GetPrefs();
     75 
     76   scoped_ptr<data_reduction_proxy::DataReductionProxyConfigurator>
     77       configurator(new DataReductionProxyChromeConfigurator(prefs));
     78   SetProxyConfigurator(configurator.Pass());
     79   DataReductionProxySettings::InitDataReductionProxySettings(
     80       prefs,
     81       g_browser_process->local_state(),
     82       ProfileManager::GetActiveUserProfile()->GetRequestContext());
     83   DataReductionProxySettings::SetDataReductionProxyAlternativeEnabled(
     84       DataReductionProxyParams::IsIncludedInAlternativeFieldTrial());
     85 }
     86 
     87 void DataReductionProxySettingsAndroid::BypassHostPattern(
     88     JNIEnv* env, jobject obj, jstring pattern) {
     89   configurator()->AddHostPatternToBypass(
     90       ConvertJavaStringToUTF8(env, pattern));
     91 }
     92 void DataReductionProxySettingsAndroid::BypassURLPattern(
     93     JNIEnv* env, jobject obj, jstring pattern) {
     94   configurator()->AddURLPatternToBypass(ConvertJavaStringToUTF8(env, pattern));
     95 }
     96 
     97 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyAllowed(
     98     JNIEnv* env, jobject obj) {
     99   return params()->allowed();
    100 }
    101 
    102 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAllowed(
    103     JNIEnv* env, jobject obj) {
    104   return params()->promo_allowed();
    105 }
    106 
    107 ScopedJavaLocalRef<jstring>
    108 DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin(
    109     JNIEnv* env, jobject obj) {
    110   return ConvertUTF8ToJavaString(env, params()->origin().spec());
    111 }
    112 
    113 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled(
    114     JNIEnv* env, jobject obj) {
    115   return DataReductionProxySettings::IsDataReductionProxyEnabled();
    116 }
    117 
    118 jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged(
    119     JNIEnv* env, jobject obj) {
    120   return DataReductionProxySettings::IsDataReductionProxyManaged();
    121 }
    122 
    123 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled(
    124     JNIEnv* env,
    125     jobject obj,
    126     jboolean enabled) {
    127   DataReductionProxySettings::SetDataReductionProxyEnabled(enabled);
    128 }
    129 
    130 jlong DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime(
    131     JNIEnv* env, jobject obj) {
    132   return DataReductionProxySettings::GetDataReductionLastUpdateTime();
    133 }
    134 
    135 base::android::ScopedJavaLocalRef<jobject>
    136 DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv* env,
    137                                                      jobject obj) {
    138   int64 original_content_length;
    139   int64 received_content_length;
    140   int64 last_update_internal;
    141   DataReductionProxySettings::GetContentLengths(
    142       data_reduction_proxy::kNumDaysInHistorySummary,
    143       &original_content_length,
    144       &received_content_length,
    145       &last_update_internal);
    146 
    147   return Java_ContentLengths_create(env,
    148                                     original_content_length,
    149                                     received_content_length);
    150 }
    151 
    152 jboolean DataReductionProxySettingsAndroid::IsAcceptableAuthChallenge(
    153     JNIEnv* env,
    154     jobject obj,
    155     jstring host,
    156     jstring realm) {
    157   scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
    158   auth_info->realm = ConvertJavaStringToUTF8(env, realm);
    159   auth_info->challenger =
    160       net::HostPortPair::FromString(ConvertJavaStringToUTF8(env, host));
    161   return DataReductionProxySettings::IsAcceptableAuthChallenge(auth_info.get());
    162 }
    163 
    164 ScopedJavaLocalRef<jstring>
    165 DataReductionProxySettingsAndroid::GetTokenForAuthChallenge(JNIEnv* env,
    166                                                             jobject obj,
    167                                                             jstring host,
    168                                                             jstring realm) {
    169   scoped_refptr<net::AuthChallengeInfo> auth_info(new net::AuthChallengeInfo);
    170   auth_info->realm = ConvertJavaStringToUTF8(env, realm);
    171   auth_info->challenger =
    172       net::HostPortPair::FromString(ConvertJavaStringToUTF8(env, host));
    173 
    174   // If an empty string != null in Java, then here we should test for the
    175   // token being empty and return a java null.
    176   return ConvertUTF16ToJavaString(env,
    177       DataReductionProxySettings::GetTokenForAuthChallenge(auth_info.get()));
    178  }
    179 
    180 ScopedJavaLocalRef<jlongArray>
    181 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths(
    182     JNIEnv* env, jobject obj) {
    183   return GetDailyContentLengths(
    184       env, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
    185 }
    186 
    187 ScopedJavaLocalRef<jlongArray>
    188 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths(
    189     JNIEnv* env, jobject obj) {
    190   return GetDailyContentLengths(
    191       env, data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
    192 }
    193 
    194 // static
    195 bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) {
    196   bool register_natives_impl_result = RegisterNativesImpl(env);
    197   return register_natives_impl_result;
    198 }
    199 
    200 void DataReductionProxySettingsAndroid::AddDefaultProxyBypassRules() {
    201    DataReductionProxySettings::AddDefaultProxyBypassRules();
    202   // Chrome cannot authenticate with the data reduction proxy when fetching URLs
    203   // from the settings menu.
    204   configurator()->AddURLPatternToBypass(
    205       "http://www.google.com/policies/privacy*");
    206 }
    207 
    208 void DataReductionProxySettingsAndroid::SetProxyConfigs(
    209     bool enabled,
    210     bool alternative_enabled,
    211     bool restricted,
    212     bool at_startup) {
    213   // Sanity check: If there's no fallback proxy, we can't do a restricted mode.
    214   std::string fallback = params()->fallback_origin().spec();
    215   if (fallback.empty() && enabled && restricted)
    216       enabled = false;
    217 
    218   LogProxyState(enabled, restricted, at_startup);
    219 
    220   if (enabled) {
    221     if (alternative_enabled) {
    222       configurator()->Enable(restricted,
    223                        !params()->fallback_allowed(),
    224                        params()->alt_origin().spec(),
    225                        params()->alt_fallback_origin().spec(),
    226                        params()->ssl_origin().spec());
    227     } else {
    228         configurator()->Enable(restricted,
    229                          !params()->fallback_allowed(),
    230                          params()->origin().spec(),
    231                          params()->fallback_origin().spec(),
    232                          std::string());
    233     }
    234   } else {
    235     configurator()->Disable();
    236   }
    237 }
    238 
    239 ScopedJavaLocalRef<jlongArray>
    240 DataReductionProxySettingsAndroid::GetDailyContentLengths(
    241     JNIEnv* env,  const char* pref_name) {
    242   jlongArray result = env->NewLongArray(
    243       data_reduction_proxy::kNumDaysInHistory);
    244 
    245   DataReductionProxySettings::ContentLengthList lengths  =
    246       DataReductionProxySettings::GetDailyContentLengths(pref_name);
    247 
    248   if (!lengths.empty()) {
    249     DCHECK_EQ(lengths.size(), data_reduction_proxy::kNumDaysInHistory);
    250     env->SetLongArrayRegion(result, 0, lengths.size(), &lengths[0]);
    251     return ScopedJavaLocalRef<jlongArray>(env, result);
    252   }
    253 
    254   return ScopedJavaLocalRef<jlongArray>(env, result);
    255 }
    256 
    257 
    258 
    259 // Used by generated jni code.
    260 static jlong Init(JNIEnv* env, jobject obj) {
    261   DataReductionProxySettingsAndroid* settings =
    262       DataReductionProxySettingsFactoryAndroid::GetForBrowserContext(
    263           ProfileManager::GetActiveUserProfile());
    264   return reinterpret_cast<intptr_t>(settings);
    265 }
    266