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 "components/password_manager/core/browser/password_autofill_manager.h" | 5 #include "components/password_manager/core/browser/password_autofill_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
13 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "components/autofill/core/browser/autofill_driver.h" | 15 #include "components/autofill/core/browser/autofill_driver.h" |
16 #include "components/autofill/core/browser/popup_item_ids.h" | 16 #include "components/autofill/core/browser/popup_item_ids.h" |
17 #include "components/autofill/core/browser/suggestion.h" | 17 #include "components/autofill/core/browser/suggestion.h" |
18 #include "components/autofill/core/common/autofill_constants.h" | 18 #include "components/autofill/core/common/autofill_constants.h" |
19 #include "components/autofill/core/common/autofill_data_validation.h" | 19 #include "components/autofill/core/common/autofill_data_validation.h" |
| 20 #include "components/autofill/core/common/autofill_util.h" |
20 #include "components/password_manager/core/browser/affiliation_utils.h" | 21 #include "components/password_manager/core/browser/affiliation_utils.h" |
21 #include "components/password_manager/core/browser/password_manager_driver.h" | 22 #include "components/password_manager/core/browser/password_manager_driver.h" |
22 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" | 23 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" |
23 #include "components/strings/grit/components_strings.h" | 24 #include "components/strings/grit/components_strings.h" |
24 #include "grit/components_strings.h" | 25 #include "grit/components_strings.h" |
25 #include "ui/base/l10n/l10n_util.h" | 26 #include "ui/base/l10n/l10n_util.h" |
26 | 27 |
27 namespace password_manager { | 28 namespace password_manager { |
28 | 29 |
29 namespace { | 30 namespace { |
(...skipping 23 matching lines...) Expand all Loading... |
53 base::string16 GetHumanReadableRealm(const std::string& signon_realm) { | 54 base::string16 GetHumanReadableRealm(const std::string& signon_realm) { |
54 // For Android application realms, remove the hash component. Otherwise, make | 55 // For Android application realms, remove the hash component. Otherwise, make |
55 // no changes. | 56 // no changes. |
56 FacetURI maybe_facet_uri(FacetURI::FromPotentiallyInvalidSpec(signon_realm)); | 57 FacetURI maybe_facet_uri(FacetURI::FromPotentiallyInvalidSpec(signon_realm)); |
57 if (maybe_facet_uri.IsValidAndroidFacetURI()) | 58 if (maybe_facet_uri.IsValidAndroidFacetURI()) |
58 return base::UTF8ToUTF16("android://" + | 59 return base::UTF8ToUTF16("android://" + |
59 maybe_facet_uri.android_package_name() + "/"); | 60 maybe_facet_uri.android_package_name() + "/"); |
60 return base::UTF8ToUTF16(signon_realm); | 61 return base::UTF8ToUTF16(signon_realm); |
61 } | 62 } |
62 | 63 |
| 64 // If |field_suggestion| matches |field_content|, creates a Suggestion out of it |
| 65 // and appends to |suggestions|. |
| 66 void AppendSuggestionIfMatching( |
| 67 const base::string16& field_suggestion, |
| 68 const base::string16& field_contents, |
| 69 const std::string& signon_realm, |
| 70 bool show_all, |
| 71 std::vector<autofill::Suggestion>* suggestions) { |
| 72 base::string16 lower_suggestion = base::i18n::ToLower(field_suggestion); |
| 73 base::string16 lower_contents = base::i18n::ToLower(field_contents); |
| 74 bool prefix_matched_suggestion = |
| 75 show_all || base::StartsWith(lower_suggestion, lower_contents, |
| 76 base::CompareCase::SENSITIVE); |
| 77 if (prefix_matched_suggestion || |
| 78 autofill::FieldIsSuggestionSubstringStartingOnTokenBoundary( |
| 79 lower_suggestion, lower_contents, true)) { |
| 80 autofill::Suggestion suggestion(ReplaceEmptyUsername(field_suggestion)); |
| 81 suggestion.label = GetHumanReadableRealm(signon_realm); |
| 82 suggestion.frontend_id = autofill::POPUP_ITEM_ID_PASSWORD_ENTRY; |
| 83 suggestion.match = prefix_matched_suggestion |
| 84 ? autofill::Suggestion::PREFIX_MATCH |
| 85 : autofill::Suggestion::SUBSTRING_MATCH; |
| 86 suggestions->push_back(suggestion); |
| 87 } |
| 88 } |
| 89 |
63 // This function attempts to fill |suggestions| and |realms| form |fill_data| | 90 // This function attempts to fill |suggestions| and |realms| form |fill_data| |
64 // based on |current_username|. Unless |show_all| is true, it only picks | 91 // based on |current_username|. Unless |show_all| is true, it only picks |
65 // suggestions where the username has |current_username| as a prefix. | 92 // suggestions where the username has |current_username| as a prefix. |
66 void GetSuggestions(const autofill::PasswordFormFillData& fill_data, | 93 void GetSuggestions(const autofill::PasswordFormFillData& fill_data, |
67 const base::string16& current_username, | 94 const base::string16& current_username, |
68 std::vector<autofill::Suggestion>* suggestions, | 95 std::vector<autofill::Suggestion>* suggestions, |
69 bool show_all) { | 96 bool show_all) { |
70 base::string16 lower_username = base::i18n::ToLower(current_username); | 97 AppendSuggestionIfMatching(fill_data.username_field.value, current_username, |
71 | 98 fill_data.preferred_realm, show_all, suggestions); |
72 if (show_all || | |
73 base::StartsWith(base::i18n::ToLower(fill_data.username_field.value), | |
74 lower_username, base::CompareCase::SENSITIVE)) { | |
75 autofill::Suggestion suggestion( | |
76 ReplaceEmptyUsername(fill_data.username_field.value)); | |
77 suggestion.label = GetHumanReadableRealm(fill_data.preferred_realm); | |
78 suggestion.frontend_id = autofill::POPUP_ITEM_ID_PASSWORD_ENTRY; | |
79 suggestions->push_back(suggestion); | |
80 } | |
81 | 99 |
82 for (const auto& login : fill_data.additional_logins) { | 100 for (const auto& login : fill_data.additional_logins) { |
83 if (show_all || | 101 AppendSuggestionIfMatching(login.first, current_username, |
84 base::StartsWith(base::i18n::ToLower(login.first), lower_username, | 102 login.second.realm, show_all, suggestions); |
85 base::CompareCase::SENSITIVE)) { | |
86 autofill::Suggestion suggestion(ReplaceEmptyUsername(login.first)); | |
87 suggestion.label = GetHumanReadableRealm(login.second.realm); | |
88 suggestion.frontend_id = autofill::POPUP_ITEM_ID_PASSWORD_ENTRY; | |
89 suggestions->push_back(suggestion); | |
90 } | |
91 } | 103 } |
92 | 104 |
93 for (const auto& usernames : fill_data.other_possible_usernames) { | 105 for (const auto& usernames : fill_data.other_possible_usernames) { |
94 for (size_t i = 0; i < usernames.second.size(); ++i) { | 106 for (size_t i = 0; i < usernames.second.size(); ++i) { |
95 if (show_all || | 107 AppendSuggestionIfMatching(usernames.second[i], current_username, |
96 base::StartsWith(base::i18n::ToLower(usernames.second[i]), | 108 usernames.first.realm, show_all, suggestions); |
97 lower_username, base::CompareCase::SENSITIVE)) { | |
98 autofill::Suggestion suggestion( | |
99 ReplaceEmptyUsername(usernames.second[i])); | |
100 suggestion.label = GetHumanReadableRealm(usernames.first.realm); | |
101 suggestion.frontend_id = autofill::POPUP_ITEM_ID_PASSWORD_ENTRY; | |
102 suggestions->push_back(suggestion); | |
103 } | |
104 } | 109 } |
105 } | 110 } |
| 111 |
| 112 // Prefix matches should precede other token matches. |
| 113 if (autofill::IsFeatureSubstringMatchEnabled()) { |
| 114 std::sort(suggestions->begin(), suggestions->end(), |
| 115 [](const autofill::Suggestion& a, const autofill::Suggestion& b) { |
| 116 return a.match < b.match; |
| 117 }); |
| 118 } |
106 } | 119 } |
107 | 120 |
108 } // namespace | 121 } // namespace |
109 | 122 |
110 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
111 // PasswordAutofillManager, public: | 124 // PasswordAutofillManager, public: |
112 | 125 |
113 PasswordAutofillManager::PasswordAutofillManager( | 126 PasswordAutofillManager::PasswordAutofillManager( |
114 PasswordManagerDriver* password_manager_driver, | 127 PasswordManagerDriver* password_manager_driver, |
115 autofill::AutofillClient* autofill_client) | 128 autofill::AutofillClient* autofill_client) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 autofill::PasswordFormFillData* found_password) { | 318 autofill::PasswordFormFillData* found_password) { |
306 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(key); | 319 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(key); |
307 if (iter == login_to_password_info_.end()) | 320 if (iter == login_to_password_info_.end()) |
308 return false; | 321 return false; |
309 | 322 |
310 *found_password = iter->second; | 323 *found_password = iter->second; |
311 return true; | 324 return true; |
312 } | 325 } |
313 | 326 |
314 } // namespace password_manager | 327 } // namespace password_manager |
OLD | NEW |