web_database.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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_WEB_DATABASE_H_ 6#define CHROME_BROWSER_WEBDATA_WEB_DATABASE_H_ 7#pragma once 8 9#include <vector> 10 11#include "app/sql/connection.h" 12#include "app/sql/init_status.h" 13#include "app/sql/meta_table.h" 14#include "base/gtest_prod_util.h" 15#include "base/scoped_ptr.h" 16#include "base/string16.h" 17#include "chrome/browser/search_engines/template_url_id.h" 18 19class AutofillChange; 20class AutofillEntry; 21class AutoFillProfile; 22class CreditCard; 23class FilePath; 24class GURL; 25class NotificationService; 26class SkBitmap; 27class TemplateURL; 28class WebDatabaseTest; 29 30namespace base { 31class Time; 32} 33 34namespace webkit_glue { 35class FormField; 36struct PasswordForm; 37} 38 39#if defined(OS_WIN) 40struct IE7PasswordInfo; 41#endif 42 43//////////////////////////////////////////////////////////////////////////////// 44// 45// A Sqlite database instance to store all the meta data we have about web pages 46// 47//////////////////////////////////////////////////////////////////////////////// 48class WebDatabase { 49 public: 50 WebDatabase(); 51 virtual ~WebDatabase(); 52 53 // Initialize the database given a name. The name defines where the sqlite 54 // file is. If this returns an error code, no other method should be called. 55 sql::InitStatus Init(const FilePath& db_name); 56 57 // Transactions management 58 void BeginTransaction(); 59 void CommitTransaction(); 60 61 ////////////////////////////////////////////////////////////////////////////// 62 // 63 // Keywords 64 // 65 ////////////////////////////////////////////////////////////////////////////// 66 67 // Adds a new keyword, updating the id field on success. 68 // Returns true if successful. 69 bool AddKeyword(const TemplateURL& url); 70 71 // Removes the specified keyword. 72 // Returns true if successful. 73 bool RemoveKeyword(TemplateURLID id); 74 75 // Loads the keywords into the specified vector. It's up to the caller to 76 // delete the returned objects. 77 // Returns true on success. 78 bool GetKeywords(std::vector<TemplateURL*>* urls); 79 80 // Updates the database values for the specified url. 81 // Returns true on success. 82 bool UpdateKeyword(const TemplateURL& url); 83 84 // ID (TemplateURL->id) of the default search provider. 85 bool SetDefaultSearchProviderID(int64 id); 86 int64 GetDefaulSearchProviderID(); 87 88 // Version of the builtin keywords. 89 bool SetBuitinKeywordVersion(int version); 90 int GetBuitinKeywordVersion(); 91 92 ////////////////////////////////////////////////////////////////////////////// 93 // 94 // Password manager support 95 // 96 ////////////////////////////////////////////////////////////////////////////// 97 98 // Adds |form| to the list of remembered password forms. 99 bool AddLogin(const webkit_glue::PasswordForm& form); 100 101#if defined(OS_WIN) 102 // Adds |info| to the list of imported passwords from ie7/ie8. 103 bool AddIE7Login(const IE7PasswordInfo& info); 104 105 // Removes |info| from the list of imported passwords from ie7/ie8. 106 bool RemoveIE7Login(const IE7PasswordInfo& info); 107 108 // Return the ie7/ie8 login matching |info|. 109 bool GetIE7Login(const IE7PasswordInfo& info, IE7PasswordInfo* result); 110#endif 111 112 // Updates remembered password form. 113 bool UpdateLogin(const webkit_glue::PasswordForm& form); 114 115 // Removes |form| from the list of remembered password forms. 116 bool RemoveLogin(const webkit_glue::PasswordForm& form); 117 118 // Removes all logins created from |delete_begin| onwards (inclusive) and 119 // before |delete_end|. You may use a null Time value to do an unbounded 120 // delete in either direction. 121 bool RemoveLoginsCreatedBetween(base::Time delete_begin, 122 base::Time delete_end); 123 124 // Loads a list of matching password forms into the specified vector |forms|. 125 // The list will contain all possibly relevant entries to the observed |form|, 126 // including blacklisted matches. 127 bool GetLogins(const webkit_glue::PasswordForm& form, 128 std::vector<webkit_glue::PasswordForm*>* forms); 129 130 // Loads the complete list of password forms into the specified vector |forms| 131 // if include_blacklisted is true, otherwise only loads those which are 132 // actually autofill-able; i.e haven't been blacklisted by the user selecting 133 // the 'Never for this site' button. 134 bool GetAllLogins(std::vector<webkit_glue::PasswordForm*>* forms, 135 bool include_blacklisted); 136 137 ////////////////////////////////////////////////////////////////////////////// 138 // 139 // AutoFill 140 // 141 ////////////////////////////////////////////////////////////////////////////// 142 143 // Records the form elements in |elements| in the database in the 144 // autofill table. A list of all added and updated autofill entries 145 // is returned in the changes out parameter. 146 bool AddFormFieldValues(const std::vector<webkit_glue::FormField>& elements, 147 std::vector<AutofillChange>* changes); 148 149 // Records a single form element in the database in the autofill table. A list 150 // of all added and updated autofill entries is returned in the changes out 151 // parameter. 152 bool AddFormFieldValue(const webkit_glue::FormField& element, 153 std::vector<AutofillChange>* changes); 154 155 // Retrieves a vector of all values which have been recorded in the autofill 156 // table as the value in a form element with name |name| and which start with 157 // |prefix|. The comparison of the prefix is case insensitive. 158 bool GetFormValuesForElementName(const string16& name, 159 const string16& prefix, 160 std::vector<string16>* values, 161 int limit); 162 163 // Removes rows from autofill_dates if they were created on or after 164 // |delete_begin| and strictly before |delete_end|. Decrements the 165 // count of the corresponding rows in the autofill table, and 166 // removes those rows if the count goes to 0. A list of all changed 167 // keys and whether each was updater or removed is returned in the 168 // changes out parameter. 169 bool RemoveFormElementsAddedBetween(base::Time delete_begin, 170 base::Time delete_end, 171 std::vector<AutofillChange>* changes); 172 173 // Removes from autofill_dates rows with given pair_id where date_created lies 174 // between delte_begin and delte_end. 175 bool RemoveFormElementForTimeRange(int64 pair_id, 176 base::Time delete_begin, 177 base::Time delete_end, 178 int* how_many); 179 180 // Increments the count in the row corresponding to |pair_id| by 181 // |delta|. Removes the row from the table and sets the 182 // |was_removed| out parameter to true if the count becomes 0. 183 bool AddToCountOfFormElement(int64 pair_id, int delta, bool* was_removed); 184 185 // Gets the pair_id and count entries from name and value specified in 186 // |element|. Sets *count to 0 if there is no such row in the table. 187 bool GetIDAndCountOfFormElement(const webkit_glue::FormField& element, 188 int64* pair_id, 189 int* count); 190 191 // Gets the count only given the pair_id. 192 bool GetCountOfFormElement(int64 pair_id, int* count); 193 194 // Updates the count entry in the row corresponding to |pair_id| to |count|. 195 bool SetCountOfFormElement(int64 pair_id, int count); 196 197 // Adds a new row to the autofill table with name and value given in 198 // |element|. Sets *pair_id to the pair_id of the new row. 199 bool InsertFormElement(const webkit_glue::FormField& element, int64* pair_id); 200 201 // Adds a new row to the autofill_dates table. 202 bool InsertPairIDAndDate(int64 pair_id, base::Time date_created); 203 204 // Removes row from the autofill tables given |pair_id|. 205 bool RemoveFormElementForID(int64 pair_id); 206 207 // Removes row from the autofill tables for the given |name| |value| pair. 208 virtual bool RemoveFormElement(const string16& name, const string16& value); 209 210 // Retrieves all of the entries in the autofill table. 211 virtual bool GetAllAutofillEntries(std::vector<AutofillEntry>* entries); 212 213 // Retrieves a single entry from the autofill table. 214 virtual bool GetAutofillTimestamps(const string16& name, 215 const string16& value, 216 std::vector<base::Time>* timestamps); 217 218 // Replaces existing autofill entries with the entries supplied in 219 // the argument. If the entry does not already exist, it will be 220 // added. 221 virtual bool UpdateAutofillEntries(const std::vector<AutofillEntry>& entries); 222 223 // Records a single AutoFill profile in the autofill_profiles table. 224 virtual bool AddAutoFillProfile(const AutoFillProfile& profile); 225 226 // Updates the database values for the specified profile. 227 virtual bool UpdateAutoFillProfile(const AutoFillProfile& profile); 228 229 // Removes a row from the autofill_profiles table. |profile_id| is the 230 // unique ID of the profile to remove. 231 // DEPRECATED: In favor of |RemoveAutoFillProfile(const std::string& guid)|. 232 // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 233 virtual bool RemoveAutoFillProfile(int profile_id); 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 profile for unique id |profile_id|, owned by caller. 240 // DEPRECATED: In favor of |GetAutoFillProfileForGUID(...)|. 241 // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 242 bool GetAutoFillProfileForID(int profile_id, AutoFillProfile** profile); 243 244 // Retrieves a profile with label |label|. The caller owns |profile|. 245 // DEPRECATED: In favor of |GetAutoFillProfileForGUID(...)|. 246 // TODO(dhollowa): Remove labels. http://crbug.com/58813 247 bool GetAutoFillProfileForLabel(const string16& label, 248 AutoFillProfile** profile); 249 250 // Retrieves a profile with guid |guid|. The caller owns |profile|. 251 bool GetAutoFillProfileForGUID(const std::string& guid, 252 AutoFillProfile** profile); 253 254 // Retrieves all profiles in the database. Caller owns the returned profiles. 255 virtual bool GetAutoFillProfiles(std::vector<AutoFillProfile*>* profiles); 256 257 // Records a single credit card in the credit_cards table. 258 bool AddCreditCard(const CreditCard& credit_card); 259 260 // Updates the database values for the specified credit card. 261 bool UpdateCreditCard(const CreditCard& credit_card); 262 263 // Removes a row from the credit_cards table. |credit_card_id| is the 264 // unique ID of the credit card to remove. 265 // DEPRECATED: In favor of |RemoveCreditCard(const std::string& guid)|. 266 // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 267 bool RemoveCreditCard(int credit_card_id); 268 269 // Removes a row from the credit_cards table. |guid| is the identifer of the 270 // credit card to remove. 271 bool RemoveCreditCard(const std::string& guid); 272 273 // Retrieves a credit card with label |label|. The caller owns 274 // |credit_card_id|. 275 // DEPRECATED: In favor of |GetCreditCardForGUID()|. 276 // TODO(dhollowa): Remove labels. http://crbug.com/58813 277 bool GetCreditCardForLabel(const string16& label, 278 CreditCard** credit_card); 279 280 // Retrieves credit card for a card with unique id |credit_card_id|. 281 // DEPRECATED: In favor of |GetCreditCardForGUID()|. 282 // TODO(dhollowa): Remove unique IDs. http://crbug.com/58813 283 bool GetCreditCardForID(int credit_card_id, CreditCard** credit_card); 284 285 // Retrieves a credit card with guid |guid|. The caller owns 286 // |credit_card_id|. 287 bool GetCreditCardForGUID(const std::string& guid, CreditCard** credit_card); 288 289 // Retrieves all credit cards in the database. Caller owns the returned 290 // credit cards. 291 virtual bool GetCreditCards(std::vector<CreditCard*>* credit_cards); 292 293 // Removes rows from autofill_profiles and credit_cards if they were created 294 // on or after |delete_begin| and strictly before |delete_end|. 295 bool RemoveAutoFillProfilesAndCreditCardsModifiedBetween( 296 base::Time delete_begin, 297 base::Time delete_end); 298 299 ////////////////////////////////////////////////////////////////////////////// 300 // 301 // Web Apps 302 // 303 ////////////////////////////////////////////////////////////////////////////// 304 305 bool SetWebAppImage(const GURL& url, const SkBitmap& image); 306 bool GetWebAppImages(const GURL& url, std::vector<SkBitmap>* images); 307 308 bool SetWebAppHasAllImages(const GURL& url, bool has_all_images); 309 bool GetWebAppHasAllImages(const GURL& url); 310 311 bool RemoveWebApp(const GURL& url); 312 313 ////////////////////////////////////////////////////////////////////////////// 314 // 315 // Token Service 316 // 317 ////////////////////////////////////////////////////////////////////////////// 318 319 // Remove all tokens previously set with SetTokenForService. 320 bool RemoveAllTokens(); 321 322 // Retrieves all tokens previously set with SetTokenForService. 323 // Returns true if there were tokens and we decrypted them, 324 // false if there was a failure somehow 325 bool GetAllTokens(std::map<std::string, std::string>* tokens); 326 327 // Store a token in the token_service table. Stored encrypted. May cause 328 // a mac keychain popup. 329 // True if we encrypted a token and stored it, false otherwise. 330 bool SetTokenForService(const std::string& service, 331 const std::string& token); 332 333 private: 334 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill); 335 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill_AddChanges); 336 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill_RemoveBetweenChanges); 337 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, 338 Autofill_GetAllAutofillEntries_OneResult); 339 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, 340 Autofill_GetAllAutofillEntries_TwoDistinct); 341 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, 342 Autofill_GetAllAutofillEntries_TwoSame); 343 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill_UpdateDontReplace); 344 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, Autofill_AddFormFieldValues); 345 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, AutoFillProfile); 346 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, CreditCard); 347 FRIEND_TEST_ALL_PREFIXES(WebDatabaseTest, 348 RemoveAutoFillProfilesAndCreditCardsModifiedBetween); 349 350 // Methods for adding autofill entries at a specified time. For 351 // testing only. 352 bool AddFormFieldValuesTime( 353 const std::vector<webkit_glue::FormField>& elements, 354 std::vector<AutofillChange>* changes, 355 base::Time time); 356 bool AddFormFieldValueTime(const webkit_glue::FormField& element, 357 std::vector<AutofillChange>* changes, 358 base::Time time); 359 360 // Removes empty values for autofill that were incorrectly stored in the DB 361 // (see bug http://crbug.com/6111). 362 // TODO(jcampan): http://crbug.com/7564 remove when we think all users have 363 // run this code. 364 bool ClearAutofillEmptyValueElements(); 365 366 // Insert a single AutofillEntry into the autofill/autofill_dates tables. 367 bool InsertAutofillEntry(const AutofillEntry& entry); 368 369 bool InitKeywordsTable(); 370 bool InitLoginsTable(); 371 bool InitAutofillTable(); 372 bool InitAutofillDatesTable(); 373 bool InitAutoFillProfilesTable(); 374 bool InitCreditCardsTable(); 375 bool InitTokenServiceTable(); 376 bool InitWebAppIconsTable(); 377 bool InitWebAppsTable(); 378 379 // Used by |Init()| to migration database schema from older versions to 380 // current version. 381 sql::InitStatus MigrateOldVersionsAsNeeded(); 382 383 sql::Connection db_; 384 sql::MetaTable meta_table_; 385 386 scoped_ptr<NotificationService> notification_service_; 387 388 DISALLOW_COPY_AND_ASSIGN(WebDatabase); 389}; 390 391#endif // CHROME_BROWSER_WEBDATA_WEB_DATABASE_H_ 392