summaryrefslogtreecommitdiffstats
path: root/chrome/browser/webdata/web_database.cc
diff options
context:
space:
mode:
authorjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-25 20:47:20 +0000
committerjhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-25 20:47:20 +0000
commitf11f6218bd425c87b82656019b27e853b91a40da (patch)
treed04f086ed6ee7b9f05225c1c5fe4368f77ded947 /chrome/browser/webdata/web_database.cc
parentd0d281589a8f6d156c37312bef32e6d1f0e5f6e5 (diff)
downloadchromium_src-f11f6218bd425c87b82656019b27e853b91a40da.zip
chromium_src-f11f6218bd425c87b82656019b27e853b91a40da.tar.gz
chromium_src-f11f6218bd425c87b82656019b27e853b91a40da.tar.bz2
Implement adding, updating and removing credit cards from the web database through the gtk AutoFill dialog.
BUG=18201 TEST=WebDatabaseTest.CreditCard, PersonalDataManagerTest.SetCreditCards Review URL: http://codereview.chromium.org/555068 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37036 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/webdata/web_database.cc')
-rw-r--r--chrome/browser/webdata/web_database.cc176
1 files changed, 175 insertions, 1 deletions
diff --git a/chrome/browser/webdata/web_database.cc b/chrome/browser/webdata/web_database.cc
index 019c7d3..3bc0438 100644
--- a/chrome/browser/webdata/web_database.cc
+++ b/chrome/browser/webdata/web_database.cc
@@ -17,6 +17,7 @@
#include "base/utf_string_conversions.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/autofill_type.h"
+#include "chrome/browser/autofill/credit_card.h"
#include "chrome/browser/diagnostics/sqlite_diagnostics.h"
#include "chrome/browser/history/history_database.h"
#include "chrome/browser/webdata/autofill_change.h"
@@ -111,6 +112,22 @@ using webkit_glue::PasswordForm;
// phone
// fax
//
+// credit_cards This table contains credit card data added by the user
+// with the AutoFill dialog. Most of the columns are
+// standard entries in a credit card form.
+//
+// label The label of the credit card. Presented to the user
+// when selecting credit cards.
+// unique_id The unique ID of this credit card.
+// name_on_card
+// type
+// card_number
+// expiration_month
+// expiration_year
+// verification_code The CVC/CVV/CVV2 card security code.
+// billing_address A foreign key into the autofill_profiles table.
+// shipping_address A foreign key into the autofill_profiles table.
+//
// web_app_icons
// url URL of the web app.
// width Width of the image.
@@ -198,7 +215,8 @@ sql::InitStatus WebDatabase::Init(const FilePath& db_name) {
// Initialize the tables.
if (!InitKeywordsTable() || !InitLoginsTable() || !InitWebAppIconsTable() ||
!InitWebAppsTable() || !InitAutofillTable() ||
- !InitAutofillDatesTable() || !InitAutoFillProfilesTable()) {
+ !InitAutofillDatesTable() || !InitAutoFillProfilesTable() ||
+ !InitCreditCardsTable()) {
LOG(WARNING) << "Unable to initialize the web database.";
return sql::INIT_FAILURE;
}
@@ -445,6 +463,31 @@ bool WebDatabase::InitAutoFillProfilesTable() {
return true;
}
+bool WebDatabase::InitCreditCardsTable() {
+ if (!db_.DoesTableExist("credit_cards")) {
+ if (!db_.Execute("CREATE TABLE credit_cards ( "
+ "label VARCHAR, "
+ "unique_id INTEGER PRIMARY KEY, "
+ "name_on_card VARCHAR, "
+ "type VARCHAR, "
+ "card_number VARCHAR, "
+ "expiration_month INTEGER, "
+ "expiration_year INTEGER, "
+ "verification_code VARCHAR, "
+ "billing_address VARCHAR, "
+ "shipping_address VARCHAR)")) {
+ NOTREACHED();
+ return false;
+ }
+ if (!db_.Execute("CREATE INDEX credit_cards_label_index "
+ "ON credit_cards (label)")) {
+ NOTREACHED();
+ return false;
+ }
+ }
+ return true;
+}
+
bool WebDatabase::InitWebAppIconsTable() {
if (!db_.DoesTableExist("web_app_icons")) {
if (!db_.Execute("CREATE TABLE web_app_icons ("
@@ -1331,6 +1374,137 @@ bool WebDatabase::RemoveAutoFillProfile(int profile_id) {
return s.Run();
}
+static void BindCreditCardToStatement(const CreditCard& creditcard,
+ sql::Statement* s) {
+ s->BindString(0, UTF16ToUTF8(creditcard.Label()));
+ s->BindInt(1, creditcard.unique_id());
+
+ string16 text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_NAME));
+ s->BindString(2, UTF16ToUTF8(text));
+ text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_TYPE));
+ s->BindString(3, UTF16ToUTF8(text));
+ text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_NUMBER));
+ s->BindString(4, UTF16ToUTF8(text));
+ text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH));
+ s->BindString(5, UTF16ToUTF8(text));
+ text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR));
+ s->BindString(6, UTF16ToUTF8(text));
+ text = creditcard.GetFieldText(AutoFillType(CREDIT_CARD_VERIFICATION_CODE));
+ s->BindString(7, UTF16ToUTF8(text));
+ s->BindString(8, UTF16ToUTF8(creditcard.billing_address()));
+ s->BindString(9, UTF16ToUTF8(creditcard.shipping_address()));
+}
+
+bool WebDatabase::AddCreditCard(const CreditCard& creditcard) {
+ sql::Statement s(db_.GetUniqueStatement(
+ "INSERT INTO credit_cards"
+ "(label, unique_id, name_on_card, type, card_number, expiration_month,"
+ " expiration_year, verification_code, billing_address, shipping_address)"
+ "VALUES (?,?,?,?,?,?,?,?,?,?)"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ BindCreditCardToStatement(creditcard, &s);
+
+ if (!s.Run()) {
+ NOTREACHED();
+ return false;
+ }
+
+ return s.Succeeded();
+}
+
+static CreditCard* CreditCardFromStatement(const sql::Statement& s) {
+ CreditCard* creditcard = new CreditCard(
+ ASCIIToUTF16(s.ColumnString(0)), s.ColumnInt(1));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_NAME),
+ ASCIIToUTF16(s.ColumnString(2)));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_TYPE),
+ ASCIIToUTF16(s.ColumnString(3)));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_NUMBER),
+ ASCIIToUTF16(s.ColumnString(4)));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH),
+ ASCIIToUTF16(s.ColumnString(5)));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR),
+ ASCIIToUTF16(s.ColumnString(6)));
+ creditcard->SetInfo(AutoFillType(CREDIT_CARD_VERIFICATION_CODE),
+ ASCIIToUTF16(s.ColumnString(7)));
+ creditcard->set_billing_address(ASCIIToUTF16(s.ColumnString(8)));
+ creditcard->set_shipping_address(ASCIIToUTF16(s.ColumnString(9)));
+
+ return creditcard;
+}
+
+bool WebDatabase::GetCreditCardForLabel(const string16& label,
+ CreditCard** creditcard) {
+ DCHECK(creditcard);
+ sql::Statement s(db_.GetUniqueStatement(
+ "SELECT * FROM credit_cards "
+ "WHERE label = ?"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ s.BindString(0, UTF16ToUTF8(label));
+ if (!s.Step())
+ return false;
+
+ *creditcard = CreditCardFromStatement(s);
+
+ return s.Succeeded();
+}
+
+bool WebDatabase::GetCreditCards(
+ std::vector<CreditCard*>* creditcards) {
+ DCHECK(creditcards);
+ creditcards->clear();
+
+ sql::Statement s(db_.GetUniqueStatement("SELECT * FROM credit_cards"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ while (s.Step())
+ creditcards->push_back(CreditCardFromStatement(s));
+
+ return s.Succeeded();
+}
+
+bool WebDatabase::UpdateCreditCard(const CreditCard& creditcard) {
+ DCHECK(creditcard.unique_id());
+ sql::Statement s(db_.GetUniqueStatement(
+ "UPDATE credit_cards "
+ "SET label=?, unique_id=?, name_on_card=?, type=?, card_number=?, "
+ " expiration_month=?, expiration_year=?, verification_code=?, "
+ " billing_address=?, shipping_address=? "
+ "WHERE unique_id=?"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ BindCreditCardToStatement(creditcard, &s);
+ s.BindInt(10, creditcard.unique_id());
+ return s.Run();
+}
+
+bool WebDatabase::RemoveCreditCard(int creditcard_id) {
+ DCHECK_NE(0, creditcard_id);
+ sql::Statement s(db_.GetUniqueStatement(
+ "DELETE FROM credit_cards WHERE unique_id = ?"));
+ if (!s) {
+ NOTREACHED() << "Statement prepare failed";
+ return false;
+ }
+
+ s.BindInt(0, creditcard_id);
+ return s.Run();
+}
+
bool WebDatabase::AddToCountOfFormElement(int64 pair_id,
int delta,
bool* was_removed) {