password_store_default.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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    const base::Time& delete_begin, const base::Time& delete_end) {
57  std::vector<PasswordForm*> forms;
58  PasswordStoreChangeList changes;
59  if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) {
60    if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) {
61      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
62           it != forms.end(); ++it) {
63        changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
64                                              **it));
65      }
66      LogStatsForBulkDeletion(changes.size());
67    }
68  }
69  STLDeleteElements(&forms);
70  return changes;
71}
72
73void PasswordStoreDefault::GetLoginsImpl(
74    const autofill::PasswordForm& form,
75    AuthorizationPromptPolicy prompt_policy,
76    const ConsumerCallbackRunner& callback_runner) {
77  std::vector<PasswordForm*> matched_forms;
78  login_db_->GetLogins(form, &matched_forms);
79  callback_runner.Run(matched_forms);
80}
81
82void PasswordStoreDefault::GetAutofillableLoginsImpl(
83    GetLoginsRequest* request) {
84  FillAutofillableLogins(request->result());
85  ForwardLoginsResult(request);
86}
87
88void PasswordStoreDefault::GetBlacklistLoginsImpl(
89    GetLoginsRequest* request) {
90  FillBlacklistLogins(request->result());
91  ForwardLoginsResult(request);
92}
93
94bool PasswordStoreDefault::FillAutofillableLogins(
95         std::vector<PasswordForm*>* forms) {
96  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
97  return login_db_->GetAutofillableLogins(forms);
98}
99
100bool PasswordStoreDefault::FillBlacklistLogins(
101         std::vector<PasswordForm*>* forms) {
102  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
103  return login_db_->GetBlacklistLogins(forms);
104}
105
106}  // namespace password_manager
107