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