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.
5 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_settings_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/values.h"
10 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings.h"
11 #include "chrome/browser/net/spdyproxy/data_reduction_proxy_chrome_settings_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_metrics.h"
16 #include "components/data_reduction_proxy/core/browser/data_reduction_proxy_settings.h"
17 #include "components/data_reduction_proxy/core/common/data_reduction_proxy_event_store.h"
18 #include "jni/DataReductionProxySettings_jni.h"
19 #include "net/proxy/proxy_server.h"
23 using base::android::ConvertUTF8ToJavaString
;
24 using base::android::ScopedJavaLocalRef
;
25 using data_reduction_proxy::DataReductionProxySettings
;
27 DataReductionProxySettingsAndroid::DataReductionProxySettingsAndroid() {
30 DataReductionProxySettingsAndroid::~DataReductionProxySettingsAndroid() {
33 jboolean
DataReductionProxySettingsAndroid::IsDataReductionProxyAllowed(
34 JNIEnv
* env
, jobject obj
) {
35 return Settings()->Allowed();
38 jboolean
DataReductionProxySettingsAndroid::IsDataReductionProxyPromoAllowed(
39 JNIEnv
* env
, jobject obj
) {
40 return Settings()->PromoAllowed();
43 jboolean
DataReductionProxySettingsAndroid::IsIncludedInAltFieldTrial(
44 JNIEnv
* env
, jobject obj
) {
48 jboolean
DataReductionProxySettingsAndroid::IsDataReductionProxyEnabled(
49 JNIEnv
* env
, jobject obj
) {
50 return Settings()->IsDataReductionProxyEnabled();
53 jboolean
DataReductionProxySettingsAndroid::CanUseDataReductionProxy(
54 JNIEnv
* env
, jobject obj
, jstring url
) {
55 return Settings()->CanUseDataReductionProxy(
56 GURL(base::android::ConvertJavaStringToUTF16(env
, url
)));
59 jboolean
DataReductionProxySettingsAndroid::WasLoFiModeActiveOnMainFrame(
60 JNIEnv
* env
, jobject obj
) {
61 return Settings()->WasLoFiModeActiveOnMainFrame();
64 jboolean
DataReductionProxySettingsAndroid::WasLoFiLoadImageRequestedBefore(
65 JNIEnv
* env
, jobject obj
) {
66 return Settings()->WasLoFiLoadImageRequestedBefore();
69 void DataReductionProxySettingsAndroid::SetLoFiLoadImageRequested(
70 JNIEnv
* env
, jobject obj
) {
71 Settings()->SetLoFiLoadImageRequested();
74 jboolean
DataReductionProxySettingsAndroid::IsDataReductionProxyManaged(
75 JNIEnv
* env
, jobject obj
) {
76 return Settings()->IsDataReductionProxyManaged();
79 void DataReductionProxySettingsAndroid::IncrementLoFiSnackbarShown(
80 JNIEnv
* env
, jobject obj
) {
81 Settings()->IncrementLoFiSnackbarShown();
84 void DataReductionProxySettingsAndroid::IncrementLoFiUserRequestsForImages(
85 JNIEnv
* env
, jobject obj
) {
86 Settings()->IncrementLoFiUserRequestsForImages();
89 void DataReductionProxySettingsAndroid::SetDataReductionProxyEnabled(
93 Settings()->SetDataReductionProxyEnabled(enabled
);
96 jlong
DataReductionProxySettingsAndroid::GetDataReductionLastUpdateTime(
97 JNIEnv
* env
, jobject obj
) {
98 return Settings()->GetDataReductionLastUpdateTime();
101 base::android::ScopedJavaLocalRef
<jobject
>
102 DataReductionProxySettingsAndroid::GetContentLengths(JNIEnv
* env
,
104 int64 original_content_length
;
105 int64 received_content_length
;
106 int64 last_update_internal
;
107 Settings()->GetContentLengths(
108 data_reduction_proxy::kNumDaysInHistorySummary
,
109 &original_content_length
,
110 &received_content_length
,
111 &last_update_internal
);
113 return Java_ContentLengths_create(env
,
114 original_content_length
,
115 received_content_length
);
118 ScopedJavaLocalRef
<jlongArray
>
119 DataReductionProxySettingsAndroid::GetDailyOriginalContentLengths(
120 JNIEnv
* env
, jobject obj
) {
121 return GetDailyContentLengths(
122 env
, data_reduction_proxy::prefs::kDailyHttpOriginalContentLength
);
125 ScopedJavaLocalRef
<jlongArray
>
126 DataReductionProxySettingsAndroid::GetDailyReceivedContentLengths(
127 JNIEnv
* env
, jobject obj
) {
128 return GetDailyContentLengths(
129 env
, data_reduction_proxy::prefs::kDailyHttpReceivedContentLength
);
132 jboolean
DataReductionProxySettingsAndroid::IsDataReductionProxyUnreachable(
133 JNIEnv
* env
, jobject obj
) {
134 return Settings()->IsDataReductionProxyUnreachable();
138 bool DataReductionProxySettingsAndroid::Register(JNIEnv
* env
) {
139 return RegisterNativesImpl(env
);
142 ScopedJavaLocalRef
<jlongArray
>
143 DataReductionProxySettingsAndroid::GetDailyContentLengths(
144 JNIEnv
* env
, const char* pref_name
) {
145 jlongArray result
= env
->NewLongArray(
146 data_reduction_proxy::kNumDaysInHistory
);
148 data_reduction_proxy::ContentLengthList lengths
=
149 Settings()->GetDailyContentLengths(pref_name
);
151 if (!lengths
.empty()) {
152 DCHECK_EQ(lengths
.size(), data_reduction_proxy::kNumDaysInHistory
);
153 env
->SetLongArrayRegion(result
, 0, lengths
.size(), &lengths
[0]);
154 return ScopedJavaLocalRef
<jlongArray
>(env
, result
);
157 return ScopedJavaLocalRef
<jlongArray
>(env
, result
);
160 ScopedJavaLocalRef
<jstring
> DataReductionProxySettingsAndroid::GetHttpProxyList(
163 data_reduction_proxy::DataReductionProxyEventStore
* event_store
=
164 Settings()->GetEventStore();
166 return ConvertUTF8ToJavaString(env
, std::string());
168 return ConvertUTF8ToJavaString(env
, event_store
->GetHttpProxyList());
171 ScopedJavaLocalRef
<jstring
>
172 DataReductionProxySettingsAndroid::GetHttpsProxyList(JNIEnv
* env
, jobject obj
) {
173 data_reduction_proxy::DataReductionProxyEventStore
* event_store
=
174 Settings()->GetEventStore();
176 return ConvertUTF8ToJavaString(env
, std::string());
178 return ConvertUTF8ToJavaString(env
, event_store
->GetHttpsProxyList());
181 DataReductionProxySettings
* DataReductionProxySettingsAndroid::Settings() {
182 DataReductionProxySettings
* settings
=
183 DataReductionProxyChromeSettingsFactory::GetForBrowserContext(
184 ProfileManager::GetActiveUserProfile());
189 ScopedJavaLocalRef
<jstring
>
190 DataReductionProxySettingsAndroid::GetLastBypassEvent(JNIEnv
* env
,
192 data_reduction_proxy::DataReductionProxyEventStore
* event_store
=
193 Settings()->GetEventStore();
195 return ConvertUTF8ToJavaString(env
, std::string());
197 return ConvertUTF8ToJavaString(env
, event_store
->SanitizedLastBypassEvent());
200 // Used by generated jni code.
201 static jlong
Init(JNIEnv
* env
, const JavaParamRef
<jobject
>& obj
) {
202 return reinterpret_cast<intptr_t>(new DataReductionProxySettingsAndroid());