Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / android / preferences / pref_service_bridge.cc
blobfdd0d2bea7a1452fae75ca5e6c2ab520f43270b2
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"
7 #include <jni.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/pref_names.h"
28 #include "chrome/grit/locale_settings.h"
29 #include "components/content_settings/core/browser/host_content_settings_map.h"
30 #include "components/content_settings/core/common/content_settings.h"
31 #include "components/content_settings/core/common/content_settings_pattern.h"
32 #include "components/content_settings/core/common/pref_names.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/version_info/version_info.h"
37 #include "components/web_resource/web_resource_pref_names.h"
38 #include "content/public/browser/browser_thread.h"
39 #include "content/public/browser/user_metrics.h"
40 #include "jni/PrefServiceBridge_jni.h"
41 #include "ui/base/l10n/l10n_util.h"
43 using base::android::AttachCurrentThread;
44 using base::android::CheckException;
45 using base::android::ConvertJavaStringToUTF8;
46 using base::android::ConvertUTF8ToJavaString;
47 using base::android::ScopedJavaLocalRef;
48 using base::android::ScopedJavaGlobalRef;
49 using content::BrowserThread;
51 namespace {
53 enum NetworkPredictionOptions {
54 NETWORK_PREDICTION_ALWAYS,
55 NETWORK_PREDICTION_WIFI_ONLY,
56 NETWORK_PREDICTION_NEVER,
59 Profile* GetOriginalProfile() {
60 return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
63 bool GetBooleanForContentSetting(ContentSettingsType type) {
64 HostContentSettingsMap* content_settings =
65 GetOriginalProfile()->GetHostContentSettingsMap();
66 switch (content_settings->GetDefaultContentSetting(type, NULL)) {
67 case CONTENT_SETTING_BLOCK:
68 return false;
69 case CONTENT_SETTING_ALLOW:
70 return true;
71 case CONTENT_SETTING_ASK:
72 default:
73 return true;
77 std::string GetStringForContentSettingsType(
78 ContentSetting content_setting) {
79 switch (content_setting) {
80 case CONTENT_SETTING_BLOCK:
81 return "block";
82 case CONTENT_SETTING_ALLOW:
83 return "allow";
84 case CONTENT_SETTING_ASK:
85 return "ask";
86 case CONTENT_SETTING_SESSION_ONLY:
87 return "session";
88 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
89 return "detect";
90 case CONTENT_SETTING_NUM_SETTINGS:
91 return "num_settings";
92 case CONTENT_SETTING_DEFAULT:
93 default:
94 return "default";
98 bool IsContentSettingManaged(ContentSettingsType content_settings_type) {
99 std::string source;
100 HostContentSettingsMap* content_settings =
101 GetOriginalProfile()->GetHostContentSettingsMap();
102 content_settings->GetDefaultContentSetting(content_settings_type, &source);
103 HostContentSettingsMap::ProviderType provider =
104 content_settings->GetProviderTypeFromSource(source);
105 return provider == HostContentSettingsMap::POLICY_PROVIDER;
108 bool IsContentSettingManagedByCustodian(
109 ContentSettingsType content_settings_type) {
110 std::string source;
111 HostContentSettingsMap* content_settings =
112 GetOriginalProfile()->GetHostContentSettingsMap();
113 content_settings->GetDefaultContentSetting(content_settings_type, &source);
114 HostContentSettingsMap::ProviderType provider =
115 content_settings->GetProviderTypeFromSource(source);
116 return provider == HostContentSettingsMap::SUPERVISED_PROVIDER;
119 bool IsContentSettingUserModifiable(ContentSettingsType content_settings_type) {
120 std::string source;
121 HostContentSettingsMap* content_settings =
122 GetOriginalProfile()->GetHostContentSettingsMap();
123 content_settings->GetDefaultContentSetting(content_settings_type, &source);
124 HostContentSettingsMap::ProviderType provider =
125 content_settings->GetProviderTypeFromSource(source);
126 return provider >= HostContentSettingsMap::PREF_PROVIDER;
129 PrefService* GetPrefService() {
130 return GetOriginalProfile()->GetPrefs();
133 } // namespace
135 // ----------------------------------------------------------------------------
136 // Native JNI methods
137 // ----------------------------------------------------------------------------
139 static jboolean IsContentSettingManaged(JNIEnv* env,
140 const JavaParamRef<jobject>& obj,
141 int content_settings_type) {
142 return IsContentSettingManaged(
143 static_cast<ContentSettingsType>(content_settings_type));
146 static jboolean IsContentSettingEnabled(JNIEnv* env,
147 const JavaParamRef<jobject>& obj,
148 int content_settings_type) {
149 // Before we migrate functions over to this central function, we must verify
150 // that the functionality provided below is correct.
151 DCHECK(content_settings_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
152 content_settings_type == CONTENT_SETTINGS_TYPE_IMAGES ||
153 content_settings_type == CONTENT_SETTINGS_TYPE_POPUPS);
154 ContentSettingsType type =
155 static_cast<ContentSettingsType>(content_settings_type);
156 if (type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
157 type == CONTENT_SETTINGS_TYPE_POPUPS)
158 return GetBooleanForContentSetting(type);
160 HostContentSettingsMap* content_settings =
161 GetOriginalProfile()->GetHostContentSettingsMap();
162 return content_settings->GetDefaultContentSetting(
163 type, nullptr) == CONTENT_SETTING_ALLOW;
166 static void SetContentSettingEnabled(JNIEnv* env,
167 const JavaParamRef<jobject>& obj,
168 int content_settings_type,
169 jboolean allow) {
170 // Before we migrate functions over to this central function, we must verify
171 // that the new category supports ALLOW/BLOCK pairs and, if not, handle them.
172 DCHECK(content_settings_type == CONTENT_SETTINGS_TYPE_JAVASCRIPT ||
173 content_settings_type == CONTENT_SETTINGS_TYPE_IMAGES ||
174 content_settings_type == CONTENT_SETTINGS_TYPE_POPUPS);
175 HostContentSettingsMap* host_content_settings_map =
176 GetOriginalProfile()->GetHostContentSettingsMap();
177 host_content_settings_map->SetDefaultContentSetting(
178 static_cast<ContentSettingsType>(content_settings_type),
179 allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
182 static void SetContentSettingForPattern(JNIEnv* env,
183 const JavaParamRef<jobject>& obj,
184 int content_settings_type,
185 const JavaParamRef<jstring>& pattern,
186 int setting) {
187 HostContentSettingsMap* host_content_settings_map =
188 GetOriginalProfile()->GetHostContentSettingsMap();
189 host_content_settings_map->SetContentSetting(
190 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env, pattern)),
191 ContentSettingsPattern::Wildcard(),
192 static_cast<ContentSettingsType>(content_settings_type),
194 static_cast<ContentSetting>(setting));
197 static void GetContentSettingsExceptions(JNIEnv* env,
198 const JavaParamRef<jobject>& obj,
199 int content_settings_type,
200 const JavaParamRef<jobject>& list) {
201 HostContentSettingsMap* host_content_settings_map =
202 GetOriginalProfile()->GetHostContentSettingsMap();
203 ContentSettingsForOneType entries;
204 host_content_settings_map->GetSettingsForOneType(
205 static_cast<ContentSettingsType>(content_settings_type), "", &entries);
206 for (size_t i = 0; i < entries.size(); ++i) {
207 Java_PrefServiceBridge_addContentSettingExceptionToList(
208 env, list,
209 content_settings_type,
210 ConvertUTF8ToJavaString(
211 env, entries[i].primary_pattern.ToString()).obj(),
212 ConvertUTF8ToJavaString(
213 env, GetStringForContentSettingsType(entries[i].setting)).obj(),
214 ConvertUTF8ToJavaString(env, entries[i].source).obj());
218 static jboolean GetAcceptCookiesEnabled(JNIEnv* env,
219 const JavaParamRef<jobject>& obj) {
220 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_COOKIES);
223 static jboolean GetAcceptCookiesManaged(JNIEnv* env,
224 const JavaParamRef<jobject>& obj) {
225 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_COOKIES);
228 static jboolean GetBlockThirdPartyCookiesEnabled(
229 JNIEnv* env,
230 const JavaParamRef<jobject>& obj) {
231 return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies);
234 static jboolean GetBlockThirdPartyCookiesManaged(
235 JNIEnv* env,
236 const JavaParamRef<jobject>& obj) {
237 return GetPrefService()->IsManagedPreference(prefs::kBlockThirdPartyCookies);
240 static jboolean GetRememberPasswordsEnabled(JNIEnv* env,
241 const JavaParamRef<jobject>& obj) {
242 return GetPrefService()->GetBoolean(
243 password_manager::prefs::kPasswordManagerSavingEnabled);
246 static jboolean GetPasswordManagerAutoSigninEnabled(
247 JNIEnv* env,
248 const JavaParamRef<jobject>& obj) {
249 return GetPrefService()->GetBoolean(
250 password_manager::prefs::kPasswordManagerAutoSignin);
253 static jboolean GetRememberPasswordsManaged(JNIEnv* env,
254 const JavaParamRef<jobject>& obj) {
255 return GetPrefService()->IsManagedPreference(
256 password_manager::prefs::kPasswordManagerSavingEnabled);
259 static jboolean GetPasswordManagerAutoSigninManaged(
260 JNIEnv* env,
261 const JavaParamRef<jobject>& obj) {
262 return GetPrefService()->IsManagedPreference(
263 password_manager::prefs::kPasswordManagerAutoSignin);
266 static jboolean GetDoNotTrackEnabled(JNIEnv* env,
267 const JavaParamRef<jobject>& obj) {
268 return GetPrefService()->GetBoolean(prefs::kEnableDoNotTrack);
271 static jint GetNetworkPredictionOptions(JNIEnv* env,
272 const JavaParamRef<jobject>& obj) {
273 return GetPrefService()->GetInteger(prefs::kNetworkPredictionOptions);
276 static jboolean GetNetworkPredictionManaged(JNIEnv* env,
277 const JavaParamRef<jobject>& obj) {
278 return GetPrefService()->IsManagedPreference(
279 prefs::kNetworkPredictionOptions);
282 static jboolean GetPasswordEchoEnabled(JNIEnv* env,
283 const JavaParamRef<jobject>& obj) {
284 return GetPrefService()->GetBoolean(prefs::kWebKitPasswordEchoEnabled);
287 static jboolean GetPrintingEnabled(JNIEnv* env,
288 const JavaParamRef<jobject>& obj) {
289 return GetPrefService()->GetBoolean(prefs::kPrintingEnabled);
292 static jboolean GetPrintingManaged(JNIEnv* env,
293 const JavaParamRef<jobject>& obj) {
294 return GetPrefService()->IsManagedPreference(prefs::kPrintingEnabled);
297 static jboolean GetTranslateEnabled(JNIEnv* env,
298 const JavaParamRef<jobject>& obj) {
299 return GetPrefService()->GetBoolean(prefs::kEnableTranslate);
302 static jboolean GetTranslateManaged(JNIEnv* env,
303 const JavaParamRef<jobject>& obj) {
304 return GetPrefService()->IsManagedPreference(prefs::kEnableTranslate);
307 static jboolean GetAutoDetectEncodingEnabled(JNIEnv* env,
308 const JavaParamRef<jobject>& obj) {
309 return GetPrefService()->GetBoolean(prefs::kWebKitUsesUniversalDetector);
312 static jboolean GetSearchSuggestEnabled(JNIEnv* env,
313 const JavaParamRef<jobject>& obj) {
314 return GetPrefService()->GetBoolean(prefs::kSearchSuggestEnabled);
317 static jboolean GetSearchSuggestManaged(JNIEnv* env,
318 const JavaParamRef<jobject>& obj) {
319 return GetPrefService()->IsManagedPreference(prefs::kSearchSuggestEnabled);
322 static jboolean GetSafeBrowsingExtendedReportingEnabled(
323 JNIEnv* env,
324 const JavaParamRef<jobject>& obj) {
325 return GetPrefService()->GetBoolean(
326 prefs::kSafeBrowsingExtendedReportingEnabled);
329 static void SetSafeBrowsingExtendedReportingEnabled(
330 JNIEnv* env,
331 const JavaParamRef<jobject>& obj,
332 jboolean enabled) {
333 GetPrefService()->SetBoolean(prefs::kSafeBrowsingExtendedReportingEnabled,
334 enabled);
337 static jboolean GetSafeBrowsingExtendedReportingManaged(
338 JNIEnv* env,
339 const JavaParamRef<jobject>& obj) {
340 return GetPrefService()->IsManagedPreference(
341 prefs::kSafeBrowsingExtendedReportingEnabled);
344 static jboolean GetSafeBrowsingEnabled(JNIEnv* env,
345 const JavaParamRef<jobject>& obj) {
346 return GetPrefService()->GetBoolean(prefs::kSafeBrowsingEnabled);
349 static void SetSafeBrowsingEnabled(JNIEnv* env,
350 const JavaParamRef<jobject>& obj,
351 jboolean enabled) {
352 GetPrefService()->SetBoolean(prefs::kSafeBrowsingEnabled, enabled);
355 static jboolean GetSafeBrowsingManaged(JNIEnv* env,
356 const JavaParamRef<jobject>& obj) {
357 return GetPrefService()->IsManagedPreference(prefs::kSafeBrowsingEnabled);
360 static jboolean GetProtectedMediaIdentifierEnabled(
361 JNIEnv* env,
362 const JavaParamRef<jobject>& obj) {
363 return GetBooleanForContentSetting(
364 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER);
367 static jboolean GetPushNotificationsEnabled(JNIEnv* env,
368 const JavaParamRef<jobject>& obj) {
369 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
372 static jboolean GetAllowLocationEnabled(JNIEnv* env,
373 const JavaParamRef<jobject>& obj) {
374 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION);
377 static jboolean GetLocationAllowedByPolicy(JNIEnv* env,
378 const JavaParamRef<jobject>& obj) {
379 if (!IsContentSettingManaged(CONTENT_SETTINGS_TYPE_GEOLOCATION))
380 return false;
381 HostContentSettingsMap* content_settings =
382 GetOriginalProfile()->GetHostContentSettingsMap();
383 return content_settings->GetDefaultContentSetting(
384 CONTENT_SETTINGS_TYPE_GEOLOCATION, nullptr) ==
385 CONTENT_SETTING_ALLOW;
388 static jboolean GetAllowLocationUserModifiable(
389 JNIEnv* env,
390 const JavaParamRef<jobject>& obj) {
391 return IsContentSettingUserModifiable(CONTENT_SETTINGS_TYPE_GEOLOCATION);
394 static jboolean GetAllowLocationManagedByCustodian(
395 JNIEnv* env,
396 const JavaParamRef<jobject>& obj) {
397 return IsContentSettingManagedByCustodian(CONTENT_SETTINGS_TYPE_GEOLOCATION);
400 static jboolean GetResolveNavigationErrorEnabled(
401 JNIEnv* env,
402 const JavaParamRef<jobject>& obj) {
403 return GetPrefService()->GetBoolean(prefs::kAlternateErrorPagesEnabled);
406 static jboolean GetResolveNavigationErrorManaged(
407 JNIEnv* env,
408 const JavaParamRef<jobject>& obj) {
409 return GetPrefService()->IsManagedPreference(
410 prefs::kAlternateErrorPagesEnabled);
413 static jboolean GetCrashReportManaged(JNIEnv* env,
414 const JavaParamRef<jobject>& obj) {
415 return GetPrefService()->IsManagedPreference(
416 prefs::kCrashReportingEnabled);
419 static jboolean GetForceGoogleSafeSearch(JNIEnv* env,
420 const JavaParamRef<jobject>& obj) {
421 return GetPrefService()->GetBoolean(prefs::kForceGoogleSafeSearch);
424 static jint GetDefaultSupervisedUserFilteringBehavior(
425 JNIEnv* env,
426 const JavaParamRef<jobject>& obj) {
427 return GetPrefService()->GetInteger(
428 prefs::kDefaultSupervisedUserFilteringBehavior);
431 static jboolean GetIncognitoModeEnabled(JNIEnv* env,
432 const JavaParamRef<jobject>& obj) {
433 PrefService* prefs = GetPrefService();
434 IncognitoModePrefs::Availability incognito_pref =
435 IncognitoModePrefs::GetAvailability(prefs);
436 DCHECK(incognito_pref == IncognitoModePrefs::ENABLED ||
437 incognito_pref == IncognitoModePrefs::DISABLED) <<
438 "Unsupported incognito mode preference: " << incognito_pref;
439 return incognito_pref != IncognitoModePrefs::DISABLED;
442 static jboolean GetIncognitoModeManaged(JNIEnv* env,
443 const JavaParamRef<jobject>& obj) {
444 return GetPrefService()->IsManagedPreference(
445 prefs::kIncognitoModeAvailability);
448 static jboolean GetFullscreenManaged(JNIEnv* env,
449 const JavaParamRef<jobject>& obj) {
450 return IsContentSettingManaged(CONTENT_SETTINGS_TYPE_FULLSCREEN);
453 static jboolean GetFullscreenAllowed(JNIEnv* env,
454 const JavaParamRef<jobject>& obj) {
455 HostContentSettingsMap* content_settings =
456 GetOriginalProfile()->GetHostContentSettingsMap();
457 return content_settings->GetDefaultContentSetting(
458 CONTENT_SETTINGS_TYPE_FULLSCREEN, NULL) == CONTENT_SETTING_ALLOW;
461 static jboolean GetMetricsReportingEnabled(JNIEnv* env,
462 const JavaParamRef<jobject>& obj) {
463 PrefService* local_state = g_browser_process->local_state();
464 return local_state->GetBoolean(prefs::kMetricsReportingEnabled);
467 static void SetMetricsReportingEnabled(JNIEnv* env,
468 const JavaParamRef<jobject>& obj,
469 jboolean enabled) {
470 PrefService* local_state = g_browser_process->local_state();
471 local_state->SetBoolean(prefs::kMetricsReportingEnabled, enabled);
474 static jboolean HasSetMetricsReporting(JNIEnv* env,
475 const JavaParamRef<jobject>& obj) {
476 PrefService* local_state = g_browser_process->local_state();
477 return local_state->HasPrefPath(prefs::kMetricsReportingEnabled);
480 namespace {
482 // Redirects a BrowsingDataRemover completion callback back into Java.
483 class ClearBrowsingDataObserver : public BrowsingDataRemover::Observer {
484 public:
485 // |obj| is expected to be the object passed into ClearBrowsingData(); e.g. a
486 // ChromePreference.
487 ClearBrowsingDataObserver(JNIEnv* env, jobject obj)
488 : weak_chrome_native_preferences_(env, obj) {
491 void OnBrowsingDataRemoverDone() override {
492 // Just as a BrowsingDataRemover deletes itself when done, we delete ourself
493 // when done. No need to remove ourself as an observer given the lifetime
494 // of BrowsingDataRemover.
495 scoped_ptr<ClearBrowsingDataObserver> auto_delete(this);
497 JNIEnv* env = AttachCurrentThread();
498 if (weak_chrome_native_preferences_.get(env).is_null())
499 return;
501 Java_PrefServiceBridge_browsingDataCleared(
502 env, weak_chrome_native_preferences_.get(env).obj());
505 private:
506 JavaObjectWeakGlobalRef weak_chrome_native_preferences_;
508 } // namespace
510 static void ClearBrowsingData(JNIEnv* env,
511 const JavaParamRef<jobject>& obj,
512 jboolean history,
513 jboolean cache,
514 jboolean cookies_and_site_data,
515 jboolean passwords,
516 jboolean form_data) {
517 // BrowsingDataRemover deletes itself.
518 BrowsingDataRemover* browsing_data_remover =
519 BrowsingDataRemover::CreateForPeriod(
520 GetOriginalProfile(),
521 static_cast<BrowsingDataRemover::TimePeriod>(
522 BrowsingDataRemover::EVERYTHING));
523 browsing_data_remover->AddObserver(new ClearBrowsingDataObserver(env, obj));
525 int remove_mask = 0;
526 if (history)
527 remove_mask |= BrowsingDataRemover::REMOVE_HISTORY;
528 if (cache)
529 remove_mask |= BrowsingDataRemover::REMOVE_CACHE;
530 if (cookies_and_site_data) {
531 remove_mask |= BrowsingDataRemover::REMOVE_COOKIES;
532 remove_mask |= BrowsingDataRemover::REMOVE_SITE_DATA;
534 if (passwords)
535 remove_mask |= BrowsingDataRemover::REMOVE_PASSWORDS;
536 if (form_data)
537 remove_mask |= BrowsingDataRemover::REMOVE_FORM_DATA;
538 browsing_data_remover->Remove(remove_mask,
539 BrowsingDataHelper::UNPROTECTED_WEB);
542 static jboolean CanDeleteBrowsingHistory(JNIEnv* env,
543 const JavaParamRef<jobject>& obj) {
544 return GetPrefService()->GetBoolean(prefs::kAllowDeletingBrowserHistory);
547 static void SetAllowCookiesEnabled(JNIEnv* env,
548 const JavaParamRef<jobject>& obj,
549 jboolean allow) {
550 HostContentSettingsMap* host_content_settings_map =
551 GetOriginalProfile()->GetHostContentSettingsMap();
552 host_content_settings_map->SetDefaultContentSetting(
553 CONTENT_SETTINGS_TYPE_COOKIES,
554 allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
557 static void SetBlockThirdPartyCookiesEnabled(JNIEnv* env,
558 const JavaParamRef<jobject>& obj,
559 jboolean enabled) {
560 GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies, enabled);
563 static void SetRememberPasswordsEnabled(JNIEnv* env,
564 const JavaParamRef<jobject>& obj,
565 jboolean allow) {
566 GetPrefService()->SetBoolean(
567 password_manager::prefs::kPasswordManagerSavingEnabled, allow);
570 static void SetPasswordManagerAutoSigninEnabled(
571 JNIEnv* env,
572 const JavaParamRef<jobject>& obj,
573 jboolean enabled) {
574 GetPrefService()->SetBoolean(
575 password_manager::prefs::kPasswordManagerAutoSignin, enabled);
578 static void SetProtectedMediaIdentifierEnabled(JNIEnv* env,
579 const JavaParamRef<jobject>& obj,
580 jboolean is_enabled) {
581 HostContentSettingsMap* host_content_settings_map =
582 GetOriginalProfile()->GetHostContentSettingsMap();
583 host_content_settings_map->SetDefaultContentSetting(
584 CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
585 is_enabled ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
588 static void SetAllowLocationEnabled(JNIEnv* env,
589 const JavaParamRef<jobject>& obj,
590 jboolean is_enabled) {
591 HostContentSettingsMap* host_content_settings_map =
592 GetOriginalProfile()->GetHostContentSettingsMap();
593 host_content_settings_map->SetDefaultContentSetting(
594 CONTENT_SETTINGS_TYPE_GEOLOCATION,
595 is_enabled ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
598 static void SetCameraEnabled(JNIEnv* env,
599 const JavaParamRef<jobject>& obj,
600 jboolean allow) {
601 HostContentSettingsMap* host_content_settings_map =
602 GetOriginalProfile()->GetHostContentSettingsMap();
603 host_content_settings_map->SetDefaultContentSetting(
604 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA,
605 allow ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
608 static void SetMicEnabled(JNIEnv* env,
609 const JavaParamRef<jobject>& obj,
610 jboolean allow) {
611 HostContentSettingsMap* host_content_settings_map =
612 GetOriginalProfile()->GetHostContentSettingsMap();
613 host_content_settings_map->SetDefaultContentSetting(
614 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC,
615 allow ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
618 static void SetFullscreenAllowed(JNIEnv* env,
619 const JavaParamRef<jobject>& obj,
620 jboolean allow) {
621 HostContentSettingsMap* host_content_settings_map =
622 GetOriginalProfile()->GetHostContentSettingsMap();
623 host_content_settings_map->SetDefaultContentSetting(
624 CONTENT_SETTINGS_TYPE_FULLSCREEN,
625 allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_ASK);
628 static void SetPushNotificationsEnabled(JNIEnv* env,
629 const JavaParamRef<jobject>& obj,
630 jboolean allow) {
631 HostContentSettingsMap* host_content_settings_map =
632 GetOriginalProfile()->GetHostContentSettingsMap();
633 host_content_settings_map->SetDefaultContentSetting(
634 CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
635 allow ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
638 static void SetCrashReporting(JNIEnv* env,
639 const JavaParamRef<jobject>& obj,
640 jboolean reporting) {
641 PrefService* local_state = g_browser_process->local_state();
642 local_state->SetBoolean(prefs::kCrashReportingEnabled, reporting);
645 static jboolean CanPredictNetworkActions(JNIEnv* env,
646 const JavaParamRef<jobject>& obj) {
647 return chrome_browser_net::CanPrefetchAndPrerenderUI(GetPrefService());
650 static void SetDoNotTrackEnabled(JNIEnv* env,
651 const JavaParamRef<jobject>& obj,
652 jboolean allow) {
653 GetPrefService()->SetBoolean(prefs::kEnableDoNotTrack, allow);
656 static ScopedJavaLocalRef<jstring> GetSyncLastAccountName(
657 JNIEnv* env,
658 const JavaParamRef<jobject>& obj) {
659 return ConvertUTF8ToJavaString(
660 env, GetPrefService()->GetString(prefs::kGoogleServicesLastUsername));
663 static void SetTranslateEnabled(JNIEnv* env,
664 const JavaParamRef<jobject>& obj,
665 jboolean enabled) {
666 GetPrefService()->SetBoolean(prefs::kEnableTranslate, enabled);
669 static void SetAutoDetectEncodingEnabled(JNIEnv* env,
670 const JavaParamRef<jobject>& obj,
671 jboolean enabled) {
672 content::RecordAction(base::UserMetricsAction("AutoDetectChange"));
673 GetPrefService()->SetBoolean(prefs::kWebKitUsesUniversalDetector, enabled);
676 static void ResetTranslateDefaults(JNIEnv* env,
677 const JavaParamRef<jobject>& obj) {
678 scoped_ptr<translate::TranslatePrefs> translate_prefs =
679 ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
680 translate_prefs->ResetToDefaults();
683 static void MigrateJavascriptPreference(JNIEnv* env,
684 const JavaParamRef<jobject>& obj) {
685 const PrefService::Preference* javascript_pref =
686 GetPrefService()->FindPreference(prefs::kWebKitJavascriptEnabled);
687 DCHECK(javascript_pref);
689 if (!javascript_pref->HasUserSetting())
690 return;
692 bool javascript_enabled = false;
693 bool retval = javascript_pref->GetValue()->GetAsBoolean(&javascript_enabled);
694 DCHECK(retval);
695 SetContentSettingEnabled(env, obj,
696 CONTENT_SETTINGS_TYPE_JAVASCRIPT, javascript_enabled);
697 GetPrefService()->ClearPref(prefs::kWebKitJavascriptEnabled);
700 static void MigrateLocationPreference(JNIEnv* env,
701 const JavaParamRef<jobject>& obj) {
702 const PrefService::Preference* pref =
703 GetPrefService()->FindPreference(prefs::kGeolocationEnabled);
704 if (!pref || !pref->HasUserSetting())
705 return;
706 bool location_enabled = false;
707 bool retval = pref->GetValue()->GetAsBoolean(&location_enabled);
708 DCHECK(retval);
709 // Do a restrictive migration. GetAllowLocationEnabled could be
710 // non-usermodifiable and we don't want to migrate that.
711 if (!location_enabled)
712 SetAllowLocationEnabled(env, obj, false);
713 GetPrefService()->ClearPref(prefs::kGeolocationEnabled);
716 static void MigrateProtectedMediaPreference(JNIEnv* env,
717 const JavaParamRef<jobject>& obj) {
718 const PrefService::Preference* pref =
719 GetPrefService()->FindPreference(prefs::kProtectedMediaIdentifierEnabled);
720 if (!pref || !pref->HasUserSetting())
721 return;
722 bool pmi_enabled = false;
723 bool retval = pref->GetValue()->GetAsBoolean(&pmi_enabled);
724 DCHECK(retval);
725 // Do a restrictive migration if values disagree.
726 if (!pmi_enabled)
727 SetProtectedMediaIdentifierEnabled(env, obj, false);
728 GetPrefService()->ClearPref(prefs::kProtectedMediaIdentifierEnabled);
731 static void SetPasswordEchoEnabled(JNIEnv* env,
732 const JavaParamRef<jobject>& obj,
733 jboolean passwordEchoEnabled) {
734 GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled,
735 passwordEchoEnabled);
738 static jboolean GetCameraEnabled(JNIEnv* env,
739 const JavaParamRef<jobject>& obj) {
740 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
743 static jboolean GetCameraUserModifiable(JNIEnv* env,
744 const JavaParamRef<jobject>& obj) {
745 return IsContentSettingUserModifiable(
746 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
749 static jboolean GetCameraManagedByCustodian(JNIEnv* env,
750 const JavaParamRef<jobject>& obj) {
751 return IsContentSettingManagedByCustodian(
752 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA);
755 static jboolean GetMicEnabled(JNIEnv* env, const JavaParamRef<jobject>& obj) {
756 return GetBooleanForContentSetting(CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
759 static jboolean GetMicUserModifiable(JNIEnv* env,
760 const JavaParamRef<jobject>& obj) {
761 return IsContentSettingUserModifiable(
762 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
765 static jboolean GetMicManagedByCustodian(JNIEnv* env,
766 const JavaParamRef<jobject>& obj) {
767 return IsContentSettingManagedByCustodian(
768 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC);
771 static void SetJavaScriptAllowed(JNIEnv* env,
772 const JavaParamRef<jobject>& obj,
773 const JavaParamRef<jstring>& pattern,
774 int setting) {
775 HostContentSettingsMap* host_content_settings_map =
776 GetOriginalProfile()->GetHostContentSettingsMap();
777 host_content_settings_map->SetContentSetting(
778 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env, pattern)),
779 ContentSettingsPattern::Wildcard(),
780 CONTENT_SETTINGS_TYPE_JAVASCRIPT,
782 static_cast<ContentSetting>(setting));
785 static void SetPopupException(JNIEnv* env,
786 const JavaParamRef<jobject>& obj,
787 const JavaParamRef<jstring>& pattern,
788 int setting) {
789 HostContentSettingsMap* host_content_settings_map =
790 GetOriginalProfile()->GetHostContentSettingsMap();
791 host_content_settings_map->SetContentSetting(
792 ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env, pattern)),
793 ContentSettingsPattern::Wildcard(),
794 CONTENT_SETTINGS_TYPE_POPUPS,
796 static_cast<ContentSetting>(setting));
799 static void SetSearchSuggestEnabled(JNIEnv* env,
800 const JavaParamRef<jobject>& obj,
801 jboolean enabled) {
802 GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled, enabled);
805 static ScopedJavaLocalRef<jstring> GetContextualSearchPreference(
806 JNIEnv* env,
807 const JavaParamRef<jobject>& obj) {
808 return ConvertUTF8ToJavaString(
809 env, GetPrefService()->GetString(prefs::kContextualSearchEnabled));
812 static jboolean GetContextualSearchPreferenceIsManaged(
813 JNIEnv* env,
814 const JavaParamRef<jobject>& obj) {
815 return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled);
818 static void SetContextualSearchPreference(JNIEnv* env,
819 const JavaParamRef<jobject>& obj,
820 const JavaParamRef<jstring>& pref) {
821 GetPrefService()->SetString(prefs::kContextualSearchEnabled,
822 ConvertJavaStringToUTF8(env, pref));
825 static void SetNetworkPredictionOptions(JNIEnv* env,
826 const JavaParamRef<jobject>& obj,
827 int option) {
828 GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions, option);
831 static void SetResolveNavigationErrorEnabled(JNIEnv* env,
832 const JavaParamRef<jobject>& obj,
833 jboolean enabled) {
834 GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled, enabled);
837 static jboolean GetFirstRunEulaAccepted(JNIEnv* env,
838 const JavaParamRef<jobject>& obj) {
839 return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
842 static void SetEulaAccepted(JNIEnv* env, const JavaParamRef<jobject>& obj) {
843 g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true);
846 static void ResetAcceptLanguages(JNIEnv* env,
847 const JavaParamRef<jobject>& obj,
848 const JavaParamRef<jstring>& default_locale) {
849 std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES));
850 std::string locale_string(ConvertJavaStringToUTF8(env, default_locale));
852 PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(locale_string,
853 &accept_languages);
854 GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages);
857 // Sends all information about the different versions to Java.
858 // From browser_about_handler.cc
859 static ScopedJavaLocalRef<jobject> GetAboutVersionStrings(
860 JNIEnv* env,
861 const JavaParamRef<jobject>& obj) {
862 std::string os_version = version_info::GetOSType();
863 os_version += " " + AndroidAboutAppInfo::GetOsInfo();
865 base::android::BuildInfo* android_build_info =
866 base::android::BuildInfo::GetInstance();
867 std::string application(android_build_info->package_label());
868 application.append(" ");
869 application.append(version_info::GetVersionNumber());
871 return Java_PrefServiceBridge_createAboutVersionStrings(
872 env, ConvertUTF8ToJavaString(env, application).obj(),
873 ConvertUTF8ToJavaString(env, os_version).obj());
876 static ScopedJavaLocalRef<jstring> GetSupervisedUserCustodianName(
877 JNIEnv* env,
878 const JavaParamRef<jobject>& obj) {
879 return ConvertUTF8ToJavaString(
880 env, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName));
883 static ScopedJavaLocalRef<jstring> GetSupervisedUserCustodianEmail(
884 JNIEnv* env,
885 const JavaParamRef<jobject>& obj) {
886 return ConvertUTF8ToJavaString(
887 env, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail));
890 static ScopedJavaLocalRef<jstring> GetSupervisedUserCustodianProfileImageURL(
891 JNIEnv* env,
892 const JavaParamRef<jobject>& obj) {
893 return ConvertUTF8ToJavaString(
894 env, GetPrefService()->GetString(
895 prefs::kSupervisedUserCustodianProfileImageURL));
898 static ScopedJavaLocalRef<jstring> GetSupervisedUserSecondCustodianName(
899 JNIEnv* env,
900 const JavaParamRef<jobject>& obj) {
901 return ConvertUTF8ToJavaString(
902 env,
903 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName));
906 static ScopedJavaLocalRef<jstring> GetSupervisedUserSecondCustodianEmail(
907 JNIEnv* env,
908 const JavaParamRef<jobject>& obj) {
909 return ConvertUTF8ToJavaString(
910 env,
911 GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail));
914 static ScopedJavaLocalRef<jstring>
915 GetSupervisedUserSecondCustodianProfileImageURL(
916 JNIEnv* env,
917 const JavaParamRef<jobject>& obj) {
918 return ConvertUTF8ToJavaString(
919 env, GetPrefService()->GetString(
920 prefs::kSupervisedUserSecondCustodianProfileImageURL));
923 // static
924 bool PrefServiceBridge::RegisterPrefServiceBridge(JNIEnv* env) {
925 return RegisterNativesImpl(env);
928 // static
929 void PrefServiceBridge::PrependToAcceptLanguagesIfNecessary(
930 const std::string& locale,
931 std::string* accept_languages) {
932 if (locale.size() != 5u || locale[2] != '_') // not well-formed
933 return;
935 std::string language(locale.substr(0, 2));
936 std::string region(locale.substr(3, 2));
938 // Java mostly follows ISO-639-1 and ICU, except for the following three.
939 // See documentation on java.util.Locale constructor for more.
940 if (language == "iw") {
941 language = "he";
942 } else if (language == "ji") {
943 language = "yi";
944 } else if (language == "in") {
945 language = "id";
948 std::string language_region(language + "-" + region);
950 if (accept_languages->find(language_region) == std::string::npos) {
951 std::vector<std::string> parts;
952 parts.push_back(language_region);
953 // If language is not in the accept languages list, also add language code.
954 if (accept_languages->find(language + ",") == std::string::npos &&
955 !std::equal(language.rbegin(), language.rend(),
956 accept_languages->rbegin())) {
957 parts.push_back(language);
959 parts.push_back(*accept_languages);
960 *accept_languages = base::JoinString(parts, ",");
964 // static
965 std::string PrefServiceBridge::GetAndroidPermissionForContentSetting(
966 ContentSettingsType content_type) {
967 JNIEnv* env = AttachCurrentThread();
968 base::android::ScopedJavaLocalRef<jstring> android_permission =
969 Java_PrefServiceBridge_getAndroidPermissionForContentSetting(
970 env, content_type);
971 if (android_permission.is_null())
972 return std::string();
974 return ConvertJavaStringToUTF8(android_permission);