1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
|
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/preferences/pref_service_bridge.h"
#include <jni.h>
#include "base/android/build_info.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/android/jni_weak_ref.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_ptr.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_util.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "chrome/browser/browsing_data/browsing_data_remover.h"
#include "chrome/browser/net/prediction_options.h"
#include "chrome/browser/prefs/incognito_mode_prefs.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/translate/chrome_translate_client.h"
#include "chrome/browser/ui/android/android_about_app_info.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/locale_settings.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_pattern.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/translate/core/browser/translate_prefs.h"
#include "components/translate/core/common/translate_pref_names.h"
#include "components/web_resource/web_resource_pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/common/user_agent.h"
#include "jni/PrefServiceBridge_jni.h"
#include "ui/base/l10n/l10n_util.h"
using base::android::AttachCurrentThread;
using base::android::CheckException;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaLocalRef;
using base::android::ScopedJavaGlobalRef;
using content::BrowserThread;
namespace {
enum NetworkPredictionOptions {
NETWORK_PREDICTION_ALWAYS,
NETWORK_PREDICTION_WIFI_ONLY,
NETWORK_PREDICTION_NEVER,
};
Profile* GetOriginalProfile() {
return ProfileManager::GetActiveUserProfile()->GetOriginalProfile();
}
bool GetBooleanForContentSetting(HostContentSettingsMap* content_settings,
ContentSettingsType type) {
switch (content_settings->GetDefaultContentSetting(type, NULL)) {
case CONTENT_SETTING_BLOCK:
return false;
case CONTENT_SETTING_ALLOW:
return true;
case CONTENT_SETTING_ASK:
default:
return true;
}
}
std::string GetStringForContentSettingsType(
ContentSetting content_setting) {
switch (content_setting) {
case CONTENT_SETTING_BLOCK:
return "block";
case CONTENT_SETTING_ALLOW:
return "allow";
case CONTENT_SETTING_ASK:
return "ask";
case CONTENT_SETTING_SESSION_ONLY:
return "session";
case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
return "detect";
case CONTENT_SETTING_NUM_SETTINGS:
return "num_settings";
case CONTENT_SETTING_DEFAULT:
default:
return "default";
}
}
bool IsContentSettingManaged(HostContentSettingsMap* content_settings,
ContentSettingsType content_settings_type) {
std::string source;
content_settings->GetDefaultContentSetting(content_settings_type, &source);
HostContentSettingsMap::ProviderType provider =
content_settings->GetProviderTypeFromSource(source);
return provider == HostContentSettingsMap::POLICY_PROVIDER;
}
bool IsContentSettingUserModifiable(HostContentSettingsMap* content_settings,
ContentSettingsType content_settings_type) {
std::string source;
content_settings->GetDefaultContentSetting(content_settings_type, &source);
HostContentSettingsMap::ProviderType provider =
content_settings->GetProviderTypeFromSource(source);
return provider >= HostContentSettingsMap::PREF_PROVIDER;
}
void OnGotProfilePath(ScopedJavaGlobalRef<jobject>* callback,
std::string path) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> j_path = ConvertUTF8ToJavaString(env, path);
Java_PrefServiceBridge_onGotProfilePath(env, j_path.obj(), callback->obj());
}
std::string GetProfilePathOnFileThread(Profile* profile) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
if (!profile)
return std::string();
base::FilePath profile_path = profile->GetPath();
return base::MakeAbsoluteFilePath(profile_path).value();
}
PrefService* GetPrefService() {
return GetOriginalProfile()->GetPrefs();
}
static void EnsureConsistentGeolocationPreferences(Profile* profile) {
// On Android, we use the kGeolocationEnabled flag to control geolocation on a
// global basis, rather than the default geolocation host content setting,
// which is only used if no previously stored more specific host exception
// cannot be found.
//
// On Android, there is currently no UI to change this default setting, so it
// needs to default to ASK. Additionally, for users that have previously set
// the default to BLOCK, we set the preference to disable geolocation
// globally.
ContentSetting defaultSetting = profile->GetHostContentSettingsMap()->
GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_GEOLOCATION, NULL);
if (defaultSetting == CONTENT_SETTING_ASK)
return;
profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_GEOLOCATION, CONTENT_SETTING_ASK);
if (defaultSetting == CONTENT_SETTING_BLOCK) {
profile->GetPrefs()->SetBoolean(prefs::kGeolocationEnabled, false);
}
}
static void EnsureConsistentProtectedMediaIdentifierPreferences(
Profile* profile) {
// We use the kProtectedMediaIdentifierEnabled flag to control protected media
// identifier on a global basis.
//
// On Android, there is currently no UI to change this default setting, so it
// needs to default to ASK. Additionally, for users that have previously set
// the default to BLOCK, we set the preference to disable protected media
// identifier globally.
ContentSetting defaultSetting =
profile->GetHostContentSettingsMap()->
GetDefaultContentSetting(CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER,
NULL);
if (defaultSetting == CONTENT_SETTING_ASK)
return;
profile->GetHostContentSettingsMap()->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER, CONTENT_SETTING_ASK);
if (defaultSetting == CONTENT_SETTING_BLOCK) {
profile->GetPrefs()->SetBoolean(prefs::kProtectedMediaIdentifierEnabled,
false);
}
}
} // namespace
// ----------------------------------------------------------------------------
// Native JNI methods
// ----------------------------------------------------------------------------
static jboolean GetAcceptCookiesEnabled(JNIEnv* env, jobject obj) {
return GetBooleanForContentSetting(
GetOriginalProfile()->GetHostContentSettingsMap(),
CONTENT_SETTINGS_TYPE_COOKIES);
}
static jboolean GetAcceptCookiesManaged(JNIEnv* env, jobject obj) {
return IsContentSettingManaged(
GetOriginalProfile()->GetHostContentSettingsMap(),
CONTENT_SETTINGS_TYPE_COOKIES);
}
static jboolean GetBlockThirdPartyCookiesEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kBlockThirdPartyCookies);
}
static jboolean GetBlockThirdPartyCookiesManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kBlockThirdPartyCookies);
}
static jboolean GetRememberPasswordsEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(
password_manager::prefs::kPasswordManagerSavingEnabled);
}
static jboolean GetRememberPasswordsManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(
password_manager::prefs::kPasswordManagerSavingEnabled);
}
static jboolean GetDoNotTrackEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kEnableDoNotTrack);
}
static jint GetNetworkPredictionOptions(JNIEnv* env, jobject obj) {
return GetPrefService()->GetInteger(prefs::kNetworkPredictionOptions);
}
static jboolean GetNetworkPredictionManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(
prefs::kNetworkPredictionOptions);
}
static jboolean GetPasswordEchoEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kWebKitPasswordEchoEnabled);
}
static jboolean GetPrintingEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kPrintingEnabled);
}
static jboolean GetPrintingManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kPrintingEnabled);
}
static jboolean GetTranslateEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kEnableTranslate);
}
static jboolean GetTranslateManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kEnableTranslate);
}
static jboolean GetSearchSuggestEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kSearchSuggestEnabled);
}
static jboolean GetSearchSuggestManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kSearchSuggestEnabled);
}
static jboolean GetProtectedMediaIdentifierEnabled(JNIEnv* env, jobject obj) {
Profile* profile = GetOriginalProfile();
EnsureConsistentProtectedMediaIdentifierPreferences(profile);
HostContentSettingsMap* content_settings =
profile->GetHostContentSettingsMap();
return GetBooleanForContentSetting(
content_settings,
CONTENT_SETTINGS_TYPE_PROTECTED_MEDIA_IDENTIFIER) &&
GetPrefService()->GetBoolean(prefs::kProtectedMediaIdentifierEnabled);
}
static jboolean GetPushNotificationsEnabled(JNIEnv* env, jobject obj) {
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
return GetBooleanForContentSetting(content_settings,
CONTENT_SETTINGS_TYPE_NOTIFICATIONS);
}
static jboolean GetAllowLocationEnabled(JNIEnv* env, jobject obj) {
Profile* profile = GetOriginalProfile();
EnsureConsistentGeolocationPreferences(profile);
HostContentSettingsMap* content_settings =
profile->GetHostContentSettingsMap();
return GetBooleanForContentSetting(content_settings,
CONTENT_SETTINGS_TYPE_GEOLOCATION) &&
GetPrefService()->GetBoolean(prefs::kGeolocationEnabled);
}
static jboolean GetAllowLocationUserModifiable(JNIEnv* env, jobject obj) {
return IsContentSettingUserModifiable(
GetOriginalProfile()->GetHostContentSettingsMap(),
CONTENT_SETTINGS_TYPE_GEOLOCATION) &&
GetPrefService()->IsUserModifiablePreference(
prefs::kGeolocationEnabled);
}
static jboolean GetAllowLocationManagedByCustodian(JNIEnv* env, jobject obj) {
return GetPrefService()->IsPreferenceManagedByCustodian(
prefs::kGeolocationEnabled);
}
static jboolean GetResolveNavigationErrorEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kAlternateErrorPagesEnabled);
}
static jboolean GetResolveNavigationErrorManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(
prefs::kAlternateErrorPagesEnabled);
}
static jboolean GetCrashReportManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(
prefs::kCrashReportingEnabled);
}
static jboolean GetForceSafeSearch(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kForceSafeSearch);
}
static jint GetDefaultSupervisedUserFilteringBehavior(JNIEnv* env,
jobject obj) {
return GetPrefService()->GetInteger(
prefs::kDefaultSupervisedUserFilteringBehavior);
}
static jboolean GetIncognitoModeEnabled(JNIEnv* env, jobject obj) {
PrefService* prefs = GetPrefService();
IncognitoModePrefs::Availability incognito_pref =
IncognitoModePrefs::GetAvailability(prefs);
DCHECK(incognito_pref == IncognitoModePrefs::ENABLED ||
incognito_pref == IncognitoModePrefs::DISABLED) <<
"Unsupported incognito mode preference: " << incognito_pref;
return incognito_pref != IncognitoModePrefs::DISABLED;
}
static jboolean GetIncognitoModeManaged(JNIEnv* env, jobject obj) {
return GetPrefService()->IsManagedPreference(
prefs::kIncognitoModeAvailability);
}
namespace {
// Redirects a BrowsingDataRemover completion callback back into Java.
class ClearBrowsingDataObserver : public BrowsingDataRemover::Observer {
public:
// |obj| is expected to be the object passed into ClearBrowsingData(); e.g. a
// ChromePreference.
ClearBrowsingDataObserver(JNIEnv* env, jobject obj)
: weak_chrome_native_preferences_(env, obj) {
}
virtual void OnBrowsingDataRemoverDone() override {
// Just as a BrowsingDataRemover deletes itself when done, we delete ourself
// when done. No need to remove ourself as an observer given the lifetime
// of BrowsingDataRemover.
scoped_ptr<ClearBrowsingDataObserver> auto_delete(this);
JNIEnv* env = AttachCurrentThread();
if (weak_chrome_native_preferences_.get(env).is_null())
return;
Java_PrefServiceBridge_browsingDataCleared(
env, weak_chrome_native_preferences_.get(env).obj());
}
private:
JavaObjectWeakGlobalRef weak_chrome_native_preferences_;
};
} // namespace
static void ClearBrowsingData(JNIEnv* env, jobject obj, jboolean history,
jboolean cache, jboolean cookies_and_site_data, jboolean passwords,
jboolean form_data) {
// BrowsingDataRemover deletes itself.
BrowsingDataRemover* browsing_data_remover =
BrowsingDataRemover::CreateForPeriod(
GetOriginalProfile(),
static_cast<BrowsingDataRemover::TimePeriod>(
BrowsingDataRemover::EVERYTHING));
browsing_data_remover->AddObserver(new ClearBrowsingDataObserver(env, obj));
int remove_mask = 0;
if (history)
remove_mask |= BrowsingDataRemover::REMOVE_HISTORY;
if (cache)
remove_mask |= BrowsingDataRemover::REMOVE_CACHE;
if (cookies_and_site_data) {
remove_mask |= BrowsingDataRemover::REMOVE_COOKIES;
remove_mask |= BrowsingDataRemover::REMOVE_SITE_DATA;
}
if (passwords)
remove_mask |= BrowsingDataRemover::REMOVE_PASSWORDS;
if (form_data)
remove_mask |= BrowsingDataRemover::REMOVE_FORM_DATA;
browsing_data_remover->Remove(remove_mask,
BrowsingDataHelper::UNPROTECTED_WEB);
}
static void SetAllowCookiesEnabled(JNIEnv* env, jobject obj, jboolean allow) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_COOKIES,
allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
}
static void SetBlockThirdPartyCookiesEnabled(JNIEnv* env, jobject obj,
jboolean enabled) {
GetPrefService()->SetBoolean(prefs::kBlockThirdPartyCookies, enabled);
}
static void SetRememberPasswordsEnabled(JNIEnv* env, jobject obj,
jboolean allow) {
GetPrefService()->SetBoolean(
password_manager::prefs::kPasswordManagerSavingEnabled, allow);
}
static void SetProtectedMediaIdentifierEnabled(JNIEnv* env,
jobject obj,
jboolean is_enabled) {
GetPrefService()->SetBoolean(prefs::kProtectedMediaIdentifierEnabled,
is_enabled);
}
static void SetAllowLocationEnabled(JNIEnv* env, jobject obj, jboolean allow) {
GetPrefService()->SetBoolean(prefs::kGeolocationEnabled, allow);
}
static void SetCameraMicEnabled(JNIEnv* env, jobject obj, jboolean allow) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_MEDIASTREAM,
allow ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
}
static void SetPushNotificationsEnabled(JNIEnv* env,
jobject obj,
jboolean allow) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_NOTIFICATIONS,
allow ? CONTENT_SETTING_ASK : CONTENT_SETTING_BLOCK);
}
static void SetCrashReporting(JNIEnv* env, jobject obj, jboolean reporting) {
PrefService* local_state = g_browser_process->local_state();
local_state->SetBoolean(prefs::kCrashReportingEnabled, reporting);
}
static jboolean CanPredictNetworkActions(JNIEnv* env, jobject obj) {
return chrome_browser_net::CanPrefetchAndPrerenderUI(GetPrefService());
}
static void SetDoNotTrackEnabled(JNIEnv* env, jobject obj, jboolean allow) {
GetPrefService()->SetBoolean(prefs::kEnableDoNotTrack, allow);
}
static jstring GetSyncLastAccountName(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env, GetPrefService()->GetString(prefs::kGoogleServicesLastUsername))
.Release();
}
static void SetTranslateEnabled(JNIEnv* env, jobject obj, jboolean enabled) {
GetPrefService()->SetBoolean(prefs::kEnableTranslate, enabled);
}
static void ResetTranslateDefaults(JNIEnv* env, jobject obj) {
scoped_ptr<translate::TranslatePrefs> translate_prefs =
ChromeTranslateClient::CreateTranslatePrefs(GetPrefService());
translate_prefs->ResetToDefaults();
}
static jboolean GetJavaScriptManaged(JNIEnv* env, jobject obj) {
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
return IsContentSettingManaged(
content_settings, CONTENT_SETTINGS_TYPE_JAVASCRIPT);
}
static void SetJavaScriptEnabled(JNIEnv* env, jobject obj, jboolean enabled) {
GetPrefService()->SetBoolean(prefs::kWebKitJavascriptEnabled, enabled);
}
static jboolean GetJavaScriptEnabled(JNIEnv* env, jobject obj) {
// The user pref for Javascript is stored in kWebKitJavascriptEnabled for
// historical reasons, but the content setting is where a possibly managed
// value will be enforced.
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
jboolean javascript_enabled = GetBooleanForContentSetting(
content_settings, CONTENT_SETTINGS_TYPE_JAVASCRIPT);
if (!GetJavaScriptManaged(env, obj)) {
javascript_enabled &= GetPrefService()->GetBoolean(
prefs::kWebKitJavascriptEnabled);
}
return javascript_enabled;
}
static void SetPasswordEchoEnabled(JNIEnv* env,
jobject obj,
jboolean passwordEchoEnabled) {
GetPrefService()->SetBoolean(prefs::kWebKitPasswordEchoEnabled,
passwordEchoEnabled);
}
static jboolean GetAllowPopupsEnabled(JNIEnv* env, jobject obj) {
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
return GetBooleanForContentSetting(content_settings,
CONTENT_SETTINGS_TYPE_POPUPS);
}
static jboolean GetAllowPopupsManaged(JNIEnv* env, jobject obj) {
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
return IsContentSettingManaged(content_settings,
CONTENT_SETTINGS_TYPE_POPUPS);
}
static void SetAllowPopupsEnabled(JNIEnv* env, jobject obj, jboolean allow) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetDefaultContentSetting(
CONTENT_SETTINGS_TYPE_POPUPS,
allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
}
static jboolean GetCameraMicEnabled(JNIEnv* env, jobject obj) {
HostContentSettingsMap* content_settings =
GetOriginalProfile()->GetHostContentSettingsMap();
PrefService* prefs = GetPrefService();
return GetBooleanForContentSetting(content_settings,
CONTENT_SETTINGS_TYPE_MEDIASTREAM) &&
prefs->GetBoolean(prefs::kAudioCaptureAllowed) &&
prefs->GetBoolean(prefs::kVideoCaptureAllowed);
}
static jboolean GetCameraMicUserModifiable(JNIEnv* env, jobject obj) {
PrefService* prefs = GetPrefService();
return IsContentSettingUserModifiable(
GetOriginalProfile()->GetHostContentSettingsMap(),
CONTENT_SETTINGS_TYPE_MEDIASTREAM) &&
prefs->IsUserModifiablePreference(prefs::kAudioCaptureAllowed) &&
prefs->IsUserModifiablePreference(prefs::kVideoCaptureAllowed);
}
static jboolean GetCameraMicManagedByCustodian(JNIEnv* env, jobject obj) {
PrefService* prefs = GetPrefService();
if (prefs->IsPreferenceManagedByCustodian(prefs::kVideoCaptureAllowed))
return true;
if (prefs->IsPreferenceManagedByCustodian(prefs::kAudioCaptureAllowed))
return true;
return false;
}
static jboolean GetAutologinEnabled(JNIEnv* env, jobject obj) {
return GetPrefService()->GetBoolean(prefs::kAutologinEnabled);
}
static void SetAutologinEnabled(JNIEnv* env, jobject obj,
jboolean autologinEnabled) {
GetPrefService()->SetBoolean(prefs::kAutologinEnabled, autologinEnabled);
}
static void SetPopupException(JNIEnv* env, jobject obj, jstring pattern,
jboolean allow) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetContentSetting(
ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env, pattern)),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_POPUPS,
"",
allow ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK);
}
static void RemovePopupException(JNIEnv* env, jobject obj, jstring pattern) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
host_content_settings_map->SetContentSetting(
ContentSettingsPattern::FromString(ConvertJavaStringToUTF8(env, pattern)),
ContentSettingsPattern::Wildcard(),
CONTENT_SETTINGS_TYPE_POPUPS,
"",
CONTENT_SETTING_DEFAULT);
}
static void GetPopupExceptions(JNIEnv* env, jobject obj, jobject list) {
HostContentSettingsMap* host_content_settings_map =
GetOriginalProfile()->GetHostContentSettingsMap();
ContentSettingsForOneType entries;
host_content_settings_map->GetSettingsForOneType(
CONTENT_SETTINGS_TYPE_POPUPS, "", &entries);
for (size_t i = 0; i < entries.size(); ++i) {
Java_PrefServiceBridge_insertPopupExceptionToList(
env, list,
ConvertUTF8ToJavaString(
env, entries[i].primary_pattern.ToString()).obj(),
ConvertUTF8ToJavaString(
env, GetStringForContentSettingsType(entries[i].setting)).obj(),
ConvertUTF8ToJavaString(env, entries[i].source).obj());
}
}
static void SetSearchSuggestEnabled(JNIEnv* env, jobject obj,
jboolean enabled) {
GetPrefService()->SetBoolean(prefs::kSearchSuggestEnabled, enabled);
}
static jstring GetContextualSearchPreference(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env, GetPrefService()->GetString(prefs::kContextualSearchEnabled)).
Release();
}
static jboolean GetContextualSearchPreferenceIsManaged(JNIEnv* env,
jobject obj) {
return GetPrefService()->IsManagedPreference(prefs::kContextualSearchEnabled);
}
static void SetContextualSearchPreference(JNIEnv* env, jobject obj,
jstring pref) {
GetPrefService()->SetString(prefs::kContextualSearchEnabled,
ConvertJavaStringToUTF8(env, pref));
}
static void SetNetworkPredictionOptions(JNIEnv* env, jobject obj, int option) {
GetPrefService()->SetInteger(prefs::kNetworkPredictionOptions, option);
}
static jboolean NetworkPredictionEnabledHasUserSetting(JNIEnv* env,
jobject obj) {
return GetPrefService()->GetUserPrefValue(
prefs::kNetworkPredictionEnabled) != NULL;
}
static jboolean NetworkPredictionOptionsHasUserSetting(JNIEnv* env,
jobject obj) {
return GetPrefService()->GetUserPrefValue(
prefs::kNetworkPredictionOptions) != NULL;
}
static jboolean GetNetworkPredictionEnabledUserPrefValue(JNIEnv* env,
jobject obj) {
const base::Value* network_prediction_enabled =
GetPrefService()->GetUserPrefValue(prefs::kNetworkPredictionEnabled);
DCHECK(network_prediction_enabled);
bool value = false;
DCHECK(network_prediction_enabled->GetAsBoolean(&value));
return value;
}
static void SetResolveNavigationErrorEnabled(JNIEnv* env, jobject obj,
jboolean enabled) {
GetPrefService()->SetBoolean(prefs::kAlternateErrorPagesEnabled, enabled);
}
static jboolean GetFirstRunEulaAccepted(JNIEnv* env, jobject obj) {
return g_browser_process->local_state()->GetBoolean(prefs::kEulaAccepted);
}
static void SetEulaAccepted(JNIEnv* env, jobject obj) {
g_browser_process->local_state()->SetBoolean(prefs::kEulaAccepted, true);
}
static void ResetAcceptLanguages(JNIEnv* env,
jobject obj,
jstring default_locale) {
std::string accept_languages(l10n_util::GetStringUTF8(IDS_ACCEPT_LANGUAGES));
std::string locale_string(ConvertJavaStringToUTF8(env, default_locale));
PrependToAcceptLanguagesIfNecessary(locale_string, &accept_languages);
GetPrefService()->SetString(prefs::kAcceptLanguages, accept_languages);
}
void PrependToAcceptLanguagesIfNecessary(std::string locale,
std::string* accept_languages) {
if (locale.size() != 5u || locale[2] != '_') // not well-formed
return;
std::string language(locale.substr(0, 2));
std::string region(locale.substr(3, 2));
// Java mostly follows ISO-639-1 and ICU, except for the following three.
// See documentation on java.util.Locale constructor for more.
if (language == "iw") {
language = "he";
} else if (language == "ji") {
language = "yi";
} else if (language == "in") {
language = "id";
}
std::string language_region(language + "-" + region);
if (accept_languages->find(language_region) == std::string::npos) {
std::vector<std::string> parts;
parts.push_back(language_region);
// If language is not in the accept languages list, also add language code.
if (accept_languages->find(language + ",") == std::string::npos &&
!std::equal(language.rbegin(), language.rend(),
accept_languages->rbegin()))
parts.push_back(language);
parts.push_back(*accept_languages);
*accept_languages = JoinString(parts, ',');
}
}
// Sends all information about the different versions to Java.
// From browser_about_handler.cc
static jobject GetAboutVersionStrings(JNIEnv* env, jobject obj) {
chrome::VersionInfo version_info;
std::string os_version = version_info.OSType();
os_version += " " + AndroidAboutAppInfo::GetOsInfo();
base::android::BuildInfo* android_build_info =
base::android::BuildInfo::GetInstance();
std::string application(android_build_info->package_label());
application.append(" ");
application.append(version_info.Version());
// OK to release, returning to Java.
return Java_PrefServiceBridge_createAboutVersionStrings(
env,
ConvertUTF8ToJavaString(env, application).obj(),
ConvertUTF8ToJavaString(env, content::GetWebKitVersion()).obj(),
ConvertUTF8ToJavaString(
env, AndroidAboutAppInfo::GetJavaScriptVersion()).obj(),
ConvertUTF8ToJavaString(env, os_version).obj()).Release();
}
static void GetProfilePath(JNIEnv* env, jobject obj, jobject j_callback) {
ScopedJavaGlobalRef<jobject>* callback = new ScopedJavaGlobalRef<jobject>();
callback->Reset(env, j_callback);
BrowserThread::PostTaskAndReplyWithResult(
BrowserThread::FILE, FROM_HERE,
base::Bind(&GetProfilePathOnFileThread, GetOriginalProfile()),
base::Bind(&OnGotProfilePath, base::Owned(callback)));
}
static jstring GetSupervisedUserCustodianName(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env, GetPrefService()->GetString(prefs::kSupervisedUserCustodianName))
.Release();
}
static jstring GetSupervisedUserCustodianEmail(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env, GetPrefService()->GetString(prefs::kSupervisedUserCustodianEmail))
.Release();
}
static jstring GetSupervisedUserCustodianProfileImageURL(JNIEnv* env,
jobject obj) {
return ConvertUTF8ToJavaString(
env,
GetPrefService()->GetString(
prefs::kSupervisedUserCustodianProfileImageURL)).Release();
}
static jstring GetSupervisedUserSecondCustodianName(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env,
GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianName))
.Release();
}
static jstring GetSupervisedUserSecondCustodianEmail(JNIEnv* env, jobject obj) {
return ConvertUTF8ToJavaString(
env,
GetPrefService()->GetString(prefs::kSupervisedUserSecondCustodianEmail))
.Release();
}
static jstring GetSupervisedUserSecondCustodianProfileImageURL(JNIEnv* env,
jobject obj) {
return ConvertUTF8ToJavaString(
env,
GetPrefService()->GetString(
prefs::kSupervisedUserSecondCustodianProfileImageURL)).Release();
}
bool RegisterPrefServiceBridge(JNIEnv* env) {
return RegisterNativesImpl(env);
}
|