Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / password_autofill_manager.cc
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/password_autofill_manager.h"
8 #include "ui/events/keycodes/keyboard_codes.h"
9
10 namespace autofill {
11
12 ////////////////////////////////////////////////////////////////////////////////
13 // PasswordAutofillManager, public:
14
15 PasswordAutofillManager::PasswordAutofillManager(
16     AutofillDriver* autofill_driver) : autofill_driver_(autofill_driver) {
17   DCHECK(autofill_driver);
18 }
19
20 PasswordAutofillManager::~PasswordAutofillManager() {
21 }
22
23 bool PasswordAutofillManager::DidAcceptAutofillSuggestion(
24     const FormFieldData& field,
25     const base::string16& username) {
26   PasswordFormFillData password;
27   if (!FindLoginInfo(field, &password))
28     return false;
29
30   if (WillFillUserNameAndPassword(username, password)) {
31     autofill_driver_->RendererShouldAcceptPasswordAutofillSuggestion(username);
32     return true;
33   }
34
35   return false;
36 }
37
38 void PasswordAutofillManager::AddPasswordFormMapping(
39     const FormFieldData& username_element,
40     const PasswordFormFillData& password) {
41   login_to_password_info_[username_element] = password;
42 }
43
44 void PasswordAutofillManager::Reset() {
45   login_to_password_info_.clear();
46 }
47
48 ////////////////////////////////////////////////////////////////////////////////
49 // PasswordAutofillManager, private:
50
51 bool PasswordAutofillManager::WillFillUserNameAndPassword(
52     const base::string16& current_username,
53     const PasswordFormFillData& fill_data) {
54   // Look for any suitable matches to current field text.
55   if (fill_data.basic_data.fields[0].value == current_username)
56     return true;
57
58   // Scan additional logins for a match.
59    for (PasswordFormFillData::LoginCollection::const_iterator iter =
60            fill_data.additional_logins.begin();
61        iter != fill_data.additional_logins.end(); ++iter) {
62     if (iter->first == current_username)
63       return true;
64   }
65
66   for (PasswordFormFillData::UsernamesCollection::const_iterator usernames_iter
67            = fill_data.other_possible_usernames.begin();
68        usernames_iter != fill_data.other_possible_usernames.end();
69        ++usernames_iter) {
70     for (size_t i = 0; i < usernames_iter->second.size(); ++i) {
71       if (usernames_iter->second[i] == current_username)
72         return true;
73     }
74   }
75
76   return false;
77 }
78
79 bool PasswordAutofillManager::FindLoginInfo(
80     const FormFieldData& field,
81     PasswordFormFillData* found_password) {
82   LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
83   if (iter == login_to_password_info_.end())
84     return false;
85
86   *found_password = iter->second;
87   return true;
88 }
89
90 }  // namespace autofill