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 substring_matched_suggestion = false; | |
72 if (show_all || base::StartsWith(field_suggestion, field_contents, false) || | |
73 (substring_matched_suggestion = autofill::ContainsTokenThatStartsWith( | |
please use gerrit instead
2015/06/29 22:06:29
If you can figure out a way to write this without
Pritam Nikam
2015/06/30 15:05:51
Done.
Changes to:
bool prefix_matched_suggest
please use gerrit instead
2015/06/30 19:06:22
Very nice.
| |
74 field_suggestion, field_contents, false))) { | |
75 autofill::Suggestion suggestion(ReplaceEmptyUsername(field_suggestion)); | |
76 suggestion.label = GetHumanReadableRealm(signon_realm); | |
77 suggestion.frontend_id = autofill::POPUP_ITEM_ID_PASSWORD_ENTRY; | |
78 suggestion.match = substring_matched_suggestion | |
79 ? autofill::Suggestion::SUBSTRING_MATCH | |
80 : autofill::Suggestion::PREFIX_MATCH; | |
81 suggestions->push_back(suggestion); | |
82 } | |
83 } | |
84 | |
62 // This function attempts to fill |suggestions| and |realms| form |fill_data| | 85 // This function attempts to fill |suggestions| and |realms| form |fill_data| |
63 // based on |current_username|. Unless |show_all| is true, it only picks | 86 // based on |current_username|. Unless |show_all| is true, it only picks |
64 // suggestions where the username has |current_username| as a prefix. | 87 // suggestions where the username matches |current_username|. |
65 void GetSuggestions(const autofill::PasswordFormFillData& fill_data, | 88 void GetSuggestions(const autofill::PasswordFormFillData& fill_data, |
66 const base::string16& current_username, | 89 const base::string16& current_username, |
67 std::vector<autofill::Suggestion>* suggestions, | 90 std::vector<autofill::Suggestion>* suggestions, |
68 bool show_all) { | 91 bool show_all) { |
69 if (show_all || base::StartsWith(fill_data.username_field.value, | 92 AppendSuggestionIfMatching(fill_data.username_field.value, current_username, |
70 current_username, false)) { | 93 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 | 94 |
78 for (const auto& login : fill_data.additional_logins) { | 95 for (const auto& login : fill_data.additional_logins) { |
79 if (show_all || base::StartsWith(login.first, current_username, false)) { | 96 AppendSuggestionIfMatching(login.first, current_username, |
80 autofill::Suggestion suggestion(ReplaceEmptyUsername(login.first)); | 97 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 } | 98 } |
86 | 99 |
87 for (const auto& usernames : fill_data.other_possible_usernames) { | 100 for (const auto& usernames : fill_data.other_possible_usernames) { |
88 for (size_t i = 0; i < usernames.second.size(); ++i) { | 101 for (size_t i = 0; i < usernames.second.size(); ++i) { |
89 if (show_all || | 102 AppendSuggestionIfMatching(usernames.second[i], current_username, |
90 base::StartsWith(usernames.second[i], current_username, false)) { | 103 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 } | 104 } |
98 } | 105 } |
106 | |
107 // Prefix matches should precede other token matches. | |
108 if (autofill::IsFeatureSubstringMatchEnabled()) { | |
109 std::sort(suggestions->begin(), suggestions->end(), | |
110 [](const autofill::Suggestion& a, const autofill::Suggestion& b) { | |
111 return a.match < b.match; | |
112 }); | |
113 } | |
99 } | 114 } |
100 | 115 |
101 } // namespace | 116 } // namespace |
102 | 117 |
103 //////////////////////////////////////////////////////////////////////////////// | 118 //////////////////////////////////////////////////////////////////////////////// |
104 // PasswordAutofillManager, public: | 119 // PasswordAutofillManager, public: |
105 | 120 |
106 PasswordAutofillManager::PasswordAutofillManager( | 121 PasswordAutofillManager::PasswordAutofillManager( |
107 PasswordManagerDriver* password_manager_driver, | 122 PasswordManagerDriver* password_manager_driver, |
108 autofill::AutofillClient* autofill_client) | 123 autofill::AutofillClient* autofill_client) |
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
298 autofill::PasswordFormFillData* found_password) { | 313 autofill::PasswordFormFillData* found_password) { |
299 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(key); | 314 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(key); |
300 if (iter == login_to_password_info_.end()) | 315 if (iter == login_to_password_info_.end()) |
301 return false; | 316 return false; |
302 | 317 |
303 *found_password = iter->second; | 318 *found_password = iter->second; |
304 return true; | 319 return true; |
305 } | 320 } |
306 | 321 |
307 } // namespace password_manager | 322 } // namespace password_manager |
OLD | NEW |