Commit 219f3003 authored by sebsg's avatar sebsg Committed by Commit Bot

[AF] Add methods to Get Set and Remove WalletMetadata independently.

This CL adds methods to get set and remove wallet metadata from the
autofill table. These methods will be used by the new USS bridges.

Next CL will update existing code to use these new methods.

Change-Id: I611ad84dca5ca9daacde4aafeefc3cce4f459182
Reviewed-on: https://chromium-review.googlesource.com/1244816
Commit-Queue: Sebastien Seguin-Gagnon <sebsg@chromium.org>
Reviewed-by: default avatarJan Krcal <jkrcal@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594437}
parent b1681c58
...@@ -57,6 +57,7 @@ jumbo_static_library("browser") { ...@@ -57,6 +57,7 @@ jumbo_static_library("browser") {
"autofill_manager.cc", "autofill_manager.cc",
"autofill_manager.h", "autofill_manager.h",
"autofill_manager_test_delegate.h", "autofill_manager_test_delegate.h",
"autofill_metadata.cc",
"autofill_metadata.h", "autofill_metadata.h",
"autofill_metrics.cc", "autofill_metrics.cc",
"autofill_metrics.h", "autofill_metrics.h",
......
// Copyright 2018 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 "components/autofill/core/browser/autofill_metadata.h"
namespace autofill {
bool AutofillMetadata::operator==(const AutofillMetadata& metadata) const {
return id == metadata.id && use_count == metadata.use_count &&
use_date == metadata.use_date &&
has_converted == metadata.has_converted &&
billing_address_id == metadata.billing_address_id;
}
} // namespace autofill
...@@ -18,6 +18,8 @@ struct AutofillMetadata { ...@@ -18,6 +18,8 @@ struct AutofillMetadata {
AutofillMetadata(){}; AutofillMetadata(){};
~AutofillMetadata(){}; ~AutofillMetadata(){};
bool operator==(const AutofillMetadata&) const;
// The ID for the model. This should be the guid for local data and server_id // The ID for the model. This should be the guid for local data and server_id
// for the server data. // for the server data.
std::string id; std::string id;
...@@ -30,7 +32,7 @@ struct AutofillMetadata { ...@@ -30,7 +32,7 @@ struct AutofillMetadata {
// Only useful for SERVER_PROFILEs. Whether the server profile has been // Only useful for SERVER_PROFILEs. Whether the server profile has been
// converted to a local profile. // converted to a local profile.
bool has_converted; bool has_converted = false;
// Only useful for SERVER_CARDs. The identifier of the billing address for the // Only useful for SERVER_CARDs. The identifier of the billing address for the
// card. // card.
......
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "components/autofill/core/browser/autofill_country.h" #include "components/autofill/core/browser/autofill_country.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/autofill_type.h" #include "components/autofill/core/browser/autofill_type.h"
#include "components/autofill/core/browser/credit_card.h" #include "components/autofill/core/browser/credit_card.h"
...@@ -1295,6 +1296,21 @@ bool AutofillTable::MaskServerCreditCard(const std::string& id) { ...@@ -1295,6 +1296,21 @@ bool AutofillTable::MaskServerCreditCard(const std::string& id) {
return DeleteFromUnmaskedCreditCards(id); return DeleteFromUnmaskedCreditCards(id);
} }
bool AutofillTable::AddServerCardMetadata(
const AutofillMetadata& card_metadata) {
sql::Statement s(
db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, "
"use_date, billing_address_id, id)"
"VALUES (?,?,?,?)"));
s.BindInt64(0, card_metadata.use_count);
s.BindInt64(1, card_metadata.use_date.ToInternalValue());
s.BindString(2, card_metadata.billing_address_id);
s.BindString(3, card_metadata.id);
s.Run();
return db_->GetLastChangeCount() > 0;
}
bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {
DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
...@@ -1316,6 +1332,52 @@ bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { ...@@ -1316,6 +1332,52 @@ bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {
return db_->GetLastChangeCount() > 0; return db_->GetLastChangeCount() > 0;
} }
bool AutofillTable::RemoveServerCardMetadata(const std::string& id) {
sql::Statement remove(
db_->GetUniqueStatement("DELETE FROM server_card_metadata WHERE id = ?"));
remove.BindString(0, id);
remove.Run();
return db_->GetLastChangeCount() > 0;
}
bool AutofillTable::GetServerCardsMetadata(
std::map<std::string, AutofillMetadata>* cards_metadata) const {
cards_metadata->clear();
sql::Statement s(
db_->GetUniqueStatement("SELECT id, use_count, use_date, "
"billing_address_id FROM server_card_metadata"));
while (s.Step()) {
int index = 0;
AutofillMetadata card_metadata;
card_metadata.id = s.ColumnString(index++);
card_metadata.use_count = s.ColumnInt64(index++);
card_metadata.use_date =
base::Time::FromInternalValue(s.ColumnInt64(index++));
card_metadata.billing_address_id = s.ColumnString(index++);
(*cards_metadata)[card_metadata.id] = card_metadata;
}
return s.Succeeded();
}
bool AutofillTable::AddServerAddressMetadata(
const AutofillMetadata& address_metadata) {
sql::Statement s(
db_->GetUniqueStatement("INSERT INTO server_address_metadata(use_count, "
"use_date, has_converted, id)"
"VALUES (?,?,?,?)"));
s.BindInt64(0, address_metadata.use_count);
s.BindInt64(1, address_metadata.use_date.ToInternalValue());
s.BindBool(2, address_metadata.has_converted);
s.BindString(3, address_metadata.id);
s.Run();
return db_->GetLastChangeCount() > 0;
}
// TODO(crbug.com/680182): Record the address conversion status when a server // TODO(crbug.com/680182): Record the address conversion status when a server
// address gets converted. // address gets converted.
bool AutofillTable::UpdateServerAddressMetadata( bool AutofillTable::UpdateServerAddressMetadata(
...@@ -1346,6 +1408,36 @@ bool AutofillTable::UpdateServerAddressMetadata( ...@@ -1346,6 +1408,36 @@ bool AutofillTable::UpdateServerAddressMetadata(
return db_->GetLastChangeCount() > 0; return db_->GetLastChangeCount() > 0;
} }
bool AutofillTable::RemoveServerAddressMetadata(const std::string& id) {
sql::Statement remove(db_->GetUniqueStatement(
"DELETE FROM server_address_metadata WHERE id = ?"));
remove.BindString(0, id);
remove.Run();
return db_->GetLastChangeCount() > 0;
}
bool AutofillTable::GetServerAddressesMetadata(
std::map<std::string, AutofillMetadata>* addresses_metadata) const {
addresses_metadata->clear();
sql::Statement s(
db_->GetUniqueStatement("SELECT id, use_count, use_date, has_converted "
"FROM server_address_metadata"));
while (s.Step()) {
int index = 0;
AutofillMetadata address_metadata;
address_metadata.id = s.ColumnString(index++);
address_metadata.use_count = s.ColumnInt64(index++);
address_metadata.use_date =
base::Time::FromInternalValue(s.ColumnInt64(index++));
address_metadata.has_converted = s.ColumnBool(index++);
(*addresses_metadata)[address_metadata.id] = address_metadata;
}
return s.Succeeded();
}
void AutofillTable::SetPaymentsCustomerData( void AutofillTable::SetPaymentsCustomerData(
const PaymentsCustomerData* customer_data) { const PaymentsCustomerData* customer_data) {
sql::Transaction transaction(db_); sql::Transaction transaction(db_);
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stddef.h> #include <stddef.h>
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
...@@ -29,13 +30,13 @@ namespace autofill { ...@@ -29,13 +30,13 @@ namespace autofill {
class AutofillChange; class AutofillChange;
class AutofillEntry; class AutofillEntry;
struct AutofillMetadata;
class AutofillProfile; class AutofillProfile;
class AutofillTableEncryptor; class AutofillTableEncryptor;
class AutofillTableTest; class AutofillTableTest;
class CreditCard; class CreditCard;
struct PaymentsCustomerData;
struct FormFieldData; struct FormFieldData;
struct PaymentsCustomerData;
// This class manages the various Autofill tables within the SQLite database // This class manages the various Autofill tables within the SQLite database
// passed to the constructor. It expects the following schemas: // passed to the constructor. It expects the following schemas:
...@@ -401,8 +402,18 @@ class AutofillTable : public WebDatabaseTable, ...@@ -401,8 +402,18 @@ class AutofillTable : public WebDatabaseTable,
const base::string16& full_number); const base::string16& full_number);
bool MaskServerCreditCard(const std::string& id); bool MaskServerCreditCard(const std::string& id);
// Methods to add, update, remove and get the metadata for server cards and
// addresses.
bool AddServerCardMetadata(const AutofillMetadata& card_metadata);
bool UpdateServerCardMetadata(const CreditCard& credit_card); bool UpdateServerCardMetadata(const CreditCard& credit_card);
bool RemoveServerCardMetadata(const std::string& id);
bool GetServerCardsMetadata(
std::map<std::string, AutofillMetadata>* cards_metadata) const;
bool AddServerAddressMetadata(const AutofillMetadata& address_metadata);
bool UpdateServerAddressMetadata(const AutofillProfile& profile); bool UpdateServerAddressMetadata(const AutofillProfile& profile);
bool RemoveServerAddressMetadata(const std::string& id);
bool GetServerAddressesMetadata(
std::map<std::string, AutofillMetadata>* addresses_metadata) const;
// Setters and getters related to the Google Payments customer data. // Setters and getters related to the Google Payments customer data.
// Passing null to the setter will clear the data. // Passing null to the setter will clear the data.
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/time/time.h" #include "base/time/time.h"
#include "build/build_config.h" #include "build/build_config.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/autofill_test_utils.h" #include "components/autofill/core/browser/autofill_test_utils.h"
#include "components/autofill/core/browser/autofill_type.h" #include "components/autofill/core/browser/autofill_type.h"
...@@ -1896,6 +1897,98 @@ TEST_F(AutofillTableTest, SetGetServerCards) { ...@@ -1896,6 +1897,98 @@ TEST_F(AutofillTableTest, SetGetServerCards) {
EXPECT_EQ(CreditCard::EXPIRED, outputs[1]->GetServerStatus()); EXPECT_EQ(CreditCard::EXPIRED, outputs[1]->GetServerStatus());
} }
TEST_F(AutofillTableTest, SetGetRemoveServerCardMetadata) {
// Create and set the metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.billing_address_id = "billing id";
EXPECT_TRUE(table_->AddServerCardMetadata(input));
// Make sure it was added correctly.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerCardsMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
// Remove the metadata from the table.
EXPECT_TRUE(table_->RemoveServerCardMetadata(input.id));
// Make sure it was removed correctly.
ASSERT_TRUE(table_->GetServerCardsMetadata(&outputs));
EXPECT_EQ(0U, outputs.size());
}
TEST_F(AutofillTableTest, SetGetRemoveServerAddressMetadata) {
// Create and set the metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.has_converted = true;
table_->AddServerAddressMetadata(input);
// Make sure it was added correctly.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerAddressesMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
// Remove the metadata from the table.
EXPECT_TRUE(table_->RemoveServerAddressMetadata(input.id));
// Make sure it was removed correctly.
ASSERT_TRUE(table_->GetServerAddressesMetadata(&outputs));
EXPECT_EQ(0U, outputs.size());
}
TEST_F(AutofillTableTest, RemoveWrongServerCardMetadata) {
// Crete and set some metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.billing_address_id = "billing id";
table_->AddServerCardMetadata(input);
// Make sure it was added correctly.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerCardsMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
// Try removing some non-existent metadata.
EXPECT_FALSE(table_->RemoveServerCardMetadata("a_wrong_id"));
// Make sure the metadata was not removed.
ASSERT_TRUE(table_->GetServerCardsMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
}
TEST_F(AutofillTableTest, RemoveWrongServerAddressMetadata) {
// Crete and set some metadata.
AutofillMetadata input;
input.id = "server id";
input.use_count = 50;
input.use_date = Time::Now();
input.has_converted = true;
table_->AddServerAddressMetadata(input);
// Make sure it was added correctly.
std::map<std::string, AutofillMetadata> outputs;
ASSERT_TRUE(table_->GetServerAddressesMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
EXPECT_EQ(input, outputs[input.id]);
// Try removing some non-existent metadata.
EXPECT_FALSE(table_->RemoveServerAddressMetadata("a_wrong_id"));
// Make sure the metadata was not removed.
ASSERT_TRUE(table_->GetServerAddressesMetadata(&outputs));
ASSERT_EQ(1U, outputs.size());
}
TEST_F(AutofillTableTest, MaskUnmaskServerCards) { TEST_F(AutofillTableTest, MaskUnmaskServerCards) {
base::string16 masked_number(ASCIIToUTF16("1111")); base::string16 masked_number(ASCIIToUTF16("1111"));
std::vector<CreditCard> inputs; std::vector<CreditCard> inputs;
......
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