| 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 "components/password_manager/core/browser/password_form_manager.h" | 5 #include "components/password_manager/core/browser/password_form_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 } | 84 } |
| 85 | 85 |
| 86 } // namespace | 86 } // namespace |
| 87 | 87 |
| 88 PasswordFormManager::PasswordFormManager( | 88 PasswordFormManager::PasswordFormManager( |
| 89 PasswordManager* password_manager, | 89 PasswordManager* password_manager, |
| 90 PasswordManagerClient* client, | 90 PasswordManagerClient* client, |
| 91 const base::WeakPtr<PasswordManagerDriver>& driver, | 91 const base::WeakPtr<PasswordManagerDriver>& driver, |
| 92 const PasswordForm& observed_form, | 92 const PasswordForm& observed_form, |
| 93 bool ssl_valid) | 93 bool ssl_valid) |
| 94 : best_matches_deleter_(&best_matches_), | 94 : observed_form_(CopyAndModifySSLValidity(observed_form, ssl_valid)), |
| 95 observed_form_(CopyAndModifySSLValidity(observed_form, ssl_valid)), | |
| 96 provisionally_saved_form_(nullptr), | 95 provisionally_saved_form_(nullptr), |
| 97 other_possible_username_action_( | 96 other_possible_username_action_( |
| 98 PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES), | 97 PasswordFormManager::IGNORE_OTHER_POSSIBLE_USERNAMES), |
| 99 is_new_login_(true), | 98 is_new_login_(true), |
| 100 has_generated_password_(false), | 99 has_generated_password_(false), |
| 101 password_manager_(password_manager), | 100 password_manager_(password_manager), |
| 102 preferred_match_(nullptr), | 101 preferred_match_(nullptr), |
| 103 is_ignorable_change_password_form_(false), | 102 is_ignorable_change_password_form_(false), |
| 104 state_(PRE_MATCHING_PHASE), | 103 state_(PRE_MATCHING_PHASE), |
| 105 client_(client), | 104 client_(client), |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } else if (no_username) { | 334 } else if (no_username) { |
| 336 form_type_ = kFormTypeLoginNoUsername; | 335 form_type_ = kFormTypeLoginNoUsername; |
| 337 } else { | 336 } else { |
| 338 form_type_ = kFormTypeLogin; | 337 form_type_ = kFormTypeLogin; |
| 339 } | 338 } |
| 340 } | 339 } |
| 341 | 340 |
| 342 void PasswordFormManager::OnRequestDone( | 341 void PasswordFormManager::OnRequestDone( |
| 343 ScopedVector<PasswordForm> logins_result) { | 342 ScopedVector<PasswordForm> logins_result) { |
| 344 preferred_match_ = nullptr; | 343 preferred_match_ = nullptr; |
| 345 STLDeleteValues(&best_matches_); | 344 best_matches_.clear(); |
| 346 const size_t logins_result_size = logins_result.size(); | 345 const size_t logins_result_size = logins_result.size(); |
| 347 | 346 |
| 348 scoped_ptr<BrowserSavePasswordProgressLogger> logger; | 347 scoped_ptr<BrowserSavePasswordProgressLogger> logger; |
| 349 if (client_->IsLoggingActive()) { | 348 if (client_->IsLoggingActive()) { |
| 350 logger.reset(new BrowserSavePasswordProgressLogger(client_)); | 349 logger.reset(new BrowserSavePasswordProgressLogger(client_)); |
| 351 logger->LogMessage(Logger::STRING_ON_REQUEST_DONE_METHOD); | 350 logger->LogMessage(Logger::STRING_ON_REQUEST_DONE_METHOD); |
| 352 } | 351 } |
| 353 | 352 |
| 354 // First, compute scores for credentials in |login_result|. | 353 // First, compute scores for credentials in |login_result|. |
| 355 std::vector<int> credential_scores; | 354 std::vector<int> credential_scores; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 403 is_credential_protected |= login->type == PasswordForm::TYPE_GENERATED; | 402 is_credential_protected |= login->type == PasswordForm::TYPE_GENERATED; |
| 404 | 403 |
| 405 if (is_credential_protected) | 404 if (is_credential_protected) |
| 406 protected_credentials.push_back(login.Pass()); | 405 protected_credentials.push_back(login.Pass()); |
| 407 continue; | 406 continue; |
| 408 } | 407 } |
| 409 | 408 |
| 410 // If there is another best-score match for the same username, replace it. | 409 // If there is another best-score match for the same username, replace it. |
| 411 // TODO(vabr): Spare the replacing and keep the first instead of the last | 410 // TODO(vabr): Spare the replacing and keep the first instead of the last |
| 412 // candidate. | 411 // candidate. |
| 413 PasswordForm*& best_match = best_matches_[login->username_value]; | 412 const base::string16& username = login->username_value; |
| 414 if (best_match == preferred_match_) | 413 auto best_match_username = best_matches_.find(username); |
| 414 if (best_match_username != best_matches_.end() && |
| 415 best_match_username->second == preferred_match_) { |
| 415 preferred_match_ = nullptr; | 416 preferred_match_ = nullptr; |
| 416 delete best_match; | 417 } |
| 417 // Transfer ownership into the map. | 418 // Transfer ownership into the map. |
| 418 best_match = login.release(); | 419 const PasswordForm* best_match = login.get(); |
| 420 // TODO(mgiuca): Directly assign to |best_match_username|, instead of doing |
| 421 // a second map traversal. This will only be possible once we have C++11 |
| 422 // library support (then |best_matches_| can be a map of scoped_ptrs instead |
| 423 // of a ScopedPtrMap). |
| 424 best_matches_.set(username, login.Pass()); |
| 419 if (best_match->preferred) | 425 if (best_match->preferred) |
| 420 preferred_match_ = best_match; | 426 preferred_match_ = best_match; |
| 421 } | 427 } |
| 422 | 428 |
| 423 // Add the protected results if we don't already have a result with the same | 429 // Add the protected results if we don't already have a result with the same |
| 424 // username. | 430 // username. |
| 425 for (ScopedVector<PasswordForm>::iterator it = protected_credentials.begin(); | 431 for (ScopedVector<PasswordForm>::iterator it = protected_credentials.begin(); |
| 426 it != protected_credentials.end(); ++it) { | 432 it != protected_credentials.end(); ++it) { |
| 427 // Take ownership of the PasswordForm from the ScopedVector. | 433 // Take ownership of the PasswordForm from the ScopedVector. |
| 428 scoped_ptr<PasswordForm> protege(*it); | 434 scoped_ptr<PasswordForm> protege(*it); |
| 429 *it = nullptr; | 435 *it = nullptr; |
| 430 | 436 const base::string16& username = protege->username_value; |
| 431 PasswordForm*& corresponding_best_match = | 437 best_matches_.insert(username, protege.Pass()); |
| 432 best_matches_[protege->username_value]; | |
| 433 if (!corresponding_best_match) | |
| 434 corresponding_best_match = protege.release(); | |
| 435 } | 438 } |
| 436 | 439 |
| 437 client_->AutofillResultsComputed(); | 440 client_->AutofillResultsComputed(); |
| 438 | 441 |
| 439 UMA_HISTOGRAM_COUNTS("PasswordManager.NumPasswordsNotShown", | 442 UMA_HISTOGRAM_COUNTS("PasswordManager.NumPasswordsNotShown", |
| 440 logins_result_size - best_matches_.size()); | 443 logins_result_size - best_matches_.size()); |
| 441 | 444 |
| 442 // It is possible we have at least one match but have no preferred_match_, | 445 // It is possible we have at least one match but have no preferred_match_, |
| 443 // because a user may have chosen to 'Forget' the preferred match. So we | 446 // because a user may have chosen to 'Forget' the preferred match. So we |
| 444 // just pick the first one and whichever the user selects for submit will | 447 // just pick the first one and whichever the user selects for submit will |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 567 if (reset_preferred_login) { | 570 if (reset_preferred_login) { |
| 568 UpdatePreferredLoginState(password_store); | 571 UpdatePreferredLoginState(password_store); |
| 569 } | 572 } |
| 570 } | 573 } |
| 571 | 574 |
| 572 void PasswordFormManager::SanitizePossibleUsernames(PasswordForm* form) { | 575 void PasswordFormManager::SanitizePossibleUsernames(PasswordForm* form) { |
| 573 // Remove any possible usernames that could be credit cards or SSN for privacy | 576 // Remove any possible usernames that could be credit cards or SSN for privacy |
| 574 // reasons. Also remove duplicates, both in other_possible_usernames and | 577 // reasons. Also remove duplicates, both in other_possible_usernames and |
| 575 // between other_possible_usernames and username_value. | 578 // between other_possible_usernames and username_value. |
| 576 std::set<base::string16> set; | 579 std::set<base::string16> set; |
| 577 for (std::vector<base::string16>::iterator it = | 580 for (std::vector<base::string16>::const_iterator it = |
| 578 form->other_possible_usernames.begin(); | 581 form->other_possible_usernames.begin(); |
| 579 it != form->other_possible_usernames.end(); ++it) { | 582 it != form->other_possible_usernames.end(); ++it) { |
| 580 if (!autofill::IsValidCreditCardNumber(*it) && !autofill::IsSSN(*it)) | 583 if (!autofill::IsValidCreditCardNumber(*it) && !autofill::IsSSN(*it)) |
| 581 set.insert(*it); | 584 set.insert(*it); |
| 582 } | 585 } |
| 583 set.erase(form->username_value); | 586 set.erase(form->username_value); |
| 584 std::vector<base::string16> temp(set.begin(), set.end()); | 587 std::vector<base::string16> temp(set.begin(), set.end()); |
| 585 form->other_possible_usernames.swap(temp); | 588 form->other_possible_usernames.swap(temp); |
| 586 } | 589 } |
| 587 | 590 |
| 588 void PasswordFormManager::UpdatePreferredLoginState( | 591 void PasswordFormManager::UpdatePreferredLoginState( |
| 589 PasswordStore* password_store) { | 592 PasswordStore* password_store) { |
| 590 DCHECK(password_store); | 593 DCHECK(password_store); |
| 591 PasswordFormMap::iterator iter; | 594 PasswordFormMap::const_iterator iter; |
| 592 for (iter = best_matches_.begin(); iter != best_matches_.end(); iter++) { | 595 for (iter = best_matches_.begin(); iter != best_matches_.end(); iter++) { |
| 593 if (iter->second->username_value != pending_credentials_.username_value && | 596 if (iter->second->username_value != pending_credentials_.username_value && |
| 594 iter->second->preferred) { | 597 iter->second->preferred) { |
| 595 // This wasn't the selected login but it used to be preferred. | 598 // This wasn't the selected login but it used to be preferred. |
| 596 iter->second->preferred = false; | 599 iter->second->preferred = false; |
| 597 if (user_action_ == kUserActionNone) | 600 if (user_action_ == kUserActionNone) |
| 598 user_action_ = kUserActionChoose; | 601 user_action_ = kUserActionChoose; |
| 599 password_store->UpdateLogin(*iter->second); | 602 password_store->UpdateLogin(*iter->second); |
| 600 } | 603 } |
| 601 } | 604 } |
| (...skipping 339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 941 LogPasswordGenerationSubmissionEvent(PASSWORD_SUBMITTED); | 944 LogPasswordGenerationSubmissionEvent(PASSWORD_SUBMITTED); |
| 942 } | 945 } |
| 943 | 946 |
| 944 void PasswordFormManager::SubmitFailed() { | 947 void PasswordFormManager::SubmitFailed() { |
| 945 submit_result_ = kSubmitResultFailed; | 948 submit_result_ = kSubmitResultFailed; |
| 946 if (has_generated_password_) | 949 if (has_generated_password_) |
| 947 LogPasswordGenerationSubmissionEvent(PASSWORD_SUBMISSION_FAILED); | 950 LogPasswordGenerationSubmissionEvent(PASSWORD_SUBMISSION_FAILED); |
| 948 } | 951 } |
| 949 | 952 |
| 950 } // namespace password_manager | 953 } // namespace password_manager |
| OLD | NEW |