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

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

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

Powered by Google App Engine
This is Rietveld 408576698