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

Side by Side Diff: chrome/browser/autofill/personal_data_manager.cc

Issue 10908065: Introduce a couple of abstract bases for WebDataService. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Respond to comments Created 8 years, 3 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/autofill/personal_data_manager.h" 5 #include "chrome/browser/autofill/personal_data_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <iterator> 8 #include <iterator>
9 9
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "chrome/browser/autofill/autofill-inl.h" 13 #include "chrome/browser/autofill/autofill-inl.h"
14 #include "chrome/browser/autofill/autofill_field.h" 14 #include "chrome/browser/autofill/autofill_field.h"
15 #include "chrome/browser/autofill/autofill_metrics.h" 15 #include "chrome/browser/autofill/autofill_metrics.h"
16 #include "chrome/browser/autofill/autofill_regexes.h" 16 #include "chrome/browser/autofill/autofill_regexes.h"
17 #include "chrome/browser/autofill/form_structure.h" 17 #include "chrome/browser/autofill/form_structure.h"
18 #include "chrome/browser/autofill/personal_data_manager_observer.h" 18 #include "chrome/browser/autofill/personal_data_manager_observer.h"
19 #include "chrome/browser/autofill/phone_number.h" 19 #include "chrome/browser/autofill/phone_number.h"
20 #include "chrome/browser/autofill/phone_number_i18n.h" 20 #include "chrome/browser/autofill/phone_number_i18n.h"
21 #include "chrome/browser/autofill/select_control_handler.h" 21 #include "chrome/browser/autofill/select_control_handler.h"
22 #include "chrome/browser/api/prefs/pref_service_base.h" 22 #include "chrome/browser/api/prefs/pref_service_base.h"
23 #include "chrome/browser/profiles/profile.h" 23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/sync/profile_sync_service.h" 24 #include "chrome/browser/sync/profile_sync_service.h"
25 #include "chrome/browser/sync/profile_sync_service_factory.h" 25 #include "chrome/browser/sync/profile_sync_service_factory.h"
26 #include "chrome/browser/webdata/autofill_entry.h"
27 #include "chrome/browser/webdata/web_data_service.h"
28 #include "chrome/browser/webdata/web_data_service_factory.h"
29 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
30 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
31 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
32 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
33 30
34 namespace { 31 namespace {
35 32
36 template<typename T> 33 template<typename T>
37 class FormGroupMatchesByGUIDFunctor { 34 class FormGroupMatchesByGUIDFunctor {
38 public: 35 public:
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 117 }
121 118
122 } // namespace 119 } // namespace
123 120
124 PersonalDataManager::~PersonalDataManager() { 121 PersonalDataManager::~PersonalDataManager() {
125 CancelPendingQuery(&pending_profiles_query_); 122 CancelPendingQuery(&pending_profiles_query_);
126 CancelPendingQuery(&pending_creditcards_query_); 123 CancelPendingQuery(&pending_creditcards_query_);
127 } 124 }
128 125
129 void PersonalDataManager::OnWebDataServiceRequestDone( 126 void PersonalDataManager::OnWebDataServiceRequestDone(
130 WebDataService::Handle h, 127 WebDataServiceBase::Handle h,
131 const WDTypedResult* result) { 128 const WDTypedResult* result) {
132 DCHECK(pending_profiles_query_ || pending_creditcards_query_); 129 DCHECK(pending_profiles_query_ || pending_creditcards_query_);
133 130
134 if (!result) { 131 if (!result) {
135 // Error from the web database. 132 // Error from the web database.
136 if (h == pending_creditcards_query_) 133 if (h == pending_creditcards_query_)
137 pending_creditcards_query_ = 0; 134 pending_creditcards_query_ = 0;
138 else if (h == pending_profiles_query_) 135 else if (h == pending_profiles_query_)
139 pending_profiles_query_ = 0; 136 pending_profiles_query_ = 0;
140 return; 137 return;
(...skipping 24 matching lines...) Expand all
165 OnPersonalDataChanged()); 162 OnPersonalDataChanged());
166 163
167 // As all Autofill data is ready, the Autocomplete data is ready as well. 164 // As all Autofill data is ready, the Autocomplete data is ready as well.
168 // If sync is not set, cull older entries of the autocomplete. Otherwise, 165 // If sync is not set, cull older entries of the autocomplete. Otherwise,
169 // the entries will be culled when sync is connected. 166 // the entries will be culled when sync is connected.
170 ProfileSyncService* sync_service = 167 ProfileSyncService* sync_service =
171 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 168 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
172 if (sync_service && (!sync_service->HasSyncSetupCompleted() || 169 if (sync_service && (!sync_service->HasSyncSetupCompleted() ||
173 !PrefServiceBase::ForProfile(profile_)->GetBoolean( 170 !PrefServiceBase::ForProfile(profile_)->GetBoolean(
174 prefs::kSyncAutofill))) { 171 prefs::kSyncAutofill))) {
175 scoped_refptr<WebDataService> web_data_service = 172 scoped_ptr<AutofillWebDataService> service(
176 WebDataServiceFactory::GetForProfile(profile_, 173 AutofillWebDataService::ForContext(profile_));
177 Profile::EXPLICIT_ACCESS); 174 if (service.get())
178 if (web_data_service) 175 service->RemoveExpiredFormElements();
179 web_data_service->RemoveExpiredFormElements();
180 } 176 }
181 } 177 }
182 } 178 }
183 179
184 void PersonalDataManager::SetObserver(PersonalDataManagerObserver* observer) { 180 void PersonalDataManager::SetObserver(PersonalDataManagerObserver* observer) {
185 // TODO(dhollowa): RemoveObserver is for compatibility with old code, it 181 // TODO(dhollowa): RemoveObserver is for compatibility with old code, it
186 // should be nuked. 182 // should be nuked.
187 observers_.RemoveObserver(observer); 183 observers_.RemoveObserver(observer);
188 observers_.AddObserver(observer); 184 observers_.AddObserver(observer);
189 } 185 }
190 186
191 void PersonalDataManager::RemoveObserver( 187 void PersonalDataManager::RemoveObserver(
192 PersonalDataManagerObserver* observer) { 188 PersonalDataManagerObserver* observer) {
193 observers_.RemoveObserver(observer); 189 observers_.RemoveObserver(observer);
194 } 190 }
195 191
196 // The |PersonalDataManager| is set up as a listener of the sync service in 192 // The |PersonalDataManager| is set up as a listener of the sync service in
197 // |EmptyMigrationTrash| in the case where sync is not yet ready to receive 193 // |EmptyMigrationTrash| in the case where sync is not yet ready to receive
198 // changes. This method, |OnStateChange| acts as a deferred call to 194 // changes. This method, |OnStateChange| acts as a deferred call to
199 // |EmptyMigrationTrash| once the sync service becomes available. 195 // |EmptyMigrationTrash| once the sync service becomes available.
200 void PersonalDataManager::OnStateChanged() { 196 void PersonalDataManager::OnStateChanged() {
201 if (!profile_ || profile_->IsOffTheRecord()) 197 if (!profile_ || profile_->IsOffTheRecord())
202 return; 198 return;
203 199
204 scoped_refptr<WebDataService> web_data_service = 200 scoped_ptr<AutofillWebDataService> autofill_data(
205 WebDataServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 201 AutofillWebDataService::ForContext(profile_));
206 if (!web_data_service.get()) { 202 if (!autofill_data.get()) {
207 NOTREACHED(); 203 NOTREACHED();
208 return; 204 return;
209 } 205 }
210 206
211 ProfileSyncService* sync_service = 207 ProfileSyncService* sync_service =
212 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 208 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
213 if (!sync_service) 209 if (!sync_service)
214 return; 210 return;
215 211
216 if (sync_service->ShouldPushChanges()) { 212 if (sync_service->ShouldPushChanges()) {
217 web_data_service->EmptyMigrationTrash(true); 213 autofill_data->EmptyMigrationTrash(true);
218 sync_service->RemoveObserver(this); 214 sync_service->RemoveObserver(this);
219 } 215 }
220 } 216 }
221 217
222 void PersonalDataManager::Shutdown() { 218 void PersonalDataManager::Shutdown() {
223 CancelPendingQuery(&pending_profiles_query_); 219 CancelPendingQuery(&pending_profiles_query_);
224 CancelPendingQuery(&pending_creditcards_query_); 220 CancelPendingQuery(&pending_creditcards_query_);
225 notification_registrar_.RemoveAll(); 221 notification_registrar_.RemoveAll();
226 } 222 }
227 223
228 void PersonalDataManager::Observe(int type, 224 void PersonalDataManager::Observe(int type,
229 const content::NotificationSource& source, 225 const content::NotificationSource& source,
230 const content::NotificationDetails& details) { 226 const content::NotificationDetails& details) {
231 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED); 227 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED);
232 scoped_refptr<WebDataService> web_data_service =
233 content::Source<WebDataService>(source).ptr();
234 228
235 DCHECK(web_data_service.get() && 229 if (DCHECK_IS_ON()) {
236 web_data_service.get() == WebDataServiceFactory::GetForProfile( 230 scoped_ptr<AutofillWebDataService> autofill_data(
237 profile_, Profile::EXPLICIT_ACCESS).get()); 231 AutofillWebDataService::ForContext(profile_));
232
233 DCHECK(autofill_data.get() &&
234 autofill_data->GetNotificationSource() == source);
235 }
236
238 Refresh(); 237 Refresh();
239 } 238 }
240 239
241 bool PersonalDataManager::ImportFormData( 240 bool PersonalDataManager::ImportFormData(
242 const FormStructure& form, 241 const FormStructure& form,
243 const CreditCard** imported_credit_card) { 242 const CreditCard** imported_credit_card) {
244 scoped_ptr<AutofillProfile> imported_profile(new AutofillProfile); 243 scoped_ptr<AutofillProfile> imported_profile(new AutofillProfile);
245 scoped_ptr<CreditCard> local_imported_credit_card(new CreditCard); 244 scoped_ptr<CreditCard> local_imported_credit_card(new CreditCard);
246 245
247 // Parse the form and construct a profile based on the information that is 246 // Parse the form and construct a profile based on the information that is
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
360 if (profile_->IsOffTheRecord()) 359 if (profile_->IsOffTheRecord())
361 return; 360 return;
362 361
363 if (profile.IsEmpty()) 362 if (profile.IsEmpty())
364 return; 363 return;
365 364
366 // Don't add an existing profile. 365 // Don't add an existing profile.
367 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 366 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
368 return; 367 return;
369 368
370 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 369 scoped_ptr<AutofillWebDataService> autofill_data(
371 profile_, Profile::EXPLICIT_ACCESS); 370 AutofillWebDataService::ForContext(profile_));
372 if (!wds.get()) 371 if (!autofill_data.get())
373 return; 372 return;
374 373
375 // Don't add a duplicate. 374 // Don't add a duplicate.
376 if (FindByContents(web_profiles_, profile)) 375 if (FindByContents(web_profiles_, profile))
377 return; 376 return;
378 377
379 // Add the new profile to the web database. 378 // Add the new profile to the web database.
380 wds->AddAutofillProfile(profile); 379 autofill_data->AddAutofillProfile(profile);
381 380
382 // Refresh our local cache and send notifications to observers. 381 // Refresh our local cache and send notifications to observers.
383 Refresh(); 382 Refresh();
384 } 383 }
385 384
386 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) { 385 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) {
387 if (profile_->IsOffTheRecord()) 386 if (profile_->IsOffTheRecord())
388 return; 387 return;
389 388
390 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 389 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
391 return; 390 return;
392 391
393 if (profile.IsEmpty()) { 392 if (profile.IsEmpty()) {
394 RemoveProfile(profile.guid()); 393 RemoveProfile(profile.guid());
395 return; 394 return;
396 } 395 }
397 396
398 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 397 scoped_ptr<AutofillWebDataService> autofill_data(
399 profile_, Profile::EXPLICIT_ACCESS); 398 AutofillWebDataService::ForContext(profile_));
400 if (!wds.get()) 399 if (!autofill_data.get())
401 return; 400 return;
402 401
403 // Make the update. 402 // Make the update.
404 wds->UpdateAutofillProfile(profile); 403 autofill_data->UpdateAutofillProfile(profile);
405 404
406 // Refresh our local cache and send notifications to observers. 405 // Refresh our local cache and send notifications to observers.
407 Refresh(); 406 Refresh();
408 } 407 }
409 408
410 void PersonalDataManager::RemoveProfile(const std::string& guid) { 409 void PersonalDataManager::RemoveProfile(const std::string& guid) {
411 if (profile_->IsOffTheRecord()) 410 if (profile_->IsOffTheRecord())
412 return; 411 return;
413 412
414 if (!FindByGUID<AutofillProfile>(web_profiles_, guid)) 413 if (!FindByGUID<AutofillProfile>(web_profiles_, guid))
415 return; 414 return;
416 415
417 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 416 scoped_ptr<AutofillWebDataService> autofill_data(
418 profile_, Profile::EXPLICIT_ACCESS); 417 AutofillWebDataService::ForContext(profile_));
419 if (!wds.get()) 418 if (!autofill_data.get())
420 return; 419 return;
421 420
422 // Remove the profile. 421 // Remove the profile.
423 wds->RemoveAutofillProfile(guid); 422 autofill_data->RemoveAutofillProfile(guid);
424 423
425 // Refresh our local cache and send notifications to observers. 424 // Refresh our local cache and send notifications to observers.
426 Refresh(); 425 Refresh();
427 } 426 }
428 427
429 AutofillProfile* PersonalDataManager::GetProfileByGUID( 428 AutofillProfile* PersonalDataManager::GetProfileByGUID(
430 const std::string& guid) { 429 const std::string& guid) {
431 for (std::vector<AutofillProfile*>::iterator iter = web_profiles_.begin(); 430 for (std::vector<AutofillProfile*>::iterator iter = web_profiles_.begin();
432 iter != web_profiles_.end(); ++iter) { 431 iter != web_profiles_.end(); ++iter) {
433 if ((*iter)->guid() == guid) 432 if ((*iter)->guid() == guid)
434 return *iter; 433 return *iter;
435 } 434 }
436 return NULL; 435 return NULL;
437 } 436 }
438 437
439 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) { 438 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) {
440 if (profile_->IsOffTheRecord()) 439 if (profile_->IsOffTheRecord())
441 return; 440 return;
442 441
443 if (credit_card.IsEmpty()) 442 if (credit_card.IsEmpty())
444 return; 443 return;
445 444
446 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 445 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
447 return; 446 return;
448 447
449 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 448 scoped_ptr<AutofillWebDataService> autofill_data(
450 profile_, Profile::EXPLICIT_ACCESS); 449 AutofillWebDataService::ForContext(profile_));
451 if (!wds.get()) 450 if (!autofill_data.get())
452 return; 451 return;
453 452
454 // Don't add a duplicate. 453 // Don't add a duplicate.
455 if (FindByContents(credit_cards_, credit_card)) 454 if (FindByContents(credit_cards_, credit_card))
456 return; 455 return;
457 456
458 // Add the new credit card to the web database. 457 // Add the new credit card to the web database.
459 wds->AddCreditCard(credit_card); 458 autofill_data->AddCreditCard(credit_card);
460 459
461 // Refresh our local cache and send notifications to observers. 460 // Refresh our local cache and send notifications to observers.
462 Refresh(); 461 Refresh();
463 } 462 }
464 463
465 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) { 464 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) {
466 if (profile_->IsOffTheRecord()) 465 if (profile_->IsOffTheRecord())
467 return; 466 return;
468 467
469 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 468 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
470 return; 469 return;
471 470
472 if (credit_card.IsEmpty()) { 471 if (credit_card.IsEmpty()) {
473 RemoveCreditCard(credit_card.guid()); 472 RemoveCreditCard(credit_card.guid());
474 return; 473 return;
475 } 474 }
476 475
477 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 476 scoped_ptr<AutofillWebDataService> autofill_data(
478 profile_, Profile::EXPLICIT_ACCESS); 477 AutofillWebDataService::ForContext(profile_));
479 if (!wds.get()) 478 if (!autofill_data.get())
480 return; 479 return;
481 480
482 // Make the update. 481 // Make the update.
483 wds->UpdateCreditCard(credit_card); 482 autofill_data->UpdateCreditCard(credit_card);
484 483
485 // Refresh our local cache and send notifications to observers. 484 // Refresh our local cache and send notifications to observers.
486 Refresh(); 485 Refresh();
487 } 486 }
488 487
489 void PersonalDataManager::RemoveCreditCard(const std::string& guid) { 488 void PersonalDataManager::RemoveCreditCard(const std::string& guid) {
490 if (profile_->IsOffTheRecord()) 489 if (profile_->IsOffTheRecord())
491 return; 490 return;
492 491
493 if (!FindByGUID<CreditCard>(credit_cards_, guid)) 492 if (!FindByGUID<CreditCard>(credit_cards_, guid))
494 return; 493 return;
495 494
496 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 495 scoped_ptr<AutofillWebDataService> autofill_data(
497 profile_, Profile::EXPLICIT_ACCESS); 496 AutofillWebDataService::ForContext(profile_));
498 if (!wds.get()) 497 if (!autofill_data.get())
499 return; 498 return;
500 499
501 // Remove the credit card. 500 // Remove the credit card.
502 wds->RemoveCreditCard(guid); 501 autofill_data->RemoveCreditCard(guid);
503 502
504 // Refresh our local cache and send notifications to observers. 503 // Refresh our local cache and send notifications to observers.
505 Refresh(); 504 Refresh();
506 } 505 }
507 506
508 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) { 507 CreditCard* PersonalDataManager::GetCreditCardByGUID(const std::string& guid) {
509 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); 508 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin();
510 iter != credit_cards_.end(); ++iter) { 509 iter != credit_cards_.end(); ++iter) {
511 if ((*iter)->guid() == guid) 510 if ((*iter)->guid() == guid)
512 return *iter; 511 return *iter;
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
576 pending_creditcards_query_(0), 575 pending_creditcards_query_(0),
577 metric_logger_(new AutofillMetrics), 576 metric_logger_(new AutofillMetrics),
578 has_logged_profile_count_(false) { 577 has_logged_profile_count_(false) {
579 } 578 }
580 579
581 void PersonalDataManager::Init(Profile* profile) { 580 void PersonalDataManager::Init(Profile* profile) {
582 profile_ = profile; 581 profile_ = profile;
583 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); 582 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled());
584 583
585 // WebDataService may not be available in tests. 584 // WebDataService may not be available in tests.
586 scoped_refptr<WebDataService> web_data_service = 585 scoped_ptr<AutofillWebDataService> autofill_data(
587 WebDataServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 586 AutofillWebDataService::ForContext(profile_));
588 if (!web_data_service.get()) 587 if (!autofill_data.get())
589 return; 588 return;
590 589
591 LoadProfiles(); 590 LoadProfiles();
592 LoadCreditCards(); 591 LoadCreditCards();
593 592
594 notification_registrar_.Add( 593 notification_registrar_.Add(
595 this, 594 this,
596 chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED, 595 chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED,
597 content::Source<WebDataService>(web_data_service)); 596 autofill_data->GetNotificationSource());
598 } 597 }
599 598
600 bool PersonalDataManager::IsAutofillEnabled() const { 599 bool PersonalDataManager::IsAutofillEnabled() const {
601 return PrefServiceBase::ForProfile(profile_)->GetBoolean( 600 return PrefServiceBase::ForProfile(profile_)->GetBoolean(
602 prefs::kAutofillEnabled); 601 prefs::kAutofillEnabled);
603 } 602 }
604 603
605 // static 604 // static
606 bool PersonalDataManager::IsValidLearnableProfile( 605 bool PersonalDataManager::IsValidLearnableProfile(
607 const AutofillProfile& profile) { 606 const AutofillProfile& profile) {
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 671
673 // Ensure that profile labels are up to date. Currently, sync relies on 672 // Ensure that profile labels are up to date. Currently, sync relies on
674 // labels to identify a profile. 673 // labels to identify a profile.
675 // TODO(dhollowa): We need to deprecate labels and update the way sync 674 // TODO(dhollowa): We need to deprecate labels and update the way sync
676 // identifies profiles. 675 // identifies profiles.
677 std::vector<AutofillProfile*> profile_pointers(profiles->size()); 676 std::vector<AutofillProfile*> profile_pointers(profiles->size());
678 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(), 677 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(),
679 address_of<AutofillProfile>); 678 address_of<AutofillProfile>);
680 AutofillProfile::AdjustInferredLabels(&profile_pointers); 679 AutofillProfile::AdjustInferredLabels(&profile_pointers);
681 680
682 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 681 scoped_ptr<AutofillWebDataService> autofill_data(
683 profile_, Profile::EXPLICIT_ACCESS); 682 AutofillWebDataService::ForContext(profile_));
684 if (!wds.get()) 683 if (!autofill_data.get())
685 return; 684 return;
686 685
687 // Any profiles that are not in the new profile list should be removed from 686 // Any profiles that are not in the new profile list should be removed from
688 // the web database. 687 // the web database.
689 for (std::vector<AutofillProfile*>::const_iterator iter = 688 for (std::vector<AutofillProfile*>::const_iterator iter =
690 web_profiles_.begin(); 689 web_profiles_.begin();
691 iter != web_profiles_.end(); ++iter) { 690 iter != web_profiles_.end(); ++iter) {
692 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid())) 691 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid()))
693 wds->RemoveAutofillProfile((*iter)->guid()); 692 autofill_data->RemoveAutofillProfile((*iter)->guid());
694 } 693 }
695 694
696 // Update the web database with the existing profiles. 695 // Update the web database with the existing profiles.
697 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); 696 for (std::vector<AutofillProfile>::iterator iter = profiles->begin();
698 iter != profiles->end(); ++iter) { 697 iter != profiles->end(); ++iter) {
699 if (FindByGUID<AutofillProfile>(web_profiles_, iter->guid())) 698 if (FindByGUID<AutofillProfile>(web_profiles_, iter->guid()))
700 wds->UpdateAutofillProfile(*iter); 699 autofill_data->UpdateAutofillProfile(*iter);
701 } 700 }
702 701
703 // Add the new profiles to the web database. Don't add a duplicate. 702 // Add the new profiles to the web database. Don't add a duplicate.
704 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); 703 for (std::vector<AutofillProfile>::iterator iter = profiles->begin();
705 iter != profiles->end(); ++iter) { 704 iter != profiles->end(); ++iter) {
706 if (!FindByGUID<AutofillProfile>(web_profiles_, iter->guid()) && 705 if (!FindByGUID<AutofillProfile>(web_profiles_, iter->guid()) &&
707 !FindByContents(web_profiles_, *iter)) 706 !FindByContents(web_profiles_, *iter))
708 wds->AddAutofillProfile(*iter); 707 autofill_data->AddAutofillProfile(*iter);
709 } 708 }
710 709
711 // Copy in the new profiles. 710 // Copy in the new profiles.
712 web_profiles_.clear(); 711 web_profiles_.clear();
713 for (std::vector<AutofillProfile>::iterator iter = profiles->begin(); 712 for (std::vector<AutofillProfile>::iterator iter = profiles->begin();
714 iter != profiles->end(); ++iter) { 713 iter != profiles->end(); ++iter) {
715 web_profiles_.push_back(new AutofillProfile(*iter)); 714 web_profiles_.push_back(new AutofillProfile(*iter));
716 } 715 }
717 716
718 // Refresh our local cache and send notifications to observers. 717 // Refresh our local cache and send notifications to observers.
719 Refresh(); 718 Refresh();
720 } 719 }
721 720
722 void PersonalDataManager::SetCreditCards( 721 void PersonalDataManager::SetCreditCards(
723 std::vector<CreditCard>* credit_cards) { 722 std::vector<CreditCard>* credit_cards) {
724 if (profile_->IsOffTheRecord()) 723 if (profile_->IsOffTheRecord())
725 return; 724 return;
726 725
727 // Remove empty credit cards from input. 726 // Remove empty credit cards from input.
728 credit_cards->erase( 727 credit_cards->erase(
729 std::remove_if( 728 std::remove_if(
730 credit_cards->begin(), credit_cards->end(), 729 credit_cards->begin(), credit_cards->end(),
731 std::mem_fun_ref(&CreditCard::IsEmpty)), 730 std::mem_fun_ref(&CreditCard::IsEmpty)),
732 credit_cards->end()); 731 credit_cards->end());
733 732
734 scoped_refptr<WebDataService> wds = WebDataServiceFactory::GetForProfile( 733 scoped_ptr<AutofillWebDataService> autofill_data(
735 profile_, Profile::EXPLICIT_ACCESS); 734 AutofillWebDataService::ForContext(profile_));
736 if (!wds.get()) 735 if (!autofill_data.get())
737 return; 736 return;
738 737
739 // Any credit cards that are not in the new credit card list should be 738 // Any credit cards that are not in the new credit card list should be
740 // removed. 739 // removed.
741 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); 740 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin();
742 iter != credit_cards_.end(); ++iter) { 741 iter != credit_cards_.end(); ++iter) {
743 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) 742 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid()))
744 wds->RemoveCreditCard((*iter)->guid()); 743 autofill_data->RemoveCreditCard((*iter)->guid());
745 } 744 }
746 745
747 // Update the web database with the existing credit cards. 746 // Update the web database with the existing credit cards.
748 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); 747 for (std::vector<CreditCard>::iterator iter = credit_cards->begin();
749 iter != credit_cards->end(); ++iter) { 748 iter != credit_cards->end(); ++iter) {
750 if (FindByGUID<CreditCard>(credit_cards_, iter->guid())) 749 if (FindByGUID<CreditCard>(credit_cards_, iter->guid()))
751 wds->UpdateCreditCard(*iter); 750 autofill_data->UpdateCreditCard(*iter);
752 } 751 }
753 752
754 // Add the new credit cards to the web database. Don't add a duplicate. 753 // Add the new credit cards to the web database. Don't add a duplicate.
755 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); 754 for (std::vector<CreditCard>::iterator iter = credit_cards->begin();
756 iter != credit_cards->end(); ++iter) { 755 iter != credit_cards->end(); ++iter) {
757 if (!FindByGUID<CreditCard>(credit_cards_, iter->guid()) && 756 if (!FindByGUID<CreditCard>(credit_cards_, iter->guid()) &&
758 !FindByContents(credit_cards_, *iter)) 757 !FindByContents(credit_cards_, *iter))
759 wds->AddCreditCard(*iter); 758 autofill_data->AddCreditCard(*iter);
760 } 759 }
761 760
762 // Copy in the new credit cards. 761 // Copy in the new credit cards.
763 credit_cards_.clear(); 762 credit_cards_.clear();
764 for (std::vector<CreditCard>::iterator iter = credit_cards->begin(); 763 for (std::vector<CreditCard>::iterator iter = credit_cards->begin();
765 iter != credit_cards->end(); ++iter) { 764 iter != credit_cards->end(); ++iter) {
766 credit_cards_.push_back(new CreditCard(*iter)); 765 credit_cards_.push_back(new CreditCard(*iter));
767 } 766 }
768 767
769 // Refresh our local cache and send notifications to observers. 768 // Refresh our local cache and send notifications to observers.
770 Refresh(); 769 Refresh();
771 } 770 }
772 771
773 void PersonalDataManager::LoadProfiles() { 772 void PersonalDataManager::LoadProfiles() {
774 scoped_refptr<WebDataService> web_data_service = 773 scoped_ptr<AutofillWebDataService> autofill_data(
775 WebDataServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 774 AutofillWebDataService::ForContext(profile_));
776 if (!web_data_service.get()) { 775 if (!autofill_data.get()) {
777 NOTREACHED(); 776 NOTREACHED();
778 return; 777 return;
779 } 778 }
780 779
781 CancelPendingQuery(&pending_profiles_query_); 780 CancelPendingQuery(&pending_profiles_query_);
782 781
783 pending_profiles_query_ = web_data_service->GetAutofillProfiles(this); 782 pending_profiles_query_ = autofill_data->GetAutofillProfiles(this);
784 } 783 }
785 784
786 // Win and Linux implementations do nothing. Mac implementation fills in the 785 // Win and Linux implementations do nothing. Mac implementation fills in the
787 // contents of |auxiliary_profiles_|. 786 // contents of |auxiliary_profiles_|.
788 #if !defined(OS_MACOSX) 787 #if !defined(OS_MACOSX)
789 void PersonalDataManager::LoadAuxiliaryProfiles() const { 788 void PersonalDataManager::LoadAuxiliaryProfiles() const {
790 } 789 }
791 #endif 790 #endif
792 791
793 void PersonalDataManager::LoadCreditCards() { 792 void PersonalDataManager::LoadCreditCards() {
794 scoped_refptr<WebDataService> web_data_service = 793 scoped_ptr<AutofillWebDataService> autofill_data(
795 WebDataServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 794 AutofillWebDataService::ForContext(profile_));
796 if (!web_data_service.get()) { 795 if (!autofill_data.get()) {
797 NOTREACHED(); 796 NOTREACHED();
798 return; 797 return;
799 } 798 }
800 799
801 CancelPendingQuery(&pending_creditcards_query_); 800 CancelPendingQuery(&pending_creditcards_query_);
802 801
803 pending_creditcards_query_ = web_data_service->GetCreditCards(this); 802 pending_creditcards_query_ = autofill_data->GetCreditCards(this);
804 } 803 }
805 804
806 void PersonalDataManager::ReceiveLoadedProfiles(WebDataService::Handle h, 805 void PersonalDataManager::ReceiveLoadedProfiles(WebDataServiceBase::Handle h,
807 const WDTypedResult* result) { 806 const WDTypedResult* result) {
808 DCHECK_EQ(pending_profiles_query_, h); 807 DCHECK_EQ(pending_profiles_query_, h);
809 808
810 pending_profiles_query_ = 0; 809 pending_profiles_query_ = 0;
811 web_profiles_.clear(); 810 web_profiles_.clear();
812 811
813 const WDResult<std::vector<AutofillProfile*> >* r = 812 const WDResult<std::vector<AutofillProfile*> >* r =
814 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result); 813 static_cast<const WDResult<std::vector<AutofillProfile*> >*>(result);
815 814
816 std::vector<AutofillProfile*> profiles = r->GetValue(); 815 std::vector<AutofillProfile*> profiles = r->GetValue();
817 for (std::vector<AutofillProfile*>::iterator iter = profiles.begin(); 816 for (std::vector<AutofillProfile*>::iterator iter = profiles.begin();
818 iter != profiles.end(); ++iter) { 817 iter != profiles.end(); ++iter) {
819 web_profiles_.push_back(*iter); 818 web_profiles_.push_back(*iter);
820 } 819 }
821 820
822 LogProfileCount(); 821 LogProfileCount();
823 EmptyMigrationTrash(); 822 EmptyMigrationTrash();
824 } 823 }
825 824
826 void PersonalDataManager::ReceiveLoadedCreditCards( 825 void PersonalDataManager::ReceiveLoadedCreditCards(
827 WebDataService::Handle h, const WDTypedResult* result) { 826 WebDataServiceBase::Handle h, const WDTypedResult* result) {
828 DCHECK_EQ(pending_creditcards_query_, h); 827 DCHECK_EQ(pending_creditcards_query_, h);
829 828
830 pending_creditcards_query_ = 0; 829 pending_creditcards_query_ = 0;
831 credit_cards_.clear(); 830 credit_cards_.clear();
832 831
833 const WDResult<std::vector<CreditCard*> >* r = 832 const WDResult<std::vector<CreditCard*> >* r =
834 static_cast<const WDResult<std::vector<CreditCard*> >*>(result); 833 static_cast<const WDResult<std::vector<CreditCard*> >*>(result);
835 834
836 std::vector<CreditCard*> credit_cards = r->GetValue(); 835 std::vector<CreditCard*> credit_cards = r->GetValue();
837 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin(); 836 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin();
838 iter != credit_cards.end(); ++iter) { 837 iter != credit_cards.end(); ++iter) {
839 credit_cards_.push_back(*iter); 838 credit_cards_.push_back(*iter);
840 } 839 }
841 } 840 }
842 841
843 void PersonalDataManager::CancelPendingQuery(WebDataService::Handle* handle) { 842 void PersonalDataManager::CancelPendingQuery(
843 WebDataServiceBase::Handle* handle) {
844 if (*handle) { 844 if (*handle) {
845 scoped_refptr<WebDataService> web_data_service = 845 scoped_ptr<AutofillWebDataService> autofill_data(
846 WebDataServiceFactory::GetForProfile(profile_, 846 AutofillWebDataService::ForContext(profile_));
847 Profile::EXPLICIT_ACCESS); 847 if (!autofill_data.get()) {
848 if (!web_data_service.get()) {
849 NOTREACHED(); 848 NOTREACHED();
850 return; 849 return;
851 } 850 }
852 web_data_service->CancelRequest(*handle); 851 autofill_data->CancelRequest(*handle);
853 } 852 }
854 *handle = 0; 853 *handle = 0;
855 } 854 }
856 855
857 void PersonalDataManager::SaveImportedProfile( 856 void PersonalDataManager::SaveImportedProfile(
858 const AutofillProfile& imported_profile) { 857 const AutofillProfile& imported_profile) {
859 if (profile_->IsOffTheRecord()) { 858 if (profile_->IsOffTheRecord()) {
860 // The |IsOffTheRecord| check should happen earlier in the import process, 859 // The |IsOffTheRecord| check should happen earlier in the import process,
861 // upon form submission. 860 // upon form submission.
862 NOTREACHED(); 861 NOTREACHED();
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
906 if (!merged) 905 if (!merged)
907 creditcards.push_back(imported_credit_card); 906 creditcards.push_back(imported_credit_card);
908 907
909 SetCreditCards(&creditcards); 908 SetCreditCards(&creditcards);
910 } 909 }
911 910
912 void PersonalDataManager::EmptyMigrationTrash() { 911 void PersonalDataManager::EmptyMigrationTrash() {
913 if (!profile_ || profile_->IsOffTheRecord()) 912 if (!profile_ || profile_->IsOffTheRecord())
914 return; 913 return;
915 914
916 scoped_refptr<WebDataService> web_data_service = 915 scoped_ptr<AutofillWebDataService> autofill_data(
917 WebDataServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); 916 AutofillWebDataService::ForContext(profile_));
918 if (!web_data_service.get()) { 917 if (!autofill_data.get()) {
919 NOTREACHED(); 918 NOTREACHED();
920 return; 919 return;
921 } 920 }
922 921
923 ProfileSyncService* sync_service = 922 ProfileSyncService* sync_service =
924 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 923 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_);
925 if (!sync_service) 924 if (!sync_service)
926 return; 925 return;
927 926
928 if (!sync_service->HasSyncSetupCompleted()) { 927 if (!sync_service->HasSyncSetupCompleted()) {
929 web_data_service->EmptyMigrationTrash(false); 928 autofill_data->EmptyMigrationTrash(false);
930 } else if (sync_service->ShouldPushChanges()) { 929 } else if (sync_service->ShouldPushChanges()) {
931 web_data_service->EmptyMigrationTrash(true); 930 autofill_data->EmptyMigrationTrash(true);
932 } else { 931 } else {
933 // Install ourself as a listener so we can empty the trash once the 932 // Install ourself as a listener so we can empty the trash once the
934 // sync service becomes available. 933 // sync service becomes available.
935 if (!sync_service->HasObserver(this)) 934 if (!sync_service->HasObserver(this))
936 sync_service->AddObserver(this); 935 sync_service->AddObserver(this);
937 } 936 }
938 } 937 }
939 938
940 void PersonalDataManager::LogProfileCount() const { 939 void PersonalDataManager::LogProfileCount() const {
941 if (!has_logged_profile_count_) { 940 if (!has_logged_profile_count_) {
942 metric_logger_->LogStoredProfileCount(web_profiles_.size()); 941 metric_logger_->LogStoredProfileCount(web_profiles_.size());
943 has_logged_profile_count_ = true; 942 has_logged_profile_count_ = true;
944 } 943 }
945 } 944 }
946 945
947 const AutofillMetrics* PersonalDataManager::metric_logger() const { 946 const AutofillMetrics* PersonalDataManager::metric_logger() const {
948 return metric_logger_.get(); 947 return metric_logger_.get();
949 } 948 }
950 949
951 void PersonalDataManager::set_metric_logger( 950 void PersonalDataManager::set_metric_logger(
952 const AutofillMetrics* metric_logger) { 951 const AutofillMetrics* metric_logger) {
953 metric_logger_.reset(metric_logger); 952 metric_logger_.reset(metric_logger);
954 } 953 }
OLDNEW
« no previous file with comments | « chrome/browser/autofill/personal_data_manager.h ('k') | chrome/browser/autofill/personal_data_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698