Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / test_password_store.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 "components/password_manager/core/browser/test_password_store.h"
6
7 #include "components/autofill/core/common/password_form.h"
8
9 namespace password_manager {
10
11 TestPasswordStore::TestPasswordStore()
12     : PasswordStore(base::MessageLoopProxy::current(),
13                     base::MessageLoopProxy::current()) {
14 }
15
16 TestPasswordStore::~TestPasswordStore() {}
17
18 const TestPasswordStore::PasswordMap& TestPasswordStore::stored_passwords()
19     const {
20   return stored_passwords_;
21 }
22
23 void TestPasswordStore::Clear() {
24   stored_passwords_.clear();
25 }
26
27 bool TestPasswordStore::IsEmpty() const {
28   // The store is empty, if the sum of all stored passwords across all entries
29   // in |stored_passwords_| is 0.
30   size_t number_of_passwords = 0u;
31   for (PasswordMap::const_iterator it = stored_passwords_.begin();
32        !number_of_passwords && it != stored_passwords_.end();
33        ++it) {
34     number_of_passwords += it->second.size();
35   }
36   return number_of_passwords == 0u;
37 }
38
39 bool TestPasswordStore::FormsAreEquivalent(const autofill::PasswordForm& lhs,
40                                            const autofill::PasswordForm& rhs) {
41   return lhs.origin == rhs.origin &&
42       lhs.username_element == rhs.username_element &&
43       lhs.username_value == rhs.username_value &&
44       lhs.password_element == rhs.password_element &&
45       lhs.signon_realm == rhs.signon_realm;
46 }
47
48 void TestPasswordStore::WrapModificationTask(ModificationTask task) {
49   task.Run();
50 }
51
52 PasswordStoreChangeList TestPasswordStore::AddLoginImpl(
53     const autofill::PasswordForm& form) {
54   PasswordStoreChangeList changes;
55   stored_passwords_[form.signon_realm].push_back(form);
56   changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
57   return changes;
58 }
59
60 PasswordStoreChangeList TestPasswordStore::UpdateLoginImpl(
61     const autofill::PasswordForm& form) {
62   PasswordStoreChangeList changes;
63   std::vector<autofill::PasswordForm>& forms =
64       stored_passwords_[form.signon_realm];
65   for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
66          it != forms.end(); ++it) {
67     if (FormsAreEquivalent(form, *it)) {
68       *it = form;
69       changes.push_back(
70           PasswordStoreChange(PasswordStoreChange::UPDATE, form));
71     }
72   }
73   return changes;
74 }
75
76 PasswordStoreChangeList TestPasswordStore::RemoveLoginImpl(
77     const autofill::PasswordForm& form) {
78   PasswordStoreChangeList changes;
79   std::vector<autofill::PasswordForm>& forms =
80       stored_passwords_[form.signon_realm];
81   std::vector<autofill::PasswordForm>::iterator it = forms.begin();
82   while (it != forms.end()) {
83     if (FormsAreEquivalent(form, *it)) {
84       it = forms.erase(it);
85       changes.push_back(
86           PasswordStoreChange(PasswordStoreChange::REMOVE, form));
87     } else {
88       ++it;
89     }
90   }
91   return changes;
92 }
93
94 void TestPasswordStore::GetLoginsImpl(
95     const autofill::PasswordForm& form,
96     PasswordStore::AuthorizationPromptPolicy prompt_policy,
97     const PasswordStore::ConsumerCallbackRunner& runner) {
98   std::vector<autofill::PasswordForm*> matched_forms;
99   std::vector<autofill::PasswordForm> forms =
100       stored_passwords_[form.signon_realm];
101   for (std::vector<autofill::PasswordForm>::iterator it = forms.begin();
102        it != forms.end(); ++it) {
103     matched_forms.push_back(new autofill::PasswordForm(*it));
104   }
105   runner.Run(matched_forms);
106 }
107
108 PasswordStoreChangeList TestPasswordStore::RemoveLoginsCreatedBetweenImpl(
109     const base::Time& begin, const base::Time& end) {
110   PasswordStoreChangeList changes;
111   return changes;
112 }
113
114 bool TestPasswordStore::FillAutofillableLogins(
115     std::vector<autofill::PasswordForm*>* forms) {
116   return true;
117 }
118
119 bool TestPasswordStore::FillBlacklistLogins(
120     std::vector<autofill::PasswordForm*>* forms) {
121   return true;
122 }
123
124 }  // namespace password_manager