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