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 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/sync/service/sync_prefs.h"
#include <utility>
#include "base/auto_reset.h"
#include "base/base64.h"
#include "base/check_op.h"
#include "base/containers/contains.h"
#include "base/containers/to_vector.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/notreached.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "build/build_config.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/pref_value_map.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "components/saved_tab_groups/public/pref_names.h"
#include "components/signin/public/base/gaia_id_hash.h"
#include "components/signin/public/base/signin_buildflags.h"
#include "components/signin/public/base/signin_pref_names.h"
#include "components/signin/public/base/signin_prefs.h"
#include "components/signin/public/base/signin_switches.h"
#include "components/sync/base/account_pref_utils.h"
#include "components/sync/base/features.h"
#include "components/sync/base/passphrase_enums.h"
#include "components/sync/base/pref_names.h"
#include "components/sync/base/user_selectable_type.h"
#include "components/sync/protocol/nigori_specifics.pb.h"
#include "components/sync/service/glue/sync_transport_data_prefs.h"
#include "components/sync/service/sync_feature_status_for_migrations_recorder.h"
#include "extensions/buildflags/buildflags.h"
#include "google_apis/gaia/gaia_id.h"
namespace syncer {
namespace {
// Historic artifact for payment methods, migrated since 2023/07 to
// prefs::internal::kSyncPayments to make the name consistent with other
// user-selectable types.
constexpr char kObsoleteAutofillWalletImportEnabled[] =
"autofill.wallet_import_enabled";
// Boolean representing whether kObsoleteAutofillWalletImportEnabled was
// migrated to the new pref representing a UserSelectableType. Should be cleaned
// up together with the migration code (after 2024-07).
constexpr char kObsoleteAutofillWalletImportEnabledMigrated[] =
"sync.autofill_wallet_import_enabled_migrated";
// State of the migration done by MaybeMigrateCustomPassphrasePref().
constexpr char kSyncEncryptionBootstrapTokenPerAccountMigrationDone[] =
"sync.encryption_bootstrap_token_per_account_migration_done";
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// Pref to record if the one-off MaybeMigrateAutofillToPerAccountPref()
// migration ran.
constexpr char kAutofillPerAccountPrefMigrationDone[] =
"sync.passwords_per_account_pref_migration_done";
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
constexpr int kNotMigrated = 0;
constexpr int kMigratedPart1ButNot2 = 1;
constexpr int kMigratedPart2AndFullyDone = 2;
// Encodes a protobuf instance of type
// sync_pb::TrustedVaultAutoUpgradeExperimentGroup in a way that can be safely
// stored in prefs, i.e. using base64 encoding.
std::string EncodeTrustedVaultAutoUpgradeExperimentGroupToString(
const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& group) {
return base::Base64Encode(group.SerializeAsString());
}
// Does the opposite of EncodeTrustedVaultAutoUpgradeExperimentGroupToString(),
// i.e. transforms from a string representation to a protobuf instance.
sync_pb::TrustedVaultAutoUpgradeExperimentGroup
DecodeTrustedVaultAutoUpgradeExperimentGroupFromString(
const std::string& encoded_group) {
sync_pb::TrustedVaultAutoUpgradeExperimentGroup proto;
std::string serialized_proto;
if (!base::Base64Decode(encoded_group, &serialized_proto)) {
return proto;
}
proto.ParseFromString(serialized_proto);
return proto;
}
} // namespace
SyncPrefObserver::~SyncPrefObserver() = default;
SyncPrefs::SyncPrefs(PrefService* pref_service)
: pref_service_(pref_service),
local_sync_enabled_(
pref_service_->GetBoolean(prefs::kEnableLocalSyncBackend)) {
DCHECK(pref_service);
// Watch the preference that indicates sync is managed so we can take
// appropriate action.
pref_sync_managed_.Init(
prefs::internal::kSyncManaged, pref_service_,
base::BindRepeating(&SyncPrefs::OnSyncManagedPrefChanged,
base::Unretained(this)));
// Observe changes to all of the prefs that may influence the selected types.
pref_change_registrar_.Init(pref_service_);
// The individual data type prefs are used for syncing users, as well as for
// enterprise policy.
for (UserSelectableType type : UserSelectableTypeSet::All()) {
pref_change_registrar_.Add(
GetPrefNameForType(type),
base::BindRepeating(&SyncPrefs::OnSelectedTypesPrefChanged,
base::Unretained(this)));
}
// The "sync everything" pref is used for syncing users.
pref_change_registrar_.Add(
prefs::internal::kSyncKeepEverythingSynced,
base::BindRepeating(&SyncPrefs::OnSelectedTypesPrefChanged,
base::Unretained(this)));
// The per-account data types dictionary pref is used for signed-in
// non-syncing users.
pref_change_registrar_.Add(
prefs::internal::kSelectedTypesPerAccount,
base::BindRepeating(&SyncPrefs::OnSelectedTypesPrefChanged,
base::Unretained(this)));
}
SyncPrefs::~SyncPrefs() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}
// static
void SyncPrefs::RegisterProfilePrefs(PrefRegistrySimple* registry) {
// Actual user-controlled preferences.
registry->RegisterBooleanPref(prefs::internal::kSyncKeepEverythingSynced,
true);
registry->RegisterDictionaryPref(prefs::internal::kSelectedTypesPerAccount);
for (UserSelectableType type : UserSelectableTypeSet::All()) {
RegisterTypeSelectedPref(registry, type);
}
#if BUILDFLAG(IS_CHROMEOS)
registry->RegisterBooleanPref(prefs::internal::kSyncDisabledViaDashboard,
false);
registry->RegisterBooleanPref(prefs::internal::kSyncAllOsTypes, true);
registry->RegisterBooleanPref(prefs::internal::kSyncOsApps, false);
registry->RegisterBooleanPref(prefs::internal::kSyncOsPreferences, false);
registry->RegisterBooleanPref(prefs::internal::kSyncWifiConfigurations,
false);
#else // BUILDFLAG(IS_CHROMEOS)
registry->RegisterBooleanPref(
prefs::internal::kSyncInitialSyncFeatureSetupComplete, false);
#endif // BUILDFLAG(IS_CHROMEOS)
registry->RegisterBooleanPref(
prefs::internal::kFirstSyncCompletedInFullSyncMode, false);
registry->RegisterBooleanPref(kObsoleteAutofillWalletImportEnabledMigrated,
false);
registry->RegisterIntegerPref(prefs::internal::kSyncToSigninMigrationState,
kNotMigrated);
registry->RegisterBooleanPref(
prefs::internal::kMigrateReadingListFromLocalToAccount, false);
// The passphrase type, determined upon the first engine initialization.
registry->RegisterIntegerPref(
prefs::internal::kSyncCachedPassphraseType,
sync_pb::NigoriSpecifics_PassphraseType_UNKNOWN);
// The user's TrustedVaultAutoUpgradeExperimentGroup, determined the first
// time the engine is successfully initialized.
registry->RegisterStringPref(
prefs::internal::kSyncCachedTrustedVaultAutoUpgradeExperimentGroup, "");
// The encryption bootstrap token represents a user-entered passphrase.
registry->RegisterStringPref(prefs::internal::kSyncEncryptionBootstrapToken,
std::string());
// Cached notion of whether or not a persistent auth error exists.
registry->RegisterBooleanPref(
prefs::internal::kSyncCachedPersistentAuthErrorForMetrics, false);
// The encryption bootstrap token represents a user-entered passphrase per
// account.
registry->RegisterDictionaryPref(
prefs::internal::kSyncEncryptionBootstrapTokenPerAccount);
// For migration only, tracks if the EncryptionBootstrapToken is migrated.
registry->RegisterBooleanPref(
kSyncEncryptionBootstrapTokenPerAccountMigrationDone, false);
registry->RegisterBooleanPref(prefs::internal::kSyncManaged, false);
registry->RegisterIntegerPref(
prefs::internal::kSyncPassphrasePromptMutedProductVersion, 0);
registry->RegisterBooleanPref(prefs::kEnableLocalSyncBackend, false);
registry->RegisterFilePathPref(prefs::kLocalSyncBackendDir, base::FilePath());
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
registry->RegisterBooleanPref(kAutofillPerAccountPrefMigrationDone, false);
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
registry->RegisterTimePref(
prefs::internal::kFirstTimeTriedToMigrateSyncFeaturePausedToSignin,
base::Time());
SyncFeatureStatusForMigrationsRecorder::RegisterProfilePrefs(registry);
// Obsolete prefs (registered for migrations only).
registry->RegisterBooleanPref(kObsoleteAutofillWalletImportEnabled, true);
}
void SyncPrefs::AddObserver(SyncPrefObserver* sync_pref_observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
sync_pref_observers_.AddObserver(sync_pref_observer);
}
void SyncPrefs::RemoveObserver(SyncPrefObserver* sync_pref_observer) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
sync_pref_observers_.RemoveObserver(sync_pref_observer);
}
bool SyncPrefs::IsInitialSyncFeatureSetupComplete() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
#if BUILDFLAG(IS_CHROMEOS)
return true;
#else // BUILDFLAG(IS_CHROMEOS)
return pref_service_->GetBoolean(
prefs::internal::kSyncInitialSyncFeatureSetupComplete);
#endif // BUILDFLAG(IS_CHROMEOS)
}
bool SyncPrefs::IsExplicitBrowserSignin() const {
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS) || BUILDFLAG(IS_CHROMEOS)
// On mobile and ChromeOS all sign-ins are considered explicit.
return true;
#else
// On desktop `prefs::kExplicitBrowserSignin` determines whether the sign-in
// is explicit or implicit.
return pref_service_->GetBoolean(::prefs::kExplicitBrowserSignin);
#endif
}
#if !BUILDFLAG(IS_CHROMEOS)
void SyncPrefs::SetInitialSyncFeatureSetupComplete() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(
prefs::internal::kSyncInitialSyncFeatureSetupComplete, true);
}
void SyncPrefs::ClearInitialSyncFeatureSetupComplete() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(
prefs::internal::kSyncInitialSyncFeatureSetupComplete);
}
#endif // !BUILDFLAG(IS_CHROMEOS)
bool SyncPrefs::IsFirstSyncCompletedInFullSyncMode() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(
prefs::internal::kFirstSyncCompletedInFullSyncMode);
}
void SyncPrefs::SetFirstSyncCompletedInFullSyncMode() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(prefs::internal::kFirstSyncCompletedInFullSyncMode,
true);
}
void SyncPrefs::ClearFirstSyncCompletedInFullSyncMode() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(prefs::internal::kFirstSyncCompletedInFullSyncMode);
}
bool SyncPrefs::HasKeepEverythingSynced() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncKeepEverythingSynced);
}
UserSelectableTypeSet SyncPrefs::GetSelectedTypesForAccount(
const GaiaId& gaia_id) const {
const signin::GaiaIdHash gaia_id_hash =
signin::GaiaIdHash::FromGaiaId(gaia_id);
UserSelectableTypeSet selected_types;
for (UserSelectableType type : UserSelectableTypeSet::All()) {
if (!IsTypeSupportedInTransportMode(type)) {
continue;
}
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
bool type_enabled = false;
if (IsTypeManagedByPolicy(type) || IsTypeManagedByCustodian(type)) {
type_enabled = pref_service_->GetBoolean(pref_name);
} else {
const base::Value* pref_value = GetAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
gaia_id_hash, pref_name);
if (pref_value && pref_value->is_bool()) {
type_enabled = pref_value->GetBool();
} else {
type_enabled = IsTypeSelectedByDefaultInTransportMode(type, gaia_id);
}
}
if (type_enabled) {
selected_types.Put(type);
}
}
if (!password_sync_allowed_) {
selected_types.Remove(UserSelectableType::kPasswords);
}
return selected_types;
}
UserSelectableTypeSet SyncPrefs::GetSelectedTypesForSyncingUser() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UserSelectableTypeSet selected_types;
for (UserSelectableType type : UserSelectableTypeSet::All()) {
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
if (pref_service_->GetBoolean(pref_name) ||
(!IsTypeManagedByPolicy(type) && !IsTypeManagedByCustodian(type) &&
pref_service_->GetBoolean(
prefs::internal::kSyncKeepEverythingSynced))) {
// In full-sync mode, the "sync everything" bit is honored. If it's
// true, all types are considered selected, irrespective of their
// individual prefs.
selected_types.Put(type);
}
}
if (!password_sync_allowed_) {
selected_types.Remove(UserSelectableType::kPasswords);
}
return selected_types;
}
bool SyncPrefs::IsTypeManagedByPolicy(UserSelectableType type) const {
const char* pref_name = GetPrefNameForType(type);
CHECK(pref_name);
return pref_service_->IsManagedPreference(pref_name);
}
bool SyncPrefs::IsTypeManagedByCustodian(UserSelectableType type) const {
const char* pref_name = GetPrefNameForType(type);
CHECK(pref_name);
return pref_service_->IsPreferenceManagedByCustodian(pref_name);
}
bool SyncPrefs::DoesTypeHaveDefaultValueForAccount(
const UserSelectableType type,
const GaiaId& gaia_id) {
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
const base::Value* value = GetAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), pref_name);
return !value;
}
bool SyncPrefs::IsTypeDisabledByUserForAccount(const UserSelectableType type,
const GaiaId& gaia_id) {
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
const base::Value* value = GetAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), pref_name);
if (value && value->is_bool()) {
return !value->GetBool();
}
return false;
}
void SyncPrefs::SetSelectedTypesForSyncingUser(
bool keep_everything_synced,
UserSelectableTypeSet registered_types,
UserSelectableTypeSet selected_types) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
{
// Prevent OnSelectedTypesPrefChanged() from notifying observers about each
// of the type changes individually.
base::AutoReset<bool> batch_update(&batch_updating_selected_types_, true);
pref_service_->SetBoolean(prefs::internal::kSyncKeepEverythingSynced,
keep_everything_synced);
for (UserSelectableType type : registered_types) {
const char* pref_name = GetPrefNameForType(type);
pref_service_->SetBoolean(pref_name, selected_types.Has(type));
}
}
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSelectedTypesPrefChange();
}
}
void SyncPrefs::SetSelectedTypeForAccount(UserSelectableType type,
bool is_type_on,
const GaiaId& gaia_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
CHECK(!gaia_id.empty());
SetAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), GetPrefNameForType(type),
base::Value(is_type_on));
}
void SyncPrefs::ResetSelectedTypeForAccount(UserSelectableType type,
const GaiaId& gaia_id) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
RemoveAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), GetPrefNameForType(type));
}
void SyncPrefs::KeepAccountSettingsPrefsOnlyForUsers(
const std::vector<GaiaId>& available_gaia_ids) {
std::vector<signin::GaiaIdHash> hashes =
base::ToVector(available_gaia_ids, &signin::GaiaIdHash::FromGaiaId);
KeepAccountKeyedPrefValuesOnlyForUsers(
pref_service_, prefs::internal::kSelectedTypesPerAccount, hashes);
KeepAccountKeyedPrefValuesOnlyForUsers(
pref_service_, prefs::internal::kSyncEncryptionBootstrapTokenPerAccount,
hashes);
// TODO(crbug.com/368409110): This is not the right place for clearing
// transport-data-related prefs - ideally there'd be an observer API for
// "accounts on this device".
SyncTransportDataPrefs::KeepAccountSettingsPrefsOnlyForUsers(pref_service_,
hashes);
// TODO(crbug.com/368409110): This is *absolutely* not the right place for
// clearing not-sync-related prefs. Move this elsewhere once signin code
// provides an observer API for "accounts on this device".
tab_groups::prefs::KeepAccountSettingsPrefsOnlyForUsers(pref_service_,
hashes);
}
#if BUILDFLAG(IS_CHROMEOS)
bool SyncPrefs::IsSyncFeatureDisabledViaDashboard() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncDisabledViaDashboard);
}
void SyncPrefs::SetSyncFeatureDisabledViaDashboard() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(prefs::internal::kSyncDisabledViaDashboard, true);
}
void SyncPrefs::ClearSyncFeatureDisabledViaDashboard() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(prefs::internal::kSyncDisabledViaDashboard);
}
bool SyncPrefs::IsSyncAllOsTypesEnabled() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncAllOsTypes);
}
UserSelectableOsTypeSet SyncPrefs::GetSelectedOsTypes() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
UserSelectableOsTypeSet selected_types;
const bool sync_all_os_types = IsSyncAllOsTypesEnabled();
for (UserSelectableOsType type : UserSelectableOsTypeSet::All()) {
const char* pref_name = GetPrefNameForOsType(type);
DCHECK(pref_name);
// If the type is managed, `sync_all_os_types` is ignored for this type.
if (pref_service_->GetBoolean(pref_name) ||
(sync_all_os_types && !IsOsTypeManagedByPolicy(type))) {
selected_types.Put(type);
}
}
return selected_types;
}
bool SyncPrefs::IsOsTypeManagedByPolicy(UserSelectableOsType type) const {
const char* pref_name = GetPrefNameForOsType(type);
CHECK(pref_name);
return pref_service_->IsManagedPreference(pref_name);
}
void SyncPrefs::SetSelectedOsTypes(bool sync_all_os_types,
UserSelectableOsTypeSet registered_types,
UserSelectableOsTypeSet selected_types) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
{
// Prevent OnSelectedTypesPrefChanged() from notifying about each of the
// type changes individually.
base::AutoReset<bool> batch_update(&batch_updating_selected_types_, true);
pref_service_->SetBoolean(prefs::internal::kSyncAllOsTypes,
sync_all_os_types);
for (UserSelectableOsType type : registered_types) {
const char* pref_name = GetPrefNameForOsType(type);
DCHECK(pref_name);
pref_service_->SetBoolean(pref_name, selected_types.Has(type));
}
}
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSelectedTypesPrefChange();
}
}
// static
const char* SyncPrefs::GetPrefNameForOsTypeForTesting(
UserSelectableOsType type) {
return GetPrefNameForOsType(type);
}
// static
const char* SyncPrefs::GetPrefNameForOsType(UserSelectableOsType type) {
switch (type) {
case UserSelectableOsType::kOsApps:
return prefs::internal::kSyncOsApps;
case UserSelectableOsType::kOsPreferences:
return prefs::internal::kSyncOsPreferences;
case UserSelectableOsType::kOsWifiConfigurations:
return prefs::internal::kSyncWifiConfigurations;
}
NOTREACHED();
}
// static
void SyncPrefs::SetOsTypeDisabledByPolicy(PrefValueMap* policy_prefs,
UserSelectableOsType type) {
const char* pref_name = syncer::SyncPrefs::GetPrefNameForOsType(type);
CHECK(pref_name);
policy_prefs->SetValue(pref_name, base::Value(false));
}
#endif // BUILDFLAG(IS_CHROMEOS)
bool SyncPrefs::IsSyncClientDisabledByPolicy() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(prefs::internal::kSyncManaged);
}
std::optional<PassphraseType> SyncPrefs::GetCachedPassphraseType() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return ProtoPassphraseInt32ToEnum(
pref_service_->GetInteger(prefs::internal::kSyncCachedPassphraseType));
}
void SyncPrefs::SetCachedPassphraseType(PassphraseType passphrase_type) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetInteger(prefs::internal::kSyncCachedPassphraseType,
EnumPassphraseTypeToProto(passphrase_type));
}
void SyncPrefs::ClearCachedPassphraseType() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(prefs::internal::kSyncCachedPassphraseType);
}
bool SyncPrefs::HasCachedPersistentAuthErrorForMetrics() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return pref_service_->GetBoolean(
prefs::internal::kSyncCachedPersistentAuthErrorForMetrics);
}
void SyncPrefs::SetHasCachedPersistentAuthErrorForMetrics(
bool has_persistent_auth_error) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetBoolean(
prefs::internal::kSyncCachedPersistentAuthErrorForMetrics,
has_persistent_auth_error);
}
void SyncPrefs::ClearCachedPersistentAuthErrorForMetrics() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(
prefs::internal::kSyncCachedPersistentAuthErrorForMetrics);
}
std::optional<sync_pb::TrustedVaultAutoUpgradeExperimentGroup>
SyncPrefs::GetCachedTrustedVaultAutoUpgradeExperimentGroup() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
const std::string& encoded_group = pref_service_->GetString(
prefs::internal::kSyncCachedTrustedVaultAutoUpgradeExperimentGroup);
if (encoded_group.empty()) {
return std::nullopt;
}
return DecodeTrustedVaultAutoUpgradeExperimentGroupFromString(encoded_group);
}
void SyncPrefs::SetCachedTrustedVaultAutoUpgradeExperimentGroup(
const sync_pb::TrustedVaultAutoUpgradeExperimentGroup& group) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->SetString(
prefs::internal::kSyncCachedTrustedVaultAutoUpgradeExperimentGroup,
EncodeTrustedVaultAutoUpgradeExperimentGroupToString(group));
}
void SyncPrefs::ClearCachedTrustedVaultAutoUpgradeExperimentGroup() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(
prefs::internal::kSyncCachedTrustedVaultAutoUpgradeExperimentGroup);
}
void SyncPrefs::ClearAllEncryptionBootstrapTokens() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
pref_service_->ClearPref(prefs::internal::kSyncEncryptionBootstrapToken);
if (IsInitialSyncFeatureSetupComplete()) {
// When Sync-the-feature gets turned off, the user's encryption bootstrap
// token should be cleared. However, at this point it's hard to determine
// the right account, since the user is already signed out. For simplicity,
// just clear all existing bootstrap tokens (in practice, there will almost
// always be at most one anyway).
KeepAccountKeyedPrefValuesOnlyForUsers(
pref_service_, prefs::internal::kSyncEncryptionBootstrapTokenPerAccount,
{});
}
}
std::string SyncPrefs::GetEncryptionBootstrapTokenForAccount(
const GaiaId& gaia_id) const {
if (gaia_id.empty()) {
return std::string();
}
const std::string* account_passphrase =
pref_service_
->GetDict(prefs::internal::kSyncEncryptionBootstrapTokenPerAccount)
.FindString(signin::GaiaIdHash::FromGaiaId(gaia_id).ToBase64());
return account_passphrase ? *account_passphrase : std::string();
}
void SyncPrefs::SetEncryptionBootstrapTokenForAccount(const std::string& token,
const GaiaId& gaia_id) {
CHECK(!gaia_id.empty());
SetAccountKeyedPrefValue(
pref_service_, prefs::internal::kSyncEncryptionBootstrapTokenPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), base::Value(token));
}
void SyncPrefs::ClearEncryptionBootstrapTokenForAccount(const GaiaId& gaia_id) {
ClearAccountKeyedPrefValue(
pref_service_, prefs::internal::kSyncEncryptionBootstrapTokenPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id));
}
// static
const char* SyncPrefs::GetPrefNameForTypeForTesting(UserSelectableType type) {
return GetPrefNameForType(type);
}
// static
const char* SyncPrefs::GetPrefNameForType(UserSelectableType type) {
switch (type) {
case UserSelectableType::kBookmarks:
return prefs::internal::kSyncBookmarks;
case UserSelectableType::kPreferences:
return prefs::internal::kSyncPreferences;
case UserSelectableType::kPasswords:
return prefs::internal::kSyncPasswords;
case UserSelectableType::kAutofill:
return prefs::internal::kSyncAutofill;
case UserSelectableType::kThemes:
return prefs::internal::kSyncThemes;
case UserSelectableType::kHistory:
return prefs::internal::kSyncHistory;
case UserSelectableType::kExtensions:
return prefs::internal::kSyncExtensions;
case UserSelectableType::kApps:
return prefs::internal::kSyncApps;
case UserSelectableType::kReadingList:
return prefs::internal::kSyncReadingList;
case UserSelectableType::kTabs:
return prefs::internal::kSyncTabs;
case UserSelectableType::kSavedTabGroups:
return prefs::internal::kSyncSavedTabGroups;
case UserSelectableType::kPayments:
return prefs::internal::kSyncPayments;
case UserSelectableType::kProductComparison:
return prefs::internal::kSyncProductComparison;
case UserSelectableType::kCookies:
return prefs::internal::kSyncCookies;
}
NOTREACHED();
}
// static
void SyncPrefs::SetTypeDisabledByPolicy(PrefValueMap* policy_prefs,
UserSelectableType type) {
const char* pref_name = syncer::SyncPrefs::GetPrefNameForType(type);
CHECK(pref_name);
CHECK(policy_prefs);
policy_prefs->SetValue(pref_name, base::Value(false));
}
// static void
void SyncPrefs::SetTypeDisabledByCustodian(PrefValueMap* supervised_user_prefs,
UserSelectableType type) {
const char* pref_name = syncer::SyncPrefs::GetPrefNameForType(type);
CHECK(pref_name);
CHECK(supervised_user_prefs);
supervised_user_prefs->SetValue(pref_name, base::Value(false));
}
// static
bool SyncPrefs::IsTypeSupportedInTransportMode(UserSelectableType type) {
// Not all types are supported in transport mode, and many require one or more
// Features to be enabled.
switch (type) {
case UserSelectableType::kBookmarks:
return base::FeatureList::IsEnabled(
switches::kSyncEnableBookmarksInTransportMode);
case UserSelectableType::kReadingList:
return syncer::IsReadingListAccountStorageEnabled();
case UserSelectableType::kPreferences:
if (!base::FeatureList::IsEnabled(
switches::kEnablePreferencesAccountStorage)) {
return false;
}
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
#else
// Search engines are behind `UserSelectableType::kPreferences`.
return base::FeatureList::IsEnabled(
kSeparateLocalAndAccountSearchEngines);
#endif
case UserSelectableType::kPasswords:
return true;
case UserSelectableType::kAutofill:
return true;
case UserSelectableType::kPayments:
// Always supported, since AUTOFILL_WALLET_DATA is supported in
// transport mode everywhere.
return true;
case UserSelectableType::kHistory:
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
case UserSelectableType::kTabs:
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
case UserSelectableType::kProductComparison:
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
case UserSelectableType::kSavedTabGroups:
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
case UserSelectableType::kExtensions:
#if BUILDFLAG(ENABLE_DESKTOP_ANDROID_EXTENSIONS)
return true;
#else
return base::FeatureList::IsEnabled(
switches::kEnableExtensionsExplicitBrowserSignin);
#endif
case UserSelectableType::kThemes:
#if BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_IOS)
return false;
#else
return base::FeatureList::IsEnabled(
syncer::kSeparateLocalAndAccountThemes);
#endif
case UserSelectableType::kApps:
case UserSelectableType::kCookies:
// These types are not supported in transport mode yet.
// TODO(crbug.com/420912307): `kApps` should be allowed in
// `kReplaceSyncPromosWithSignInPromos` is enabled.
return false;
}
NOTREACHED();
}
void SyncPrefs::OnSyncManagedPrefChanged() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSyncManagedPrefChange(*pref_sync_managed_);
}
}
void SyncPrefs::OnSelectedTypesPrefChanged(const std::string& pref_name) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
// If there is a batch update of selected-types prefs ongoing, don't notify
// observers - the call site will take care of it.
if (batch_updating_selected_types_) {
return;
}
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSelectedTypesPrefChange();
}
}
// static
void SyncPrefs::RegisterTypeSelectedPref(PrefRegistrySimple* registry,
UserSelectableType type) {
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
registry->RegisterBooleanPref(pref_name, false);
}
bool SyncPrefs::IsLocalSyncEnabled() const {
return local_sync_enabled_;
}
int SyncPrefs::GetPassphrasePromptMutedProductVersion() const {
return pref_service_->GetInteger(
prefs::internal::kSyncPassphrasePromptMutedProductVersion);
}
void SyncPrefs::SetPassphrasePromptMutedProductVersion(int major_version) {
pref_service_->SetInteger(
prefs::internal::kSyncPassphrasePromptMutedProductVersion, major_version);
}
void SyncPrefs::ClearPassphrasePromptMutedProductVersion() {
pref_service_->ClearPref(
prefs::internal::kSyncPassphrasePromptMutedProductVersion);
}
bool SyncPrefs::MaybeMigratePrefsForSyncToSigninPart1(
SyncAccountState account_state,
const GaiaId& gaia_id) {
if (!base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos)) {
// Ensure that the migration runs again when the feature gets enabled.
pref_service_->ClearPref(prefs::internal::kSyncToSigninMigrationState);
return false;
}
// Don't migrate again if this profile was previously migrated.
if (pref_service_->GetInteger(prefs::internal::kSyncToSigninMigrationState) !=
kNotMigrated) {
return false;
}
if (IsLocalSyncEnabled()) {
// Special case for local sync: There isn't necessarily a signed-in user
// (even if the SyncAccountState is kSyncing), so just mark the migration as
// done.
pref_service_->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart2AndFullyDone);
return false;
}
switch (account_state) {
case SyncAccountState::kNotSignedIn:
case SyncAccountState::kSyncing: {
// Nothing to migrate for signed-out or syncing users. Also make sure the
// second part of the migration does *not* run if a signed-out user does
// later sign in / turn on sync.
pref_service_->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart2AndFullyDone);
return false;
}
case SyncAccountState::kSignedInNotSyncing: {
pref_service_->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart1ButNot2);
CHECK(!gaia_id.empty());
ScopedDictPrefUpdate update_selected_types_dict(
pref_service_, prefs::internal::kSelectedTypesPerAccount);
base::Value::Dict* account_settings =
update_selected_types_dict->EnsureDict(
signin::GaiaIdHash::FromGaiaId(gaia_id).ToBase64());
// Settings aka preferences always starts out "off" for existing
// signed-in non-syncing users.
account_settings->Set(
GetPrefNameForType(UserSelectableType::kPreferences), false);
// Bookmarks and reading list remain enabled only if the user previously
// explicitly opted in, which is represented in the regular account-keyed
// prefs. However, the default value for new sign-ins changes with
// `kReplaceSyncPromosWithSignInPromos`, so it is important to grab a
// snapshot now during migration.
for (UserSelectableType type :
{UserSelectableType::kBookmarks, UserSelectableType::kReadingList}) {
const char* pref_name = GetPrefNameForType(type);
DCHECK(pref_name);
const base::Value* value = account_settings->Find(pref_name);
const bool is_type_on = value && value->is_bool() && value->GetBool();
// Setting the value explicitly is important to convert absence to
// false, so it doesn't use the default, which is enabled after
// `kReplaceSyncPromosWithSignInPromos`.
account_settings->Set(pref_name, is_type_on);
}
return true;
}
}
}
bool SyncPrefs::MaybeMigratePrefsForSyncToSigninPart2(
const GaiaId& gaia_id,
bool is_using_explicit_passphrase) {
// The migration pref shouldn't be set if the feature is disabled, but if it
// somehow happened, do *not* run the migration, and clear the pref so that
// the migration will get triggered again once the feature gets enabled again.
if (!base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos)) {
pref_service_->ClearPref(prefs::internal::kSyncToSigninMigrationState);
return false;
}
// Only run part 2 of the migration if part 1 has run but part 2 hasn't yet.
// This ensures that it only runs once.
if (pref_service_->GetInteger(prefs::internal::kSyncToSigninMigrationState) !=
kMigratedPart1ButNot2) {
return false;
}
pref_service_->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart2AndFullyDone);
// The actual migration: For explicit-passphrase users, addresses sync gets
// disabled by default.
if (is_using_explicit_passphrase && !IsLocalSyncEnabled()) {
CHECK(!gaia_id.empty());
SetAccountKeyedPrefDictEntry(
pref_service_, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id),
GetPrefNameForType(UserSelectableType::kAutofill), base::Value(false));
return true;
}
return false;
}
void SyncPrefs::MaybeMigrateCustomPassphrasePref(const GaiaId& gaia_id) {
if (pref_service_->GetBoolean(
kSyncEncryptionBootstrapTokenPerAccountMigrationDone)) {
return;
}
pref_service_->SetBoolean(
kSyncEncryptionBootstrapTokenPerAccountMigrationDone, true);
if (gaia_id.empty()) {
// Do not migrate if gaia_id is empty; no signed in user.
return;
}
const std::string& token =
pref_service_->GetString(prefs::internal::kSyncEncryptionBootstrapToken);
if (token.empty()) {
// No custom passphrase is used, or it is not set.
return;
}
SetAccountKeyedPrefValue(
pref_service_, prefs::internal::kSyncEncryptionBootstrapTokenPerAccount,
signin::GaiaIdHash::FromGaiaId(gaia_id), base::Value(token));
return;
}
// static
void SyncPrefs::MigrateAutofillWalletImportEnabledPref(
PrefService* pref_service) {
if (pref_service->GetBoolean(kObsoleteAutofillWalletImportEnabledMigrated)) {
// Migration already happened; nothing else needed.
return;
}
const base::Value* autofill_wallet_import_enabled =
pref_service->GetUserPrefValue(kObsoleteAutofillWalletImportEnabled);
if (autofill_wallet_import_enabled != nullptr) {
// If the previous pref was populated explicitly, carry over the value.
pref_service->SetBoolean(prefs::internal::kSyncPayments,
autofill_wallet_import_enabled->GetBool());
} else if (pref_service->GetBoolean(
prefs::internal::kSyncKeepEverythingSynced)) {
// The old pref isn't explicitly set (defaults to true) and sync-everything
// is on: in this case there is no need to explicitly set individual
// UserSelectableType to true.
} else {
// There is a special case for very old profiles, created before 2019 (i.e.
// before https://codereview.chromium.org/2068653003 and similar code
// changes). In older versions of the UI, it was possible to set
// kSyncKeepEverythingSynced to false, without populating
// kObsoleteAutofillWalletImportEnabled. The latter defaults to true, but
// that's not the case for the new replacement, i.e.
// prefs::internal::kSyncPayments, so it needs to be populated manually to
// migrate the old behavior.
pref_service->SetBoolean(prefs::internal::kSyncPayments, true);
}
pref_service->ClearPref(kObsoleteAutofillWalletImportEnabled);
pref_service->SetBoolean(kObsoleteAutofillWalletImportEnabledMigrated, true);
}
// static
void SyncPrefs::MigrateGlobalDataTypePrefsToAccount(PrefService* pref_service,
const GaiaId& gaia_id) {
CHECK(!gaia_id.empty());
// Note: This method does *not* ensure that the migration only runs once -
// that's the caller's responsibility! (In practice, that's
// MaybeMigrateSyncingUserToSignedIn()).
ScopedDictPrefUpdate update_selected_types_dict(
pref_service, prefs::internal::kSelectedTypesPerAccount);
base::Value::Dict* account_settings = update_selected_types_dict->EnsureDict(
signin::GaiaIdHash::FromGaiaId(gaia_id).ToBase64());
// The values of the "global" data type prefs get copied to the
// account-specific ones.
bool everything_enabled =
pref_service->GetBoolean(prefs::internal::kSyncKeepEverythingSynced);
// History and Tabs should remain enabled only if they were both enabled
// previously, so they're specially tracked here.
bool history_and_tabs_enabled = false;
if (everything_enabled) {
// Most of the per-account prefs default to "true", so nothing needs to be
// done for those. The exceptions are History and Tabs, which need to be
// enabled explicitly.
history_and_tabs_enabled = true;
// Additionally, on desktop, Passwords is considered disabled by default and
// so also needs to be enabled explicitly.
#if !BUILDFLAG(IS_ANDROID) && !BUILDFLAG(IS_IOS)
// TODO(b/314773312): Remove this when Uno is enabled.
account_settings->Set(GetPrefNameForType(UserSelectableType::kPasswords),
true);
#endif
} else {
// "Sync everything" is off, so copy over the individual value for each
// type.
for (UserSelectableType type : UserSelectableTypeSet::All()) {
const char* pref_name = GetPrefNameForType(type);
CHECK(pref_name);
// Copy value from global to per-account pref.
account_settings->Set(pref_name, pref_service->GetBoolean(pref_name));
}
// Special case: History and Tabs remain enabled only if they were both
// enabled previously.
history_and_tabs_enabled =
pref_service->GetBoolean(
GetPrefNameForType(UserSelectableType::kHistory)) &&
pref_service->GetBoolean(GetPrefNameForType(UserSelectableType::kTabs));
}
account_settings->Set(GetPrefNameForType(UserSelectableType::kHistory),
history_and_tabs_enabled);
account_settings->Set(GetPrefNameForType(UserSelectableType::kTabs),
history_and_tabs_enabled);
// Another special case: For custom passphrase users, "Addresses and more"
// gets disabled by default. The reason is that for syncing custom passphrase
// users, this toggle mapped to the legacy AUTOFILL_PROFILE type (which
// supported custom passphrase), but for migrated users it maps to
// CONTACT_INFO (which does not).
std::optional<PassphraseType> passphrase_type = ProtoPassphraseInt32ToEnum(
pref_service->GetInteger(prefs::internal::kSyncCachedPassphraseType));
if (passphrase_type.has_value() && IsExplicitPassphrase(*passphrase_type)) {
account_settings->Set(GetPrefNameForType(UserSelectableType::kAutofill),
false);
}
// Usually, the "SyncToSignin" migration (aka phase 2) will have completed
// previously. But just in case it hasn't, make sure it doesn't run in the
// future - it's not neeced, and in fact it might mess up some of the things
// that were just migrated here.
pref_service->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart2AndFullyDone);
}
#if BUILDFLAG(ENABLE_DICE_SUPPORT)
// static
void SyncPrefs::MaybeMigrateAutofillToPerAccountPref(
PrefService* pref_service) {
if (pref_service->GetBoolean(kAutofillPerAccountPrefMigrationDone)) {
return;
}
pref_service->SetBoolean(kAutofillPerAccountPrefMigrationDone, true);
const GaiaId last_syncing_gaia_id(
pref_service->GetString(::prefs::kGoogleServicesLastSyncingGaiaId));
if (last_syncing_gaia_id.empty()) {
return;
}
if (pref_service->GetBoolean(prefs::internal::kSyncKeepEverythingSynced)) {
return;
}
for (auto user_selectable_type :
{UserSelectableType::kPasswords, UserSelectableType::kAutofill}) {
const char* const pref_name_for_type =
GetPrefNameForType(user_selectable_type);
if (pref_service->GetBoolean(pref_name_for_type)) {
continue;
}
SetAccountKeyedPrefDictEntry(
pref_service, prefs::internal::kSelectedTypesPerAccount,
signin::GaiaIdHash::FromGaiaId(last_syncing_gaia_id),
pref_name_for_type, base::Value(false));
}
}
#endif // BUILDFLAG(ENABLE_DICE_SUPPORT)
void SyncPrefs::MarkPartialSyncToSigninMigrationFullyDone() {
// If the first part of the migration has run, but the second part has not,
// then mark the migration as fully done - at this point (after signout)
// there's no more need for any migration.
// In all other cases (migration never even started, or completed fully),
// nothing to be done here.
if (pref_service_->GetInteger(prefs::internal::kSyncToSigninMigrationState) ==
kMigratedPart1ButNot2) {
pref_service_->SetInteger(prefs::internal::kSyncToSigninMigrationState,
kMigratedPart2AndFullyDone);
}
}
void SyncPrefs::SetPasswordSyncAllowed(bool allowed) {
if (password_sync_allowed_ == allowed) {
return;
}
password_sync_allowed_ = allowed;
for (SyncPrefObserver& observer : sync_pref_observers_) {
observer.OnSelectedTypesPrefChange();
}
}
bool SyncPrefs::IsTypeSelectedByDefaultInTransportMode(
UserSelectableType type,
const GaiaId& gaia_id) const {
// If sign-in is implicit (legacy desktop Dice state), only payments is on by
// default.
if (!IsExplicitBrowserSignin()) {
return type == UserSelectableType::kPayments;
}
switch (type) {
case UserSelectableType::kPayments:
case UserSelectableType::kPasswords:
case UserSelectableType::kAutofill:
return true;
case UserSelectableType::kHistory:
case UserSelectableType::kTabs:
case UserSelectableType::kSavedTabGroups:
// History and tabs require a separate opt in.
return false;
case UserSelectableType::kBookmarks:
case UserSelectableType::kReadingList:
// Before kReplaceSyncPromosWithSignInPromos, Bookmarks and Reading List
// require a specific explicit sign in (relevant for desktop only).
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos) ||
SigninPrefs(*pref_service_)
.GetBookmarksExplicitBrowserSignin(gaia_id) ||
base::FeatureList::IsEnabled(
kEnableBookmarksSelectedTypeOnSigninForTesting);
case UserSelectableType::kPreferences:
case UserSelectableType::kThemes:
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos) ||
pref_service_->GetBoolean(
::prefs::kPrefsThemesSearchEnginesAccountStorageEnabled);
case UserSelectableType::kExtensions:
// Before kReplaceSyncPromosWithSignInPromos, Extensions require a
// specific explicit sign in.
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos) ||
SigninPrefs(*pref_service_)
.GetExtensionsExplicitBrowserSignin(gaia_id);
case UserSelectableType::kApps:
case UserSelectableType::kProductComparison:
case UserSelectableType::kCookies:
// All other types are always enabled by default with
// kReplaceSyncPromosWithSignInPromos.
return base::FeatureList::IsEnabled(kReplaceSyncPromosWithSignInPromos);
}
NOTREACHED();
}
} // namespace syncer
|