• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 CHROME_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
6 #define CHROME_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
7 #pragma once
8 
9 #include "base/gtest_prod_util.h"
10 #include "base/string16.h"
11 #include "chrome/browser/webdata/web_database_table.h"
12 
13 #include <vector>
14 
15 class AutofillChange;
16 class AutofillEntry;
17 class AutofillProfile;
18 class AutofillTableTest;
19 class CreditCard;
20 
21 namespace webkit_glue {
22 struct FormField;
23 }
24 
25 // This class manages the various autofill tables within the SQLite database
26 // passed to the constructor. It expects the following schemas:
27 //
28 // Note: The database stores time in seconds, UTC.
29 //
30 // autofill
31 //   name                The name of the input as specified in the html.
32 //   value               The literal contents of the text field.
33 //   value_lower         The contents of the text field made lower_case.
34 //   pair_id             An ID number unique to the row in the table.
35 //   count               How many times the user has entered the string |value|
36 //                       in a field of name |name|.
37 //
38 // autofill_dates        This table associates a row to each separate time the
39 //                       user submits a form containing a certain name/value
40 //                       pair.  The |pair_id| should match the |pair_id| field
41 //                       in the appropriate row of the autofill table.
42 //   pair_id
43 //   date_created
44 //
45 // autofill_profiles    This table contains Autofill profile data added by the
46 //                      user with the Autofill dialog.  Most of the columns are
47 //                      standard entries in a contact information form.
48 //
49 //   guid               A guid string to uniquely identify the profile.
50 //                      Added in version 31.
51 //   company_name
52 //   address_line_1
53 //   address_line_2
54 //   city
55 //   state
56 //   zipcode
57 //   country            The country name.  Deprecated, should be removed once
58 //                      the stable channel reaches version 11.
59 //   country_code
60 //   date_modified      The date on which this profile was last modified.
61 //                      Added in version 30.
62 //
63 // autofill_profile_names
64 //                      This table contains the multi-valued name fields
65 //                      associated with a profile.
66 //
67 //   guid               The guid string that identifies the profile to which
68 //                      the name belongs.
69 //   first_name
70 //   middle_name
71 //   last_name
72 //
73 // autofill_profile_emails
74 //                      This table contains the multi-valued email fields
75 //                      associated with a profile.
76 //
77 //   guid               The guid string that identifies the profile to which
78 //                      the email belongs.
79 //   email
80 //
81 // autofill_profile_phones
82 //                      This table contains the multi-valued phone fields
83 //                      associated with a profile.
84 //
85 //   guid               The guid string that identifies the profile to which
86 //                      the phone or fax number belongs.
87 //   type               An integer constant designating either phone or fax type
88 //                      of the number.
89 //   number
90 //
91 // autofill_profiles_trash
92 //                      This table contains guids of "trashed" autofill
93 //                      profiles.  When a profile is removed its guid is added
94 //                      to this table so that Sync can perform deferred removal.
95 //
96 //   guid               The guid string that identifies the trashed profile.
97 //
98 // credit_cards         This table contains credit card data added by the user
99 //                      with the Autofill dialog.  Most of the columns are
100 //                      standard entries in a credit card form.
101 //
102 //   guid               A guid string to uniquely identify the profile.
103 //                      Added in version 31.
104 //   name_on_card
105 //   expiration_month
106 //   expiration_year
107 //   card_number_encrypted Stores encrypted credit card number.
108 //   date_modified      The date on which this entry was last modified.
109 //                      Added in version 30.
110 //
111 class AutofillTable : public WebDatabaseTable {
112  public:
AutofillTable(sql::Connection * db,sql::MetaTable * meta_table)113   AutofillTable(sql::Connection* db, sql::MetaTable* meta_table)
114       : WebDatabaseTable(db, meta_table) {}
~AutofillTable()115   virtual ~AutofillTable() {}
116   virtual bool Init();
117   virtual bool IsSyncable();
118 
119   // Records the form elements in |elements| in the database in the
120   // autofill table.  A list of all added and updated autofill entries
121   // is returned in the changes out parameter.
122   bool AddFormFieldValues(const std::vector<webkit_glue::FormField>& elements,
123                           std::vector<AutofillChange>* changes);
124 
125   // Records a single form element in the database in the autofill table. A list
126   // of all added and updated autofill entries is returned in the changes out
127   // parameter.
128   bool AddFormFieldValue(const webkit_glue::FormField& element,
129                          std::vector<AutofillChange>* changes);
130 
131   // Retrieves a vector of all values which have been recorded in the autofill
132   // table as the value in a form element with name |name| and which start with
133   // |prefix|.  The comparison of the prefix is case insensitive.
134   bool GetFormValuesForElementName(const string16& name,
135                                    const string16& prefix,
136                                    std::vector<string16>* values,
137                                    int limit);
138 
139   // Removes rows from autofill_dates if they were created on or after
140   // |delete_begin| and strictly before |delete_end|.  Decrements the
141   // count of the corresponding rows in the autofill table, and
142   // removes those rows if the count goes to 0.  A list of all changed
143   // keys and whether each was updater or removed is returned in the
144   // changes out parameter.
145   bool RemoveFormElementsAddedBetween(base::Time delete_begin,
146                                       base::Time delete_end,
147                                       std::vector<AutofillChange>* changes);
148 
149   // Removes from autofill_dates rows with given pair_id where date_created lies
150   // between delte_begin and delte_end.
151   bool RemoveFormElementForTimeRange(int64 pair_id,
152                                      base::Time delete_begin,
153                                      base::Time delete_end,
154                                      int* how_many);
155 
156   // Increments the count in the row corresponding to |pair_id| by
157   // |delta|.  Removes the row from the table and sets the
158   // |was_removed| out parameter to true if the count becomes 0.
159   bool AddToCountOfFormElement(int64 pair_id, int delta, bool* was_removed);
160 
161   // Gets the pair_id and count entries from name and value specified in
162   // |element|.  Sets *pair_id and *count to 0 if there is no such row in
163   // the table.
164   bool GetIDAndCountOfFormElement(const webkit_glue::FormField& element,
165                                   int64* pair_id,
166                                   int* count);
167 
168   // Gets the count only given the pair_id.
169   bool GetCountOfFormElement(int64 pair_id, int* count);
170 
171   // Updates the count entry in the row corresponding to |pair_id| to |count|.
172   bool SetCountOfFormElement(int64 pair_id, int count);
173 
174   // Adds a new row to the autofill table with name and value given in
175   // |element|.  Sets *pair_id to the pair_id of the new row.
176   bool InsertFormElement(const webkit_glue::FormField& element, int64* pair_id);
177 
178   // Adds a new row to the autofill_dates table.
179   bool InsertPairIDAndDate(int64 pair_id, base::Time date_created);
180 
181   // Removes row from the autofill tables given |pair_id|.
182   bool RemoveFormElementForID(int64 pair_id);
183 
184   // Removes row from the autofill tables for the given |name| |value| pair.
185   virtual bool RemoveFormElement(const string16& name, const string16& value);
186 
187   // Retrieves all of the entries in the autofill table.
188   virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries);
189 
190   // Retrieves a single entry from the autofill table.
191   virtual bool GetAutofillTimestamps(const string16& name,
192                              const string16& value,
193                              std::vector<base::Time>* timestamps);
194 
195   // Replaces existing autofill entries with the entries supplied in
196   // the argument.  If the entry does not already exist, it will be
197   // added.
198   virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries);
199 
200   // Records a single Autofill profile in the autofill_profiles table.
201   virtual bool AddAutofillProfile(const AutofillProfile& profile);
202 
203   // Updates the database values for the specified profile.
204   // DEPRECATED: Use |UpdateAutofillProfileMulti| instead.
205   virtual bool UpdateAutofillProfile(const AutofillProfile& profile);
206 
207   // Updates the database values for the specified profile.  Mulit-value aware.
208   virtual bool UpdateAutofillProfileMulti(const AutofillProfile& profile);
209 
210   // Removes a row from the autofill_profiles table.  |guid| is the identifier
211   // of the profile to remove.
212   virtual bool RemoveAutofillProfile(const std::string& guid);
213 
214   // Retrieves a profile with guid |guid|.  The caller owns |profile|.
215   bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile);
216 
217   // Retrieves all profiles in the database.  Caller owns the returned profiles.
218   virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles);
219 
220   // Records a single credit card in the credit_cards table.
221   bool AddCreditCard(const CreditCard& credit_card);
222 
223   // Updates the database values for the specified credit card.
224   bool UpdateCreditCard(const CreditCard& credit_card);
225 
226   // Removes a row from the credit_cards table.  |guid| is the identifer  of the
227   // credit card to remove.
228   bool RemoveCreditCard(const std::string& guid);
229 
230   // Retrieves a credit card with guid |guid|.  The caller owns
231   // |credit_card_id|.
232   bool GetCreditCard(const std::string& guid, CreditCard** credit_card);
233 
234   // Retrieves all credit cards in the database.  Caller owns the returned
235   // credit cards.
236   virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards);
237 
238   // Removes rows from autofill_profiles and credit_cards if they were created
239   // on or after |delete_begin| and strictly before |delete_end|.  Returns lists
240   // of deleted guids in |profile_guids| and |credit_card_guids|.
241   bool RemoveAutofillProfilesAndCreditCardsModifiedBetween(
242       base::Time delete_begin,
243       base::Time delete_end,
244       std::vector<std::string>* profile_guids,
245       std::vector<std::string>* credit_card_guids);
246 
247   // Retrieves all profiles in the database that have been deleted since last
248   // "empty" of the trash.
249   bool GetAutofillProfilesInTrash(std::vector<std::string>* guids);
250 
251   // Empties the Autofill profiles "trash can".
252   bool EmptyAutofillProfilesTrash();
253 
254   // Removes empty values for autofill that were incorrectly stored in the DB
255   // See bug http://crbug.com/6111
256   bool ClearAutofillEmptyValueElements();
257 
258   // Retrieves all profiles in the database that have been deleted since last
259   // "empty" of the trash.
260   bool AddAutofillGUIDToTrash(const std::string& guid);
261 
262   // Clear all profiles.
263   bool ClearAutofillProfiles();
264 
265   // Table migration functions.
266   bool MigrateToVersion23AddCardNumberEncryptedColumn();
267   bool MigrateToVersion24CleanupOversizedStringFields();
268   bool MigrateToVersion27UpdateLegacyCreditCards();
269   bool MigrateToVersion30AddDateModifed();
270   bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles();
271   bool MigrateToVersion32UpdateProfilesAndCreditCards();
272   bool MigrateToVersion33ProfilesBasedOnFirstName();
273   bool MigrateToVersion34ProfilesBasedOnCountryCode();
274   bool MigrateToVersion35GreatBritainCountryCodes();
275   bool MigrateToVersion37MergeAndCullOlderProfiles();
276 
277  private:
278   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill);
279   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges);
280   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges);
281 
282   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace);
283   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues);
284   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile);
285   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile);
286   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash);
287   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction);
288   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
289                            RemoveAutofillProfilesAndCreditCardsModifiedBetween);
290   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard);
291   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard);
292   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
293                            Autofill_GetAllAutofillEntries_OneResult);
294   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
295                            Autofill_GetAllAutofillEntries_TwoDistinct);
296   FRIEND_TEST_ALL_PREFIXES(AutofillTableTest,
297                            Autofill_GetAllAutofillEntries_TwoSame);
298 
299   // Methods for adding autofill entries at a specified time.  For
300   // testing only.
301   bool AddFormFieldValuesTime(
302       const std::vector<webkit_glue::FormField>& elements,
303       std::vector<AutofillChange>* changes,
304       base::Time time);
305   bool AddFormFieldValueTime(const webkit_glue::FormField& element,
306                              std::vector<AutofillChange>* changes,
307                              base::Time time);
308 
309   // Insert a single AutofillEntry into the autofill/autofill_dates tables.
310   bool InsertAutofillEntry(const AutofillEntry& entry);
311 
312   // Checks if the trash is empty.
313   bool IsAutofillProfilesTrashEmpty();
314 
315   // Checks if the guid is in the trash.
316   bool IsAutofillGUIDInTrash(const std::string& guid);
317 
318   bool InitMainTable();
319   bool InitCreditCardsTable();
320   bool InitDatesTable();
321   bool InitProfilesTable();
322   bool InitProfileNamesTable();
323   bool InitProfileEmailsTable();
324   bool InitProfilePhonesTable();
325   bool InitProfileTrashTable();
326 
327   DISALLOW_COPY_AND_ASSIGN(AutofillTable);
328 };
329 
330 #endif  // CHROME_BROWSER_WEBDATA_AUTOFILL_TABLE_H_
331