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 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 // 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 // full_name 88 // 89 // autofill_profile_emails 90 // This table contains the multi-valued email fields 91 // associated with a profile. 92 // 93 // guid The guid string that identifies the profile to which 94 // the email belongs. 95 // email 96 // 97 // autofill_profile_phones 98 // This table contains the multi-valued phone fields 99 // associated with a profile. 100 // 101 // guid The guid string that identifies the profile to which the 102 // phone number belongs. 103 // number 104 // 105 // autofill_profiles_trash 106 // This table contains guids of "trashed" autofill 107 // profiles. When a profile is removed its guid is added 108 // to this table so that Sync can perform deferred removal. 109 // 110 // guid The guid string that identifies the trashed profile. 111 // 112 // credit_cards This table contains credit card data added by the user 113 // with the Autofill dialog. Most of the columns are 114 // standard entries in a credit card form. 115 // 116 // guid A guid string to uniquely identify the profile. 117 // Added in version 31. 118 // name_on_card 119 // expiration_month 120 // expiration_year 121 // card_number_encrypted 122 // Stores encrypted credit card number. 123 // date_modified The date on which this entry was last modified. 124 // Added in version 30. 125 // origin The domain of origin for this profile. 126 // Added in version 50. 127 // 128 class AutofillTable : public WebDatabaseTable { 129 public: 130 explicit AutofillTable(const std::string& app_locale); 131 virtual ~AutofillTable(); 132 133 // Retrieves the AutofillTable* owned by |database|. 134 static AutofillTable* FromWebDatabase(WebDatabase* db); 135 136 virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; 137 virtual bool CreateTablesIfNecessary() OVERRIDE; 138 virtual bool IsSyncable() OVERRIDE; 139 virtual bool MigrateToVersion(int version, 140 bool* update_compatible_version) OVERRIDE; 141 142 // Records the form elements in |elements| in the database in the 143 // autofill table. A list of all added and updated autofill entries 144 // is returned in the changes out parameter. 145 bool AddFormFieldValues(const std::vector<FormFieldData>& elements, 146 std::vector<AutofillChange>* changes); 147 148 // Records a single form element in the database in the autofill table. A list 149 // of all added and updated autofill entries is returned in the changes out 150 // parameter. 151 bool AddFormFieldValue(const FormFieldData& element, 152 std::vector<AutofillChange>* changes); 153 154 // Retrieves a vector of all values which have been recorded in the autofill 155 // table as the value in a form element with name |name| and which start with 156 // |prefix|. The comparison of the prefix is case insensitive. 157 bool GetFormValuesForElementName(const base::string16& name, 158 const base::string16& prefix, 159 std::vector<base::string16>* values, 160 int limit); 161 162 // Returns whether any form elements are stored in the database. 163 bool HasFormElements(); 164 165 // Removes rows from the autofill table if they were created on or after 166 // |delete_begin| and last used strictly before |delete_end|. For rows where 167 // the time range [date_created, date_last_used] overlaps with [delete_begin, 168 // delete_end), but is not entirely contained within the latter range, updates 169 // the rows so that their resulting time range [new_date_created, 170 // new_date_last_used] lies entirely outside of [delete_begin, delete_end), 171 // updating the count accordingly. A list of all changed keys and whether 172 // each was updater or removed is returned in the changes out parameter. 173 bool RemoveFormElementsAddedBetween(const base::Time& delete_begin, 174 const base::Time& delete_end, 175 std::vector<AutofillChange>* changes); 176 177 // Removes rows from the autofill table if they were last accessed strictly 178 // before |AutofillEntry::ExpirationTime()|. 179 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes); 180 181 // Removes the row from the autofill table for the given |name| |value| pair. 182 virtual bool RemoveFormElement(const base::string16& name, 183 const base::string16& value); 184 185 // Retrieves all of the entries in the autofill table. 186 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries); 187 188 // Retrieves a single entry from the autofill table. 189 virtual bool GetAutofillTimestamps(const base::string16& name, 190 const base::string16& value, 191 base::Time* date_created, 192 base::Time* date_last_used); 193 194 // Replaces existing autofill entries with the entries supplied in 195 // the argument. If the entry does not already exist, it will be 196 // added. 197 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries); 198 199 // Records a single Autofill profile in the autofill_profiles table. 200 virtual bool AddAutofillProfile(const AutofillProfile& profile); 201 202 // Updates the database values for the specified profile. Mulit-value aware. 203 virtual bool UpdateAutofillProfile(const AutofillProfile& profile); 204 205 // Removes a row from the autofill_profiles table. |guid| is the identifier 206 // of the profile to remove. 207 virtual bool RemoveAutofillProfile(const std::string& guid); 208 209 // Retrieves a profile with guid |guid|. The caller owns |profile|. 210 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile); 211 212 // Retrieves all profiles in the database. Caller owns the returned profiles. 213 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles); 214 215 // Records a single credit card in the credit_cards table. 216 bool AddCreditCard(const CreditCard& credit_card); 217 218 // Updates the database values for the specified credit card. 219 bool UpdateCreditCard(const CreditCard& credit_card); 220 221 // Removes a row from the credit_cards table. |guid| is the identifer of the 222 // credit card to remove. 223 bool RemoveCreditCard(const std::string& guid); 224 225 // Retrieves a credit card with guid |guid|. The caller owns 226 // |credit_card_id|. 227 bool GetCreditCard(const std::string& guid, CreditCard** credit_card); 228 229 // Retrieves all credit cards in the database. Caller owns the returned 230 // credit cards. 231 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards); 232 233 // Removes rows from autofill_profiles and credit_cards if they were created 234 // on or after |delete_begin| and strictly before |delete_end|. Returns the 235 // list of deleted profile guids in |profile_guids|. Return value is true if 236 // all rows were successfully removed. Returns false on database error. In 237 // that case, the output vector state is undefined, and may be partially 238 // filled. 239 bool RemoveAutofillDataModifiedBetween( 240 const base::Time& delete_begin, 241 const base::Time& delete_end, 242 std::vector<std::string>* profile_guids, 243 std::vector<std::string>* credit_card_guids); 244 245 // Removes origin URLs from the autofill_profiles and credit_cards tables if 246 // they were written on or after |delete_begin| and strictly before 247 // |delete_end|. Returns the list of modified profiles in |profiles|. Return 248 // value is true if all rows were successfully updated. Returns false on 249 // database error. In that case, the output vector state is undefined, and 250 // may be partially filled. 251 bool RemoveOriginURLsModifiedBetween( 252 const base::Time& delete_begin, 253 const base::Time& delete_end, 254 ScopedVector<AutofillProfile>* profiles); 255 256 // Retrieves all profiles in the database that have been deleted since last 257 // "empty" of the trash. 258 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids); 259 260 // Empties the Autofill profiles "trash can". 261 bool EmptyAutofillProfilesTrash(); 262 263 // Retrieves all profiles in the database that have been deleted since last 264 // "empty" of the trash. 265 bool AddAutofillGUIDToTrash(const std::string& guid); 266 267 // Clear all profiles. 268 bool ClearAutofillProfiles(); 269 270 // Table migration functions. 271 // Removes empty values for autofill that were incorrectly stored in the DB 272 // See bug http://crbug.com/6111 273 bool MigrateToVersion22ClearAutofillEmptyValueElements(); 274 bool MigrateToVersion23AddCardNumberEncryptedColumn(); 275 bool MigrateToVersion24CleanupOversizedStringFields(); 276 bool MigrateToVersion27UpdateLegacyCreditCards(); 277 bool MigrateToVersion30AddDateModifed(); 278 bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles(); 279 bool MigrateToVersion32UpdateProfilesAndCreditCards(); 280 bool MigrateToVersion33ProfilesBasedOnFirstName(); 281 bool MigrateToVersion34ProfilesBasedOnCountryCode(); 282 bool MigrateToVersion35GreatBritainCountryCodes(); 283 bool MigrateToVersion37MergeAndCullOlderProfiles(); 284 bool MigrateToVersion51AddOriginColumn(); 285 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields(); 286 bool MigrateToVersion55MergeAutofillDatesTable(); 287 bool MigrateToVersion56AddProfileLanguageCodeForFormatting(); 288 bool MigrateToVersion57AddFullNameField(); 289 290 // Max data length saved in the table; 291 static const size_t kMaxDataLength; 292 293 private: 294 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill); 295 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges); 296 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges); 297 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace); 298 FRIEND_TEST_ALL_PREFIXES( 299 AutofillTableTest, 300 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore); 301 FRIEND_TEST_ALL_PREFIXES( 302 AutofillTableTest, 303 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter); 304 FRIEND_TEST_ALL_PREFIXES( 305 AutofillTableTest, 306 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring); 307 FRIEND_TEST_ALL_PREFIXES( 308 AutofillTableTest, 309 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring); 310 FRIEND_TEST_ALL_PREFIXES( 311 AutofillTableTest, 312 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter); 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 table. 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 } // namespace autofill 367 368 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_ 369