Complete hooking up server cards and profiles to the database.
[chromium-blink-merge.git] / components / autofill / core / browser / webdata / autofill_table.h
blob74bb2bf28473f88a42f73feb32fc91e48253a02c
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
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.
54 // company_name
55 // street_address The combined lines of the street address.
56 // Added in version 54.
57 // dependent_locality
58 // A sub-classification beneath the city, e.g. an
59 // inner-city district or suburb. Added in version 54.
60 // city
61 // state
62 // zipcode
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.
67 // country_code
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
83 // the name belongs.
84 // first_name
85 // middle_name
86 // last_name
87 // full_name
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
94 // the email belongs.
95 // email
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.
103 // number
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.
118 // name_on_card
119 // expiration_month
120 // expiration_year
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 // masked_credit_cards
129 // This table contains "masked" credit card information
130 // about credit cards stored on the server. It consists
131 // of a short description and an ID, but not full payment
132 // information. Writing to this table is only done by sync.
133 // When a server card is unmasked, it will stay here and
134 // will additionally be added in unmasked_credit_cards.
136 // id String assigned by the server to identify this card.
137 // This is opaque to the client.
138 // status Server's status of this card.
139 // TODO(brettw) define constants for this.
140 // name_on_card
141 // type Type of the credit card. This is one of the
142 // kSyncCardType* strings.
143 // last_four Last four digits of the card number. For de-duping
144 // with locally stored cards and generating descriptions.
145 // exp_month Expiration month: 1-12
146 // exp_year Four-digit year: 2017
148 // unmasked_credit_cards
149 // When a masked credit credit card is unmasked and the
150 // full number is downloaded, it will be stored here.
152 // id Server ID. This can be joined with the id in the
153 // masked_credit_cards table to get the rest of the data.
154 // card_number_encrypted
155 // Full card number, encrypted.
157 // server_addresses This table contains Autofill address data synced from
158 // the wallet server. It's basically the same as the
159 // autofill_profiles table but locally immutable.
161 // id String assigned by the server to identify this address.
162 // This is opaque to the client.
163 // company_name
164 // street_address The combined lines of the street address.
165 // address_1 Also known as "administrative area". This is normally
166 // the state or province in most countries.
167 // address_2 Also known as "locality". In the US this is the city.
168 // address_3 A sub-classification beneath the city, e.g. an
169 // inner-city district or suburb. Also known as
170 // "dependent_locality".
171 // address_4 Used in certain countries. Also known as
172 // "sub_dependent_locality".
173 // postal_code
174 // sorting_code Similar to the zipcode column, but used for businesses
175 // or organizations that might not be geographically
176 // contiguous. The canonical example is CEDEX in France.
177 // country_code
178 // language_code The BCP 47 language code used to format the address for
179 // display. For example, a JP address with "ja" language
180 // code starts with the postal code, but a JP address with
181 // "ja-latn" language code starts with the recipient name.
183 class AutofillTable : public WebDatabaseTable {
184 public:
185 explicit AutofillTable(const std::string& app_locale);
186 ~AutofillTable() override;
188 // Retrieves the AutofillTable* owned by |database|.
189 static AutofillTable* FromWebDatabase(WebDatabase* db);
191 WebDatabaseTable::TypeKey GetTypeKey() const override;
192 bool CreateTablesIfNecessary() override;
193 bool IsSyncable() override;
194 bool MigrateToVersion(int version, bool* update_compatible_version) override;
196 // Records the form elements in |elements| in the database in the
197 // autofill table. A list of all added and updated autofill entries
198 // is returned in the changes out parameter.
199 bool AddFormFieldValues(const std::vector<FormFieldData>& elements,
200 std::vector<AutofillChange>* changes);
202 // Records a single form element in the database in the autofill table. A list
203 // of all added and updated autofill entries is returned in the changes out
204 // parameter.
205 bool AddFormFieldValue(const FormFieldData& element,
206 std::vector<AutofillChange>* changes);
208 // Retrieves a vector of all values which have been recorded in the autofill
209 // table as the value in a form element with name |name| and which start with
210 // |prefix|. The comparison of the prefix is case insensitive.
211 bool GetFormValuesForElementName(const base::string16& name,
212 const base::string16& prefix,
213 std::vector<base::string16>* values,
214 int limit);
216 // Returns whether any form elements are stored in the database.
217 bool HasFormElements();
219 // Removes rows from the autofill table if they were created on or after
220 // |delete_begin| and last used strictly before |delete_end|. For rows where
221 // the time range [date_created, date_last_used] overlaps with [delete_begin,
222 // delete_end), but is not entirely contained within the latter range, updates
223 // the rows so that their resulting time range [new_date_created,
224 // new_date_last_used] lies entirely outside of [delete_begin, delete_end),
225 // updating the count accordingly. A list of all changed keys and whether
226 // each was updater or removed is returned in the changes out parameter.
227 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin,
228 const base::Time& delete_end,
229 std::vector<AutofillChange>* changes);
231 // Removes rows from the autofill table if they were last accessed strictly
232 // before |AutofillEntry::ExpirationTime()|.
233 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes);
235 // Removes the row from the autofill table for the given |name| |value| pair.
236 virtual bool RemoveFormElement(const base::string16& name,
237 const base::string16& value);
239 // Retrieves all of the entries in the autofill table.
240 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
242 // Retrieves a single entry from the autofill table.
243 virtual bool GetAutofillTimestamps(const base::string16& name,
244 const base::string16& value,
245 base::Time* date_created,
246 base::Time* date_last_used);
248 // Replaces existing autofill entries with the entries supplied in
249 // the argument. If the entry does not already exist, it will be
250 // added.
251 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
253 // Records a single Autofill profile in the autofill_profiles table.
254 virtual bool AddAutofillProfile(const AutofillProfile& profile);
256 // Updates the database values for the specified profile. Mulit-value aware.
257 virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
259 // Removes a row from the autofill_profiles table. |guid| is the identifier
260 // of the profile to remove.
261 virtual bool RemoveAutofillProfile(const std::string& guid);
263 // Retrieves a profile with guid |guid|. The caller owns |profile|.
264 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
266 // Retrieves local/server profiles in the database. Caller owns the returned
267 // profiles.
268 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
269 virtual bool GetAutofillServerProfiles(
270 std::vector<AutofillProfile*>* profiles);
272 // Sets the server profiles. All old profiles are deleted and replaced with
273 // the given ones.
274 void SetAutofillServerProfiles(const std::vector<AutofillProfile>& profiles);
276 // Records a single credit card in the credit_cards table.
277 bool AddCreditCard(const CreditCard& credit_card);
279 // Updates the database values for the specified credit card.
280 bool UpdateCreditCard(const CreditCard& credit_card);
282 // Removes a row from the credit_cards table. |guid| is the identifer of the
283 // credit card to remove.
284 bool RemoveCreditCard(const std::string& guid);
286 // Retrieves a credit card with guid |guid|. The caller owns
287 // |credit_card_id|.
288 bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
290 // Retrieves the local/server credit cards in the database. Caller owns the
291 // returned credit cards.
292 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
293 virtual bool GetServerCreditCards(std::vector<CreditCard*>* credit_cards);
295 // Replaces all server credit cards with the given vector. Unmasked cards
296 // present in the new list will be preserved (even if the input is MASKED).
297 void SetServerCreditCards(const std::vector<CreditCard>& credit_cards);
299 // Cards synced from the server may be "masked" (only last 4 digits
300 // available) or "unmasked" (everything is available). These functions set
301 // that state.
302 bool UnmaskServerCreditCard(const std::string& id,
303 const base::string16& full_number);
304 bool MaskServerCreditCard(const std::string& id);
306 // Removes rows from autofill_profiles and credit_cards if they were created
307 // on or after |delete_begin| and strictly before |delete_end|. Returns the
308 // list of deleted profile guids in |profile_guids|. Return value is true if
309 // all rows were successfully removed. Returns false on database error. In
310 // that case, the output vector state is undefined, and may be partially
311 // filled.
312 bool RemoveAutofillDataModifiedBetween(
313 const base::Time& delete_begin,
314 const base::Time& delete_end,
315 std::vector<std::string>* profile_guids,
316 std::vector<std::string>* credit_card_guids);
318 // Removes origin URLs from the autofill_profiles and credit_cards tables if
319 // they were written on or after |delete_begin| and strictly before
320 // |delete_end|. Returns the list of modified profiles in |profiles|. Return
321 // value is true if all rows were successfully updated. Returns false on
322 // database error. In that case, the output vector state is undefined, and
323 // may be partially filled.
324 bool RemoveOriginURLsModifiedBetween(
325 const base::Time& delete_begin,
326 const base::Time& delete_end,
327 ScopedVector<AutofillProfile>* profiles);
329 // Retrieves all profiles in the database that have been deleted since last
330 // "empty" of the trash.
331 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids);
333 // Empties the Autofill profiles "trash can".
334 bool EmptyAutofillProfilesTrash();
336 // Retrieves all profiles in the database that have been deleted since last
337 // "empty" of the trash.
338 bool AddAutofillGUIDToTrash(const std::string& guid);
340 // Clear all profiles.
341 bool ClearAutofillProfiles();
343 // Table migration functions.
344 // Removes empty values for autofill that were incorrectly stored in the DB
345 // See bug http://crbug.com/6111
346 bool MigrateToVersion22ClearAutofillEmptyValueElements();
347 bool MigrateToVersion23AddCardNumberEncryptedColumn();
348 bool MigrateToVersion24CleanupOversizedStringFields();
349 bool MigrateToVersion27UpdateLegacyCreditCards();
350 bool MigrateToVersion30AddDateModifed();
351 bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles();
352 bool MigrateToVersion32UpdateProfilesAndCreditCards();
353 bool MigrateToVersion33ProfilesBasedOnFirstName();
354 bool MigrateToVersion34ProfilesBasedOnCountryCode();
355 bool MigrateToVersion35GreatBritainCountryCodes();
356 bool MigrateToVersion37MergeAndCullOlderProfiles();
357 bool MigrateToVersion51AddOriginColumn();
358 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields();
359 bool MigrateToVersion55MergeAutofillDatesTable();
360 bool MigrateToVersion56AddProfileLanguageCodeForFormatting();
361 bool MigrateToVersion57AddFullNameField();
362 bool MigrateToVersion60AddServerCards();
364 // Max data length saved in the table;
365 static const size_t kMaxDataLength;
367 private:
368 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
369 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
370 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
371 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
372 FRIEND_TEST_ALL_PREFIXES(
373 AutofillTableTest,
374 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore);
375 FRIEND_TEST_ALL_PREFIXES(
376 AutofillTableTest,
377 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter);
378 FRIEND_TEST_ALL_PREFIXES(
379 AutofillTableTest,
380 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring);
381 FRIEND_TEST_ALL_PREFIXES(
382 AutofillTableTest,
383 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring);
384 FRIEND_TEST_ALL_PREFIXES(
385 AutofillTableTest,
386 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter);
387 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
388 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
389 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
390 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
391 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
392 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
393 RemoveAutofillDataModifiedBetween);
394 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
395 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
396 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
397 Autofill_GetAllAutofillEntries_OneResult);
398 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
399 Autofill_GetAllAutofillEntries_TwoDistinct);
400 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
401 Autofill_GetAllAutofillEntries_TwoSame);
403 // Methods for adding autofill entries at a specified time. For
404 // testing only.
405 bool AddFormFieldValuesTime(
406 const std::vector<FormFieldData>& elements,
407 std::vector<AutofillChange>* changes,
408 base::Time time);
409 bool AddFormFieldValueTime(const FormFieldData& element,
410 std::vector<AutofillChange>* changes,
411 base::Time time);
413 // Insert a single AutofillEntry into the autofill table.
414 bool InsertAutofillEntry(const AutofillEntry& entry);
416 // Checks if the trash is empty.
417 bool IsAutofillProfilesTrashEmpty();
419 // Checks if the guid is in the trash.
420 bool IsAutofillGUIDInTrash(const std::string& guid);
422 bool InitMainTable();
423 bool InitCreditCardsTable();
424 bool InitDatesTable();
425 bool InitProfilesTable();
426 bool InitProfileNamesTable();
427 bool InitProfileEmailsTable();
428 bool InitProfilePhonesTable();
429 bool InitProfileTrashTable();
430 bool InitMaskedCreditCardsTable();
431 bool InitUnmaskedCreditCardsTable();
432 bool InitServerAddressesTable();
434 // The application locale. The locale is needed for the migration to version
435 // 35. Since it must be read on the UI thread, it is set when the table is
436 // created (on the UI thread), and cached here so that it can be used for
437 // migrations (on the DB thread).
438 std::string app_locale_;
440 DISALLOW_COPY_AND_ASSIGN(AutofillTable);
443 } // namespace autofill
445 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_