autofill_table.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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 16class WebDatabase; 17 18namespace base { 19class Time; 20} 21 22namespace autofill { 23 24class AutofillChange; 25class AutofillEntry; 26class AutofillProfile; 27class AutofillTableTest; 28class CreditCard; 29 30struct 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// 73// autofill_profile_names 74// This table contains the multi-valued name fields 75// associated with a profile. 76// 77// guid The guid string that identifies the profile to which 78// the name belongs. 79// first_name 80// middle_name 81// last_name 82// 83// autofill_profile_emails 84// This table contains the multi-valued email fields 85// associated with a profile. 86// 87// guid The guid string that identifies the profile to which 88// the email belongs. 89// email 90// 91// autofill_profile_phones 92// This table contains the multi-valued phone fields 93// associated with a profile. 94// 95// guid The guid string that identifies the profile to which the 96// phone number belongs. 97// number 98// 99// autofill_profiles_trash 100// This table contains guids of "trashed" autofill 101// profiles. When a profile is removed its guid is added 102// to this table so that Sync can perform deferred removal. 103// 104// guid The guid string that identifies the trashed profile. 105// 106// credit_cards This table contains credit card data added by the user 107// with the Autofill dialog. Most of the columns are 108// standard entries in a credit card form. 109// 110// guid A guid string to uniquely identify the profile. 111// Added in version 31. 112// name_on_card 113// expiration_month 114// expiration_year 115// card_number_encrypted 116// Stores encrypted credit card number. 117// date_modified The date on which this entry was last modified. 118// Added in version 30. 119// origin The domain of origin for this profile. 120// Added in version 50. 121// 122class AutofillTable : public WebDatabaseTable { 123 public: 124 explicit AutofillTable(const std::string& app_locale); 125 virtual ~AutofillTable(); 126 127 // Retrieves the AutofillTable* owned by |database|. 128 static AutofillTable* FromWebDatabase(WebDatabase* db); 129 130 virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE; 131 virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE; 132 virtual bool IsSyncable() OVERRIDE; 133 virtual bool MigrateToVersion(int version, 134 bool* update_compatible_version) OVERRIDE; 135 136 // Records the form elements in |elements| in the database in the 137 // autofill table. A list of all added and updated autofill entries 138 // is returned in the changes out parameter. 139 bool AddFormFieldValues(const std::vector<FormFieldData>& elements, 140 std::vector<AutofillChange>* changes); 141 142 // Records a single form element in the database in the autofill table. A list 143 // of all added and updated autofill entries is returned in the changes out 144 // parameter. 145 bool AddFormFieldValue(const FormFieldData& element, 146 std::vector<AutofillChange>* changes); 147 148 // Retrieves a vector of all values which have been recorded in the autofill 149 // table as the value in a form element with name |name| and which start with 150 // |prefix|. The comparison of the prefix is case insensitive. 151 bool GetFormValuesForElementName(const base::string16& name, 152 const base::string16& prefix, 153 std::vector<base::string16>* values, 154 int limit); 155 156 // Returns whether any form elements are stored in the database. 157 bool HasFormElements(); 158 159 // Removes rows from the autofill table if they were created on or after 160 // |delete_begin| and last used strictly before |delete_end|. For rows where 161 // the time range [date_created, date_last_used] overlaps with [delete_begin, 162 // delete_end), but is not entirely contained within the latter range, updates 163 // the rows so that their resulting time range [new_date_created, 164 // new_date_last_used] lies entirely outside of [delete_begin, delete_end), 165 // updating the count accordingly. A list of all changed keys and whether 166 // each was updater or removed is returned in the 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 the autofill table if they were last accessed strictly 172 // before |AutofillEntry::ExpirationTime()|. 173 bool RemoveExpiredFormElements(std::vector<AutofillChange>* changes); 174 175 // Removes the row from the autofill table for the given |name| |value| pair. 176 virtual bool RemoveFormElement(const base::string16& name, 177 const base::string16& value); 178 179 // Retrieves all of the entries in the autofill table. 180 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries); 181 182 // Retrieves a single entry from the autofill table. 183 virtual bool GetAutofillTimestamps(const base::string16& name, 184 const base::string16& value, 185 base::Time* date_created, 186 base::Time* date_last_used); 187 188 // Replaces existing autofill entries with the entries supplied in 189 // the argument. If the entry does not already exist, it will be 190 // added. 191 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries); 192 193 // Records a single Autofill profile in the autofill_profiles table. 194 virtual bool AddAutofillProfile(const AutofillProfile& profile); 195 196 // Updates the database values for the specified profile. Mulit-value aware. 197 virtual bool UpdateAutofillProfile(const AutofillProfile& profile); 198 199 // Removes a row from the autofill_profiles table. |guid| is the identifier 200 // of the profile to remove. 201 virtual bool RemoveAutofillProfile(const std::string& guid); 202 203 // Retrieves a profile with guid |guid|. The caller owns |profile|. 204 bool GetAutofillProfile(const std::string& guid, AutofillProfile** profile); 205 206 // Retrieves all profiles in the database. Caller owns the returned profiles. 207 virtual bool GetAutofillProfiles(std::vector<AutofillProfile*>* profiles); 208 209 // Records a single credit card in the credit_cards table. 210 bool AddCreditCard(const CreditCard& credit_card); 211 212 // Updates the database values for the specified credit card. 213 bool UpdateCreditCard(const CreditCard& credit_card); 214 215 // Removes a row from the credit_cards table. |guid| is the identifer of the 216 // credit card to remove. 217 bool RemoveCreditCard(const std::string& guid); 218 219 // Retrieves a credit card with guid |guid|. The caller owns 220 // |credit_card_id|. 221 bool GetCreditCard(const std::string& guid, CreditCard** credit_card); 222 223 // Retrieves all credit cards in the database. Caller owns the returned 224 // credit cards. 225 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards); 226 227 // Removes rows from autofill_profiles and credit_cards if they were created 228 // on or after |delete_begin| and strictly before |delete_end|. Returns the 229 // list of deleted profile guids in |profile_guids|. Return value is true if 230 // all rows were successfully removed. Returns false on database error. In 231 // that case, the output vector state is undefined, and may be partially 232 // filled. 233 bool RemoveAutofillDataModifiedBetween( 234 const base::Time& delete_begin, 235 const base::Time& delete_end, 236 std::vector<std::string>* profile_guids, 237 std::vector<std::string>* credit_card_guids); 238 239 // Removes origin URLs from the autofill_profiles and credit_cards tables if 240 // they were written on or after |delete_begin| and strictly before 241 // |delete_end|. Returns the list of modified profiles in |profiles|. Return 242 // value is true if all rows were successfully updated. Returns false on 243 // database error. In that case, the output vector state is undefined, and 244 // may be partially filled. 245 bool RemoveOriginURLsModifiedBetween( 246 const base::Time& delete_begin, 247 const base::Time& delete_end, 248 ScopedVector<AutofillProfile>* profiles); 249 250 // Retrieves all profiles in the database that have been deleted since last 251 // "empty" of the trash. 252 bool GetAutofillProfilesInTrash(std::vector<std::string>* guids); 253 254 // Empties the Autofill profiles "trash can". 255 bool EmptyAutofillProfilesTrash(); 256 257 // Retrieves all profiles in the database that have been deleted since last 258 // "empty" of the trash. 259 bool AddAutofillGUIDToTrash(const std::string& guid); 260 261 // Clear all profiles. 262 bool ClearAutofillProfiles(); 263 264 // Table migration functions. 265 // Removes empty values for autofill that were incorrectly stored in the DB 266 // See bug http://crbug.com/6111 267 bool MigrateToVersion22ClearAutofillEmptyValueElements(); 268 bool MigrateToVersion23AddCardNumberEncryptedColumn(); 269 bool MigrateToVersion24CleanupOversizedStringFields(); 270 bool MigrateToVersion27UpdateLegacyCreditCards(); 271 bool MigrateToVersion30AddDateModifed(); 272 bool MigrateToVersion31AddGUIDToCreditCardsAndProfiles(); 273 bool MigrateToVersion32UpdateProfilesAndCreditCards(); 274 bool MigrateToVersion33ProfilesBasedOnFirstName(); 275 bool MigrateToVersion34ProfilesBasedOnCountryCode(); 276 bool MigrateToVersion35GreatBritainCountryCodes(); 277 bool MigrateToVersion37MergeAndCullOlderProfiles(); 278 bool MigrateToVersion51AddOriginColumn(); 279 bool MigrateToVersion54AddI18nFieldsAndRemoveDeprecatedFields(); 280 bool MigrateToVersion55MergeAutofillDatesTable(); 281 282 // Max data length saved in the table; 283 static const size_t kMaxDataLength; 284 285 private: 286 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill); 287 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddChanges); 288 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_RemoveBetweenChanges); 289 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_UpdateDontReplace); 290 FRIEND_TEST_ALL_PREFIXES( 291 AutofillTableTest, 292 Autofill_RemoveFormElementsAddedBetween_UsedOnlyBefore); 293 FRIEND_TEST_ALL_PREFIXES( 294 AutofillTableTest, 295 Autofill_RemoveFormElementsAddedBetween_UsedOnlyAfter); 296 FRIEND_TEST_ALL_PREFIXES( 297 AutofillTableTest, 298 Autofill_RemoveFormElementsAddedBetween_UsedOnlyDuring); 299 FRIEND_TEST_ALL_PREFIXES( 300 AutofillTableTest, 301 Autofill_RemoveFormElementsAddedBetween_UsedBeforeAndDuring); 302 FRIEND_TEST_ALL_PREFIXES( 303 AutofillTableTest, 304 Autofill_RemoveFormElementsAddedBetween_UsedDuringAndAfter); 305 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, Autofill_AddFormFieldValues); 306 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfile); 307 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateAutofillProfile); 308 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrash); 309 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, AutofillProfileTrashInteraction); 310 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, 311 RemoveAutofillDataModifiedBetween); 312 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, CreditCard); 313 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, UpdateCreditCard); 314 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, 315 Autofill_GetAllAutofillEntries_OneResult); 316 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, 317 Autofill_GetAllAutofillEntries_TwoDistinct); 318 FRIEND_TEST_ALL_PREFIXES(AutofillTableTest, 319 Autofill_GetAllAutofillEntries_TwoSame); 320 321 // Methods for adding autofill entries at a specified time. For 322 // testing only. 323 bool AddFormFieldValuesTime( 324 const std::vector<FormFieldData>& elements, 325 std::vector<AutofillChange>* changes, 326 base::Time time); 327 bool AddFormFieldValueTime(const FormFieldData& element, 328 std::vector<AutofillChange>* changes, 329 base::Time time); 330 331 // Insert a single AutofillEntry into the autofill table. 332 bool InsertAutofillEntry(const AutofillEntry& entry); 333 334 // Checks if the trash is empty. 335 bool IsAutofillProfilesTrashEmpty(); 336 337 // Checks if the guid is in the trash. 338 bool IsAutofillGUIDInTrash(const std::string& guid); 339 340 bool InitMainTable(); 341 bool InitCreditCardsTable(); 342 bool InitDatesTable(); 343 bool InitProfilesTable(); 344 bool InitProfileNamesTable(); 345 bool InitProfileEmailsTable(); 346 bool InitProfilePhonesTable(); 347 bool InitProfileTrashTable(); 348 349 // The application locale. The locale is needed for the migration to version 350 // 35. Since it must be read on the UI thread, it is set when the table is 351 // created (on the UI thread), and cached here so that it can be used for 352 // migrations (on the DB thread). 353 std::string app_locale_; 354 355 DISALLOW_COPY_AND_ASSIGN(AutofillTable); 356}; 357 358} // namespace autofill 359 360#endif // COMPONENTS_AUTOFILL_CORE_BROWSER_WEBDATA_AUTOFILL_TABLE_H_ 361