Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: components/autofill/core/browser/webdata/autofill_table.cc

Issue 2844463004: Rename card 'type' into 'issuer network.' (Closed)
Patch Set: read -> use Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <cmath> 10 #include <cmath>
(...skipping 27 matching lines...) Expand all
38 #include "components/autofill/core/common/form_field_data.h" 38 #include "components/autofill/core/common/form_field_data.h"
39 #include "components/sync/base/model_type.h" 39 #include "components/sync/base/model_type.h"
40 #include "components/sync/protocol/entity_metadata.pb.h" 40 #include "components/sync/protocol/entity_metadata.pb.h"
41 #include "components/sync/protocol/model_type_state.pb.h" 41 #include "components/sync/protocol/model_type_state.pb.h"
42 #include "components/webdata/common/web_database.h" 42 #include "components/webdata/common/web_database.h"
43 #include "sql/statement.h" 43 #include "sql/statement.h"
44 #include "sql/transaction.h" 44 #include "sql/transaction.h"
45 #include "ui/base/l10n/l10n_util.h" 45 #include "ui/base/l10n/l10n_util.h"
46 #include "url/gurl.h" 46 #include "url/gurl.h"
47 47
48 using base::ASCIIToUTF16;
49 using base::Time;
50 using base::TimeDelta;
51
52 namespace autofill { 48 namespace autofill {
53 namespace { 49 namespace {
54 50
55 // The period after which autocomplete entries should expire in days. 51 // The period after which autocomplete entries should expire in days.
56 const int64_t kExpirationPeriodInDays = 60; 52 const int64_t kExpirationPeriodInDays = 60;
57 53
58 // Helper struct for AutofillTable::RemoveFormElementsAddedBetween(). 54 // Helper struct for AutofillTable::RemoveFormElementsAddedBetween().
59 // Contains all the necessary fields to update a row in the 'autofill' table. 55 // Contains all the necessary fields to update a row in the 'autofill' table.
60 struct AutofillUpdate { 56 struct AutofillUpdate {
61 base::string16 name; 57 base::string16 name;
(...skipping 14 matching lines...) Expand all
76 base::string16 GetInfo(const AutofillDataModel& data_model, 72 base::string16 GetInfo(const AutofillDataModel& data_model,
77 ServerFieldType type) { 73 ServerFieldType type) {
78 base::string16 data = data_model.GetRawInfo(type); 74 base::string16 data = data_model.GetRawInfo(type);
79 if (data.size() > AutofillTable::kMaxDataLength) 75 if (data.size() > AutofillTable::kMaxDataLength)
80 return data.substr(0, AutofillTable::kMaxDataLength); 76 return data.substr(0, AutofillTable::kMaxDataLength);
81 77
82 return data; 78 return data;
83 } 79 }
84 80
85 void BindAutofillProfileToStatement(const AutofillProfile& profile, 81 void BindAutofillProfileToStatement(const AutofillProfile& profile,
86 const Time& modification_date, 82 const base::Time& modification_date,
87 sql::Statement* s) { 83 sql::Statement* s) {
88 DCHECK(base::IsValidGUID(profile.guid())); 84 DCHECK(base::IsValidGUID(profile.guid()));
89 int index = 0; 85 int index = 0;
90 s->BindString(index++, profile.guid()); 86 s->BindString(index++, profile.guid());
91 87
92 s->BindString16(index++, GetInfo(profile, COMPANY_NAME)); 88 s->BindString16(index++, GetInfo(profile, COMPANY_NAME));
93 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_STREET_ADDRESS)); 89 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_STREET_ADDRESS));
94 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_DEPENDENT_LOCALITY)); 90 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_DEPENDENT_LOCALITY));
95 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_CITY)); 91 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_CITY));
96 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_STATE)); 92 s->BindString16(index++, GetInfo(profile, ADDRESS_HOME_STATE));
(...skipping 17 matching lines...) Expand all
114 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); 110 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
115 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); 111 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
116 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 112 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
117 s.ColumnString16(index++)); 113 s.ColumnString16(index++));
118 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); 114 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
119 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); 115 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
120 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++)); 116 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
121 profile->SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++)); 117 profile->SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++));
122 profile->SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++)); 118 profile->SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++));
123 profile->set_use_count(s.ColumnInt64(index++)); 119 profile->set_use_count(s.ColumnInt64(index++));
124 profile->set_use_date(Time::FromTimeT(s.ColumnInt64(index++))); 120 profile->set_use_date(base::Time::FromTimeT(s.ColumnInt64(index++)));
125 profile->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++))); 121 profile->set_modification_date(base::Time::FromTimeT(s.ColumnInt64(index++)));
126 profile->set_origin(s.ColumnString(index++)); 122 profile->set_origin(s.ColumnString(index++));
127 profile->set_language_code(s.ColumnString(index++)); 123 profile->set_language_code(s.ColumnString(index++));
128 124
129 return profile; 125 return profile;
130 } 126 }
131 127
132 void BindEncryptedCardToColumn(sql::Statement* s, 128 void BindEncryptedCardToColumn(sql::Statement* s,
133 int column_index, 129 int column_index,
134 const base::string16& number, 130 const base::string16& number,
135 const AutofillTableEncryptor& encryptor) { 131 const AutofillTableEncryptor& encryptor) {
136 std::string encrypted_data; 132 std::string encrypted_data;
137 encryptor.EncryptString16(number, &encrypted_data); 133 encryptor.EncryptString16(number, &encrypted_data);
138 s->BindBlob(column_index, encrypted_data.data(), 134 s->BindBlob(column_index, encrypted_data.data(),
139 static_cast<int>(encrypted_data.length())); 135 static_cast<int>(encrypted_data.length()));
140 } 136 }
141 137
142 void BindCreditCardToStatement(const CreditCard& credit_card, 138 void BindCreditCardToStatement(const CreditCard& credit_card,
143 const Time& modification_date, 139 const base::Time& modification_date,
144 sql::Statement* s, 140 sql::Statement* s,
145 const AutofillTableEncryptor& encryptor) { 141 const AutofillTableEncryptor& encryptor) {
146 DCHECK(base::IsValidGUID(credit_card.guid())); 142 DCHECK(base::IsValidGUID(credit_card.guid()));
147 int index = 0; 143 int index = 0;
148 s->BindString(index++, credit_card.guid()); 144 s->BindString(index++, credit_card.guid());
149 145
150 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_NAME_FULL)); 146 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_NAME_FULL));
151 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_MONTH)); 147 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_MONTH));
152 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR)); 148 s->BindString16(index++, GetInfo(credit_card, CREDIT_CARD_EXP_4_DIGIT_YEAR));
153 BindEncryptedCardToColumn( 149 BindEncryptedCardToColumn(
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 credit_card->set_guid(s.ColumnString(index++)); 181 credit_card->set_guid(s.ColumnString(index++));
186 DCHECK(base::IsValidGUID(credit_card->guid())); 182 DCHECK(base::IsValidGUID(credit_card->guid()));
187 183
188 credit_card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); 184 credit_card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++));
189 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 185 credit_card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
190 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, 186 credit_card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR,
191 s.ColumnString16(index++)); 187 s.ColumnString16(index++));
192 credit_card->SetRawInfo(CREDIT_CARD_NUMBER, 188 credit_card->SetRawInfo(CREDIT_CARD_NUMBER,
193 UnencryptedCardFromColumn(s, index++, encryptor)); 189 UnencryptedCardFromColumn(s, index++, encryptor));
194 credit_card->set_use_count(s.ColumnInt64(index++)); 190 credit_card->set_use_count(s.ColumnInt64(index++));
195 credit_card->set_use_date(Time::FromTimeT(s.ColumnInt64(index++))); 191 credit_card->set_use_date(base::Time::FromTimeT(s.ColumnInt64(index++)));
196 credit_card->set_modification_date(Time::FromTimeT(s.ColumnInt64(index++))); 192 credit_card->set_modification_date(
193 base::Time::FromTimeT(s.ColumnInt64(index++)));
197 credit_card->set_origin(s.ColumnString(index++)); 194 credit_card->set_origin(s.ColumnString(index++));
198 credit_card->set_billing_address_id(s.ColumnString(index++)); 195 credit_card->set_billing_address_id(s.ColumnString(index++));
199 196
200 return credit_card; 197 return credit_card;
201 } 198 }
202 199
203 bool AddAutofillProfileNamesToProfile(sql::Connection* db, 200 bool AddAutofillProfileNamesToProfile(sql::Connection* db,
204 AutofillProfile* profile) { 201 AutofillProfile* profile) {
205 // TODO(estade): update schema so that multiple names are not associated per 202 // TODO(estade): update schema so that multiple names are not associated per
206 // unique profile guid. Please refer https://crbug.com/497934. 203 // unique profile guid. Please refer https://crbug.com/497934.
(...skipping 15 matching lines...) Expand all
222 profile->SetRawInfo(NAME_FULL, s.ColumnString16(4)); 219 profile->SetRawInfo(NAME_FULL, s.ColumnString16(4));
223 } 220 }
224 return s.Succeeded(); 221 return s.Succeeded();
225 } 222 }
226 223
227 bool AddAutofillProfileEmailsToProfile(sql::Connection* db, 224 bool AddAutofillProfileEmailsToProfile(sql::Connection* db,
228 AutofillProfile* profile) { 225 AutofillProfile* profile) {
229 // TODO(estade): update schema so that multiple emails are not associated per 226 // TODO(estade): update schema so that multiple emails are not associated per
230 // unique profile guid. Please refer https://crbug.com/497934. 227 // unique profile guid. Please refer https://crbug.com/497934.
231 sql::Statement s(db->GetUniqueStatement( 228 sql::Statement s(db->GetUniqueStatement(
232 "SELECT guid, email " 229 "SELECT guid, email FROM autofill_profile_emails WHERE guid=? LIMIT 1"));
233 "FROM autofill_profile_emails "
234 "WHERE guid=?"
235 "LIMIT 1"));
236 s.BindString(0, profile->guid()); 230 s.BindString(0, profile->guid());
237 231
238 if (!s.is_valid()) 232 if (!s.is_valid())
239 return false; 233 return false;
240 234
241 if (s.Step()) { 235 if (s.Step()) {
242 DCHECK_EQ(profile->guid(), s.ColumnString(0)); 236 DCHECK_EQ(profile->guid(), s.ColumnString(0));
243 profile->SetRawInfo(EMAIL_ADDRESS, s.ColumnString16(1)); 237 profile->SetRawInfo(EMAIL_ADDRESS, s.ColumnString16(1));
244 } 238 }
245 return s.Succeeded(); 239 return s.Succeeded();
246 } 240 }
247 241
248 bool AddAutofillProfilePhonesToProfile(sql::Connection* db, 242 bool AddAutofillProfilePhonesToProfile(sql::Connection* db,
249 AutofillProfile* profile) { 243 AutofillProfile* profile) {
250 // TODO(estade): update schema so that multiple phone numbers are not 244 // TODO(estade): update schema so that multiple phone numbers are not
251 // associated per unique profile guid. Please refer https://crbug.com/497934. 245 // associated per unique profile guid. Please refer https://crbug.com/497934.
252 sql::Statement s(db->GetUniqueStatement( 246 sql::Statement s(db->GetUniqueStatement(
253 "SELECT guid, number " 247 "SELECT guid, number FROM autofill_profile_phones WHERE guid=? LIMIT 1"));
254 "FROM autofill_profile_phones "
255 "WHERE guid=?"
256 "LIMIT 1"));
257 s.BindString(0, profile->guid()); 248 s.BindString(0, profile->guid());
258 249
259 if (!s.is_valid()) 250 if (!s.is_valid())
260 return false; 251 return false;
261 252
262 if (s.Step()) { 253 if (s.Step()) {
263 DCHECK_EQ(profile->guid(), s.ColumnString(0)); 254 DCHECK_EQ(profile->guid(), s.ColumnString(0));
264 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(1)); 255 profile->SetRawInfo(PHONE_HOME_WHOLE_NUMBER, s.ColumnString16(1));
265 } 256 }
266 return s.Succeeded(); 257 return s.Succeeded();
(...skipping 12 matching lines...) Expand all
279 s.BindString16(3, profile.GetRawInfo(NAME_LAST)); 270 s.BindString16(3, profile.GetRawInfo(NAME_LAST));
280 s.BindString16(4, profile.GetRawInfo(NAME_FULL)); 271 s.BindString16(4, profile.GetRawInfo(NAME_FULL));
281 272
282 return s.Run(); 273 return s.Run();
283 } 274 }
284 275
285 bool AddAutofillProfileEmails(const AutofillProfile& profile, 276 bool AddAutofillProfileEmails(const AutofillProfile& profile,
286 sql::Connection* db) { 277 sql::Connection* db) {
287 // Add the new email. 278 // Add the new email.
288 sql::Statement s(db->GetUniqueStatement( 279 sql::Statement s(db->GetUniqueStatement(
289 "INSERT INTO autofill_profile_emails" 280 "INSERT INTO autofill_profile_emails (guid, email) VALUES (?,?)"));
290 " (guid, email) "
291 "VALUES (?,?)"));
292 s.BindString(0, profile.guid()); 281 s.BindString(0, profile.guid());
293 s.BindString16(1, profile.GetRawInfo(EMAIL_ADDRESS)); 282 s.BindString16(1, profile.GetRawInfo(EMAIL_ADDRESS));
294 283
295 return s.Run(); 284 return s.Run();
296 } 285 }
297 286
298 bool AddAutofillProfilePhones(const AutofillProfile& profile, 287 bool AddAutofillProfilePhones(const AutofillProfile& profile,
299 sql::Connection* db) { 288 sql::Connection* db) {
300 // Add the new number. 289 // Add the new number.
301 sql::Statement s(db->GetUniqueStatement( 290 sql::Statement s(db->GetUniqueStatement(
302 "INSERT INTO autofill_profile_phones" 291 "INSERT INTO autofill_profile_phones (guid, number) VALUES (?,?)"));
303 " (guid, number) "
304 "VALUES (?,?)"));
305 s.BindString(0, profile.guid()); 292 s.BindString(0, profile.guid());
306 s.BindString16(1, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER)); 293 s.BindString16(1, profile.GetRawInfo(PHONE_HOME_WHOLE_NUMBER));
307 294
308 return s.Run(); 295 return s.Run();
309 } 296 }
310 297
311 bool AddAutofillProfilePieces(const AutofillProfile& profile, 298 bool AddAutofillProfilePieces(const AutofillProfile& profile,
312 sql::Connection* db) { 299 sql::Connection* db) {
313 if (!AddAutofillProfileNames(profile, db)) 300 if (!AddAutofillProfileNames(profile, db))
314 return false; 301 return false;
(...skipping 29 matching lines...) Expand all
344 return s3.Run(); 331 return s3.Run();
345 } 332 }
346 333
347 WebDatabaseTable::TypeKey GetKey() { 334 WebDatabaseTable::TypeKey GetKey() {
348 // We just need a unique constant. Use the address of a static that 335 // We just need a unique constant. Use the address of a static that
349 // COMDAT folding won't touch in an optimizing linker. 336 // COMDAT folding won't touch in an optimizing linker.
350 static int table_key = 0; 337 static int table_key = 0;
351 return reinterpret_cast<void*>(&table_key); 338 return reinterpret_cast<void*>(&table_key);
352 } 339 }
353 340
354 time_t GetEndTime(const Time& end) { 341 time_t GetEndTime(const base::Time& end) {
355 if (end.is_null() || end == Time::Max()) 342 if (end.is_null() || end == base::Time::Max())
356 return std::numeric_limits<time_t>::max(); 343 return std::numeric_limits<time_t>::max();
357 344
358 return end.ToTimeT(); 345 return end.ToTimeT();
359 } 346 }
360 347
361 std::string ServerStatusEnumToString(CreditCard::ServerStatus status) { 348 std::string ServerStatusEnumToString(CreditCard::ServerStatus status) {
362 switch (status) { 349 switch (status) {
363 case CreditCard::EXPIRED: 350 case CreditCard::EXPIRED:
364 return "EXPIRED"; 351 return "EXPIRED";
365 352
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
405 392
406 // static 393 // static
407 const size_t AutofillTable::kMaxDataLength = 1024; 394 const size_t AutofillTable::kMaxDataLength = 1024;
408 395
409 AutofillTable::AutofillTable() 396 AutofillTable::AutofillTable()
410 : autofill_table_encryptor_( 397 : autofill_table_encryptor_(
411 AutofillTableEncryptorFactory::GetInstance()->Create()) { 398 AutofillTableEncryptorFactory::GetInstance()->Create()) {
412 DCHECK(autofill_table_encryptor_); 399 DCHECK(autofill_table_encryptor_);
413 } 400 }
414 401
415 AutofillTable::~AutofillTable() { 402 AutofillTable::~AutofillTable() {}
416 }
417 403
418 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) { 404 AutofillTable* AutofillTable::FromWebDatabase(WebDatabase* db) {
419 return static_cast<AutofillTable*>(db->GetTable(GetKey())); 405 return static_cast<AutofillTable*>(db->GetTable(GetKey()));
420 } 406 }
421 407
422 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const { 408 WebDatabaseTable::TypeKey AutofillTable::GetTypeKey() const {
423 return GetKey(); 409 return GetKey();
424 } 410 }
425 411
426 bool AutofillTable::CreateTablesIfNecessary() { 412 bool AutofillTable::CreateTablesIfNecessary() {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 return MigrateToVersion66AddCardBillingAddress(); 462 return MigrateToVersion66AddCardBillingAddress();
477 case 67: 463 case 67:
478 *update_compatible_version = false; 464 *update_compatible_version = false;
479 return MigrateToVersion67AddMaskedCardBillingAddress(); 465 return MigrateToVersion67AddMaskedCardBillingAddress();
480 case 70: 466 case 70:
481 *update_compatible_version = false; 467 *update_compatible_version = false;
482 return MigrateToVersion70AddSyncMetadata(); 468 return MigrateToVersion70AddSyncMetadata();
483 case 71: 469 case 71:
484 *update_compatible_version = true; 470 *update_compatible_version = true;
485 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata(); 471 return MigrateToVersion71AddHasConvertedAndBillingAddressIdMetadata();
472 case 72:
473 *update_compatible_version = true;
474 return MigrateToVersion72RenameCardTypeToIssuerNetwork();
486 } 475 }
487 return true; 476 return true;
488 } 477 }
489 478
490 bool AutofillTable::AddFormFieldValues( 479 bool AutofillTable::AddFormFieldValues(
491 const std::vector<FormFieldData>& elements, 480 const std::vector<FormFieldData>& elements,
492 std::vector<AutofillChange>* changes) { 481 std::vector<AutofillChange>* changes) {
493 return AddFormFieldValuesTime(elements, changes, AutofillClock::Now()); 482 return AddFormFieldValuesTime(elements, changes, AutofillClock::Now());
494 } 483 }
495 484
496 bool AutofillTable::AddFormFieldValue(const FormFieldData& element, 485 bool AutofillTable::AddFormFieldValue(const FormFieldData& element,
497 std::vector<AutofillChange>* changes) { 486 std::vector<AutofillChange>* changes) {
498 return AddFormFieldValueTime(element, changes, AutofillClock::Now()); 487 return AddFormFieldValueTime(element, changes, AutofillClock::Now());
499 } 488 }
500 489
501 bool AutofillTable::GetFormValuesForElementName( 490 bool AutofillTable::GetFormValuesForElementName(
502 const base::string16& name, 491 const base::string16& name,
503 const base::string16& prefix, 492 const base::string16& prefix,
504 std::vector<base::string16>* values, 493 std::vector<base::string16>* values,
505 int limit) { 494 int limit) {
506 DCHECK(values); 495 DCHECK(values);
507 bool succeeded = false; 496 bool succeeded = false;
508 497
509 if (prefix.empty()) { 498 if (prefix.empty()) {
510 sql::Statement s; 499 sql::Statement s;
511 s.Assign(db_->GetUniqueStatement( 500 s.Assign(
512 "SELECT value FROM autofill " 501 db_->GetUniqueStatement("SELECT value FROM autofill WHERE name = ? "
513 "WHERE name = ? " 502 "ORDER BY count DESC LIMIT ?"));
514 "ORDER BY count DESC "
515 "LIMIT ?"));
516 s.BindString16(0, name); 503 s.BindString16(0, name);
517 s.BindInt(1, limit); 504 s.BindInt(1, limit);
518 505
519 values->clear(); 506 values->clear();
520 while (s.Step()) 507 while (s.Step())
521 values->push_back(s.ColumnString16(0)); 508 values->push_back(s.ColumnString16(0));
522 509
523 succeeded = s.Succeeded(); 510 succeeded = s.Succeeded();
524 } else { 511 } else {
525 base::string16 prefix_lower = base::i18n::ToLower(prefix); 512 base::string16 prefix_lower = base::i18n::ToLower(prefix);
526 base::string16 next_prefix = prefix_lower; 513 base::string16 next_prefix = prefix_lower;
527 next_prefix.back()++; 514 next_prefix.back()++;
528 515
529 sql::Statement s1; 516 sql::Statement s1;
530 s1.Assign(db_->GetUniqueStatement( 517 s1.Assign(
531 "SELECT value FROM autofill " 518 db_->GetUniqueStatement("SELECT value FROM autofill "
532 "WHERE name = ? AND " 519 "WHERE name = ? AND "
533 "value_lower >= ? AND " 520 "value_lower >= ? AND "
534 "value_lower < ? " 521 "value_lower < ? "
535 "ORDER BY count DESC " 522 "ORDER BY count DESC "
536 "LIMIT ?")); 523 "LIMIT ?"));
537 s1.BindString16(0, name); 524 s1.BindString16(0, name);
538 s1.BindString16(1, prefix_lower); 525 s1.BindString16(1, prefix_lower);
539 s1.BindString16(2, next_prefix); 526 s1.BindString16(2, next_prefix);
540 s1.BindInt(3, limit); 527 s1.BindInt(3, limit);
541 528
542 values->clear(); 529 values->clear();
543 while (s1.Step()) 530 while (s1.Step())
544 values->push_back(s1.ColumnString16(0)); 531 values->push_back(s1.ColumnString16(0));
545 532
546 succeeded = s1.Succeeded(); 533 succeeded = s1.Succeeded();
547 534
548 if (IsFeatureSubstringMatchEnabled()) { 535 if (IsFeatureSubstringMatchEnabled()) {
549 sql::Statement s2; 536 sql::Statement s2;
550 s2.Assign(db_->GetUniqueStatement( 537 s2.Assign(db_->GetUniqueStatement(
551 "SELECT value FROM autofill " 538 "SELECT value FROM autofill "
552 "WHERE name = ? AND (" 539 "WHERE name = ? AND ("
553 " value LIKE '% ' || :prefix || '%' ESCAPE '!' OR " 540 " value LIKE '% ' || :prefix || '%' ESCAPE '!' OR "
554 " value LIKE '%.' || :prefix || '%' ESCAPE '!' OR " 541 " value LIKE '%.' || :prefix || '%' ESCAPE '!' OR "
555 " value LIKE '%,' || :prefix || '%' ESCAPE '!' OR " 542 " value LIKE '%,' || :prefix || '%' ESCAPE '!' OR "
556 " value LIKE '%-' || :prefix || '%' ESCAPE '!' OR " 543 " value LIKE '%-' || :prefix || '%' ESCAPE '!' OR "
557 " value LIKE '%@' || :prefix || '%' ESCAPE '!' OR " 544 " value LIKE '%@' || :prefix || '%' ESCAPE '!' OR "
558 " value LIKE '%!_' || :prefix || '%' ESCAPE '!' ) " 545 " value LIKE '%!_' || :prefix || '%' ESCAPE '!' ) "
559 "ORDER BY count DESC " 546 "ORDER BY count DESC "
560 "LIMIT ?")); 547 "LIMIT ?"));
561 548
562 s2.BindString16(0, name); 549 s2.BindString16(0, name);
563 // escaper as L'!' -> 0x21. 550 // escaper as L'!' -> 0x21.
564 s2.BindString16(1, Substitute(prefix_lower, ASCIIToUTF16("_%"), 0x21)); 551 s2.BindString16(1,
552 Substitute(prefix_lower, base::ASCIIToUTF16("_%"), 0x21));
565 s2.BindInt(2, limit); 553 s2.BindInt(2, limit);
566 while (s2.Step()) 554 while (s2.Step())
567 values->push_back(s2.ColumnString16(0)); 555 values->push_back(s2.ColumnString16(0));
568 556
569 succeeded &= s2.Succeeded(); 557 succeeded &= s2.Succeeded();
570 } 558 }
571 } 559 }
572 560
573 return succeeded; 561 return succeeded;
574 } 562 }
575 563
576 bool AutofillTable::RemoveFormElementsAddedBetween( 564 bool AutofillTable::RemoveFormElementsAddedBetween(
577 const Time& delete_begin, 565 const base::Time& delete_begin,
578 const Time& delete_end, 566 const base::Time& delete_end,
579 std::vector<AutofillChange>* changes) { 567 std::vector<AutofillChange>* changes) {
580 const time_t delete_begin_time_t = delete_begin.ToTimeT(); 568 const time_t delete_begin_time_t = delete_begin.ToTimeT();
581 const time_t delete_end_time_t = GetEndTime(delete_end); 569 const time_t delete_end_time_t = GetEndTime(delete_end);
582 570
583 // Query for the name, value, count, and access dates of all form elements 571 // Query for the name, value, count, and access dates of all form elements
584 // that were used between the given times. 572 // that were used between the given times.
585 sql::Statement s(db_->GetUniqueStatement( 573 sql::Statement s(db_->GetUniqueStatement(
586 "SELECT name, value, count, date_created, date_last_used FROM autofill " 574 "SELECT name, value, count, date_created, date_last_used FROM autofill "
587 "WHERE (date_created >= ? AND date_created < ?) OR " 575 "WHERE (date_created >= ? AND date_created < ?) OR "
588 " (date_last_used >= ? AND date_last_used < ?)")); 576 " (date_last_used >= ? AND date_last_used < ?)"));
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
622 // count_new = 1 + (date_last_used_new - date_created_new) / avg_delta 610 // count_new = 1 + (date_last_used_new - date_created_new) / avg_delta
623 // = 1 + ((count - 1) * 611 // = 1 + ((count - 1) *
624 // (date_last_used_new - date_created_new) / 612 // (date_last_used_new - date_created_new) /
625 // (date_last_used_orig - date_created_orig)) 613 // (date_last_used_orig - date_created_orig))
626 // Interpolating might not give a result that completely accurately 614 // Interpolating might not give a result that completely accurately
627 // reflects the user's history, but it's the best that can be done given 615 // reflects the user's history, but it's the best that can be done given
628 // the information in the database. 616 // the information in the database.
629 AutofillUpdate updated_entry; 617 AutofillUpdate updated_entry;
630 updated_entry.name = name; 618 updated_entry.name = name;
631 updated_entry.value = value; 619 updated_entry.value = value;
632 updated_entry.date_created = 620 updated_entry.date_created = date_created_time_t < delete_begin_time_t
633 date_created_time_t < delete_begin_time_t ? 621 ? date_created_time_t
634 date_created_time_t : 622 : delete_end_time_t;
635 delete_end_time_t; 623 updated_entry.date_last_used = date_last_used_time_t >= delete_end_time_t
636 updated_entry.date_last_used = 624 ? date_last_used_time_t
637 date_last_used_time_t >= delete_end_time_t ? 625 : delete_begin_time_t - 1;
638 date_last_used_time_t :
639 delete_begin_time_t - 1;
640 updated_entry.count = 626 updated_entry.count =
641 1 + 627 1 +
642 Round(1.0 * (count - 1) * 628 Round(1.0 * (count - 1) *
643 (updated_entry.date_last_used - updated_entry.date_created) / 629 (updated_entry.date_last_used - updated_entry.date_created) /
644 (date_last_used_time_t - date_created_time_t)); 630 (date_last_used_time_t - date_created_time_t));
645 updates.push_back(updated_entry); 631 updates.push_back(updated_entry);
646 } 632 }
647 633
648 tentative_changes.push_back( 634 tentative_changes.push_back(
649 AutofillChange(change_type, AutofillKey(name, value))); 635 AutofillChange(change_type, AutofillKey(name, value)));
650 } 636 }
651 if (!s.Succeeded()) 637 if (!s.Succeeded())
652 return false; 638 return false;
653 639
654 // As a single transaction, remove or update the elements appropriately. 640 // As a single transaction, remove or update the elements appropriately.
655 sql::Statement s_delete(db_->GetUniqueStatement( 641 sql::Statement s_delete(db_->GetUniqueStatement(
656 "DELETE FROM autofill WHERE date_created >= ? AND date_last_used < ?")); 642 "DELETE FROM autofill WHERE date_created >= ? AND date_last_used < ?"));
657 s_delete.BindInt64(0, delete_begin_time_t); 643 s_delete.BindInt64(0, delete_begin_time_t);
658 s_delete.BindInt64(1, delete_end_time_t); 644 s_delete.BindInt64(1, delete_end_time_t);
659 sql::Transaction transaction(db_); 645 sql::Transaction transaction(db_);
660 if (!transaction.Begin()) 646 if (!transaction.Begin())
661 return false; 647 return false;
662 if (!s_delete.Run()) 648 if (!s_delete.Run())
663 return false; 649 return false;
664 for (size_t i = 0; i < updates.size(); ++i) { 650 for (size_t i = 0; i < updates.size(); ++i) {
665 sql::Statement s_update(db_->GetUniqueStatement( 651 sql::Statement s_update(db_->GetUniqueStatement(
666 "UPDATE autofill SET date_created = ?, date_last_used = ?, count = ?" 652 "UPDATE autofill SET date_created = ?, date_last_used = ?, count = ?"
667 "WHERE name = ? AND value = ?")); 653 "WHERE name = ? AND value = ?"));
668 s_update.BindInt64(0, updates[i].date_created); 654 s_update.BindInt64(0, updates[i].date_created);
669 s_update.BindInt64(1, updates[i].date_last_used); 655 s_update.BindInt64(1, updates[i].date_last_used);
670 s_update.BindInt(2, updates[i].count); 656 s_update.BindInt(2, updates[i].count);
671 s_update.BindString16(3, updates[i].name); 657 s_update.BindString16(3, updates[i].name);
672 s_update.BindString16(4, updates[i].value); 658 s_update.BindString16(4, updates[i].value);
673 if (!s_update.Run()) 659 if (!s_update.Run())
674 return false; 660 return false;
675 } 661 }
676 if (!transaction.Commit()) 662 if (!transaction.Commit())
677 return false; 663 return false;
678 664
679 *changes = tentative_changes; 665 *changes = tentative_changes;
680 return true; 666 return true;
681 } 667 }
682 668
683 bool AutofillTable::RemoveExpiredFormElements( 669 bool AutofillTable::RemoveExpiredFormElements(
684 std::vector<AutofillChange>* changes) { 670 std::vector<AutofillChange>* changes) {
685 Time expiration_time = 671 base::Time expiration_time =
686 AutofillClock::Now() - TimeDelta::FromDays(kExpirationPeriodInDays); 672 AutofillClock::Now() - base::TimeDelta::FromDays(kExpirationPeriodInDays);
687 673
688 // Query for the name and value of all form elements that were last used 674 // Query for the name and value of all form elements that were last used
689 // before the |expiration_time|. 675 // before the |expiration_time|.
690 sql::Statement select_for_delete(db_->GetUniqueStatement( 676 sql::Statement select_for_delete(db_->GetUniqueStatement(
691 "SELECT name, value FROM autofill WHERE date_last_used < ?")); 677 "SELECT name, value FROM autofill WHERE date_last_used < ?"));
692 select_for_delete.BindInt64(0, expiration_time.ToTimeT()); 678 select_for_delete.BindInt64(0, expiration_time.ToTimeT());
693 std::vector<AutofillChange> tentative_changes; 679 std::vector<AutofillChange> tentative_changes;
694 while (select_for_delete.Step()) { 680 while (select_for_delete.Step()) {
695 base::string16 name = select_for_delete.ColumnString16(0); 681 base::string16 name = select_for_delete.ColumnString16(0);
696 base::string16 value = select_for_delete.ColumnString16(1); 682 base::string16 value = select_for_delete.ColumnString16(1);
697 tentative_changes.push_back( 683 tentative_changes.push_back(
698 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); 684 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value)));
699 } 685 }
700 686
701 if (!select_for_delete.Succeeded()) 687 if (!select_for_delete.Succeeded())
702 return false; 688 return false;
703 689
704 sql::Statement delete_data_statement(db_->GetUniqueStatement( 690 sql::Statement delete_data_statement(
705 "DELETE FROM autofill WHERE date_last_used < ?")); 691 db_->GetUniqueStatement("DELETE FROM autofill WHERE date_last_used < ?"));
706 delete_data_statement.BindInt64(0, expiration_time.ToTimeT()); 692 delete_data_statement.BindInt64(0, expiration_time.ToTimeT());
707 if (!delete_data_statement.Run()) 693 if (!delete_data_statement.Run())
708 return false; 694 return false;
709 695
710 *changes = tentative_changes; 696 *changes = tentative_changes;
711 return true; 697 return true;
712 } 698 }
713 699
714 bool AutofillTable::AddFormFieldValuesTime( 700 bool AutofillTable::AddFormFieldValuesTime(
715 const std::vector<FormFieldData>& elements, 701 const std::vector<FormFieldData>& elements,
716 std::vector<AutofillChange>* changes, 702 std::vector<AutofillChange>* changes,
717 Time time) { 703 base::Time time) {
718 // Only add one new entry for each unique element name. Use |seen_names| to 704 // Only add one new entry for each unique element name. Use |seen_names| to
719 // track this. Add up to |kMaximumUniqueNames| unique entries per form. 705 // track this. Add up to |kMaximumUniqueNames| unique entries per form.
720 const size_t kMaximumUniqueNames = 256; 706 const size_t kMaximumUniqueNames = 256;
721 std::set<base::string16> seen_names; 707 std::set<base::string16> seen_names;
722 bool result = true; 708 bool result = true;
723 for (const FormFieldData& element : elements) { 709 for (const FormFieldData& element : elements) {
724 if (seen_names.size() >= kMaximumUniqueNames) 710 if (seen_names.size() >= kMaximumUniqueNames)
725 break; 711 break;
726 if (base::ContainsKey(seen_names, element.name)) 712 if (base::ContainsKey(seen_names, element.name))
727 continue; 713 continue;
728 result = result && AddFormFieldValueTime(element, changes, time); 714 result = result && AddFormFieldValueTime(element, changes, time);
729 seen_names.insert(element.name); 715 seen_names.insert(element.name);
730 } 716 }
731 return result; 717 return result;
732 } 718 }
733 719
734 int AutofillTable::GetCountOfValuesContainedBetween( 720 int AutofillTable::GetCountOfValuesContainedBetween(const base::Time& begin,
735 const Time& begin, 721 const base::Time& end) {
736 const Time& end) {
737 const time_t begin_time_t = begin.ToTimeT(); 722 const time_t begin_time_t = begin.ToTimeT();
738 const time_t end_time_t = GetEndTime(end); 723 const time_t end_time_t = GetEndTime(end);
739 724
740 sql::Statement s(db_->GetUniqueStatement( 725 sql::Statement s(db_->GetUniqueStatement(
741 "SELECT COUNT(DISTINCT(value1)) FROM ( " 726 "SELECT COUNT(DISTINCT(value1)) FROM ( "
742 " SELECT value AS value1 FROM autofill " 727 " SELECT value AS value1 FROM autofill "
743 " WHERE NOT EXISTS ( " 728 " WHERE NOT EXISTS ( "
744 " SELECT value AS value2, date_created, date_last_used FROM autofill " 729 " SELECT value AS value2, date_created, date_last_used FROM autofill "
745 " WHERE value1 = value2 AND " 730 " WHERE value1 = value2 AND "
746 " (date_created < ? OR date_last_used >= ?)))")); 731 " (date_created < ? OR date_last_used >= ?)))"));
747 s.BindInt64(0, begin_time_t); 732 s.BindInt64(0, begin_time_t);
748 s.BindInt64(1, end_time_t); 733 s.BindInt64(1, end_time_t);
749 734
750 if (!s.Step()) { 735 if (!s.Step()) {
751 NOTREACHED(); 736 NOTREACHED();
752 return false; 737 return false;
753 } 738 }
754 return s.ColumnInt(0); 739 return s.ColumnInt(0);
755 } 740 }
756 741
757 bool AutofillTable::GetAllAutofillEntries(std::vector<AutofillEntry>* entries) { 742 bool AutofillTable::GetAllAutofillEntries(std::vector<AutofillEntry>* entries) {
758 sql::Statement s(db_->GetUniqueStatement( 743 sql::Statement s(db_->GetUniqueStatement(
759 "SELECT name, value, date_created, date_last_used FROM autofill")); 744 "SELECT name, value, date_created, date_last_used FROM autofill"));
760 745
761 while (s.Step()) { 746 while (s.Step()) {
762 base::string16 name = s.ColumnString16(0); 747 base::string16 name = s.ColumnString16(0);
763 base::string16 value = s.ColumnString16(1); 748 base::string16 value = s.ColumnString16(1);
764 Time date_created = Time::FromTimeT(s.ColumnInt64(2)); 749 base::Time date_created = base::Time::FromTimeT(s.ColumnInt64(2));
765 Time date_last_used = Time::FromTimeT(s.ColumnInt64(3)); 750 base::Time date_last_used = base::Time::FromTimeT(s.ColumnInt64(3));
766 entries->push_back( 751 entries->push_back(
767 AutofillEntry(AutofillKey(name, value), date_created, date_last_used)); 752 AutofillEntry(AutofillKey(name, value), date_created, date_last_used));
768 } 753 }
769 754
770 return s.Succeeded(); 755 return s.Succeeded();
771 } 756 }
772 757
773 bool AutofillTable::GetAutofillTimestamps(const base::string16& name, 758 bool AutofillTable::GetAutofillTimestamps(const base::string16& name,
774 const base::string16& value, 759 const base::string16& value,
775 Time* date_created, 760 base::Time* date_created,
776 Time* date_last_used) { 761 base::Time* date_last_used) {
777 sql::Statement s(db_->GetUniqueStatement( 762 sql::Statement s(db_->GetUniqueStatement(
778 "SELECT date_created, date_last_used FROM autofill " 763 "SELECT date_created, date_last_used FROM autofill "
779 "WHERE name = ? AND value = ?")); 764 "WHERE name = ? AND value = ?"));
780 s.BindString16(0, name); 765 s.BindString16(0, name);
781 s.BindString16(1, value); 766 s.BindString16(1, value);
782 if (!s.Step()) 767 if (!s.Step())
783 return false; 768 return false;
784 769
785 *date_created = Time::FromTimeT(s.ColumnInt64(0)); 770 *date_created = base::Time::FromTimeT(s.ColumnInt64(0));
786 *date_last_used = Time::FromTimeT(s.ColumnInt64(1)); 771 *date_last_used = base::Time::FromTimeT(s.ColumnInt64(1));
787 772
788 DCHECK(!s.Step()); 773 DCHECK(!s.Step());
789 return true; 774 return true;
790 } 775 }
791 776
792 bool AutofillTable::UpdateAutofillEntries( 777 bool AutofillTable::UpdateAutofillEntries(
793 const std::vector<AutofillEntry>& entries) { 778 const std::vector<AutofillEntry>& entries) {
794 if (entries.empty()) 779 if (entries.empty())
795 return true; 780 return true;
796 781
(...skipping 30 matching lines...) Expand all
827 // TODO(isherman): The counts column is currently synced implicitly as the 812 // TODO(isherman): The counts column is currently synced implicitly as the
828 // number of timestamps. Sync the value explicitly instead, since the DB now 813 // number of timestamps. Sync the value explicitly instead, since the DB now
829 // only saves the first and last timestamp, which makes counting timestamps 814 // only saves the first and last timestamp, which makes counting timestamps
830 // completely meaningless as a way to track frequency of usage. 815 // completely meaningless as a way to track frequency of usage.
831 s.BindInt(5, entry.date_last_used() == entry.date_created() ? 1 : 2); 816 s.BindInt(5, entry.date_last_used() == entry.date_created() ? 1 : 2);
832 return s.Run(); 817 return s.Run();
833 } 818 }
834 819
835 bool AutofillTable::AddFormFieldValueTime(const FormFieldData& element, 820 bool AutofillTable::AddFormFieldValueTime(const FormFieldData& element,
836 std::vector<AutofillChange>* changes, 821 std::vector<AutofillChange>* changes,
837 Time time) { 822 base::Time time) {
838 sql::Statement s_exists(db_->GetUniqueStatement( 823 sql::Statement s_exists(db_->GetUniqueStatement(
839 "SELECT COUNT(*) FROM autofill WHERE name = ? AND value = ?")); 824 "SELECT COUNT(*) FROM autofill WHERE name = ? AND value = ?"));
840 s_exists.BindString16(0, element.name); 825 s_exists.BindString16(0, element.name);
841 s_exists.BindString16(1, element.value); 826 s_exists.BindString16(1, element.value);
842 if (!s_exists.Step()) 827 if (!s_exists.Step())
843 return false; 828 return false;
844 829
845 bool already_exists = s_exists.ColumnInt(0) > 0; 830 bool already_exists = s_exists.ColumnInt(0) > 0;
846 if (already_exists) { 831 if (already_exists) {
847 sql::Statement s(db_->GetUniqueStatement( 832 sql::Statement s(db_->GetUniqueStatement(
(...skipping 20 matching lines...) Expand all
868 return false; 853 return false;
869 } 854 }
870 855
871 AutofillChange::Type change_type = 856 AutofillChange::Type change_type =
872 already_exists ? AutofillChange::UPDATE : AutofillChange::ADD; 857 already_exists ? AutofillChange::UPDATE : AutofillChange::ADD;
873 changes->push_back( 858 changes->push_back(
874 AutofillChange(change_type, AutofillKey(element.name, element.value))); 859 AutofillChange(change_type, AutofillKey(element.name, element.value)));
875 return true; 860 return true;
876 } 861 }
877 862
878
879 bool AutofillTable::RemoveFormElement(const base::string16& name, 863 bool AutofillTable::RemoveFormElement(const base::string16& name,
880 const base::string16& value) { 864 const base::string16& value) {
881 sql::Statement s(db_->GetUniqueStatement( 865 sql::Statement s(db_->GetUniqueStatement(
882 "DELETE FROM autofill WHERE name = ? AND value= ?")); 866 "DELETE FROM autofill WHERE name = ? AND value= ?"));
883 s.BindString16(0, name); 867 s.BindString16(0, name);
884 s.BindString16(1, value); 868 s.BindString16(1, value);
885 return s.Run(); 869 return s.Run();
886 } 870 }
887 871
888 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) { 872 bool AutofillTable::AddAutofillProfile(const AutofillProfile& profile) {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
931 915
932 return p; 916 return p;
933 } 917 }
934 918
935 bool AutofillTable::GetAutofillProfiles( 919 bool AutofillTable::GetAutofillProfiles(
936 std::vector<std::unique_ptr<AutofillProfile>>* profiles) { 920 std::vector<std::unique_ptr<AutofillProfile>>* profiles) {
937 DCHECK(profiles); 921 DCHECK(profiles);
938 profiles->clear(); 922 profiles->clear();
939 923
940 sql::Statement s(db_->GetUniqueStatement( 924 sql::Statement s(db_->GetUniqueStatement(
941 "SELECT guid " 925 "SELECT guid FROM autofill_profiles ORDER BY date_modified DESC, guid"));
942 "FROM autofill_profiles "
943 "ORDER BY date_modified DESC, guid"));
944 926
945 while (s.Step()) { 927 while (s.Step()) {
946 std::string guid = s.ColumnString(0); 928 std::string guid = s.ColumnString(0);
947 std::unique_ptr<AutofillProfile> profile = GetAutofillProfile(guid); 929 std::unique_ptr<AutofillProfile> profile = GetAutofillProfile(guid);
948 if (!profile) 930 if (!profile)
949 return false; 931 return false;
950 profiles->push_back(std::move(profile)); 932 profiles->push_back(std::move(profile));
951 } 933 }
952 934
953 return s.Succeeded(); 935 return s.Succeeded();
(...skipping 23 matching lines...) Expand all
977 "has_converted " 959 "has_converted "
978 "FROM server_addresses addresses " 960 "FROM server_addresses addresses "
979 "LEFT OUTER JOIN server_address_metadata USING (id)")); 961 "LEFT OUTER JOIN server_address_metadata USING (id)"));
980 962
981 while (s.Step()) { 963 while (s.Step()) {
982 int index = 0; 964 int index = 0;
983 std::unique_ptr<AutofillProfile> profile = 965 std::unique_ptr<AutofillProfile> profile =
984 base::MakeUnique<AutofillProfile>(AutofillProfile::SERVER_PROFILE, 966 base::MakeUnique<AutofillProfile>(AutofillProfile::SERVER_PROFILE,
985 s.ColumnString(index++)); 967 s.ColumnString(index++));
986 profile->set_use_count(s.ColumnInt64(index++)); 968 profile->set_use_count(s.ColumnInt64(index++));
987 profile->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); 969 profile->set_use_date(
970 base::Time::FromInternalValue(s.ColumnInt64(index++)));
988 // Modification date is not tracked for server profiles. Explicitly set it 971 // Modification date is not tracked for server profiles. Explicitly set it
989 // here to override the default value of AutofillClock::Now(). 972 // here to override the default value of AutofillClock::Now().
990 profile->set_modification_date(Time()); 973 profile->set_modification_date(base::Time());
991 974
992 base::string16 recipient_name = s.ColumnString16(index++); 975 base::string16 recipient_name = s.ColumnString16(index++);
993 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); 976 profile->SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
994 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); 977 profile->SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
995 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); 978 profile->SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
996 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); 979 profile->SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
997 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 980 profile->SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
998 s.ColumnString16(index++)); 981 s.ColumnString16(index++));
999 index++; // Skip address_4 which we haven't added to AutofillProfile yet. 982 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
1000 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++)); 983 profile->SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
(...skipping 16 matching lines...) Expand all
1017 return s.Succeeded(); 1000 return s.Succeeded();
1018 } 1001 }
1019 1002
1020 void AutofillTable::SetServerProfiles( 1003 void AutofillTable::SetServerProfiles(
1021 const std::vector<AutofillProfile>& profiles) { 1004 const std::vector<AutofillProfile>& profiles) {
1022 sql::Transaction transaction(db_); 1005 sql::Transaction transaction(db_);
1023 if (!transaction.Begin()) 1006 if (!transaction.Begin())
1024 return; 1007 return;
1025 1008
1026 // Delete all old ones first. 1009 // Delete all old ones first.
1027 sql::Statement delete_old(db_->GetUniqueStatement( 1010 sql::Statement delete_old(
1028 "DELETE FROM server_addresses")); 1011 db_->GetUniqueStatement("DELETE FROM server_addresses"));
1029 delete_old.Run(); 1012 delete_old.Run();
1030 1013
1031 sql::Statement insert(db_->GetUniqueStatement( 1014 sql::Statement insert(db_->GetUniqueStatement(
1032 "INSERT INTO server_addresses(" 1015 "INSERT INTO server_addresses("
1033 "id," 1016 "id,"
1034 "recipient_name," 1017 "recipient_name,"
1035 "company_name," 1018 "company_name,"
1036 "street_address," 1019 "street_address,"
1037 "address_1," // ADDRESS_HOME_STATE 1020 "address_1," // ADDRESS_HOME_STATE
1038 "address_2," // ADDRESS_HOME_CITY 1021 "address_2," // ADDRESS_HOME_CITY
1039 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 1022 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
1040 "address_4," // Not supported in AutofillProfile yet. 1023 "address_4," // Not supported in AutofillProfile yet.
1041 "postal_code," // ADDRESS_HOME_ZIP 1024 "postal_code," // ADDRESS_HOME_ZIP
1042 "sorting_code," // ADDRESS_HOME_SORTING_CODE 1025 "sorting_code," // ADDRESS_HOME_SORTING_CODE
1043 "country_code," // ADDRESS_HOME_COUNTRY 1026 "country_code," // ADDRESS_HOME_COUNTRY
1044 "phone_number," // PHONE_HOME_WHOLE_NUMBER 1027 "phone_number," // PHONE_HOME_WHOLE_NUMBER
1045 "language_code) " 1028 "language_code) "
1046 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); 1029 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
1047 for (const auto& profile : profiles) { 1030 for (const auto& profile : profiles) {
1048 DCHECK(profile.record_type() == AutofillProfile::SERVER_PROFILE); 1031 DCHECK(profile.record_type() == AutofillProfile::SERVER_PROFILE);
1049 1032
1050 int index = 0; 1033 int index = 0;
1051 insert.BindString(index++, profile.server_id()); 1034 insert.BindString(index++, profile.server_id());
1052 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL)); 1035 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
1053 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME)); 1036 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
1054 insert.BindString16(index++, 1037 insert.BindString16(index++,
1055 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 1038 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
(...skipping 11 matching lines...) Expand all
1067 insert.Run(); 1050 insert.Run();
1068 insert.Reset(true); 1051 insert.Reset(true);
1069 1052
1070 // Save the use count and use date of the profile. 1053 // Save the use count and use date of the profile.
1071 UpdateServerAddressMetadata(profile); 1054 UpdateServerAddressMetadata(profile);
1072 } 1055 }
1073 1056
1074 // Delete metadata that's no longer relevant. 1057 // Delete metadata that's no longer relevant.
1075 sql::Statement metadata_delete(db_->GetUniqueStatement( 1058 sql::Statement metadata_delete(db_->GetUniqueStatement(
1076 "DELETE FROM server_address_metadata WHERE id NOT IN " 1059 "DELETE FROM server_address_metadata WHERE id NOT IN "
1077 "(SELECT id FROM server_addresses)")); 1060 "(SELECT id FROM server_addresses)"));
1078 metadata_delete.Run(); 1061 metadata_delete.Run();
1079 1062
1080 transaction.Commit(); 1063 transaction.Commit();
1081 } 1064 }
1082 1065
1083 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) { 1066 bool AutofillTable::UpdateAutofillProfile(const AutofillProfile& profile) {
1084 DCHECK(base::IsValidGUID(profile.guid())); 1067 DCHECK(base::IsValidGUID(profile.guid()));
1085 1068
1086 // Don't update anything until the trash has been emptied. There may be 1069 // Don't update anything until the trash has been emptied. There may be
1087 // pending modifications to process. 1070 // pending modifications to process.
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1126 if (IsAutofillGUIDInTrash(guid)) { 1109 if (IsAutofillGUIDInTrash(guid)) {
1127 sql::Statement s_trash(db_->GetUniqueStatement( 1110 sql::Statement s_trash(db_->GetUniqueStatement(
1128 "DELETE FROM autofill_profiles_trash WHERE guid = ?")); 1111 "DELETE FROM autofill_profiles_trash WHERE guid = ?"));
1129 s_trash.BindString(0, guid); 1112 s_trash.BindString(0, guid);
1130 1113
1131 bool success = s_trash.Run(); 1114 bool success = s_trash.Run();
1132 DCHECK_GT(db_->GetLastChangeCount(), 0) << "Expected item in trash"; 1115 DCHECK_GT(db_->GetLastChangeCount(), 0) << "Expected item in trash";
1133 return success; 1116 return success;
1134 } 1117 }
1135 1118
1136 sql::Statement s(db_->GetUniqueStatement( 1119 sql::Statement s(
1137 "DELETE FROM autofill_profiles WHERE guid = ?")); 1120 db_->GetUniqueStatement("DELETE FROM autofill_profiles WHERE guid = ?"));
1138 s.BindString(0, guid); 1121 s.BindString(0, guid);
1139 1122
1140 if (!s.Run()) 1123 if (!s.Run())
1141 return false; 1124 return false;
1142 1125
1143 return RemoveAutofillProfilePieces(guid, db_); 1126 return RemoveAutofillProfilePieces(guid, db_);
1144 } 1127 }
1145 1128
1146 bool AutofillTable::ClearAutofillProfiles() { 1129 bool AutofillTable::ClearAutofillProfiles() {
1147 sql::Statement s1(db_->GetUniqueStatement( 1130 sql::Statement s1(db_->GetUniqueStatement("DELETE FROM autofill_profiles"));
1148 "DELETE FROM autofill_profiles"));
1149 1131
1150 if (!s1.Run()) 1132 if (!s1.Run())
1151 return false; 1133 return false;
1152 1134
1153 sql::Statement s2(db_->GetUniqueStatement( 1135 sql::Statement s2(
1154 "DELETE FROM autofill_profile_names")); 1136 db_->GetUniqueStatement("DELETE FROM autofill_profile_names"));
1155 1137
1156 if (!s2.Run()) 1138 if (!s2.Run())
1157 return false; 1139 return false;
1158 1140
1159 sql::Statement s3(db_->GetUniqueStatement( 1141 sql::Statement s3(
1160 "DELETE FROM autofill_profile_emails")); 1142 db_->GetUniqueStatement("DELETE FROM autofill_profile_emails"));
1161 1143
1162 if (!s3.Run()) 1144 if (!s3.Run())
1163 return false; 1145 return false;
1164 1146
1165 sql::Statement s4(db_->GetUniqueStatement( 1147 sql::Statement s4(
1166 "DELETE FROM autofill_profile_phones")); 1148 db_->GetUniqueStatement("DELETE FROM autofill_profile_phones"));
1167 1149
1168 return s4.Run(); 1150 return s4.Run();
1169 } 1151 }
1170 1152
1171 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) { 1153 bool AutofillTable::AddCreditCard(const CreditCard& credit_card) {
1172 sql::Statement s(db_->GetUniqueStatement( 1154 sql::Statement s(db_->GetUniqueStatement(
1173 "INSERT INTO credit_cards" 1155 "INSERT INTO credit_cards"
1174 "(guid, name_on_card, expiration_month, expiration_year, " 1156 "(guid, name_on_card, expiration_month, expiration_year, "
1175 " card_number_encrypted, use_count, use_date, date_modified, origin," 1157 " card_number_encrypted, use_count, use_date, date_modified, origin,"
1176 " billing_address_id)" 1158 " billing_address_id)"
1177 "VALUES (?,?,?,?,?,?,?,?,?,?)")); 1159 "VALUES (?,?,?,?,?,?,?,?,?,?)"));
1178 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s, 1160 BindCreditCardToStatement(credit_card, AutofillClock::Now(), &s,
1179 *autofill_table_encryptor_); 1161 *autofill_table_encryptor_);
1180 1162
1181 if (!s.Run()) 1163 if (!s.Run())
1182 return false; 1164 return false;
1183 1165
1184 DCHECK_GT(db_->GetLastChangeCount(), 0); 1166 DCHECK_GT(db_->GetLastChangeCount(), 0);
1185 return true; 1167 return true;
1186 } 1168 }
1187 1169
1188 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard( 1170 std::unique_ptr<CreditCard> AutofillTable::GetCreditCard(
1189 const std::string& guid) { 1171 const std::string& guid) {
1190 DCHECK(base::IsValidGUID(guid)); 1172 DCHECK(base::IsValidGUID(guid));
1191 sql::Statement s(db_->GetUniqueStatement( 1173 sql::Statement s(db_->GetUniqueStatement(
1192 "SELECT guid, name_on_card, expiration_month, expiration_year, " 1174 "SELECT guid, name_on_card, expiration_month, expiration_year, "
1193 "card_number_encrypted, use_count, use_date, date_modified, " 1175 "card_number_encrypted, use_count, use_date, date_modified, "
1194 "origin, billing_address_id " 1176 "origin, billing_address_id "
1195 "FROM credit_cards " 1177 "FROM credit_cards "
1196 "WHERE guid = ?")); 1178 "WHERE guid = ?"));
1197 s.BindString(0, guid); 1179 s.BindString(0, guid);
1198 1180
1199 if (!s.Step()) 1181 if (!s.Step())
1200 return std::unique_ptr<CreditCard>(); 1182 return std::unique_ptr<CreditCard>();
1201 1183
1202 return CreditCardFromStatement(s, *autofill_table_encryptor_); 1184 return CreditCardFromStatement(s, *autofill_table_encryptor_);
1203 } 1185 }
1204 1186
1205 bool AutofillTable::GetCreditCards( 1187 bool AutofillTable::GetCreditCards(
1206 std::vector<std::unique_ptr<CreditCard>>* credit_cards) { 1188 std::vector<std::unique_ptr<CreditCard>>* credit_cards) {
1207 DCHECK(credit_cards); 1189 DCHECK(credit_cards);
1208 credit_cards->clear(); 1190 credit_cards->clear();
1209 1191
1210 sql::Statement s(db_->GetUniqueStatement( 1192 sql::Statement s(db_->GetUniqueStatement(
1211 "SELECT guid " 1193 "SELECT guid FROM credit_cards ORDER BY date_modified DESC, guid"));
1212 "FROM credit_cards "
1213 "ORDER BY date_modified DESC, guid"));
1214 1194
1215 while (s.Step()) { 1195 while (s.Step()) {
1216 std::string guid = s.ColumnString(0); 1196 std::string guid = s.ColumnString(0);
1217 std::unique_ptr<CreditCard> credit_card = GetCreditCard(guid); 1197 std::unique_ptr<CreditCard> credit_card = GetCreditCard(guid);
1218 if (!credit_card) 1198 if (!credit_card)
1219 return false; 1199 return false;
1220 credit_cards->push_back(std::move(credit_card)); 1200 credit_cards->push_back(std::move(credit_card));
1221 } 1201 }
1222 1202
1223 return s.Succeeded(); 1203 return s.Succeeded();
1224 } 1204 }
1225 1205
1226 bool AutofillTable::GetServerCreditCards( 1206 bool AutofillTable::GetServerCreditCards(
1227 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const { 1207 std::vector<std::unique_ptr<CreditCard>>* credit_cards) const {
1228 credit_cards->clear(); 1208 credit_cards->clear();
1229 1209
1230 sql::Statement s(db_->GetUniqueStatement( 1210 sql::Statement s(db_->GetUniqueStatement(
1231 "SELECT " 1211 "SELECT "
1232 "card_number_encrypted, " // 0 1212 "card_number_encrypted, " // 0
1233 "last_four," // 1 1213 "last_four," // 1
1234 "masked.id," // 2 1214 "masked.id," // 2
1235 "metadata.use_count," // 3 1215 "metadata.use_count," // 3
1236 "metadata.use_date," // 4 1216 "metadata.use_date," // 4
1237 "type," // 5 1217 "network," // 5
1238 "status," // 6 1218 "status," // 6
1239 "name_on_card," // 7 1219 "name_on_card," // 7
1240 "exp_month," // 8 1220 "exp_month," // 8
1241 "exp_year," // 9 1221 "exp_year," // 9
1242 "metadata.billing_address_id " // 10 1222 "metadata.billing_address_id " // 10
1243 "FROM masked_credit_cards masked " 1223 "FROM masked_credit_cards masked "
1244 "LEFT OUTER JOIN unmasked_credit_cards USING (id) " 1224 "LEFT OUTER JOIN unmasked_credit_cards USING (id) "
1245 "LEFT OUTER JOIN server_card_metadata metadata USING (id)")); 1225 "LEFT OUTER JOIN server_card_metadata metadata USING (id)"));
1246 while (s.Step()) { 1226 while (s.Step()) {
1247 int index = 0; 1227 int index = 0;
1248 1228
1249 // If the card_number_encrypted field is nonempty, we can assume this card 1229 // If the card_number_encrypted field is nonempty, we can assume this card
1250 // is a full card, otherwise it's masked. 1230 // is a full card, otherwise it's masked.
1251 base::string16 full_card_number = 1231 base::string16 full_card_number =
1252 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_); 1232 UnencryptedCardFromColumn(s, index++, *autofill_table_encryptor_);
1253 base::string16 last_four = s.ColumnString16(index++); 1233 base::string16 last_four = s.ColumnString16(index++);
1254 CreditCard::RecordType record_type = full_card_number.empty() ? 1234 CreditCard::RecordType record_type = full_card_number.empty()
1255 CreditCard::MASKED_SERVER_CARD : 1235 ? CreditCard::MASKED_SERVER_CARD
1256 CreditCard::FULL_SERVER_CARD; 1236 : CreditCard::FULL_SERVER_CARD;
1257 std::string server_id = s.ColumnString(index++); 1237 std::string server_id = s.ColumnString(index++);
1258 std::unique_ptr<CreditCard> card = 1238 std::unique_ptr<CreditCard> card =
1259 base::MakeUnique<CreditCard>(record_type, server_id); 1239 base::MakeUnique<CreditCard>(record_type, server_id);
1260 card->SetRawInfo( 1240 card->SetRawInfo(CREDIT_CARD_NUMBER,
1261 CREDIT_CARD_NUMBER, 1241 record_type == CreditCard::MASKED_SERVER_CARD
1262 record_type == CreditCard::MASKED_SERVER_CARD ? last_four 1242 ? last_four
1263 : full_card_number); 1243 : full_card_number);
1264 card->set_use_count(s.ColumnInt64(index++)); 1244 card->set_use_count(s.ColumnInt64(index++));
1265 card->set_use_date(Time::FromInternalValue(s.ColumnInt64(index++))); 1245 card->set_use_date(base::Time::FromInternalValue(s.ColumnInt64(index++)));
1266 // Modification date is not tracked for server cards. Explicitly set it here 1246 // Modification date is not tracked for server cards. Explicitly set it here
1267 // to override the default value of AutofillClock::Now(). 1247 // to override the default value of AutofillClock::Now().
1268 card->set_modification_date(Time()); 1248 card->set_modification_date(base::Time());
1269 1249
1270 std::string card_type = s.ColumnString(index++); 1250 std::string card_network = s.ColumnString(index++);
1271 if (record_type == CreditCard::MASKED_SERVER_CARD) { 1251 if (record_type == CreditCard::MASKED_SERVER_CARD) {
1272 // The type must be set after setting the number to override the 1252 // The issuer network must be set after setting the number to override the
1273 // autodetected type. 1253 // autodetected issuer network.
1274 card->SetTypeForMaskedCard(card_type.c_str()); 1254 card->SetNetworkForMaskedCard(card_network.c_str());
1275 } else { 1255 } else {
1276 DCHECK_EQ(CreditCard::GetCreditCardType(full_card_number), card_type); 1256 DCHECK_EQ(CreditCard::GetCardNetwork(full_card_number), card_network);
1277 } 1257 }
1278 1258
1279 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++))); 1259 card->SetServerStatus(ServerStatusStringToEnum(s.ColumnString(index++)));
1280 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++)); 1260 card->SetRawInfo(CREDIT_CARD_NAME_FULL, s.ColumnString16(index++));
1281 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++)); 1261 card->SetRawInfo(CREDIT_CARD_EXP_MONTH, s.ColumnString16(index++));
1282 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++)); 1262 card->SetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, s.ColumnString16(index++));
1283 card->set_billing_address_id(s.ColumnString(index++)); 1263 card->set_billing_address_id(s.ColumnString(index++));
1284 credit_cards->push_back(std::move(card)); 1264 credit_cards->push_back(std::move(card));
1285 } 1265 }
1286 return s.Succeeded(); 1266 return s.Succeeded();
1287 } 1267 }
1288 1268
1289 void AutofillTable::AddMaskedCreditCards( 1269 void AutofillTable::AddMaskedCreditCards(
1290 const std::vector<CreditCard>& credit_cards) { 1270 const std::vector<CreditCard>& credit_cards) {
1291 DCHECK_GT(db_->transaction_nesting(), 0); 1271 DCHECK_GT(db_->transaction_nesting(), 0);
1292 sql::Statement masked_insert( 1272 sql::Statement masked_insert(
1293 db_->GetUniqueStatement("INSERT INTO masked_credit_cards(" 1273 db_->GetUniqueStatement("INSERT INTO masked_credit_cards("
1294 "id," // 0 1274 "id," // 0
1295 "type," // 1 1275 "network," // 1
1296 "status," // 2 1276 "status," // 2
1297 "name_on_card," // 3 1277 "name_on_card," // 3
1298 "last_four," // 4 1278 "last_four," // 4
1299 "exp_month," // 5 1279 "exp_month," // 5
1300 "exp_year)" // 6 1280 "exp_year)" // 6
1301 "VALUES (?,?,?,?,?,?,?)")); 1281 "VALUES (?,?,?,?,?,?,?)"));
1302 for (const CreditCard& card : credit_cards) { 1282 for (const CreditCard& card : credit_cards) {
1303 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type()); 1283 DCHECK_EQ(CreditCard::MASKED_SERVER_CARD, card.record_type());
1304 masked_insert.BindString(0, card.server_id()); 1284 masked_insert.BindString(0, card.server_id());
1305 masked_insert.BindString(1, card.type()); 1285 masked_insert.BindString(1, card.network());
1306 masked_insert.BindString(2, 1286 masked_insert.BindString(2,
1307 ServerStatusEnumToString(card.GetServerStatus())); 1287 ServerStatusEnumToString(card.GetServerStatus()));
1308 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL)); 1288 masked_insert.BindString16(3, card.GetRawInfo(CREDIT_CARD_NAME_FULL));
1309 masked_insert.BindString16(4, card.LastFourDigits()); 1289 masked_insert.BindString16(4, card.LastFourDigits());
1310 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH)); 1290 masked_insert.BindString16(5, card.GetRawInfo(CREDIT_CARD_EXP_MONTH));
1311 masked_insert.BindString16(6, 1291 masked_insert.BindString16(6,
1312 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); 1292 card.GetRawInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
1313 1293
1314 masked_insert.Run(); 1294 masked_insert.Run();
1315 masked_insert.Reset(true); 1295 masked_insert.Reset(true);
(...skipping 12 matching lines...) Expand all
1328 // Delete all old values. 1308 // Delete all old values.
1329 sql::Statement masked_delete( 1309 sql::Statement masked_delete(
1330 db_->GetUniqueStatement("DELETE FROM masked_credit_cards")); 1310 db_->GetUniqueStatement("DELETE FROM masked_credit_cards"));
1331 masked_delete.Run(); 1311 masked_delete.Run();
1332 1312
1333 AddMaskedCreditCards(credit_cards); 1313 AddMaskedCreditCards(credit_cards);
1334 1314
1335 // Delete all items in the unmasked table that aren't in the new set. 1315 // Delete all items in the unmasked table that aren't in the new set.
1336 sql::Statement unmasked_delete(db_->GetUniqueStatement( 1316 sql::Statement unmasked_delete(db_->GetUniqueStatement(
1337 "DELETE FROM unmasked_credit_cards WHERE id NOT IN " 1317 "DELETE FROM unmasked_credit_cards WHERE id NOT IN "
1338 "(SELECT id FROM masked_credit_cards)")); 1318 "(SELECT id FROM masked_credit_cards)"));
1339 unmasked_delete.Run(); 1319 unmasked_delete.Run();
1340 // Do the same for metadata. 1320 // Do the same for metadata.
1341 sql::Statement metadata_delete(db_->GetUniqueStatement( 1321 sql::Statement metadata_delete(db_->GetUniqueStatement(
1342 "DELETE FROM server_card_metadata WHERE id NOT IN " 1322 "DELETE FROM server_card_metadata WHERE id NOT IN "
1343 "(SELECT id FROM masked_credit_cards)")); 1323 "(SELECT id FROM masked_credit_cards)"));
1344 metadata_delete.Run(); 1324 metadata_delete.Run();
1345 1325
1346 transaction.Commit(); 1326 transaction.Commit();
1347 } 1327 }
1348 1328
1349 bool AutofillTable::AddFullServerCreditCard(const CreditCard& credit_card) { 1329 bool AutofillTable::AddFullServerCreditCard(const CreditCard& credit_card) {
1350 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type()); 1330 DCHECK_EQ(CreditCard::FULL_SERVER_CARD, credit_card.record_type());
1351 DCHECK(!credit_card.number().empty()); 1331 DCHECK(!credit_card.number().empty());
1352 DCHECK(!credit_card.server_id().empty()); 1332 DCHECK(!credit_card.server_id().empty());
1353 1333
1354 sql::Transaction transaction(db_); 1334 sql::Transaction transaction(db_);
1355 if (!transaction.Begin()) 1335 if (!transaction.Begin())
1356 return false; 1336 return false;
1357 1337
1358 // Make sure there aren't duplicates for this card. 1338 // Make sure there aren't duplicates for this card.
1359 DeleteFromUnmaskedCreditCards(credit_card.server_id()); 1339 DeleteFromUnmaskedCreditCards(credit_card.server_id());
1360 DeleteFromMaskedCreditCards(credit_card.server_id()); 1340 DeleteFromMaskedCreditCards(credit_card.server_id());
1361 1341
1362 CreditCard masked(credit_card); 1342 CreditCard masked(credit_card);
1363 masked.set_record_type(CreditCard::MASKED_SERVER_CARD); 1343 masked.set_record_type(CreditCard::MASKED_SERVER_CARD);
1364 masked.SetNumber(credit_card.LastFourDigits()); 1344 masked.SetNumber(credit_card.LastFourDigits());
1365 masked.RecordAndLogUse(); 1345 masked.RecordAndLogUse();
1366 DCHECK(!masked.type().empty()); 1346 DCHECK(!masked.network().empty());
1367 AddMaskedCreditCards({masked}); 1347 AddMaskedCreditCards({masked});
1368 1348
1369 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number()); 1349 AddUnmaskedCreditCard(credit_card.server_id(), credit_card.number());
1370 1350
1371 transaction.Commit(); 1351 transaction.Commit();
1372 1352
1373 return db_->GetLastChangeCount() > 0; 1353 return db_->GetLastChangeCount() > 0;
1374 } 1354 }
1375 1355
1376 void AutofillTable::AddUnmaskedCreditCard(const std::string& id, 1356 void AutofillTable::AddUnmaskedCreditCard(const std::string& id,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1430 return db_->GetLastChangeCount() > 0; 1410 return db_->GetLastChangeCount() > 0;
1431 } 1411 }
1432 1412
1433 bool AutofillTable::MaskServerCreditCard(const std::string& id) { 1413 bool AutofillTable::MaskServerCreditCard(const std::string& id) {
1434 return DeleteFromUnmaskedCreditCards(id); 1414 return DeleteFromUnmaskedCreditCards(id);
1435 } 1415 }
1436 1416
1437 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) { 1417 bool AutofillTable::UpdateServerCardMetadata(const CreditCard& credit_card) {
1438 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type()); 1418 DCHECK_NE(CreditCard::LOCAL_CARD, credit_card.record_type());
1439 1419
1440 sql::Statement remove(db_->GetUniqueStatement( 1420 sql::Statement remove(
1441 "DELETE FROM server_card_metadata WHERE id = ?")); 1421 db_->GetUniqueStatement("DELETE FROM server_card_metadata WHERE id = ?"));
1442 remove.BindString(0, credit_card.server_id()); 1422 remove.BindString(0, credit_card.server_id());
1443 remove.Run(); 1423 remove.Run();
1444 1424
1445 sql::Statement s( 1425 sql::Statement s(
1446 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, " 1426 db_->GetUniqueStatement("INSERT INTO server_card_metadata(use_count, "
1447 "use_date, billing_address_id, id)" 1427 "use_date, billing_address_id, id)"
1448 "VALUES (?,?,?,?)")); 1428 "VALUES (?,?,?,?)"));
1449 s.BindInt64(0, credit_card.use_count()); 1429 s.BindInt64(0, credit_card.use_count());
1450 s.BindInt64(1, credit_card.use_date().ToInternalValue()); 1430 s.BindInt64(1, credit_card.use_date().ToInternalValue());
1451 s.BindString(2, credit_card.billing_address_id()); 1431 s.BindString(2, credit_card.billing_address_id());
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
1483 transaction.Commit(); 1463 transaction.Commit();
1484 1464
1485 return db_->GetLastChangeCount() > 0; 1465 return db_->GetLastChangeCount() > 0;
1486 } 1466 }
1487 1467
1488 bool AutofillTable::ClearAllServerData() { 1468 bool AutofillTable::ClearAllServerData() {
1489 sql::Transaction transaction(db_); 1469 sql::Transaction transaction(db_);
1490 if (!transaction.Begin()) 1470 if (!transaction.Begin())
1491 return false; // Some error, nothing was changed. 1471 return false; // Some error, nothing was changed.
1492 1472
1493 sql::Statement masked(db_->GetUniqueStatement( 1473 sql::Statement masked(
1494 "DELETE FROM masked_credit_cards")); 1474 db_->GetUniqueStatement("DELETE FROM masked_credit_cards"));
1495 masked.Run(); 1475 masked.Run();
1496 bool changed = db_->GetLastChangeCount() > 0; 1476 bool changed = db_->GetLastChangeCount() > 0;
1497 1477
1498 sql::Statement unmasked(db_->GetUniqueStatement( 1478 sql::Statement unmasked(
1499 "DELETE FROM unmasked_credit_cards")); 1479 db_->GetUniqueStatement("DELETE FROM unmasked_credit_cards"));
1500 unmasked.Run(); 1480 unmasked.Run();
1501 changed |= db_->GetLastChangeCount() > 0; 1481 changed |= db_->GetLastChangeCount() > 0;
1502 1482
1503 sql::Statement addresses(db_->GetUniqueStatement( 1483 sql::Statement addresses(
1504 "DELETE FROM server_addresses")); 1484 db_->GetUniqueStatement("DELETE FROM server_addresses"));
1505 addresses.Run(); 1485 addresses.Run();
1506 changed |= db_->GetLastChangeCount() > 0; 1486 changed |= db_->GetLastChangeCount() > 0;
1507 1487
1508 sql::Statement card_metadata(db_->GetUniqueStatement( 1488 sql::Statement card_metadata(
1509 "DELETE FROM server_card_metadata")); 1489 db_->GetUniqueStatement("DELETE FROM server_card_metadata"));
1510 card_metadata.Run(); 1490 card_metadata.Run();
1511 changed |= db_->GetLastChangeCount() > 0; 1491 changed |= db_->GetLastChangeCount() > 0;
1512 1492
1513 sql::Statement address_metadata(db_->GetUniqueStatement( 1493 sql::Statement address_metadata(
1514 "DELETE FROM server_address_metadata")); 1494 db_->GetUniqueStatement("DELETE FROM server_address_metadata"));
1515 address_metadata.Run(); 1495 address_metadata.Run();
1516 changed |= db_->GetLastChangeCount() > 0; 1496 changed |= db_->GetLastChangeCount() > 0;
1517 1497
1518 transaction.Commit(); 1498 transaction.Commit();
1519 return changed; 1499 return changed;
1520 } 1500 }
1521 1501
1522 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) { 1502 bool AutofillTable::UpdateCreditCard(const CreditCard& credit_card) {
1523 DCHECK(base::IsValidGUID(credit_card.guid())); 1503 DCHECK(base::IsValidGUID(credit_card.guid()));
1524 1504
1525 std::unique_ptr<CreditCard> old_credit_card = 1505 std::unique_ptr<CreditCard> old_credit_card =
1526 GetCreditCard(credit_card.guid()); 1506 GetCreditCard(credit_card.guid());
1527 if (!old_credit_card) 1507 if (!old_credit_card)
1528 return false; 1508 return false;
1529 1509
1530 bool update_modification_date = *old_credit_card != credit_card; 1510 bool update_modification_date = *old_credit_card != credit_card;
1531 1511
1532 sql::Statement s(db_->GetUniqueStatement( 1512 sql::Statement s(db_->GetUniqueStatement(
1533 "UPDATE credit_cards " 1513 "UPDATE credit_cards "
1534 "SET guid=?, name_on_card=?, expiration_month=?," 1514 "SET guid=?, name_on_card=?, expiration_month=?,"
1535 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?," 1515 "expiration_year=?, card_number_encrypted=?, use_count=?, use_date=?,"
1536 "date_modified=?, origin=?, billing_address_id=?" 1516 "date_modified=?, origin=?, billing_address_id=?"
1537 "WHERE guid=?1")); 1517 "WHERE guid=?1"));
1538 BindCreditCardToStatement(credit_card, 1518 BindCreditCardToStatement(credit_card,
1539 update_modification_date 1519 update_modification_date
1540 ? AutofillClock::Now() 1520 ? AutofillClock::Now()
1541 : old_credit_card->modification_date(), 1521 : old_credit_card->modification_date(),
1542 &s, *autofill_table_encryptor_); 1522 &s, *autofill_table_encryptor_);
1543 1523
1544 bool result = s.Run(); 1524 bool result = s.Run();
1545 DCHECK_GT(db_->GetLastChangeCount(), 0); 1525 DCHECK_GT(db_->GetLastChangeCount(), 0);
1546 return result; 1526 return result;
1547 } 1527 }
1548 1528
1549 bool AutofillTable::RemoveCreditCard(const std::string& guid) { 1529 bool AutofillTable::RemoveCreditCard(const std::string& guid) {
1550 DCHECK(base::IsValidGUID(guid)); 1530 DCHECK(base::IsValidGUID(guid));
1551 sql::Statement s(db_->GetUniqueStatement( 1531 sql::Statement s(
1552 "DELETE FROM credit_cards WHERE guid = ?")); 1532 db_->GetUniqueStatement("DELETE FROM credit_cards WHERE guid = ?"));
1553 s.BindString(0, guid); 1533 s.BindString(0, guid);
1554 1534
1555 return s.Run(); 1535 return s.Run();
1556 } 1536 }
1557 1537
1558 bool AutofillTable::RemoveAutofillDataModifiedBetween( 1538 bool AutofillTable::RemoveAutofillDataModifiedBetween(
1559 const Time& delete_begin, 1539 const base::Time& delete_begin,
1560 const Time& delete_end, 1540 const base::Time& delete_end,
1561 std::vector<std::string>* profile_guids, 1541 std::vector<std::string>* profile_guids,
1562 std::vector<std::string>* credit_card_guids) { 1542 std::vector<std::string>* credit_card_guids) {
1563 DCHECK(delete_end.is_null() || delete_begin < delete_end); 1543 DCHECK(delete_end.is_null() || delete_begin < delete_end);
1564 1544
1565 time_t delete_begin_t = delete_begin.ToTimeT(); 1545 time_t delete_begin_t = delete_begin.ToTimeT();
1566 time_t delete_end_t = GetEndTime(delete_end); 1546 time_t delete_end_t = GetEndTime(delete_end);
1567 1547
1568 // Remember Autofill profiles in the time range. 1548 // Remember Autofill profiles in the time range.
1569 sql::Statement s_profiles_get(db_->GetUniqueStatement( 1549 sql::Statement s_profiles_get(db_->GetUniqueStatement(
1570 "SELECT guid FROM autofill_profiles " 1550 "SELECT guid FROM autofill_profiles "
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
1608 // Remove Autofill credit cards in the time range. 1588 // Remove Autofill credit cards in the time range.
1609 sql::Statement s_credit_cards(db_->GetUniqueStatement( 1589 sql::Statement s_credit_cards(db_->GetUniqueStatement(
1610 "DELETE FROM credit_cards " 1590 "DELETE FROM credit_cards "
1611 "WHERE date_modified >= ? AND date_modified < ?")); 1591 "WHERE date_modified >= ? AND date_modified < ?"));
1612 s_credit_cards.BindInt64(0, delete_begin_t); 1592 s_credit_cards.BindInt64(0, delete_begin_t);
1613 s_credit_cards.BindInt64(1, delete_end_t); 1593 s_credit_cards.BindInt64(1, delete_end_t);
1614 if (!s_credit_cards.Run()) 1594 if (!s_credit_cards.Run())
1615 return false; 1595 return false;
1616 1596
1617 // Remove unmasked credit cards in the time range. 1597 // Remove unmasked credit cards in the time range.
1618 sql::Statement s_unmasked_cards(db_->GetUniqueStatement( 1598 sql::Statement s_unmasked_cards(
1619 "DELETE FROM unmasked_credit_cards " 1599 db_->GetUniqueStatement("DELETE FROM unmasked_credit_cards "
1620 "WHERE unmask_date >= ? AND unmask_date < ?")); 1600 "WHERE unmask_date >= ? AND unmask_date < ?"));
1621 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue()); 1601 s_unmasked_cards.BindInt64(0, delete_begin.ToInternalValue());
1622 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue()); 1602 s_unmasked_cards.BindInt64(1, delete_end.ToInternalValue());
1623 return s_unmasked_cards.Run(); 1603 return s_unmasked_cards.Run();
1624 } 1604 }
1625 1605
1626 bool AutofillTable::RemoveOriginURLsModifiedBetween( 1606 bool AutofillTable::RemoveOriginURLsModifiedBetween(
1627 const Time& delete_begin, 1607 const base::Time& delete_begin,
1628 const Time& delete_end, 1608 const base::Time& delete_end,
1629 std::vector<std::unique_ptr<AutofillProfile>>* profiles) { 1609 std::vector<std::unique_ptr<AutofillProfile>>* profiles) {
1630 DCHECK(delete_end.is_null() || delete_begin < delete_end); 1610 DCHECK(delete_end.is_null() || delete_begin < delete_end);
1631 1611
1632 time_t delete_begin_t = delete_begin.ToTimeT(); 1612 time_t delete_begin_t = delete_begin.ToTimeT();
1633 time_t delete_end_t = GetEndTime(delete_end); 1613 time_t delete_end_t = GetEndTime(delete_end);
1634 1614
1635 // Remember Autofill profiles with URL origins in the time range. 1615 // Remember Autofill profiles with URL origins in the time range.
1636 sql::Statement s_profiles_get(db_->GetUniqueStatement( 1616 sql::Statement s_profiles_get(db_->GetUniqueStatement(
1637 "SELECT guid, origin FROM autofill_profiles " 1617 "SELECT guid, origin FROM autofill_profiles "
1638 "WHERE date_modified >= ? AND date_modified < ?")); 1618 "WHERE date_modified >= ? AND date_modified < ?"));
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
1690 return false; 1670 return false;
1691 } 1671 }
1692 1672
1693 return true; 1673 return true;
1694 } 1674 }
1695 1675
1696 bool AutofillTable::GetAutofillProfilesInTrash( 1676 bool AutofillTable::GetAutofillProfilesInTrash(
1697 std::vector<std::string>* guids) { 1677 std::vector<std::string>* guids) {
1698 guids->clear(); 1678 guids->clear();
1699 1679
1700 sql::Statement s(db_->GetUniqueStatement( 1680 sql::Statement s(
1701 "SELECT guid " 1681 db_->GetUniqueStatement("SELECT guid FROM autofill_profiles_trash"));
1702 "FROM autofill_profiles_trash"));
1703 1682
1704 while (s.Step()) { 1683 while (s.Step()) {
1705 std::string guid = s.ColumnString(0); 1684 std::string guid = s.ColumnString(0);
1706 guids->push_back(guid); 1685 guids->push_back(guid);
1707 } 1686 }
1708 1687
1709 return s.Succeeded(); 1688 return s.Succeeded();
1710 } 1689 }
1711 1690
1712 bool AutofillTable::EmptyAutofillProfilesTrash() { 1691 bool AutofillTable::EmptyAutofillProfilesTrash() {
1713 sql::Statement s(db_->GetUniqueStatement( 1692 sql::Statement s(
1714 "DELETE FROM autofill_profiles_trash")); 1693 db_->GetUniqueStatement("DELETE FROM autofill_profiles_trash"));
1715 1694
1716 return s.Run(); 1695 return s.Run();
1717 } 1696 }
1718 1697
1719
1720 bool AutofillTable::AddAutofillGUIDToTrash(const std::string& guid) { 1698 bool AutofillTable::AddAutofillGUIDToTrash(const std::string& guid) {
1721 sql::Statement s(db_->GetUniqueStatement( 1699 sql::Statement s(db_->GetUniqueStatement(
1722 "INSERT INTO autofill_profiles_trash" 1700 "INSERT INTO autofill_profiles_trash (guid) VALUES (?)"));
1723 " (guid) "
1724 "VALUES (?)"));
1725 s.BindString(0, guid); 1701 s.BindString(0, guid);
1726 1702
1727 return s.Run(); 1703 return s.Run();
1728 } 1704 }
1729 1705
1730 bool AutofillTable::IsAutofillProfilesTrashEmpty() { 1706 bool AutofillTable::IsAutofillProfilesTrashEmpty() {
1731 sql::Statement s(db_->GetUniqueStatement( 1707 sql::Statement s(
1732 "SELECT guid " 1708 db_->GetUniqueStatement("SELECT guid FROM autofill_profiles_trash"));
1733 "FROM autofill_profiles_trash"));
1734 1709
1735 return !s.Step(); 1710 return !s.Step();
1736 } 1711 }
1737 1712
1738 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) { 1713 bool AutofillTable::IsAutofillGUIDInTrash(const std::string& guid) {
1739 sql::Statement s(db_->GetUniqueStatement( 1714 sql::Statement s(db_->GetUniqueStatement(
1740 "SELECT guid " 1715 "SELECT guid FROM autofill_profiles_trash WHERE guid = ?"));
1741 "FROM autofill_profiles_trash "
1742 "WHERE guid = ?"));
1743 s.BindString(0, guid); 1716 s.BindString(0, guid);
1744 1717
1745 return s.Step(); 1718 return s.Step();
1746 } 1719 }
1747 1720
1748 bool AutofillTable::GetAllSyncMetadata(syncer::ModelType model_type, 1721 bool AutofillTable::GetAllSyncMetadata(syncer::ModelType model_type,
1749 syncer::MetadataBatch* metadata_batch) { 1722 syncer::MetadataBatch* metadata_batch) {
1750 DCHECK_EQ(model_type, syncer::AUTOFILL) 1723 DCHECK_EQ(model_type, syncer::AUTOFILL)
1751 << "Only the AUTOFILL model type is supported"; 1724 << "Only the AUTOFILL model type is supported";
1752 DCHECK(metadata_batch); 1725 DCHECK(metadata_batch);
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
1865 "name VARCHAR, " 1838 "name VARCHAR, "
1866 "value VARCHAR, " 1839 "value VARCHAR, "
1867 "value_lower VARCHAR, " 1840 "value_lower VARCHAR, "
1868 "date_created INTEGER DEFAULT 0, " 1841 "date_created INTEGER DEFAULT 0, "
1869 "date_last_used INTEGER DEFAULT 0, " 1842 "date_last_used INTEGER DEFAULT 0, "
1870 "count INTEGER DEFAULT 1, " 1843 "count INTEGER DEFAULT 1, "
1871 "PRIMARY KEY (name, value))") || 1844 "PRIMARY KEY (name, value))") ||
1872 !db_->Execute("CREATE INDEX autofill_name ON autofill (name)") || 1845 !db_->Execute("CREATE INDEX autofill_name ON autofill (name)") ||
1873 !db_->Execute("CREATE INDEX autofill_name_value_lower ON " 1846 !db_->Execute("CREATE INDEX autofill_name_value_lower ON "
1874 "autofill (name, value_lower)")) { 1847 "autofill (name, value_lower)")) {
1875 NOTREACHED(); 1848 NOTREACHED();
1876 return false; 1849 return false;
1877 } 1850 }
1878 } 1851 }
1879 return true; 1852 return true;
1880 } 1853 }
1881 1854
1882 bool AutofillTable::InitCreditCardsTable() { 1855 bool AutofillTable::InitCreditCardsTable() {
1883 if (!db_->DoesTableExist("credit_cards")) { 1856 if (!db_->DoesTableExist("credit_cards")) {
1884 if (!db_->Execute("CREATE TABLE credit_cards ( " 1857 if (!db_->Execute("CREATE TABLE credit_cards ( "
1885 "guid VARCHAR PRIMARY KEY, " 1858 "guid VARCHAR PRIMARY KEY, "
1886 "name_on_card VARCHAR, " 1859 "name_on_card VARCHAR, "
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
1973 } 1946 }
1974 return true; 1947 return true;
1975 } 1948 }
1976 1949
1977 bool AutofillTable::InitMaskedCreditCardsTable() { 1950 bool AutofillTable::InitMaskedCreditCardsTable() {
1978 if (!db_->DoesTableExist("masked_credit_cards")) { 1951 if (!db_->DoesTableExist("masked_credit_cards")) {
1979 if (!db_->Execute("CREATE TABLE masked_credit_cards (" 1952 if (!db_->Execute("CREATE TABLE masked_credit_cards ("
1980 "id VARCHAR," 1953 "id VARCHAR,"
1981 "status VARCHAR," 1954 "status VARCHAR,"
1982 "name_on_card VARCHAR," 1955 "name_on_card VARCHAR,"
1983 "type VARCHAR," 1956 "network VARCHAR,"
1984 "last_four VARCHAR," 1957 "last_four VARCHAR,"
1985 "exp_month INTEGER DEFAULT 0," 1958 "exp_month INTEGER DEFAULT 0,"
1986 "exp_year INTEGER DEFAULT 0)")) { 1959 "exp_year INTEGER DEFAULT 0)")) {
1987 NOTREACHED(); 1960 NOTREACHED();
1988 return false; 1961 return false;
1989 } 1962 }
1990 } 1963 }
1991 return true; 1964 return true;
1992 } 1965 }
1993 1966
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2028 "id VARCHAR," 2001 "id VARCHAR,"
2029 "company_name VARCHAR," 2002 "company_name VARCHAR,"
2030 "street_address VARCHAR," 2003 "street_address VARCHAR,"
2031 "address_1 VARCHAR," 2004 "address_1 VARCHAR,"
2032 "address_2 VARCHAR," 2005 "address_2 VARCHAR,"
2033 "address_3 VARCHAR," 2006 "address_3 VARCHAR,"
2034 "address_4 VARCHAR," 2007 "address_4 VARCHAR,"
2035 "postal_code VARCHAR," 2008 "postal_code VARCHAR,"
2036 "sorting_code VARCHAR," 2009 "sorting_code VARCHAR,"
2037 "country_code VARCHAR," 2010 "country_code VARCHAR,"
2038 "language_code VARCHAR, " // Space required. 2011 "language_code VARCHAR, " // Space required.
2039 "recipient_name VARCHAR, " // Ditto. 2012 "recipient_name VARCHAR, " // Ditto.
2040 "phone_number VARCHAR)")) { 2013 "phone_number VARCHAR)")) {
2041 NOTREACHED(); 2014 NOTREACHED();
2042 return false; 2015 return false;
2043 } 2016 }
2044 } 2017 }
2045 return true; 2018 return true;
2046 } 2019 }
2047 2020
2048 bool AutofillTable::InitServerAddressMetadataTable() { 2021 bool AutofillTable::InitServerAddressMetadataTable() {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
2236 return false; 2209 return false;
2237 } 2210 }
2238 2211
2239 // Create indices on the new table, for fast lookups. 2212 // Create indices on the new table, for fast lookups.
2240 if (!db_->Execute("CREATE INDEX autofill_name ON autofill (name)") || 2213 if (!db_->Execute("CREATE INDEX autofill_name ON autofill (name)") ||
2241 !db_->Execute("CREATE INDEX autofill_name_value_lower ON " 2214 !db_->Execute("CREATE INDEX autofill_name_value_lower ON "
2242 "autofill (name, value_lower)")) { 2215 "autofill (name, value_lower)")) {
2243 return false; 2216 return false;
2244 } 2217 }
2245 2218
2246
2247 return transaction.Commit(); 2219 return transaction.Commit();
2248 } 2220 }
2249 2221
2250 bool AutofillTable::MigrateToVersion56AddProfileLanguageCodeForFormatting() { 2222 bool AutofillTable::MigrateToVersion56AddProfileLanguageCodeForFormatting() {
2251 return db_->Execute("ALTER TABLE autofill_profiles " 2223 return db_->Execute(
2252 "ADD COLUMN language_code VARCHAR"); 2224 "ALTER TABLE autofill_profiles ADD COLUMN language_code VARCHAR");
2253 } 2225 }
2254 2226
2255 bool AutofillTable::MigrateToVersion57AddFullNameField() { 2227 bool AutofillTable::MigrateToVersion57AddFullNameField() {
2256 return db_->Execute("ALTER TABLE autofill_profile_names " 2228 return db_->Execute(
2257 "ADD COLUMN full_name VARCHAR"); 2229 "ALTER TABLE autofill_profile_names ADD COLUMN full_name VARCHAR");
2258 } 2230 }
2259 2231
2260 bool AutofillTable::MigrateToVersion60AddServerCards() { 2232 bool AutofillTable::MigrateToVersion60AddServerCards() {
2261 sql::Transaction transaction(db_); 2233 sql::Transaction transaction(db_);
2262 if (!transaction.Begin()) 2234 if (!transaction.Begin())
2263 return false; 2235 return false;
2264 2236
2265 if (!db_->DoesTableExist("masked_credit_cards") && 2237 if (!db_->DoesTableExist("masked_credit_cards") &&
2266 !db_->Execute("CREATE TABLE masked_credit_cards (" 2238 !db_->Execute("CREATE TABLE masked_credit_cards ("
2267 "id VARCHAR," 2239 "id VARCHAR,"
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
2411 !db_->Execute("CREATE TABLE server_address_metadata (" 2383 !db_->Execute("CREATE TABLE server_address_metadata ("
2412 "id VARCHAR NOT NULL," 2384 "id VARCHAR NOT NULL,"
2413 "use_count INTEGER NOT NULL DEFAULT 0, " 2385 "use_count INTEGER NOT NULL DEFAULT 0, "
2414 "use_date INTEGER NOT NULL DEFAULT 0)")) { 2386 "use_date INTEGER NOT NULL DEFAULT 0)")) {
2415 return false; 2387 return false;
2416 } 2388 }
2417 2389
2418 // Get existing server addresses and generate IDs for them. 2390 // Get existing server addresses and generate IDs for them.
2419 sql::Statement s(db_->GetUniqueStatement( 2391 sql::Statement s(db_->GetUniqueStatement(
2420 "SELECT " 2392 "SELECT "
2421 "id," 2393 "id,"
2422 "recipient_name," 2394 "recipient_name,"
2423 "company_name," 2395 "company_name,"
2424 "street_address," 2396 "street_address,"
2425 "address_1," // ADDRESS_HOME_STATE 2397 "address_1," // ADDRESS_HOME_STATE
2426 "address_2," // ADDRESS_HOME_CITY 2398 "address_2," // ADDRESS_HOME_CITY
2427 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 2399 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2428 "address_4," // Not supported in AutofillProfile yet. 2400 "address_4," // Not supported in AutofillProfile yet.
2429 "postal_code," // ADDRESS_HOME_ZIP 2401 "postal_code," // ADDRESS_HOME_ZIP
2430 "sorting_code," // ADDRESS_HOME_SORTING_CODE 2402 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2431 "country_code," // ADDRESS_HOME_COUNTRY 2403 "country_code," // ADDRESS_HOME_COUNTRY
2432 "phone_number," // PHONE_HOME_WHOLE_NUMBER 2404 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2433 "language_code " 2405 "language_code "
2434 "FROM server_addresses addresses")); 2406 "FROM server_addresses addresses"));
2435 std::vector<AutofillProfile> profiles; 2407 std::vector<AutofillProfile> profiles;
2436 while (s.Step()) { 2408 while (s.Step()) {
2437 int index = 0; 2409 int index = 0;
2438 AutofillProfile profile( 2410 AutofillProfile profile(AutofillProfile::SERVER_PROFILE,
2439 AutofillProfile::SERVER_PROFILE, s.ColumnString(index++)); 2411 s.ColumnString(index++));
2440 2412
2441 base::string16 recipient_name = s.ColumnString16(index++); 2413 base::string16 recipient_name = s.ColumnString16(index++);
2442 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++)); 2414 profile.SetRawInfo(COMPANY_NAME, s.ColumnString16(index++));
2443 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++)); 2415 profile.SetRawInfo(ADDRESS_HOME_STREET_ADDRESS, s.ColumnString16(index++));
2444 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++)); 2416 profile.SetRawInfo(ADDRESS_HOME_STATE, s.ColumnString16(index++));
2445 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++)); 2417 profile.SetRawInfo(ADDRESS_HOME_CITY, s.ColumnString16(index++));
2446 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY, 2418 profile.SetRawInfo(ADDRESS_HOME_DEPENDENT_LOCALITY,
2447 s.ColumnString16(index++)); 2419 s.ColumnString16(index++));
2448 index++; // Skip address_4 which we haven't added to AutofillProfile yet. 2420 index++; // Skip address_4 which we haven't added to AutofillProfile yet.
2449 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++)); 2421 profile.SetRawInfo(ADDRESS_HOME_ZIP, s.ColumnString16(index++));
2450 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++)); 2422 profile.SetRawInfo(ADDRESS_HOME_SORTING_CODE, s.ColumnString16(index++));
2451 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++)); 2423 profile.SetRawInfo(ADDRESS_HOME_COUNTRY, s.ColumnString16(index++));
2452 base::string16 phone_number = s.ColumnString16(index++); 2424 base::string16 phone_number = s.ColumnString16(index++);
2453 profile.set_language_code(s.ColumnString(index++)); 2425 profile.set_language_code(s.ColumnString(index++));
2454 profile.SetInfo(AutofillType(NAME_FULL), recipient_name, 2426 profile.SetInfo(AutofillType(NAME_FULL), recipient_name,
2455 profile.language_code()); 2427 profile.language_code());
2456 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number, 2428 profile.SetInfo(AutofillType(PHONE_HOME_WHOLE_NUMBER), phone_number,
2457 profile.language_code()); 2429 profile.language_code());
2458 profile.GenerateServerProfileIdentifier(); 2430 profile.GenerateServerProfileIdentifier();
2459 profiles.push_back(profile); 2431 profiles.push_back(profile);
2460 } 2432 }
2461 2433
2462 // Reinsert with the generated IDs. 2434 // Reinsert with the generated IDs.
2463 sql::Statement delete_old(db_->GetUniqueStatement( 2435 sql::Statement delete_old(
2464 "DELETE FROM server_addresses")); 2436 db_->GetUniqueStatement("DELETE FROM server_addresses"));
2465 delete_old.Run(); 2437 delete_old.Run();
2466 2438
2467 sql::Statement insert(db_->GetUniqueStatement( 2439 sql::Statement insert(db_->GetUniqueStatement(
2468 "INSERT INTO server_addresses(" 2440 "INSERT INTO server_addresses("
2469 "id," 2441 "id,"
2470 "recipient_name," 2442 "recipient_name,"
2471 "company_name," 2443 "company_name,"
2472 "street_address," 2444 "street_address,"
2473 "address_1," // ADDRESS_HOME_STATE 2445 "address_1," // ADDRESS_HOME_STATE
2474 "address_2," // ADDRESS_HOME_CITY 2446 "address_2," // ADDRESS_HOME_CITY
2475 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY 2447 "address_3," // ADDRESS_HOME_DEPENDENT_LOCALITY
2476 "address_4," // Not supported in AutofillProfile yet. 2448 "address_4," // Not supported in AutofillProfile yet.
2477 "postal_code," // ADDRESS_HOME_ZIP 2449 "postal_code," // ADDRESS_HOME_ZIP
2478 "sorting_code," // ADDRESS_HOME_SORTING_CODE 2450 "sorting_code," // ADDRESS_HOME_SORTING_CODE
2479 "country_code," // ADDRESS_HOME_COUNTRY 2451 "country_code," // ADDRESS_HOME_COUNTRY
2480 "phone_number," // PHONE_HOME_WHOLE_NUMBER 2452 "phone_number," // PHONE_HOME_WHOLE_NUMBER
2481 "language_code) " 2453 "language_code) "
2482 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)")); 2454 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)"));
2483 for (const AutofillProfile& profile : profiles) { 2455 for (const AutofillProfile& profile : profiles) {
2484 int index = 0; 2456 int index = 0;
2485 insert.BindString(index++, profile.server_id()); 2457 insert.BindString(index++, profile.server_id());
2486 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL)); 2458 insert.BindString16(index++, profile.GetRawInfo(NAME_FULL));
2487 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME)); 2459 insert.BindString16(index++, profile.GetRawInfo(COMPANY_NAME));
2488 insert.BindString16(index++, 2460 insert.BindString16(index++,
2489 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS)); 2461 profile.GetRawInfo(ADDRESS_HOME_STREET_ADDRESS));
2490 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE)); 2462 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_STATE));
2491 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY)); 2463 insert.BindString16(index++, profile.GetRawInfo(ADDRESS_HOME_CITY));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
2552 // Copy over the billing_address_id from the masked_server_cards to 2524 // Copy over the billing_address_id from the masked_server_cards to
2553 // server_card_metadata. 2525 // server_card_metadata.
2554 if (!db_->Execute("UPDATE server_card_metadata " 2526 if (!db_->Execute("UPDATE server_card_metadata "
2555 "SET billing_address_id = " 2527 "SET billing_address_id = "
2556 "(SELECT billing_address_id " 2528 "(SELECT billing_address_id "
2557 "FROM masked_credit_cards " 2529 "FROM masked_credit_cards "
2558 "WHERE id = server_card_metadata.id)")) { 2530 "WHERE id = server_card_metadata.id)")) {
2559 return false; 2531 return false;
2560 } 2532 }
2561 2533
2534 if (db_->DoesTableExist("masked_credit_cards_temp") &&
2535 !db_->Execute("DROP TABLE masked_credit_cards_temp")) {
2536 return false;
2537 }
2538
2562 // Remove the billing_address_id column from the masked_credit_cards table. 2539 // Remove the billing_address_id column from the masked_credit_cards table.
2563 // Create a temporary table that is a copy of masked_credit_cards but without 2540 // Create a temporary table that is a copy of masked_credit_cards but without
2564 // the billing_address_id column. 2541 // the billing_address_id column.
2565 if (db_->DoesTableExist("masked_credit_cards_temp") || 2542 if (!db_->Execute("CREATE TABLE masked_credit_cards_temp ("
2566 !db_->Execute("CREATE TABLE masked_credit_cards_temp ("
2567 "id VARCHAR," 2543 "id VARCHAR,"
2568 "status VARCHAR," 2544 "status VARCHAR,"
2569 "name_on_card VARCHAR," 2545 "name_on_card VARCHAR,"
2570 "type VARCHAR," 2546 "type VARCHAR,"
2571 "last_four VARCHAR," 2547 "last_four VARCHAR,"
2572 "exp_month INTEGER DEFAULT 0," 2548 "exp_month INTEGER DEFAULT 0,"
2573 "exp_year INTEGER DEFAULT 0)")) { 2549 "exp_year INTEGER DEFAULT 0)")) {
2574 return false; 2550 return false;
2575 } 2551 }
2576 // Copy over the data from the original masked_credit_cards table. 2552 // Copy over the data from the original masked_credit_cards table.
2577 if (!db_->Execute("INSERT INTO masked_credit_cards_temp " 2553 if (!db_->Execute("INSERT INTO masked_credit_cards_temp "
2578 "SELECT id, status, name_on_card, type, last_four, " 2554 "SELECT id, status, name_on_card, type, last_four, "
2579 "exp_month, exp_year " 2555 "exp_month, exp_year "
2580 "FROM masked_credit_cards")) { 2556 "FROM masked_credit_cards")) {
2581 return false; 2557 return false;
2582 } 2558 }
2583 // Delete the existing table and replace it with the contents of the 2559 // Delete the existing table and replace it with the contents of the
2584 // temporary table. 2560 // temporary table.
2585 if (!db_->Execute("DROP TABLE masked_credit_cards") || 2561 if (!db_->Execute("DROP TABLE masked_credit_cards") ||
2586 !db_->Execute("ALTER TABLE masked_credit_cards_temp " 2562 !db_->Execute("ALTER TABLE masked_credit_cards_temp "
2587 "RENAME TO masked_credit_cards")) { 2563 "RENAME TO masked_credit_cards")) {
2588 return false; 2564 return false;
2589 } 2565 }
2590 2566
2591 return transaction.Commit(); 2567 return transaction.Commit();
2592 } 2568 }
2593 2569
2570 bool AutofillTable::MigrateToVersion72RenameCardTypeToIssuerNetwork() {
2571 sql::Transaction transaction(db_);
2572 if (!transaction.Begin())
2573 return false;
2574
2575 if (db_->DoesTableExist("masked_credit_cards_temp") &&
2576 !db_->Execute("DROP TABLE masked_credit_cards_temp")) {
2577 return false;
2578 }
2579
2580 return db_->Execute(
2581 "CREATE TABLE masked_credit_cards_temp ("
2582 "id VARCHAR,"
2583 "status VARCHAR,"
2584 "name_on_card VARCHAR,"
2585 "network VARCHAR,"
2586 "last_four VARCHAR,"
2587 "exp_month INTEGER DEFAULT 0,"
2588 "exp_year INTEGER DEFAULT 0)") &&
2589 db_->Execute(
2590 "INSERT INTO masked_credit_cards_temp ("
2591 "id, status, name_on_card, network, last_four, exp_month, exp_year"
2592 ") SELECT "
2593 "id, status, name_on_card, type, last_four, exp_month, exp_year"
2594 " FROM masked_credit_cards") &&
2595 db_->Execute("DROP TABLE masked_credit_cards") &&
2596 db_->Execute(
2597 "ALTER TABLE masked_credit_cards_temp "
2598 "RENAME TO masked_credit_cards") &&
2599 transaction.Commit();
2600 }
2601
2594 } // namespace autofill 2602 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698