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 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 void OnGotProfilePath(ScopedJavaGlobalRef
<jobject
>* callback
,
130 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
131 JNIEnv
* env
= AttachCurrentThread();
132 ScopedJavaLocalRef
<jstring
> j_path
= ConvertUTF8ToJavaString(env
, path
);
133 Java_PrefServiceBridge_onGotProfilePath(env
, j_path
.obj(), callback
->obj());
136 std::string
GetProfilePathOnFileThread(Profile
* profile
) {
137 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
139 return std::string();
141 base::FilePath profile_path
= profile
->GetPath();
142 return base::MakeAbsoluteFilePath(profile_path
).value();
145 PrefService
* GetPrefService() {
146 return GetOriginalProfile()->GetPrefs();
151 // ----------------------------------------------------------------------------
152 // Native JNI methods
153 // ----------------------------------------------------------------------------
155 static jboolean
GetAcceptCookiesEnabled(JNIEnv
* env
, jobject obj
) {
156 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_COOKIES
);
159 static jboolean
GetAcceptCookiesManaged(JNIEnv
* env
, jobject obj
) {
160 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_COOKIES
);
163 static jboolean
GetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
) {
164 return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies
);
167 static jboolean
GetBlockThirdPartyCookiesManaged(JNIEnv
* env
, jobject obj
) {
168 return GetPrefService()->IsManagedPreference(prefs::kBlockThirdPartyCookies
);
171 static jboolean
GetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
) {
172 return GetPrefService()->GetBoolean(
173 password_manager::prefs::kPasswordManagerSavingEnabled
);
176 static jboolean
GetPasswordManagerAutoSigninEnabled(JNIEnv
* env
, jobject obj
) {
177 return GetPrefService()->GetBoolean(
178 password_manager::prefs::kPasswordManagerAutoSignin
);
181 static jboolean
GetRememberPasswordsManaged(JNIEnv
* env
, jobject obj
) {
182 return GetPrefService()->IsManagedPreference(
183 password_manager::prefs::kPasswordManagerSavingEnabled
);
186 static jboolean
GetPasswordManagerAutoSigninManaged(JNIEnv
* env
, jobject obj
) {
187 return GetPrefService()->IsManagedPreference(
188 password_manager::prefs::kPasswordManagerAutoSignin
);
191 static jboolean
GetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
) {
192 return GetPrefService()->GetBoolean(prefs::kEnableDoNotTrack
);
195 static jint
GetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
) {
196 return GetPrefService()->GetInteger(prefs::kNetworkPredictionOptions
);
199 static jboolean
GetNetworkPredictionManaged(JNIEnv
* env
, jobject obj
) {
200 return GetPrefService()->IsManagedPreference(
201 prefs::kNetworkPredictionOptions
);
204 static jboolean
GetPasswordEchoEnabled(JNIEnv
* env
, jobject obj
) {
205 return GetPrefService()->GetBoolean(prefs::kWebKitPasswordEchoEnabled
);
208 static jboolean
GetPrintingEnabled(JNIEnv
* env
, jobject obj
) {
209 return GetPrefService()->GetBoolean(prefs::kPrintingEnabled
);
212 static jboolean
GetPrintingManaged(JNIEnv
* env
, jobject obj
) {
213 return GetPrefService()->IsManagedPreference(prefs::kPrintingEnabled
);
216 static jboolean
GetTranslateEnabled(JNIEnv
* env
, jobject obj
) {
217 return GetPrefService()->GetBoolean(prefs::kEnableTranslate
);
220 static jboolean
GetTranslateManaged(JNIEnv
* env
, jobject obj
) {
221 return GetPrefService()->IsManagedPreference(prefs::kEnableTranslate
);
224 static jboolean
GetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
) {
225 return GetPrefService()->GetBoolean(prefs::kSearchSuggestEnabled
);
228 static jboolean
GetSearchSuggestManaged(JNIEnv
* env
, jobject obj
) {
229 return GetPrefService()->IsManagedPreference(prefs::kSearchSuggestEnabled
);
232 static jboolean
GetProtectedMediaIdentifierEnabled(JNIEnv
* env
, jobject obj
) {
233 // Migrate Settings from Preferences to the Host Content Settings Map.
234 // TODO (knn): Remove this once users have migrated.
235 PrefService
* prefs
= GetPrefService();
236 if (!prefs
->GetBoolean(prefs::kProtectedMediaIdentifierEnabled
)) {
237 prefs
->SetBoolean(prefs::kProtectedMediaIdentifierEnabled
, true);
238 SetProtectedMediaIdentifierEnabled(env
, obj
, false);
240 return GetBooleanForContentSetting(
241 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
);
244 static jboolean
GetPushNotificationsEnabled(JNIEnv
* env
, jobject obj
) {
245 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS
);
248 static jboolean
GetAllowLocationEnabled(JNIEnv
* env
, jobject obj
) {
249 // Migrate Settings from Preferences to the Host Content Settings Map.
250 // TODO (knn): Remove this once users have migrated.
251 PrefService
* prefs
= GetPrefService();
252 // Non-user-modifiable preference levels should have been migrated already.
253 DCHECK(prefs
->IsUserModifiablePreference(prefs::kGeolocationEnabled
));
254 if (!prefs
->GetBoolean(prefs::kGeolocationEnabled
)) {
255 prefs
->SetBoolean(prefs::kGeolocationEnabled
, true);
256 SetAllowLocationEnabled(env
, obj
, false);
258 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
261 static jboolean
GetAllowLocationUserModifiable(JNIEnv
* env
, jobject obj
) {
262 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
265 static jboolean
GetAllowLocationManagedByCustodian(JNIEnv
* env
, jobject obj
) {
266 return IsContentSettingManagedByCustodian(CONTENT_SETTINGS_TYPE_GEOLOCATION
);
269 static jboolean
GetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
) {
270 return GetPrefService()->GetBoolean(prefs::kAlternateErrorPagesEnabled
);
273 static jboolean
GetResolveNavigationErrorManaged(JNIEnv
* env
, jobject obj
) {
274 return GetPrefService()->IsManagedPreference(
275 prefs::kAlternateErrorPagesEnabled
);
278 static jboolean
GetCrashReportManaged(JNIEnv
* env
, jobject obj
) {
279 return GetPrefService()->IsManagedPreference(
280 prefs::kCrashReportingEnabled
);
283 static jboolean
GetForceSafeSearch(JNIEnv
* env
, jobject obj
) {
284 return GetPrefService()->GetBoolean(prefs::kForceSafeSearch
);
287 static jint
GetDefaultSupervisedUserFilteringBehavior(JNIEnv
* env
,
289 return GetPrefService()->GetInteger(
290 prefs::kDefaultSupervisedUserFilteringBehavior
);
293 static jboolean
GetIncognitoModeEnabled(JNIEnv
* env
, jobject obj
) {
294 PrefService
* prefs
= GetPrefService();
295 IncognitoModePrefs::Availability incognito_pref
=
296 IncognitoModePrefs::GetAvailability(prefs
);
297 DCHECK(incognito_pref
== IncognitoModePrefs::ENABLED
||
298 incognito_pref
== IncognitoModePrefs::DISABLED
) <<
299 "Unsupported incognito mode preference: " << incognito_pref
;
300 return incognito_pref
!= IncognitoModePrefs::DISABLED
;
303 static jboolean
GetIncognitoModeManaged(JNIEnv
* env
, jobject obj
) {
304 return GetPrefService()->IsManagedPreference(
305 prefs::kIncognitoModeAvailability
);
308 static jboolean
GetFullscreenManaged(JNIEnv
* env
, jobject obj
) {
309 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_FULLSCREEN
);
312 static jboolean
GetFullscreenAllowed(JNIEnv
* env
, jobject obj
) {
313 HostContentSettingsMap
* content_settings
=
314 GetOriginalProfile()->GetHostContentSettingsMap();
315 return content_settings
->GetDefaultContentSetting(
316 CONTENT_SETTINGS_TYPE_FULLSCREEN
, NULL
) == CONTENT_SETTING_ALLOW
;
321 // Redirects a BrowsingDataRemover completion callback back into Java.
322 class ClearBrowsingDataObserver
: public BrowsingDataRemover::Observer
{
324 // |obj| is expected to be the object passed into ClearBrowsingData(); e.g. a
326 ClearBrowsingDataObserver(JNIEnv
* env
, jobject obj
)
327 : weak_chrome_native_preferences_(env
, obj
) {
330 void OnBrowsingDataRemoverDone() override
{
331 // Just as a BrowsingDataRemover deletes itself when done, we delete ourself
332 // when done. No need to remove ourself as an observer given the lifetime
333 // of BrowsingDataRemover.
334 scoped_ptr
<ClearBrowsingDataObserver
> auto_delete(this);
336 JNIEnv
* env
= AttachCurrentThread();
337 if (weak_chrome_native_preferences_
.get(env
).is_null())
340 Java_PrefServiceBridge_browsingDataCleared(
341 env
, weak_chrome_native_preferences_
.get(env
).obj());
345 JavaObjectWeakGlobalRef weak_chrome_native_preferences_
;
349 static void ClearBrowsingData(JNIEnv
* env
, jobject obj
, jboolean history
,
350 jboolean cache
, jboolean cookies_and_site_data
, jboolean passwords
,
351 jboolean form_data
) {
352 // BrowsingDataRemover deletes itself.
353 BrowsingDataRemover
* browsing_data_remover
=
354 BrowsingDataRemover::CreateForPeriod(
355 GetOriginalProfile(),
356 static_cast<BrowsingDataRemover::TimePeriod
>(
357 BrowsingDataRemover::EVERYTHING
));
358 browsing_data_remover
->AddObserver(new ClearBrowsingDataObserver(env
, obj
));
362 remove_mask
|= BrowsingDataRemover::REMOVE_HISTORY
;
364 remove_mask
|= BrowsingDataRemover::REMOVE_CACHE
;
365 if (cookies_and_site_data
) {
366 remove_mask
|= BrowsingDataRemover::REMOVE_COOKIES
;
367 remove_mask
|= BrowsingDataRemover::REMOVE_SITE_DATA
;
370 remove_mask
|= BrowsingDataRemover::REMOVE_PASSWORDS
;
372 remove_mask
|= BrowsingDataRemover::REMOVE_FORM_DATA
;
373 browsing_data_remover
->Remove(remove_mask
,
374 BrowsingDataHelper::UNPROTECTED_WEB
);
377 static jboolean
CanDeleteBrowsingHistory(JNIEnv
* env
, jobject obj
) {
378 return GetPrefService()->GetBoolean(prefs::kAllowDeletingBrowserHistory
);
381 static void SetAllowCookiesEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
382 HostContentSettingsMap
* host_content_settings_map
=
383 GetOriginalProfile()->GetHostContentSettingsMap();
384 host_content_settings_map
->SetDefaultContentSetting(
385 CONTENT_SETTINGS_TYPE_COOKIES
,
386 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
389 static void SetBlockThirdPartyCookiesEnabled(JNIEnv
* env
, jobject obj
,
391 GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies
, enabled
);
394 static void SetRememberPasswordsEnabled(JNIEnv
* env
, jobject obj
,
396 GetPrefService()->SetBoolean(
397 password_manager::prefs::kPasswordManagerSavingEnabled
, allow
);
400 static void SetPasswordManagerAutoSigninEnabled(JNIEnv
* env
,
403 GetPrefService()->SetBoolean(
404 password_manager::prefs::kPasswordManagerAutoSignin
, enabled
);
407 static void SetProtectedMediaIdentifierEnabled(JNIEnv
* env
,
409 jboolean is_enabled
) {
410 HostContentSettingsMap
* host_content_settings_map
=
411 GetOriginalProfile()->GetHostContentSettingsMap();
412 host_content_settings_map
->SetDefaultContentSetting(
413 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER
,
414 is_enabled
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
417 static void SetAllowLocationEnabled(JNIEnv
* env
,
419 jboolean is_enabled
) {
420 HostContentSettingsMap
* host_content_settings_map
=
421 GetOriginalProfile()->GetHostContentSettingsMap();
422 host_content_settings_map
->SetDefaultContentSetting(
423 CONTENT_SETTINGS_TYPE_GEOLOCATION
,
424 is_enabled
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
427 static void SetCameraMicEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
428 HostContentSettingsMap
* host_content_settings_map
=
429 GetOriginalProfile()->GetHostContentSettingsMap();
430 host_content_settings_map
->SetDefaultContentSetting(
431 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
,
432 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
433 host_content_settings_map
->SetDefaultContentSetting(
434 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
,
435 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
438 static void SetFullscreenAllowed(JNIEnv
* env
, jobject obj
, jboolean allow
) {
439 HostContentSettingsMap
* host_content_settings_map
=
440 GetOriginalProfile()->GetHostContentSettingsMap();
441 host_content_settings_map
->SetDefaultContentSetting(
442 CONTENT_SETTINGS_TYPE_FULLSCREEN
,
443 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_ASK
);
446 static void SetPushNotificationsEnabled(JNIEnv
* env
,
449 HostContentSettingsMap
* host_content_settings_map
=
450 GetOriginalProfile()->GetHostContentSettingsMap();
451 host_content_settings_map
->SetDefaultContentSetting(
452 CONTENT_SETTINGS_TYPE_NOTIFICATIONS
,
453 allow
? CONTENT_SETTING_ASK
: CONTENT_SETTING_BLOCK
);
456 static void SetCrashReporting(JNIEnv
* env
, jobject obj
, jboolean reporting
) {
457 PrefService
* local_state
= g_browser_process
->local_state();
458 local_state
->SetBoolean(prefs::kCrashReportingEnabled
, reporting
);
461 static jboolean
CanPredictNetworkActions(JNIEnv
* env
, jobject obj
) {
462 return chrome_browser_net::CanPrefetchAndPrerenderUI(GetPrefService());
465 static void SetDoNotTrackEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
466 GetPrefService()->SetBoolean(prefs::kEnableDoNotTrack
, allow
);
469 static jstring
GetSyncLastAccountName(JNIEnv
* env
, jobject obj
) {
470 return ConvertUTF8ToJavaString(
471 env
, GetPrefService()->GetString(prefs::kGoogleServicesLastUsername
))
475 static void SetTranslateEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
) {
476 GetPrefService()->SetBoolean(prefs::kEnableTranslate
, enabled
);
479 static void ResetTranslateDefaults(JNIEnv
* env
, jobject obj
) {
480 scoped_ptr
<translate::TranslatePrefs
> translate_prefs
=
481 ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
482 translate_prefs
->ResetToDefaults();
485 static jboolean
GetJavaScriptManaged(JNIEnv
* env
, jobject obj
) {
486 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_JAVASCRIPT
);
489 static void SetJavaScriptEnabled(JNIEnv
* env
, jobject obj
, jboolean enabled
) {
490 HostContentSettingsMap
* host_content_settings_map
=
491 GetOriginalProfile()->GetHostContentSettingsMap();
492 host_content_settings_map
->SetDefaultContentSetting(
493 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
494 enabled
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
497 static jboolean
GetJavaScriptEnabled(JNIEnv
* env
, jobject obj
) {
498 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_JAVASCRIPT
);
501 static void MigrateJavascriptPreference(JNIEnv
* env
, jobject obj
) {
502 const PrefService::Preference
* javascript_pref
=
503 GetPrefService()->FindPreference(prefs::kWebKitJavascriptEnabled
);
504 DCHECK(javascript_pref
);
506 if (!javascript_pref
->HasUserSetting())
509 bool javascript_enabled
= false;
510 bool retval
= javascript_pref
->GetValue()->GetAsBoolean(&javascript_enabled
);
512 SetJavaScriptEnabled(env
, obj
, javascript_enabled
);
513 GetPrefService()->ClearPref(prefs::kWebKitJavascriptEnabled
);
516 static void SetPasswordEchoEnabled(JNIEnv
* env
,
518 jboolean passwordEchoEnabled
) {
519 GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled
,
520 passwordEchoEnabled
);
523 static jboolean
GetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
) {
524 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_POPUPS
);
527 static jboolean
GetAllowPopupsManaged(JNIEnv
* env
, jobject obj
) {
528 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_POPUPS
);
531 static void SetAllowPopupsEnabled(JNIEnv
* env
, jobject obj
, jboolean allow
) {
532 HostContentSettingsMap
* host_content_settings_map
=
533 GetOriginalProfile()->GetHostContentSettingsMap();
534 host_content_settings_map
->SetDefaultContentSetting(
535 CONTENT_SETTINGS_TYPE_POPUPS
,
536 allow
? CONTENT_SETTING_ALLOW
: CONTENT_SETTING_BLOCK
);
539 static jboolean
GetCameraMicEnabled(JNIEnv
* env
, jobject obj
) {
540 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
) &&
541 GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
544 static jboolean
GetCameraMicUserModifiable(JNIEnv
* env
, jobject obj
) {
545 return IsContentSettingUserModifiable(
546 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
) &&
547 IsContentSettingUserModifiable(
548 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
551 static jboolean
GetCameraMicManagedByCustodian(JNIEnv
* env
, jobject obj
) {
552 return IsContentSettingManagedByCustodian(
553 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
) &&
554 IsContentSettingManagedByCustodian(
555 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
558 static jboolean
GetAutologinEnabled(JNIEnv
* env
, jobject obj
) {
559 return GetPrefService()->GetBoolean(prefs::kAutologinEnabled
);
562 static void SetAutologinEnabled(JNIEnv
* env
, jobject obj
,
563 jboolean autologinEnabled
) {
564 GetPrefService()->SetBoolean(prefs::kAutologinEnabled
, autologinEnabled
);
567 static void SetJavaScriptAllowed(JNIEnv
* env
, jobject obj
, jstring pattern
,
569 HostContentSettingsMap
* host_content_settings_map
=
570 GetOriginalProfile()->GetHostContentSettingsMap();
571 host_content_settings_map
->SetContentSetting(
572 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
573 ContentSettingsPattern::Wildcard(),
574 CONTENT_SETTINGS_TYPE_JAVASCRIPT
,
576 static_cast<ContentSetting
>(setting
));
579 static void GetJavaScriptExceptions(JNIEnv
* env
, jobject obj
, jobject list
) {
580 HostContentSettingsMap
* host_content_settings_map
=
581 GetOriginalProfile()->GetHostContentSettingsMap();
582 ContentSettingsForOneType entries
;
583 host_content_settings_map
->GetSettingsForOneType(
584 CONTENT_SETTINGS_TYPE_JAVASCRIPT
, "", &entries
);
585 for (size_t i
= 0; i
< entries
.size(); ++i
) {
586 Java_PrefServiceBridge_addJavaScriptExceptionToList(
588 ConvertUTF8ToJavaString(
589 env
, entries
[i
].primary_pattern
.ToString()).obj(),
590 ConvertUTF8ToJavaString(
591 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
592 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
596 static void SetPopupException(JNIEnv
* env
, jobject obj
, jstring pattern
,
598 HostContentSettingsMap
* host_content_settings_map
=
599 GetOriginalProfile()->GetHostContentSettingsMap();
600 host_content_settings_map
->SetContentSetting(
601 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env
, pattern
)),
602 ContentSettingsPattern::Wildcard(),
603 CONTENT_SETTINGS_TYPE_POPUPS
,
605 static_cast<ContentSetting
>(setting
));
608 static void GetPopupExceptions(JNIEnv
* env
, jobject obj
, jobject list
) {
609 HostContentSettingsMap
* host_content_settings_map
=
610 GetOriginalProfile()->GetHostContentSettingsMap();
611 ContentSettingsForOneType entries
;
612 host_content_settings_map
->GetSettingsForOneType(
613 CONTENT_SETTINGS_TYPE_POPUPS
, "", &entries
);
614 for (size_t i
= 0; i
< entries
.size(); ++i
) {
615 Java_PrefServiceBridge_insertPopupExceptionToList(
617 ConvertUTF8ToJavaString(
618 env
, entries
[i
].primary_pattern
.ToString()).obj(),
619 ConvertUTF8ToJavaString(
620 env
, GetStringForContentSettingsType(entries
[i
].setting
)).obj(),
621 ConvertUTF8ToJavaString(env
, entries
[i
].source
).obj());
625 static void SetSearchSuggestEnabled(JNIEnv
* env
, jobject obj
,
627 GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled
, enabled
);
630 static jstring
GetContextualSearchPreference(JNIEnv
* env
, jobject obj
) {
631 return ConvertUTF8ToJavaString(
632 env
, GetPrefService()->GetString(prefs::kContextualSearchEnabled
)).
636 static jboolean
GetContextualSearchPreferenceIsManaged(JNIEnv
* env
,
638 return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled
);
641 static void SetContextualSearchPreference(JNIEnv
* env
, jobject obj
,
643 GetPrefService()->SetString(prefs::kContextualSearchEnabled
,
644 ConvertJavaStringToUTF8(env
, pref
));
647 static void SetNetworkPredictionOptions(JNIEnv
* env
, jobject obj
, int option
) {
648 GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions
, option
);
651 static jboolean
NetworkPredictionEnabledHasUserSetting(JNIEnv
* env
,
653 return GetPrefService()->GetUserPrefValue(
654 prefs::kNetworkPredictionEnabled
) != NULL
;
657 static jboolean
NetworkPredictionOptionsHasUserSetting(JNIEnv
* env
,
659 return GetPrefService()->GetUserPrefValue(
660 prefs::kNetworkPredictionOptions
) != NULL
;
663 static jboolean
GetNetworkPredictionEnabledUserPrefValue(JNIEnv
* env
,
665 const base::Value
* network_prediction_enabled
=
666 GetPrefService()->GetUserPrefValue(prefs::kNetworkPredictionEnabled
);
667 DCHECK(network_prediction_enabled
);
669 DCHECK(network_prediction_enabled
->GetAsBoolean(&value
));
673 static void SetResolveNavigationErrorEnabled(JNIEnv
* env
, jobject obj
,
675 GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled
, enabled
);
678 static jboolean
GetFirstRunEulaAccepted(JNIEnv
* env
, jobject obj
) {
679 return g_browser_process
->local_state()->GetBoolean(prefs::kEulaAccepted
);
682 static void SetEulaAccepted(JNIEnv
* env
, jobject obj
) {
683 g_browser_process
->local_state()->SetBoolean(prefs::kEulaAccepted
, true);
686 static void ResetAcceptLanguages(JNIEnv
* env
,
688 jstring default_locale
) {
689 std::string
accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES
));
690 std::string
locale_string(ConvertJavaStringToUTF8(env
, default_locale
));
692 PrependToAcceptLanguagesIfNecessary(locale_string
, &accept_languages
);
693 GetPrefService()->SetString(prefs::kAcceptLanguages
, accept_languages
);
696 void PrependToAcceptLanguagesIfNecessary(std::string locale
,
697 std::string
* accept_languages
) {
698 if (locale
.size() != 5u || locale
[2] != '_') // not well-formed
701 std::string
language(locale
.substr(0, 2));
702 std::string
region(locale
.substr(3, 2));
704 // Java mostly follows ISO-639-1 and ICU, except for the following three.
705 // See documentation on java.util.Locale constructor for more.
706 if (language
== "iw") {
708 } else if (language
== "ji") {
710 } else if (language
== "in") {
714 std::string
language_region(language
+ "-" + region
);
716 if (accept_languages
->find(language_region
) == std::string::npos
) {
717 std::vector
<std::string
> parts
;
718 parts
.push_back(language_region
);
719 // If language is not in the accept languages list, also add language code.
720 if (accept_languages
->find(language
+ ",") == std::string::npos
&&
721 !std::equal(language
.rbegin(), language
.rend(),
722 accept_languages
->rbegin()))
723 parts
.push_back(language
);
724 parts
.push_back(*accept_languages
);
725 *accept_languages
= JoinString(parts
, ',');
729 // Sends all information about the different versions to Java.
730 // From browser_about_handler.cc
731 static jobject
GetAboutVersionStrings(JNIEnv
* env
, jobject obj
) {
732 chrome::VersionInfo version_info
;
733 std::string os_version
= version_info
.OSType();
734 os_version
+= " " + AndroidAboutAppInfo::GetOsInfo();
736 base::android::BuildInfo
* android_build_info
=
737 base::android::BuildInfo::GetInstance();
738 std::string
application(android_build_info
->package_label());
739 application
.append(" ");
740 application
.append(version_info
.Version());
742 // OK to release, returning to Java.
743 return Java_PrefServiceBridge_createAboutVersionStrings(
745 ConvertUTF8ToJavaString(env
, application
).obj(),
746 ConvertUTF8ToJavaString(env
, content::GetWebKitVersion()).obj(),
747 ConvertUTF8ToJavaString(
748 env
, AndroidAboutAppInfo::GetJavaScriptVersion()).obj(),
749 ConvertUTF8ToJavaString(env
, os_version
).obj()).Release();
752 static void GetProfilePath(JNIEnv
* env
, jobject obj
, jobject j_callback
) {
753 ScopedJavaGlobalRef
<jobject
>* callback
= new ScopedJavaGlobalRef
<jobject
>();
754 callback
->Reset(env
, j_callback
);
755 BrowserThread::PostTaskAndReplyWithResult(
756 BrowserThread::FILE, FROM_HERE
,
757 base::Bind(&GetProfilePathOnFileThread
, GetOriginalProfile()),
758 base::Bind(&OnGotProfilePath
, base::Owned(callback
)));
761 static jstring
GetSupervisedUserCustodianName(JNIEnv
* env
, jobject obj
) {
762 return ConvertUTF8ToJavaString(
763 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName
))
767 static jstring
GetSupervisedUserCustodianEmail(JNIEnv
* env
, jobject obj
) {
768 return ConvertUTF8ToJavaString(
769 env
, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail
))
773 static jstring
GetSupervisedUserCustodianProfileImageURL(JNIEnv
* env
,
775 return ConvertUTF8ToJavaString(
777 GetPrefService()->GetString(
778 prefs::kSupervisedUserCustodianProfileImageURL
)).Release();
781 static jstring
GetSupervisedUserSecondCustodianName(JNIEnv
* env
, jobject obj
) {
782 return ConvertUTF8ToJavaString(
784 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName
))
788 static jstring
GetSupervisedUserSecondCustodianEmail(JNIEnv
* env
, jobject obj
) {
789 return ConvertUTF8ToJavaString(
791 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail
))
795 static jstring
GetSupervisedUserSecondCustodianProfileImageURL(JNIEnv
* env
,
797 return ConvertUTF8ToJavaString(
799 GetPrefService()->GetString(
800 prefs::kSupervisedUserSecondCustodianProfileImageURL
)).Release();
803 bool RegisterPrefServiceBridge(JNIEnv
* env
) {
804 return RegisterNativesImpl(env
);