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