autofill_table.h revision a02191e04bc25c4935f804f2c080ae28663d096d
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.
4
5#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
6#define COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
7
8#include <vector>
9
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"
15
16class WebDatabase;
17
18namespace base {
19class Time;
20}
21
22namespace autofill {
23
24class AutofillChange;
25class AutofillEntry;
26class AutofillProfile;
27class AutofillTableTest;
28class CreditCard;
29
30struct FormFieldData;
31
32// This class manages the various Autofill tables within the SQLite database
33// passed to the constructor. It expects the following schemas:
34//
35// Note: The database stores time in seconds, UTC.
36//
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|.
47//
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.
51//
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.
77//
78// autofill_profile_names
79//                      This table contains the multi-valued name fields
80//                      associated with a profile.
81//
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//
88// autofill_profile_emails
89//                      This table contains the multi-valued email fields
90//                      associated with a profile.
91//
92//   guid               The guid string that identifies the profile to which
93//                      the email belongs.
94//   email
95//
96// autofill_profile_phones
97//                      This table contains the multi-valued phone fields
98//                      associated with a profile.
99//
100//   guid               The guid string that identifies the profile to which the
101//                      phone number belongs.
102//   number
103//
104// autofill_profiles_trash
105//                      This table contains guids of "trashed" autofill
106//                      profiles.  When a profile is removed its guid is added
107//                      to this table so that Sync can perform deferred removal.
108//
109//   guid               The guid string that identifies the trashed profile.
110//
111// credit_cards         This table contains credit card data added by the user
112//                      with the Autofill dialog.  Most of the columns are
113//                      standard entries in a credit card form.
114//
115//   guid               A guid string to uniquely identify the profile.
116//                      Added in version 31.
117//   name_on_card
118//   expiration_month
119//   expiration_year
120//   card_number_encrypted
121//                      Stores encrypted credit card number.
122//   date_modified      The date on which this entry was last modified.
123//                      Added in version 30.
124//   origin             The domain of origin for this profile.
125//                      Added in version 50.
126//
127class AutofillTable : public WebDatabaseTable {
128 public:
129  explicit AutofillTable(const std::string& app_locale);
130  virtual ~AutofillTable();
131
132  // Retrieves the AutofillTable* owned by |database|.
133  static AutofillTable* FromWebDatabase(WebDatabase* db);
134
135  virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE;
136  virtual bool CreateTablesIfNecessary() OVERRIDE;
137  virtual bool IsSyncable() OVERRIDE;
138  virtual bool MigrateToVersion(int version,
139                                bool* update_compatible_version) OVERRIDE;
140
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);
146
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
149  // parameter.
150  bool AddFormFieldValue(const FormFieldData& element,
151                         std::vector<AutofillChange>* changes);
152
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,
159                                   int limit);
160
161  // Returns whether any form elements are stored in the database.
162  bool HasFormElements();
163
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);
175
176  // Removes rows from the autofill table if they were last accessed strictly
177  // before |AutofillEntry::ExpirationTime()|.
178  bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes);
179
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);
183
184  // Retrieves all of the entries in the autofill table.
185  virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
186
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);
192
193  // Replaces existing autofill entries with the entries supplied in
194  // the argument.  If the entry does not already exist, it will be
195  // added.
196  virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
197
198  // Records a single Autofill profile in the autofill_profiles table.
199  virtual bool AddAutofillProfile(const AutofillProfile& profile);
200
201  // Updates the database values for the specified profile.  Mulit-value aware.
202  virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
203
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);
207
208  // Retrieves a profile with guid |guid|.  The caller owns |profile|.
209  bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
210
211  // Retrieves all profiles in the database.  Caller owns the returned profiles.
212  virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
213
214  // Records a single credit card in the credit_cards table.
215  bool AddCreditCard(const CreditCard& credit_card);
216
217  // Updates the database values for the specified credit card.
218  bool UpdateCreditCard(const CreditCard& credit_card);
219
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);
223
224  // Retrieves a credit card with guid |guid|.  The caller owns
225  // |credit_card_id|.
226  bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
227
228  // Retrieves all credit cards in the database.  Caller owns the returned
229  // credit cards.
230  virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
231
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
237  // filled.
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);
243
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);
254
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);
258
259  // Empties the Autofill profiles "trash can".
260  bool EmptyAutofillProfilesTrash();
261
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);
265
266  // Clear all profiles.
267  bool ClearAutofillProfiles();
268
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
288  // Max data length saved in the table;
289  static const size_t kMaxDataLength;
290
291 private:
292  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
293  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
294  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
295  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
296  FRIEND_TEST_ALL_PREFIXES(
297      AutofillTableTest,
298      Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore);
299  FRIEND_TEST_ALL_PREFIXES(
300      AutofillTableTest,
301      Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter);
302  FRIEND_TEST_ALL_PREFIXES(
303      AutofillTableTest,
304      Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring);
305  FRIEND_TEST_ALL_PREFIXES(
306      AutofillTableTest,
307      Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring);
308  FRIEND_TEST_ALL_PREFIXES(
309      AutofillTableTest,
310      Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter);
311  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
312  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
313  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
314  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
315  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
316  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
317                           RemoveAutofillDataModifiedBetween);
318  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
319  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
320  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
321                           Autofill_GetAllAutofillEntries_OneResult);
322  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
323                           Autofill_GetAllAutofillEntries_TwoDistinct);
324  FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
325                           Autofill_GetAllAutofillEntries_TwoSame);
326
327  // Methods for adding autofill entries at a specified time.  For
328  // testing only.
329  bool AddFormFieldValuesTime(
330      const std::vector<FormFieldData>& elements,
331      std::vector<AutofillChange>* changes,
332      base::Time time);
333  bool AddFormFieldValueTime(const FormFieldData& element,
334                             std::vector<AutofillChange>* changes,
335                             base::Time time);
336
337  // Insert a single AutofillEntry into the autofill table.
338  bool InsertAutofillEntry(const AutofillEntry& entry);
339
340  // Checks if the trash is empty.
341  bool IsAutofillProfilesTrashEmpty();
342
343  // Checks if the guid is in the trash.
344  bool IsAutofillGUIDInTrash(const std::string& guid);
345
346  bool InitMainTable();
347  bool InitCreditCardsTable();
348  bool InitDatesTable();
349  bool InitProfilesTable();
350  bool InitProfileNamesTable();
351  bool InitProfileEmailsTable();
352  bool InitProfilePhonesTable();
353  bool InitProfileTrashTable();
354
355  // The application locale.  The locale is needed for the migration to version
356  // 35. Since it must be read on the UI thread, it is set when the table is
357  // created (on the UI thread), and cached here so that it can be used for
358  // migrations (on the DB thread).
359  std::string app_locale_;
360
361  DISALLOW_COPY_AND_ASSIGN(AutofillTable);
362};
363
364}  // namespace autofill
365
366#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
367