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