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 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/threading/thread.h"
15 #include "base/threading/thread_checker.h"
16 #include "base/time/time.h"
17 #include "chrome/browser/common/cancelable_request.h"
18 #include "components/browser_context_keyed_service/refcounted_browser_context_keyed_service.h"
21 class PasswordStoreConsumer;
22 class PasswordSyncableService;
29 namespace browser_sync {
30 class PasswordChangeProcessor;
31 class PasswordDataTypeController;
32 class PasswordModelAssociator;
33 class PasswordModelWorker;
36 namespace passwords_helper {
37 void AddLogin(PasswordStore* store, const autofill::PasswordForm& form);
38 void RemoveLogin(PasswordStore* store, const autofill::PasswordForm& form);
39 void UpdateLogin(PasswordStore* store, const autofill::PasswordForm& form);
42 // Interface for storing form passwords in a platform-specific secure way.
43 // The login request/manipulation API is not threadsafe and must be used
44 // from the UI thread.
45 class PasswordStore : public RefcountedBrowserContextKeyedService {
47 // Whether or not it's acceptable for Chrome to request access to locked
48 // passwords, which requires prompting the user for permission.
49 enum AuthorizationPromptPolicy {
54 // PasswordForm vector elements are meant to be owned by the
55 // PasswordStoreConsumer. However, if the request is canceled after the
56 // allocation, then the request must take care of the deletion.
57 class GetLoginsRequest {
59 explicit GetLoginsRequest(PasswordStoreConsumer* consumer);
60 virtual ~GetLoginsRequest();
62 void set_ignore_logins_cutoff(const base::Time& cutoff) {
63 ignore_logins_cutoff_ = cutoff;
66 // Removes any logins in the result list that were saved before the cutoff.
67 void ApplyIgnoreLoginsCutoff();
69 // Forward the result to the consumer on the original message loop.
72 std::vector<autofill::PasswordForm*>* result() const {
77 // See GetLogins(). Logins older than this will be removed from the reply.
78 base::Time ignore_logins_cutoff_;
80 base::WeakPtr<PasswordStoreConsumer> consumer_weak_;
82 // The result of the request. It is filled in on the PasswordStore's task
83 // thread and consumed on the UI thread.
84 // TODO(dubroy): Remove this, and instead pass the vector directly to the
86 scoped_ptr< std::vector<autofill::PasswordForm*> > result_;
88 base::ThreadChecker thread_checker_;
89 scoped_refptr<base::MessageLoopProxy> origin_loop_;
91 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
94 // An interface used to notify clients (observers) of this object that data in
95 // the password store has changed. Register the observer via
96 // PasswordStore::SetObserver.
99 // Notifies the observer that password data changed in some way.
100 virtual void OnLoginsChanged() = 0;
103 virtual ~Observer() {}
108 // Reimplement this to add custom initialization. Always call this too.
111 // Adds the given PasswordForm to the secure password store asynchronously.
112 virtual void AddLogin(const autofill::PasswordForm& form);
114 // Updates the matching PasswordForm in the secure password store (async).
115 void UpdateLogin(const autofill::PasswordForm& form);
117 // Removes the matching PasswordForm from the secure password store (async).
118 void RemoveLogin(const autofill::PasswordForm& form);
120 // Removes all logins created in the given date range.
121 void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
122 const base::Time& delete_end);
124 // Searches for a matching PasswordForm, and notifies |consumer| on
125 // completion. The request will be cancelled if the consumer is destroyed.
126 // |prompt_policy| indicates whether it's permissible to prompt the user to
127 // authorize access to locked passwords. This argument is only used on
128 // platforms that support prompting the user for access (such as Mac OS).
129 // NOTE: This means that this method can return different results depending
130 // on the value of |prompt_policy|.
131 virtual void GetLogins(
132 const autofill::PasswordForm& form,
133 AuthorizationPromptPolicy prompt_policy,
134 PasswordStoreConsumer* consumer);
136 // Gets the complete list of PasswordForms that are not blacklist entries--and
137 // are thus auto-fillable. |consumer| will be notified on completion.
138 // The request will be cancelled if the consumer is destroyed.
139 void GetAutofillableLogins(PasswordStoreConsumer* consumer);
141 // Gets the complete list of PasswordForms that are blacklist entries,
142 // and notify |consumer| on completion. The request will be cancelled if the
143 // consumer is destroyed.
144 void GetBlacklistLogins(PasswordStoreConsumer* consumer);
146 // Reports usage metrics for the database.
147 void ReportMetrics();
149 // Adds an observer to be notified when the password store data changes.
150 void AddObserver(Observer* observer);
152 // Removes |observer| from the observer list.
153 void RemoveObserver(Observer* observer);
156 friend class base::RefCountedThreadSafe<PasswordStore>;
157 // Sync's interaction with password store needs to be synchronous.
158 // Since the synchronous methods are private these classes are made
159 // as friends. This can be fixed by moving the private impl to a new
160 // class. See http://crbug.com/307750
161 friend class browser_sync::PasswordChangeProcessor;
162 friend class browser_sync::PasswordDataTypeController;
163 friend class browser_sync::PasswordModelAssociator;
164 friend class browser_sync::PasswordModelWorker;
165 friend class PasswordSyncableService;
166 friend void passwords_helper::AddLogin(PasswordStore*,
167 const autofill::PasswordForm&);
168 friend void passwords_helper::RemoveLogin(PasswordStore*,
169 const autofill::PasswordForm&);
170 friend void passwords_helper::UpdateLogin(PasswordStore*,
171 const autofill::PasswordForm&);
173 virtual ~PasswordStore();
175 // Schedules the given |task| to be run on the PasswordStore's TaskRunner.
176 bool ScheduleTask(const base::Closure& task);
178 // Get the TaskRunner to use for PasswordStore tasks.
179 // By default, a SingleThreadTaskRunner on the DB thread is used, but
180 // subclasses can override.
181 virtual scoped_refptr<base::SequencedTaskRunner> GetTaskRunner();
183 // These will be run in PasswordStore's own thread.
184 // Synchronous implementation that reports usage metrics.
185 virtual void ReportMetricsImpl() = 0;
186 // Synchronous implementation to add the given login.
187 virtual void AddLoginImpl(const autofill::PasswordForm& form) = 0;
188 // Synchronous implementation to update the given login.
189 virtual void UpdateLoginImpl(const autofill::PasswordForm& form) = 0;
190 // Synchronous implementation to remove the given login.
191 virtual void RemoveLoginImpl(const autofill::PasswordForm& form) = 0;
192 // Synchronous implementation to remove the given logins.
193 virtual void RemoveLoginsCreatedBetweenImpl(const base::Time& delete_begin,
194 const base::Time& delete_end) = 0;
196 typedef base::Callback<void(const std::vector<autofill::PasswordForm*>&)>
197 ConsumerCallbackRunner; // Owns all PasswordForms in the vector.
199 // Should find all PasswordForms with the same signon_realm. The results
200 // will then be scored by the PasswordFormManager. Once they are found
201 // (or not), the consumer should be notified.
202 virtual void GetLoginsImpl(
203 const autofill::PasswordForm& form,
204 AuthorizationPromptPolicy prompt_policy,
205 const ConsumerCallbackRunner& callback_runner) = 0;
207 // Finds all non-blacklist PasswordForms, and notifies the consumer.
208 virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) = 0;
210 // Finds all blacklist PasswordForms, and notifies the consumer.
211 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
213 // Finds all non-blacklist PasswordForms, and fills the vector.
214 virtual bool FillAutofillableLogins(
215 std::vector<autofill::PasswordForm*>* forms) = 0;
217 // Finds all blacklist PasswordForms, and fills the vector.
218 virtual bool FillBlacklistLogins(
219 std::vector<autofill::PasswordForm*>* forms) = 0;
221 // Dispatches the result to the PasswordStoreConsumer on the original caller's
222 // thread so the callback can be executed there. This should be the UI thread.
223 virtual void ForwardLoginsResult(GetLoginsRequest* request);
225 // Log UMA stats for number of bulk deletions.
226 void LogStatsForBulkDeletion(int num_deletions);
229 // Schedule the given |func| to be run in the PasswordStore's own thread with
230 // responses delivered to |consumer| on the current thread.
231 template<typename BackendFunc>
232 void Schedule(BackendFunc func, PasswordStoreConsumer* consumer);
234 // Wrapper method called on the destination thread (DB for non-mac) that
235 // invokes |task| and then calls back into the source thread to notify
236 // observers that the password store may have been modified via
237 // NotifyLoginsChanged(). Note that there is no guarantee that the called
238 // method will actually modify the password store data.
239 virtual void WrapModificationTask(base::Closure task);
241 // Post a message to the UI thread to run NotifyLoginsChanged(). Called by
242 // WrapModificationTask() above, and split out as a separate method so that
243 // password sync can call it as well after synchronously updating the password
245 void PostNotifyLoginsChanged();
247 // Called by WrapModificationTask() once the underlying data-modifying
248 // operation has been performed. Notifies observers that password store data
249 // may have been changed.
250 void NotifyLoginsChanged();
252 // Copies |matched_forms| into the request's result vector, then calls
253 // |ForwardLoginsResult|. Temporarily used as an adapter between the API of
254 // |GetLoginsImpl| and |PasswordStoreConsumer|.
255 // TODO(dubroy): Get rid of this.
256 void CopyAndForwardLoginsResult(
257 PasswordStore::GetLoginsRequest* request,
258 const std::vector<autofill::PasswordForm*>& matched_forms);
261 ObserverList<Observer> observers_;
263 DISALLOW_COPY_AND_ASSIGN(PasswordStore);
266 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_