summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/password_autofill_manager.cc
blob: d05ec84c3283f6d66462b63757e415220ff4ea45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/autofill/password_autofill_manager.h"
#include "chrome/common/autofill_messages.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/keycodes/keyboard_codes.h"

////////////////////////////////////////////////////////////////////////////////
// PasswordAutofillManager, public:

PasswordAutofillManager::PasswordAutofillManager(
    content::WebContents* web_contents) : web_contents_(web_contents) {
}

PasswordAutofillManager::~PasswordAutofillManager() {
}

bool PasswordAutofillManager::DidAcceptAutofillSuggestion(
    const webkit::forms::FormField& field,
    const string16& value) {
  webkit::forms::PasswordFormFillData password;
  if (!FindLoginInfo(field, &password))
    return false;

  if (WillFillUserNameAndPassword(value, password)) {
    if (web_contents_) {
      content::RenderViewHost* render_view_host =
          web_contents_->GetRenderViewHost();
      render_view_host->Send(new AutofillMsg_AcceptPasswordAutofillSuggestion(
          render_view_host->GetRoutingID(),
          value));
    }
    return true;
  }

  return false;
}

void PasswordAutofillManager::AddPasswordFormMapping(
    const webkit::forms::FormField& username_element,
    const webkit::forms::PasswordFormFillData& password) {
  login_to_password_info_[username_element] = password;
}

void PasswordAutofillManager::Reset() {
  login_to_password_info_.clear();
}

////////////////////////////////////////////////////////////////////////////////
// PasswordAutofillManager, private:

bool PasswordAutofillManager::WillFillUserNameAndPassword(
    const string16& current_username,
    const webkit::forms::PasswordFormFillData& fill_data) {
  // Look for any suitable matches to current field text.
  if (fill_data.basic_data.fields[0].value == current_username) {
    return true;
  } else {
    // Scan additional logins for a match.
    webkit::forms::PasswordFormFillData::LoginCollection::const_iterator iter;
    for (iter = fill_data.additional_logins.begin();
         iter != fill_data.additional_logins.end(); ++iter) {
      if (iter->first == current_username)
        return true;
    }
  }

  return false;
}

bool PasswordAutofillManager::FindLoginInfo(
    const webkit::forms::FormField& field,
    webkit::forms::PasswordFormFillData* found_password) {
  LoginToPasswordInfoMap::iterator iter = login_to_password_info_.find(field);
  if (iter == login_to_password_info_.end())
    return false;

  *found_password = iter->second;
  return true;
}