1// Copyright 2014 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/strings/string_util.h"
6#include "base/strings/utf_string_conversions.h"
7#include "components/password_manager/core/browser/password_form_data.h"
8
9using autofill::PasswordForm;
10
11namespace password_manager {
12
13PasswordForm* CreatePasswordFormFromData(
14    const PasswordFormData& form_data) {
15  PasswordForm* form = new PasswordForm();
16  form->scheme = form_data.scheme;
17  form->preferred = form_data.preferred;
18  form->ssl_valid = form_data.ssl_valid;
19  form->date_created = base::Time::FromDoubleT(form_data.creation_time);
20  if (form_data.signon_realm)
21    form->signon_realm = std::string(form_data.signon_realm);
22  if (form_data.origin)
23    form->origin = GURL(form_data.origin);
24  if (form_data.action)
25    form->action = GURL(form_data.action);
26  if (form_data.submit_element)
27    form->submit_element = base::WideToUTF16(form_data.submit_element);
28  if (form_data.username_element)
29    form->username_element = base::WideToUTF16(form_data.username_element);
30  if (form_data.password_element)
31    form->password_element = base::WideToUTF16(form_data.password_element);
32  if (form_data.username_value) {
33    form->username_value = base::WideToUTF16(form_data.username_value);
34    if (form_data.password_value)
35      form->password_value = base::WideToUTF16(form_data.password_value);
36  } else {
37    form->blacklisted_by_user = true;
38  }
39  return form;
40}
41
42typedef std::set<const autofill::PasswordForm*> SetOfForms;
43
44bool ContainsSamePasswordFormsPtr(
45    const std::vector<PasswordForm*>& first,
46    const std::vector<PasswordForm*>& second) {
47  if (first.size() != second.size())
48    return false;
49
50  // TODO(cramya): As per b/7079906, the STLport of Android causes a crash
51  // if we use expectations(first.begin(), first.end()) to copy a vector
52  // into a set rather than std::copy that is used below.
53  // Need to revert this once STLport is fixed.
54  SetOfForms expectations;
55  std::copy(first.begin(), first.end(), std::inserter(expectations,
56                                                      expectations.begin()));
57  for (unsigned int i = 0; i < second.size(); ++i) {
58    const PasswordForm* actual = second[i];
59    bool found_match = false;
60    for (SetOfForms::iterator it = expectations.begin();
61         it != expectations.end(); ++it) {
62      const PasswordForm* expected = *it;
63      if (*expected == *actual) {
64        found_match = true;
65        expectations.erase(it);
66        break;
67      }
68    }
69    if (!found_match) {
70      LOG(ERROR) << "No match for:" << std::endl << *actual;
71      return false;
72    }
73  }
74  return true;
75}
76
77bool ContainsSamePasswordForms(
78    std::vector<autofill::PasswordForm>& first,
79    std::vector<autofill::PasswordForm>& second) {
80  std::vector<PasswordForm*> first_ptr;
81  for (unsigned int i = 0; i < first.size(); ++i) {
82    first_ptr.push_back(&first[i]);
83  }
84  std::vector<PasswordForm*> second_ptr;
85  for (unsigned int i = 0; i < second.size(); ++i) {
86    second_ptr.push_back(&second[i]);
87  }
88  return ContainsSamePasswordFormsPtr(first_ptr, second_ptr);
89}
90
91}  // namespace password_manager
92