Commit affe4c45 authored by sebsg's avatar sebsg Committed by Commit Bot

[AF] Add a wallet sync transport opt-in pref.

This will be used to record whether a user has opted in to seeing their
Google Account cards in the Autofill dropdown.

The dictionary should not contain info about accounts that are not opted
in.

Bug: 907929
Change-Id: Icc663b228792c58b00c02b204249fba9f557481b
Reviewed-on: https://chromium-review.googlesource.com/c/1348792Reviewed-by: default avatarFabio Tirelo <ftirelo@chromium.org>
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610621}
parent 14083b7e
......@@ -6,9 +6,32 @@
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "components/prefs/scoped_user_pref_update.h"
namespace autofill {
namespace prefs {
namespace {
// TODO(crbug.com/907929): Use a hash of the account id.
// Returns the opt-in bitfield for the specifiec |account_id| or 0 if no entry
// was found.
int GetSyncTransportOptInBitFieldForAccount(const PrefService* prefs,
const std::string& account_id) {
auto* upload_events =
prefs->GetDictionary(prefs::kAutofillSyncTransportOptIn);
// If there is no dictionary or no entry in the dictionary, it means the
// account didn't opt-in. Use 0 because it's the same as not having opted-in
// to anything.
if (!upload_events) {
return 0;
}
auto* found =
upload_events->FindKeyOfType(account_id, base::Value::Type::INTEGER);
return found ? found->GetInt() : 0;
}
} // namespace
// Integer that is set to the last choice user made when prompted for saving a
// credit card. The prompt is for user's consent in saving the card in the
......@@ -21,9 +44,6 @@ const char kAutofillAcceptSaveCreditCardPromptState[] =
// preference.
const char kAutofillBillingCustomerNumber[] = "billing_customer_number";
// The field type, validity state map of all profiles.
const char kAutofillProfileValidity[] = "autofill.profile_validity";
// Boolean that is true if Autofill is enabled and allowed to save credit card
// data.
const char kAutofillCreditCardEnabled[] = "autofill.credit_card_enabled";
......@@ -70,6 +90,12 @@ const char kAutofillOrphanRowsRemoved[] = "autofill.orphan_rows_removed";
// Boolean that is true if Autofill is enabled and allowed to save profile data.
const char kAutofillProfileEnabled[] = "autofill.profile_enabled";
// The field type, validity state map of all profiles.
const char kAutofillProfileValidity[] = "autofill.profile_validity";
// The opt-ins for Sync Transport features for each client.
const char kAutofillSyncTransportOptIn[] = "autofill.sync_transport_opt_ins";
// The (randomly inititialied) seed value to use when encoding form/field
// metadata for randomized uploads. The value of this pref is a string.
const char kAutofillUploadEncodingSeed[] = "autofill.upload_encoding_seed";
......@@ -141,6 +167,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterDictionaryPref(prefs::kAutofillUploadEvents);
registry->RegisterTimePref(prefs::kAutofillUploadEventsLastResetTimestamp,
base::Time());
registry->RegisterDictionaryPref(prefs::kAutofillSyncTransportOptIn);
}
void MigrateDeprecatedAutofillPrefs(PrefService* prefs) {
......@@ -235,5 +262,35 @@ std::string GetAllProfilesValidityMapsEncodedString(const PrefService* prefs) {
return prefs->GetString(kAutofillProfileValidity);
}
void SetUserOptedInWalletSyncTransport(PrefService* prefs,
const std::string& account_id,
bool opted_in) {
DictionaryPrefUpdate update(prefs, prefs::kAutofillSyncTransportOptIn);
int value = GetSyncTransportOptInBitFieldForAccount(prefs, account_id);
// If the user has opted in, set that bit while leaving the others intact.
if (opted_in) {
update->SetKey(account_id,
base::Value(value | sync_transport_opt_in::kWallet));
return;
}
// Invert the mask in order to reset the Wallet bit while leaving the other
// bits intact, or remove the key entirely if the Wallet was the only opt-in.
if (value & ~sync_transport_opt_in::kWallet) {
update->SetKey(account_id,
base::Value(value & ~sync_transport_opt_in::kWallet));
} else {
update->RemoveKey(account_id);
}
}
bool IsUserOptedInWalletSyncTransport(const PrefService* prefs,
const std::string& account_id) {
// Return whether the wallet opt-in bit is set.
return GetSyncTransportOptInBitFieldForAccount(prefs, account_id) &
sync_transport_opt_in::kWallet;
}
} // namespace prefs
} // namespace autofill
......@@ -35,12 +35,19 @@ extern const char kAutofillOrphanRowsRemoved[];
// Do not get/set the value of this pref directly. Use provided getter/setter.
extern const char kAutofillProfileEnabled[];
extern const char kAutofillProfileValidity[];
extern const char kAutofillSyncTransportOptIn[];
extern const char kAutofillUploadEncodingSeed[];
extern const char kAutofillUploadEvents[];
extern const char kAutofillUploadEventsLastResetTimestamp[];
extern const char kAutofillWalletImportEnabled[];
extern const char kAutofillWalletImportStorageCheckboxState[];
namespace sync_transport_opt_in {
enum Flags {
kWallet = 1 << 0,
};
} // namespace sync_transport_opt_in
// Possible values for previous user decision when we displayed a save credit
// card prompt.
enum PreviousSaveCreditCardPromptUserDecision {
......@@ -87,6 +94,13 @@ void SetPaymentsIntegrationEnabled(PrefService* prefs, bool enabled);
std::string GetAllProfilesValidityMapsEncodedString(const PrefService* prefs);
void SetUserOptedInWalletSyncTransport(PrefService* prefs,
const std::string& account_id,
bool opted_in);
bool IsUserOptedInWalletSyncTransport(const PrefService* prefs,
const std::string& account_id);
} // namespace prefs
} // namespace autofill
......
......@@ -73,5 +73,59 @@ TEST_F(AutofillPrefsTest, MigrateDeprecatedAutofillPrefs) {
EXPECT_FALSE(pref_service()->GetBoolean(kAutofillCreditCardEnabled));
}
// Tests that setting and getting the AutofillSyncTransportOptIn works as
// expected.
TEST_F(AutofillPrefsTest, WalletSyncTransportPref) {
const std::string account1 = "account1";
const std::string account2 = "account2";
// There should be no opt-in recorded at first.
ASSERT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account1));
ASSERT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account2));
// There should be no entry for the accounts in the dictionary.
auto* upload_events =
pref_service()->GetDictionary(prefs::kAutofillSyncTransportOptIn);
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account1, base::Value::Type::INTEGER));
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account2, base::Value::Type::INTEGER));
// Set the opt-in for the first account.
SetUserOptedInWalletSyncTransport(pref_service(), account1, true);
EXPECT_TRUE(IsUserOptedInWalletSyncTransport(pref_service(), account1));
EXPECT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account2));
// There should be an entry for the first account only in the dictionary.
upload_events =
pref_service()->GetDictionary(prefs::kAutofillSyncTransportOptIn);
EXPECT_NE(nullptr,
upload_events->FindKeyOfType(account1, base::Value::Type::INTEGER));
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account2, base::Value::Type::INTEGER));
// Unset the opt-in for the first account.
SetUserOptedInWalletSyncTransport(pref_service(), account1, false);
EXPECT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account1));
EXPECT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account2));
// There should be no entry for the accounts in the dictionary.
upload_events =
pref_service()->GetDictionary(prefs::kAutofillSyncTransportOptIn);
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account1, base::Value::Type::INTEGER));
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account2, base::Value::Type::INTEGER));
// Set the opt-in for the second account.
SetUserOptedInWalletSyncTransport(pref_service(), account2, true);
EXPECT_FALSE(IsUserOptedInWalletSyncTransport(pref_service(), account1));
EXPECT_TRUE(IsUserOptedInWalletSyncTransport(pref_service(), account2));
// There should be an entry for the second account only in the dictionary.
upload_events =
pref_service()->GetDictionary(prefs::kAutofillSyncTransportOptIn);
EXPECT_EQ(nullptr,
upload_events->FindKeyOfType(account1, base::Value::Type::INTEGER));
EXPECT_NE(nullptr,
upload_events->FindKeyOfType(account2, base::Value::Type::INTEGER));
}
} // namespace prefs
} // namespace autofill
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment