Commit e39c85ae authored by Jan Krcal's avatar Jan Krcal Committed by Commit Bot

[AF] Use AutofillMetadata in the wallet metadata bridge

This CL refactors a few helpers for AutofillWalletMetadataSyncBridge
with the new AutofillMetadata struct in mind.

Bug: 853688
Change-Id: Idec8e641f116ad57454527a9c4b7f42a408507d7
Reviewed-on: https://chromium-review.googlesource.com/1245724
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarSebastien Seguin-Gagnon <sebsg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594694}
parent 9a396edd
...@@ -122,9 +122,17 @@ std::string GetBase64EncodedServerId(const std::string& server_id) { ...@@ -122,9 +122,17 @@ std::string GetBase64EncodedServerId(const std::string& server_id) {
} }
std::string GetSpecificsIdForEntryServerId(const std::string& server_id) { std::string GetSpecificsIdForEntryServerId(const std::string& server_id) {
// TODO(jkrcal): This specifics id for wallet_data probably should not be
// base64 encoded - this function is only used in printing debug data; should
// the storage key for wallet_data below be encoded? (probably yes, as this is
// already launched).
return GetBase64EncodedServerId(server_id); return GetBase64EncodedServerId(server_id);
} }
std::string GetSpecificsIdForMetadataId(const std::string& metadata_id) {
return GetBase64EncodedServerId(metadata_id);
}
std::string GetStorageKeyForSpecificsId(const std::string& specifics_id) { std::string GetStorageKeyForSpecificsId(const std::string& specifics_id) {
// We use the base64 encoded |specifics_id| directly as the storage key, this // We use the base64 encoded |specifics_id| directly as the storage key, this
// function only hides this definition from all its call sites. // function only hides this definition from all its call sites.
...@@ -132,9 +140,15 @@ std::string GetStorageKeyForSpecificsId(const std::string& specifics_id) { ...@@ -132,9 +140,15 @@ std::string GetStorageKeyForSpecificsId(const std::string& specifics_id) {
} }
std::string GetStorageKeyForEntryServerId(const std::string& server_id) { std::string GetStorageKeyForEntryServerId(const std::string& server_id) {
// TODO(jkrcal): This probably needs to stay base64 encoded while specifics id
// should not. Fix.
return GetStorageKeyForSpecificsId(GetSpecificsIdForEntryServerId(server_id)); return GetStorageKeyForSpecificsId(GetSpecificsIdForEntryServerId(server_id));
} }
std::string GetStorageKeyForMetadataId(const std::string& metadata_id) {
return GetStorageKeyForSpecificsId(GetSpecificsIdForMetadataId(metadata_id));
}
std::string GetClientTagForSpecificsId( std::string GetClientTagForSpecificsId(
AutofillWalletSpecifics::WalletInfoType type, AutofillWalletSpecifics::WalletInfoType type,
const std::string& wallet_data_specifics_id) { const std::string& wallet_data_specifics_id) {
......
...@@ -21,15 +21,23 @@ struct PaymentsCustomerData; ...@@ -21,15 +21,23 @@ struct PaymentsCustomerData;
// Returns the specified |server_id| encoded in base 64. // Returns the specified |server_id| encoded in base 64.
std::string GetBase64EncodedServerId(const std::string& server_id); std::string GetBase64EncodedServerId(const std::string& server_id);
// Returns the wallet specifics id for the specified |server_id|. // Returns the wallet data specifics id for the specified |server_id|.
std::string GetSpecificsIdForEntryServerId(const std::string& server_id); std::string GetSpecificsIdForEntryServerId(const std::string& server_id);
// Returns the storage key for the specified |server_id|. // Returns the wallet metadata specifics id for the specified |metadata_id|.
std::string GetSpecificsIdForMetadataId(const std::string& metadata_id);
// Returns the storage key for the specified |specifics_id|.
std::string GetStorageKeyForSpecificsId(const std::string& specifics_id); std::string GetStorageKeyForSpecificsId(const std::string& specifics_id);
// Returns the wallet specifics storage key for the specified |server_id|. // Returns the wallet data specifics storage key for the specified
// |server_id|.
std::string GetStorageKeyForEntryServerId(const std::string& server_id); std::string GetStorageKeyForEntryServerId(const std::string& server_id);
// Returns the wallet metadata specifics storage key for the specified
// |metadata_id|.
std::string GetStorageKeyForMetadataId(const std::string& metadata_id);
// Returns the client tag for the specified wallet |type| and // Returns the client tag for the specified wallet |type| and
// |wallet_data_specifics_id|. // |wallet_data_specifics_id|.
std::string GetClientTagForSpecificsId( std::string GetClientTagForSpecificsId(
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "base/base64.h" #include "base/base64.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/optional.h" #include "base/optional.h"
#include "components/autofill/core/browser/autofill_metadata.h"
#include "components/autofill/core/browser/autofill_profile.h" #include "components/autofill/core/browser/autofill_profile.h"
#include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/credit_card.h"
#include "components/autofill/core/browser/webdata/autofill_sync_bridge_util.h" #include "components/autofill/core/browser/webdata/autofill_sync_bridge_util.h"
...@@ -45,53 +46,56 @@ std::string GetClientTagForSpecificsId(WalletMetadataSpecifics::Type type, ...@@ -45,53 +46,56 @@ std::string GetClientTagForSpecificsId(WalletMetadataSpecifics::Type type,
} }
} }
// Returns EntityData with common fields set based on |local_data_model|. // Returns EntityData for wallet_metadata for |local_metadata| and |type|.
std::unique_ptr<EntityData> CreateEntityDataFromAutofillDataModel( std::unique_ptr<EntityData> CreateEntityDataFromAutofillMetadata(
const AutofillDataModel& local_data_model, const AutofillMetadata& local_metadata,
WalletMetadataSpecifics::Type type, WalletMetadataSpecifics::Type type) {
const std::string& specifics_id) {
auto entity_data = std::make_unique<EntityData>(); auto entity_data = std::make_unique<EntityData>();
std::string specifics_id = GetSpecificsIdForMetadataId(local_metadata.id);
entity_data->non_unique_name = GetClientTagForSpecificsId(type, specifics_id); entity_data->non_unique_name = GetClientTagForSpecificsId(type, specifics_id);
WalletMetadataSpecifics* metadata = WalletMetadataSpecifics* remote_metadata =
entity_data->specifics.mutable_wallet_metadata(); entity_data->specifics.mutable_wallet_metadata();
metadata->set_type(type); remote_metadata->set_type(type);
metadata->set_id(specifics_id); remote_metadata->set_id(specifics_id);
metadata->set_use_count(local_data_model.use_count()); remote_metadata->set_use_count(local_metadata.use_count);
metadata->set_use_date( remote_metadata->set_use_date(
local_data_model.use_date().ToDeltaSinceWindowsEpoch().InMicroseconds()); local_metadata.use_date.ToDeltaSinceWindowsEpoch().InMicroseconds());
switch (type) {
case WalletMetadataSpecifics::ADDRESS: {
remote_metadata->set_address_has_converted(local_metadata.has_converted);
break;
}
case WalletMetadataSpecifics::CARD: {
// The strings must be in valid UTF-8 to sync.
std::string billing_address_id;
base::Base64Encode(local_metadata.billing_address_id,
&billing_address_id);
remote_metadata->set_card_billing_address_id(billing_address_id);
break;
}
case WalletMetadataSpecifics::UNKNOWN: {
NOTREACHED();
break;
}
}
return entity_data; return entity_data;
} }
// Returns EntityData for wallet_metadata for |local_profile|. // Returns EntityData for wallet_metadata for |local_profile|.
std::unique_ptr<EntityData> CreateMetadataEntityDataFromAutofillServerProfile( std::unique_ptr<EntityData> CreateMetadataEntityDataFromAutofillServerProfile(
const AutofillProfile& local_profile) { const AutofillProfile& local_profile) {
std::unique_ptr<EntityData> entity_data = return CreateEntityDataFromAutofillMetadata(local_profile.GetMetadata(),
CreateEntityDataFromAutofillDataModel( WalletMetadataSpecifics::ADDRESS);
local_profile, WalletMetadataSpecifics::ADDRESS,
GetSpecificsIdForEntryServerId(local_profile.server_id()));
WalletMetadataSpecifics* metadata =
entity_data->specifics.mutable_wallet_metadata();
metadata->set_address_has_converted(local_profile.has_converted());
return entity_data;
} }
// Returns EntityData for wallet_metadata for |local_card|. // Returns EntityData for wallet_metadata for |local_card|.
std::unique_ptr<EntityData> CreateMetadataEntityDataFromCard( std::unique_ptr<EntityData> CreateMetadataEntityDataFromCard(
const CreditCard& local_card) { const CreditCard& local_card) {
std::unique_ptr<EntityData> entity_data = return CreateEntityDataFromAutofillMetadata(local_card.GetMetadata(),
CreateEntityDataFromAutofillDataModel( WalletMetadataSpecifics::CARD);
local_card, WalletMetadataSpecifics::CARD,
GetSpecificsIdForEntryServerId(local_card.server_id()));
WalletMetadataSpecifics* metadata =
entity_data->specifics.mutable_wallet_metadata();
// The strings must be in valid UTF-8 to sync.
std::string billing_address_id;
base::Base64Encode(local_card.billing_address_id(), &billing_address_id);
metadata->set_card_billing_address_id(billing_address_id);
return entity_data;
} }
} // namespace } // namespace
...@@ -269,12 +273,12 @@ void AutofillWalletMetadataSyncBridge::LoadDataCacheAndMetadata() { ...@@ -269,12 +273,12 @@ void AutofillWalletMetadataSyncBridge::LoadDataCacheAndMetadata() {
return; return;
} }
for (const std::unique_ptr<AutofillProfile>& entry : profiles) { for (const std::unique_ptr<AutofillProfile>& entry : profiles) {
cache_[GetStorageKeyForEntryServerId(entry->server_id())] = cache_[GetStorageKeyForMetadataId(entry->GetMetadata().id)] =
CreateMetadataEntityDataFromAutofillServerProfile(*entry) CreateMetadataEntityDataFromAutofillServerProfile(*entry)
->specifics.wallet_metadata(); ->specifics.wallet_metadata();
} }
for (const std::unique_ptr<CreditCard>& entry : cards) { for (const std::unique_ptr<CreditCard>& entry : cards) {
cache_[GetStorageKeyForEntryServerId(entry->server_id())] = cache_[GetStorageKeyForMetadataId(entry->GetMetadata().id)] =
CreateMetadataEntityDataFromCard(*entry)->specifics.wallet_metadata(); CreateMetadataEntityDataFromCard(*entry)->specifics.wallet_metadata();
} }
...@@ -307,17 +311,16 @@ void AutofillWalletMetadataSyncBridge::GetDataImpl( ...@@ -307,17 +311,16 @@ void AutofillWalletMetadataSyncBridge::GetDataImpl(
auto batch = std::make_unique<syncer::MutableDataBatch>(); auto batch = std::make_unique<syncer::MutableDataBatch>();
for (const std::unique_ptr<AutofillProfile>& entry : profiles) { for (const std::unique_ptr<AutofillProfile>& entry : profiles) {
std::string key = GetStorageKeyForEntryServerId(entry->server_id()); std::string key = GetStorageKeyForMetadataId(entry->GetMetadata().id);
if (!storage_keys_set || base::ContainsKey(*storage_keys_set, key)) { if (!storage_keys_set || base::ContainsKey(*storage_keys_set, key)) {
batch->Put(key, batch->Put(key,
CreateMetadataEntityDataFromAutofillServerProfile(*entry)); CreateMetadataEntityDataFromAutofillServerProfile(*entry));
} }
} }
for (const std::unique_ptr<CreditCard>& entry : cards) { for (const std::unique_ptr<CreditCard>& entry : cards) {
std::string key = GetStorageKeyForEntryServerId(entry->server_id()); std::string key = GetStorageKeyForMetadataId(entry->GetMetadata().id);
if (!storage_keys_set || base::ContainsKey(*storage_keys_set, key)) { if (!storage_keys_set || base::ContainsKey(*storage_keys_set, key)) {
batch->Put(GetStorageKeyForEntryServerId(entry->server_id()), batch->Put(key, CreateMetadataEntityDataFromCard(*entry));
CreateMetadataEntityDataFromCard(*entry));
} }
} }
......
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