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 GetPrefService()->SetBoolean(prefs::kWebKitJavascriptEnabled
, enabled
);
419 static jboolean
GetJavaScriptEnabled(JNIEnv
* env
, jobject obj
) {
420 // The user pref for Javascript is stored in kWebKitJavascriptEnabled for
421 // historical reasons, but the content setting is where a possibly managed
422 // value will be enforced.
423 jboolean javascript_enabled
=
424 GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT
);
425 if (!GetJavaScriptManaged(env
, obj
)) {
426 javascript_enabled
&= GetPrefService()->GetBoolean(
427 prefs::kWebKitJavascriptEnabled
);
429 return javascript_enabled
;
432 static void SetPasswordEchoEnabled(JNIEnv
* env
,
434 jboolean passwordEchoEnabled
) {
435 GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled
,
436 passwordEchoEnabled
);
439 static jboolean
GetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
) {
440 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_POPUPS
);
443 static jboolean
GetAllowPopupsManaged(JNIEnv
* env
, jobject obj
) {
444 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_POPUPS
);
447 static void SetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
448 HostContentSettingsMap
* host_content_settings_map
=
449 GetOriginalProfile()->GetHostContentSettingsMap();
450 host_content_settings_map
->SetDefaultContentSetting(
451 CONTENT_SETTINGS_TYPE_POPUPS
,
452 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
455 static jboolean
GetCameraMicEnabled(JNIEnv
* env
, jobject obj
) {
456 PrefService
* prefs
= GetPrefService();
457 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM
) &&
458 prefs
->GetBoolean(prefs::kAudioCaptureAllowed
) &&
459 prefs
->GetBoolean(prefs::kVideoCaptureAllowed
);
462 static jboolean
GetCameraMicUserModifiable(JNIEnv
* env
, jobject obj
) {
463 PrefService
* prefs
= GetPrefService();
464 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_MEDIASTREAM
) &&
465 prefs
->IsUserModifiablePreference(prefs::kAudioCaptureAllowed
) &&
466 prefs
->IsUserModifiablePreference(prefs::kVideoCaptureAllowed
);
469 static jboolean
GetCameraMicManagedByCustodian(JNIEnv
* env
, jobject obj
) {
470 PrefService
* prefs
= GetPrefService();
471 if (prefs
->IsPreferenceManagedByCustodian(prefs::kVideoCaptureAllowed
))
473 if (prefs
->IsPreferenceManagedByCustodian(prefs::kAudioCaptureAllowed
))
478 static jboolean
GetAutologinEnabled(JNIEnv
* env
, jobject obj
) {
479 return GetPrefService()->GetBoolean(prefs::kAutologinEnabled
);
482 static void SetAutologinEnabled(JNIEnv
* env
, jobject obj
,
483 jboolean autologinEnabled
) {
484 GetPrefService()->SetBoolean(prefs::kAutologinEnabled
, autologinEnabled
);
487 static void SetPopupException(JNIEnv
* env
, jobject obj
, jstring pattern
,
489 HostContentSettingsMap
* host_content_settings_map
=
490 GetOriginalProfile()->GetHostContentSettingsMap();
491 host_content_settings_map
->SetContentSetting(
492 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
493 ContentSettingsPattern::Wildcard(),
494 CONTENT_SETTINGS_TYPE_POPUPS
,
496 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
499 static void RemovePopupException(JNIEnv
* env
, jobject obj
, jstring pattern
) {
500 HostContentSettingsMap
* host_content_settings_map
=
501 GetOriginalProfile()->GetHostContentSettingsMap();
502 host_content_settings_map
->SetContentSetting(
503 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
504 ContentSettingsPattern::Wildcard(),
505 CONTENT_SETTINGS_TYPE_POPUPS
,
507 CONTENT_SETTING_DEFAULT
);
510 static void GetPopupExceptions(JNIEnv
* env
, jobject obj
, jobject list
) {
511 HostContentSettingsMap
* host_content_settings_map
=
512 GetOriginalProfile()->GetHostContentSettingsMap();
513 ContentSettingsForOneType entries
;
514 host_content_settings_map
->GetSettingsForOneType(
515 CONTENT_SETTINGS_TYPE_POPUPS
, "", &entries
);
516 for (size_t i
= 0; i
< entries
.size(); ++i
) {
517 Java_PrefServiceBridge_insertPopupExceptionToList(
519 ConvertUTF8ToJavaString(
520 env
, entries
[i
].primary_pattern
.ToString()).obj(),
521 ConvertUTF8ToJavaString(
522 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
523 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
527 static void SetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
,
529 GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled
, enabled
);
532 static jstring
GetContextualSearchPreference(JNIEnv
* env
, jobject obj
) {
533 return ConvertUTF8ToJavaString(
534 env
, GetPrefService()->GetString(prefs::kContextualSearchEnabled
)).
538 static jboolean
GetContextualSearchPreferenceIsManaged(JNIEnv
* env
,
540 return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled
);
543 static void SetContextualSearchPreference(JNIEnv
* env
, jobject obj
,
545 GetPrefService()->SetString(prefs::kContextualSearchEnabled
,
546 ConvertJavaStringToUTF8(env
, pref
));
549 static void SetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
, int option
) {
550 GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions
, option
);
553 static jboolean
NetworkPredictionEnabledHasUserSetting(JNIEnv
* env
,
555 return GetPrefService()->GetUserPrefValue(
556 prefs::kNetworkPredictionEnabled
) != NULL
;
559 static jboolean
NetworkPredictionOptionsHasUserSetting(JNIEnv
* env
,
561 return GetPrefService()->GetUserPrefValue(
562 prefs::kNetworkPredictionOptions
) != NULL
;
565 static jboolean
GetNetworkPredictionEnabledUserPrefValue(JNIEnv
* env
,
567 const base::Value
* network_prediction_enabled
=
568 GetPrefService()->GetUserPrefValue(prefs::kNetworkPredictionEnabled
);
569 DCHECK(network_prediction_enabled
);
571 DCHECK(network_prediction_enabled
->GetAsBoolean(&value
));
575 static void SetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
,
577 GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled
, enabled
);
580 static jboolean
GetFirstRunEulaAccepted(JNIEnv
* env
, jobject obj
) {
581 return g_browser_process
->local_state()->GetBoolean(prefs::kEulaAccepted
);
584 static void SetEulaAccepted(JNIEnv
* env
, jobject obj
) {
585 g_browser_process
->local_state()->SetBoolean(prefs::kEulaAccepted
, true);
588 static void ResetAcceptLanguages(JNIEnv
* env
,
590 jstring default_locale
) {
591 std::string
accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES
));
592 std::string
locale_string(ConvertJavaStringToUTF8(env
, default_locale
));
594 PrependToAcceptLanguagesIfNecessary(locale_string
, &accept_languages
);
595 GetPrefService()->SetString(prefs::kAcceptLanguages
, accept_languages
);
598 void PrependToAcceptLanguagesIfNecessary(std::string locale
,
599 std::string
* accept_languages
) {
600 if (locale
.size() != 5u || locale
[2] != '_') // not well-formed
603 std::string
language(locale
.substr(0, 2));
604 std::string
region(locale
.substr(3, 2));
606 // Java mostly follows ISO-639-1 and ICU, except for the following three.
607 // See documentation on java.util.Locale constructor for more.
608 if (language
== "iw") {
610 } else if (language
== "ji") {
612 } else if (language
== "in") {
616 std::string
language_region(language
+ "-" + region
);
618 if (accept_languages
->find(language_region
) == std::string::npos
) {
619 std::vector
<std::string
> parts
;
620 parts
.push_back(language_region
);
621 // If language is not in the accept languages list, also add language code.
622 if (accept_languages
->find(language
+ ",") == std::string::npos
&&
623 !std::equal(language
.rbegin(), language
.rend(),
624 accept_languages
->rbegin()))
625 parts
.push_back(language
);
626 parts
.push_back(*accept_languages
);
627 *accept_languages
= JoinString(parts
, ',');
631 // Sends all information about the different versions to Java.
632 // From browser_about_handler.cc
633 static jobject
GetAboutVersionStrings(JNIEnv
* env
, jobject obj
) {
634 chrome::VersionInfo version_info
;
635 std::string os_version
= version_info
.OSType();
636 os_version
+= " " + AndroidAboutAppInfo::GetOsInfo();
638 base::android::BuildInfo
* android_build_info
=
639 base::android::BuildInfo::GetInstance();
640 std::string
application(android_build_info
->package_label());
641 application
.append(" ");
642 application
.append(version_info
.Version());
644 // OK to release, returning to Java.
645 return Java_PrefServiceBridge_createAboutVersionStrings(
647 ConvertUTF8ToJavaString(env
, application
).obj(),
648 ConvertUTF8ToJavaString(env
, content::GetWebKitVersion()).obj(),
649 ConvertUTF8ToJavaString(
650 env
, AndroidAboutAppInfo::GetJavaScriptVersion()).obj(),
651 ConvertUTF8ToJavaString(env
, os_version
).obj()).Release();
654 static void GetProfilePath(JNIEnv
* env
, jobject obj
, jobject j_callback
) {
655 ScopedJavaGlobalRef
<jobject
>* callback
= new ScopedJavaGlobalRef
<jobject
>();
656 callback
->Reset(env
, j_callback
);
657 BrowserThread::PostTaskAndReplyWithResult(
658 BrowserThread::FILE, FROM_HERE
,
659 base::Bind(&GetProfilePathOnFileThread
, GetOriginalProfile()),
660 base::Bind(&OnGotProfilePath
, base::Owned(callback
)));
663 static jstring
GetSupervisedUserCustodianName(JNIEnv
* env
, jobject obj
) {
664 return ConvertUTF8ToJavaString(
665 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName
))
669 static jstring
GetSupervisedUserCustodianEmail(JNIEnv
* env
, jobject obj
) {
670 return ConvertUTF8ToJavaString(
671 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail
))
675 static jstring
GetSupervisedUserCustodianProfileImageURL(JNIEnv
* env
,
677 return ConvertUTF8ToJavaString(
679 GetPrefService()->GetString(
680 prefs::kSupervisedUserCustodianProfileImageURL
)).Release();
683 static jstring
GetSupervisedUserSecondCustodianName(JNIEnv
* env
, jobject obj
) {
684 return ConvertUTF8ToJavaString(
686 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName
))
690 static jstring
GetSupervisedUserSecondCustodianEmail(JNIEnv
* env
, jobject obj
) {
691 return ConvertUTF8ToJavaString(
693 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail
))
697 static jstring
GetSupervisedUserSecondCustodianProfileImageURL(JNIEnv
* env
,
699 return ConvertUTF8ToJavaString(
701 GetPrefService()->GetString(
702 prefs::kSupervisedUserSecondCustodianProfileImageURL
)).Release();
705 bool RegisterPrefServiceBridge(JNIEnv
* env
) {
706 return RegisterNativesImpl(env
);