Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / password_store_default.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/password_store_default.h"
6
7 #include <set>
8
9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "components/password_manager/core/browser/password_store_change.h"
13
14 using autofill::PasswordForm;
15
16 namespace password_manager {
17
18 PasswordStoreDefault::PasswordStoreDefault(
19     scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
20     scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
21     LoginDatabase* login_db)
22     : PasswordStore(main_thread_runner, db_thread_runner), login_db_(login_db) {
23   DCHECK(login_db);
24 }
25
26 PasswordStoreDefault::~PasswordStoreDefault() {
27 }
28
29 void PasswordStoreDefault::ReportMetricsImpl() {
30   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
31   login_db_->ReportMetrics();
32 }
33
34 PasswordStoreChangeList PasswordStoreDefault::AddLoginImpl(
35     const PasswordForm& form) {
36   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
37   PasswordStoreChangeList changes;
38   if (login_db_->AddLogin(form))
39     changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
40   return changes;
41 }
42
43 PasswordStoreChangeList PasswordStoreDefault::UpdateLoginImpl(
44     const PasswordForm& form) {
45   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
46   PasswordStoreChangeList changes;
47   if (login_db_->UpdateLogin(form, NULL))
48     changes.push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, form));
49   return changes;
50 }
51
52 PasswordStoreChangeList PasswordStoreDefault::RemoveLoginImpl(
53     const PasswordForm& form) {
54   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
55   PasswordStoreChangeList changes;
56   if (login_db_->RemoveLogin(form))
57     changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, form));
58   return changes;
59 }
60
61 PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsCreatedBetweenImpl(
62     const base::Time& delete_begin, const base::Time& delete_end) {
63   std::vector<PasswordForm*> forms;
64   PasswordStoreChangeList changes;
65   if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) {
66     if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) {
67       for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
68            it != forms.end(); ++it) {
69         changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
70                                               **it));
71       }
72       LogStatsForBulkDeletion(changes.size());
73     }
74   }
75   STLDeleteElements(&forms);
76   return changes;
77 }
78
79 void PasswordStoreDefault::GetLoginsImpl(
80     const autofill::PasswordForm& form,
81     AuthorizationPromptPolicy prompt_policy,
82     const ConsumerCallbackRunner& callback_runner) {
83   std::vector<PasswordForm*> matched_forms;
84   login_db_->GetLogins(form, &matched_forms);
85   callback_runner.Run(matched_forms);
86 }
87
88 void PasswordStoreDefault::GetAutofillableLoginsImpl(
89     GetLoginsRequest* request) {
90   FillAutofillableLogins(request->result());
91   ForwardLoginsResult(request);
92 }
93
94 void PasswordStoreDefault::GetBlacklistLoginsImpl(
95     GetLoginsRequest* request) {
96   FillBlacklistLogins(request->result());
97   ForwardLoginsResult(request);
98 }
99
100 bool PasswordStoreDefault::FillAutofillableLogins(
101          std::vector<PasswordForm*>* forms) {
102   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
103   return login_db_->GetAutofillableLogins(forms);
104 }
105
106 bool PasswordStoreDefault::FillBlacklistLogins(
107          std::vector<PasswordForm*>* forms) {
108   DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
109   return login_db_->GetBlacklistLogins(forms);
110 }
111
112 }  // namespace password_manager