summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/password_form_data.cc
blob: 3c6f377224c6c944db4203770ad83ce5286de776 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright (c) 2011 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 "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/password_manager/password_form_data.h"

using webkit::forms::PasswordForm;

PasswordForm* CreatePasswordFormFromData(
    const PasswordFormData& form_data) {
  PasswordForm* form = new PasswordForm();
  form->scheme = form_data.scheme;
  form->preferred = form_data.preferred;
  form->ssl_valid = form_data.ssl_valid;
  form->date_created = base::Time::FromDoubleT(form_data.creation_time);
  if (form_data.signon_realm)
    form->signon_realm = std::string(form_data.signon_realm);
  if (form_data.origin)
    form->origin = GURL(form_data.origin);
  if (form_data.action)
    form->action = GURL(form_data.action);
  if (form_data.submit_element)
    form->submit_element = WideToUTF16(form_data.submit_element);
  if (form_data.username_element)
    form->username_element = WideToUTF16(form_data.username_element);
  if (form_data.password_element)
    form->password_element = WideToUTF16(form_data.password_element);
  if (form_data.username_value) {
    form->username_value = WideToUTF16(form_data.username_value);
    if (form_data.password_value)
      form->password_value = WideToUTF16(form_data.password_value);
  } else {
    form->blacklisted_by_user = true;
  }
  return form;
}

bool operator==(const PasswordForm& lhs, const PasswordForm& rhs) {
  return (lhs.scheme == rhs.scheme &&
          lhs.signon_realm == rhs.signon_realm &&
          lhs.origin == rhs.origin &&
          lhs.action == rhs.action &&
          lhs.submit_element == rhs.submit_element &&
          lhs.username_element == rhs.username_element &&
          lhs.password_element == rhs.password_element &&
          lhs.username_value == rhs.username_value &&
          lhs.password_value == rhs.password_value &&
          lhs.blacklisted_by_user == rhs.blacklisted_by_user &&
          lhs.preferred == rhs.preferred &&
          lhs.ssl_valid == rhs.ssl_valid &&
          lhs.date_created == rhs.date_created);
}

std::ostream& operator<<(std::ostream& os, const PasswordForm& form) {
  return os << "scheme: " << form.scheme << std::endl
            << "signon_realm: " << form.signon_realm << std::endl
            << "origin: " << form.origin << std::endl
            << "action: " << form.action << std::endl
            << "submit_element: " << form.submit_element << std::endl
            << "username_elem: " << form.username_element << std::endl
            << "password_elem: " << form.password_element << std::endl
            << "username_value: " << form.username_value << std::endl
            << "password_value: " << form.password_value << std::endl
            << "blacklisted: " << form.blacklisted_by_user << std::endl
            << "preferred: " << form.preferred << std::endl
            << "ssl_valid: " << form.ssl_valid << std::endl
            << "date_created: " << form.date_created.ToDoubleT();
}

typedef std::set<const webkit::forms::PasswordForm*> SetOfForms;

bool ContainsSamePasswordFormsPtr(
    const std::vector<PasswordForm*>& first,
    const std::vector<PasswordForm*>& second) {
  if (first.size() != second.size())
    return false;

  // TODO(cramya): As per b/7079906, the STLport of Android causes a crash
  // if we use expectations(first.begin(), first.end()) to copy a vector
  // into a set rather than std::copy that is used below.
  // Need to revert this once STLport is fixed.
  SetOfForms expectations;
  std::copy(first.begin(), first.end(), std::inserter(expectations,
                                                      expectations.begin()));
  for (unsigned int i = 0; i < second.size(); ++i) {
    const PasswordForm* actual = second[i];
    bool found_match = false;
    for (SetOfForms::iterator it = expectations.begin();
         it != expectations.end(); ++it) {
      const PasswordForm* expected = *it;
      if (*expected == *actual) {
        found_match = true;
        expectations.erase(it);
        break;
      }
    }
    if (!found_match) {
      LOG(ERROR) << "No match for:" << std::endl << *actual;
      return false;
    }
  }
  return true;
}

bool ContainsSamePasswordForms(
    std::vector<webkit::forms::PasswordForm>& first,
    std::vector<webkit::forms::PasswordForm>& second) {
  std::vector<PasswordForm*> first_ptr;
  for (unsigned int i = 0; i < first.size(); ++i) {
    first_ptr.push_back(&first[i]);
  }
  std::vector<PasswordForm*> second_ptr;
  for (unsigned int i = 0; i < second.size(); ++i) {
    second_ptr.push_back(&second[i]);
  }
  return ContainsSamePasswordFormsPtr(first_ptr, second_ptr);
}