| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/password_manager/password_form_manager.h" | 5 #include "chrome/browser/password_manager/password_form_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "chrome/browser/password_manager/password_manager.h" | 10 #include "chrome/browser/password_manager/password_manager.h" |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // shows up in the future for this origin URL. However, we don't want to | 113 // shows up in the future for this origin URL. However, we don't want to |
| 114 // delete logins that were actually saved on a different page (hence with | 114 // delete logins that were actually saved on a different page (hence with |
| 115 // different origin URL) and just happened to match this form because of | 115 // different origin URL) and just happened to match this form because of |
| 116 // the scoring algorithm. See bug 1204493. | 116 // the scoring algorithm. See bug 1204493. |
| 117 if (iter->second->origin == observed_form_.origin) | 117 if (iter->second->origin == observed_form_.origin) |
| 118 password_store->RemoveLogin(*iter->second); | 118 password_store->RemoveLogin(*iter->second); |
| 119 } | 119 } |
| 120 } | 120 } |
| 121 | 121 |
| 122 // Save the pending_credentials_ entry marked as blacklisted. | 122 // Save the pending_credentials_ entry marked as blacklisted. |
| 123 SaveAsNewLogin(); | 123 SaveAsNewLogin(false); |
| 124 } | 124 } |
| 125 | 125 |
| 126 bool PasswordFormManager::IsNewLogin() { | 126 bool PasswordFormManager::IsNewLogin() { |
| 127 DCHECK_EQ(state_, POST_MATCHING_PHASE); | 127 DCHECK_EQ(state_, POST_MATCHING_PHASE); |
| 128 return is_new_login_; | 128 return is_new_login_; |
| 129 } | 129 } |
| 130 | 130 |
| 131 void PasswordFormManager::ProvisionallySave(const PasswordForm& credentials) { | 131 void PasswordFormManager::ProvisionallySave(const PasswordForm& credentials) { |
| 132 DCHECK_EQ(state_, POST_MATCHING_PHASE); | 132 DCHECK_EQ(state_, POST_MATCHING_PHASE); |
| 133 DCHECK(DoesManage(credentials)); | 133 DCHECK(DoesManage(credentials)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 154 | 154 |
| 155 pending_credentials_.password_value = credentials.password_value; | 155 pending_credentials_.password_value = credentials.password_value; |
| 156 pending_credentials_.preferred = credentials.preferred; | 156 pending_credentials_.preferred = credentials.preferred; |
| 157 } | 157 } |
| 158 | 158 |
| 159 void PasswordFormManager::Save() { | 159 void PasswordFormManager::Save() { |
| 160 DCHECK_EQ(state_, POST_MATCHING_PHASE); | 160 DCHECK_EQ(state_, POST_MATCHING_PHASE); |
| 161 DCHECK(!profile_->IsOffTheRecord()); | 161 DCHECK(!profile_->IsOffTheRecord()); |
| 162 | 162 |
| 163 if (IsNewLogin()) | 163 if (IsNewLogin()) |
| 164 SaveAsNewLogin(); | 164 SaveAsNewLogin(true); |
| 165 else | 165 else |
| 166 UpdateLogin(); | 166 UpdateLogin(); |
| 167 } | 167 } |
| 168 | 168 |
| 169 void PasswordFormManager::FetchMatchingLoginsFromWebDatabase() { | 169 void PasswordFormManager::FetchMatchingLoginsFromWebDatabase() { |
| 170 DCHECK_EQ(state_, PRE_MATCHING_PHASE); | 170 DCHECK_EQ(state_, PRE_MATCHING_PHASE); |
| 171 DCHECK(!pending_login_query_); | 171 DCHECK(!pending_login_query_); |
| 172 state_ = MATCHING_PHASE; | 172 state_ = MATCHING_PHASE; |
| 173 PasswordStore* password_store = | 173 PasswordStore* password_store = |
| 174 profile_->GetPasswordStore(Profile::EXPLICIT_ACCESS); | 174 profile_->GetPasswordStore(Profile::EXPLICIT_ACCESS); |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 return true; | 286 return true; |
| 287 } | 287 } |
| 288 // Don't match an invalid SSL form with one saved under secure | 288 // Don't match an invalid SSL form with one saved under secure |
| 289 // circumstances. | 289 // circumstances. |
| 290 if (form.ssl_valid && !observed_form_.ssl_valid) { | 290 if (form.ssl_valid && !observed_form_.ssl_valid) { |
| 291 return true; | 291 return true; |
| 292 } | 292 } |
| 293 return false; | 293 return false; |
| 294 } | 294 } |
| 295 | 295 |
| 296 void PasswordFormManager::SaveAsNewLogin() { | 296 void PasswordFormManager::SaveAsNewLogin(bool reset_preferred_login) { |
| 297 DCHECK_EQ(state_, POST_MATCHING_PHASE); | 297 DCHECK_EQ(state_, POST_MATCHING_PHASE); |
| 298 DCHECK(IsNewLogin()); | 298 DCHECK(IsNewLogin()); |
| 299 // The new_form is being used to sign in, so it is preferred. | 299 // The new_form is being used to sign in, so it is preferred. |
| 300 DCHECK(pending_credentials_.preferred); | 300 DCHECK(pending_credentials_.preferred); |
| 301 // new_form contains the same basic data as observed_form_ (because its the | 301 // new_form contains the same basic data as observed_form_ (because its the |
| 302 // same form), but with the newly added credentials. | 302 // same form), but with the newly added credentials. |
| 303 | 303 |
| 304 DCHECK(!profile_->IsOffTheRecord()); | 304 DCHECK(!profile_->IsOffTheRecord()); |
| 305 | 305 |
| 306 PasswordStore* password_store = | 306 PasswordStore* password_store = |
| 307 profile_->GetPasswordStore(Profile::IMPLICIT_ACCESS); | 307 profile_->GetPasswordStore(Profile::IMPLICIT_ACCESS); |
| 308 if (!password_store) { | 308 if (!password_store) { |
| 309 NOTREACHED(); | 309 NOTREACHED(); |
| 310 return; | 310 return; |
| 311 } | 311 } |
| 312 | 312 |
| 313 pending_credentials_.date_created = Time::Now(); | 313 pending_credentials_.date_created = Time::Now(); |
| 314 password_store->AddLogin(pending_credentials_); | 314 password_store->AddLogin(pending_credentials_); |
| 315 |
| 316 if (reset_preferred_login) { |
| 317 UpdatePreferredLoginState(password_store); |
| 318 } |
| 319 } |
| 320 |
| 321 void PasswordFormManager::UpdatePreferredLoginState( |
| 322 PasswordStore* password_store) { |
| 323 DCHECK(password_store); |
| 324 PasswordFormMap::iterator iter; |
| 325 for (iter = best_matches_.begin(); iter != best_matches_.end(); iter++) { |
| 326 if (iter->second->username_value != pending_credentials_.username_value && |
| 327 iter->second->preferred) { |
| 328 // This wasn't the selected login but it used to be preferred. |
| 329 iter->second->preferred = false; |
| 330 password_store->UpdateLogin(*iter->second); |
| 331 } |
| 332 } |
| 315 } | 333 } |
| 316 | 334 |
| 317 void PasswordFormManager::UpdateLogin() { | 335 void PasswordFormManager::UpdateLogin() { |
| 318 DCHECK_EQ(state_, POST_MATCHING_PHASE); | 336 DCHECK_EQ(state_, POST_MATCHING_PHASE); |
| 319 DCHECK(preferred_match_); | 337 DCHECK(preferred_match_); |
| 320 // If we're doing an Update, its because we autofilled a form and the user | 338 // If we're doing an Update, its because we autofilled a form and the user |
| 321 // submitted it with a possibly new password value, page security, or selected | 339 // submitted it with a possibly new password value, page security, or selected |
| 322 // one of the non-preferred matches, thus requiring a swap of preferred bits. | 340 // one of the non-preferred matches, thus requiring a swap of preferred bits. |
| 323 DCHECK(!IsNewLogin() && pending_credentials_.preferred); | 341 DCHECK(!IsNewLogin() && pending_credentials_.preferred); |
| 324 DCHECK(!profile_->IsOffTheRecord()); | 342 DCHECK(!profile_->IsOffTheRecord()); |
| 325 | 343 |
| 326 PasswordStore* password_store = | 344 PasswordStore* password_store = |
| 327 profile_->GetPasswordStore(Profile::IMPLICIT_ACCESS); | 345 profile_->GetPasswordStore(Profile::IMPLICIT_ACCESS); |
| 328 if (!password_store) { | 346 if (!password_store) { |
| 329 NOTREACHED(); | 347 NOTREACHED(); |
| 330 return; | 348 return; |
| 331 } | 349 } |
| 332 | 350 |
| 333 // Update all matches to reflect new preferred status. | 351 UpdatePreferredLoginState(password_store); |
| 334 PasswordFormMap::iterator iter; | 352 |
| 335 for (iter = best_matches_.begin(); iter != best_matches_.end(); iter++) { | |
| 336 if ((iter->second->username_value != pending_credentials_.username_value) && | |
| 337 iter->second->preferred) { | |
| 338 // This wasn't the selected login but it used to be preferred. | |
| 339 iter->second->preferred = false; | |
| 340 password_store->UpdateLogin(*iter->second); | |
| 341 } | |
| 342 } | |
| 343 // Update the new preferred login. | 353 // Update the new preferred login. |
| 344 // Note origin.spec().length > signon_realm.length implies the origin has a | 354 // Note origin.spec().length > signon_realm.length implies the origin has a |
| 345 // path, since signon_realm is a prefix of origin for HTML password forms. | 355 // path, since signon_realm is a prefix of origin for HTML password forms. |
| 346 if ((observed_form_.scheme == PasswordForm::SCHEME_HTML) && | 356 if ((observed_form_.scheme == PasswordForm::SCHEME_HTML) && |
| 347 (observed_form_.origin.spec().length() > | 357 (observed_form_.origin.spec().length() > |
| 348 observed_form_.signon_realm.length()) && | 358 observed_form_.signon_realm.length()) && |
| 349 (observed_form_.signon_realm == pending_credentials_.origin.spec())) { | 359 (observed_form_.signon_realm == pending_credentials_.origin.spec())) { |
| 350 // The user logged in successfully with one of our autofilled logins on a | 360 // The user logged in successfully with one of our autofilled logins on a |
| 351 // page with non-empty path, but the autofilled entry was initially saved/ | 361 // page with non-empty path, but the autofilled entry was initially saved/ |
| 352 // imported with an empty path. Rather than just mark this entry preferred, | 362 // imported with an empty path. Rather than just mark this entry preferred, |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 if (candidate.password_element == observed_form_.password_element) | 429 if (candidate.password_element == observed_form_.password_element) |
| 420 score += 1 << 2; | 430 score += 1 << 2; |
| 421 if (candidate.submit_element == observed_form_.submit_element) | 431 if (candidate.submit_element == observed_form_.submit_element) |
| 422 score += 1 << 1; | 432 score += 1 << 1; |
| 423 if (candidate.username_element == observed_form_.username_element) | 433 if (candidate.username_element == observed_form_.username_element) |
| 424 score += 1 << 0; | 434 score += 1 << 0; |
| 425 } | 435 } |
| 426 | 436 |
| 427 return score; | 437 return score; |
| 428 } | 438 } |
| OLD | NEW |