| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/passwords/password_manager_presenter.h" | 5 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 if (!is_android_uri) | 66 if (!is_android_uri) |
| 67 return "0"; | 67 return "0"; |
| 68 if (is_clickable) | 68 if (is_clickable) |
| 69 return "1"; | 69 return "1"; |
| 70 return "2"; | 70 return "2"; |
| 71 } | 71 } |
| 72 | 72 |
| 73 // Creates key for sorting password or password exception entries. | 73 // Creates key for sorting password or password exception entries. |
| 74 // The key is eTLD+1 followed by subdomains | 74 // The key is eTLD+1 followed by subdomains |
| 75 // (e.g. secure.accounts.example.com => example.com.accounts.secure). | 75 // (e.g. secure.accounts.example.com => example.com.accounts.secure). |
| 76 // If |username_and_password_in_key == true|, username and password is appended | 76 // If |entry_type == SAVED|, username, password and federation are appended to |
| 77 // to the key. The entry type code (non-Android, Android w/ or w/o affiliated | 77 // the key. The entry type code (non-Android, Android w/ or w/o affiliated web |
| 78 // web realm) is also appended to the key. | 78 // realm) is also appended to the key. |
| 79 std::string CreateSortKey(const autofill::PasswordForm& form, | 79 std::string CreateSortKey(const autofill::PasswordForm& form, |
| 80 bool username_and_password_in_key) { | 80 PasswordEntryType entry_type) { |
| 81 bool is_android_uri = false; | 81 bool is_android_uri = false; |
| 82 bool is_clickable = false; | 82 bool is_clickable = false; |
| 83 GURL link_url; | 83 GURL link_url; |
| 84 std::string origin = password_manager::GetShownOriginAndLinkUrl( | 84 std::string origin = password_manager::GetShownOriginAndLinkUrl( |
| 85 form, &is_android_uri, &link_url, &is_clickable); | 85 form, &is_android_uri, &link_url, &is_clickable); |
| 86 | 86 |
| 87 if (!is_clickable) { // e.g. android://com.example.r => r.example.com. | 87 if (!is_clickable) { // e.g. android://com.example.r => r.example.com. |
| 88 origin = SplitByDotAndReverse( | 88 origin = SplitByDotAndReverse( |
| 89 StringPiece(&origin[kAndroidAppSchemeAndDelimiterLength], | 89 StringPiece(&origin[kAndroidAppSchemeAndDelimiterLength], |
| 90 origin.length() - kAndroidAppSchemeAndDelimiterLength)); | 90 origin.length() - kAndroidAppSchemeAndDelimiterLength)); |
| 91 } | 91 } |
| 92 | 92 |
| 93 std::string site_name = | 93 std::string site_name = |
| 94 net::registry_controlled_domains::GetDomainAndRegistry( | 94 net::registry_controlled_domains::GetDomainAndRegistry( |
| 95 origin, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 95 origin, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 96 if (site_name.empty()) // e.g. localhost. | 96 if (site_name.empty()) // e.g. localhost. |
| 97 site_name = origin; | 97 site_name = origin; |
| 98 std::string key = | 98 std::string key = |
| 99 site_name + SplitByDotAndReverse(StringPiece( | 99 site_name + SplitByDotAndReverse(StringPiece( |
| 100 &origin[0], origin.length() - site_name.length())); | 100 &origin[0], origin.length() - site_name.length())); |
| 101 | 101 |
| 102 if (username_and_password_in_key) { | 102 if (entry_type == PasswordEntryType::SAVED) { |
| 103 key = key + kSortKeyPartsSeparator + | 103 key = key + kSortKeyPartsSeparator + |
| 104 base::UTF16ToUTF8(form.username_value) + kSortKeyPartsSeparator + | 104 base::UTF16ToUTF8(form.username_value) + kSortKeyPartsSeparator + |
| 105 base::UTF16ToUTF8(form.password_value); | 105 base::UTF16ToUTF8(form.password_value); |
| 106 if (!form.federation_origin.unique()) |
| 107 key = key + kSortKeyPartsSeparator + form.federation_origin.host(); |
| 106 } | 108 } |
| 107 | 109 |
| 108 // Since Android and non-Android entries shouldn't be merged into one entry, | 110 // Since Android and non-Android entries shouldn't be merged into one entry, |
| 109 // add the entry type code to the sort key. | 111 // add the entry type code to the sort key. |
| 110 key += | 112 key += |
| 111 kSortKeyPartsSeparator + GetEntryTypeCode(is_android_uri, is_clickable); | 113 kSortKeyPartsSeparator + GetEntryTypeCode(is_android_uri, is_clickable); |
| 112 return key; | 114 return key; |
| 113 } | 115 } |
| 114 | 116 |
| 115 // Finds duplicates of |form| in |duplicates|, removes them from |store| and | 117 // Finds duplicates of |form| in |duplicates|, removes them from |store| and |
| 116 // from |duplicates|. | 118 // from |duplicates|. |
| 117 void RemoveDuplicates(const autofill::PasswordForm& form, | 119 void RemoveDuplicates(const autofill::PasswordForm& form, |
| 118 DuplicatesMap* duplicates, | 120 DuplicatesMap* duplicates, |
| 119 PasswordStore* store, | 121 PasswordStore* store, |
| 120 bool username_and_password_in_key) { | 122 PasswordEntryType entry_type) { |
| 121 std::string key = | 123 std::string key = CreateSortKey(form, entry_type); |
| 122 CreateSortKey(form, username_and_password_in_key); | |
| 123 std::pair<DuplicatesMap::iterator, DuplicatesMap::iterator> dups = | 124 std::pair<DuplicatesMap::iterator, DuplicatesMap::iterator> dups = |
| 124 duplicates->equal_range(key); | 125 duplicates->equal_range(key); |
| 125 for (DuplicatesMap::iterator it = dups.first; it != dups.second; ++it) | 126 for (DuplicatesMap::iterator it = dups.first; it != dups.second; ++it) |
| 126 store->RemoveLogin(*it->second); | 127 store->RemoveLogin(*it->second); |
| 127 duplicates->erase(key); | 128 duplicates->erase(key); |
| 128 } | 129 } |
| 129 | 130 |
| 130 } // namespace | 131 } // namespace |
| 131 | 132 |
| 132 PasswordManagerPresenter::PasswordManagerPresenter( | 133 PasswordManagerPresenter::PasswordManagerPresenter( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 // |index| out of bounds might come from a compromised renderer | 181 // |index| out of bounds might come from a compromised renderer |
| 181 // (http://crbug.com/362054), or the user removed a password while a request | 182 // (http://crbug.com/362054), or the user removed a password while a request |
| 182 // to the store is in progress (i.e. |password_list_| is empty). | 183 // to the store is in progress (i.e. |password_list_| is empty). |
| 183 // Don't let it crash the browser. | 184 // Don't let it crash the browser. |
| 184 return; | 185 return; |
| 185 } | 186 } |
| 186 PasswordStore* store = GetPasswordStore(); | 187 PasswordStore* store = GetPasswordStore(); |
| 187 if (!store) | 188 if (!store) |
| 188 return; | 189 return; |
| 189 | 190 |
| 190 RemoveDuplicates(*password_list_[index], &password_duplicates_, | 191 RemoveDuplicates(*password_list_[index], &password_duplicates_, store, |
| 191 store, true); | 192 PasswordEntryType::SAVED); |
| 192 store->RemoveLogin(*password_list_[index]); | 193 store->RemoveLogin(*password_list_[index]); |
| 193 content::RecordAction( | 194 content::RecordAction( |
| 194 base::UserMetricsAction("PasswordManager_RemoveSavedPassword")); | 195 base::UserMetricsAction("PasswordManager_RemoveSavedPassword")); |
| 195 } | 196 } |
| 196 | 197 |
| 197 void PasswordManagerPresenter::RemovePasswordException(size_t index) { | 198 void PasswordManagerPresenter::RemovePasswordException(size_t index) { |
| 198 if (index >= password_exception_list_.size()) { | 199 if (index >= password_exception_list_.size()) { |
| 199 // |index| out of bounds might come from a compromised renderer | 200 // |index| out of bounds might come from a compromised renderer |
| 200 // (http://crbug.com/362054), or the user removed a password exception while | 201 // (http://crbug.com/362054), or the user removed a password exception while |
| 201 // a request to the store is in progress (i.e. |password_exception_list_| | 202 // a request to the store is in progress (i.e. |password_exception_list_| |
| 202 // is empty). Don't let it crash the browser. | 203 // is empty). Don't let it crash the browser. |
| 203 return; | 204 return; |
| 204 } | 205 } |
| 205 PasswordStore* store = GetPasswordStore(); | 206 PasswordStore* store = GetPasswordStore(); |
| 206 if (!store) | 207 if (!store) |
| 207 return; | 208 return; |
| 208 RemoveDuplicates(*password_exception_list_[index], | 209 RemoveDuplicates(*password_exception_list_[index], |
| 209 &password_exception_duplicates_, store, false); | 210 &password_exception_duplicates_, store, |
| 211 PasswordEntryType::BLACKLISTED); |
| 210 store->RemoveLogin(*password_exception_list_[index]); | 212 store->RemoveLogin(*password_exception_list_[index]); |
| 211 content::RecordAction( | 213 content::RecordAction( |
| 212 base::UserMetricsAction("PasswordManager_RemovePasswordException")); | 214 base::UserMetricsAction("PasswordManager_RemovePasswordException")); |
| 213 } | 215 } |
| 214 | 216 |
| 215 void PasswordManagerPresenter::RequestShowPassword(size_t index) { | 217 void PasswordManagerPresenter::RequestShowPassword(size_t index) { |
| 216 #if !defined(OS_ANDROID) // This is never called on Android. | 218 #if !defined(OS_ANDROID) // This is never called on Android. |
| 217 if (index >= password_list_.size()) { | 219 if (index >= password_list_.size()) { |
| 218 // |index| out of bounds might come from a compromised renderer | 220 // |index| out of bounds might come from a compromised renderer |
| 219 // (http://crbug.com/362054), or the user requested to show a password while | 221 // (http://crbug.com/362054), or the user requested to show a password while |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 password_view_->SetPasswordList(password_list_); | 290 password_view_->SetPasswordList(password_list_); |
| 289 } | 291 } |
| 290 | 292 |
| 291 void PasswordManagerPresenter::SetPasswordExceptionList() { | 293 void PasswordManagerPresenter::SetPasswordExceptionList() { |
| 292 password_view_->SetPasswordExceptionList(password_exception_list_); | 294 password_view_->SetPasswordExceptionList(password_exception_list_); |
| 293 } | 295 } |
| 294 | 296 |
| 295 void PasswordManagerPresenter::SortEntriesAndHideDuplicates( | 297 void PasswordManagerPresenter::SortEntriesAndHideDuplicates( |
| 296 std::vector<std::unique_ptr<autofill::PasswordForm>>* list, | 298 std::vector<std::unique_ptr<autofill::PasswordForm>>* list, |
| 297 DuplicatesMap* duplicates, | 299 DuplicatesMap* duplicates, |
| 298 bool username_and_password_in_key) { | 300 PasswordEntryType entry_type) { |
| 299 std::vector<std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>> | 301 std::vector<std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>> |
| 300 pairs; | 302 pairs; |
| 301 pairs.reserve(list->size()); | 303 pairs.reserve(list->size()); |
| 302 for (auto& form : *list) { | 304 for (auto& form : *list) { |
| 303 pairs.push_back(std::make_pair( | 305 pairs.push_back( |
| 304 CreateSortKey(*form, username_and_password_in_key), std::move(form))); | 306 std::make_pair(CreateSortKey(*form, entry_type), std::move(form))); |
| 305 } | 307 } |
| 306 | 308 |
| 307 std::sort( | 309 std::sort( |
| 308 pairs.begin(), pairs.end(), | 310 pairs.begin(), pairs.end(), |
| 309 [](const std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>& | 311 [](const std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>& |
| 310 left, | 312 left, |
| 311 const std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>& | 313 const std::pair<std::string, std::unique_ptr<autofill::PasswordForm>>& |
| 312 right) { return left.first < right.first; }); | 314 right) { return left.first < right.first; }); |
| 313 | 315 |
| 314 list->clear(); | 316 list->clear(); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 344 LOG(ERROR) << "No password store! Cannot display passwords."; | 346 LOG(ERROR) << "No password store! Cannot display passwords."; |
| 345 } | 347 } |
| 346 } | 348 } |
| 347 | 349 |
| 348 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults( | 350 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults( |
| 349 ScopedVector<autofill::PasswordForm> results) { | 351 ScopedVector<autofill::PasswordForm> results) { |
| 350 page_->password_list_ = | 352 page_->password_list_ = |
| 351 password_manager_util::ConvertScopedVector(std::move(results)); | 353 password_manager_util::ConvertScopedVector(std::move(results)); |
| 352 page_->SortEntriesAndHideDuplicates(&page_->password_list_, | 354 page_->SortEntriesAndHideDuplicates(&page_->password_list_, |
| 353 &page_->password_duplicates_, | 355 &page_->password_duplicates_, |
| 354 true /* use username and password */); | 356 PasswordEntryType::SAVED); |
| 355 page_->SetPasswordList(); | 357 page_->SetPasswordList(); |
| 356 } | 358 } |
| 357 | 359 |
| 358 PasswordManagerPresenter::PasswordExceptionListPopulater:: | 360 PasswordManagerPresenter::PasswordExceptionListPopulater:: |
| 359 PasswordExceptionListPopulater(PasswordManagerPresenter* page) | 361 PasswordExceptionListPopulater(PasswordManagerPresenter* page) |
| 360 : ListPopulater(page) { | 362 : ListPopulater(page) { |
| 361 } | 363 } |
| 362 | 364 |
| 363 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() { | 365 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() { |
| 364 PasswordStore* store = page_->GetPasswordStore(); | 366 PasswordStore* store = page_->GetPasswordStore(); |
| 365 if (store != NULL) { | 367 if (store != NULL) { |
| 366 cancelable_task_tracker()->TryCancelAll(); | 368 cancelable_task_tracker()->TryCancelAll(); |
| 367 store->GetBlacklistLoginsWithAffiliatedRealms(this); | 369 store->GetBlacklistLoginsWithAffiliatedRealms(this); |
| 368 } else { | 370 } else { |
| 369 LOG(ERROR) << "No password store! Cannot display exceptions."; | 371 LOG(ERROR) << "No password store! Cannot display exceptions."; |
| 370 } | 372 } |
| 371 } | 373 } |
| 372 | 374 |
| 373 void PasswordManagerPresenter::PasswordExceptionListPopulater:: | 375 void PasswordManagerPresenter::PasswordExceptionListPopulater:: |
| 374 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { | 376 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { |
| 375 page_->password_exception_list_ = | 377 page_->password_exception_list_ = |
| 376 password_manager_util::ConvertScopedVector(std::move(results)); | 378 password_manager_util::ConvertScopedVector(std::move(results)); |
| 377 page_->SortEntriesAndHideDuplicates(&page_->password_exception_list_, | 379 page_->SortEntriesAndHideDuplicates(&page_->password_exception_list_, |
| 378 &page_->password_exception_duplicates_, | 380 &page_->password_exception_duplicates_, |
| 379 false /* don't use username and password*/); | 381 PasswordEntryType::BLACKLISTED); |
| 380 page_->SetPasswordExceptionList(); | 382 page_->SetPasswordExceptionList(); |
| 381 } | 383 } |
| OLD | NEW |