blob: 6d22a7fc39f4d7ebea41b960eb2c5aa2634fc6e6 [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
5#include "chrome/browser/autofill/password_autofill_manager.h"
6#include "chrome/common/autofill_messages.h"
7#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
11////////////////////////////////////////////////////////////////////////////////
12// PasswordAutofillManager, public:
13
14PasswordAutofillManager::PasswordAutofillManager(
15 content::WebContents* web_contents) : web_contents_(web_contents) {
16}
17
18PasswordAutofillManager::~PasswordAutofillManager() {
19}
20
21bool PasswordAutofillManager::DidAcceptAutofillSuggestion(
22 const webkit::forms::FormField& field,
23 const string16& value) {
24 webkit::forms::PasswordFormFillData password;
25 if (!FindLoginInfo(field, &password))
26 return false;
27
28 if (WillFillUserNameAndPassword(value, password)) {
29 if (web_contents_) {
30 content::RenderViewHost* render_view_host =
31 web_contents_->GetRenderViewHost();
32 render_view_host->Send(new AutofillMsg_AcceptPasswordAutofillSuggestion(
33 render_view_host->GetRoutingID(),
34 value));
35 }
36 return true;
37 }
38
39 return false;
40}
41
42bool PasswordAutofillManager::DidSelectAutofillSuggestion(
43 const webkit::forms::FormField& field) {
44 webkit::forms::FormField input;
45 webkit::forms::PasswordFormFillData password;
46 return FindLoginInfo(field, &password);
47}
48
49bool PasswordAutofillManager::DidClearAutofillSelection(
50 const webkit::forms::FormField& field) {
51 webkit::forms::FormField input;
52 webkit::forms::PasswordFormFillData password;
53 return FindLoginInfo(field, &password);
54}
55
56void PasswordAutofillManager::AddPasswordFormMapping(
57 const webkit::forms::FormField& username_element,
58 const webkit::forms::PasswordFormFillData& password) {
59 login_to_password_info_[username_element] = password;
60}
61
62void PasswordAutofillManager::Reset() {
63 login_to_password_info_.clear();
64}
65
66////////////////////////////////////////////////////////////////////////////////
67// PasswordAutofillManager, private:
68
69bool PasswordAutofillManager::WillFillUserNameAndPassword(
70 const string16& current_username,
71 const webkit::forms::PasswordFormFillData& fill_data) {
72 // Look for any suitable matches to current field text.
73 if (fill_data.basic_data.fields[0].value == current_username) {
74 return true;
75 } else {
76 // Scan additional logins for a match.
77 webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
78 for (iter = fill_data.additional_logins.begin();
79 iter != fill_data.additional_logins.end(); ++iter) {
80 if (iter->first == current_username)
81 return true;
82 }
83 }
84
85 return false;
86}
87
88bool PasswordAutofillManager::FindLoginInfo(
89 const webkit::forms::FormField& field,
90 webkit::forms::PasswordFormFillData* found_password) {
91 LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
92 if (iter == login_to_password_info_.end())
93 return false;
94
95 *found_password = iter->second;
96 return true;
97}