| 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 #include "components/autofill/browser/webdata/autofill_webdata_backend.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "components/autofill/browser/autofill_country.h" | |
| 10 #include "components/autofill/browser/autofill_profile.h" | |
| 11 #include "components/autofill/browser/credit_card.h" | |
| 12 #include "components/autofill/browser/webdata/autofill_change.h" | |
| 13 #include "components/autofill/browser/webdata/autofill_entry.h" | |
| 14 #include "components/autofill/browser/webdata/autofill_table.h" | |
| 15 #include "components/autofill/browser/webdata/autofill_webdata_service_observer.
h" | |
| 16 #include "components/autofill/common/form_field_data.h" | |
| 17 | |
| 18 using base::Bind; | |
| 19 using base::Time; | |
| 20 using content::BrowserThread; | |
| 21 | |
| 22 namespace autofill { | |
| 23 | |
| 24 AutofillWebDataBackend::AutofillWebDataBackend() { | |
| 25 } | |
| 26 | |
| 27 void AutofillWebDataBackend::AddObserver( | |
| 28 AutofillWebDataServiceObserverOnDBThread* observer) { | |
| 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 30 db_observer_list_.AddObserver(observer); | |
| 31 } | |
| 32 | |
| 33 void AutofillWebDataBackend::RemoveObserver( | |
| 34 AutofillWebDataServiceObserverOnDBThread* observer) { | |
| 35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 36 db_observer_list_.RemoveObserver(observer); | |
| 37 } | |
| 38 | |
| 39 AutofillWebDataBackend::~AutofillWebDataBackend() { | |
| 40 } | |
| 41 | |
| 42 WebDatabase::State AutofillWebDataBackend::AddFormElements( | |
| 43 const std::vector<FormFieldData>& fields, WebDatabase* db) { | |
| 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 45 AutofillChangeList changes; | |
| 46 if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( | |
| 47 fields, &changes)) { | |
| 48 NOTREACHED(); | |
| 49 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 50 } | |
| 51 | |
| 52 // Post the notifications including the list of affected keys. | |
| 53 // This is sent here so that work resulting from this notification will be | |
| 54 // done on the DB thread, and not the UI thread. | |
| 55 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 56 db_observer_list_, | |
| 57 AutofillEntriesChanged(changes)); | |
| 58 | |
| 59 return WebDatabase::COMMIT_NEEDED; | |
| 60 } | |
| 61 | |
| 62 scoped_ptr<WDTypedResult> | |
| 63 AutofillWebDataBackend::GetFormValuesForElementName( | |
| 64 const base::string16& name, const base::string16& prefix, int limit, | |
| 65 WebDatabase* db) { | |
| 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 67 std::vector<base::string16> values; | |
| 68 AutofillTable::FromWebDatabase(db)->GetFormValuesForElementName( | |
| 69 name, prefix, &values, limit); | |
| 70 return scoped_ptr<WDTypedResult>( | |
| 71 new WDResult<std::vector<base::string16> >(AUTOFILL_VALUE_RESULT, | |
| 72 values)); | |
| 73 } | |
| 74 | |
| 75 scoped_ptr<WDTypedResult> AutofillWebDataBackend::HasFormElements( | |
| 76 WebDatabase* db) { | |
| 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 78 bool value = AutofillTable::FromWebDatabase(db)->HasFormElements(); | |
| 79 return scoped_ptr<WDTypedResult>( | |
| 80 new WDResult<bool>(AUTOFILL_VALUE_RESULT, value)); | |
| 81 } | |
| 82 | |
| 83 WebDatabase::State AutofillWebDataBackend::RemoveFormElementsAddedBetween( | |
| 84 const base::Time& delete_begin, const base::Time& delete_end, | |
| 85 WebDatabase* db) { | |
| 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 87 AutofillChangeList changes; | |
| 88 | |
| 89 if (AutofillTable::FromWebDatabase(db)->RemoveFormElementsAddedBetween( | |
| 90 delete_begin, delete_end, &changes)) { | |
| 91 if (!changes.empty()) { | |
| 92 // Post the notifications including the list of affected keys. | |
| 93 // This is sent here so that work resulting from this notification | |
| 94 // will be done on the DB thread, and not the UI thread. | |
| 95 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 96 db_observer_list_, | |
| 97 AutofillEntriesChanged(changes)); | |
| 98 } | |
| 99 return WebDatabase::COMMIT_NEEDED; | |
| 100 } | |
| 101 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 102 } | |
| 103 | |
| 104 WebDatabase::State AutofillWebDataBackend::RemoveExpiredFormElements( | |
| 105 WebDatabase* db) { | |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 107 AutofillChangeList changes; | |
| 108 | |
| 109 if (AutofillTable::FromWebDatabase(db)->RemoveExpiredFormElements(&changes)) { | |
| 110 if (!changes.empty()) { | |
| 111 // Post the notifications including the list of affected keys. | |
| 112 // This is sent here so that work resulting from this notification | |
| 113 // will be done on the DB thread, and not the UI thread. | |
| 114 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 115 db_observer_list_, | |
| 116 AutofillEntriesChanged(changes)); | |
| 117 } | |
| 118 return WebDatabase::COMMIT_NEEDED; | |
| 119 } | |
| 120 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 121 } | |
| 122 | |
| 123 WebDatabase::State AutofillWebDataBackend::RemoveFormValueForElementName( | |
| 124 const base::string16& name, const base::string16& value, WebDatabase* db) { | |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 126 | |
| 127 if (AutofillTable::FromWebDatabase(db)->RemoveFormElement(name, value)) { | |
| 128 AutofillChangeList changes; | |
| 129 changes.push_back( | |
| 130 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); | |
| 131 | |
| 132 // Post the notifications including the list of affected keys. | |
| 133 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 134 db_observer_list_, | |
| 135 AutofillEntriesChanged(changes)); | |
| 136 | |
| 137 return WebDatabase::COMMIT_NEEDED; | |
| 138 } | |
| 139 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 140 } | |
| 141 | |
| 142 WebDatabase::State AutofillWebDataBackend::AddAutofillProfile( | |
| 143 const AutofillProfile& profile, WebDatabase* db) { | |
| 144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 145 if (!AutofillTable::FromWebDatabase(db)->AddAutofillProfile(profile)) { | |
| 146 NOTREACHED(); | |
| 147 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 148 } | |
| 149 | |
| 150 // Send GUID-based notification. | |
| 151 AutofillProfileChange change( | |
| 152 AutofillProfileChange::ADD, profile.guid(), &profile); | |
| 153 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 154 db_observer_list_, | |
| 155 AutofillProfileChanged(change)); | |
| 156 | |
| 157 return WebDatabase::COMMIT_NEEDED; | |
| 158 } | |
| 159 | |
| 160 WebDatabase::State AutofillWebDataBackend::UpdateAutofillProfile( | |
| 161 const AutofillProfile& profile, WebDatabase* db) { | |
| 162 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 163 // Only perform the update if the profile exists. It is currently | |
| 164 // valid to try to update a missing profile. We simply drop the write and | |
| 165 // the caller will detect this on the next refresh. | |
| 166 AutofillProfile* original_profile = NULL; | |
| 167 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(profile.guid(), | |
| 168 &original_profile)) { | |
| 169 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 170 } | |
| 171 scoped_ptr<AutofillProfile> scoped_profile(original_profile); | |
| 172 | |
| 173 if (!AutofillTable::FromWebDatabase(db)->UpdateAutofillProfileMulti( | |
| 174 profile)) { | |
| 175 NOTREACHED(); | |
| 176 return WebDatabase::COMMIT_NEEDED; | |
| 177 } | |
| 178 | |
| 179 // Send GUID-based notification. | |
| 180 AutofillProfileChange change( | |
| 181 AutofillProfileChange::UPDATE, profile.guid(), &profile); | |
| 182 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 183 db_observer_list_, | |
| 184 AutofillProfileChanged(change)); | |
| 185 | |
| 186 return WebDatabase::COMMIT_NEEDED; | |
| 187 } | |
| 188 | |
| 189 WebDatabase::State AutofillWebDataBackend::RemoveAutofillProfile( | |
| 190 const std::string& guid, WebDatabase* db) { | |
| 191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 192 AutofillProfile* profile = NULL; | |
| 193 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(guid, &profile)) { | |
| 194 NOTREACHED(); | |
| 195 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 196 } | |
| 197 scoped_ptr<AutofillProfile> scoped_profile(profile); | |
| 198 | |
| 199 if (!AutofillTable::FromWebDatabase(db)->RemoveAutofillProfile(guid)) { | |
| 200 NOTREACHED(); | |
| 201 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 202 } | |
| 203 | |
| 204 // Send GUID-based notification. | |
| 205 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); | |
| 206 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 207 db_observer_list_, | |
| 208 AutofillProfileChanged(change)); | |
| 209 | |
| 210 return WebDatabase::COMMIT_NEEDED; | |
| 211 } | |
| 212 | |
| 213 scoped_ptr<WDTypedResult> AutofillWebDataBackend::GetAutofillProfiles( | |
| 214 WebDatabase* db) { | |
| 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 216 std::vector<AutofillProfile*> profiles; | |
| 217 AutofillTable::FromWebDatabase(db)->GetAutofillProfiles(&profiles); | |
| 218 return scoped_ptr<WDTypedResult>( | |
| 219 new WDDestroyableResult<std::vector<AutofillProfile*> >( | |
| 220 AUTOFILL_PROFILES_RESULT, | |
| 221 profiles, | |
| 222 base::Bind(&AutofillWebDataBackend::DestroyAutofillProfileResult, | |
| 223 base::Unretained(this)))); | |
| 224 } | |
| 225 | |
| 226 WebDatabase::State AutofillWebDataBackend::AddCreditCard( | |
| 227 const CreditCard& credit_card, WebDatabase* db) { | |
| 228 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 229 if (!AutofillTable::FromWebDatabase(db)->AddCreditCard(credit_card)) { | |
| 230 NOTREACHED(); | |
| 231 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 232 } | |
| 233 | |
| 234 return WebDatabase::COMMIT_NEEDED; | |
| 235 } | |
| 236 | |
| 237 WebDatabase::State AutofillWebDataBackend::UpdateCreditCard( | |
| 238 const CreditCard& credit_card, WebDatabase* db) { | |
| 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 240 // It is currently valid to try to update a missing profile. We simply drop | |
| 241 // the write and the caller will detect this on the next refresh. | |
| 242 CreditCard* original_credit_card = NULL; | |
| 243 if (!AutofillTable::FromWebDatabase(db)->GetCreditCard(credit_card.guid(), | |
| 244 &original_credit_card)) { | |
| 245 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 246 } | |
| 247 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); | |
| 248 | |
| 249 if (!AutofillTable::FromWebDatabase(db)->UpdateCreditCard(credit_card)) { | |
| 250 NOTREACHED(); | |
| 251 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 252 } | |
| 253 return WebDatabase::COMMIT_NEEDED; | |
| 254 } | |
| 255 | |
| 256 WebDatabase::State AutofillWebDataBackend::RemoveCreditCard( | |
| 257 const std::string& guid, WebDatabase* db) { | |
| 258 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 259 if (!AutofillTable::FromWebDatabase(db)->RemoveCreditCard(guid)) { | |
| 260 NOTREACHED(); | |
| 261 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 262 } | |
| 263 return WebDatabase::COMMIT_NEEDED; | |
| 264 } | |
| 265 | |
| 266 scoped_ptr<WDTypedResult> AutofillWebDataBackend::GetCreditCards( | |
| 267 WebDatabase* db) { | |
| 268 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 269 std::vector<CreditCard*> credit_cards; | |
| 270 AutofillTable::FromWebDatabase(db)->GetCreditCards(&credit_cards); | |
| 271 return scoped_ptr<WDTypedResult>( | |
| 272 new WDDestroyableResult<std::vector<CreditCard*> >( | |
| 273 AUTOFILL_CREDITCARDS_RESULT, | |
| 274 credit_cards, | |
| 275 base::Bind(&AutofillWebDataBackend::DestroyAutofillCreditCardResult, | |
| 276 base::Unretained(this)))); | |
| 277 } | |
| 278 | |
| 279 WebDatabase::State | |
| 280 AutofillWebDataBackend::RemoveAutofillDataModifiedBetween( | |
| 281 const base::Time& delete_begin, | |
| 282 const base::Time& delete_end, | |
| 283 WebDatabase* db) { | |
| 284 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 285 std::vector<std::string> profile_guids; | |
| 286 std::vector<std::string> credit_card_guids; | |
| 287 if (AutofillTable::FromWebDatabase(db)->RemoveAutofillDataModifiedBetween( | |
| 288 delete_begin, | |
| 289 delete_end, | |
| 290 &profile_guids, | |
| 291 &credit_card_guids)) { | |
| 292 for (std::vector<std::string>::iterator iter = profile_guids.begin(); | |
| 293 iter != profile_guids.end(); ++iter) { | |
| 294 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, NULL); | |
| 295 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 296 db_observer_list_, | |
| 297 AutofillProfileChanged(change)); | |
| 298 } | |
| 299 // Note: It is the caller's responsibility to post notifications for any | |
| 300 // changes, e.g. by calling the Refresh() method of PersonalDataManager. | |
| 301 return WebDatabase::COMMIT_NEEDED; | |
| 302 } | |
| 303 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 304 } | |
| 305 | |
| 306 void AutofillWebDataBackend::DestroyAutofillProfileResult( | |
| 307 const WDTypedResult* result) { | |
| 308 DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT); | |
| 309 const WDResult<std::vector<AutofillProfile*> >* r = | |
| 310 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result); | |
| 311 std::vector<AutofillProfile*> profiles = r->GetValue(); | |
| 312 STLDeleteElements(&profiles); | |
| 313 } | |
| 314 | |
| 315 void AutofillWebDataBackend::DestroyAutofillCreditCardResult( | |
| 316 const WDTypedResult* result) { | |
| 317 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); | |
| 318 const WDResult<std::vector<CreditCard*> >* r = | |
| 319 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | |
| 320 | |
| 321 std::vector<CreditCard*> credit_cards = r->GetValue(); | |
| 322 STLDeleteElements(&credit_cards); | |
| 323 } | |
| 324 | |
| 325 } // namespace autofill | |
| OLD | NEW |