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