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 const std::string& app_locale, | |
129 bool* update_compatible_version) OVERRIDE; | |
130 | |
131 // Records the form elements in |elements| in the database in the | |
132 // autofill table. A list of all added and updated autofill entries | |
133 // is returned in the changes out parameter. | |
134 bool AddFormFieldValues(const std::vector<FormFieldData>& elements, | |
135 std::vector<AutofillChange>* changes); | |
136 | |
137 // Records a single form element in the database in the autofill table. A list | |
138 // of all added and updated autofill entries is returned in the changes out | |
139 // parameter. | |
140 bool AddFormFieldValue(const FormFieldData& element, | |
141 std::vector<AutofillChange>* changes); | |
142 | |
143 // Retrieves a vector of all values which have been recorded in the autofill | |
144 // table as the value in a form element with name |name| and which start with | |
145 // |prefix|. The comparison of the prefix is case insensitive. | |
146 bool GetFormValuesForElementName(const string16& name, | |
147 const string16& prefix, | |
148 std::vector<string16>* values, | |
149 int limit); | |
150 | |
151 // Removes rows from autofill_dates if they were created on or after | |
152 // |delete_begin| and strictly before |delete_end|. Decrements the | |
153 // count of the corresponding rows in the autofill table, and | |
154 // removes those rows if the count goes to 0. A list of all changed | |
155 // keys and whether each was updater or removed is returned in the | |
156 // changes out parameter. | |
157 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin, | |
158 const base::Time& delete_end, | |
159 std::vector<AutofillChange>* changes); | |
160 | |
161 // Removes rows from autofill_dates if they were accessed strictly before | |
162 // |AutofillEntry::ExpirationTime()|. Removes the corresponding row from the | |
163 // autofill table. Also culls timestamps to only two. TODO(georgey): remove | |
164 // culling in future versions. | |
165 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes); | |
166 | |
167 // Removes from autofill_dates rows with given pair_id where date_created lies | |
168 // between |delete_begin| and |delete_end|. | |
169 bool RemoveFormElementForTimeRange(int64 pair_id, | |
170 const base::Time& delete_begin, | |
171 const base::Time& delete_end, | |
172 int* how_many); | |
173 | |
174 // Increments the count in the row corresponding to |pair_id| by |delta|. | |
175 bool AddToCountOfFormElement(int64 pair_id, int delta); | |
176 | |
177 // Counts how many timestamp data rows are in the |autofill_dates| table for | |
178 // a given |pair_id|. GetCountOfFormElement() on the other hand gives the | |
179 // |count| property for a given id. | |
180 int CountTimestampsData(int64 pair_id); | |
181 | |
182 // Gets the pair_id and count entries from name and value specified in | |
183 // |element|. Sets *pair_id and *count to 0 if there is no such row in | |
184 // the table. | |
185 bool GetIDAndCountOfFormElement(const FormFieldData& element, | |
186 int64* pair_id, | |
187 int* count); | |
188 | |
189 // Gets the count only given the pair_id. | |
190 bool GetCountOfFormElement(int64 pair_id, int* count); | |
191 | |
192 // Updates the count entry in the row corresponding to |pair_id| to |count|. | |
193 bool SetCountOfFormElement(int64 pair_id, int count); | |
194 | |
195 // Adds a new row to the autofill table with name and value given in | |
196 // |element|. Sets *pair_id to the pair_id of the new row. | |
197 bool InsertFormElement(const FormFieldData& element, | |
198 int64* pair_id); | |
199 | |
200 // Adds a new row to the autofill_dates table. | |
201 bool InsertPairIDAndDate(int64 pair_id, const base::Time& date_created); | |
202 | |
203 // Deletes last access to the Autofill data from the autofill_dates table. | |
204 bool DeleteLastAccess(int64 pair_id); | |
205 | |
206 // Removes row from the autofill tables given |pair_id|. | |
207 bool RemoveFormElementForID(int64 pair_id); | |
208 | |
209 // Removes row from the autofill tables for the given |name| |value| pair. | |
210 virtual bool RemoveFormElement(const string16& name, const string16& value); | |
211 | |
212 // Retrieves all of the entries in the autofill table. | |
213 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries); | |
214 | |
215 // Retrieves a single entry from the autofill table. | |
216 virtual bool GetAutofillTimestamps(const string16& name, | |
217 const string16& value, | |
218 std::vector<base::Time>* timestamps); | |
219 | |
220 // Replaces existing autofill entries with the entries supplied in | |
221 // the argument. If the entry does not already exist, it will be | |
222 // added. | |
223 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries); | |
224 | |
225 // Records a single Autofill profile in the autofill_profiles table. | |
226 virtual bool AddAutofillProfile(const AutofillProfile& profile); | |
227 | |
228 // Updates the database values for the specified profile. | |
229 // DEPRECATED: Use |UpdateAutofillProfileMulti| instead. | |
230 virtual bool UpdateAutofillProfile(const AutofillProfile& profile); | |
231 | |
232 // Updates the database values for the specified profile. Mulit-value aware. | |
233 virtual bool UpdateAutofillProfileMulti(const AutofillProfile& profile); | |
234 | |
235 // Removes a row from the autofill_profiles table. |guid| is the identifier | |
236 // of the profile to remove. | |
237 virtual bool RemoveAutofillProfile(const std::string& guid); | |
238 | |
239 // Retrieves a profile with guid |guid|. The caller owns |profile|. | |
240 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile); | |
241 | |
242 // Retrieves all profiles in the database. Caller owns the returned profiles. | |
243 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles); | |
244 | |
245 // Records a single credit card in the credit_cards table. | |
246 bool AddCreditCard(const CreditCard& credit_card); | |
247 | |
248 // Updates the database values for the specified credit card. | |
249 bool UpdateCreditCard(const CreditCard& credit_card); | |
250 | |
251 // Removes a row from the credit_cards table. |guid| is the identifer of the | |
252 // credit card to remove. | |
253 bool RemoveCreditCard(const std::string& guid); | |
254 | |
255 // Retrieves a credit card with guid |guid|. The caller owns | |
256 // |credit_card_id|. | |
257 bool GetCreditCard(const std::string& guid, CreditCard** credit_card); | |
258 | |
259 // Retrieves all credit cards in the database. Caller owns the returned | |
260 // credit cards. | |
261 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards); | |
262 | |
263 // Removes rows from autofill_profiles and credit_cards if they were created | |
264 // on or after |delete_begin| and strictly before |delete_end|. Returns lists | |
265 // of deleted guids in |profile_guids| and |credit_card_guids|. Return value | |
266 // is true if all rows were successfully removed. Returns false on database | |
267 // error. In that case, the output vector state is undefined, and may be | |
268 // partially filled. | |
269 bool RemoveAutofillDataModifiedBetween( | |
270 const base::Time& delete_begin, | |
271 const base::Time& delete_end, | |
272 std::vector<std::string>* profile_guids, | |
273 std::vector<std::string>* credit_card_guids); | |
274 | |
275 // Retrieves all profiles in the database that have been deleted since last | |
276 // "empty" of the trash. | |
277 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids); | |
278 | |
279 // Empties the Autofill profiles "trash can". | |
280 bool EmptyAutofillProfilesTrash(); | |
281 | |
282 // Removes empty values for autofill that were incorrectly stored in the DB | |
283 // See bug http://crbug.com/6111 | |
284 bool ClearAutofillEmptyValueElements(); | |
285 | |
286 // Retrieves all profiles in the database that have been deleted since last | |
287 // "empty" of the trash. | |
288 bool AddAutofillGUIDToTrash(const std::string& guid); | |
289 | |
290 // Clear all profiles. | |
291 bool ClearAutofillProfiles(); | |
292 | |
293 // Table migration functions. | |
294 bool MigrateToVersion23AddCardNumberEncryptedColumn(); | |
295 bool MigrateToVersion24CleanupOversizedStringFields(); | |
296 bool MigrateToVersion27UpdateLegacyCreditCards(); | |
297 bool MigrateToVersion30AddDateModifed(); | |
298 bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles(); | |
299 bool MigrateToVersion32UpdateProfilesAndCreditCards(); | |
300 bool MigrateToVersion33ProfilesBasedOnFirstName(); | |
301 bool MigrateToVersion34ProfilesBasedOnCountryCode( | |
302 const std::string& app_locale); | |
303 bool MigrateToVersion35GreatBritainCountryCodes(); | |
304 bool MigrateToVersion37MergeAndCullOlderProfiles(); | |
305 | |
306 // Max data length saved in the table; | |
307 static const size_t kMaxDataLength; | |
308 | |
309 private: | |
310 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill); | |
311 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges); | |
312 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges); | |
313 | |
314 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace); | |
315 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues); | |
316 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile); | |
317 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile); | |
318 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash); | |
319 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction); | |
320 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, | |
321 RemoveAutofillDataModifiedBetween); | |
322 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard); | |
323 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard); | |
324 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, | |
325 Autofill_GetAllAutofillEntries_OneResult); | |
326 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, | |
327 Autofill_GetAllAutofillEntries_TwoDistinct); | |
328 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, | |
329 Autofill_GetAllAutofillEntries_TwoSame); | |
330 | |
331 // Methods for adding autofill entries at a specified time. For | |
332 // testing only. | |
333 bool AddFormFieldValuesTime( | |
334 const std::vector<FormFieldData>& elements, | |
335 std::vector<AutofillChange>* changes, | |
336 base::Time time); | |
337 bool AddFormFieldValueTime(const FormFieldData& element, | |
338 std::vector<AutofillChange>* changes, | |
339 base::Time time); | |
340 | |
341 // Insert a single AutofillEntry into the autofill/autofill_dates tables. | |
342 bool InsertAutofillEntry(const AutofillEntry& entry); | |
343 | |
344 // Checks if the trash is empty. | |
345 bool IsAutofillProfilesTrashEmpty(); | |
346 | |
347 // Checks if the guid is in the trash. | |
348 bool IsAutofillGUIDInTrash(const std::string& guid); | |
349 | |
350 bool InitMainTable(); | |
351 bool InitCreditCardsTable(); | |
352 bool InitDatesTable(); | |
353 bool InitProfilesTable(); | |
354 bool InitProfileNamesTable(); | |
355 bool InitProfileEmailsTable(); | |
356 bool InitProfilePhonesTable(); | |
357 bool InitProfileTrashTable(); | |
358 | |
359 DISALLOW_COPY_AND_ASSIGN(AutofillTable); | |
360 }; | |
361 | |
362 #endif // CHROME_BROWSER_WEBDATA_AUTOFILL_TABLE_H_ | |
OLD | NEW |