ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / webdata / autofill_table.h
blob221cbe87267807d503b0895a430d095a22ea959e
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 The number of times this card has been used to fill
167 // a form. Added in version 62.
168 // use_date The date this card was last used to fill a form, in
169 // internal time format (NOT time_t). Added in version 62.
171 // server_addresses This table contains Autofill address data synced from
172 // the wallet server. It's basically the same as the
173 // autofill_profiles table but locally immutable.
175 // id String assigned by the server to identify this address.
176 // This is opaque to the client.
177 // recipient_name Added in v63.
178 // company_name
179 // street_address The combined lines of the street address.
180 // address_1 Also known as "administrative area". This is normally
181 // the state or province in most countries.
182 // address_2 Also known as "locality". In the US this is the city.
183 // address_3 A sub-classification beneath the city, e.g. an
184 // inner-city district or suburb. Also known as
185 // "dependent_locality".
186 // address_4 Used in certain countries. Also known as
187 // "sub_dependent_locality".
188 // postal_code
189 // sorting_code Similar to the zipcode column, but used for businesses
190 // or organizations that might not be geographically
191 // contiguous. The canonical example is CEDEX in France.
192 // country_code
193 // language_code The BCP 47 language code used to format the address for
194 // display. For example, a JP address with "ja" language
195 // code starts with the postal code, but a JP address with
196 // "ja-latn" language code starts with the recipient name.
198 class AutofillTable : public WebDatabaseTable {
199 public:
200 explicit AutofillTable(const std::string& app_locale);
201 ~AutofillTable() override;
203 // Retrieves the AutofillTable* owned by |database|.
204 static AutofillTable* FromWebDatabase(WebDatabase* db);
206 WebDatabaseTable::TypeKey GetTypeKey() const override;
207 bool CreateTablesIfNecessary() override;
208 bool IsSyncable() override;
209 bool MigrateToVersion(int version, bool* update_compatible_version) override;
211 // Records the form elements in |elements| in the database in the
212 // autofill table. A list of all added and updated autofill entries
213 // is returned in the changes out parameter.
214 bool AddFormFieldValues(const std::vector<FormFieldData>& elements,
215 std::vector<AutofillChange>* changes);
217 // Records a single form element in the database in the autofill table. A list
218 // of all added and updated autofill entries is returned in the changes out
219 // parameter.
220 bool AddFormFieldValue(const FormFieldData& element,
221 std::vector<AutofillChange>* changes);
223 // Retrieves a vector of all values which have been recorded in the autofill
224 // table as the value in a form element with name |name| and which start with
225 // |prefix|. The comparison of the prefix is case insensitive.
226 bool GetFormValuesForElementName(const base::string16& name,
227 const base::string16& prefix,
228 std::vector<base::string16>* values,
229 int limit);
231 // Returns whether any form elements are stored in the database.
232 bool HasFormElements();
234 // Removes rows from the autofill table if they were created on or after
235 // |delete_begin| and last used strictly before |delete_end|. For rows where
236 // the time range [date_created, date_last_used] overlaps with [delete_begin,
237 // delete_end), but is not entirely contained within the latter range, updates
238 // the rows so that their resulting time range [new_date_created,
239 // new_date_last_used] lies entirely outside of [delete_begin, delete_end),
240 // updating the count accordingly. A list of all changed keys and whether
241 // each was updater or removed is returned in the changes out parameter.
242 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin,
243 const base::Time& delete_end,
244 std::vector<AutofillChange>* changes);
246 // Removes rows from the autofill table if they were last accessed strictly
247 // before |AutofillEntry::ExpirationTime()|.
248 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes);
250 // Removes the row from the autofill table for the given |name| |value| pair.
251 virtual bool RemoveFormElement(const base::string16& name,
252 const base::string16& value);
254 // Retrieves all of the entries in the autofill table.
255 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
257 // Retrieves a single entry from the autofill table.
258 virtual bool GetAutofillTimestamps(const base::string16& name,
259 const base::string16& value,
260 base::Time* date_created,
261 base::Time* date_last_used);
263 // Replaces existing autofill entries with the entries supplied in
264 // the argument. If the entry does not already exist, it will be
265 // added.
266 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
268 // Records a single Autofill profile in the autofill_profiles table.
269 virtual bool AddAutofillProfile(const AutofillProfile& profile);
271 // Updates the database values for the specified profile. Mulit-value aware.
272 virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
274 // Removes a row from the autofill_profiles table. |guid| is the identifier
275 // of the profile to remove.
276 virtual bool RemoveAutofillProfile(const std::string& guid);
278 // Retrieves a profile with guid |guid|. The caller owns |profile|.
279 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
281 // Retrieves local/server profiles in the database. Caller owns the returned
282 // profiles.
283 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
284 virtual bool GetAutofillServerProfiles(
285 std::vector<AutofillProfile*>* profiles);
287 // Sets the server profiles. All old profiles are deleted and replaced with
288 // the given ones.
289 void SetAutofillServerProfiles(const std::vector<AutofillProfile>& profiles);
291 // Records a single credit card in the credit_cards table.
292 bool AddCreditCard(const CreditCard& credit_card);
294 // Updates the database values for the specified credit card.
295 bool UpdateCreditCard(const CreditCard& credit_card);
297 // Removes a row from the credit_cards table. |guid| is the identifer of the
298 // credit card to remove.
299 bool RemoveCreditCard(const std::string& guid);
301 // Retrieves a credit card with guid |guid|. The caller owns
302 // |credit_card_id|.
303 bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
305 // Retrieves the local/server credit cards in the database. Caller owns the
306 // returned credit cards.
307 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
308 virtual bool GetServerCreditCards(std::vector<CreditCard*>* credit_cards);
310 // Replaces all server credit cards with the given vector. Unmasked cards
311 // present in the new list will be preserved (even if the input is MASKED).
312 void SetServerCreditCards(const std::vector<CreditCard>& credit_cards);
314 // Cards synced from the server may be "masked" (only last 4 digits
315 // available) or "unmasked" (everything is available). These functions set
316 // that state.
317 bool UnmaskServerCreditCard(const std::string& id,
318 const base::string16& full_number);
319 bool MaskServerCreditCard(const std::string& id);
321 // Updates the use count and last use date for an unmasked server card.
322 bool UpdateUnmaskedCardUsageStats(const CreditCard& credit_card);
324 // Removes rows from autofill_profiles and credit_cards if they were created
325 // on or after |delete_begin| and strictly before |delete_end|. Returns the
326 // list of deleted profile guids in |profile_guids|. Return value is true if
327 // all rows were successfully removed. Returns false on database error. In
328 // that case, the output vector state is undefined, and may be partially
329 // filled.
330 bool RemoveAutofillDataModifiedBetween(
331 const base::Time& delete_begin,
332 const base::Time& delete_end,
333 std::vector<std::string>* profile_guids,
334 std::vector<std::string>* credit_card_guids);
336 // Removes origin URLs from the autofill_profiles and credit_cards tables if
337 // they were written on or after |delete_begin| and strictly before
338 // |delete_end|. Returns the list of modified profiles in |profiles|. Return
339 // value is true if all rows were successfully updated. Returns false on
340 // database error. In that case, the output vector state is undefined, and
341 // may be partially filled.
342 bool RemoveOriginURLsModifiedBetween(
343 const base::Time& delete_begin,
344 const base::Time& delete_end,
345 ScopedVector<AutofillProfile>* profiles);
347 // Retrieves all profiles in the database that have been deleted since last
348 // "empty" of the trash.
349 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids);
351 // Empties the Autofill profiles "trash can".
352 bool EmptyAutofillProfilesTrash();
354 // Retrieves all profiles in the database that have been deleted since last
355 // "empty" of the trash.
356 bool AddAutofillGUIDToTrash(const std::string& guid);
358 // Clear all profiles.
359 bool ClearAutofillProfiles();
361 // Table migration functions.
362 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields();
363 bool MigrateToVersion55MergeAutofillDatesTable();
364 bool MigrateToVersion56AddProfileLanguageCodeForFormatting();
365 bool MigrateToVersion57AddFullNameField();
366 bool MigrateToVersion60AddServerCards();
367 bool MigrateToVersion61AddUsageStats();
368 bool MigrateToVersion62AddUsageStatsForUnmaskedCards();
369 bool MigrateToVersion63AddServerRecipientName();
371 // Max data length saved in the table;
372 static const size_t kMaxDataLength;
374 private:
375 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
376 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
377 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
378 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
379 FRIEND_TEST_ALL_PREFIXES(
380 AutofillTableTest,
381 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore);
382 FRIEND_TEST_ALL_PREFIXES(
383 AutofillTableTest,
384 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter);
385 FRIEND_TEST_ALL_PREFIXES(
386 AutofillTableTest,
387 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring);
388 FRIEND_TEST_ALL_PREFIXES(
389 AutofillTableTest,
390 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring);
391 FRIEND_TEST_ALL_PREFIXES(
392 AutofillTableTest,
393 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter);
394 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
395 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
396 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
397 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
398 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
399 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
400 RemoveAutofillDataModifiedBetween);
401 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
402 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
403 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
404 Autofill_GetAllAutofillEntries_OneResult);
405 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
406 Autofill_GetAllAutofillEntries_TwoDistinct);
407 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
408 Autofill_GetAllAutofillEntries_TwoSame);
410 // Methods for adding autofill entries at a specified time. For
411 // testing only.
412 bool AddFormFieldValuesTime(
413 const std::vector<FormFieldData>& elements,
414 std::vector<AutofillChange>* changes,
415 base::Time time);
416 bool AddFormFieldValueTime(const FormFieldData& element,
417 std::vector<AutofillChange>* changes,
418 base::Time time);
420 // Insert a single AutofillEntry into the autofill table.
421 bool InsertAutofillEntry(const AutofillEntry& entry);
423 // Checks if the trash is empty.
424 bool IsAutofillProfilesTrashEmpty();
426 // Checks if the guid is in the trash.
427 bool IsAutofillGUIDInTrash(const std::string& guid);
429 bool InitMainTable();
430 bool InitCreditCardsTable();
431 bool InitDatesTable();
432 bool InitProfilesTable();
433 bool InitProfileNamesTable();
434 bool InitProfileEmailsTable();
435 bool InitProfilePhonesTable();
436 bool InitProfileTrashTable();
437 bool InitMaskedCreditCardsTable();
438 bool InitUnmaskedCreditCardsTable();
439 bool InitServerAddressesTable();
441 // The application locale. The locale is needed for the migration to version
442 // 35. Since it must be read on the UI thread, it is set when the table is
443 // created (on the UI thread), and cached here so that it can be used for
444 // migrations (on the DB thread).
445 std::string app_locale_;
447 DISALLOW_COPY_AND_ASSIGN(AutofillTable);
450 } // namespace autofill
452 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_