Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(578)

Side by Side Diff: components/password_manager/core/browser/password_autofill_manager.cc

Issue 184103016: Autofill: Refactoring to support fetching password after a username is selected (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address gcasto's comments. Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/logging.h"
6 #include "components/autofill/core/browser/autofill_driver.h"
7 #include "components/autofill/core/browser/popup_item_ids.h"
8 #include "components/autofill/core/common/autofill_data_validation.h"
9 #include "components/password_manager/core/browser/password_autofill_manager.h"
10 #include "components/password_manager/core/browser/password_manager_client.h"
11 #include "components/password_manager/core/browser/password_manager_driver.h"
12
13 ////////////////////////////////////////////////////////////////////////////////
14 // PasswordAutofillManager, public:
15
16 PasswordAutofillManager::PasswordAutofillManager(
17 PasswordManagerClient* password_manager_client,
18 autofill::AutofillManagerDelegate* autofill_manager_delegate)
19 : password_manager_client_(password_manager_client),
20 autofill_manager_delegate_(autofill_manager_delegate),
21 weak_ptr_factory_(this) {
22 }
23
24 PasswordAutofillManager::~PasswordAutofillManager() {
25 }
26
27 bool PasswordAutofillManager::AcceptAutofillSuggestion(
28 const autofill::FormFieldData& field,
29 const base::string16& username) {
30 autofill::PasswordFormFillData fill_data;
31 base::string16 password;
32 if (FindLoginInfo(field, &fill_data) &&
33 GetPasswordForUsername(username, fill_data, &password)) {
34 PasswordManagerDriver* driver = password_manager_client_->GetDriver();
35 driver->AcceptPasswordAutofillSuggestion(username, password);
36 return true;
37 }
38 return false;
39 }
40
41 void PasswordAutofillManager::OnAddPasswordFormMapping(
42 const autofill::FormFieldData& field,
43 const autofill::PasswordFormFillData& fill_data) {
44 if (!autofill::IsValidFormFieldData(field) ||
45 !autofill::IsValidPasswordFormFillData(fill_data))
46 return;
47
48 login_to_password_info_[field] = fill_data;
49 }
50
51 void PasswordAutofillManager::OnShowPasswordSuggestions(
52 const autofill::FormFieldData& field,
53 const gfx::RectF& bounds,
54 const std::vector<base::string16>& suggestions,
55 const std::vector<base::string16>& realms) {
56 if (!autofill::IsValidString16Vector(suggestions) ||
57 !autofill::IsValidString16Vector(realms) ||
58 suggestions.size() != realms.size())
59 return;
60
61 form_field_ = field;
62
63 if (suggestions.empty()) {
64 autofill_manager_delegate_->HideAutofillPopup();
65 return;
66 }
67
68 std::vector<base::string16> empty(suggestions.size());
69 std::vector<int> password_ids(suggestions.size(),
70 autofill::POPUP_ITEM_ID_PASSWORD_ENTRY);
71 autofill_manager_delegate_->ShowAutofillPopup(
72 bounds,
73 field.text_direction,
74 suggestions,
75 realms,
76 empty,
77 password_ids,
78 weak_ptr_factory_.GetWeakPtr());
79 }
80
81 void PasswordAutofillManager::Reset() {
82 login_to_password_info_.clear();
83 }
84
85 void PasswordAutofillManager::OnPopupShown() {
86 }
87
88 void PasswordAutofillManager::OnPopupHidden() {
89 }
90
91 bool PasswordAutofillManager::ShouldRepostEvent(const ui::MouseEvent& event) {
92 NOTREACHED();
93 return true;
94 }
95
96 void PasswordAutofillManager::DidSelectSuggestion(int identifier) {
97 // This is called to preview an autofill suggestion, but we don't currently
98 // do that for password forms (crbug.com/63421). If it is ever implemented,
99 // ClearPreviewedForm() must also be implemented().
100 }
101
102 void PasswordAutofillManager::DidAcceptSuggestion(const base::string16& value,
103 int identifier) {
104 if (!AcceptAutofillSuggestion(form_field_, value))
105 NOTREACHED();
106 autofill_manager_delegate_->HideAutofillPopup();
107 }
108
109 void PasswordAutofillManager::RemoveSuggestion(const base::string16& value,
110 int identifier) {
111 NOTREACHED();
112 }
113
114 void PasswordAutofillManager::ClearPreviewedForm() {
115 // There is currently no preview for password autofill (crbug.com/63421).
116 // This function needs an implemention if preview is ever implemented.
117 NOTREACHED();
118 }
119
120 ////////////////////////////////////////////////////////////////////////////////
121 // PasswordAutofillManager, private:
122
123 bool PasswordAutofillManager::GetPasswordForUsername(
124 const base::string16& current_username,
125 const autofill::PasswordFormFillData& fill_data,
126 base::string16* password) {
127 // TODO(dubroy): When password access requires some kind of authentication
128 // (e.g. Keychain access on Mac OS), use |password_manager_client_| here to
129 // fetch the actual password. See crbug.com/178358 for more context.
130
131 // Look for any suitable matches to current field text.
132 if (fill_data.basic_data.fields[0].value == current_username) {
133 *password = fill_data.basic_data.fields[1].value;
134 return true;
135 }
136
137 // Scan additional logins for a match.
138 for (autofill::PasswordFormFillData::LoginCollection::const_iterator iter =
139 fill_data.additional_logins.begin();
140 iter != fill_data.additional_logins.end(); ++iter) {
141 if (iter->first == current_username) {
142 *password = iter->second.password;
143 return true;
144 }
145 }
146
147 for (autofill::PasswordFormFillData::UsernamesCollection::const_iterator
148 usernames_iter = fill_data.other_possible_usernames.begin();
149 usernames_iter != fill_data.other_possible_usernames.end();
150 ++usernames_iter) {
151 for (size_t i = 0; i < usernames_iter->second.size(); ++i) {
152 if (usernames_iter->second[i] == current_username) {
153 *password = usernames_iter->first.password;
154 return true;
155 }
156 }
157 }
158
159 return false;
160 }
161
162 bool PasswordAutofillManager::FindLoginInfo(
163 const autofill::FormFieldData& field,
164 autofill::PasswordFormFillData* found_password) {
165 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
166 if (iter == login_to_password_info_.end())
167 return false;
168
169 *found_password = iter->second;
170 return true;
171 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698