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