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