Chromium Code Reviews| 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 "components/autofill/browser/webdata/autofill_webdata_service.h" | 5 #include "components/autofill/browser/webdata/autofill_webdata_service.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "components/autofill/browser/autofill_country.h" | 9 #include "components/autofill/browser/autofill_country.h" |
| 10 #include "components/autofill/browser/autofill_profile.h" | 10 #include "components/autofill/browser/autofill_profile.h" |
| 11 #include "components/autofill/browser/credit_card.h" | 11 #include "components/autofill/browser/credit_card.h" |
| 12 #include "components/autofill/browser/webdata/autofill_change.h" | 12 #include "components/autofill/browser/webdata/autofill_change.h" |
| 13 #include "components/autofill/browser/webdata/autofill_entry.h" | 13 #include "components/autofill/browser/webdata/autofill_entry.h" |
| 14 #include "components/autofill/browser/webdata/autofill_table.h" | 14 #include "components/autofill/browser/webdata/autofill_table.h" |
| 15 #include "components/autofill/browser/webdata/autofill_webdata_backend.h" | |
| 15 #include "components/autofill/browser/webdata/autofill_webdata_service_observer. h" | 16 #include "components/autofill/browser/webdata/autofill_webdata_service_observer. h" |
| 16 #include "components/autofill/common/form_field_data.h" | 17 #include "components/autofill/common/form_field_data.h" |
| 17 #include "components/webdata/common/web_database_service.h" | 18 #include "components/webdata/common/web_database_service.h" |
| 18 | 19 |
| 19 using base::Bind; | 20 using base::Bind; |
| 20 using base::Time; | 21 using base::Time; |
| 21 using content::BrowserThread; | 22 using content::BrowserThread; |
| 22 | 23 |
| 23 namespace autofill { | 24 namespace autofill { |
| 24 | 25 |
| 25 // static | 26 // static |
| 26 void AutofillWebDataService::NotifyOfMultipleAutofillChanges( | 27 void AutofillWebDataService::NotifyOfMultipleAutofillChanges( |
| 27 AutofillWebDataService* web_data_service) { | 28 AutofillWebDataService* web_data_service) { |
| 28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 29 | 30 |
| 30 if (!web_data_service) | 31 if (!web_data_service) |
| 31 return; | 32 return; |
| 32 | 33 |
| 33 BrowserThread::PostTask( | 34 BrowserThread::PostTask( |
| 34 BrowserThread::UI, FROM_HERE, | 35 BrowserThread::UI, FROM_HERE, |
| 35 Bind(&AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread, | 36 Bind(&AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread, |
| 36 make_scoped_refptr(web_data_service))); | 37 make_scoped_refptr(web_data_service))); |
| 37 } | 38 } |
| 38 | 39 |
| 39 AutofillWebDataService::AutofillWebDataService( | 40 AutofillWebDataService::AutofillWebDataService( |
| 40 scoped_refptr<WebDatabaseService> wdbs, | 41 scoped_refptr<WebDatabaseService> wdbs, |
| 41 const ProfileErrorCallback& callback) | 42 const ProfileErrorCallback& callback) |
| 42 : WebDataServiceBase(wdbs, callback) { | 43 : WebDataServiceBase(wdbs, callback), |
| 44 autofill_backend_(new AutofillWebDataBackend()) { | |
| 43 } | 45 } |
| 44 | 46 |
| 45 AutofillWebDataService::AutofillWebDataService() | 47 AutofillWebDataService::AutofillWebDataService() |
| 46 : WebDataServiceBase(NULL, | 48 : WebDataServiceBase(NULL, |
| 47 WebDataServiceBase::ProfileErrorCallback()) { | 49 WebDataServiceBase::ProfileErrorCallback()), |
| 50 autofill_backend_(new AutofillWebDataBackend()) { | |
| 48 } | 51 } |
| 49 | 52 |
| 50 void AutofillWebDataService::ShutdownOnUIThread() { | 53 void AutofillWebDataService::ShutdownOnUIThread() { |
| 51 BrowserThread::PostTask( | 54 BrowserThread::PostTask( |
| 52 BrowserThread::DB, FROM_HERE, | 55 BrowserThread::DB, FROM_HERE, |
| 53 base::Bind(&AutofillWebDataService::ShutdownOnDBThread, this)); | 56 base::Bind(&AutofillWebDataService::ShutdownOnDBThread, this)); |
| 54 WebDataServiceBase::ShutdownOnUIThread(); | 57 WebDataServiceBase::ShutdownOnUIThread(); |
| 55 } | 58 } |
| 56 | 59 |
| 57 void AutofillWebDataService::AddFormFields( | 60 void AutofillWebDataService::AddFormFields( |
| 58 const std::vector<FormFieldData>& fields) { | 61 const std::vector<FormFieldData>& fields) { |
| 59 wdbs_->ScheduleDBTask(FROM_HERE, | 62 wdbs_->ScheduleDBTask(FROM_HERE, |
| 60 Bind(&AutofillWebDataService::AddFormElementsImpl, this, fields)); | 63 Bind(&AutofillWebDataBackend::AddFormElements, |
| 64 autofill_backend_, fields)); | |
| 61 } | 65 } |
| 62 | 66 |
| 63 WebDataServiceBase::Handle AutofillWebDataService::GetFormValuesForElementName( | 67 WebDataServiceBase::Handle AutofillWebDataService::GetFormValuesForElementName( |
| 64 const base::string16& name, const base::string16& prefix, int limit, | 68 const base::string16& name, const base::string16& prefix, int limit, |
| 65 WebDataServiceConsumer* consumer) { | 69 WebDataServiceConsumer* consumer) { |
| 66 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, | 70 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 67 Bind(&AutofillWebDataService::GetFormValuesForElementNameImpl, | 71 Bind(&AutofillWebDataBackend::GetFormValuesForElementName, |
| 68 this, name, prefix, limit), consumer); | 72 autofill_backend_, name, prefix, limit), consumer); |
| 69 } | 73 } |
| 70 | 74 |
| 71 void AutofillWebDataService::RemoveFormElementsAddedBetween( | 75 void AutofillWebDataService::RemoveFormElementsAddedBetween( |
| 72 const Time& delete_begin, const Time& delete_end) { | 76 const Time& delete_begin, const Time& delete_end) { |
| 73 wdbs_->ScheduleDBTask(FROM_HERE, | 77 wdbs_->ScheduleDBTask(FROM_HERE, |
| 74 Bind(&AutofillWebDataService::RemoveFormElementsAddedBetweenImpl, | 78 Bind(&AutofillWebDataBackend::RemoveFormElementsAddedBetween, |
| 75 this, delete_begin, delete_end)); | 79 autofill_backend_, delete_begin, delete_end)); |
| 76 } | 80 } |
| 77 | 81 |
| 78 void AutofillWebDataService::RemoveExpiredFormElements() { | 82 void AutofillWebDataService::RemoveExpiredFormElements() { |
| 79 wdbs_->ScheduleDBTask(FROM_HERE, | 83 wdbs_->ScheduleDBTask(FROM_HERE, |
| 80 Bind(&AutofillWebDataService::RemoveExpiredFormElementsImpl, this)); | 84 Bind(&AutofillWebDataBackend::RemoveExpiredFormElements, |
| 85 autofill_backend_)); | |
| 81 } | 86 } |
| 82 | 87 |
| 83 void AutofillWebDataService::RemoveFormValueForElementName( | 88 void AutofillWebDataService::RemoveFormValueForElementName( |
| 84 const base::string16& name, const base::string16& value) { | 89 const base::string16& name, const base::string16& value) { |
| 85 wdbs_->ScheduleDBTask(FROM_HERE, | 90 wdbs_->ScheduleDBTask(FROM_HERE, |
| 86 Bind(&AutofillWebDataService::RemoveFormValueForElementNameImpl, | 91 Bind(&AutofillWebDataBackend::RemoveFormValueForElementName, |
| 87 this, name, value)); | 92 autofill_backend_, name, value)); |
| 88 } | 93 } |
| 89 | 94 |
| 90 void AutofillWebDataService::AddAutofillProfile( | 95 void AutofillWebDataService::AddAutofillProfile( |
| 91 const AutofillProfile& profile) { | 96 const AutofillProfile& profile) { |
| 92 wdbs_->ScheduleDBTask(FROM_HERE, | 97 wdbs_->ScheduleDBTask(FROM_HERE, |
| 93 Bind(&AutofillWebDataService::AddAutofillProfileImpl, this, profile)); | 98 Bind(&AutofillWebDataBackend::AddAutofillProfile, |
| 99 autofill_backend_, profile)); | |
| 94 } | 100 } |
| 95 | 101 |
| 96 void AutofillWebDataService::UpdateAutofillProfile( | 102 void AutofillWebDataService::UpdateAutofillProfile( |
| 97 const AutofillProfile& profile) { | 103 const AutofillProfile& profile) { |
| 98 wdbs_->ScheduleDBTask(FROM_HERE, | 104 wdbs_->ScheduleDBTask(FROM_HERE, |
| 99 Bind(&AutofillWebDataService::UpdateAutofillProfileImpl, | 105 Bind(&AutofillWebDataBackend::UpdateAutofillProfile, |
| 100 this, profile)); | 106 autofill_backend_, profile)); |
| 101 } | 107 } |
| 102 | 108 |
| 103 void AutofillWebDataService::RemoveAutofillProfile( | 109 void AutofillWebDataService::RemoveAutofillProfile( |
| 104 const std::string& guid) { | 110 const std::string& guid) { |
| 105 wdbs_->ScheduleDBTask(FROM_HERE, | 111 wdbs_->ScheduleDBTask(FROM_HERE, |
| 106 Bind(&AutofillWebDataService::RemoveAutofillProfileImpl, this, guid)); | 112 Bind(&AutofillWebDataBackend::RemoveAutofillProfile, |
| 113 autofill_backend_, guid)); | |
| 107 } | 114 } |
| 108 | 115 |
| 109 WebDataServiceBase::Handle AutofillWebDataService::GetAutofillProfiles( | 116 WebDataServiceBase::Handle AutofillWebDataService::GetAutofillProfiles( |
| 110 WebDataServiceConsumer* consumer) { | 117 WebDataServiceConsumer* consumer) { |
| 111 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, | 118 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 112 Bind(&AutofillWebDataService::GetAutofillProfilesImpl, this), | 119 Bind(&AutofillWebDataBackend::GetAutofillProfiles, autofill_backend_), |
| 113 consumer); | 120 consumer); |
| 114 } | 121 } |
| 115 | 122 |
| 116 void AutofillWebDataService::AddCreditCard(const CreditCard& credit_card) { | 123 void AutofillWebDataService::AddCreditCard(const CreditCard& credit_card) { |
| 117 wdbs_->ScheduleDBTask( | 124 wdbs_->ScheduleDBTask( |
| 118 FROM_HERE, | 125 FROM_HERE, |
| 119 Bind(&AutofillWebDataService::AddCreditCardImpl, this, credit_card)); | 126 Bind(&AutofillWebDataBackend::AddCreditCard, |
| 127 autofill_backend_, credit_card)); | |
| 120 } | 128 } |
| 121 | 129 |
| 122 void AutofillWebDataService::UpdateCreditCard( | 130 void AutofillWebDataService::UpdateCreditCard( |
| 123 const CreditCard& credit_card) { | 131 const CreditCard& credit_card) { |
| 124 wdbs_->ScheduleDBTask( | 132 wdbs_->ScheduleDBTask( |
| 125 FROM_HERE, | 133 FROM_HERE, |
| 126 Bind(&AutofillWebDataService::UpdateCreditCardImpl, this, credit_card)); | 134 Bind(&AutofillWebDataBackend::UpdateCreditCard, |
| 135 autofill_backend_, credit_card)); | |
| 127 } | 136 } |
| 128 | 137 |
| 129 void AutofillWebDataService::RemoveCreditCard(const std::string& guid) { | 138 void AutofillWebDataService::RemoveCreditCard(const std::string& guid) { |
| 130 wdbs_->ScheduleDBTask( | 139 wdbs_->ScheduleDBTask( |
| 131 FROM_HERE, | 140 FROM_HERE, |
| 132 Bind(&AutofillWebDataService::RemoveCreditCardImpl, this, guid)); | 141 Bind(&AutofillWebDataBackend::RemoveCreditCard, |
| 142 autofill_backend_, guid)); | |
|
Ilya Sherman
2013/04/30 23:52:11
nit: Looks like this fits on the previous line
Cait (Slow)
2013/05/01 23:14:20
Done.
| |
| 133 } | 143 } |
| 134 | 144 |
| 135 WebDataServiceBase::Handle AutofillWebDataService::GetCreditCards( | 145 WebDataServiceBase::Handle AutofillWebDataService::GetCreditCards( |
| 136 WebDataServiceConsumer* consumer) { | 146 WebDataServiceConsumer* consumer) { |
| 137 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, | 147 return wdbs_->ScheduleDBTaskWithResult(FROM_HERE, |
| 138 Bind(&AutofillWebDataService::GetCreditCardsImpl, this), consumer); | 148 Bind(&AutofillWebDataBackend::GetCreditCards, autofill_backend_), |
| 149 consumer); | |
| 139 } | 150 } |
| 140 | 151 |
| 141 void AutofillWebDataService::RemoveAutofillDataModifiedBetween( | 152 void AutofillWebDataService::RemoveAutofillDataModifiedBetween( |
| 142 const Time& delete_begin, | 153 const Time& delete_begin, |
| 143 const Time& delete_end) { | 154 const Time& delete_end) { |
| 144 wdbs_->ScheduleDBTask( | 155 wdbs_->ScheduleDBTask( |
| 145 FROM_HERE, | 156 FROM_HERE, |
| 146 Bind(&AutofillWebDataService::RemoveAutofillDataModifiedBetweenImpl, | 157 Bind(&AutofillWebDataBackend::RemoveAutofillDataModifiedBetween, |
| 147 this, delete_begin, delete_end)); | 158 autofill_backend_, delete_begin, delete_end)); |
| 148 } | 159 } |
| 149 | 160 |
| 150 void AutofillWebDataService::AddObserver( | 161 void AutofillWebDataService::AddObserver( |
| 151 AutofillWebDataServiceObserverOnDBThread* observer) { | 162 AutofillWebDataServiceObserverOnDBThread* observer) { |
| 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 163 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 153 db_observer_list_.AddObserver(observer); | 164 if (autofill_backend_) |
| 165 autofill_backend_->AddObserver(observer); | |
| 154 } | 166 } |
| 155 | 167 |
| 156 void AutofillWebDataService::RemoveObserver( | 168 void AutofillWebDataService::RemoveObserver( |
| 157 AutofillWebDataServiceObserverOnDBThread* observer) { | 169 AutofillWebDataServiceObserverOnDBThread* observer) { |
| 158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 159 db_observer_list_.RemoveObserver(observer); | 171 if (autofill_backend_) |
| 172 autofill_backend_->RemoveObserver(observer); | |
| 160 } | 173 } |
| 161 | 174 |
| 162 void AutofillWebDataService::AddObserver( | 175 void AutofillWebDataService::AddObserver( |
| 163 AutofillWebDataServiceObserverOnUIThread* observer) { | 176 AutofillWebDataServiceObserverOnUIThread* observer) { |
| 164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 165 ui_observer_list_.AddObserver(observer); | 178 ui_observer_list_.AddObserver(observer); |
| 166 } | 179 } |
| 167 | 180 |
| 168 void AutofillWebDataService::RemoveObserver( | 181 void AutofillWebDataService::RemoveObserver( |
| 169 AutofillWebDataServiceObserverOnUIThread* observer) { | 182 AutofillWebDataServiceObserverOnUIThread* observer) { |
| 170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 183 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 171 ui_observer_list_.RemoveObserver(observer); | 184 ui_observer_list_.RemoveObserver(observer); |
| 172 } | 185 } |
| 173 | 186 |
| 174 base::SupportsUserData* AutofillWebDataService::GetDBUserData() { | 187 base::SupportsUserData* AutofillWebDataService::GetDBUserData() { |
| 175 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 176 if (!db_thread_user_data_) | 189 if (!db_thread_user_data_) |
| 177 db_thread_user_data_.reset(new SupportsUserDataAggregatable()); | 190 db_thread_user_data_.reset(new SupportsUserDataAggregatable()); |
| 178 return db_thread_user_data_.get(); | 191 return db_thread_user_data_.get(); |
| 179 } | 192 } |
| 180 | 193 |
| 181 void AutofillWebDataService::ShutdownOnDBThread() { | 194 void AutofillWebDataService::ShutdownOnDBThread() { |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | 195 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); |
| 183 db_thread_user_data_.reset(); | 196 db_thread_user_data_.reset(); |
| 184 } | 197 } |
| 185 | 198 |
| 186 AutofillWebDataService::~AutofillWebDataService() { | 199 AutofillWebDataService::~AutofillWebDataService() { |
| 187 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?"; | 200 DCHECK(!db_thread_user_data_.get()) << "Forgot to call ShutdownOnUIThread?"; |
| 188 } | |
| 189 | |
| 190 //////////////////////////////////////////////////////////////////////////////// | |
| 191 // | |
| 192 // Autofill implementation. | |
| 193 // | |
| 194 //////////////////////////////////////////////////////////////////////////////// | |
| 195 | |
| 196 WebDatabase::State AutofillWebDataService::AddFormElementsImpl( | |
| 197 const std::vector<FormFieldData>& fields, WebDatabase* db) { | |
| 198 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 199 AutofillChangeList changes; | |
| 200 if (!AutofillTable::FromWebDatabase(db)->AddFormFieldValues( | |
| 201 fields, &changes)) { | |
| 202 NOTREACHED(); | |
| 203 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 204 } | |
| 205 | |
| 206 // Post the notifications including the list of affected keys. | |
| 207 // This is sent here so that work resulting from this notification will be | |
| 208 // done on the DB thread, and not the UI thread. | |
| 209 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 210 db_observer_list_, | |
| 211 AutofillEntriesChanged(changes)); | |
| 212 | |
| 213 return WebDatabase::COMMIT_NEEDED; | |
| 214 } | |
| 215 | |
| 216 scoped_ptr<WDTypedResult> | |
| 217 AutofillWebDataService::GetFormValuesForElementNameImpl( | |
| 218 const base::string16& name, const base::string16& prefix, int limit, | |
| 219 WebDatabase* db) { | |
| 220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 221 std::vector<base::string16> values; | |
| 222 AutofillTable::FromWebDatabase(db)->GetFormValuesForElementName( | |
| 223 name, prefix, &values, limit); | |
| 224 return scoped_ptr<WDTypedResult>( | |
| 225 new WDResult<std::vector<base::string16> >(AUTOFILL_VALUE_RESULT, | |
| 226 values)); | |
| 227 } | |
| 228 | |
| 229 WebDatabase::State AutofillWebDataService::RemoveFormElementsAddedBetweenImpl( | |
| 230 const base::Time& delete_begin, const base::Time& delete_end, | |
| 231 WebDatabase* db) { | |
| 232 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 233 AutofillChangeList changes; | |
| 234 | |
| 235 if (AutofillTable::FromWebDatabase(db)->RemoveFormElementsAddedBetween( | |
| 236 delete_begin, delete_end, &changes)) { | |
| 237 if (!changes.empty()) { | |
| 238 // Post the notifications including the list of affected keys. | |
| 239 // This is sent here so that work resulting from this notification | |
| 240 // will be done on the DB thread, and not the UI thread. | |
| 241 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 242 db_observer_list_, | |
| 243 AutofillEntriesChanged(changes)); | |
| 244 } | |
| 245 return WebDatabase::COMMIT_NEEDED; | |
| 246 } | |
| 247 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 248 } | |
| 249 | |
| 250 WebDatabase::State AutofillWebDataService::RemoveExpiredFormElementsImpl( | |
| 251 WebDatabase* db) { | |
| 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 253 AutofillChangeList changes; | |
| 254 | |
| 255 if (AutofillTable::FromWebDatabase(db)->RemoveExpiredFormElements(&changes)) { | |
| 256 if (!changes.empty()) { | |
| 257 // Post the notifications including the list of affected keys. | |
| 258 // This is sent here so that work resulting from this notification | |
| 259 // will be done on the DB thread, and not the UI thread. | |
| 260 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 261 db_observer_list_, | |
| 262 AutofillEntriesChanged(changes)); | |
| 263 } | |
| 264 return WebDatabase::COMMIT_NEEDED; | |
| 265 } | |
| 266 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 267 } | |
| 268 | |
| 269 WebDatabase::State AutofillWebDataService::RemoveFormValueForElementNameImpl( | |
| 270 const base::string16& name, const base::string16& value, WebDatabase* db) { | |
| 271 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 272 | |
| 273 if (AutofillTable::FromWebDatabase(db)->RemoveFormElement(name, value)) { | |
| 274 AutofillChangeList changes; | |
| 275 changes.push_back( | |
| 276 AutofillChange(AutofillChange::REMOVE, AutofillKey(name, value))); | |
| 277 | |
| 278 // Post the notifications including the list of affected keys. | |
| 279 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 280 db_observer_list_, | |
| 281 AutofillEntriesChanged(changes)); | |
| 282 | |
| 283 return WebDatabase::COMMIT_NEEDED; | |
| 284 } | |
| 285 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 286 } | |
| 287 | |
| 288 WebDatabase::State AutofillWebDataService::AddAutofillProfileImpl( | |
| 289 const AutofillProfile& profile, WebDatabase* db) { | |
| 290 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 291 if (!AutofillTable::FromWebDatabase(db)->AddAutofillProfile(profile)) { | |
| 292 NOTREACHED(); | |
| 293 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 294 } | |
| 295 | |
| 296 // Send GUID-based notification. | |
| 297 AutofillProfileChange change( | |
| 298 AutofillProfileChange::ADD, profile.guid(), &profile); | |
| 299 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 300 db_observer_list_, | |
| 301 AutofillProfileChanged(change)); | |
| 302 | |
| 303 return WebDatabase::COMMIT_NEEDED; | |
| 304 } | |
| 305 | |
| 306 WebDatabase::State AutofillWebDataService::UpdateAutofillProfileImpl( | |
| 307 const AutofillProfile& profile, WebDatabase* db) { | |
| 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 309 // Only perform the update if the profile exists. It is currently | |
| 310 // valid to try to update a missing profile. We simply drop the write and | |
| 311 // the caller will detect this on the next refresh. | |
| 312 AutofillProfile* original_profile = NULL; | |
| 313 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(profile.guid(), | |
| 314 &original_profile)) { | |
| 315 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 316 } | |
| 317 scoped_ptr<AutofillProfile> scoped_profile(original_profile); | |
| 318 | |
| 319 if (!AutofillTable::FromWebDatabase(db)->UpdateAutofillProfileMulti( | |
| 320 profile)) { | |
| 321 NOTREACHED(); | |
| 322 return WebDatabase::COMMIT_NEEDED; | |
| 323 } | |
| 324 | |
| 325 // Send GUID-based notification. | |
| 326 AutofillProfileChange change( | |
| 327 AutofillProfileChange::UPDATE, profile.guid(), &profile); | |
| 328 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 329 db_observer_list_, | |
| 330 AutofillProfileChanged(change)); | |
| 331 | |
| 332 return WebDatabase::COMMIT_NEEDED; | |
| 333 } | |
| 334 | |
| 335 WebDatabase::State AutofillWebDataService::RemoveAutofillProfileImpl( | |
| 336 const std::string& guid, WebDatabase* db) { | |
| 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 338 AutofillProfile* profile = NULL; | |
| 339 if (!AutofillTable::FromWebDatabase(db)->GetAutofillProfile(guid, &profile)) { | |
| 340 NOTREACHED(); | |
| 341 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 342 } | |
| 343 scoped_ptr<AutofillProfile> scoped_profile(profile); | |
| 344 | |
| 345 if (!AutofillTable::FromWebDatabase(db)->RemoveAutofillProfile(guid)) { | |
| 346 NOTREACHED(); | |
| 347 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 348 } | |
| 349 | |
| 350 // Send GUID-based notification. | |
| 351 AutofillProfileChange change(AutofillProfileChange::REMOVE, guid, NULL); | |
| 352 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 353 db_observer_list_, | |
| 354 AutofillProfileChanged(change)); | |
| 355 | |
| 356 return WebDatabase::COMMIT_NEEDED; | |
| 357 } | |
| 358 | |
| 359 scoped_ptr<WDTypedResult> AutofillWebDataService::GetAutofillProfilesImpl( | |
| 360 WebDatabase* db) { | |
| 361 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 362 std::vector<AutofillProfile*> profiles; | |
| 363 AutofillTable::FromWebDatabase(db)->GetAutofillProfiles(&profiles); | |
| 364 return scoped_ptr<WDTypedResult>( | |
| 365 new WDDestroyableResult<std::vector<AutofillProfile*> >( | |
| 366 AUTOFILL_PROFILES_RESULT, | |
| 367 profiles, | |
| 368 base::Bind(&AutofillWebDataService::DestroyAutofillProfileResult, | |
| 369 base::Unretained(this)))); | |
| 370 } | |
| 371 | |
| 372 WebDatabase::State AutofillWebDataService::AddCreditCardImpl( | |
| 373 const CreditCard& credit_card, WebDatabase* db) { | |
| 374 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 375 if (!AutofillTable::FromWebDatabase(db)->AddCreditCard(credit_card)) { | |
| 376 NOTREACHED(); | |
| 377 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 378 } | |
| 379 | |
| 380 return WebDatabase::COMMIT_NEEDED; | |
| 381 } | |
| 382 | |
| 383 WebDatabase::State AutofillWebDataService::UpdateCreditCardImpl( | |
| 384 const CreditCard& credit_card, WebDatabase* db) { | |
| 385 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 386 // It is currently valid to try to update a missing profile. We simply drop | |
| 387 // the write and the caller will detect this on the next refresh. | |
| 388 CreditCard* original_credit_card = NULL; | |
| 389 if (!AutofillTable::FromWebDatabase(db)->GetCreditCard(credit_card.guid(), | |
| 390 &original_credit_card)) { | |
| 391 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 392 } | |
| 393 scoped_ptr<CreditCard> scoped_credit_card(original_credit_card); | |
| 394 | |
| 395 if (!AutofillTable::FromWebDatabase(db)->UpdateCreditCard(credit_card)) { | |
| 396 NOTREACHED(); | |
| 397 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 398 } | |
| 399 return WebDatabase::COMMIT_NEEDED; | |
| 400 } | |
| 401 | |
| 402 WebDatabase::State AutofillWebDataService::RemoveCreditCardImpl( | |
| 403 const std::string& guid, WebDatabase* db) { | |
| 404 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 405 if (!AutofillTable::FromWebDatabase(db)->RemoveCreditCard(guid)) { | |
| 406 NOTREACHED(); | |
| 407 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 408 } | |
| 409 return WebDatabase::COMMIT_NEEDED; | |
| 410 } | |
| 411 | |
| 412 scoped_ptr<WDTypedResult> AutofillWebDataService::GetCreditCardsImpl( | |
| 413 WebDatabase* db) { | |
| 414 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 415 std::vector<CreditCard*> credit_cards; | |
| 416 AutofillTable::FromWebDatabase(db)->GetCreditCards(&credit_cards); | |
| 417 return scoped_ptr<WDTypedResult>( | |
| 418 new WDDestroyableResult<std::vector<CreditCard*> >( | |
| 419 AUTOFILL_CREDITCARDS_RESULT, | |
| 420 credit_cards, | |
| 421 base::Bind(&AutofillWebDataService::DestroyAutofillCreditCardResult, | |
| 422 base::Unretained(this)))); | |
| 423 } | |
| 424 | |
| 425 WebDatabase::State | |
| 426 AutofillWebDataService::RemoveAutofillDataModifiedBetweenImpl( | |
| 427 const base::Time& delete_begin, | |
| 428 const base::Time& delete_end, | |
| 429 WebDatabase* db) { | |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB)); | |
| 431 std::vector<std::string> profile_guids; | |
| 432 std::vector<std::string> credit_card_guids; | |
| 433 if (AutofillTable::FromWebDatabase(db)->RemoveAutofillDataModifiedBetween( | |
| 434 delete_begin, | |
| 435 delete_end, | |
| 436 &profile_guids, | |
| 437 &credit_card_guids)) { | |
| 438 for (std::vector<std::string>::iterator iter = profile_guids.begin(); | |
| 439 iter != profile_guids.end(); ++iter) { | |
| 440 AutofillProfileChange change(AutofillProfileChange::REMOVE, *iter, NULL); | |
| 441 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnDBThread, | |
| 442 db_observer_list_, | |
| 443 AutofillProfileChanged(change)); | |
| 444 } | |
| 445 // Note: It is the caller's responsibility to post notifications for any | |
| 446 // changes, e.g. by calling the Refresh() method of PersonalDataManager. | |
| 447 return WebDatabase::COMMIT_NEEDED; | |
| 448 } | |
| 449 return WebDatabase::COMMIT_NOT_NEEDED; | |
| 450 } | |
| 451 | |
| 452 void AutofillWebDataService::DestroyAutofillProfileResult( | |
| 453 const WDTypedResult* result) { | |
| 454 DCHECK(result->GetType() == AUTOFILL_PROFILES_RESULT); | |
| 455 const WDResult<std::vector<AutofillProfile*> >* r = | |
| 456 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result); | |
| 457 std::vector<AutofillProfile*> profiles = r->GetValue(); | |
| 458 STLDeleteElements(&profiles); | |
| 459 } | |
| 460 | |
| 461 void AutofillWebDataService::DestroyAutofillCreditCardResult( | |
| 462 const WDTypedResult* result) { | |
| 463 DCHECK(result->GetType() == AUTOFILL_CREDITCARDS_RESULT); | |
| 464 const WDResult<std::vector<CreditCard*> >* r = | |
| 465 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); | |
| 466 | |
| 467 std::vector<CreditCard*> credit_cards = r->GetValue(); | |
| 468 STLDeleteElements(&credit_cards); | |
| 469 } | 201 } |
| 470 | 202 |
| 471 void AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread() { | 203 void AutofillWebDataService::NotifyAutofillMultipleChangedOnUIThread() { |
| 472 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 473 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnUIThread, | 205 FOR_EACH_OBSERVER(AutofillWebDataServiceObserverOnUIThread, |
| 474 ui_observer_list_, | 206 ui_observer_list_, |
| 475 AutofillMultipleChanged()); | 207 AutofillMultipleChanged()); |
| 476 } | 208 } |
| 477 | 209 |
| 478 } // namespace autofill | 210 } // namespace autofill |
| OLD | NEW |