password_form_fill_data.cc revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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  return password < other.password;
33}
34
35PasswordFormFillData::PasswordFormFillData() : wait_for_username(false) {
36}
37
38PasswordFormFillData::~PasswordFormFillData() {
39}
40
41void InitPasswordFormFillData(
42    const content::PasswordForm& form_on_page,
43    const content::PasswordFormMap& matches,
44    const content::PasswordForm* const preferred_match,
45    bool wait_for_username_before_autofill,
46    bool enable_other_possible_usernames,
47    PasswordFormFillData* result) {
48  // Note that many of the |FormFieldData| members are not initialized for
49  // |username_field| and |password_field| because they are currently not used
50  // by the password autocomplete code.
51  FormFieldData username_field;
52  username_field.name = form_on_page.username_element;
53  username_field.value = preferred_match->username_value;
54  FormFieldData password_field;
55  password_field.name = form_on_page.password_element;
56  password_field.value = preferred_match->password_value;
57  password_field.form_control_type = "password";
58
59  // Fill basic form data.
60  result->basic_data.origin = form_on_page.origin;
61  result->basic_data.action = form_on_page.action;
62  result->basic_data.fields.push_back(username_field);
63  result->basic_data.fields.push_back(password_field);
64  result->wait_for_username = wait_for_username_before_autofill;
65
66  result->preferred_realm = GetPreferredSignonRealm(*preferred_match);
67
68  // Copy additional username/value pairs.
69  content::PasswordFormMap::const_iterator iter;
70  for (iter = matches.begin(); iter != matches.end(); iter++) {
71    if (iter->second != preferred_match) {
72      PasswordAndRealm value;
73      value.password = iter->second->password_value;
74      value.realm = GetPreferredSignonRealm(*iter->second);
75      result->additional_logins[iter->first] = value;
76    }
77    if (enable_other_possible_usernames &&
78        !iter->second->other_possible_usernames.empty()) {
79      // Note that there may be overlap between other_possible_usernames and
80      // other saved usernames or with other other_possible_usernames. For now
81      // we will ignore this overlap as it should be a rare occurence. We may
82      // want to revisit this in the future.
83      UsernamesCollectionKey key;
84      key.username = iter->first;
85      key.password = iter->second->password_value;
86      result->other_possible_usernames[key] =
87          iter->second->other_possible_usernames;
88    }
89  }
90}
91
92}  // namespace autofill
93