14e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
24e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
34e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// found in the LICENSE file.
44e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
54e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
64e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
74e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/jni_android.h"
84e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/jni_string.h"
9a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_configurator.h"
105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/profiles/profile.h"
134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/profiles/profile_manager.h"
144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/common/pref_names.h"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "components/data_reduction_proxy/browser/data_reduction_proxy_params.h"
16a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch#include "components/data_reduction_proxy/browser/data_reduction_proxy_settings.h"
175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.h"
184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "jni/DataReductionProxySettings_jni.h"
195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
205f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)using base::android::ConvertUTF8ToJavaString;
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)using base::android::ScopedJavaLocalRef;
2346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)using data_reduction_proxy::DataReductionProxyParams;
24a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochusing data_reduction_proxy::DataReductionProxySettings;
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
265f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid() {
27010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
29010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() {
30010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)}
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyAllowed(
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
345f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->params()->allowed();
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAllowed(
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->params()->promo_allowed();
405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)jboolean DataReductionProxySettingsAndroid::IsIncludedInAltFieldTrial(
435f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    JNIEnv* env, jobject obj) {
445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return DataReductionProxyParams::IsIncludedInAlternativeFieldTrial();
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ScopedJavaLocalRef<jstring>
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)DataReductionProxySettingsAndroid::GetDataReductionProxyOrigin(
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return ConvertUTF8ToJavaString(env, Settings()->params()->origin().spec());
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled(
544e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->IsDataReductionProxyEnabled();
564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)jboolean DataReductionProxySettingsAndroid::IsDataReductionProxyManaged(
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->IsDataReductionProxyManaged();
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled(
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env,
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    jobject obj,
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    jboolean enabled) {
675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Settings()->SetDataReductionProxyEnabled(enabled);
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)jlong DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime(
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
725f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->GetDataReductionLastUpdateTime();
734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)base::android::ScopedJavaLocalRef<jobject>
764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv* env,
774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jobject obj) {
784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 original_content_length;
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 received_content_length;
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  int64 last_update_internal;
815f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Settings()->GetContentLengths(
82a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      data_reduction_proxy::kNumDaysInHistorySummary,
838bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &original_content_length,
848bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &received_content_length,
858bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      &last_update_internal);
864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return Java_ContentLengths_create(env,
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                    original_content_length,
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                    received_content_length);
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ScopedJavaLocalRef<jlongArray>
934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths(
944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
95a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return GetDailyContentLengths(
96a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      env, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength);
974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ScopedJavaLocalRef<jlongArray>
1004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths(
1014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    JNIEnv* env, jobject obj) {
102a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  return GetDailyContentLengths(
103a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      env, data_reduction_proxy::prefs::kDailyHttpReceivedContentLength);
1044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
106116680a4aac90f2aa7413d9095a592090648e557Ben Murdochjboolean DataReductionProxySettingsAndroid::IsDataReductionProxyUnreachable(
107116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    JNIEnv* env, jobject obj) {
1085f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Settings()->IsDataReductionProxyUnreachable();
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
110116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
1124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)bool DataReductionProxySettingsAndroid::Register(JNIEnv* env) {
1134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  bool register_natives_impl_result = RegisterNativesImpl(env);
1144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return register_natives_impl_result;
1154e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)ScopedJavaLocalRef<jlongArray>
1184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)DataReductionProxySettingsAndroid::GetDailyContentLengths(
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    JNIEnv* env,  const char* pref_name) {
120a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  jlongArray result = env->NewLongArray(
121a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      data_reduction_proxy::kNumDaysInHistory);
1224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  DataReductionProxySettings::ContentLengthList lengths  =
1245f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Settings()->GetDailyContentLengths(pref_name);
1254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (!lengths.empty()) {
127a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch    DCHECK_EQ(lengths.size(), data_reduction_proxy::kNumDaysInHistory);
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    env->SetLongArrayRegion(result, 0, lengths.size(), &lengths[0]);
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return ScopedJavaLocalRef<jlongArray>(env, result);
1304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  }
1314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1328bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return ScopedJavaLocalRef<jlongArray>(env, result);
1334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
1344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)DataReductionProxySettings* DataReductionProxySettingsAndroid::Settings() {
1365f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DataReductionProxySettings* settings =
1375f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
1385f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          ProfileManager::GetActiveUserProfile());
1395f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  DCHECK(settings);
1405f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return settings;
1415f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
142a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch
1434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
1444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// Used by generated jni code.
145f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)static jlong Init(JNIEnv* env, jobject obj) {
1465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return reinterpret_cast<intptr_t>(new DataReductionProxySettingsAndroid());
1474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
148