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

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

Powered by Google App Engine
This is Rietveld 408576698