password_form_fill_data.cc revision 558790d6acca3451cf3a6b497803a5f07d0bec58
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 "components/autofill/core/common/password_form_fill_data.h"
6
7#include "base/logging.h"
8#include "base/strings/utf_string_conversions.h"
9#include "components/autofill/core/common/form_field_data.h"
10
11namespace {
12
13std::string GetPreferredSignonRealm(const content::PasswordForm& form) {
14  if (form.IsPublicSuffixMatch())
15    return form.original_signon_realm;
16  else
17    return form.signon_realm;
18}
19
20}  // namespace
21
22namespace autofill {
23
24UsernamesCollectionKey::UsernamesCollectionKey() {}
25
26UsernamesCollectionKey::~UsernamesCollectionKey() {}
27
28bool UsernamesCollectionKey::operator<(
29    const UsernamesCollectionKey& other) const {
30  if (username != other.username)
31    return username < other.username;
32  if (password != other.password)
33    return password < other.password;
34  return realm < other.realm;
35}
36
37PasswordFormFillData::PasswordFormFillData() : wait_for_username(false) {
38}
39
40PasswordFormFillData::~PasswordFormFillData() {
41}
42
43void InitPasswordFormFillData(
44    const content::PasswordForm& form_on_page,
45    const content::PasswordFormMap& matches,
46    const content::PasswordForm* const preferred_match,
47    bool wait_for_username_before_autofill,
48    bool enable_other_possible_usernames,
49    PasswordFormFillData* result) {
50  // Note that many of the |FormFieldData| members are not initialized for
51  // |username_field| and |password_field| because they are currently not used
52  // by the password autocomplete code.
53  FormFieldData username_field;
54  username_field.name = form_on_page.username_element;
55  username_field.value = preferred_match->username_value;
56  FormFieldData password_field;
57  password_field.name = form_on_page.password_element;
58  password_field.value = preferred_match->password_value;
59  password_field.form_control_type = "password";
60
61  // Fill basic form data.
62  result->basic_data.origin = form_on_page.origin;
63  result->basic_data.action = form_on_page.action;
64  result->basic_data.fields.push_back(username_field);
65  result->basic_data.fields.push_back(password_field);
66  result->wait_for_username = wait_for_username_before_autofill;
67
68  result->preferred_realm = GetPreferredSignonRealm(*preferred_match);
69
70  // Copy additional username/value pairs.
71  content::PasswordFormMap::const_iterator iter;
72  for (iter = matches.begin(); iter != matches.end(); iter++) {
73    if (iter->second != preferred_match) {
74      PasswordAndRealm value;
75      value.password = iter->second->password_value;
76      value.realm = GetPreferredSignonRealm(*iter->second);
77      result->additional_logins[iter->first] = value;
78    }
79    if (enable_other_possible_usernames &&
80        !iter->second->other_possible_usernames.empty()) {
81      // Note that there may be overlap between other_possible_usernames and
82      // other saved usernames or with other other_possible_usernames. For now
83      // we will ignore this overlap as it should be a rare occurence. We may
84      // want to revisit this in the future.
85      UsernamesCollectionKey key;
86      key.username = iter->first;
87      key.password = iter->second->password_value;
88      key.realm = GetPreferredSignonRealm(*iter->second);
89      result->other_possible_usernames[key] =
90          iter->second->other_possible_usernames;
91    }
92  }
93}
94
95}  // namespace autofill
96