OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/webdata/autofill_web_data_service_impl.h" | 5 #include "chrome/browser/webdata/autofill_web_data_service_impl.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/webdata/autofill_change.h" |
| 9 #include "chrome/browser/webdata/autofill_entry.h" |
| 10 #include "chrome/browser/webdata/autofill_table.h" |
8 #include "chrome/browser/webdata/web_data_service.h" | 11 #include "chrome/browser/webdata/web_data_service.h" |
| 12 #include "chrome/browser/webdata/web_database_service.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "components/autofill/browser/autofill_country.h" |
| 15 #include "components/autofill/browser/autofill_profile.h" |
| 16 #include "components/autofill/browser/credit_card.h" |
| 17 #include "components/autofill/common/form_field_data.h" |
| 18 #include "content/public/browser/notification_details.h" |
| 19 #include "content/public/browser/notification_service.h" |
| 20 #include "content/public/browser/notification_source.h" |
| 21 |
| 22 using base::Bind; |
| 23 using base::Time; |
| 24 using content::BrowserThread; |
| 25 |
| 26 AutofillWebDataService::AutofillWebDataService( |
| 27 scoped_refptr<WebDatabaseService> wdbs, |
| 28 const ProfileErrorCallback& callback) |
| 29 : WebDataServiceBase(wdbs, callback) { |
| 30 } |
9 | 31 |
10 AutofillWebDataServiceImpl::AutofillWebDataServiceImpl( | 32 AutofillWebDataServiceImpl::AutofillWebDataServiceImpl( |
11 scoped_refptr<WebDataService> service) | 33 scoped_refptr<WebDatabaseService> wdbs, |
12 : service_(service) { | 34 const ProfileErrorCallback& callback) |
13 DCHECK(service.get()); | 35 : AutofillWebDataService(wdbs, callback) { |
14 } | |
15 | |
16 AutofillWebDataServiceImpl::~AutofillWebDataServiceImpl() { | |
17 } | 36 } |
18 | 37 |
19 void AutofillWebDataServiceImpl::AddFormFields( | 38 void AutofillWebDataServiceImpl::AddFormFields( |
20 const std::vector<FormFieldData>& fields) { | 39 const std::vector<FormFieldData>& fields) { |
21 service_->AddFormFields(fields); | 40 wdbs_->ScheduleDBTask(FROM_HERE, |
| 41 Bind(&AutofillWebDataServiceImpl::AddFormElementsImpl, this, fields)); |
22 } | 42 } |
23 | 43 |
24 WebDataServiceBase::Handle | 44 WebDataServiceBase::Handle |
25 AutofillWebDataServiceImpl::GetFormValuesForElementName( | 45 AutofillWebDataServiceImpl::GetFormValuesForElementName( |
26 const string16& name, | 46 const string16& name, const string16& prefix, int limit, |
27 const string16& prefix, | |
28 int limit, | |
29 WebDataServiceConsumer* consumer) { | 47 WebDataServiceConsumer* consumer) { |
30 return service_->GetFormValuesForElementName(name, prefix, limit, consumer); | 48 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 49 Bind(&AutofillWebDataServiceImpl::GetFormValuesForElementNameImpl, |
| 50 this, name, prefix, limit), consumer); |
31 } | 51 } |
32 | 52 |
33 void AutofillWebDataServiceImpl::RemoveExpiredFormElements() { | 53 void AutofillWebDataServiceImpl::RemoveExpiredFormElements() { |
34 service_->RemoveExpiredFormElements(); | 54 wdbs_->ScheduleDBTask(FROM_HERE, |
| 55 Bind(&AutofillWebDataServiceImpl::RemoveExpiredFormElementsImpl, this)); |
35 } | 56 } |
36 | 57 |
37 void AutofillWebDataServiceImpl::RemoveFormValueForElementName( | 58 void AutofillWebDataServiceImpl::RemoveFormValueForElementName( |
38 const string16& name, const string16& value) { | 59 const string16& name, const string16& value) { |
39 service_->RemoveFormValueForElementName(name, value); | 60 wdbs_->ScheduleDBTask(FROM_HERE, |
| 61 Bind(&AutofillWebDataServiceImpl::RemoveFormValueForElementNameImpl, |
| 62 this, name, value)); |
40 } | 63 } |
41 | 64 |
42 void AutofillWebDataServiceImpl::AddAutofillProfile( | 65 void AutofillWebDataServiceImpl::AddAutofillProfile( |
43 const AutofillProfile& profile) { | 66 const AutofillProfile& profile) { |
44 service_->AddAutofillProfile(profile); | 67 wdbs_->ScheduleDBTask(FROM_HERE, |
| 68 Bind(&AutofillWebDataServiceImpl::AddAutofillProfileImpl, this, profile)); |
45 } | 69 } |
46 | 70 |
47 void AutofillWebDataServiceImpl::UpdateAutofillProfile( | 71 void AutofillWebDataServiceImpl::UpdateAutofillProfile( |
48 const AutofillProfile& profile) { | 72 const AutofillProfile& profile) { |
49 service_->UpdateAutofillProfile(profile); | 73 wdbs_->ScheduleDBTask(FROM_HERE, |
| 74 Bind(&AutofillWebDataServiceImpl::UpdateAutofillProfileImpl, |
| 75 this, profile)); |
50 } | 76 } |
51 | 77 |
52 void AutofillWebDataServiceImpl::RemoveAutofillProfile( | 78 void AutofillWebDataServiceImpl::RemoveAutofillProfile( |
53 const std::string& guid) { | 79 const std::string& guid) { |
54 service_->RemoveAutofillProfile(guid); | 80 wdbs_->ScheduleDBTask(FROM_HERE, |
| 81 Bind(&AutofillWebDataServiceImpl::RemoveAutofillProfileImpl, this, guid)); |
55 } | 82 } |
56 | 83 |
57 WebDataServiceBase::Handle AutofillWebDataServiceImpl::GetAutofillProfiles( | 84 WebDataServiceBase::Handle AutofillWebDataServiceImpl::GetAutofillProfiles( |
58 WebDataServiceConsumer* consumer) { | 85 WebDataServiceConsumer* consumer) { |
59 return service_->GetAutofillProfiles(consumer); | 86 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 87 Bind(&AutofillWebDataServiceImpl::GetAutofillProfilesImpl, this), |
| 88 consumer); |
60 } | 89 } |
61 | 90 |
62 void AutofillWebDataServiceImpl::AddCreditCard(const CreditCard& credit_card) { | 91 void AutofillWebDataServiceImpl::AddCreditCard(const CreditCard& credit_card) { |
63 service_->AddCreditCard(credit_card); | 92 wdbs_->ScheduleDBTask(FROM_HERE, |
| 93 Bind(&AutofillWebDataServiceImpl::AddCreditCardImpl, this, credit_card)); |
64 } | 94 } |
65 | 95 |
66 void AutofillWebDataServiceImpl::UpdateCreditCard( | 96 void AutofillWebDataServiceImpl::UpdateCreditCard( |
67 const CreditCard& credit_card) { | 97 const CreditCard& credit_card) { |
68 service_->UpdateCreditCard(credit_card); | 98 wdbs_->ScheduleDBTask(FROM_HERE, |
| 99 Bind(&AutofillWebDataServiceImpl::UpdateCreditCardImpl, this, |
| 100 credit_card)); |
69 } | 101 } |
70 | 102 |
71 void AutofillWebDataServiceImpl::RemoveCreditCard(const std::string& guid) { | 103 void AutofillWebDataServiceImpl::RemoveCreditCard(const std::string& guid) { |
72 service_->RemoveCreditCard(guid); | 104 wdbs_->ScheduleDBTask(FROM_HERE, |
73 } | 105 Bind(&AutofillWebDataServiceImpl::RemoveCreditCardImpl, this, guid)); |
74 | 106 } |
75 WebDataServiceBase::Handle | 107 |
76 AutofillWebDataServiceImpl::GetCreditCards(WebDataServiceConsumer* consumer) { | 108 AutofillWebDataServiceImpl::Handle AutofillWebDataServiceImpl::GetCreditCards( |
77 return service_->GetCreditCards(consumer); | 109 WebDataServiceConsumer* consumer) { |
78 } | 110 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
79 | 111 Bind(&AutofillWebDataServiceImpl::GetCreditCardsImpl, this), consumer); |
80 void AutofillWebDataServiceImpl::CancelRequest(Handle h) { | 112 } |
81 service_->CancelRequest(h); | 113 |
82 } | 114 AutofillWebDataServiceImpl::~AutofillWebDataServiceImpl() { |
83 | 115 } |
84 content::NotificationSource | 116 |
85 AutofillWebDataServiceImpl::GetNotificationSource() { | 117 //////////////////////////////////////////////////////////////////////////////// |
86 return service_->GetNotificationSource(); | 118 // |
87 } | 119 // Autofill implementation. |
88 | 120 // |
89 bool AutofillWebDataServiceImpl::IsDatabaseLoaded() { | 121 //////////////////////////////////////////////////////////////////////////////// |
90 return service_->IsDatabaseLoaded(); | 122 |
91 } | 123 WebDatabase::State AutofillWebDataServiceImpl::AddFormElementsImpl( |
92 | 124 const std::vector<FormFieldData>& fields, WebDatabase* db) { |
93 WebDatabase* AutofillWebDataServiceImpl::GetDatabase() { | 125 AutofillChangeList changes; |
94 return service_->GetDatabase(); | 126 if (!db->GetAutofillTable()->AddFormFieldValues(fields, &changes)) { |
95 } | 127 NOTREACHED(); |
| 128 return WebDatabase::COMMIT_NOT_NEEDED; |
| 129 } |
| 130 |
| 131 // Post the notifications including the list of affected keys. |
| 132 // This is sent here so that work resulting from this notification will be |
| 133 // done on the DB thread, and not the UI thread. |
| 134 content::NotificationService::current()->Notify( |
| 135 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 136 content::Source<AutofillWebDataService>(this), |
| 137 content::Details<AutofillChangeList>(&changes)); |
| 138 |
| 139 return WebDatabase::COMMIT_NEEDED; |
| 140 } |
| 141 |
| 142 scoped_ptr<WDTypedResult> |
| 143 AutofillWebDataServiceImpl::GetFormValuesForElementNameImpl( |
| 144 const string16& name, const string16& prefix, int limit, WebDatabase* db) { |
| 145 std::vector<string16> values; |
| 146 db->GetAutofillTable()->GetFormValuesForElementName( |
| 147 name, prefix, &values, limit); |
| 148 return scoped_ptr<WDTypedResult>( |
| 149 new WDResult<std::vector<string16> >(AUTOFILL_VALUE_RESULT, values)); |
| 150 } |
| 151 |
| 152 WebDatabase::State |
| 153 AutofillWebDataServiceImpl::RemoveFormElementsAddedBetweenImpl( |
| 154 const base::Time& delete_begin, const base::Time& delete_end, |
| 155 WebDatabase* db) { |
| 156 AutofillChangeList changes; |
| 157 |
| 158 if (db->GetAutofillTable()->RemoveFormElementsAddedBetween( |
| 159 delete_begin, delete_end, &changes)) { |
| 160 if (!changes.empty()) { |
| 161 // Post the notifications including the list of affected keys. |
| 162 // This is sent here so that work resulting from this notification |
| 163 // will be done on the DB thread, and not the UI thread. |
| 164 content::NotificationService::current()->Notify( |
| 165 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 166 content::Source<AutofillWebDataService>(this), |
| 167 content::Details<AutofillChangeList>(&changes)); |
| 168 } |
| 169 return WebDatabase::COMMIT_NEEDED; |
| 170 } |
| 171 return WebDatabase::COMMIT_NOT_NEEDED; |
| 172 } |
| 173 |
| 174 WebDatabase::State |
| 175 AutofillWebDataServiceImpl::RemoveExpiredFormElementsImpl( |
| 176 WebDatabase* db) { |
| 177 AutofillChangeList changes; |
| 178 |
| 179 if (db->GetAutofillTable()->RemoveExpiredFormElements(&changes)) { |
| 180 if (!changes.empty()) { |
| 181 // Post the notifications including the list of affected keys. |
| 182 // This is sent here so that work resulting from this notification |
| 183 // will be done on the DB thread, and not the UI thread. |
| 184 content::NotificationService::current()->Notify( |
| 185 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 186 content::Source<AutofillWebDataService>(this), |
| 187 content::Details<AutofillChangeList>(&changes)); |
| 188 } |
| 189 return WebDatabase::COMMIT_NEEDED; |
| 190 } |
| 191 return WebDatabase::COMMIT_NOT_NEEDED; |
| 192 } |
| 193 |
| 194 WebDatabase::State |
| 195 AutofillWebDataServiceImpl::RemoveFormValueForElementNameImpl( |
| 196 const string16& name, const string16& value, WebDatabase* db) { |
| 197 |
| 198 if (db->GetAutofillTable()->RemoveFormElement(name, value)) { |
| 199 AutofillChangeList changes; |
| 200 changes.push_back(AutofillChange(AutofillChange::REMOVE, |
| 201 AutofillKey(name, value))); |
| 202 |
| 203 // Post the notifications including the list of affected keys. |
| 204 content::NotificationService::current()->Notify( |
| 205 chrome::NOTIFICATION_AUTOFILL_ENTRIES_CHANGED, |
| 206 content::Source<AutofillWebDataService>(this), |
| 207 content::Details<AutofillChangeList>(&changes)); |
| 208 |
| 209 return WebDatabase::COMMIT_NEEDED; |
| 210 } |
| 211 return WebDatabase::COMMIT_NOT_NEEDED; |
| 212 } |
| 213 |
| 214 WebDatabase::State |
| 215 AutofillWebDataServiceImpl::AddAutofillProfileImpl( |
| 216 const AutofillProfile& profile, WebDatabase* db) { |
| 217 if (!db->GetAutofillTable()->AddAutofillProfile(profile)) { |
| 218 NOTREACHED(); |
| 219 return WebDatabase::COMMIT_NOT_NEEDED; |
| 220 } |
| 221 |
| 222 // Send GUID-based notification. |
| 223 AutofillProfileChange change(AutofillProfileChange::ADD, |
| 224 profile.guid(), &profile); |
| 225 content::NotificationService::current()->Notify( |
| 226 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 227 content::Source<AutofillWebDataService>(this), |
| 228 content::Details<AutofillProfileChange>(&change)); |
| 229 |
| 230 return WebDatabase::COMMIT_NEEDED; |
| 231 } |
| 232 |
| 233 WebDatabase::State |
| 234 AutofillWebDataServiceImpl::UpdateAutofillProfileImpl( |
| 235 const AutofillProfile& profile, WebDatabase* db) { |
| 236 // Only perform the update if the profile exists. It is currently |
| 237 // valid to try to update a missing profile. We simply drop the write and |
| 238 // the caller will detect this on the next refresh. |
| 239 AutofillProfile* original_profile = NULL; |
| 240 if (!db->GetAutofillTable()->GetAutofillProfile(profile.guid(), |
| 241 &original_profile)) { |
| 242 return WebDatabase::COMMIT_NOT_NEEDED; |
| 243 } |
| 244 scoped_ptr<AutofillProfile> scoped_profile(original_profile); |
| 245 |
| 246 if (!db->GetAutofillTable()->UpdateAutofillProfileMulti(profile)) { |
| 247 NOTREACHED(); |
| 248 return WebDatabase::COMMIT_NEEDED; |
| 249 } |
| 250 |
| 251 // Send GUID-based notification. |
| 252 AutofillProfileChange change(AutofillProfileChange::UPDATE, |
| 253 profile.guid(), &profile); |
| 254 content::NotificationService::current()->Notify( |
| 255 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 256 content::Source<AutofillWebDataService>(this), |
| 257 content::Details<AutofillProfileChange>(&change)); |
| 258 |
| 259 return WebDatabase::COMMIT_NEEDED; |
| 260 } |
| 261 |
| 262 WebDatabase::State |
| 263 AutofillWebDataServiceImpl::RemoveAutofillProfileImpl( |
| 264 const std::string& guid, WebDatabase* db) { |
| 265 AutofillProfile* profile = NULL; |
| 266 if (!db->GetAutofillTable()->GetAutofillProfile(guid, &profile)) { |
| 267 NOTREACHED(); |
| 268 return WebDatabase::COMMIT_NOT_NEEDED; |
| 269 } |
| 270 scoped_ptr<AutofillProfile> scoped_profile(profile); |
| 271 |
| 272 if (!db->GetAutofillTable()->RemoveAutofillProfile(guid)) { |
| 273 NOTREACHED(); |
| 274 return WebDatabase::COMMIT_NOT_NEEDED; |
| 275 } |
| 276 |
| 277 // Send GUID-based notification. |
| 278 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); |
| 279 content::NotificationService::current()->Notify( |
| 280 chrome::NOTIFICATION_AUTOFILL_PROFILE_CHANGED, |
| 281 content::Source<AutofillWebDataService>(this), |
| 282 content::Details<AutofillProfileChange>(&change)); |
| 283 |
| 284 return WebDatabase::COMMIT_NEEDED; |
| 285 } |
| 286 |
| 287 scoped_ptr<WDTypedResult> |
| 288 AutofillWebDataServiceImpl::GetAutofillProfilesImpl( |
| 289 WebDatabase* db) { |
| 290 std::vector<AutofillProfile*> profiles; |
| 291 db->GetAutofillTable()->GetAutofillProfiles(&profiles); |
| 292 return scoped_ptr<WDTypedResult>( |
| 293 new WDDestroyableResult<std::vector<AutofillProfile*> >( |
| 294 AUTOFILL_PROFILES_RESULT, |
| 295 profiles, |
| 296 base::Bind(&AutofillWebDataServiceImpl::DestroyAutofillProfileResult, |
| 297 base::Unretained(this)))); |
| 298 } |
| 299 |
| 300 WebDatabase::State |
| 301 AutofillWebDataServiceImpl::AddCreditCardImpl( |
| 302 const CreditCard& credit_card, WebDatabase* db) { |
| 303 if (!db->GetAutofillTable()->AddCreditCard(credit_card)) { |
| 304 NOTREACHED(); |
| 305 return WebDatabase::COMMIT_NOT_NEEDED; |
| 306 } |
| 307 |
| 308 return WebDatabase::COMMIT_NEEDED; |
| 309 } |
| 310 |
| 311 WebDatabase::State |
| 312 AutofillWebDataServiceImpl::UpdateCreditCardImpl( |
| 313 const CreditCard& credit_card, WebDatabase* db) { |
| 314 // It is currently valid to try to update a missing profile. We simply drop |
| 315 // the write and the caller will detect this on the next refresh. |
| 316 CreditCard* original_credit_card = NULL; |
| 317 if (!db->GetAutofillTable()->GetCreditCard(credit_card.guid(), |
| 318 &original_credit_card)) { |
| 319 return WebDatabase::COMMIT_NOT_NEEDED; |
| 320 } |
| 321 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); |
| 322 |
| 323 if (!db->GetAutofillTable()->UpdateCreditCard(credit_card)) { |
| 324 NOTREACHED(); |
| 325 return WebDatabase::COMMIT_NOT_NEEDED; |
| 326 } |
| 327 return WebDatabase::COMMIT_NEEDED; |
| 328 } |
| 329 |
| 330 WebDatabase::State |
| 331 AutofillWebDataServiceImpl::RemoveCreditCardImpl( |
| 332 const std::string& guid, WebDatabase* db) { |
| 333 if (!db->GetAutofillTable()->RemoveCreditCard(guid)) { |
| 334 NOTREACHED(); |
| 335 return WebDatabase::COMMIT_NOT_NEEDED; |
| 336 } |
| 337 return WebDatabase::COMMIT_NEEDED; |
| 338 } |
| 339 |
| 340 scoped_ptr<WDTypedResult> |
| 341 AutofillWebDataServiceImpl::GetCreditCardsImpl(WebDatabase* db) { |
| 342 std::vector<CreditCard*> credit_cards; |
| 343 db->GetAutofillTable()->GetCreditCards(&credit_cards); |
| 344 return scoped_ptr<WDTypedResult>( |
| 345 new WDDestroyableResult<std::vector<CreditCard*> >( |
| 346 AUTOFILL_CREDITCARDS_RESULT, |
| 347 credit_cards, |
| 348 base::Bind(&AutofillWebDataServiceImpl::DestroyAutofillCreditCardResult, |
| 349 base::Unretained(this)))); |
| 350 } |
| 351 |
| 352 void AutofillWebDataServiceImpl::DestroyAutofillProfileResult( |
| 353 const WDTypedResult* result) { |
| 354 DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT); |
| 355 const WDResult<std::vector<AutofillProfile*> >* r = |
| 356 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result); |
| 357 std::vector<AutofillProfile*> profiles = r->GetValue(); |
| 358 STLDeleteElements(&profiles); |
| 359 } |
| 360 |
| 361 void AutofillWebDataServiceImpl::DestroyAutofillCreditCardResult( |
| 362 const WDTypedResult* result) { |
| 363 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); |
| 364 const WDResult<std::vector<CreditCard*> >* r = |
| 365 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); |
| 366 |
| 367 std::vector<CreditCard*> credit_cards = r->GetValue(); |
| 368 STLDeleteElements(&credit_cards); |
| 369 } |
OLD | NEW |