OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/autofill/core/browser/webdata/autofill_table.h" | 5 #include "components/autofill/core/browser/webdata/autofill_table.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cmath> | 8 #include <cmath> |
9 #include <limits> | 9 #include <limits> |
10 #include <map> | 10 #include <map> |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 std::string encrypted_data; | 130 std::string encrypted_data; |
131 OSCrypt::EncryptString16(credit_card.GetRawInfo(CREDIT_CARD_NUMBER), | 131 OSCrypt::EncryptString16(credit_card.GetRawInfo(CREDIT_CARD_NUMBER), |
132 &encrypted_data); | 132 &encrypted_data); |
133 s->BindBlob(index++, encrypted_data.data(), | 133 s->BindBlob(index++, encrypted_data.data(), |
134 static_cast<int>(encrypted_data.length())); | 134 static_cast<int>(encrypted_data.length())); |
135 | 135 |
136 s->BindInt64(index++, Time::Now().ToTimeT()); | 136 s->BindInt64(index++, Time::Now().ToTimeT()); |
137 s->BindString(index++, credit_card.origin()); | 137 s->BindString(index++, credit_card.origin()); |
138 } | 138 } |
139 | 139 |
| 140 base::string16 UnencryptedCardFromColumn(const sql::Statement& s, |
| 141 int column_index) { |
| 142 base::string16 credit_card_number; |
| 143 int encrypted_number_len = s.ColumnByteLength(column_index); |
| 144 if (encrypted_number_len) { |
| 145 std::string encrypted_number; |
| 146 encrypted_number.resize(encrypted_number_len); |
| 147 memcpy(&encrypted_number[0], s.ColumnBlob(column_index), |
| 148 encrypted_number_len); |
| 149 OSCrypt::DecryptString16(encrypted_number, &credit_card_number); |
| 150 } |
| 151 return credit_card_number; |
| 152 } |
| 153 |
140 scoped_ptr<CreditCard> CreditCardFromStatement(const sql::Statement& s) { | 154 scoped_ptr<CreditCard> CreditCardFromStatement(const sql::Statement& s) { |
141 scoped_ptr<CreditCard> credit_card(new CreditCard); | 155 scoped_ptr<CreditCard> credit_card(new CreditCard); |
142 | 156 |
143 int index = 0; | 157 int index = 0; |
144 credit_card->set_guid(s.ColumnString(index++)); | 158 credit_card->set_guid(s.ColumnString(index++)); |
145 DCHECK(base::IsValidGUID(credit_card->guid())); | 159 DCHECK(base::IsValidGUID(credit_card->guid())); |
146 | 160 |
147 credit_card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); | 161 credit_card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); |
148 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); | 162 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); |
149 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, | 163 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, |
150 s.ColumnString16(index++)); | 164 s.ColumnString16(index++)); |
151 int encrypted_number_len = s.ColumnByteLength(index); | 165 credit_card->SetRawInfo(CREDIT_CARD_NUMBER, |
152 base::string16 credit_card_number; | 166 UnencryptedCardFromColumn(s, index++)); |
153 if (encrypted_number_len) { | |
154 std::string encrypted_number; | |
155 encrypted_number.resize(encrypted_number_len); | |
156 memcpy(&encrypted_number[0], s.ColumnBlob(index++), encrypted_number_len); | |
157 OSCrypt::DecryptString16(encrypted_number, &credit_card_number); | |
158 } else { | |
159 index++; | |
160 } | |
161 credit_card->SetRawInfo(CREDIT_CARD_NUMBER, credit_card_number); | |
162 // Intentionally skip column 5, which stores the modification date. | 167 // Intentionally skip column 5, which stores the modification date. |
163 index++; | 168 index++; |
164 credit_card->set_origin(s.ColumnString(index++)); | 169 credit_card->set_origin(s.ColumnString(index++)); |
165 | 170 |
166 return credit_card.Pass(); | 171 return credit_card.Pass(); |
167 } | 172 } |
168 | 173 |
169 // Obsolete version of AddAutofillProfileNamesToProfile, but still needed | 174 // Obsolete version of AddAutofillProfileNamesToProfile, but still needed |
170 // for MigrateToVersion37MergeAndCullOlderProfiles(). | 175 // for MigrateToVersion37MergeAndCullOlderProfiles(). |
171 bool AddAutofillProfileNamesToProfileForVersion37(sql::Connection* db, | 176 bool AddAutofillProfileNamesToProfileForVersion37(sql::Connection* db, |
(...skipping 907 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1079 std::string guid = s.ColumnString(0); | 1084 std::string guid = s.ColumnString(0); |
1080 CreditCard* credit_card = NULL; | 1085 CreditCard* credit_card = NULL; |
1081 if (!GetCreditCard(guid, &credit_card)) | 1086 if (!GetCreditCard(guid, &credit_card)) |
1082 return false; | 1087 return false; |
1083 credit_cards->push_back(credit_card); | 1088 credit_cards->push_back(credit_card); |
1084 } | 1089 } |
1085 | 1090 |
1086 return s.Succeeded(); | 1091 return s.Succeeded(); |
1087 } | 1092 } |
1088 | 1093 |
| 1094 bool AutofillTable::GetWalletCreditCards( |
| 1095 std::vector<CreditCard*>* credit_cards) { |
| 1096 credit_cards->clear(); |
| 1097 |
| 1098 sql::Statement s(db_->GetUniqueStatement( |
| 1099 "SELECT " |
| 1100 "card_number_encrypted, " // 0 |
| 1101 "last_four," // 1 |
| 1102 "masked.id," // 2 |
| 1103 "status," // 3 |
| 1104 "name_on_card," // 4 |
| 1105 "exp_month," // 6 |
| 1106 "exp_year " // 7 |
| 1107 "FROM masked_credit_cards masked JOIN unmasked_credit_cards unmasked " |
| 1108 "ON masked.id = unmasked.id")); |
| 1109 while (s.Step()) { |
| 1110 int index = 0; |
| 1111 |
| 1112 // If the card_number_encrypted field is nonempty, we can assume this card |
| 1113 // is a full card, otherwise it's masked. |
| 1114 base::string16 full_card_number = UnencryptedCardFromColumn(s, index++); |
| 1115 base::string16 last_four = s.ColumnString16(index++); |
| 1116 CreditCard::RecordType type = full_card_number.empty() ? |
| 1117 CreditCard::MASKED_WALLET_CARD : |
| 1118 CreditCard::FULL_WALLET_CARD; |
| 1119 std::string wallet_id = s.ColumnString(index++); |
| 1120 |
| 1121 CreditCard* card = new CreditCard(wallet_id, type); |
| 1122 card->SetRawInfo( |
| 1123 CREDIT_CARD_NUMBER, |
| 1124 type == CreditCard::MASKED_WALLET_CARD ? last_four : full_card_number); |
| 1125 |
| 1126 index++; // TODO(brettw) hook up status. For now, skip over it. |
| 1127 card->SetRawInfo(CREDIT_CARD_NAME, s.ColumnString16(index++)); |
| 1128 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); |
| 1129 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); |
| 1130 credit_cards->push_back(card); |
| 1131 } |
| 1132 return s.Succeeded(); |
| 1133 } |
| 1134 |
| 1135 void AutofillTable::UnmaskWalletCreditCard(const std::string& id, |
| 1136 const base::string16& full_number) { |
| 1137 // Make sure there aren't duplicates for this card. |
| 1138 MaskWalletCreditCard(id); |
| 1139 sql::Statement s(db_->GetUniqueStatement( |
| 1140 "INSERT INTO unmasked_credit_cards(id, card_number_encrypted) " |
| 1141 "VALUES (?,?)")); |
| 1142 s.BindString(0, id); |
| 1143 |
| 1144 std::string encrypted_data; |
| 1145 OSCrypt::EncryptString16(full_number, &encrypted_data); |
| 1146 s.BindBlob(1, encrypted_data.data(), |
| 1147 static_cast<int>(encrypted_data.length())); |
| 1148 |
| 1149 s.Run(); |
| 1150 } |
| 1151 |
| 1152 void AutofillTable::MaskWalletCreditCard(const std::string& id) { |
| 1153 sql::Statement s(db_->GetUniqueStatement( |
| 1154 "DELETE FROM unmasked_credit_cards WHERE id = ?")); |
| 1155 s.BindString(0, id); |
| 1156 s.Run(); |
| 1157 } |
| 1158 |
1089 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { | 1159 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { |
1090 DCHECK(base::IsValidGUID(credit_card.guid())); | 1160 DCHECK(base::IsValidGUID(credit_card.guid())); |
1091 | 1161 |
1092 CreditCard* tmp_credit_card = NULL; | 1162 CreditCard* tmp_credit_card = NULL; |
1093 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) | 1163 if (!GetCreditCard(credit_card.guid(), &tmp_credit_card)) |
1094 return false; | 1164 return false; |
1095 | 1165 |
1096 // Preserve appropriate modification dates by not updating unchanged cards. | 1166 // Preserve appropriate modification dates by not updating unchanged cards. |
1097 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); | 1167 scoped_ptr<CreditCard> old_credit_card(tmp_credit_card); |
1098 if (*old_credit_card == credit_card) | 1168 if (*old_credit_card == credit_card) |
(...skipping 1314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2413 "ADD COLUMN full_name VARCHAR"); | 2483 "ADD COLUMN full_name VARCHAR"); |
2414 } | 2484 } |
2415 | 2485 |
2416 bool AutofillTable::MigrateToVersion60AddServerCards() { | 2486 bool AutofillTable::MigrateToVersion60AddServerCards() { |
2417 return InitMaskedCreditCardsTable() && | 2487 return InitMaskedCreditCardsTable() && |
2418 InitUnmaskedCreditCardsTable() && | 2488 InitUnmaskedCreditCardsTable() && |
2419 InitServerAddressesTable(); | 2489 InitServerAddressesTable(); |
2420 } | 2490 } |
2421 | 2491 |
2422 } // namespace autofill | 2492 } // namespace autofill |
OLD | NEW |