password_store_default.cc revision c5cede9ae108bb15f6b7a8aea21c7e1fefa2834c
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) { 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) PasswordStoreChangeList changes; 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (login_db_->AddLogin(form)) 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) changes.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form)); 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return changes; 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::UpdateLoginImpl( 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const PasswordForm& form) { 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) PasswordStoreChangeList changes; 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (login_db_->UpdateLogin(form, NULL)) 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) changes.push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, form)); 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return changes; 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::RemoveLoginImpl( 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const PasswordForm& form) { 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) PasswordStoreChangeList changes; 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (login_db_->RemoveLogin(form)) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, form)); 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return changes; 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)PasswordStoreChangeList PasswordStoreDefault::RemoveLoginsCreatedBetweenImpl( 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const base::Time& delete_begin, const base::Time& delete_end) { 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<PasswordForm*> forms; 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) PasswordStoreChangeList changes; 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (login_db_->GetLoginsCreatedBetween(delete_begin, delete_end, &forms)) { 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) if (login_db_->RemoveLoginsCreatedBetween(delete_begin, delete_end)) { 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) for (std::vector<PasswordForm*>::const_iterator it = forms.begin(); 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) it != forms.end(); ++it) { 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) changes.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) **it)); 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) LogStatsForBulkDeletion(changes.size()); 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) STLDeleteElements(&forms); 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return changes; 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetLoginsImpl( 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const autofill::PasswordForm& form, 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AuthorizationPromptPolicy prompt_policy, 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const ConsumerCallbackRunner& callback_runner) { 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<PasswordForm*> matched_forms; 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) login_db_->GetLogins(form, &matched_forms); 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) callback_runner.Run(matched_forms); 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetAutofillableLoginsImpl( 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GetLoginsRequest* request) { 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FillAutofillableLogins(request->result()); 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ForwardLoginsResult(request); 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void PasswordStoreDefault::GetBlacklistLoginsImpl( 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) GetLoginsRequest* request) { 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) FillBlacklistLogins(request->result()); 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ForwardLoginsResult(request); 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillAutofillableLogins( 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<PasswordForm*>* forms) { 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return login_db_->GetAutofillableLogins(forms); 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool PasswordStoreDefault::FillBlacklistLogins( 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::vector<PasswordForm*>* forms) { 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) DCHECK(GetBackgroundTaskRunner()->BelongsToCurrentThread()); 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return login_db_->GetBlacklistLogins(forms); 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 108c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch 109c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch} // namespace password_manager 110