1 // Copyright (c) 2012 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.
5 #include "chrome/browser/password_manager/password_store_default.h"
9 #include "base/logging.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/stl_util.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/password_manager/password_store_change.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/pref_names.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/notification_service.h"
20 using autofill::PasswordForm
;
21 using content::BrowserThread
;
23 PasswordStoreDefault::PasswordStoreDefault(LoginDatabase
* login_db
,
25 : login_db_(login_db
), profile_(profile
) {
30 PasswordStoreDefault::~PasswordStoreDefault() {
33 void PasswordStoreDefault::ShutdownOnUIThread() {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
38 void PasswordStoreDefault::ReportMetricsImpl() {
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
40 login_db_
->ReportMetrics();
43 void PasswordStoreDefault::AddLoginImpl(const PasswordForm
& form
) {
44 if (login_db_
->AddLogin(form
)) {
45 PasswordStoreChangeList changes
;
46 changes
.push_back(PasswordStoreChange(PasswordStoreChange::ADD
, form
));
47 content::NotificationService::current()->Notify(
48 chrome::NOTIFICATION_LOGINS_CHANGED
,
49 content::Source
<PasswordStore
>(this),
50 content::Details
<PasswordStoreChangeList
>(&changes
));
54 void PasswordStoreDefault::UpdateLoginImpl(const PasswordForm
& form
) {
55 if (login_db_
->UpdateLogin(form
, NULL
)) {
56 PasswordStoreChangeList changes
;
57 changes
.push_back(PasswordStoreChange(PasswordStoreChange::UPDATE
, form
));
58 content::NotificationService::current()->Notify(
59 chrome::NOTIFICATION_LOGINS_CHANGED
,
60 content::Source
<PasswordStore
>(this),
61 content::Details
<PasswordStoreChangeList
>(&changes
));
65 void PasswordStoreDefault::RemoveLoginImpl(const PasswordForm
& form
) {
66 if (login_db_
->RemoveLogin(form
)) {
67 PasswordStoreChangeList changes
;
68 changes
.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE
, form
));
69 content::NotificationService::current()->Notify(
70 chrome::NOTIFICATION_LOGINS_CHANGED
,
71 content::Source
<PasswordStore
>(this),
72 content::Details
<PasswordStoreChangeList
>(&changes
));
76 void PasswordStoreDefault::RemoveLoginsCreatedBetweenImpl(
77 const base::Time
& delete_begin
, const base::Time
& delete_end
) {
78 std::vector
<PasswordForm
*> forms
;
79 if (login_db_
->GetLoginsCreatedBetween(delete_begin
, delete_end
, &forms
)) {
80 if (login_db_
->RemoveLoginsCreatedBetween(delete_begin
, delete_end
)) {
81 PasswordStoreChangeList changes
;
82 for (std::vector
<PasswordForm
*>::const_iterator it
= forms
.begin();
83 it
!= forms
.end(); ++it
) {
84 changes
.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE
,
87 LogStatsForBulkDeletion(changes
.size());
88 content::NotificationService::current()->Notify(
89 chrome::NOTIFICATION_LOGINS_CHANGED
,
90 content::Source
<PasswordStore
>(this),
91 content::Details
<PasswordStoreChangeList
>(&changes
));
94 STLDeleteElements(&forms
);
97 void PasswordStoreDefault::GetLoginsImpl(
98 const autofill::PasswordForm
& form
,
99 AuthorizationPromptPolicy prompt_policy
,
100 const ConsumerCallbackRunner
& callback_runner
) {
101 std::vector
<PasswordForm
*> matched_forms
;
102 login_db_
->GetLogins(form
, &matched_forms
);
103 callback_runner
.Run(matched_forms
);
106 void PasswordStoreDefault::GetAutofillableLoginsImpl(
107 GetLoginsRequest
* request
) {
108 FillAutofillableLogins(&request
->value
);
109 ForwardLoginsResult(request
);
112 void PasswordStoreDefault::GetBlacklistLoginsImpl(
113 GetLoginsRequest
* request
) {
114 FillBlacklistLogins(&request
->value
);
115 ForwardLoginsResult(request
);
118 bool PasswordStoreDefault::FillAutofillableLogins(
119 std::vector
<PasswordForm
*>* forms
) {
120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
121 return login_db_
->GetAutofillableLogins(forms
);
124 bool PasswordStoreDefault::FillBlacklistLogins(
125 std::vector
<PasswordForm
*>* forms
) {
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB
));
127 return login_db_
->GetBlacklistLogins(forms
);