password_store_default.cc revision cedac228d2dd51db4b79ea1e72c7f249408ee061
15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store_default.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <set>
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/logging.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/prefs/pref_service.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/stl_util.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store_change.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)using autofill::PasswordForm;
155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
16c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochnamespace password_manager {
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreDefault::PasswordStoreDefault(
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner,
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner,
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LoginDatabase* login_db)
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : PasswordStore(main_thread_runner, db_thread_runner), login_db_(login_db) {
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(login_db);
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreDefault::~PasswordStoreDefault() {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::ReportMetricsImpl() {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  login_db_->ReportMetrics();
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::AddLoginImpl(
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const PasswordForm& form) {
36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return login_db_->AddLogin(form);
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::UpdateLoginImpl(
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const PasswordForm& form) {
42010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return login_db_->UpdateLogin(form);
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::RemoveLoginImpl(
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const PasswordForm& form) {
48010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreChangeList changes;
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (login_db_->RemoveLogin(form))
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, form));
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return changes;
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsCreatedBetweenImpl(
565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const base::Time& delete_begin, const base::Time& delete_end) {
575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<PasswordForm*> forms;
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreChangeList changes;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) {
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           it != forms.end(); ++it) {
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              **it));
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      LogStatsForBulkDeletion(changes.size());
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  STLDeleteElements(&forms);
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return changes;
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetLoginsImpl(
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const autofill::PasswordForm& form,
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AuthorizationPromptPolicy prompt_policy,
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ConsumerCallbackRunner& callback_runner) {
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<PasswordForm*> matched_forms;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  login_db_->GetLogins(form, &matched_forms);
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_runner.Run(matched_forms);
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetAutofillableLoginsImpl(
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetLoginsRequest* request) {
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillAutofillableLogins(request->result());
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ForwardLoginsResult(request);
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetBlacklistLoginsImpl(
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetLoginsRequest* request) {
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillBlacklistLogins(request->result());
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ForwardLoginsResult(request);
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillAutofillableLogins(
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         std::vector<PasswordForm*>* forms) {
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return login_db_->GetAutofillableLogins(forms);
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillBlacklistLogins(
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         std::vector<PasswordForm*>* forms) {
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return login_db_->GetBlacklistLogins(forms);
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
105c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
106c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}  // namespace password_manager
107