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/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"
26 class AutofillProfile;
27 class AutofillTableTest;
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.
38 // name The name of the input as specified in the html.
39 // value The literal contents of the text field.
40 // value_lower The contents of the text field made lower_case.
41 // date_created The date on which the user first entered the string
42 // |value| into a field of name |name|.
43 // date_last_used The date on which the user last entered the string
44 // |value| into a field of name |name|.
45 // count How many times the user has entered the string |value|
46 // in a field of name |name|.
48 // autofill_profiles This table contains Autofill profile data added by the
49 // user with the Autofill dialog. Most of the columns are
50 // standard entries in a contact information form.
52 // guid A guid string to uniquely identify the profile.
53 // Added in version 31.
55 // street_address The combined lines of the street address.
56 // Added in version 54.
58 // A sub-classification beneath the city, e.g. an
59 // inner-city district or suburb. Added in version 54.
63 // sorting_code Similar to the zipcode column, but used for businesses
64 // or organizations that might not be geographically
65 // contiguous. The canonical example is CEDEX in France.
66 // Added in version 54.
68 // date_modified The date on which this profile was last modified.
69 // Added in version 30.
70 // origin The domain of origin for this profile.
71 // Added in version 50.
72 // language_code The BCP 47 language code used to format the address for
73 // display. For example, a JP address with "ja" language
74 // code starts with the postal code, but a JP address with
75 // "ja-latn" language code starts with the recipient name.
76 // Added in version 56.
78 // autofill_profile_names
79 // This table contains the multi-valued name fields
80 // associated with a profile.
82 // guid The guid string that identifies the profile to which
89 // autofill_profile_emails
90 // This table contains the multi-valued email fields
91 // associated with a profile.
93 // guid The guid string that identifies the profile to which
97 // autofill_profile_phones
98 // This table contains the multi-valued phone fields
99 // associated with a profile.
101 // guid The guid string that identifies the profile to which the
102 // phone number belongs.
105 // autofill_profiles_trash
106 // This table contains guids of "trashed" autofill
107 // profiles. When a profile is removed its guid is added
108 // to this table so that Sync can perform deferred removal.
110 // guid The guid string that identifies the trashed profile.
112 // credit_cards This table contains credit card data added by the user
113 // with the Autofill dialog. Most of the columns are
114 // standard entries in a credit card form.
116 // guid A guid string to uniquely identify the profile.
117 // Added in version 31.
121 // card_number_encrypted
122 // Stores encrypted credit card number.
123 // date_modified The date on which this entry was last modified.
124 // Added in version 30.
125 // origin The domain of origin for this profile.
126 // Added in version 50.
128 class AutofillTable : public WebDatabaseTable {
130 explicit AutofillTable(const std::string& app_locale);
131 ~AutofillTable() override;
133 // Retrieves the AutofillTable* owned by |database|.
134 static AutofillTable* FromWebDatabase(WebDatabase* db);
136 WebDatabaseTable::TypeKey GetTypeKey() const override;
137 bool CreateTablesIfNecessary() override;
138 bool IsSyncable() override;
139 bool MigrateToVersion(int version, bool* update_compatible_version) override;
141 // Records the form elements in |elements| in the database in the
142 // autofill table. A list of all added and updated autofill entries
143 // is returned in the changes out parameter.
144 bool AddFormFieldValues(const std::vector<FormFieldData>& elements,
145 std::vector<AutofillChange>* changes);
147 // Records a single form element in the database in the autofill table. A list
148 // of all added and updated autofill entries is returned in the changes out
150 bool AddFormFieldValue(const FormFieldData& element,
151 std::vector<AutofillChange>* changes);
153 // Retrieves a vector of all values which have been recorded in the autofill
154 // table as the value in a form element with name |name| and which start with
155 // |prefix|. The comparison of the prefix is case insensitive.
156 bool GetFormValuesForElementName(const base::string16& name,
157 const base::string16& prefix,
158 std::vector<base::string16>* values,
161 // Returns whether any form elements are stored in the database.
162 bool HasFormElements();
164 // Removes rows from the autofill table if they were created on or after
165 // |delete_begin| and last used strictly before |delete_end|. For rows where
166 // the time range [date_created, date_last_used] overlaps with [delete_begin,
167 // delete_end), but is not entirely contained within the latter range, updates
168 // the rows so that their resulting time range [new_date_created,
169 // new_date_last_used] lies entirely outside of [delete_begin, delete_end),
170 // updating the count accordingly. A list of all changed keys and whether
171 // each was updater or removed is returned in the changes out parameter.
172 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin,
173 const base::Time& delete_end,
174 std::vector<AutofillChange>* changes);
176 // Removes rows from the autofill table if they were last accessed strictly
177 // before |AutofillEntry::ExpirationTime()|.
178 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes);
180 // Removes the row from the autofill table for the given |name| |value| pair.
181 virtual bool RemoveFormElement(const base::string16& name,
182 const base::string16& value);
184 // Retrieves all of the entries in the autofill table.
185 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
187 // Retrieves a single entry from the autofill table.
188 virtual bool GetAutofillTimestamps(const base::string16& name,
189 const base::string16& value,
190 base::Time* date_created,
191 base::Time* date_last_used);
193 // Replaces existing autofill entries with the entries supplied in
194 // the argument. If the entry does not already exist, it will be
196 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
198 // Records a single Autofill profile in the autofill_profiles table.
199 virtual bool AddAutofillProfile(const AutofillProfile& profile);
201 // Updates the database values for the specified profile. Mulit-value aware.
202 virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
204 // Removes a row from the autofill_profiles table. |guid| is the identifier
205 // of the profile to remove.
206 virtual bool RemoveAutofillProfile(const std::string& guid);
208 // Retrieves a profile with guid |guid|. The caller owns |profile|.
209 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
211 // Retrieves all profiles in the database. Caller owns the returned profiles.
212 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
214 // Records a single credit card in the credit_cards table.
215 bool AddCreditCard(const CreditCard& credit_card);
217 // Updates the database values for the specified credit card.
218 bool UpdateCreditCard(const CreditCard& credit_card);
220 // Removes a row from the credit_cards table. |guid| is the identifer of the
221 // credit card to remove.
222 bool RemoveCreditCard(const std::string& guid);
224 // Retrieves a credit card with guid |guid|. The caller owns
226 bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
228 // Retrieves all credit cards in the database. Caller owns the returned
230 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
232 // Removes rows from autofill_profiles and credit_cards if they were created
233 // on or after |delete_begin| and strictly before |delete_end|. Returns the
234 // list of deleted profile guids in |profile_guids|. Return value is true if
235 // all rows were successfully removed. Returns false on database error. In
236 // that case, the output vector state is undefined, and may be partially
238 bool RemoveAutofillDataModifiedBetween(
239 const base::Time& delete_begin,
240 const base::Time& delete_end,
241 std::vector<std::string>* profile_guids,
242 std::vector<std::string>* credit_card_guids);
244 // Removes origin URLs from the autofill_profiles and credit_cards tables if
245 // they were written on or after |delete_begin| and strictly before
246 // |delete_end|. Returns the list of modified profiles in |profiles|. Return
247 // value is true if all rows were successfully updated. Returns false on
248 // database error. In that case, the output vector state is undefined, and
249 // may be partially filled.
250 bool RemoveOriginURLsModifiedBetween(
251 const base::Time& delete_begin,
252 const base::Time& delete_end,
253 ScopedVector<AutofillProfile>* profiles);
255 // Retrieves all profiles in the database that have been deleted since last
256 // "empty" of the trash.
257 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids);
259 // Empties the Autofill profiles "trash can".
260 bool EmptyAutofillProfilesTrash();
262 // Retrieves all profiles in the database that have been deleted since last
263 // "empty" of the trash.
264 bool AddAutofillGUIDToTrash(const std::string& guid);
266 // Clear all profiles.
267 bool ClearAutofillProfiles();
269 // Table migration functions.
270 // Removes empty values for autofill that were incorrectly stored in the DB
271 // See bug http://crbug.com/6111
272 bool MigrateToVersion22ClearAutofillEmptyValueElements();
273 bool MigrateToVersion23AddCardNumberEncryptedColumn();
274 bool MigrateToVersion24CleanupOversizedStringFields();
275 bool MigrateToVersion27UpdateLegacyCreditCards();
276 bool MigrateToVersion30AddDateModifed();
277 bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles();
278 bool MigrateToVersion32UpdateProfilesAndCreditCards();
279 bool MigrateToVersion33ProfilesBasedOnFirstName();
280 bool MigrateToVersion34ProfilesBasedOnCountryCode();
281 bool MigrateToVersion35GreatBritainCountryCodes();
282 bool MigrateToVersion37MergeAndCullOlderProfiles();
283 bool MigrateToVersion51AddOriginColumn();
284 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields();
285 bool MigrateToVersion55MergeAutofillDatesTable();
286 bool MigrateToVersion56AddProfileLanguageCodeForFormatting();
287 bool MigrateToVersion57AddFullNameField();
289 // Max data length saved in the table;
290 static const size_t kMaxDataLength;
293 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
294 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
295 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
296 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
297 FRIEND_TEST_ALL_PREFIXES(
299 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore);
300 FRIEND_TEST_ALL_PREFIXES(
302 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter);
303 FRIEND_TEST_ALL_PREFIXES(
305 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring);
306 FRIEND_TEST_ALL_PREFIXES(
308 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring);
309 FRIEND_TEST_ALL_PREFIXES(
311 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter);
312 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
313 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
314 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
315 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
316 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
317 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
318 RemoveAutofillDataModifiedBetween);
319 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
320 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
321 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
322 Autofill_GetAllAutofillEntries_OneResult);
323 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
324 Autofill_GetAllAutofillEntries_TwoDistinct);
325 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
326 Autofill_GetAllAutofillEntries_TwoSame);
328 // Methods for adding autofill entries at a specified time. For
330 bool AddFormFieldValuesTime(
331 const std::vector<FormFieldData>& elements,
332 std::vector<AutofillChange>* changes,
334 bool AddFormFieldValueTime(const FormFieldData& element,
335 std::vector<AutofillChange>* changes,
338 // Insert a single AutofillEntry into the autofill table.
339 bool InsertAutofillEntry(const AutofillEntry& entry);
341 // Checks if the trash is empty.
342 bool IsAutofillProfilesTrashEmpty();
344 // Checks if the guid is in the trash.
345 bool IsAutofillGUIDInTrash(const std::string& guid);
347 bool InitMainTable();
348 bool InitCreditCardsTable();
349 bool InitDatesTable();
350 bool InitProfilesTable();
351 bool InitProfileNamesTable();
352 bool InitProfileEmailsTable();
353 bool InitProfilePhonesTable();
354 bool InitProfileTrashTable();
356 // The application locale. The locale is needed for the migration to version
357 // 35. Since it must be read on the UI thread, it is set when the table is
358 // created (on the UI thread), and cached here so that it can be used for
359 // migrations (on the DB thread).
360 std::string app_locale_;
362 DISALLOW_COPY_AND_ASSIGN(AutofillTable);
365 } // namespace autofill
367 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_