1 // Copyright 2014 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/android/preferences/pref_service_bridge.h"
9 #include "base/android/build_info.h"
10 #include "base/android/jni_android.h"
11 #include "base/android/jni_string.h"
12 #include "base/android/jni_weak_ref.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/prefs/pref_service.h"
17 #include "base/strings/string_util.h"
18 #include "base/values.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/browsing_data/browsing_data_helper.h"
21 #include "chrome/browser/browsing_data/browsing_data_remover.h"
22 #include "chrome/browser/net/prediction_options.h"
23 #include "chrome/browser/prefs/incognito_mode_prefs.h"
24 #include "chrome/browser/profiles/profile_manager.h"
25 #include "chrome/browser/translate/chrome_translate_client.h"
26 #include "chrome/browser/ui/android/android_about_app_info.h"
27 #include "chrome/common/chrome_version_info.h"
28 #include "chrome/common/pref_names.h"
29 #include "chrome/grit/locale_settings.h"
30 #include "components/content_settings/core/browser/host_content_settings_map.h"
31 #include "components/content_settings/core/common/content_settings.h"
32 #include "components/content_settings/core/common/content_settings_pattern.h"
33 #include "components/content_settings/core/common/pref_names.h"
34 #include "components/password_manager/core/common/password_manager_pref_names.h"
35 #include "components/translate/core/browser/translate_prefs.h"
36 #include "components/translate/core/common/translate_pref_names.h"
37 #include "components/web_resource/web_resource_pref_names.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "jni/PrefServiceBridge_jni.h"
40 #include "ui/base/l10n/l10n_util.h"
42 using base::android::AttachCurrentThread
;
43 using base::android::CheckException
;
44 using base::android::ConvertJavaStringToUTF8
;
45 using base::android::ConvertUTF8ToJavaString
;
46 using base::android::ScopedJavaLocalRef
;
47 using base::android::ScopedJavaGlobalRef
;
48 using content::BrowserThread
;
52 enum NetworkPredictionOptions
{
53 NETWORK_PREDICTION_ALWAYS
,
54 NETWORK_PREDICTION_WIFI_ONLY
,
55 NETWORK_PREDICTION_NEVER
,
58 Profile
* GetOriginalProfile() {
59 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
62 bool GetBooleanForContentSetting(ContentSettingsType type
) {
63 HostContentSettingsMap
* content_settings
=
64 GetOriginalProfile()->GetHostContentSettingsMap();
65 switch (content_settings
->GetDefaultContentSetting(type
, NULL
)) {
66 case CONTENT_SETTING_BLOCK
:
68 case CONTENT_SETTING_ALLOW
:
70 case CONTENT_SETTING_ASK
:
76 std::string
GetStringForContentSettingsType(
77 ContentSetting content_setting
) {
78 switch (content_setting
) {
79 case CONTENT_SETTING_BLOCK
:
81 case CONTENT_SETTING_ALLOW
:
83 case CONTENT_SETTING_ASK
:
85 case CONTENT_SETTING_SESSION_ONLY
:
87 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT
:
89 case CONTENT_SETTING_NUM_SETTINGS
:
90 return "num_settings";
91 case CONTENT_SETTING_DEFAULT
:
97 bool IsContentSettingManaged(ContentSettingsType content_settings_type
) {
99 HostContentSettingsMap
* content_settings
=
100 GetOriginalProfile()->GetHostContentSettingsMap();
101 content_settings
->GetDefaultContentSetting(content_settings_type
, &source
);
102 HostContentSettingsMap::ProviderType provider
=
103 content_settings
->GetProviderTypeFromSource(source
);
104 return provider
== HostContentSettingsMap::POLICY_PROVIDER
;
107 bool IsContentSettingManagedByCustodian(
108 ContentSettingsType content_settings_type
) {
110 HostContentSettingsMap
* content_settings
=
111 GetOriginalProfile()->GetHostContentSettingsMap();
112 content_settings
->GetDefaultContentSetting(content_settings_type
, &source
);
113 HostContentSettingsMap::ProviderType provider
=
114 content_settings
->GetProviderTypeFromSource(source
);
115 return provider
== HostContentSettingsMap::SUPERVISED_PROVIDER
;
118 bool IsContentSettingUserModifiable(ContentSettingsType content_settings_type
) {
120 HostContentSettingsMap
* content_settings
=
121 GetOriginalProfile()->GetHostContentSettingsMap();
122 content_settings
->GetDefaultContentSetting(content_settings_type
, &source
);
123 HostContentSettingsMap::ProviderType provider
=
124 content_settings
->GetProviderTypeFromSource(source
);
125 return provider
>= HostContentSettingsMap::PREF_PROVIDER
;
128 PrefService
* GetPrefService() {
129 return GetOriginalProfile()->GetPrefs();
134 // ----------------------------------------------------------------------------
135 // Native JNI methods
136 // ----------------------------------------------------------------------------
138 static jboolean
IsContentSettingManaged(JNIEnv
* env
, jobject obj
,
139 int content_settings_type
) {
140 return IsContentSettingManaged(
141 static_cast<ContentSettingsType
>(content_settings_type
));
144 static jboolean
IsContentSettingEnabled(JNIEnv
* env
, jobject obj
,
145 int content_settings_type
) {
146 // Before we migrate functions over to this central function, we must verify
147 // that the functionality provided below is correct.
148 DCHECK(content_settings_type
== CONTENT_SETTINGS_TYPE_JAVASCRIPT
||
149 content_settings_type
== CONTENT_SETTINGS_TYPE_IMAGES
||
150 content_settings_type
== CONTENT_SETTINGS_TYPE_POPUPS
);
151 ContentSettingsType type
=
152 static_cast<ContentSettingsType
>(content_settings_type
);
153 if (type
== CONTENT_SETTINGS_TYPE_JAVASCRIPT
||
154 type
== CONTENT_SETTINGS_TYPE_POPUPS
)
155 return GetBooleanForContentSetting(type
);
157 HostContentSettingsMap
* content_settings
=
158 GetOriginalProfile()->GetHostContentSettingsMap();
159 return content_settings
->GetDefaultContentSetting(
160 type
, nullptr) == CONTENT_SETTING_ALLOW
;
163 static void SetContentSettingEnabled(JNIEnv
* env
, jobject obj
,
164 int content_settings_type
, jboolean allow
) {
165 // Before we migrate functions over to this central function, we must verify
166 // that the new category supports ALLOW/BLOCK pairs and, if not, handle them.
167 DCHECK(content_settings_type
== CONTENT_SETTINGS_TYPE_JAVASCRIPT
||
168 content_settings_type
== CONTENT_SETTINGS_TYPE_IMAGES
||
169 content_settings_type
== CONTENT_SETTINGS_TYPE_POPUPS
);
170 HostContentSettingsMap
* host_content_settings_map
=
171 GetOriginalProfile()->GetHostContentSettingsMap();
172 host_content_settings_map
->SetDefaultContentSetting(
173 static_cast<ContentSettingsType
>(content_settings_type
),
174 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
177 static void SetContentSettingForPattern(JNIEnv
* env
, jobject obj
,
178 int content_settings_type
, jstring pattern
, int setting
) {
179 HostContentSettingsMap
* host_content_settings_map
=
180 GetOriginalProfile()->GetHostContentSettingsMap();
181 host_content_settings_map
->SetContentSetting(
182 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
183 ContentSettingsPattern::Wildcard(),
184 static_cast<ContentSettingsType
>(content_settings_type
),
186 static_cast<ContentSetting
>(setting
));
189 static void GetContentSettingsExceptions(JNIEnv
* env
, jobject obj
,
190 int content_settings_type
, jobject list
) {
191 HostContentSettingsMap
* host_content_settings_map
=
192 GetOriginalProfile()->GetHostContentSettingsMap();
193 ContentSettingsForOneType entries
;
194 host_content_settings_map
->GetSettingsForOneType(
195 static_cast<ContentSettingsType
>(content_settings_type
), "", &entries
);
196 for (size_t i
= 0; i
< entries
.size(); ++i
) {
197 Java_PrefServiceBridge_addContentSettingExceptionToList(
199 content_settings_type
,
200 ConvertUTF8ToJavaString(
201 env
, entries
[i
].primary_pattern
.ToString()).obj(),
202 ConvertUTF8ToJavaString(
203 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
204 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
208 static jboolean
GetAcceptCookiesEnabled(JNIEnv
* env
, jobject obj
) {
209 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_COOKIES
);
212 static jboolean
GetAcceptCookiesManaged(JNIEnv
* env
, jobject obj
) {
213 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_COOKIES
);
216 static jboolean
GetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
) {
217 return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies
);
220 static jboolean
GetBlockThirdPartyCookiesManaged(JNIEnv
* env
, jobject obj
) {
221 return GetPrefService()->IsManagedPreference(prefs::kBlockThirdPartyCookies
);
224 static jboolean
GetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
) {
225 return GetPrefService()->GetBoolean(
226 password_manager::prefs::kPasswordManagerSavingEnabled
);
229 static jboolean
GetPasswordManagerAutoSigninEnabled(JNIEnv
* env
, jobject obj
) {
230 return GetPrefService()->GetBoolean(
231 password_manager::prefs::kPasswordManagerAutoSignin
);
234 static jboolean
GetRememberPasswordsManaged(JNIEnv
* env
, jobject obj
) {
235 return GetPrefService()->IsManagedPreference(
236 password_manager::prefs::kPasswordManagerSavingEnabled
);
239 static jboolean
GetPasswordManagerAutoSigninManaged(JNIEnv
* env
, jobject obj
) {
240 return GetPrefService()->IsManagedPreference(
241 password_manager::prefs::kPasswordManagerAutoSignin
);
244 static jboolean
GetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
) {
245 return GetPrefService()->GetBoolean(prefs::kEnableDoNotTrack
);
248 static jint
GetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
) {
249 return GetPrefService()->GetInteger(prefs::kNetworkPredictionOptions
);
252 static jboolean
GetNetworkPredictionManaged(JNIEnv
* env
, jobject obj
) {
253 return GetPrefService()->IsManagedPreference(
254 prefs::kNetworkPredictionOptions
);
257 static jboolean
GetPasswordEchoEnabled(JNIEnv
* env
, jobject obj
) {
258 return GetPrefService()->GetBoolean(prefs::kWebKitPasswordEchoEnabled
);
261 static jboolean
GetPrintingEnabled(JNIEnv
* env
, jobject obj
) {
262 return GetPrefService()->GetBoolean(prefs::kPrintingEnabled
);
265 static jboolean
GetPrintingManaged(JNIEnv
* env
, jobject obj
) {
266 return GetPrefService()->IsManagedPreference(prefs::kPrintingEnabled
);
269 static jboolean
GetTranslateEnabled(JNIEnv
* env
, jobject obj
) {
270 return GetPrefService()->GetBoolean(prefs::kEnableTranslate
);
273 static jboolean
GetTranslateManaged(JNIEnv
* env
, jobject obj
) {
274 return GetPrefService()->IsManagedPreference(prefs::kEnableTranslate
);
277 static jboolean
GetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
) {
278 return GetPrefService()->GetBoolean(prefs::kSearchSuggestEnabled
);
281 static jboolean
GetSearchSuggestManaged(JNIEnv
* env
, jobject obj
) {
282 return GetPrefService()->IsManagedPreference(prefs::kSearchSuggestEnabled
);
285 static jboolean
GetProtectedMediaIdentifierEnabled(JNIEnv
* env
, jobject obj
) {
286 return GetBooleanForContentSetting(
287 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
);
290 static jboolean
GetPushNotificationsEnabled(JNIEnv
* env
, jobject obj
) {
291 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
294 static jboolean
GetAllowLocationEnabled(JNIEnv
* env
, jobject obj
) {
295 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
298 static jboolean
GetLocationAllowedByPolicy(JNIEnv
* env
, jobject obj
) {
299 if (!IsContentSettingManaged(CONTENT_SETTINGS_TYPE_GEOLOCATION
))
301 HostContentSettingsMap
* content_settings
=
302 GetOriginalProfile()->GetHostContentSettingsMap();
303 return content_settings
->GetDefaultContentSetting(
304 CONTENT_SETTINGS_TYPE_GEOLOCATION
, nullptr) ==
305 CONTENT_SETTING_ALLOW
;
308 static jboolean
GetAllowLocationUserModifiable(JNIEnv
* env
, jobject obj
) {
309 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
312 static jboolean
GetAllowLocationManagedByCustodian(JNIEnv
* env
, jobject obj
) {
313 return IsContentSettingManagedByCustodian(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
316 static jboolean
GetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
) {
317 return GetPrefService()->GetBoolean(prefs::kAlternateErrorPagesEnabled
);
320 static jboolean
GetResolveNavigationErrorManaged(JNIEnv
* env
, jobject obj
) {
321 return GetPrefService()->IsManagedPreference(
322 prefs::kAlternateErrorPagesEnabled
);
325 static jboolean
GetCrashReportManaged(JNIEnv
* env
, jobject obj
) {
326 return GetPrefService()->IsManagedPreference(
327 prefs::kCrashReportingEnabled
);
330 static jboolean
GetForceGoogleSafeSearch(JNIEnv
* env
, jobject obj
) {
331 return GetPrefService()->GetBoolean(prefs::kForceGoogleSafeSearch
);
334 static jint
GetDefaultSupervisedUserFilteringBehavior(JNIEnv
* env
,
336 return GetPrefService()->GetInteger(
337 prefs::kDefaultSupervisedUserFilteringBehavior
);
340 static jboolean
GetIncognitoModeEnabled(JNIEnv
* env
, jobject obj
) {
341 PrefService
* prefs
= GetPrefService();
342 IncognitoModePrefs::Availability incognito_pref
=
343 IncognitoModePrefs::GetAvailability(prefs
);
344 DCHECK(incognito_pref
== IncognitoModePrefs::ENABLED
||
345 incognito_pref
== IncognitoModePrefs::DISABLED
) <<
346 "Unsupported incognito mode preference: " << incognito_pref
;
347 return incognito_pref
!= IncognitoModePrefs::DISABLED
;
350 static jboolean
GetIncognitoModeManaged(JNIEnv
* env
, jobject obj
) {
351 return GetPrefService()->IsManagedPreference(
352 prefs::kIncognitoModeAvailability
);
355 static jboolean
GetFullscreenManaged(JNIEnv
* env
, jobject obj
) {
356 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_FULLSCREEN
);
359 static jboolean
GetFullscreenAllowed(JNIEnv
* env
, jobject obj
) {
360 HostContentSettingsMap
* content_settings
=
361 GetOriginalProfile()->GetHostContentSettingsMap();
362 return content_settings
->GetDefaultContentSetting(
363 CONTENT_SETTINGS_TYPE_FULLSCREEN
, NULL
) == CONTENT_SETTING_ALLOW
;
366 static jboolean
GetMetricsReportingEnabled(JNIEnv
* env
, jobject obj
) {
367 PrefService
* local_state
= g_browser_process
->local_state();
368 return local_state
->GetBoolean(prefs::kMetricsReportingEnabled
);
371 static void SetMetricsReportingEnabled(JNIEnv
* env
,
374 PrefService
* local_state
= g_browser_process
->local_state();
375 local_state
->SetBoolean(prefs::kMetricsReportingEnabled
, enabled
);
378 static jboolean
HasSetMetricsReporting(JNIEnv
* env
, jobject obj
) {
379 PrefService
* local_state
= g_browser_process
->local_state();
380 return local_state
->HasPrefPath(prefs::kMetricsReportingEnabled
);
385 // Redirects a BrowsingDataRemover completion callback back into Java.
386 class ClearBrowsingDataObserver
: public BrowsingDataRemover::Observer
{
388 // |obj| is expected to be the object passed into ClearBrowsingData(); e.g. a
390 ClearBrowsingDataObserver(JNIEnv
* env
, jobject obj
)
391 : weak_chrome_native_preferences_(env
, obj
) {
394 void OnBrowsingDataRemoverDone() override
{
395 // Just as a BrowsingDataRemover deletes itself when done, we delete ourself
396 // when done. No need to remove ourself as an observer given the lifetime
397 // of BrowsingDataRemover.
398 scoped_ptr
<ClearBrowsingDataObserver
> auto_delete(this);
400 JNIEnv
* env
= AttachCurrentThread();
401 if (weak_chrome_native_preferences_
.get(env
).is_null())
404 Java_PrefServiceBridge_browsingDataCleared(
405 env
, weak_chrome_native_preferences_
.get(env
).obj());
409 JavaObjectWeakGlobalRef weak_chrome_native_preferences_
;
413 static void ClearBrowsingData(JNIEnv
* env
, jobject obj
, jboolean history
,
414 jboolean cache
, jboolean cookies_and_site_data
, jboolean passwords
,
415 jboolean form_data
) {
416 // BrowsingDataRemover deletes itself.
417 BrowsingDataRemover
* browsing_data_remover
=
418 BrowsingDataRemover::CreateForPeriod(
419 GetOriginalProfile(),
420 static_cast<BrowsingDataRemover::TimePeriod
>(
421 BrowsingDataRemover::EVERYTHING
));
422 browsing_data_remover
->AddObserver(new ClearBrowsingDataObserver(env
, obj
));
426 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
428 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
429 if (cookies_and_site_data
) {
430 remove_mask
|= BrowsingDataRemover::REMOVE_COOKIES
;
431 remove_mask
|= BrowsingDataRemover::REMOVE_SITE_DATA
;
434 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
436 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
437 browsing_data_remover
->Remove(remove_mask
,
438 BrowsingDataHelper::UNPROTECTED_WEB
);
441 static jboolean
CanDeleteBrowsingHistory(JNIEnv
* env
, jobject obj
) {
442 return GetPrefService()->GetBoolean(prefs::kAllowDeletingBrowserHistory
);
445 static void SetAllowCookiesEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
446 HostContentSettingsMap
* host_content_settings_map
=
447 GetOriginalProfile()->GetHostContentSettingsMap();
448 host_content_settings_map
->SetDefaultContentSetting(
449 CONTENT_SETTINGS_TYPE_COOKIES
,
450 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
453 static void SetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
,
455 GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies
, enabled
);
458 static void SetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
,
460 GetPrefService()->SetBoolean(
461 password_manager::prefs::kPasswordManagerSavingEnabled
, allow
);
464 static void SetPasswordManagerAutoSigninEnabled(JNIEnv
* env
,
467 GetPrefService()->SetBoolean(
468 password_manager::prefs::kPasswordManagerAutoSignin
, enabled
);
471 static void SetProtectedMediaIdentifierEnabled(JNIEnv
* env
,
473 jboolean is_enabled
) {
474 HostContentSettingsMap
* host_content_settings_map
=
475 GetOriginalProfile()->GetHostContentSettingsMap();
476 host_content_settings_map
->SetDefaultContentSetting(
477 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
,
478 is_enabled
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
481 static void SetAllowLocationEnabled(JNIEnv
* env
,
483 jboolean is_enabled
) {
484 HostContentSettingsMap
* host_content_settings_map
=
485 GetOriginalProfile()->GetHostContentSettingsMap();
486 host_content_settings_map
->SetDefaultContentSetting(
487 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
488 is_enabled
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
491 static void SetCameraEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
492 HostContentSettingsMap
* host_content_settings_map
=
493 GetOriginalProfile()->GetHostContentSettingsMap();
494 host_content_settings_map
->SetDefaultContentSetting(
495 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
496 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
499 static void SetMicEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
500 HostContentSettingsMap
* host_content_settings_map
=
501 GetOriginalProfile()->GetHostContentSettingsMap();
502 host_content_settings_map
->SetDefaultContentSetting(
503 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
504 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
507 static void SetFullscreenAllowed(JNIEnv
* env
, jobject obj
, jboolean allow
) {
508 HostContentSettingsMap
* host_content_settings_map
=
509 GetOriginalProfile()->GetHostContentSettingsMap();
510 host_content_settings_map
->SetDefaultContentSetting(
511 CONTENT_SETTINGS_TYPE_FULLSCREEN
,
512 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_ASK
);
515 static void SetPushNotificationsEnabled(JNIEnv
* env
,
518 HostContentSettingsMap
* host_content_settings_map
=
519 GetOriginalProfile()->GetHostContentSettingsMap();
520 host_content_settings_map
->SetDefaultContentSetting(
521 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
522 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
525 static void SetCrashReporting(JNIEnv
* env
, jobject obj
, jboolean reporting
) {
526 PrefService
* local_state
= g_browser_process
->local_state();
527 local_state
->SetBoolean(prefs::kCrashReportingEnabled
, reporting
);
530 static jboolean
CanPredictNetworkActions(JNIEnv
* env
, jobject obj
) {
531 return chrome_browser_net::CanPrefetchAndPrerenderUI(GetPrefService());
534 static void SetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
535 GetPrefService()->SetBoolean(prefs::kEnableDoNotTrack
, allow
);
538 static jstring
GetSyncLastAccountName(JNIEnv
* env
, jobject obj
) {
539 return ConvertUTF8ToJavaString(
540 env
, GetPrefService()->GetString(prefs::kGoogleServicesLastUsername
))
544 static void SetTranslateEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
) {
545 GetPrefService()->SetBoolean(prefs::kEnableTranslate
, enabled
);
548 static void ResetTranslateDefaults(JNIEnv
* env
, jobject obj
) {
549 scoped_ptr
<translate::TranslatePrefs
> translate_prefs
=
550 ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
551 translate_prefs
->ResetToDefaults();
554 static void MigrateJavascriptPreference(JNIEnv
* env
, jobject obj
) {
555 const PrefService::Preference
* javascript_pref
=
556 GetPrefService()->FindPreference(prefs::kWebKitJavascriptEnabled
);
557 DCHECK(javascript_pref
);
559 if (!javascript_pref
->HasUserSetting())
562 bool javascript_enabled
= false;
563 bool retval
= javascript_pref
->GetValue()->GetAsBoolean(&javascript_enabled
);
565 SetContentSettingEnabled(env
, obj
,
566 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, javascript_enabled
);
567 GetPrefService()->ClearPref(prefs::kWebKitJavascriptEnabled
);
570 static void MigrateLocationPreference(JNIEnv
* env
, jobject obj
) {
571 const PrefService::Preference
* pref
=
572 GetPrefService()->FindPreference(prefs::kGeolocationEnabled
);
573 if (!pref
|| !pref
->HasUserSetting())
575 bool location_enabled
= false;
576 bool retval
= pref
->GetValue()->GetAsBoolean(&location_enabled
);
578 // Do a restrictive migration. GetAllowLocationEnabled could be
579 // non-usermodifiable and we don't want to migrate that.
580 if (!location_enabled
)
581 SetAllowLocationEnabled(env
, obj
, false);
582 GetPrefService()->ClearPref(prefs::kGeolocationEnabled
);
585 static void MigrateProtectedMediaPreference(JNIEnv
* env
, jobject obj
) {
586 const PrefService::Preference
* pref
=
587 GetPrefService()->FindPreference(prefs::kProtectedMediaIdentifierEnabled
);
588 if (!pref
|| !pref
->HasUserSetting())
590 bool pmi_enabled
= false;
591 bool retval
= pref
->GetValue()->GetAsBoolean(&pmi_enabled
);
593 // Do a restrictive migration if values disagree.
595 SetProtectedMediaIdentifierEnabled(env
, obj
, false);
596 GetPrefService()->ClearPref(prefs::kProtectedMediaIdentifierEnabled
);
599 static void SetPasswordEchoEnabled(JNIEnv
* env
,
601 jboolean passwordEchoEnabled
) {
602 GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled
,
603 passwordEchoEnabled
);
606 static jboolean
GetCameraEnabled(JNIEnv
* env
, jobject obj
) {
607 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
610 static jboolean
GetCameraUserModifiable(JNIEnv
* env
, jobject obj
) {
611 return IsContentSettingUserModifiable(
612 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
615 static jboolean
GetCameraManagedByCustodian(JNIEnv
* env
, jobject obj
) {
616 return IsContentSettingManagedByCustodian(
617 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
620 static jboolean
GetMicEnabled(JNIEnv
* env
, jobject obj
) {
621 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
624 static jboolean
GetMicUserModifiable(JNIEnv
* env
, jobject obj
) {
625 return IsContentSettingUserModifiable(
626 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
629 static jboolean
GetMicManagedByCustodian(JNIEnv
* env
, jobject obj
) {
630 return IsContentSettingManagedByCustodian(
631 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
634 static void SetJavaScriptAllowed(JNIEnv
* env
, jobject obj
, jstring pattern
,
636 HostContentSettingsMap
* host_content_settings_map
=
637 GetOriginalProfile()->GetHostContentSettingsMap();
638 host_content_settings_map
->SetContentSetting(
639 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
640 ContentSettingsPattern::Wildcard(),
641 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
643 static_cast<ContentSetting
>(setting
));
646 static void SetPopupException(JNIEnv
* env
, jobject obj
, jstring pattern
,
648 HostContentSettingsMap
* host_content_settings_map
=
649 GetOriginalProfile()->GetHostContentSettingsMap();
650 host_content_settings_map
->SetContentSetting(
651 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
652 ContentSettingsPattern::Wildcard(),
653 CONTENT_SETTINGS_TYPE_POPUPS
,
655 static_cast<ContentSetting
>(setting
));
658 static void SetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
,
660 GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled
, enabled
);
663 static jstring
GetContextualSearchPreference(JNIEnv
* env
, jobject obj
) {
664 return ConvertUTF8ToJavaString(
665 env
, GetPrefService()->GetString(prefs::kContextualSearchEnabled
)).
669 static jboolean
GetContextualSearchPreferenceIsManaged(JNIEnv
* env
,
671 return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled
);
674 static void SetContextualSearchPreference(JNIEnv
* env
, jobject obj
,
676 GetPrefService()->SetString(prefs::kContextualSearchEnabled
,
677 ConvertJavaStringToUTF8(env
, pref
));
680 static void SetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
, int option
) {
681 GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions
, option
);
684 static jboolean
NetworkPredictionEnabledHasUserSetting(JNIEnv
* env
,
686 return GetPrefService()->GetUserPrefValue(
687 prefs::kNetworkPredictionEnabled
) != NULL
;
690 static jboolean
NetworkPredictionOptionsHasUserSetting(JNIEnv
* env
,
692 return GetPrefService()->GetUserPrefValue(
693 prefs::kNetworkPredictionOptions
) != NULL
;
696 static jboolean
GetNetworkPredictionEnabledUserPrefValue(JNIEnv
* env
,
698 const base::Value
* network_prediction_enabled
=
699 GetPrefService()->GetUserPrefValue(prefs::kNetworkPredictionEnabled
);
700 DCHECK(network_prediction_enabled
);
702 DCHECK(network_prediction_enabled
->GetAsBoolean(&value
));
706 static void SetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
,
708 GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled
, enabled
);
711 static jboolean
GetFirstRunEulaAccepted(JNIEnv
* env
, jobject obj
) {
712 return g_browser_process
->local_state()->GetBoolean(prefs::kEulaAccepted
);
715 static void SetEulaAccepted(JNIEnv
* env
, jobject obj
) {
716 g_browser_process
->local_state()->SetBoolean(prefs::kEulaAccepted
, true);
719 static void ResetAcceptLanguages(JNIEnv
* env
,
721 jstring default_locale
) {
722 std::string
accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES
));
723 std::string
locale_string(ConvertJavaStringToUTF8(env
, default_locale
));
725 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string
,
727 GetPrefService()->SetString(prefs::kAcceptLanguages
, accept_languages
);
730 // Sends all information about the different versions to Java.
731 // From browser_about_handler.cc
732 static jobject
GetAboutVersionStrings(JNIEnv
* env
, jobject obj
) {
733 chrome::VersionInfo version_info
;
734 std::string os_version
= version_info
.OSType();
735 os_version
+= " " + AndroidAboutAppInfo::GetOsInfo();
737 base::android::BuildInfo
* android_build_info
=
738 base::android::BuildInfo::GetInstance();
739 std::string
application(android_build_info
->package_label());
740 application
.append(" ");
741 application
.append(version_info
.Version());
743 // OK to release, returning to Java.
744 return Java_PrefServiceBridge_createAboutVersionStrings(
746 ConvertUTF8ToJavaString(env
, application
).obj(),
747 ConvertUTF8ToJavaString(env
, os_version
).obj()).Release();
750 static jstring
GetSupervisedUserCustodianName(JNIEnv
* env
, jobject obj
) {
751 return ConvertUTF8ToJavaString(
752 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName
))
756 static jstring
GetSupervisedUserCustodianEmail(JNIEnv
* env
, jobject obj
) {
757 return ConvertUTF8ToJavaString(
758 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail
))
762 static jstring
GetSupervisedUserCustodianProfileImageURL(JNIEnv
* env
,
764 return ConvertUTF8ToJavaString(
766 GetPrefService()->GetString(
767 prefs::kSupervisedUserCustodianProfileImageURL
)).Release();
770 static jstring
GetSupervisedUserSecondCustodianName(JNIEnv
* env
, jobject obj
) {
771 return ConvertUTF8ToJavaString(
773 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName
))
777 static jstring
GetSupervisedUserSecondCustodianEmail(JNIEnv
* env
, jobject obj
) {
778 return ConvertUTF8ToJavaString(
780 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail
))
784 static jstring
GetSupervisedUserSecondCustodianProfileImageURL(JNIEnv
* env
,
786 return ConvertUTF8ToJavaString(
788 GetPrefService()->GetString(
789 prefs::kSupervisedUserSecondCustodianProfileImageURL
)).Release();
793 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv
* env
) {
794 return RegisterNativesImpl(env
);
798 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
799 const std::string
& locale
,
800 std::string
* accept_languages
) {
801 if (locale
.size() != 5u || locale
[2] != '_') // not well-formed
804 std::string
language(locale
.substr(0, 2));
805 std::string
region(locale
.substr(3, 2));
807 // Java mostly follows ISO-639-1 and ICU, except for the following three.
808 // See documentation on java.util.Locale constructor for more.
809 if (language
== "iw") {
811 } else if (language
== "ji") {
813 } else if (language
== "in") {
817 std::string
language_region(language
+ "-" + region
);
819 if (accept_languages
->find(language_region
) == std::string::npos
) {
820 std::vector
<std::string
> parts
;
821 parts
.push_back(language_region
);
822 // If language is not in the accept languages list, also add language code.
823 if (accept_languages
->find(language
+ ",") == std::string::npos
&&
824 !std::equal(language
.rbegin(), language
.rend(),
825 accept_languages
->rbegin())) {
826 parts
.push_back(language
);
828 parts
.push_back(*accept_languages
);
829 *accept_languages
= base::JoinString(parts
, ",");
834 std::string
PrefServiceBridge::GetAndroidPermissionForContentSetting(
835 ContentSettingsType content_type
) {
836 JNIEnv
* env
= AttachCurrentThread();
837 base::android::ScopedJavaLocalRef
<jstring
> android_permission
=
838 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
840 if (android_permission
.is_null())
841 return std::string();
843 return ConvertJavaStringToUTF8(android_permission
);