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_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/strings/string16.h"
13 #include "components/webdata/common/web_database_table.h"
25 class AutofillProfile
;
26 class AutofillTableTest
;
31 // This class manages the various Autofill tables within the SQLite database
32 // passed to the constructor. It expects the following schemas:
34 // Note: The database stores time in seconds, UTC.
36 // autofill This table contains autocomplete history data (not
37 // structured information).
39 // name The name of the input as specified in the html.
40 // value The literal contents of the text field.
41 // value_lower The contents of the text field made lower_case.
42 // date_created The date on which the user first entered the string
43 // |value| into a field of name |name|.
44 // date_last_used The date on which the user last entered the string
45 // |value| into a field of name |name|.
46 // count How many times the user has entered the string |value|
47 // in a field of name |name|.
49 // autofill_profiles This table contains Autofill profile data added by the
50 // user with the Autofill dialog. Most of the columns are
51 // standard entries in a contact information form.
53 // guid A guid string to uniquely identify the profile.
54 // Added in version 31.
56 // street_address The combined lines of the street address.
57 // Added in version 54.
59 // A sub-classification beneath the city, e.g. an
60 // inner-city district or suburb. Added in version 54.
64 // sorting_code Similar to the zipcode column, but used for businesses
65 // or organizations that might not be geographically
66 // contiguous. The canonical example is CEDEX in France.
67 // Added in version 54.
69 // use_count The number of times this profile has been used to fill
70 // a form. Added in version 61.
71 // use_date The date this profile was last used to fill a form,
72 // in time_t. Added in version 61.
73 // date_modified The date on which this profile was last modified, in
74 // time_t. Added in version 30.
75 // origin The domain of origin for this profile.
76 // Added in version 50.
77 // language_code The BCP 47 language code used to format the address for
78 // display. For example, a JP address with "ja" language
79 // code starts with the postal code, but a JP address with
80 // "ja-latn" language code starts with the recipient name.
81 // Added in version 56.
83 // autofill_profile_names
84 // This table contains the multi-valued name fields
85 // associated with a profile.
87 // guid The guid string that identifies the profile to which
94 // autofill_profile_emails
95 // This table contains the multi-valued email fields
96 // associated with a profile.
98 // guid The guid string that identifies the profile to which
102 // autofill_profile_phones
103 // This table contains the multi-valued phone fields
104 // associated with a profile.
106 // guid The guid string that identifies the profile to which the
107 // phone number belongs.
110 // autofill_profiles_trash
111 // This table contains guids of "trashed" autofill
112 // profiles. When a profile is removed its guid is added
113 // to this table so that Sync can perform deferred removal.
115 // guid The guid string that identifies the trashed profile.
117 // credit_cards This table contains credit card data added by the user
118 // with the Autofill dialog. Most of the columns are
119 // standard entries in a credit card form.
121 // guid A guid string to uniquely identify the profile.
122 // Added in version 31.
126 // card_number_encrypted
127 // Stores encrypted credit card number.
128 // use_count The number of times this card has been used to fill
129 // a form. Added in version 61.
130 // use_date The date this card was last used to fill a form,
131 // in time_t. Added in version 61.
132 // date_modified The date on which this entry was last modified, in
133 // time_t. Added in version 30.
134 // origin The domain of origin for this profile.
135 // Added in version 50.
137 // masked_credit_cards
138 // This table contains "masked" credit card information
139 // about credit cards stored on the server. It consists
140 // of a short description and an ID, but not full payment
141 // information. Writing to this table is only done by sync.
142 // When a server card is unmasked, it will stay here and
143 // will additionally be added in unmasked_credit_cards.
145 // id String assigned by the server to identify this card.
146 // This is opaque to the client.
147 // status Server's status of this card.
148 // TODO(brettw) define constants for this.
150 // type Type of the credit card. This is one of the
151 // kSyncCardType* strings.
152 // last_four Last four digits of the card number. For de-duping
153 // with locally stored cards and generating descriptions.
154 // exp_month Expiration month: 1-12
155 // exp_year Four-digit year: 2017
157 // unmasked_credit_cards
158 // When a masked credit credit card is unmasked and the
159 // full number is downloaded, it will be stored here.
161 // id Server ID. This can be joined with the id in the
162 // masked_credit_cards table to get the rest of the data.
163 // card_number_encrypted
164 // Full card number, encrypted.
165 // use_count DEPRECATED in version 65. See server_card_metadata.
166 // use_date DEPRECATED in version 65. See server_card_metadata.
167 // unmask_date The date this card was unmasked in units of
168 // Time::ToInternalValue. Added in version 64.
170 // server_card_metadata
171 // Metadata (currently, usage data) about server credit
172 // cards. This will be synced.
174 // id The server ID, which matches an ID from the
175 // masked_credit_cards table.
176 // use_count The number of times this card has been used to fill
178 // use_date The date this card was last used to fill a form,
181 // server_addresses This table contains Autofill address data synced from
182 // the wallet server. It's basically the same as the
183 // autofill_profiles table but locally immutable.
185 // id String assigned by the server to identify this address.
186 // This is opaque to the client.
187 // recipient_name Added in v63.
189 // street_address The combined lines of the street address.
190 // address_1 Also known as "administrative area". This is normally
191 // the state or province in most countries.
192 // address_2 Also known as "locality". In the US this is the city.
193 // address_3 A sub-classification beneath the city, e.g. an
194 // inner-city district or suburb. Also known as
195 // "dependent_locality".
196 // address_4 Used in certain countries. Also known as
197 // "sub_dependent_locality".
199 // sorting_code Similar to the zipcode column, but used for businesses
200 // or organizations that might not be geographically
201 // contiguous. The canonical example is CEDEX in France.
203 // language_code The BCP 47 language code used to format the address for
204 // display. For example, a JP address with "ja" language
205 // code starts with the postal code, but a JP address with
206 // "ja-latn" language code starts with the recipient name.
207 // phone_number Phone number. This is a string and has no formatting
208 // constraints. Added in version 64.
210 // server_address_metadata
211 // Metadata (currently, usage data) about server addresses.
212 // This will be synced.
214 // id The server ID, which matches an ID from the
215 // server_addresses table.
216 // use_count The number of times this address has been used to fill
218 // use_date The date this address was last used to fill a form,
221 class AutofillTable
: public WebDatabaseTable
{
224 ~AutofillTable() override
;
226 // Retrieves the AutofillTable* owned by |db|.
227 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
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
,
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
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|.
303 scoped_ptr
<AutofillProfile
> GetAutofillProfile(const std::string
& guid
);
305 // Retrieves local/server profiles in the database. Caller owns the returned
307 // TODO(thestig): Convert to scopers.
308 virtual bool GetAutofillProfiles(std::vector
<AutofillProfile
*>* profiles
);
309 virtual bool GetServerProfiles(std::vector
<AutofillProfile
*>* profiles
);
311 // Sets the server profiles. All old profiles are deleted and replaced with
313 void SetServerProfiles(const std::vector
<AutofillProfile
>& profiles
);
315 // Records a single credit card in the credit_cards table.
316 bool AddCreditCard(const CreditCard
& credit_card
);
318 // Updates the database values for the specified credit card.
319 bool UpdateCreditCard(const CreditCard
& credit_card
);
321 // Removes a row from the credit_cards table. |guid| is the identifer of the
322 // credit card to remove.
323 bool RemoveCreditCard(const std::string
& guid
);
325 // Retrieves a credit card with guid |guid|.
326 scoped_ptr
<CreditCard
> GetCreditCard(const std::string
& guid
);
328 // Retrieves the local/server credit cards in the database. Caller owns the
329 // returned credit cards.
330 // TODO(thestig): Convert to scopers.
331 virtual bool GetCreditCards(std::vector
<CreditCard
*>* credit_cards
);
332 virtual bool GetServerCreditCards(std::vector
<CreditCard
*>* credit_cards
);
334 // Replaces all server credit cards with the given vector. Unmasked cards
335 // present in the new list will be preserved (even if the input is MASKED).
336 void SetServerCreditCards(const std::vector
<CreditCard
>& credit_cards
);
338 // Cards synced from the server may be "masked" (only last 4 digits
339 // available) or "unmasked" (everything is available). These functions set
341 bool UnmaskServerCreditCard(const CreditCard
& masked
,
342 const base::string16
& full_number
);
343 bool MaskServerCreditCard(const std::string
& id
);
345 bool UpdateServerCardUsageStats(const CreditCard
& credit_card
);
346 bool UpdateServerAddressUsageStats(const AutofillProfile
& profile
);
348 // Deletes all data from the server card and profile tables. Returns true if
349 // any data was deleted, false if not (so false means "commit not needed"
350 // rather than "error").
351 bool ClearAllServerData();
353 // Removes rows from autofill_profiles and credit_cards if they were created
354 // on or after |delete_begin| and strictly before |delete_end|. Returns the
355 // list of deleted profile guids in |profile_guids|. Return value is true if
356 // all rows were successfully removed. Returns false on database error. In
357 // that case, the output vector state is undefined, and may be partially
359 bool RemoveAutofillDataModifiedBetween(
360 const base::Time
& delete_begin
,
361 const base::Time
& delete_end
,
362 std::vector
<std::string
>* profile_guids
,
363 std::vector
<std::string
>* credit_card_guids
);
365 // Removes origin URLs from the autofill_profiles and credit_cards tables if
366 // they were written on or after |delete_begin| and strictly before
367 // |delete_end|. Returns the list of modified profiles in |profiles|. Return
368 // value is true if all rows were successfully updated. Returns false on
369 // database error. In that case, the output vector state is undefined, and
370 // may be partially filled.
371 bool RemoveOriginURLsModifiedBetween(
372 const base::Time
& delete_begin
,
373 const base::Time
& delete_end
,
374 ScopedVector
<AutofillProfile
>* profiles
);
376 // Retrieves all profiles in the database that have been deleted since last
377 // "empty" of the trash.
378 bool GetAutofillProfilesInTrash(std::vector
<std::string
>* guids
);
380 // Empties the Autofill profiles "trash can".
381 bool EmptyAutofillProfilesTrash();
383 // Retrieves all profiles in the database that have been deleted since last
384 // "empty" of the trash.
385 bool AddAutofillGUIDToTrash(const std::string
& guid
);
387 // Clear all profiles.
388 bool ClearAutofillProfiles();
390 // Table migration functions. NB: These do not and should not rely on other
391 // functions in this class. The implementation of a function such as
392 // GetCreditCard may change over time, but MigrateToVersionXX should never
394 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields();
395 bool MigrateToVersion55MergeAutofillDatesTable();
396 bool MigrateToVersion56AddProfileLanguageCodeForFormatting();
397 bool MigrateToVersion57AddFullNameField();
398 bool MigrateToVersion60AddServerCards();
399 bool MigrateToVersion61AddUsageStats();
400 bool MigrateToVersion62AddUsageStatsForUnmaskedCards();
401 bool MigrateToVersion63AddServerRecipientName();
402 bool MigrateToVersion64AddUnmaskDate();
403 bool MigrateToVersion65AddServerMetadataTables();
405 // Max data length saved in the table, AKA the maximum length allowed for
407 // Copied to components/autofill/ios/browser/resources/autofill_controller.js.
408 static const size_t kMaxDataLength
;
411 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, Autofill
);
412 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, Autofill_AddChanges
);
413 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, Autofill_RemoveBetweenChanges
);
414 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, Autofill_UpdateDontReplace
);
415 FRIEND_TEST_ALL_PREFIXES(
417 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore
);
418 FRIEND_TEST_ALL_PREFIXES(
420 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter
);
421 FRIEND_TEST_ALL_PREFIXES(
423 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring
);
424 FRIEND_TEST_ALL_PREFIXES(
426 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring
);
427 FRIEND_TEST_ALL_PREFIXES(
429 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter
);
430 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, Autofill_AddFormFieldValues
);
431 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, AutofillProfile
);
432 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, UpdateAutofillProfile
);
433 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, AutofillProfileTrash
);
434 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, AutofillProfileTrashInteraction
);
435 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
,
436 RemoveAutofillDataModifiedBetween
);
437 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, CreditCard
);
438 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
, UpdateCreditCard
);
439 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
,
440 Autofill_GetAllAutofillEntries_OneResult
);
441 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
,
442 Autofill_GetAllAutofillEntries_TwoDistinct
);
443 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest
,
444 Autofill_GetAllAutofillEntries_TwoSame
);
446 // Methods for adding autofill entries at a specified time. For
448 bool AddFormFieldValuesTime(
449 const std::vector
<FormFieldData
>& elements
,
450 std::vector
<AutofillChange
>* changes
,
452 bool AddFormFieldValueTime(const FormFieldData
& element
,
453 std::vector
<AutofillChange
>* changes
,
456 // Insert a single AutofillEntry into the autofill table.
457 bool InsertAutofillEntry(const AutofillEntry
& entry
);
459 // Checks if the trash is empty.
460 bool IsAutofillProfilesTrashEmpty();
462 // Checks if the guid is in the trash.
463 bool IsAutofillGUIDInTrash(const std::string
& guid
);
465 bool InitMainTable();
466 bool InitCreditCardsTable();
467 bool InitDatesTable();
468 bool InitProfilesTable();
469 bool InitProfileNamesTable();
470 bool InitProfileEmailsTable();
471 bool InitProfilePhonesTable();
472 bool InitProfileTrashTable();
473 bool InitMaskedCreditCardsTable();
474 bool InitUnmaskedCreditCardsTable();
475 bool InitServerCardMetadataTable();
476 bool InitServerAddressesTable();
477 bool InitServerAddressMetadataTable();
479 DISALLOW_COPY_AND_ASSIGN(AutofillTable
);
482 } // namespace autofill
484 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_