Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: chrome/browser/webdata/autofill_web_data_service_impl.cc

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

Powered by Google App Engine
This is Rietveld 408576698