Commit 0814dbad authored by Parastoo Geranmayeh's avatar Parastoo Geranmayeh Committed by Commit Bot

[Autofill] Read Profile Validity from server

Reads the profiles fields validity map from the profile
validation pipeline on the server side  via ChromeSync
PriorityPreferences. Returns the fields validity map of
a profile by its GUID.

Change-Id: I85ffad3495463bf38c452ced99858d1240cd6b89
Reviewed-on: https://chromium-review.googlesource.com/1178933Reviewed-by: default avatarTatiana Gornak <melandory@chromium.org>
Reviewed-by: default avatarRoger McFarlane <rogerm@chromium.org>
Commit-Queue: Parastoo Geranmayeh <parastoog@google.com>
Cr-Commit-Position: refs/heads/master@{#584752}
parent 3fd29830
...@@ -468,7 +468,16 @@ void PersonalDataManager::Init( ...@@ -468,7 +468,16 @@ void PersonalDataManager::Init(
bool is_off_the_record) { bool is_off_the_record) {
CountryNames::SetLocaleString(app_locale_); CountryNames::SetLocaleString(app_locale_);
database_helper_->Init(profile_database, account_database); database_helper_->Init(profile_database, account_database);
SetPrefService(pref_service); SetPrefService(pref_service);
// Listen for the preference changes.
pref_registrar_.Init(pref_service);
pref_registrar_.Add(
prefs::kAutofillProfileValidity,
base::BindRepeating(&PersonalDataManager::ResetProfileValidity,
base::Unretained(this)));
identity_manager_ = identity_manager; identity_manager_ = identity_manager;
is_off_the_record_ = is_off_the_record; is_off_the_record_ = is_off_the_record;
...@@ -1541,6 +1550,22 @@ void PersonalDataManager::MoveJapanCityToStreetAddress() { ...@@ -1541,6 +1550,22 @@ void PersonalDataManager::MoveJapanCityToStreetAddress() {
pref_service_->SetBoolean(prefs::kAutofillJapanCityFieldMigrated, true); pref_service_->SetBoolean(prefs::kAutofillJapanCityFieldMigrated, true);
} }
const ProfileValidityMap& PersonalDataManager::GetProfileValidityByGUID(
std::string& guid) {
static const ProfileValidityMap& empty_validity_map = ProfileValidityMap();
if (!synced_profile_validity_) {
synced_profile_validity_ = std::make_unique<UserProfileValidityMap>();
if (!synced_profile_validity_->ParseFromString(
::autofill::prefs::GetAllProfilesValidityMapsEncodedString(
pref_service_)))
return empty_validity_map;
}
auto it = synced_profile_validity_->profile_validity().find(guid);
if (it != synced_profile_validity_->profile_validity().end())
return it->second;
return empty_validity_map;
}
// TODO(crbug.com/618448): Refactor MergeProfile to not depend on class // TODO(crbug.com/618448): Refactor MergeProfile to not depend on class
// variables. // variables.
std::string PersonalDataManager::MergeProfile( std::string PersonalDataManager::MergeProfile(
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/field_types.h" #include "components/autofill/core/browser/field_types.h"
#include "components/autofill/core/browser/payments/payments_customer_data.h" #include "components/autofill/core/browser/payments/payments_customer_data.h"
#include "components/autofill/core/browser/proto/server.pb.h"
#include "components/autofill/core/browser/suggestion.h" #include "components/autofill/core/browser/suggestion.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service.h" #include "components/autofill/core/browser/webdata/autofill_webdata_service.h"
#include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h" #include "components/autofill/core/browser/webdata/autofill_webdata_service_observer.h"
...@@ -424,6 +425,7 @@ class PersonalDataManager : public KeyedService, ...@@ -424,6 +425,7 @@ class PersonalDataManager : public KeyedService,
ClearCreditCardNonSettingsOrigins); ClearCreditCardNonSettingsOrigins);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest, FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest,
MoveJapanCityToStreetAddress); MoveJapanCityToStreetAddress);
FRIEND_TEST_ALL_PREFIXES(PersonalDataManagerTest, RequestProfileValidity);
friend class autofill::AutofillInteractiveTest; friend class autofill::AutofillInteractiveTest;
friend class autofill::PersonalDataManagerFactory; friend class autofill::PersonalDataManagerFactory;
...@@ -521,6 +523,9 @@ class PersonalDataManager : public KeyedService, ...@@ -521,6 +523,9 @@ class PersonalDataManager : public KeyedService,
// https://crbug.com/871301 // https://crbug.com/871301
void MoveJapanCityToStreetAddress(); void MoveJapanCityToStreetAddress();
// Get the profiles fields validity map by |guid|.
const ProfileValidityMap& GetProfileValidityByGUID(std::string& guid);
// Decides which database type to use for server and local cards. // Decides which database type to use for server and local cards.
std::unique_ptr<PersonalDatabaseHelper> database_helper_; std::unique_ptr<PersonalDatabaseHelper> database_helper_;
...@@ -681,6 +686,9 @@ class PersonalDataManager : public KeyedService, ...@@ -681,6 +686,9 @@ class PersonalDataManager : public KeyedService,
// Applies various fixes and cleanups on autofill credit cards. // Applies various fixes and cleanups on autofill credit cards.
void ApplyCardFixesAndCleanups(); void ApplyCardFixesAndCleanups();
// Resets |synced_profile_validity_|.
void ResetProfileValidity() { synced_profile_validity_.reset(); };
const std::string app_locale_; const std::string app_locale_;
// The default country code for new addresses. // The default country code for new addresses.
...@@ -689,6 +697,13 @@ class PersonalDataManager : public KeyedService, ...@@ -689,6 +697,13 @@ class PersonalDataManager : public KeyedService,
// The PrefService that this instance uses. Must outlive this instance. // The PrefService that this instance uses. Must outlive this instance.
PrefService* pref_service_ = nullptr; PrefService* pref_service_ = nullptr;
// Pref registrar for managing the change observers.
PrefChangeRegistrar pref_registrar_;
// Profiles validity read from the prefs. They are kept updated by
// observing changes in pref_services.
std::unique_ptr<UserProfileValidityMap> synced_profile_validity_;
// The identity manager that this instance uses. Must outlive this instance. // The identity manager that this instance uses. Must outlive this instance.
identity::IdentityManager* identity_manager_ = nullptr; identity::IdentityManager* identity_manager_ = nullptr;
......
...@@ -6601,4 +6601,80 @@ TEST_F(PersonalDataManagerTest, UseCorrectStorageForDifferentCards) { ...@@ -6601,4 +6601,80 @@ TEST_F(PersonalDataManagerTest, UseCorrectStorageForDifferentCards) {
EXPECT_EQ(profile, *profiles[0]); EXPECT_EQ(profile, *profiles[0]);
} }
// Requests profiles fields validities: empty profiles, non-existent profiles,
// and normal ones.
TEST_F(PersonalDataManagerTest, RequestProfileValidity) {
ResetPersonalDataManager(USER_MODE_NORMAL);
ProfileValidityMap profile_validity_map;
UserProfileValidityMap user_profile_validity_map;
std::string autofill_profile_validity;
// Empty validity map.
ASSERT_TRUE(
user_profile_validity_map.SerializeToString(&autofill_profile_validity));
personal_data_->pref_service_->SetString(prefs::kAutofillProfileValidity,
autofill_profile_validity);
std::string guid = "00000000-0000-0000-0000-0000000000001";
EXPECT_TRUE(personal_data_->GetProfileValidityByGUID(guid)
.field_validity_states()
.empty());
// Non-empty validity map.
std::vector<ServerFieldType> types = {
ADDRESS_HOME_LINE1, ADDRESS_HOME_STATE, ADDRESS_HOME_COUNTRY,
EMAIL_ADDRESS, ADDRESS_HOME_ZIP, NAME_FULL};
std::vector<AutofillProfile::ValidityState> states = {
AutofillProfile::UNSUPPORTED, AutofillProfile::EMPTY,
AutofillProfile::INVALID, AutofillProfile::VALID,
AutofillProfile::UNVALIDATED, AutofillProfile::INVALID};
ASSERT_EQ(types.size(), states.size());
for (unsigned long i = 0; i < types.size(); ++i) {
(*profile_validity_map
.mutable_field_validity_states())[static_cast<int>(types[i])] =
static_cast<int>(states[i]);
}
(*user_profile_validity_map.mutable_profile_validity())[guid] =
profile_validity_map;
ASSERT_TRUE(
user_profile_validity_map.SerializeToString(&autofill_profile_validity));
personal_data_->pref_service_->SetString(prefs::kAutofillProfileValidity,
autofill_profile_validity);
// Add another non-empty valdity profile.
guid = "00000000-0000-0000-0000-0000000000002";
profile_validity_map.Clear();
autofill_profile_validity.clear();
(*profile_validity_map
.mutable_field_validity_states())[static_cast<int>(EMAIL_ADDRESS)] =
static_cast<int>(AutofillProfile::VALID);
(*user_profile_validity_map.mutable_profile_validity())[guid] =
profile_validity_map;
ASSERT_TRUE(
user_profile_validity_map.SerializeToString(&autofill_profile_validity));
personal_data_->pref_service_->SetString(prefs::kAutofillProfileValidity,
autofill_profile_validity);
// Profile not found.
guid = "00000000-0000-0000-0000-0000000000003";
EXPECT_TRUE(personal_data_->GetProfileValidityByGUID(guid)
.field_validity_states()
.empty());
// Existing Profiles.
guid = "00000000-0000-0000-0000-0000000000001";
auto validities =
personal_data_->GetProfileValidityByGUID(guid).field_validity_states();
ASSERT_EQ(validities.size(), types.size());
for (unsigned long i = 0; i < types.size(); ++i)
EXPECT_EQ(validities.at(types[i]), states[i]);
guid = "00000000-0000-0000-0000-0000000000002";
validities =
personal_data_->GetProfileValidityByGUID(guid).field_validity_states();
ASSERT_FALSE(validities.empty());
EXPECT_EQ(validities.at(EMAIL_ADDRESS), AutofillProfile::VALID);
}
} // namespace autofill } // namespace autofill
...@@ -182,3 +182,24 @@ message AutofillUploadContents { ...@@ -182,3 +182,24 @@ message AutofillUploadContents {
optional uint32 password_length = 29; optional uint32 password_length = 29;
// The end of the section of password attributes. // The end of the section of password attributes.
} }
// This proto contains information about the validity of each field in an
// autofill profile. It is used to transfer the results of running the profile
// validation pipeline on the server side to the client via ChromeSync
// PriorityPreferences. An identical copy of this proto is maintained in
// the server code base.
message ProfileValidityMap {
// Map from autofill type to the validity of its value in the profile.
//
// Key should be one of the enum values from ServerFieldType. Values should be
// from the AutofillProfile::ValidityState enum above. Plain integers are used
// instead of enums because proto2 treats unknown enum values as unknown
// fields, which is confusing when the enums are in maps.
map<int32, int32> field_validity_states = 1;
}
// Map from profile GUIDs to profile validity maps for that profile. Each
// message should contain entries for all profiles from a single user.
message UserProfileValidityMap {
map<string, ProfileValidityMap> profile_validity = 1;
}
...@@ -21,6 +21,9 @@ const char kAutofillAcceptSaveCreditCardPromptState[] = ...@@ -21,6 +21,9 @@ const char kAutofillAcceptSaveCreditCardPromptState[] =
// preference. // preference.
const char kAutofillBillingCustomerNumber[] = "billing_customer_number"; 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 // Boolean that is true if Autofill is enabled and allowed to save credit card
// data. // data.
const char kAutofillCreditCardEnabled[] = "autofill.credit_card_enabled"; const char kAutofillCreditCardEnabled[] = "autofill.credit_card_enabled";
...@@ -98,6 +101,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { ...@@ -98,6 +101,9 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
prefs::kAutofillLastVersionDisusedCreditCardsDeleted, 0); prefs::kAutofillLastVersionDisusedCreditCardsDeleted, 0);
registry->RegisterBooleanPref(prefs::kAutofillCreditCardEnabled, true); registry->RegisterBooleanPref(prefs::kAutofillCreditCardEnabled, true);
registry->RegisterBooleanPref(prefs::kAutofillOrphanRowsRemoved, false); registry->RegisterBooleanPref(prefs::kAutofillOrphanRowsRemoved, false);
registry->RegisterStringPref(
prefs::kAutofillProfileValidity, "",
user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
} }
bool IsAutocompleteEnabled(const PrefService* prefs) { bool IsAutocompleteEnabled(const PrefService* prefs) {
...@@ -141,5 +147,9 @@ void SetPaymentsIntegrationEnabled(PrefService* prefs, bool enabled) { ...@@ -141,5 +147,9 @@ void SetPaymentsIntegrationEnabled(PrefService* prefs, bool enabled) {
prefs->SetBoolean(kAutofillWalletImportEnabled, enabled); prefs->SetBoolean(kAutofillWalletImportEnabled, enabled);
} }
std::string GetAllProfilesValidityMapsEncodedString(const PrefService* prefs) {
return prefs->GetString(kAutofillProfileValidity);
}
} // namespace prefs } // namespace prefs
} // namespace autofill } // namespace autofill
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#ifndef COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_PREFS_H_ #ifndef COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_PREFS_H_
#define COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_PREFS_H_ #define COMPONENTS_AUTOFILL_CORE_COMMON_AUTOFILL_PREFS_H_
#include <string>
class PrefService; class PrefService;
namespace user_prefs { namespace user_prefs {
...@@ -30,6 +32,7 @@ extern const char kAutofillOrphanRowsRemoved[]; ...@@ -30,6 +32,7 @@ extern const char kAutofillOrphanRowsRemoved[];
extern const char kAutofillProfileEnabled[]; extern const char kAutofillProfileEnabled[];
extern const char kAutofillWalletImportEnabled[]; extern const char kAutofillWalletImportEnabled[];
extern const char kAutofillWalletImportStorageCheckboxState[]; extern const char kAutofillWalletImportStorageCheckboxState[];
extern const char kAutofillProfileValidity[];
// Possible values for previous user decision when we displayed a save credit // Possible values for previous user decision when we displayed a save credit
// card prompt. // card prompt.
...@@ -63,6 +66,8 @@ bool IsPaymentsIntegrationEnabled(const PrefService* prefs); ...@@ -63,6 +66,8 @@ bool IsPaymentsIntegrationEnabled(const PrefService* prefs);
void SetPaymentsIntegrationEnabled(PrefService* prefs, bool enabled); void SetPaymentsIntegrationEnabled(PrefService* prefs, bool enabled);
std::string GetAllProfilesValidityMapsEncodedString(const PrefService* prefs);
} // namespace prefs } // namespace prefs
} // namespace autofill } // namespace autofill
......
...@@ -110,6 +110,9 @@ void RegisterAutofillPrefs(user_prefs::PrefRegistrySyncable* registry) { ...@@ -110,6 +110,9 @@ void RegisterAutofillPrefs(user_prefs::PrefRegistrySyncable* registry) {
autofill::prefs::kAutofillLastVersionDisusedAddressesDeleted, 0); autofill::prefs::kAutofillLastVersionDisusedAddressesDeleted, 0);
registry->RegisterIntegerPref( registry->RegisterIntegerPref(
autofill::prefs::kAutofillLastVersionDisusedCreditCardsDeleted, 0); autofill::prefs::kAutofillLastVersionDisusedCreditCardsDeleted, 0);
registry->RegisterStringPref(
autofill::prefs::kAutofillProfileValidity, "",
user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF);
} }
void RunAndSignal(base::OnceClosure cb, WaitableEvent* event) { void RunAndSignal(base::OnceClosure cb, WaitableEvent* event) {
......
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