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

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

Issue 10919066: Use BrowserContext as key in API. Switch Autofill to use BC in place of Profile. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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"
(...skipping 10 matching lines...) Expand all
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/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
27 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
28 #include "content/public/browser/browser_thread.h" 28 #include "content/public/browser/browser_thread.h"
29 #include "content/public/browser/notification_source.h" 29 #include "content/public/browser/notification_source.h"
30 30
31 using content::BrowserContext;
32
31 namespace { 33 namespace {
32 34
33 template<typename T> 35 template<typename T>
34 class FormGroupMatchesByGUIDFunctor { 36 class FormGroupMatchesByGUIDFunctor {
35 public: 37 public:
36 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid) 38 explicit FormGroupMatchesByGUIDFunctor(const std::string& guid)
37 : guid_(guid) { 39 : guid_(guid) {
38 } 40 }
39 41
40 bool operator()(const T& form_group) { 42 bool operator()(const T& form_group) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 std::copy(web_profiles_.begin(), web_profiles_.end(), 160 std::copy(web_profiles_.begin(), web_profiles_.end(),
159 profile_pointers.begin()); 161 profile_pointers.begin());
160 AutofillProfile::AdjustInferredLabels(&profile_pointers); 162 AutofillProfile::AdjustInferredLabels(&profile_pointers);
161 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_, 163 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_,
162 OnPersonalDataChanged()); 164 OnPersonalDataChanged());
163 165
164 // As all Autofill data is ready, the Autocomplete data is ready as well. 166 // As all Autofill data is ready, the Autocomplete data is ready as well.
165 // If sync is not set, cull older entries of the autocomplete. Otherwise, 167 // If sync is not set, cull older entries of the autocomplete. Otherwise,
166 // the entries will be culled when sync is connected. 168 // the entries will be culled when sync is connected.
167 ProfileSyncService* sync_service = 169 ProfileSyncService* sync_service =
168 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 170 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
171 static_cast<Profile*>(browser_context_));
169 if (sync_service && (!sync_service->HasSyncSetupCompleted() || 172 if (sync_service && (!sync_service->HasSyncSetupCompleted() ||
170 !PrefServiceBase::ForProfile(profile_)->GetBoolean( 173 !PrefServiceBase::ForContext(
171 prefs::kSyncAutofill))) { 174 browser_context_)->GetBoolean(
175 prefs::kSyncAutofill))) {
172 scoped_refptr<WebDataServiceBase> service = 176 scoped_refptr<WebDataServiceBase> service =
173 WebDataServiceBase::ForProfile(profile_); 177 WebDataServiceBase::ForContext(browser_context_);
174 if (service) { 178 if (service) {
175 AutofillWebData::ForService(service)->RemoveExpiredFormElements(); 179 AutofillWebData::ForService(service)->RemoveExpiredFormElements();
176 } 180 }
177 } 181 }
178 } 182 }
179 } 183 }
180 184
181 void PersonalDataManager::SetObserver(PersonalDataManagerObserver* observer) { 185 void PersonalDataManager::SetObserver(PersonalDataManagerObserver* observer) {
182 // TODO(dhollowa): RemoveObserver is for compatibility with old code, it 186 // TODO(dhollowa): RemoveObserver is for compatibility with old code, it
183 // should be nuked. 187 // should be nuked.
184 observers_.RemoveObserver(observer); 188 observers_.RemoveObserver(observer);
185 observers_.AddObserver(observer); 189 observers_.AddObserver(observer);
186 } 190 }
187 191
188 void PersonalDataManager::RemoveObserver( 192 void PersonalDataManager::RemoveObserver(
189 PersonalDataManagerObserver* observer) { 193 PersonalDataManagerObserver* observer) {
190 observers_.RemoveObserver(observer); 194 observers_.RemoveObserver(observer);
191 } 195 }
192 196
193 // The |PersonalDataManager| is set up as a listener of the sync service in 197 // The |PersonalDataManager| is set up as a listener of the sync service in
194 // |EmptyMigrationTrash| in the case where sync is not yet ready to receive 198 // |EmptyMigrationTrash| in the case where sync is not yet ready to receive
195 // changes. This method, |OnStateChange| acts as a deferred call to 199 // changes. This method, |OnStateChange| acts as a deferred call to
196 // |EmptyMigrationTrash| once the sync service becomes available. 200 // |EmptyMigrationTrash| once the sync service becomes available.
197 void PersonalDataManager::OnStateChanged() { 201 void PersonalDataManager::OnStateChanged() {
198 if (!profile_ || profile_->IsOffTheRecord()) 202 if (!browser_context_ || browser_context_->IsOffTheRecord())
199 return; 203 return;
200 204
201 scoped_refptr<WebDataServiceBase> web_data_service = 205 scoped_refptr<WebDataServiceBase> web_data_service =
202 WebDataServiceBase::ForProfile(profile_); 206 WebDataServiceBase::ForContext(browser_context_);
203 if (!web_data_service.get()) { 207 if (!web_data_service.get()) {
204 NOTREACHED(); 208 NOTREACHED();
205 return; 209 return;
206 } 210 }
207 211
208 ProfileSyncService* sync_service = 212 ProfileSyncService* sync_service =
209 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 213 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
214 static_cast<Profile*>(browser_context_));
210 if (!sync_service) 215 if (!sync_service)
211 return; 216 return;
212 217
213 if (sync_service->ShouldPushChanges()) { 218 if (sync_service->ShouldPushChanges()) {
214 AutofillWebData::ForService(web_data_service)->EmptyMigrationTrash(true); 219 AutofillWebData::ForService(web_data_service)->EmptyMigrationTrash(true);
215 sync_service->RemoveObserver(this); 220 sync_service->RemoveObserver(this);
216 } 221 }
217 } 222 }
218 223
219 void PersonalDataManager::Shutdown() { 224 void PersonalDataManager::Shutdown() {
220 CancelPendingQuery(&pending_profiles_query_); 225 CancelPendingQuery(&pending_profiles_query_);
221 CancelPendingQuery(&pending_creditcards_query_); 226 CancelPendingQuery(&pending_creditcards_query_);
222 notification_registrar_.RemoveAll(); 227 notification_registrar_.RemoveAll();
223 } 228 }
224 229
225 void PersonalDataManager::Observe(int type, 230 void PersonalDataManager::Observe(int type,
226 const content::NotificationSource& source, 231 const content::NotificationSource& source,
227 const content::NotificationDetails& details) { 232 const content::NotificationDetails& details) {
228 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED); 233 DCHECK_EQ(type, chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED);
229 scoped_refptr<WebDataServiceBase> web_data_service = 234 scoped_refptr<WebDataServiceBase> web_data_service =
230 content::Source<WebDataServiceBase>(source).ptr(); 235 content::Source<WebDataServiceBase>(source).ptr();
231 236
232 DCHECK(web_data_service.get() && 237 DCHECK(web_data_service.get() &&
233 web_data_service.get() == WebDataServiceBase::ForProfile( 238 web_data_service.get() == WebDataServiceBase::ForContext(
234 profile_).get()); 239 browser_context_).get());
235 Refresh(); 240 Refresh();
236 } 241 }
237 242
238 bool PersonalDataManager::ImportFormData( 243 bool PersonalDataManager::ImportFormData(
239 const FormStructure& form, 244 const FormStructure& form,
240 const CreditCard** imported_credit_card) { 245 const CreditCard** imported_credit_card) {
241 scoped_ptr<AutofillProfile> imported_profile(new AutofillProfile); 246 scoped_ptr<AutofillProfile> imported_profile(new AutofillProfile);
242 scoped_ptr<CreditCard> local_imported_credit_card(new CreditCard); 247 scoped_ptr<CreditCard> local_imported_credit_card(new CreditCard);
243 248
244 // Parse the form and construct a profile based on the information that is 249 // Parse the form and construct a profile based on the information that is
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 if (imported_profile.get() || *imported_credit_card || merged_credit_card) { 352 if (imported_profile.get() || *imported_credit_card || merged_credit_card) {
348 return true; 353 return true;
349 } else { 354 } else {
350 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_, 355 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_,
351 OnInsufficientFormData()); 356 OnInsufficientFormData());
352 return false; 357 return false;
353 } 358 }
354 } 359 }
355 360
356 void PersonalDataManager::AddProfile(const AutofillProfile& profile) { 361 void PersonalDataManager::AddProfile(const AutofillProfile& profile) {
357 if (profile_->IsOffTheRecord()) 362 if (browser_context_->IsOffTheRecord())
358 return; 363 return;
359 364
360 if (profile.IsEmpty()) 365 if (profile.IsEmpty())
361 return; 366 return;
362 367
363 // Don't add an existing profile. 368 // Don't add an existing profile.
364 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 369 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
365 return; 370 return;
366 371
367 scoped_refptr<WebDataServiceBase> wds = 372 scoped_refptr<WebDataServiceBase> wds =
368 WebDataServiceBase::ForProfile(profile_); 373 WebDataServiceBase::ForContext(browser_context_);
369 if (!wds.get()) 374 if (!wds.get())
370 return; 375 return;
371 376
372 // Don't add a duplicate. 377 // Don't add a duplicate.
373 if (FindByContents(web_profiles_, profile)) 378 if (FindByContents(web_profiles_, profile))
374 return; 379 return;
375 380
376 // Add the new profile to the web database. 381 // Add the new profile to the web database.
377 AutofillWebData::ForService(wds)->AddAutofillProfile(profile); 382 AutofillWebData::ForService(wds)->AddAutofillProfile(profile);
378 383
379 // Refresh our local cache and send notifications to observers. 384 // Refresh our local cache and send notifications to observers.
380 Refresh(); 385 Refresh();
381 } 386 }
382 387
383 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) { 388 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) {
384 if (profile_->IsOffTheRecord()) 389 if (browser_context_->IsOffTheRecord())
385 return; 390 return;
386 391
387 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 392 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
388 return; 393 return;
389 394
390 if (profile.IsEmpty()) { 395 if (profile.IsEmpty()) {
391 RemoveProfile(profile.guid()); 396 RemoveProfile(profile.guid());
392 return; 397 return;
393 } 398 }
394 399
395 scoped_refptr<WebDataServiceBase> wds = 400 scoped_refptr<WebDataServiceBase> wds =
396 WebDataServiceBase::ForProfile(profile_); 401 WebDataServiceBase::ForContext(browser_context_);
397 if (!wds.get()) 402 if (!wds.get())
398 return; 403 return;
399 404
400 // Make the update. 405 // Make the update.
401 AutofillWebData::ForService(wds)->UpdateAutofillProfile(profile); 406 AutofillWebData::ForService(wds)->UpdateAutofillProfile(profile);
402 407
403 // Refresh our local cache and send notifications to observers. 408 // Refresh our local cache and send notifications to observers.
404 Refresh(); 409 Refresh();
405 } 410 }
406 411
407 void PersonalDataManager::RemoveProfile(const std::string& guid) { 412 void PersonalDataManager::RemoveProfile(const std::string& guid) {
408 if (profile_->IsOffTheRecord()) 413 if (browser_context_->IsOffTheRecord())
409 return; 414 return;
410 415
411 if (!FindByGUID<AutofillProfile>(web_profiles_, guid)) 416 if (!FindByGUID<AutofillProfile>(web_profiles_, guid))
412 return; 417 return;
413 418
414 scoped_refptr<WebDataServiceBase> wds = 419 scoped_refptr<WebDataServiceBase> wds =
415 WebDataServiceBase::ForProfile(profile_); 420 WebDataServiceBase::ForContext(browser_context_);
416 if (!wds.get()) 421 if (!wds.get())
417 return; 422 return;
418 423
419 // Remove the profile. 424 // Remove the profile.
420 AutofillWebData::ForService(wds)->RemoveAutofillProfile(guid); 425 AutofillWebData::ForService(wds)->RemoveAutofillProfile(guid);
421 426
422 // Refresh our local cache and send notifications to observers. 427 // Refresh our local cache and send notifications to observers.
423 Refresh(); 428 Refresh();
424 } 429 }
425 430
426 AutofillProfile* PersonalDataManager::GetProfileByGUID( 431 AutofillProfile* PersonalDataManager::GetProfileByGUID(
427 const std::string& guid) { 432 const std::string& guid) {
428 for (std::vector<AutofillProfile*>::iterator iter = web_profiles_.begin(); 433 for (std::vector<AutofillProfile*>::iterator iter = web_profiles_.begin();
429 iter != web_profiles_.end(); ++iter) { 434 iter != web_profiles_.end(); ++iter) {
430 if ((*iter)->guid() == guid) 435 if ((*iter)->guid() == guid)
431 return *iter; 436 return *iter;
432 } 437 }
433 return NULL; 438 return NULL;
434 } 439 }
435 440
436 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) { 441 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) {
437 if (profile_->IsOffTheRecord()) 442 if (browser_context_->IsOffTheRecord())
438 return; 443 return;
439 444
440 if (credit_card.IsEmpty()) 445 if (credit_card.IsEmpty())
441 return; 446 return;
442 447
443 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 448 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
444 return; 449 return;
445 450
446 scoped_refptr<WebDataServiceBase> wds = 451 scoped_refptr<WebDataServiceBase> wds =
447 WebDataServiceBase::ForProfile(profile_); 452 WebDataServiceBase::ForContext(browser_context_);
448 if (!wds.get()) 453 if (!wds.get())
449 return; 454 return;
450 455
451 // Don't add a duplicate. 456 // Don't add a duplicate.
452 if (FindByContents(credit_cards_, credit_card)) 457 if (FindByContents(credit_cards_, credit_card))
453 return; 458 return;
454 459
455 // Add the new credit card to the web database. 460 // Add the new credit card to the web database.
456 AutofillWebData::ForService(wds)->AddCreditCard(credit_card); 461 AutofillWebData::ForService(wds)->AddCreditCard(credit_card);
457 462
458 // Refresh our local cache and send notifications to observers. 463 // Refresh our local cache and send notifications to observers.
459 Refresh(); 464 Refresh();
460 } 465 }
461 466
462 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) { 467 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) {
463 if (profile_->IsOffTheRecord()) 468 if (browser_context_->IsOffTheRecord())
464 return; 469 return;
465 470
466 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 471 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
467 return; 472 return;
468 473
469 if (credit_card.IsEmpty()) { 474 if (credit_card.IsEmpty()) {
470 RemoveCreditCard(credit_card.guid()); 475 RemoveCreditCard(credit_card.guid());
471 return; 476 return;
472 } 477 }
473 478
474 scoped_refptr<WebDataServiceBase> wds = 479 scoped_refptr<WebDataServiceBase> wds =
475 WebDataServiceBase::ForProfile(profile_); 480 WebDataServiceBase::ForContext(browser_context_);
476 if (!wds.get()) 481 if (!wds.get())
477 return; 482 return;
478 483
479 // Make the update. 484 // Make the update.
480 AutofillWebData::ForService(wds)->UpdateCreditCard(credit_card); 485 AutofillWebData::ForService(wds)->UpdateCreditCard(credit_card);
481 486
482 // Refresh our local cache and send notifications to observers. 487 // Refresh our local cache and send notifications to observers.
483 Refresh(); 488 Refresh();
484 } 489 }
485 490
486 void PersonalDataManager::RemoveCreditCard(const std::string& guid) { 491 void PersonalDataManager::RemoveCreditCard(const std::string& guid) {
487 if (profile_->IsOffTheRecord()) 492 if (browser_context_->IsOffTheRecord())
488 return; 493 return;
489 494
490 if (!FindByGUID<CreditCard>(credit_cards_, guid)) 495 if (!FindByGUID<CreditCard>(credit_cards_, guid))
491 return; 496 return;
492 497
493 scoped_refptr<WebDataServiceBase> wds = 498 scoped_refptr<WebDataServiceBase> wds =
494 WebDataServiceBase::ForProfile(profile_); 499 WebDataServiceBase::ForContext(browser_context_);
495 if (!wds.get()) 500 if (!wds.get())
496 return; 501 return;
497 502
498 // Remove the credit card. 503 // Remove the credit card.
499 AutofillWebData::ForService(wds)->RemoveCreditCard(guid); 504 AutofillWebData::ForService(wds)->RemoveCreditCard(guid);
500 505
501 // Refresh our local cache and send notifications to observers. 506 // Refresh our local cache and send notifications to observers.
502 Refresh(); 507 Refresh();
503 } 508 }
504 509
(...skipping 18 matching lines...) Expand all
523 iter != credit_cards_.end(); ++iter) { 528 iter != credit_cards_.end(); ++iter) {
524 (*iter)->GetNonEmptyTypes(non_empty_types); 529 (*iter)->GetNonEmptyTypes(non_empty_types);
525 } 530 }
526 } 531 }
527 532
528 bool PersonalDataManager::IsDataLoaded() const { 533 bool PersonalDataManager::IsDataLoaded() const {
529 return is_data_loaded_; 534 return is_data_loaded_;
530 } 535 }
531 536
532 const std::vector<AutofillProfile*>& PersonalDataManager::profiles() const { 537 const std::vector<AutofillProfile*>& PersonalDataManager::profiles() const {
533 // |profile_| is NULL in AutofillManagerTest. 538 // |browser_context_| is NULL in AutofillManagerTest.
534 bool auxiliary_profiles_enabled = profile_ ? 539 bool auxiliary_profiles_enabled = browser_context_ ?
535 PrefServiceBase::ForProfile(profile_)->GetBoolean( 540 PrefServiceBase::ForContext(browser_context_)->GetBoolean(
536 prefs::kAutofillAuxiliaryProfilesEnabled) : 541 prefs::kAutofillAuxiliaryProfilesEnabled) :
537 false; 542 false;
538 if (!auxiliary_profiles_enabled) 543 if (!auxiliary_profiles_enabled)
539 return web_profiles(); 544 return web_profiles();
540 545
541 #if !defined(OS_MACOSX) 546 #if !defined(OS_MACOSX)
542 NOTREACHED() << "Auxiliary profiles supported on Mac only"; 547 NOTREACHED() << "Auxiliary profiles supported on Mac only";
543 #endif 548 #endif
544 549
545 profiles_.clear(); 550 profiles_.clear();
(...skipping 14 matching lines...) Expand all
560 const std::vector<CreditCard*>& PersonalDataManager::credit_cards() const { 565 const std::vector<CreditCard*>& PersonalDataManager::credit_cards() const {
561 return credit_cards_.get(); 566 return credit_cards_.get();
562 } 567 }
563 568
564 void PersonalDataManager::Refresh() { 569 void PersonalDataManager::Refresh() {
565 LoadProfiles(); 570 LoadProfiles();
566 LoadCreditCards(); 571 LoadCreditCards();
567 } 572 }
568 573
569 PersonalDataManager::PersonalDataManager() 574 PersonalDataManager::PersonalDataManager()
570 : profile_(NULL), 575 : browser_context_(NULL),
571 is_data_loaded_(false), 576 is_data_loaded_(false),
572 pending_profiles_query_(0), 577 pending_profiles_query_(0),
573 pending_creditcards_query_(0), 578 pending_creditcards_query_(0),
574 metric_logger_(new AutofillMetrics), 579 metric_logger_(new AutofillMetrics),
575 has_logged_profile_count_(false) { 580 has_logged_profile_count_(false) {
576 } 581 }
577 582
578 void PersonalDataManager::Init(Profile* profile) { 583 void PersonalDataManager::Init(BrowserContext* browser_context) {
579 profile_ = profile; 584 browser_context_ = browser_context;
580 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); 585 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled());
581 586
582 // WebDataService may not be available in tests. 587 // WebDataService may not be available in tests.
583 scoped_refptr<WebDataServiceBase> web_data_service = 588 scoped_refptr<WebDataServiceBase> web_data_service =
584 WebDataServiceBase::ForProfile(profile_); 589 WebDataServiceBase::ForContext(browser_context_);
585 if (!web_data_service.get()) 590 if (!web_data_service.get())
586 return; 591 return;
587 592
588 LoadProfiles(); 593 LoadProfiles();
589 LoadCreditCards(); 594 LoadCreditCards();
590 595
591 notification_registrar_.Add( 596 notification_registrar_.Add(
592 this, 597 this,
593 chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED, 598 chrome::NOTIFICATION_AUTOFILL_MULTIPLE_CHANGED,
594 content::Source<WebDataServiceBase>(web_data_service)); 599 content::Source<WebDataServiceBase>(web_data_service));
595 } 600 }
596 601
597 bool PersonalDataManager::IsAutofillEnabled() const { 602 bool PersonalDataManager::IsAutofillEnabled() const {
598 return PrefServiceBase::ForProfile(profile_)->GetBoolean( 603 return PrefServiceBase::ForContext(browser_context_)->GetBoolean(
599 prefs::kAutofillEnabled); 604 prefs::kAutofillEnabled);
600 } 605 }
601 606
602 // static 607 // static
603 bool PersonalDataManager::IsValidLearnableProfile( 608 bool PersonalDataManager::IsValidLearnableProfile(
604 const AutofillProfile& profile) { 609 const AutofillProfile& profile) {
605 if (!IsMinimumAddress(profile)) 610 if (!IsMinimumAddress(profile))
606 return false; 611 return false;
607 612
608 string16 email = profile.GetInfo(EMAIL_ADDRESS); 613 string16 email = profile.GetInfo(EMAIL_ADDRESS);
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
651 } 656 }
652 657
653 // If the new profile was not merged with an existing one, add it to the list. 658 // If the new profile was not merged with an existing one, add it to the list.
654 if (!merged) 659 if (!merged)
655 merged_profiles->push_back(profile); 660 merged_profiles->push_back(profile);
656 661
657 return merged; 662 return merged;
658 } 663 }
659 664
660 void PersonalDataManager::SetProfiles(std::vector<AutofillProfile>* profiles) { 665 void PersonalDataManager::SetProfiles(std::vector<AutofillProfile>* profiles) {
661 if (profile_->IsOffTheRecord()) 666 if (browser_context_->IsOffTheRecord())
662 return; 667 return;
663 668
664 // Remove empty profiles from input. 669 // Remove empty profiles from input.
665 profiles->erase( 670 profiles->erase(
666 std::remove_if(profiles->begin(), profiles->end(), 671 std::remove_if(profiles->begin(), profiles->end(),
667 std::mem_fun_ref(&AutofillProfile::IsEmpty)), 672 std::mem_fun_ref(&AutofillProfile::IsEmpty)),
668 profiles->end()); 673 profiles->end());
669 674
670 // Ensure that profile labels are up to date. Currently, sync relies on 675 // Ensure that profile labels are up to date. Currently, sync relies on
671 // labels to identify a profile. 676 // labels to identify a profile.
672 // TODO(dhollowa): We need to deprecate labels and update the way sync 677 // TODO(dhollowa): We need to deprecate labels and update the way sync
673 // identifies profiles. 678 // identifies profiles.
674 std::vector<AutofillProfile*> profile_pointers(profiles->size()); 679 std::vector<AutofillProfile*> profile_pointers(profiles->size());
675 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(), 680 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(),
676 address_of<AutofillProfile>); 681 address_of<AutofillProfile>);
677 AutofillProfile::AdjustInferredLabels(&profile_pointers); 682 AutofillProfile::AdjustInferredLabels(&profile_pointers);
678 683
679 scoped_refptr<WebDataServiceBase> wds = 684 scoped_refptr<WebDataServiceBase> wds =
680 WebDataServiceBase::ForProfile(profile_); 685 WebDataServiceBase::ForContext(browser_context_);
681 if (!wds.get()) 686 if (!wds.get())
682 return; 687 return;
683 AutofillWebData* autofill_data = AutofillWebData::ForService(wds); 688 AutofillWebData* autofill_data = AutofillWebData::ForService(wds);
684 689
685 // Any profiles that are not in the new profile list should be removed from 690 // Any profiles that are not in the new profile list should be removed from
686 // the web database. 691 // the web database.
687 for (std::vector<AutofillProfile*>::const_iterator iter = 692 for (std::vector<AutofillProfile*>::const_iterator iter =
688 web_profiles_.begin(); 693 web_profiles_.begin();
689 iter != web_profiles_.end(); ++iter) { 694 iter != web_profiles_.end(); ++iter) {
690 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid())) 695 if (!FindByGUID<AutofillProfile>(*profiles, (*iter)->guid()))
(...skipping 21 matching lines...) Expand all
712 iter != profiles->end(); ++iter) { 717 iter != profiles->end(); ++iter) {
713 web_profiles_.push_back(new AutofillProfile(*iter)); 718 web_profiles_.push_back(new AutofillProfile(*iter));
714 } 719 }
715 720
716 // Refresh our local cache and send notifications to observers. 721 // Refresh our local cache and send notifications to observers.
717 Refresh(); 722 Refresh();
718 } 723 }
719 724
720 void PersonalDataManager::SetCreditCards( 725 void PersonalDataManager::SetCreditCards(
721 std::vector<CreditCard>* credit_cards) { 726 std::vector<CreditCard>* credit_cards) {
722 if (profile_->IsOffTheRecord()) 727 if (browser_context_->IsOffTheRecord())
723 return; 728 return;
724 729
725 // Remove empty credit cards from input. 730 // Remove empty credit cards from input.
726 credit_cards->erase( 731 credit_cards->erase(
727 std::remove_if( 732 std::remove_if(
728 credit_cards->begin(), credit_cards->end(), 733 credit_cards->begin(), credit_cards->end(),
729 std::mem_fun_ref(&CreditCard::IsEmpty)), 734 std::mem_fun_ref(&CreditCard::IsEmpty)),
730 credit_cards->end()); 735 credit_cards->end());
731 736
732 scoped_refptr<WebDataServiceBase> wds = 737 scoped_refptr<WebDataServiceBase> wds =
733 WebDataServiceBase::ForProfile(profile_); 738 WebDataServiceBase::ForContext(browser_context_);
734 if (!wds.get()) 739 if (!wds.get())
735 return; 740 return;
736 AutofillWebData* autofill_data = AutofillWebData::ForService(wds); 741 AutofillWebData* autofill_data = AutofillWebData::ForService(wds);
737 742
738 // Any credit cards that are not in the new credit card list should be 743 // Any credit cards that are not in the new credit card list should be
739 // removed. 744 // removed.
740 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); 745 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin();
741 iter != credit_cards_.end(); ++iter) { 746 iter != credit_cards_.end(); ++iter) {
742 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid())) 747 if (!FindByGUID<CreditCard>(*credit_cards, (*iter)->guid()))
743 autofill_data->RemoveCreditCard((*iter)->guid()); 748 autofill_data->RemoveCreditCard((*iter)->guid());
(...skipping 20 matching lines...) Expand all
764 iter != credit_cards->end(); ++iter) { 769 iter != credit_cards->end(); ++iter) {
765 credit_cards_.push_back(new CreditCard(*iter)); 770 credit_cards_.push_back(new CreditCard(*iter));
766 } 771 }
767 772
768 // Refresh our local cache and send notifications to observers. 773 // Refresh our local cache and send notifications to observers.
769 Refresh(); 774 Refresh();
770 } 775 }
771 776
772 void PersonalDataManager::LoadProfiles() { 777 void PersonalDataManager::LoadProfiles() {
773 scoped_refptr<WebDataServiceBase> web_data_service = 778 scoped_refptr<WebDataServiceBase> web_data_service =
774 WebDataServiceBase::ForProfile(profile_); 779 WebDataServiceBase::ForContext(browser_context_);
775 if (!web_data_service.get()) { 780 if (!web_data_service.get()) {
776 NOTREACHED(); 781 NOTREACHED();
777 return; 782 return;
778 } 783 }
779 784
780 CancelPendingQuery(&pending_profiles_query_); 785 CancelPendingQuery(&pending_profiles_query_);
781 786
782 pending_profiles_query_ = AutofillWebData::ForService( 787 pending_profiles_query_ = AutofillWebData::ForService(
783 web_data_service)->GetAutofillProfiles(this); 788 web_data_service)->GetAutofillProfiles(this);
784 } 789 }
785 790
786 // Win and Linux implementations do nothing. Mac implementation fills in the 791 // Win and Linux implementations do nothing. Mac implementation fills in the
787 // contents of |auxiliary_profiles_|. 792 // contents of |auxiliary_profiles_|.
788 #if !defined(OS_MACOSX) 793 #if !defined(OS_MACOSX)
789 void PersonalDataManager::LoadAuxiliaryProfiles() const { 794 void PersonalDataManager::LoadAuxiliaryProfiles() const {
790 } 795 }
791 #endif 796 #endif
792 797
793 void PersonalDataManager::LoadCreditCards() { 798 void PersonalDataManager::LoadCreditCards() {
794 scoped_refptr<WebDataServiceBase> web_data_service = 799 scoped_refptr<WebDataServiceBase> web_data_service =
795 WebDataServiceBase::ForProfile(profile_); 800 WebDataServiceBase::ForContext(browser_context_);
796 if (!web_data_service.get()) { 801 if (!web_data_service.get()) {
797 NOTREACHED(); 802 NOTREACHED();
798 return; 803 return;
799 } 804 }
800 805
801 CancelPendingQuery(&pending_creditcards_query_); 806 CancelPendingQuery(&pending_creditcards_query_);
802 807
803 pending_creditcards_query_ = AutofillWebData::ForService( 808 pending_creditcards_query_ = AutofillWebData::ForService(
804 web_data_service)->GetCreditCards(this); 809 web_data_service)->GetCreditCards(this);
805 } 810 }
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
838 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin(); 843 for (std::vector<CreditCard*>::iterator iter = credit_cards.begin();
839 iter != credit_cards.end(); ++iter) { 844 iter != credit_cards.end(); ++iter) {
840 credit_cards_.push_back(*iter); 845 credit_cards_.push_back(*iter);
841 } 846 }
842 } 847 }
843 848
844 void PersonalDataManager::CancelPendingQuery( 849 void PersonalDataManager::CancelPendingQuery(
845 WebDataServiceBase::Handle* handle) { 850 WebDataServiceBase::Handle* handle) {
846 if (*handle) { 851 if (*handle) {
847 scoped_refptr<WebDataServiceBase> web_data_service = 852 scoped_refptr<WebDataServiceBase> web_data_service =
848 WebDataServiceBase::ForProfile(profile_); 853 WebDataServiceBase::ForContext(browser_context_);
849 if (!web_data_service.get()) { 854 if (!web_data_service.get()) {
850 NOTREACHED(); 855 NOTREACHED();
851 return; 856 return;
852 } 857 }
853 web_data_service->CancelRequest(*handle); 858 web_data_service->CancelRequest(*handle);
854 } 859 }
855 *handle = 0; 860 *handle = 0;
856 } 861 }
857 862
858 void PersonalDataManager::SaveImportedProfile( 863 void PersonalDataManager::SaveImportedProfile(
859 const AutofillProfile& imported_profile) { 864 const AutofillProfile& imported_profile) {
860 if (profile_->IsOffTheRecord()) { 865 if (browser_context_->IsOffTheRecord()) {
861 // The |IsOffTheRecord| check should happen earlier in the import process, 866 // The |IsOffTheRecord| check should happen earlier in the import process,
862 // upon form submission. 867 // upon form submission.
863 NOTREACHED(); 868 NOTREACHED();
864 return; 869 return;
865 } 870 }
866 871
867 // Don't save a web profile if the data in the profile is a subset of an 872 // Don't save a web profile if the data in the profile is a subset of an
868 // auxiliary profile. 873 // auxiliary profile.
869 for (std::vector<AutofillProfile*>::const_iterator iter = 874 for (std::vector<AutofillProfile*>::const_iterator iter =
870 auxiliary_profiles_.begin(); 875 auxiliary_profiles_.begin();
871 iter != auxiliary_profiles_.end(); ++iter) { 876 iter != auxiliary_profiles_.end(); ++iter) {
872 if (imported_profile.IsSubsetOf(**iter)) 877 if (imported_profile.IsSubsetOf(**iter))
873 return; 878 return;
874 } 879 }
875 880
876 std::vector<AutofillProfile> profiles; 881 std::vector<AutofillProfile> profiles;
877 MergeProfile(imported_profile, web_profiles_.get(), &profiles); 882 MergeProfile(imported_profile, web_profiles_.get(), &profiles);
878 SetProfiles(&profiles); 883 SetProfiles(&profiles);
879 } 884 }
880 885
881 886
882 void PersonalDataManager::SaveImportedCreditCard( 887 void PersonalDataManager::SaveImportedCreditCard(
883 const CreditCard& imported_credit_card) { 888 const CreditCard& imported_credit_card) {
884 DCHECK(!imported_credit_card.number().empty()); 889 DCHECK(!imported_credit_card.number().empty());
885 if (profile_->IsOffTheRecord()) { 890 if (browser_context_->IsOffTheRecord()) {
886 // The |IsOffTheRecord| check should happen earlier in the import process, 891 // The |IsOffTheRecord| check should happen earlier in the import process,
887 // upon form submission. 892 // upon form submission.
888 NOTREACHED(); 893 NOTREACHED();
889 return; 894 return;
890 } 895 }
891 896
892 // Set to true if |imported_credit_card| is merged into the credit card list. 897 // Set to true if |imported_credit_card| is merged into the credit card list.
893 bool merged = false; 898 bool merged = false;
894 899
895 std::vector<CreditCard> creditcards; 900 std::vector<CreditCard> creditcards;
896 for (std::vector<CreditCard*>::const_iterator card = credit_cards_.begin(); 901 for (std::vector<CreditCard*>::const_iterator card = credit_cards_.begin();
897 card != credit_cards_.end(); 902 card != credit_cards_.end();
898 ++card) { 903 ++card) {
899 // If |imported_credit_card| has not yet been merged, check whether it 904 // If |imported_credit_card| has not yet been merged, check whether it
900 // should be with the current |card|. 905 // should be with the current |card|.
901 if (!merged && (*card)->UpdateFromImportedCard(imported_credit_card)) 906 if (!merged && (*card)->UpdateFromImportedCard(imported_credit_card))
902 merged = true; 907 merged = true;
903 908
904 creditcards.push_back(**card); 909 creditcards.push_back(**card);
905 } 910 }
906 911
907 if (!merged) 912 if (!merged)
908 creditcards.push_back(imported_credit_card); 913 creditcards.push_back(imported_credit_card);
909 914
910 SetCreditCards(&creditcards); 915 SetCreditCards(&creditcards);
911 } 916 }
912 917
913 void PersonalDataManager::EmptyMigrationTrash() { 918 void PersonalDataManager::EmptyMigrationTrash() {
914 if (!profile_ || profile_->IsOffTheRecord()) 919 if (!browser_context_ || browser_context_->IsOffTheRecord())
915 return; 920 return;
916 921
917 scoped_refptr<WebDataServiceBase> web_data_service = 922 scoped_refptr<WebDataServiceBase> web_data_service =
918 WebDataServiceBase::ForProfile(profile_); 923 WebDataServiceBase::ForContext(browser_context_);
919 if (!web_data_service.get()) { 924 if (!web_data_service.get()) {
920 NOTREACHED(); 925 NOTREACHED();
921 return; 926 return;
922 } 927 }
923 928
924 ProfileSyncService* sync_service = 929 ProfileSyncService* sync_service =
925 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_); 930 ProfileSyncServiceFactory::GetInstance()->GetForProfile(
931 static_cast<Profile*>(browser_context_));
erikwright (departed) 2012/09/04 19:42:15 Will this be permanent?
Jói 2012/09/04 20:10:25 This goes away in the next change after, where ins
926 if (!sync_service) 932 if (!sync_service)
927 return; 933 return;
928 934
929 AutofillWebData* autofill_data = 935 AutofillWebData* autofill_data =
930 AutofillWebData::ForService(web_data_service); 936 AutofillWebData::ForService(web_data_service);
931 if (!sync_service->HasSyncSetupCompleted()) { 937 if (!sync_service->HasSyncSetupCompleted()) {
932 autofill_data->EmptyMigrationTrash(false); 938 autofill_data->EmptyMigrationTrash(false);
933 } else if (sync_service->ShouldPushChanges()) { 939 } else if (sync_service->ShouldPushChanges()) {
934 autofill_data->EmptyMigrationTrash(true); 940 autofill_data->EmptyMigrationTrash(true);
935 } else { 941 } else {
(...skipping 12 matching lines...) Expand all
948 } 954 }
949 955
950 const AutofillMetrics* PersonalDataManager::metric_logger() const { 956 const AutofillMetrics* PersonalDataManager::metric_logger() const {
951 return metric_logger_.get(); 957 return metric_logger_.get();
952 } 958 }
953 959
954 void PersonalDataManager::set_metric_logger( 960 void PersonalDataManager::set_metric_logger(
955 const AutofillMetrics* metric_logger) { 961 const AutofillMetrics* metric_logger) {
956 metric_logger_.reset(metric_logger); 962 metric_logger_.reset(metric_logger);
957 } 963 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698