password_store_default.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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
14using autofill::PasswordForm;
15
16namespace password_manager {
17
18PasswordStoreDefault::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
26PasswordStoreDefault::~PasswordStoreDefault() {
27}
28
29void PasswordStoreDefault::ReportMetricsImpl() {
30  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
31  login_db_->ReportMetrics();
32}
33
34PasswordStoreChangeList PasswordStoreDefault::AddLoginImpl(
35    const PasswordForm& form) {
36  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
37  return login_db_->AddLogin(form);
38}
39
40PasswordStoreChangeList PasswordStoreDefault::UpdateLoginImpl(
41    const PasswordForm& form) {
42  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
43  return login_db_->UpdateLogin(form);
44}
45
46PasswordStoreChangeList PasswordStoreDefault::RemoveLoginImpl(
47    const PasswordForm& form) {
48  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
49  PasswordStoreChangeList changes;
50  if (login_db_->RemoveLogin(form))
51    changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, form));
52  return changes;
53}
54
55PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsCreatedBetweenImpl(
56    base::Time delete_begin,
57    base::Time delete_end) {
58  std::vector<PasswordForm*> forms;
59  PasswordStoreChangeList changes;
60  if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) {
61    if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) {
62      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
63           it != forms.end(); ++it) {
64        changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
65                                              **it));
66      }
67      LogStatsForBulkDeletion(changes.size());
68    }
69  }
70  STLDeleteElements(&forms);
71  return changes;
72}
73
74PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsSyncedBetweenImpl(
75    base::Time delete_begin,
76    base::Time delete_end) {
77  std::vector<PasswordForm*> forms;
78  PasswordStoreChangeList changes;
79  if (login_db_->GetLoginsSyncedBetween(delete_begin, delete_end, &forms)) {
80    if (login_db_->RemoveLoginsSyncedBetween(delete_begin, delete_end)) {
81      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
82           it != forms.end();
83           ++it) {
84        changes.push_back(
85            PasswordStoreChange(PasswordStoreChange::REMOVE, **it));
86      }
87    }
88  }
89  STLDeleteElements(&forms);
90  return changes;
91}
92
93void PasswordStoreDefault::GetLoginsImpl(
94    const autofill::PasswordForm& form,
95    AuthorizationPromptPolicy prompt_policy,
96    const ConsumerCallbackRunner& callback_runner) {
97  std::vector<PasswordForm*> matched_forms;
98  login_db_->GetLogins(form, &matched_forms);
99  callback_runner.Run(matched_forms);
100}
101
102void PasswordStoreDefault::GetAutofillableLoginsImpl(
103    GetLoginsRequest* request) {
104  FillAutofillableLogins(request->result());
105  ForwardLoginsResult(request);
106}
107
108void PasswordStoreDefault::GetBlacklistLoginsImpl(
109    GetLoginsRequest* request) {
110  FillBlacklistLogins(request->result());
111  ForwardLoginsResult(request);
112}
113
114bool PasswordStoreDefault::FillAutofillableLogins(
115         std::vector<PasswordForm*>* forms) {
116  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
117  return login_db_->GetAutofillableLogins(forms);
118}
119
120bool PasswordStoreDefault::FillBlacklistLogins(
121         std::vector<PasswordForm*>* forms) {
122  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
123  return login_db_->GetBlacklistLogins(forms);
124}
125
126}  // namespace password_manager
127