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/password_manager/core/common/password_manager_pref_names.h"
34 #include "components/translate/core/browser/translate_prefs.h"
35 #include "components/translate/core/common/translate_pref_names.h"
36 #include "components/web_resource/web_resource_pref_names.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/common/user_agent.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 IsContentSettingUserModifiable(ContentSettingsType content_settings_type
) {
109 HostContentSettingsMap
* content_settings
=
110 GetOriginalProfile()->GetHostContentSettingsMap();
111 content_settings
->GetDefaultContentSetting(content_settings_type
, &source
);
112 HostContentSettingsMap::ProviderType provider
=
113 content_settings
->GetProviderTypeFromSource(source
);
114 return provider
>= HostContentSettingsMap::PREF_PROVIDER
;
117 void OnGotProfilePath(ScopedJavaGlobalRef
<jobject
>* callback
,
119 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
120 JNIEnv
* env
= AttachCurrentThread();
121 ScopedJavaLocalRef
<jstring
> j_path
= ConvertUTF8ToJavaString(env
, path
);
122 Java_PrefServiceBridge_onGotProfilePath(env
, j_path
.obj(), callback
->obj());
125 std::string
GetProfilePathOnFileThread(Profile
* profile
) {
126 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
128 return std::string();
130 base::FilePath profile_path
= profile
->GetPath();
131 return base::MakeAbsoluteFilePath(profile_path
).value();
134 PrefService
* GetPrefService() {
135 return GetOriginalProfile()->GetPrefs();
140 // ----------------------------------------------------------------------------
141 // Native JNI methods
142 // ----------------------------------------------------------------------------
144 static jboolean
GetAcceptCookiesEnabled(JNIEnv
* env
, jobject obj
) {
145 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_COOKIES
);
148 static jboolean
GetAcceptCookiesManaged(JNIEnv
* env
, jobject obj
) {
149 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_COOKIES
);
152 static jboolean
GetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
) {
153 return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies
);
156 static jboolean
GetBlockThirdPartyCookiesManaged(JNIEnv
* env
, jobject obj
) {
157 return GetPrefService()->IsManagedPreference(prefs::kBlockThirdPartyCookies
);
160 static jboolean
GetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
) {
161 return GetPrefService()->GetBoolean(
162 password_manager::prefs::kPasswordManagerSavingEnabled
);
165 static jboolean
GetRememberPasswordsManaged(JNIEnv
* env
, jobject obj
) {
166 return GetPrefService()->IsManagedPreference(
167 password_manager::prefs::kPasswordManagerSavingEnabled
);
170 static jboolean
GetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
) {
171 return GetPrefService()->GetBoolean(prefs::kEnableDoNotTrack
);
174 static jint
GetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
) {
175 return GetPrefService()->GetInteger(prefs::kNetworkPredictionOptions
);
178 static jboolean
GetNetworkPredictionManaged(JNIEnv
* env
, jobject obj
) {
179 return GetPrefService()->IsManagedPreference(
180 prefs::kNetworkPredictionOptions
);
183 static jboolean
GetPasswordEchoEnabled(JNIEnv
* env
, jobject obj
) {
184 return GetPrefService()->GetBoolean(prefs::kWebKitPasswordEchoEnabled
);
187 static jboolean
GetPrintingEnabled(JNIEnv
* env
, jobject obj
) {
188 return GetPrefService()->GetBoolean(prefs::kPrintingEnabled
);
191 static jboolean
GetPrintingManaged(JNIEnv
* env
, jobject obj
) {
192 return GetPrefService()->IsManagedPreference(prefs::kPrintingEnabled
);
195 static jboolean
GetTranslateEnabled(JNIEnv
* env
, jobject obj
) {
196 return GetPrefService()->GetBoolean(prefs::kEnableTranslate
);
199 static jboolean
GetTranslateManaged(JNIEnv
* env
, jobject obj
) {
200 return GetPrefService()->IsManagedPreference(prefs::kEnableTranslate
);
203 static jboolean
GetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
) {
204 return GetPrefService()->GetBoolean(prefs::kSearchSuggestEnabled
);
207 static jboolean
GetSearchSuggestManaged(JNIEnv
* env
, jobject obj
) {
208 return GetPrefService()->IsManagedPreference(prefs::kSearchSuggestEnabled
);
211 static jboolean
GetProtectedMediaIdentifierEnabled(JNIEnv
* env
, jobject obj
) {
212 return GetBooleanForContentSetting(
213 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
) &&
214 GetPrefService()->GetBoolean(prefs::kProtectedMediaIdentifierEnabled
);
217 static jboolean
GetPushNotificationsEnabled(JNIEnv
* env
, jobject obj
) {
218 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
221 static jboolean
GetAllowLocationEnabled(JNIEnv
* env
, jobject obj
) {
222 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION
) &&
223 GetPrefService()->GetBoolean(prefs::kGeolocationEnabled
);
226 static jboolean
GetAllowLocationUserModifiable(JNIEnv
* env
, jobject obj
) {
227 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_GEOLOCATION
) &&
228 GetPrefService()->IsUserModifiablePreference(
229 prefs::kGeolocationEnabled
);
232 static jboolean
GetAllowLocationManagedByCustodian(JNIEnv
* env
, jobject obj
) {
233 return GetPrefService()->IsPreferenceManagedByCustodian(
234 prefs::kGeolocationEnabled
);
237 static jboolean
GetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
) {
238 return GetPrefService()->GetBoolean(prefs::kAlternateErrorPagesEnabled
);
241 static jboolean
GetResolveNavigationErrorManaged(JNIEnv
* env
, jobject obj
) {
242 return GetPrefService()->IsManagedPreference(
243 prefs::kAlternateErrorPagesEnabled
);
246 static jboolean
GetCrashReportManaged(JNIEnv
* env
, jobject obj
) {
247 return GetPrefService()->IsManagedPreference(
248 prefs::kCrashReportingEnabled
);
251 static jboolean
GetForceSafeSearch(JNIEnv
* env
, jobject obj
) {
252 return GetPrefService()->GetBoolean(prefs::kForceSafeSearch
);
255 static jint
GetDefaultSupervisedUserFilteringBehavior(JNIEnv
* env
,
257 return GetPrefService()->GetInteger(
258 prefs::kDefaultSupervisedUserFilteringBehavior
);
261 static jboolean
GetIncognitoModeEnabled(JNIEnv
* env
, jobject obj
) {
262 PrefService
* prefs
= GetPrefService();
263 IncognitoModePrefs::Availability incognito_pref
=
264 IncognitoModePrefs::GetAvailability(prefs
);
265 DCHECK(incognito_pref
== IncognitoModePrefs::ENABLED
||
266 incognito_pref
== IncognitoModePrefs::DISABLED
) <<
267 "Unsupported incognito mode preference: " << incognito_pref
;
268 return incognito_pref
!= IncognitoModePrefs::DISABLED
;
271 static jboolean
GetIncognitoModeManaged(JNIEnv
* env
, jobject obj
) {
272 return GetPrefService()->IsManagedPreference(
273 prefs::kIncognitoModeAvailability
);
278 // Redirects a BrowsingDataRemover completion callback back into Java.
279 class ClearBrowsingDataObserver
: public BrowsingDataRemover::Observer
{
281 // |obj| is expected to be the object passed into ClearBrowsingData(); e.g. a
283 ClearBrowsingDataObserver(JNIEnv
* env
, jobject obj
)
284 : weak_chrome_native_preferences_(env
, obj
) {
287 void OnBrowsingDataRemoverDone() override
{
288 // Just as a BrowsingDataRemover deletes itself when done, we delete ourself
289 // when done. No need to remove ourself as an observer given the lifetime
290 // of BrowsingDataRemover.
291 scoped_ptr
<ClearBrowsingDataObserver
> auto_delete(this);
293 JNIEnv
* env
= AttachCurrentThread();
294 if (weak_chrome_native_preferences_
.get(env
).is_null())
297 Java_PrefServiceBridge_browsingDataCleared(
298 env
, weak_chrome_native_preferences_
.get(env
).obj());
302 JavaObjectWeakGlobalRef weak_chrome_native_preferences_
;
306 static void ClearBrowsingData(JNIEnv
* env
, jobject obj
, jboolean history
,
307 jboolean cache
, jboolean cookies_and_site_data
, jboolean passwords
,
308 jboolean form_data
) {
309 // BrowsingDataRemover deletes itself.
310 BrowsingDataRemover
* browsing_data_remover
=
311 BrowsingDataRemover::CreateForPeriod(
312 GetOriginalProfile(),
313 static_cast<BrowsingDataRemover::TimePeriod
>(
314 BrowsingDataRemover::EVERYTHING
));
315 browsing_data_remover
->AddObserver(new ClearBrowsingDataObserver(env
, obj
));
319 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
321 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
322 if (cookies_and_site_data
) {
323 remove_mask
|= BrowsingDataRemover::REMOVE_COOKIES
;
324 remove_mask
|= BrowsingDataRemover::REMOVE_SITE_DATA
;
327 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
329 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
330 browsing_data_remover
->Remove(remove_mask
,
331 BrowsingDataHelper::UNPROTECTED_WEB
);
334 static void SetAllowCookiesEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
335 HostContentSettingsMap
* host_content_settings_map
=
336 GetOriginalProfile()->GetHostContentSettingsMap();
337 host_content_settings_map
->SetDefaultContentSetting(
338 CONTENT_SETTINGS_TYPE_COOKIES
,
339 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
342 static void SetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
,
344 GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies
, enabled
);
347 static void SetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
,
349 GetPrefService()->SetBoolean(
350 password_manager::prefs::kPasswordManagerSavingEnabled
, allow
);
353 static void SetProtectedMediaIdentifierEnabled(JNIEnv
* env
,
355 jboolean is_enabled
) {
356 GetPrefService()->SetBoolean(prefs::kProtectedMediaIdentifierEnabled
,
360 static void SetAllowLocationEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
361 GetPrefService()->SetBoolean(prefs::kGeolocationEnabled
, allow
);
364 static void SetCameraMicEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
365 HostContentSettingsMap
* host_content_settings_map
=
366 GetOriginalProfile()->GetHostContentSettingsMap();
367 host_content_settings_map
->SetDefaultContentSetting(
368 CONTENT_SETTINGS_TYPE_MEDIASTREAM
,
369 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
372 static void SetPushNotificationsEnabled(JNIEnv
* env
,
375 HostContentSettingsMap
* host_content_settings_map
=
376 GetOriginalProfile()->GetHostContentSettingsMap();
377 host_content_settings_map
->SetDefaultContentSetting(
378 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
379 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
382 static void SetCrashReporting(JNIEnv
* env
, jobject obj
, jboolean reporting
) {
383 PrefService
* local_state
= g_browser_process
->local_state();
384 local_state
->SetBoolean(prefs::kCrashReportingEnabled
, reporting
);
387 static jboolean
CanPredictNetworkActions(JNIEnv
* env
, jobject obj
) {
388 return chrome_browser_net::CanPrefetchAndPrerenderUI(GetPrefService());
391 static void SetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
392 GetPrefService()->SetBoolean(prefs::kEnableDoNotTrack
, allow
);
395 static jstring
GetSyncLastAccountName(JNIEnv
* env
, jobject obj
) {
396 return ConvertUTF8ToJavaString(
397 env
, GetPrefService()->GetString(prefs::kGoogleServicesLastUsername
))
401 static void SetTranslateEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
) {
402 GetPrefService()->SetBoolean(prefs::kEnableTranslate
, enabled
);
405 static void ResetTranslateDefaults(JNIEnv
* env
, jobject obj
) {
406 scoped_ptr
<translate::TranslatePrefs
> translate_prefs
=
407 ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
408 translate_prefs
->ResetToDefaults();
411 static jboolean
GetJavaScriptManaged(JNIEnv
* env
, jobject obj
) {
412 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_JAVASCRIPT
);
415 static void SetJavaScriptEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
) {
416 HostContentSettingsMap
* host_content_settings_map
=
417 GetOriginalProfile()->GetHostContentSettingsMap();
418 host_content_settings_map
->SetDefaultContentSetting(
419 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
420 enabled
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
423 static jboolean
GetJavaScriptEnabled(JNIEnv
* env
, jobject obj
) {
424 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT
);
427 static void MigrateJavascriptPreference(JNIEnv
* env
, jobject obj
) {
428 const PrefService::Preference
* javascript_pref
=
429 GetPrefService()->FindPreference(prefs::kWebKitJavascriptEnabled
);
430 DCHECK(javascript_pref
);
432 if (!javascript_pref
->HasUserSetting())
435 bool javascript_enabled
= false;
436 bool retval
= javascript_pref
->GetValue()->GetAsBoolean(&javascript_enabled
);
438 SetJavaScriptEnabled(env
, obj
, javascript_enabled
);
439 GetPrefService()->ClearPref(prefs::kWebKitJavascriptEnabled
);
442 static void SetPasswordEchoEnabled(JNIEnv
* env
,
444 jboolean passwordEchoEnabled
) {
445 GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled
,
446 passwordEchoEnabled
);
449 static jboolean
GetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
) {
450 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_POPUPS
);
453 static jboolean
GetAllowPopupsManaged(JNIEnv
* env
, jobject obj
) {
454 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_POPUPS
);
457 static void SetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
458 HostContentSettingsMap
* host_content_settings_map
=
459 GetOriginalProfile()->GetHostContentSettingsMap();
460 host_content_settings_map
->SetDefaultContentSetting(
461 CONTENT_SETTINGS_TYPE_POPUPS
,
462 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
465 static jboolean
GetCameraMicEnabled(JNIEnv
* env
, jobject obj
) {
466 PrefService
* prefs
= GetPrefService();
467 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM
) &&
468 prefs
->GetBoolean(prefs::kAudioCaptureAllowed
) &&
469 prefs
->GetBoolean(prefs::kVideoCaptureAllowed
);
472 static jboolean
GetCameraMicUserModifiable(JNIEnv
* env
, jobject obj
) {
473 PrefService
* prefs
= GetPrefService();
474 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_MEDIASTREAM
) &&
475 prefs
->IsUserModifiablePreference(prefs::kAudioCaptureAllowed
) &&
476 prefs
->IsUserModifiablePreference(prefs::kVideoCaptureAllowed
);
479 static jboolean
GetCameraMicManagedByCustodian(JNIEnv
* env
, jobject obj
) {
480 PrefService
* prefs
= GetPrefService();
481 if (prefs
->IsPreferenceManagedByCustodian(prefs::kVideoCaptureAllowed
))
483 if (prefs
->IsPreferenceManagedByCustodian(prefs::kAudioCaptureAllowed
))
488 static jboolean
GetAutologinEnabled(JNIEnv
* env
, jobject obj
) {
489 return GetPrefService()->GetBoolean(prefs::kAutologinEnabled
);
492 static void SetAutologinEnabled(JNIEnv
* env
, jobject obj
,
493 jboolean autologinEnabled
) {
494 GetPrefService()->SetBoolean(prefs::kAutologinEnabled
, autologinEnabled
);
497 static void SetJavaScriptAllowed(JNIEnv
* env
, jobject obj
, jstring pattern
,
499 HostContentSettingsMap
* host_content_settings_map
=
500 GetOriginalProfile()->GetHostContentSettingsMap();
501 host_content_settings_map
->SetContentSetting(
502 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
503 ContentSettingsPattern::Wildcard(),
504 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
506 static_cast<ContentSetting
>(setting
));
509 static void GetJavaScriptExceptions(JNIEnv
* env
, jobject obj
, jobject list
) {
510 HostContentSettingsMap
* host_content_settings_map
=
511 GetOriginalProfile()->GetHostContentSettingsMap();
512 ContentSettingsForOneType entries
;
513 host_content_settings_map
->GetSettingsForOneType(
514 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, "", &entries
);
515 for (size_t i
= 0; i
< entries
.size(); ++i
) {
516 Java_PrefServiceBridge_addJavaScriptExceptionToList(
518 ConvertUTF8ToJavaString(
519 env
, entries
[i
].primary_pattern
.ToString()).obj(),
520 ConvertUTF8ToJavaString(
521 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
522 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
526 static void SetPopupException(JNIEnv
* env
, jobject obj
, jstring pattern
,
528 HostContentSettingsMap
* host_content_settings_map
=
529 GetOriginalProfile()->GetHostContentSettingsMap();
530 host_content_settings_map
->SetContentSetting(
531 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
532 ContentSettingsPattern::Wildcard(),
533 CONTENT_SETTINGS_TYPE_POPUPS
,
535 static_cast<ContentSetting
>(setting
));
538 static void GetPopupExceptions(JNIEnv
* env
, jobject obj
, jobject list
) {
539 HostContentSettingsMap
* host_content_settings_map
=
540 GetOriginalProfile()->GetHostContentSettingsMap();
541 ContentSettingsForOneType entries
;
542 host_content_settings_map
->GetSettingsForOneType(
543 CONTENT_SETTINGS_TYPE_POPUPS
, "", &entries
);
544 for (size_t i
= 0; i
< entries
.size(); ++i
) {
545 Java_PrefServiceBridge_insertPopupExceptionToList(
547 ConvertUTF8ToJavaString(
548 env
, entries
[i
].primary_pattern
.ToString()).obj(),
549 ConvertUTF8ToJavaString(
550 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
551 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
555 static void SetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
,
557 GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled
, enabled
);
560 static jstring
GetContextualSearchPreference(JNIEnv
* env
, jobject obj
) {
561 return ConvertUTF8ToJavaString(
562 env
, GetPrefService()->GetString(prefs::kContextualSearchEnabled
)).
566 static jboolean
GetContextualSearchPreferenceIsManaged(JNIEnv
* env
,
568 return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled
);
571 static void SetContextualSearchPreference(JNIEnv
* env
, jobject obj
,
573 GetPrefService()->SetString(prefs::kContextualSearchEnabled
,
574 ConvertJavaStringToUTF8(env
, pref
));
577 static void SetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
, int option
) {
578 GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions
, option
);
581 static jboolean
NetworkPredictionEnabledHasUserSetting(JNIEnv
* env
,
583 return GetPrefService()->GetUserPrefValue(
584 prefs::kNetworkPredictionEnabled
) != NULL
;
587 static jboolean
NetworkPredictionOptionsHasUserSetting(JNIEnv
* env
,
589 return GetPrefService()->GetUserPrefValue(
590 prefs::kNetworkPredictionOptions
) != NULL
;
593 static jboolean
GetNetworkPredictionEnabledUserPrefValue(JNIEnv
* env
,
595 const base::Value
* network_prediction_enabled
=
596 GetPrefService()->GetUserPrefValue(prefs::kNetworkPredictionEnabled
);
597 DCHECK(network_prediction_enabled
);
599 DCHECK(network_prediction_enabled
->GetAsBoolean(&value
));
603 static void SetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
,
605 GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled
, enabled
);
608 static jboolean
GetFirstRunEulaAccepted(JNIEnv
* env
, jobject obj
) {
609 return g_browser_process
->local_state()->GetBoolean(prefs::kEulaAccepted
);
612 static void SetEulaAccepted(JNIEnv
* env
, jobject obj
) {
613 g_browser_process
->local_state()->SetBoolean(prefs::kEulaAccepted
, true);
616 static void ResetAcceptLanguages(JNIEnv
* env
,
618 jstring default_locale
) {
619 std::string
accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES
));
620 std::string
locale_string(ConvertJavaStringToUTF8(env
, default_locale
));
622 PrependToAcceptLanguagesIfNecessary(locale_string
, &accept_languages
);
623 GetPrefService()->SetString(prefs::kAcceptLanguages
, accept_languages
);
626 void PrependToAcceptLanguagesIfNecessary(std::string locale
,
627 std::string
* accept_languages
) {
628 if (locale
.size() != 5u || locale
[2] != '_') // not well-formed
631 std::string
language(locale
.substr(0, 2));
632 std::string
region(locale
.substr(3, 2));
634 // Java mostly follows ISO-639-1 and ICU, except for the following three.
635 // See documentation on java.util.Locale constructor for more.
636 if (language
== "iw") {
638 } else if (language
== "ji") {
640 } else if (language
== "in") {
644 std::string
language_region(language
+ "-" + region
);
646 if (accept_languages
->find(language_region
) == std::string::npos
) {
647 std::vector
<std::string
> parts
;
648 parts
.push_back(language_region
);
649 // If language is not in the accept languages list, also add language code.
650 if (accept_languages
->find(language
+ ",") == std::string::npos
&&
651 !std::equal(language
.rbegin(), language
.rend(),
652 accept_languages
->rbegin()))
653 parts
.push_back(language
);
654 parts
.push_back(*accept_languages
);
655 *accept_languages
= JoinString(parts
, ',');
659 // Sends all information about the different versions to Java.
660 // From browser_about_handler.cc
661 static jobject
GetAboutVersionStrings(JNIEnv
* env
, jobject obj
) {
662 chrome::VersionInfo version_info
;
663 std::string os_version
= version_info
.OSType();
664 os_version
+= " " + AndroidAboutAppInfo::GetOsInfo();
666 base::android::BuildInfo
* android_build_info
=
667 base::android::BuildInfo::GetInstance();
668 std::string
application(android_build_info
->package_label());
669 application
.append(" ");
670 application
.append(version_info
.Version());
672 // OK to release, returning to Java.
673 return Java_PrefServiceBridge_createAboutVersionStrings(
675 ConvertUTF8ToJavaString(env
, application
).obj(),
676 ConvertUTF8ToJavaString(env
, content::GetWebKitVersion()).obj(),
677 ConvertUTF8ToJavaString(
678 env
, AndroidAboutAppInfo::GetJavaScriptVersion()).obj(),
679 ConvertUTF8ToJavaString(env
, os_version
).obj()).Release();
682 static void GetProfilePath(JNIEnv
* env
, jobject obj
, jobject j_callback
) {
683 ScopedJavaGlobalRef
<jobject
>* callback
= new ScopedJavaGlobalRef
<jobject
>();
684 callback
->Reset(env
, j_callback
);
685 BrowserThread::PostTaskAndReplyWithResult(
686 BrowserThread::FILE, FROM_HERE
,
687 base::Bind(&GetProfilePathOnFileThread
, GetOriginalProfile()),
688 base::Bind(&OnGotProfilePath
, base::Owned(callback
)));
691 static jstring
GetSupervisedUserCustodianName(JNIEnv
* env
, jobject obj
) {
692 return ConvertUTF8ToJavaString(
693 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName
))
697 static jstring
GetSupervisedUserCustodianEmail(JNIEnv
* env
, jobject obj
) {
698 return ConvertUTF8ToJavaString(
699 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail
))
703 static jstring
GetSupervisedUserCustodianProfileImageURL(JNIEnv
* env
,
705 return ConvertUTF8ToJavaString(
707 GetPrefService()->GetString(
708 prefs::kSupervisedUserCustodianProfileImageURL
)).Release();
711 static jstring
GetSupervisedUserSecondCustodianName(JNIEnv
* env
, jobject obj
) {
712 return ConvertUTF8ToJavaString(
714 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName
))
718 static jstring
GetSupervisedUserSecondCustodianEmail(JNIEnv
* env
, jobject obj
) {
719 return ConvertUTF8ToJavaString(
721 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail
))
725 static jstring
GetSupervisedUserSecondCustodianProfileImageURL(JNIEnv
* env
,
727 return ConvertUTF8ToJavaString(
729 GetPrefService()->GetString(
730 prefs::kSupervisedUserSecondCustodianProfileImageURL
)).Release();
733 bool RegisterPrefServiceBridge(JNIEnv
* env
) {
734 return RegisterNativesImpl(env
);