Include all dupe types (event when value is zero) in scan stats.
[chromium-blink-merge.git] / components / autofill / core / browser / webdata / autofill_table.h
blob9ed5c8c86d3412c798e72037e5b07712e07f7242
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
8 #include <vector>
10 #include "base/compiler_specific.h"
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/strings/string16.h"
14 #include "components/webdata/common/web_database_table.h"
16 class WebDatabase;
18 namespace base {
19 class Time;
22 namespace autofill {
24 class AutofillChange;
25 class AutofillEntry;
26 class AutofillProfile;
27 class AutofillTableTest;
28 class CreditCard;
30 struct FormFieldData;
32 // This class manages the various Autofill tables within the SQLite database
33 // passed to the constructor. It expects the following schemas:
35 // Note: The database stores time in seconds, UTC.
37 // autofill This table contains autocomplete history data (not
38 // structured information).
40 // name The name of the input as specified in the html.
41 // value The literal contents of the text field.
42 // value_lower The contents of the text field made lower_case.
43 // date_created The date on which the user first entered the string
44 // |value| into a field of name |name|.
45 // date_last_used The date on which the user last entered the string
46 // |value| into a field of name |name|.
47 // count How many times the user has entered the string |value|
48 // in a field of name |name|.
50 // autofill_profiles This table contains Autofill profile data added by the
51 // user with the Autofill dialog. Most of the columns are
52 // standard entries in a contact information form.
54 // guid A guid string to uniquely identify the profile.
55 // Added in version 31.
56 // company_name
57 // street_address The combined lines of the street address.
58 // Added in version 54.
59 // dependent_locality
60 // A sub-classification beneath the city, e.g. an
61 // inner-city district or suburb. Added in version 54.
62 // city
63 // state
64 // zipcode
65 // sorting_code Similar to the zipcode column, but used for businesses
66 // or organizations that might not be geographically
67 // contiguous. The canonical example is CEDEX in France.
68 // Added in version 54.
69 // country_code
70 // use_count The number of times this profile has been used to fill
71 // a form. Added in version 61.
72 // use_date The date this profile was last used to fill a form,
73 // in time_t. Added in version 61.
74 // date_modified The date on which this profile was last modified, in
75 // time_t. Added in version 30.
76 // origin The domain of origin for this profile.
77 // Added in version 50.
78 // language_code The BCP 47 language code used to format the address for
79 // display. For example, a JP address with "ja" language
80 // code starts with the postal code, but a JP address with
81 // "ja-latn" language code starts with the recipient name.
82 // Added in version 56.
84 // autofill_profile_names
85 // This table contains the multi-valued name fields
86 // associated with a profile.
88 // guid The guid string that identifies the profile to which
89 // the name belongs.
90 // first_name
91 // middle_name
92 // last_name
93 // full_name
95 // autofill_profile_emails
96 // This table contains the multi-valued email fields
97 // associated with a profile.
99 // guid The guid string that identifies the profile to which
100 // the email belongs.
101 // email
103 // autofill_profile_phones
104 // This table contains the multi-valued phone fields
105 // associated with a profile.
107 // guid The guid string that identifies the profile to which the
108 // phone number belongs.
109 // number
111 // autofill_profiles_trash
112 // This table contains guids of "trashed" autofill
113 // profiles. When a profile is removed its guid is added
114 // to this table so that Sync can perform deferred removal.
116 // guid The guid string that identifies the trashed profile.
118 // credit_cards This table contains credit card data added by the user
119 // with the Autofill dialog. Most of the columns are
120 // standard entries in a credit card form.
122 // guid A guid string to uniquely identify the profile.
123 // Added in version 31.
124 // name_on_card
125 // expiration_month
126 // expiration_year
127 // card_number_encrypted
128 // Stores encrypted credit card number.
129 // use_count The number of times this card has been used to fill
130 // a form. Added in version 61.
131 // use_date The date this card was last used to fill a form,
132 // in time_t. Added in version 61.
133 // date_modified The date on which this entry was last modified, in
134 // time_t. Added in version 30.
135 // origin The domain of origin for this profile.
136 // Added in version 50.
138 // masked_credit_cards
139 // This table contains "masked" credit card information
140 // about credit cards stored on the server. It consists
141 // of a short description and an ID, but not full payment
142 // information. Writing to this table is only done by sync.
143 // When a server card is unmasked, it will stay here and
144 // will additionally be added in unmasked_credit_cards.
146 // id String assigned by the server to identify this card.
147 // This is opaque to the client.
148 // status Server's status of this card.
149 // TODO(brettw) define constants for this.
150 // name_on_card
151 // type Type of the credit card. This is one of the
152 // kSyncCardType* strings.
153 // last_four Last four digits of the card number. For de-duping
154 // with locally stored cards and generating descriptions.
155 // exp_month Expiration month: 1-12
156 // exp_year Four-digit year: 2017
158 // unmasked_credit_cards
159 // When a masked credit credit card is unmasked and the
160 // full number is downloaded, it will be stored here.
162 // id Server ID. This can be joined with the id in the
163 // masked_credit_cards table to get the rest of the data.
164 // card_number_encrypted
165 // Full card number, encrypted.
166 // use_count DEPRECATED in version 65. See server_card_metadata.
167 // use_date DEPRECATED in version 65. See server_card_metadata.
168 // unmask_date The date this card was unmasked in units of
169 // Time::ToInternalValue. Added in version 64.
171 // server_card_metadata
172 // Metadata (currently, usage data) about server credit
173 // cards. This will be synced.
175 // id The server ID, which matches an ID from the
176 // masked_credit_cards table.
177 // use_count The number of times this card has been used to fill
178 // a form.
179 // use_date The date this card was last used to fill a form,
180 // in internal t.
182 // server_addresses This table contains Autofill address data synced from
183 // the wallet server. It's basically the same as the
184 // autofill_profiles table but locally immutable.
186 // id String assigned by the server to identify this address.
187 // This is opaque to the client.
188 // recipient_name Added in v63.
189 // company_name
190 // street_address The combined lines of the street address.
191 // address_1 Also known as "administrative area". This is normally
192 // the state or province in most countries.
193 // address_2 Also known as "locality". In the US this is the city.
194 // address_3 A sub-classification beneath the city, e.g. an
195 // inner-city district or suburb. Also known as
196 // "dependent_locality".
197 // address_4 Used in certain countries. Also known as
198 // "sub_dependent_locality".
199 // postal_code
200 // sorting_code Similar to the zipcode column, but used for businesses
201 // or organizations that might not be geographically
202 // contiguous. The canonical example is CEDEX in France.
203 // country_code
204 // language_code The BCP 47 language code used to format the address for
205 // display. For example, a JP address with "ja" language
206 // code starts with the postal code, but a JP address with
207 // "ja-latn" language code starts with the recipient name.
208 // phone_number Phone number. This is a string and has no formatting
209 // constraints. Added in version 64.
211 // server_address_metadata
212 // Metadata (currently, usage data) about server addresses.
213 // This will be synced.
215 // id The server ID, which matches an ID from the
216 // server_addresses table.
217 // use_count The number of times this address has been used to fill
218 // a form.
219 // use_date The date this address was last used to fill a form,
220 // in internal t.
222 class AutofillTable : public WebDatabaseTable {
223 public:
224 explicit AutofillTable(const std::string& app_locale);
225 ~AutofillTable() override;
227 // Retrieves the AutofillTable* owned by |database|.
228 static AutofillTable* FromWebDatabase(WebDatabase* db);
230 WebDatabaseTable::TypeKey GetTypeKey() const override;
231 bool CreateTablesIfNecessary() override;
232 bool IsSyncable() override;
233 bool MigrateToVersion(int version, bool* update_compatible_version) override;
235 // Records the form elements in |elements| in the database in the
236 // autofill table. A list of all added and updated autofill entries
237 // is returned in the changes out parameter.
238 bool AddFormFieldValues(const std::vector<FormFieldData>& elements,
239 std::vector<AutofillChange>* changes);
241 // Records a single form element in the database in the autofill table. A list
242 // of all added and updated autofill entries is returned in the changes out
243 // parameter.
244 bool AddFormFieldValue(const FormFieldData& element,
245 std::vector<AutofillChange>* changes);
247 // Retrieves a vector of all values which have been recorded in the autofill
248 // table as the value in a form element with name |name| and which start with
249 // |prefix|. The comparison of the prefix is case insensitive.
250 bool GetFormValuesForElementName(const base::string16& name,
251 const base::string16& prefix,
252 std::vector<base::string16>* values,
253 int limit);
255 // Returns whether any form elements are stored in the database.
256 bool HasFormElements();
258 // Removes rows from the autofill table if they were created on or after
259 // |delete_begin| and last used strictly before |delete_end|. For rows where
260 // the time range [date_created, date_last_used] overlaps with [delete_begin,
261 // delete_end), but is not entirely contained within the latter range, updates
262 // the rows so that their resulting time range [new_date_created,
263 // new_date_last_used] lies entirely outside of [delete_begin, delete_end),
264 // updating the count accordingly. A list of all changed keys and whether
265 // each was updater or removed is returned in the changes out parameter.
266 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin,
267 const base::Time& delete_end,
268 std::vector<AutofillChange>* changes);
270 // Removes rows from the autofill table if they were last accessed strictly
271 // before |AutofillEntry::ExpirationTime()|.
272 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes);
274 // Removes the row from the autofill table for the given |name| |value| pair.
275 virtual bool RemoveFormElement(const base::string16& name,
276 const base::string16& value);
278 // Retrieves all of the entries in the autofill table.
279 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
281 // Retrieves a single entry from the autofill table.
282 virtual bool GetAutofillTimestamps(const base::string16& name,
283 const base::string16& value,
284 base::Time* date_created,
285 base::Time* date_last_used);
287 // Replaces existing autofill entries with the entries supplied in
288 // the argument. If the entry does not already exist, it will be
289 // added.
290 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
292 // Records a single Autofill profile in the autofill_profiles table.
293 virtual bool AddAutofillProfile(const AutofillProfile& profile);
295 // Updates the database values for the specified profile. Mulit-value aware.
296 virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
298 // Removes a row from the autofill_profiles table. |guid| is the identifier
299 // of the profile to remove.
300 virtual bool RemoveAutofillProfile(const std::string& guid);
302 // Retrieves a profile with guid |guid|. The caller owns |profile|.
303 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
305 // Retrieves local/server profiles in the database. Caller owns the returned
306 // profiles.
307 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
308 virtual bool GetServerProfiles(std::vector<AutofillProfile*>* profiles);
310 // Sets the server profiles. All old profiles are deleted and replaced with
311 // the given ones.
312 void SetServerProfiles(const std::vector<AutofillProfile>& profiles);
314 // Records a single credit card in the credit_cards table.
315 bool AddCreditCard(const CreditCard& credit_card);
317 // Updates the database values for the specified credit card.
318 bool UpdateCreditCard(const CreditCard& credit_card);
320 // Removes a row from the credit_cards table. |guid| is the identifer of the
321 // credit card to remove.
322 bool RemoveCreditCard(const std::string& guid);
324 // Retrieves a credit card with guid |guid|. The caller owns
325 // |credit_card_id|.
326 bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
328 // Retrieves the local/server credit cards in the database. Caller owns the
329 // returned credit cards.
330 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
331 virtual bool GetServerCreditCards(std::vector<CreditCard*>* credit_cards);
333 // Replaces all server credit cards with the given vector. Unmasked cards
334 // present in the new list will be preserved (even if the input is MASKED).
335 void SetServerCreditCards(const std::vector<CreditCard>& credit_cards);
337 // Cards synced from the server may be "masked" (only last 4 digits
338 // available) or "unmasked" (everything is available). These functions set
339 // that state.
340 bool UnmaskServerCreditCard(const CreditCard& masked,
341 const base::string16& full_number);
342 bool MaskServerCreditCard(const std::string& id);
344 bool UpdateServerCardUsageStats(const CreditCard& credit_card);
345 bool UpdateServerAddressUsageStats(const AutofillProfile& profile);
347 // Deletes all data from the server card and profile tables. Returns true if
348 // any data was deleted, false if not (so false means "commit not needed"
349 // rather than "error").
350 bool ClearAllServerData();
352 // Removes rows from autofill_profiles and credit_cards if they were created
353 // on or after |delete_begin| and strictly before |delete_end|. Returns the
354 // list of deleted profile guids in |profile_guids|. Return value is true if
355 // all rows were successfully removed. Returns false on database error. In
356 // that case, the output vector state is undefined, and may be partially
357 // filled.
358 bool RemoveAutofillDataModifiedBetween(
359 const base::Time& delete_begin,
360 const base::Time& delete_end,
361 std::vector<std::string>* profile_guids,
362 std::vector<std::string>* credit_card_guids);
364 // Removes origin URLs from the autofill_profiles and credit_cards tables if
365 // they were written on or after |delete_begin| and strictly before
366 // |delete_end|. Returns the list of modified profiles in |profiles|. Return
367 // value is true if all rows were successfully updated. Returns false on
368 // database error. In that case, the output vector state is undefined, and
369 // may be partially filled.
370 bool RemoveOriginURLsModifiedBetween(
371 const base::Time& delete_begin,
372 const base::Time& delete_end,
373 ScopedVector<AutofillProfile>* profiles);
375 // Retrieves all profiles in the database that have been deleted since last
376 // "empty" of the trash.
377 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids);
379 // Empties the Autofill profiles "trash can".
380 bool EmptyAutofillProfilesTrash();
382 // Retrieves all profiles in the database that have been deleted since last
383 // "empty" of the trash.
384 bool AddAutofillGUIDToTrash(const std::string& guid);
386 // Clear all profiles.
387 bool ClearAutofillProfiles();
389 // Table migration functions. NB: These do not and should not rely on other
390 // functions in this class. The implementation of a function such as
391 // GetCreditCard may change over time, but MigrateToVersionXX should never
392 // change.
393 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields();
394 bool MigrateToVersion55MergeAutofillDatesTable();
395 bool MigrateToVersion56AddProfileLanguageCodeForFormatting();
396 bool MigrateToVersion57AddFullNameField();
397 bool MigrateToVersion60AddServerCards();
398 bool MigrateToVersion61AddUsageStats();
399 bool MigrateToVersion62AddUsageStatsForUnmaskedCards();
400 bool MigrateToVersion63AddServerRecipientName();
401 bool MigrateToVersion64AddUnmaskDate();
402 bool MigrateToVersion65AddServerMetadataTables();
404 // Max data length saved in the table;
405 static const size_t kMaxDataLength;
407 private:
408 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
409 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
410 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
411 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
412 FRIEND_TEST_ALL_PREFIXES(
413 AutofillTableTest,
414 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore);
415 FRIEND_TEST_ALL_PREFIXES(
416 AutofillTableTest,
417 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter);
418 FRIEND_TEST_ALL_PREFIXES(
419 AutofillTableTest,
420 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring);
421 FRIEND_TEST_ALL_PREFIXES(
422 AutofillTableTest,
423 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring);
424 FRIEND_TEST_ALL_PREFIXES(
425 AutofillTableTest,
426 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter);
427 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
428 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
429 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
430 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
431 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
432 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
433 RemoveAutofillDataModifiedBetween);
434 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
435 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
436 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
437 Autofill_GetAllAutofillEntries_OneResult);
438 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
439 Autofill_GetAllAutofillEntries_TwoDistinct);
440 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
441 Autofill_GetAllAutofillEntries_TwoSame);
443 // Methods for adding autofill entries at a specified time. For
444 // testing only.
445 bool AddFormFieldValuesTime(
446 const std::vector<FormFieldData>& elements,
447 std::vector<AutofillChange>* changes,
448 base::Time time);
449 bool AddFormFieldValueTime(const FormFieldData& element,
450 std::vector<AutofillChange>* changes,
451 base::Time time);
453 // Insert a single AutofillEntry into the autofill table.
454 bool InsertAutofillEntry(const AutofillEntry& entry);
456 // Checks if the trash is empty.
457 bool IsAutofillProfilesTrashEmpty();
459 // Checks if the guid is in the trash.
460 bool IsAutofillGUIDInTrash(const std::string& guid);
462 bool InitMainTable();
463 bool InitCreditCardsTable();
464 bool InitDatesTable();
465 bool InitProfilesTable();
466 bool InitProfileNamesTable();
467 bool InitProfileEmailsTable();
468 bool InitProfilePhonesTable();
469 bool InitProfileTrashTable();
470 bool InitMaskedCreditCardsTable();
471 bool InitUnmaskedCreditCardsTable();
472 bool InitServerCardMetadataTable();
473 bool InitServerAddressesTable();
474 bool InitServerAddressMetadataTable();
476 // The application locale. The locale is needed for the migration to version
477 // 35. Since it must be read on the UI thread, it is set when the table is
478 // created (on the UI thread), and cached here so that it can be used for
479 // migrations (on the DB thread).
480 std::string app_locale_;
482 DISALLOW_COPY_AND_ASSIGN(AutofillTable);
485 } // namespace autofill
487 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_