- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_store_default.cc
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.
4
5 #include "chrome/browser/password_manager/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 "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"
19
20 using autofill::PasswordForm;
21 using content::BrowserThread;
22
23 PasswordStoreDefault::PasswordStoreDefault(LoginDatabase* login_db,
24                                            Profile* profile)
25     : login_db_(login_db), profile_(profile) {
26   DCHECK(login_db);
27   DCHECK(profile);
28 }
29
30 PasswordStoreDefault::~PasswordStoreDefault() {
31 }
32
33 void PasswordStoreDefault::ShutdownOnUIThread() {
34   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35   profile_ = NULL;
36 }
37
38 void PasswordStoreDefault::ReportMetricsImpl() {
39   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
40   login_db_->ReportMetrics();
41 }
42
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));
51   }
52 }
53
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));
62   }
63 }
64
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));
73   }
74 }
75
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,
85                                               **it));
86       }
87       LogStatsForBulkDeletion(changes.size());
88       content::NotificationService::current()->Notify(
89           chrome::NOTIFICATION_LOGINS_CHANGED,
90           content::Source<PasswordStore>(this),
91           content::Details<PasswordStoreChangeList>(&changes));
92     }
93   }
94   STLDeleteElements(&forms);
95 }
96
97 void PasswordStoreDefault::GetLoginsImpl(
98     const autofill::PasswordForm& form,
99     const ConsumerCallbackRunner& callback_runner) {
100   std::vector<PasswordForm*> matched_forms;
101   login_db_->GetLogins(form, &matched_forms);
102   callback_runner.Run(matched_forms);
103 }
104
105 void PasswordStoreDefault::GetAutofillableLoginsImpl(
106     GetLoginsRequest* request) {
107   FillAutofillableLogins(&request->value);
108   ForwardLoginsResult(request);
109 }
110
111 void PasswordStoreDefault::GetBlacklistLoginsImpl(
112     GetLoginsRequest* request) {
113   FillBlacklistLogins(&request->value);
114   ForwardLoginsResult(request);
115 }
116
117 bool PasswordStoreDefault::FillAutofillableLogins(
118          std::vector<PasswordForm*>* forms) {
119   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
120   return login_db_->GetAutofillableLogins(forms);
121 }
122
123 bool PasswordStoreDefault::FillBlacklistLogins(
124          std::vector<PasswordForm*>* forms) {
125   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
126   return login_db_->GetBlacklistLogins(forms);
127 }