OLD | NEW |
(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 namespace password_manager { |
| 14 |
| 15 //////////////////////////////////////////////////////////////////////////////// |
| 16 // PasswordAutofillManager, public: |
| 17 |
| 18 PasswordAutofillManager::PasswordAutofillManager( |
| 19 password_manager::PasswordManagerClient* password_manager_client, |
| 20 autofill::AutofillManagerDelegate* autofill_manager_delegate) |
| 21 : password_manager_client_(password_manager_client), |
| 22 autofill_manager_delegate_(autofill_manager_delegate), |
| 23 weak_ptr_factory_(this) { |
| 24 } |
| 25 |
| 26 PasswordAutofillManager::~PasswordAutofillManager() { |
| 27 } |
| 28 |
| 29 bool PasswordAutofillManager::AcceptSuggestion( |
| 30 const autofill::FormFieldData& field, |
| 31 const base::string16& username) { |
| 32 autofill::PasswordFormFillData fill_data; |
| 33 base::string16 password; |
| 34 if (FindLoginInfo(field, &fill_data) && |
| 35 GetPasswordForUsername(username, fill_data, &password)) { |
| 36 PasswordManagerDriver* driver = password_manager_client_->GetDriver(); |
| 37 driver->AcceptPasswordAutofillSuggestion(username, password); |
| 38 return true; |
| 39 } |
| 40 return false; |
| 41 } |
| 42 |
| 43 void PasswordAutofillManager::OnAddPasswordFormMapping( |
| 44 const autofill::FormFieldData& field, |
| 45 const autofill::PasswordFormFillData& fill_data) { |
| 46 if (!autofill::IsValidFormFieldData(field) || |
| 47 !autofill::IsValidPasswordFormFillData(fill_data)) |
| 48 return; |
| 49 |
| 50 login_to_password_info_[field] = fill_data; |
| 51 } |
| 52 |
| 53 void PasswordAutofillManager::OnShowPasswordSuggestions( |
| 54 const autofill::FormFieldData& field, |
| 55 const gfx::RectF& bounds, |
| 56 const std::vector<base::string16>& suggestions, |
| 57 const std::vector<base::string16>& realms) { |
| 58 if (!autofill::IsValidString16Vector(suggestions) || |
| 59 !autofill::IsValidString16Vector(realms) || |
| 60 suggestions.size() != realms.size()) |
| 61 return; |
| 62 |
| 63 form_field_ = field; |
| 64 |
| 65 if (suggestions.empty()) { |
| 66 autofill_manager_delegate_->HideAutofillPopup(); |
| 67 return; |
| 68 } |
| 69 |
| 70 std::vector<base::string16> empty(suggestions.size()); |
| 71 std::vector<int> password_ids(suggestions.size(), |
| 72 autofill::POPUP_ITEM_ID_PASSWORD_ENTRY); |
| 73 autofill_manager_delegate_->ShowAutofillPopup( |
| 74 bounds, |
| 75 field.text_direction, |
| 76 suggestions, |
| 77 realms, |
| 78 empty, |
| 79 password_ids, |
| 80 weak_ptr_factory_.GetWeakPtr()); |
| 81 } |
| 82 |
| 83 void PasswordAutofillManager::Reset() { |
| 84 login_to_password_info_.clear(); |
| 85 } |
| 86 |
| 87 bool PasswordAutofillManager::AcceptSuggestionForTest( |
| 88 const autofill::FormFieldData& field, |
| 89 const base::string16& username) { |
| 90 return AcceptSuggestion(field, username); |
| 91 } |
| 92 |
| 93 void PasswordAutofillManager::OnPopupShown() { |
| 94 } |
| 95 |
| 96 void PasswordAutofillManager::OnPopupHidden() { |
| 97 } |
| 98 |
| 99 void PasswordAutofillManager::DidSelectSuggestion(const base::string16& value, |
| 100 int identifier) { |
| 101 // This is called to preview an autofill suggestion, but we don't currently |
| 102 // do that for password forms (crbug.com/63421). If it is ever implemented, |
| 103 // ClearPreviewedForm() must also be implemented(). |
| 104 } |
| 105 |
| 106 void PasswordAutofillManager::DidAcceptSuggestion(const base::string16& value, |
| 107 int identifier) { |
| 108 if (!AcceptSuggestion(form_field_, value)) |
| 109 NOTREACHED(); |
| 110 autofill_manager_delegate_->HideAutofillPopup(); |
| 111 } |
| 112 |
| 113 void PasswordAutofillManager::RemoveSuggestion(const base::string16& value, |
| 114 int identifier) { |
| 115 NOTREACHED(); |
| 116 } |
| 117 |
| 118 void PasswordAutofillManager::ClearPreviewedForm() { |
| 119 // There is currently no preview for password autofill (crbug.com/63421). |
| 120 // This function needs an implemention if preview is ever implemented. |
| 121 } |
| 122 |
| 123 //////////////////////////////////////////////////////////////////////////////// |
| 124 // PasswordAutofillManager, private: |
| 125 |
| 126 bool PasswordAutofillManager::GetPasswordForUsername( |
| 127 const base::string16& current_username, |
| 128 const autofill::PasswordFormFillData& fill_data, |
| 129 base::string16* password) { |
| 130 // TODO(dubroy): When password access requires some kind of authentication |
| 131 // (e.g. Keychain access on Mac OS), use |password_manager_client_| here to |
| 132 // fetch the actual password. See crbug.com/178358 for more context. |
| 133 |
| 134 // Look for any suitable matches to current field text. |
| 135 if (fill_data.basic_data.fields[0].value == current_username) { |
| 136 *password = fill_data.basic_data.fields[1].value; |
| 137 return true; |
| 138 } |
| 139 |
| 140 // Scan additional logins for a match. |
| 141 for (autofill::PasswordFormFillData::LoginCollection::const_iterator iter = |
| 142 fill_data.additional_logins.begin(); |
| 143 iter != fill_data.additional_logins.end(); |
| 144 ++iter) { |
| 145 if (iter->first == current_username) { |
| 146 *password = iter->second.password; |
| 147 return true; |
| 148 } |
| 149 } |
| 150 |
| 151 for (autofill::PasswordFormFillData::UsernamesCollection::const_iterator |
| 152 usernames_iter = fill_data.other_possible_usernames.begin(); |
| 153 usernames_iter != fill_data.other_possible_usernames.end(); |
| 154 ++usernames_iter) { |
| 155 for (size_t i = 0; i < usernames_iter->second.size(); ++i) { |
| 156 if (usernames_iter->second[i] == current_username) { |
| 157 *password = usernames_iter->first.password; |
| 158 return true; |
| 159 } |
| 160 } |
| 161 } |
| 162 |
| 163 return false; |
| 164 } |
| 165 |
| 166 bool PasswordAutofillManager::FindLoginInfo( |
| 167 const autofill::FormFieldData& field, |
| 168 autofill::PasswordFormFillData* found_password) { |
| 169 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field); |
| 170 if (iter == login_to_password_info_.end()) |
| 171 return false; |
| 172 |
| 173 *found_password = iter->second; |
| 174 return true; |
| 175 } |
| 176 |
| 177 } // namespace password_manager |
OLD | NEW |