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)
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PasswordStoreDefault::ReportMetricsImpl(const std::string& sync_username) {
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  login_db_->ReportMetrics(sync_username);
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(
566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    base::Time delete_begin,
576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    base::Time delete_end) {
585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<PasswordForm*> forms;
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordStoreChangeList changes;
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) {
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) {
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           it != forms.end(); ++it) {
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE,
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                              **it));
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      LogStatsForBulkDeletion(changes.size());
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  STLDeleteElements(&forms);
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return changes;
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
74f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsSyncedBetweenImpl(
75f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    base::Time delete_begin,
76f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    base::Time delete_end) {
77f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::vector<PasswordForm*> forms;
78f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  PasswordStoreChangeList changes;
79f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (login_db_->GetLoginsSyncedBetween(delete_begin, delete_end, &forms)) {
80f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (login_db_->RemoveLoginsSyncedBetween(delete_begin, delete_end)) {
81f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      for (std::vector<PasswordForm*>::const_iterator it = forms.begin();
82f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)           it != forms.end();
83f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)           ++it) {
84f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        changes.push_back(
85f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            PasswordStoreChange(PasswordStoreChange::REMOVE, **it));
86f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      }
87f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
88f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  }
89f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  STLDeleteElements(&forms);
90f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return changes;
91f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
92f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetLoginsImpl(
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const autofill::PasswordForm& form,
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    AuthorizationPromptPolicy prompt_policy,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    const ConsumerCallbackRunner& callback_runner) {
975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::vector<PasswordForm*> matched_forms;
985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  login_db_->GetLogins(form, &matched_forms);
995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  callback_runner.Run(matched_forms);
1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetAutofillableLoginsImpl(
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetLoginsRequest* request) {
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillAutofillableLogins(request->result());
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ForwardLoginsResult(request);
1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetBlacklistLoginsImpl(
1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    GetLoginsRequest* request) {
1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FillBlacklistLogins(request->result());
1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  ForwardLoginsResult(request);
1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillAutofillableLogins(
1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         std::vector<PasswordForm*>* forms) {
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return login_db_->GetAutofillableLogins(forms);
1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillBlacklistLogins(
1215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)         std::vector<PasswordForm*>* forms) {
1225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread());
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return login_db_->GetBlacklistLogins(forms);
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
125c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
126c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}  // namespace password_manager
127