blob: bdd097172835e3c4ce553ec237518d8501e5261c [file] [log] [blame]
[email protected]e7e83472012-04-05 02:56:261// Copyright (c) 2012 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
[email protected]e4b2fa32013-03-09 22:56:565#include "components/autofill/browser/password_autofill_manager.h"
[email protected]edf48d42013-03-07 05:44:436#include "components/autofill/common/autofill_messages.h"
[email protected]e7e83472012-04-05 02:56:267#include "content/public/browser/render_view_host.h"
8#include "content/public/browser/web_contents.h"
9#include "ui/base/keycodes/keyboard_codes.h"
10
[email protected]e217c5632013-04-12 19:11:4811namespace autofill {
12
[email protected]e7e83472012-04-05 02:56:2613////////////////////////////////////////////////////////////////////////////////
14// PasswordAutofillManager, public:
15
16PasswordAutofillManager::PasswordAutofillManager(
17 content::WebContents* web_contents) : web_contents_(web_contents) {
18}
19
20PasswordAutofillManager::~PasswordAutofillManager() {
21}
22
23bool PasswordAutofillManager::DidAcceptAutofillSuggestion(
[email protected]1ecbe862012-10-05 01:29:1424 const FormFieldData& field,
[email protected]d5ca8fb2013-04-11 17:54:3125 const base::string16& value) {
[email protected]1ecbe862012-10-05 01:29:1426 PasswordFormFillData password;
[email protected]e7e83472012-04-05 02:56:2627 if (!FindLoginInfo(field, &password))
28 return false;
29
30 if (WillFillUserNameAndPassword(value, password)) {
31 if (web_contents_) {
32 content::RenderViewHost* render_view_host =
33 web_contents_->GetRenderViewHost();
34 render_view_host->Send(new AutofillMsg_AcceptPasswordAutofillSuggestion(
35 render_view_host->GetRoutingID(),
36 value));
37 }
38 return true;
39 }
40
41 return false;
42}
43
[email protected]e7e83472012-04-05 02:56:2644void PasswordAutofillManager::AddPasswordFormMapping(
[email protected]1ecbe862012-10-05 01:29:1445 const FormFieldData& username_element,
46 const PasswordFormFillData& password) {
[email protected]e7e83472012-04-05 02:56:2647 login_to_password_info_[username_element] = password;
48}
49
50void PasswordAutofillManager::Reset() {
51 login_to_password_info_.clear();
52}
53
54////////////////////////////////////////////////////////////////////////////////
55// PasswordAutofillManager, private:
56
57bool PasswordAutofillManager::WillFillUserNameAndPassword(
[email protected]d5ca8fb2013-04-11 17:54:3158 const base::string16& current_username,
[email protected]1ecbe862012-10-05 01:29:1459 const PasswordFormFillData& fill_data) {
[email protected]e7e83472012-04-05 02:56:2660 // Look for any suitable matches to current field text.
61 if (fill_data.basic_data.fields[0].value == current_username) {
62 return true;
63 } else {
64 // Scan additional logins for a match.
[email protected]1ecbe862012-10-05 01:29:1465 PasswordFormFillData::LoginCollection::const_iterator iter;
[email protected]e7e83472012-04-05 02:56:2666 for (iter = fill_data.additional_logins.begin();
67 iter != fill_data.additional_logins.end(); ++iter) {
68 if (iter->first == current_username)
69 return true;
70 }
71 }
72
73 return false;
74}
75
76bool PasswordAutofillManager::FindLoginInfo(
[email protected]1ecbe862012-10-05 01:29:1477 const FormFieldData& field,
78 PasswordFormFillData* found_password) {
[email protected]e7e83472012-04-05 02:56:2679 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
80 if (iter == login_to_password_info_.end())
81 return false;
82
83 *found_password = iter->second;
84 return true;
85}
[email protected]e217c5632013-04-12 19:11:4886
87} // namespace autofill