OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ | 5 #ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ |
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ | 6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ |
7 | 7 |
8 #include <vector> | |
9 | |
10 #include "base/callback.h" | 8 #include "base/callback.h" |
11 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
12 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/scoped_vector.h" | 11 #include "base/memory/scoped_vector.h" |
14 #include "base/observer_list_threadsafe.h" | 12 #include "base/observer_list_threadsafe.h" |
15 #include "base/threading/thread.h" | 13 #include "base/single_thread_task_runner.h" |
16 #include "base/threading/thread_checker.h" | |
17 #include "base/time/time.h" | 14 #include "base/time/time.h" |
18 #include "components/password_manager/core/browser/password_store_change.h" | 15 #include "components/password_manager/core/browser/password_store_change.h" |
19 #include "components/password_manager/core/browser/password_store_sync.h" | 16 #include "components/password_manager/core/browser/password_store_sync.h" |
20 #include "sync/api/syncable_service.h" | 17 #include "sync/api/syncable_service.h" |
21 | 18 |
22 namespace autofill { | 19 namespace autofill { |
23 struct PasswordForm; | 20 struct PasswordForm; |
24 } | 21 } |
25 | 22 |
26 namespace syncer { | 23 namespace syncer { |
(...skipping 15 matching lines...) Expand all Loading... |
42 // should fail without side effects, return no data, and send no notifications. | 39 // should fail without side effects, return no data, and send no notifications. |
43 // PasswordStoreSync is a hidden base class because only PasswordSyncableService | 40 // PasswordStoreSync is a hidden base class because only PasswordSyncableService |
44 // needs to access these methods. | 41 // needs to access these methods. |
45 class PasswordStore : protected PasswordStoreSync, | 42 class PasswordStore : protected PasswordStoreSync, |
46 public base::RefCountedThreadSafe<PasswordStore> { | 43 public base::RefCountedThreadSafe<PasswordStore> { |
47 public: | 44 public: |
48 // Whether or not it's acceptable for Chrome to request access to locked | 45 // Whether or not it's acceptable for Chrome to request access to locked |
49 // passwords, which requires prompting the user for permission. | 46 // passwords, which requires prompting the user for permission. |
50 enum AuthorizationPromptPolicy { ALLOW_PROMPT, DISALLOW_PROMPT }; | 47 enum AuthorizationPromptPolicy { ALLOW_PROMPT, DISALLOW_PROMPT }; |
51 | 48 |
52 // PasswordForm vector elements are meant to be owned by the | |
53 // PasswordStoreConsumer. However, if the request is canceled after the | |
54 // allocation, then the request must take care of the deletion. | |
55 class GetLoginsRequest { | |
56 public: | |
57 explicit GetLoginsRequest(PasswordStoreConsumer* consumer); | |
58 virtual ~GetLoginsRequest(); | |
59 | |
60 void set_ignore_logins_cutoff(base::Time cutoff) { | |
61 ignore_logins_cutoff_ = cutoff; | |
62 } | |
63 | |
64 // Removes any logins in the result list that were saved before the cutoff. | |
65 void ApplyIgnoreLoginsCutoff(); | |
66 | |
67 // Forward the result to the consumer on the original message loop. | |
68 void ForwardResult(); | |
69 | |
70 std::vector<autofill::PasswordForm*>* result() const { | |
71 return result_.get(); | |
72 } | |
73 | |
74 private: | |
75 // See GetLogins(). Logins older than this will be removed from the reply. | |
76 base::Time ignore_logins_cutoff_; | |
77 | |
78 base::WeakPtr<PasswordStoreConsumer> consumer_weak_; | |
79 | |
80 // The result of the request. It is filled in on the PasswordStore's task | |
81 // thread and consumed on the UI thread. | |
82 // TODO(dubroy): Remove this, and instead pass the vector directly to the | |
83 // backend methods. | |
84 scoped_ptr<std::vector<autofill::PasswordForm*>> result_; | |
85 | |
86 base::ThreadChecker thread_checker_; | |
87 scoped_refptr<base::MessageLoopProxy> origin_loop_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest); | |
90 }; | |
91 | |
92 // An interface used to notify clients (observers) of this object that data in | 49 // An interface used to notify clients (observers) of this object that data in |
93 // the password store has changed. Register the observer via | 50 // the password store has changed. Register the observer via |
94 // PasswordStore::AddObserver. | 51 // PasswordStore::AddObserver. |
95 class Observer { | 52 class Observer { |
96 public: | 53 public: |
97 // Notifies the observer that password data changed. Will be called from | 54 // Notifies the observer that password data changed. Will be called from |
98 // the UI thread. | 55 // the UI thread. |
99 virtual void OnLoginsChanged(const PasswordStoreChangeList& changes) = 0; | 56 virtual void OnLoginsChanged(const PasswordStoreChangeList& changes) = 0; |
100 | 57 |
101 protected: | 58 protected: |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
167 | 124 |
168 #if defined(PASSWORD_MANAGER_ENABLE_SYNC) | 125 #if defined(PASSWORD_MANAGER_ENABLE_SYNC) |
169 base::WeakPtr<syncer::SyncableService> GetPasswordSyncableService(); | 126 base::WeakPtr<syncer::SyncableService> GetPasswordSyncableService(); |
170 #endif | 127 #endif |
171 | 128 |
172 protected: | 129 protected: |
173 friend class base::RefCountedThreadSafe<PasswordStore>; | 130 friend class base::RefCountedThreadSafe<PasswordStore>; |
174 | 131 |
175 typedef base::Callback<PasswordStoreChangeList(void)> ModificationTask; | 132 typedef base::Callback<PasswordStoreChangeList(void)> ModificationTask; |
176 | 133 |
| 134 class GetLoginsRequest { |
| 135 public: |
| 136 explicit GetLoginsRequest(PasswordStoreConsumer* consumer); |
| 137 virtual ~GetLoginsRequest(); |
| 138 |
| 139 void set_ignore_logins_cutoff(base::Time cutoff) { |
| 140 ignore_logins_cutoff_ = cutoff; |
| 141 } |
| 142 |
| 143 // Removes any logins in the result list that were saved before the cutoff. |
| 144 void ApplyIgnoreLoginsCutoff(); |
| 145 |
| 146 // Forward the result to the consumer on the original message loop. |
| 147 void ForwardResult(); |
| 148 |
| 149 ScopedVector<autofill::PasswordForm>* result() { return &result_; } |
| 150 |
| 151 private: |
| 152 // See GetLogins(). Logins older than this will be removed from the reply. |
| 153 base::Time ignore_logins_cutoff_; |
| 154 |
| 155 base::WeakPtr<PasswordStoreConsumer> consumer_weak_; |
| 156 |
| 157 // The result of the request. It is filled in on the PasswordStore's task |
| 158 // thread and consumed on the UI thread. |
| 159 ScopedVector<autofill::PasswordForm> result_; |
| 160 |
| 161 scoped_refptr<base::MessageLoopProxy> origin_loop_; |
| 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest); |
| 164 }; |
| 165 |
177 ~PasswordStore() override; | 166 ~PasswordStore() override; |
178 | 167 |
179 // Get the TaskRunner to use for PasswordStore background tasks. | 168 // Get the TaskRunner to use for PasswordStore background tasks. |
180 // By default, a SingleThreadTaskRunner on the DB thread is used, but | 169 // By default, a SingleThreadTaskRunner on the DB thread is used, but |
181 // subclasses can override. | 170 // subclasses can override. |
182 virtual scoped_refptr<base::SingleThreadTaskRunner> GetBackgroundTaskRunner(); | 171 virtual scoped_refptr<base::SingleThreadTaskRunner> GetBackgroundTaskRunner(); |
183 | 172 |
184 // Methods below will be run in PasswordStore's own thread. | 173 // Methods below will be run in PasswordStore's own thread. |
185 // Synchronous implementation that reports usage metrics. | 174 // Synchronous implementation that reports usage metrics. |
186 virtual void ReportMetricsImpl(const std::string& sync_username, | 175 virtual void ReportMetricsImpl(const std::string& sync_username, |
(...skipping 23 matching lines...) Expand all Loading... |
210 ConsumerCallbackRunner; | 199 ConsumerCallbackRunner; |
211 | 200 |
212 // Should find all PasswordForms with the same signon_realm. The results | 201 // Should find all PasswordForms with the same signon_realm. The results |
213 // will then be scored by the PasswordFormManager. Once they are found | 202 // will then be scored by the PasswordFormManager. Once they are found |
214 // (or not), the consumer should be notified. | 203 // (or not), the consumer should be notified. |
215 virtual void GetLoginsImpl(const autofill::PasswordForm& form, | 204 virtual void GetLoginsImpl(const autofill::PasswordForm& form, |
216 AuthorizationPromptPolicy prompt_policy, | 205 AuthorizationPromptPolicy prompt_policy, |
217 const ConsumerCallbackRunner& callback_runner) = 0; | 206 const ConsumerCallbackRunner& callback_runner) = 0; |
218 | 207 |
219 // Finds all non-blacklist PasswordForms, and notifies the consumer. | 208 // Finds all non-blacklist PasswordForms, and notifies the consumer. |
220 virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) = 0; | 209 virtual void GetAutofillableLoginsImpl( |
| 210 scoped_ptr<GetLoginsRequest> request) = 0; |
221 | 211 |
222 // Finds all blacklist PasswordForms, and notifies the consumer. | 212 // Finds all blacklist PasswordForms, and notifies the consumer. |
223 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0; | 213 virtual void GetBlacklistLoginsImpl(scoped_ptr<GetLoginsRequest> request) = 0; |
224 | 214 |
225 // Dispatches the result to the PasswordStoreConsumer on the original caller's | 215 // Dispatches the result to the PasswordStoreConsumer on the original caller's |
226 // thread so the callback can be executed there. This should be the UI thread. | 216 // thread so the callback can be executed there. This should be the UI thread. |
227 static void ForwardLoginsResult(GetLoginsRequest* request); | 217 static void ForwardLoginsResult(scoped_ptr<GetLoginsRequest> request); |
228 | 218 |
229 // Log UMA stats for number of bulk deletions. | 219 // Log UMA stats for number of bulk deletions. |
230 void LogStatsForBulkDeletion(int num_deletions); | 220 void LogStatsForBulkDeletion(int num_deletions); |
231 | 221 |
232 // Log UMA stats for password deletions happening on clear browsing data | 222 // Log UMA stats for password deletions happening on clear browsing data |
233 // since first sync during rollback. | 223 // since first sync during rollback. |
234 void LogStatsForBulkDeletionDuringRollback(int num_deletions); | 224 void LogStatsForBulkDeletionDuringRollback(int num_deletions); |
235 | 225 |
236 // PasswordStoreSync: | 226 // PasswordStoreSync: |
237 // Called by WrapModificationTask() once the underlying data-modifying | 227 // Called by WrapModificationTask() once the underlying data-modifying |
238 // operation has been performed. Notifies observers that password store data | 228 // operation has been performed. Notifies observers that password store data |
239 // may have been changed. | 229 // may have been changed. |
240 void NotifyLoginsChanged(const PasswordStoreChangeList& changes) override; | 230 void NotifyLoginsChanged(const PasswordStoreChangeList& changes) override; |
241 | 231 |
242 // TaskRunner for tasks that run on the main thread (usually the UI thread). | 232 // TaskRunner for tasks that run on the main thread (usually the UI thread). |
243 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; | 233 scoped_refptr<base::SingleThreadTaskRunner> main_thread_runner_; |
244 | 234 |
245 // TaskRunner for the DB thread. By default, this is the task runner used for | 235 // TaskRunner for the DB thread. By default, this is the task runner used for |
246 // background tasks -- see |GetBackgroundTaskRunner|. | 236 // background tasks -- see |GetBackgroundTaskRunner|. |
247 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner_; | 237 scoped_refptr<base::SingleThreadTaskRunner> db_thread_runner_; |
248 | 238 |
249 private: | 239 private: |
250 // Schedule the given |func| to be run in the PasswordStore's own thread with | 240 // Schedule the given |func| to be run in the PasswordStore's own thread with |
251 // responses delivered to |consumer| on the current thread. | 241 // responses delivered to |consumer| on the current thread. |
252 template <typename BackendFunc> | 242 void Schedule(void (PasswordStore::*func)(scoped_ptr<GetLoginsRequest>), |
253 void Schedule(BackendFunc func, PasswordStoreConsumer* consumer); | 243 PasswordStoreConsumer* consumer); |
254 | 244 |
255 // Wrapper method called on the destination thread (DB for non-mac) that | 245 // Wrapper method called on the destination thread (DB for non-mac) that |
256 // invokes |task| and then calls back into the source thread to notify | 246 // invokes |task| and then calls back into the source thread to notify |
257 // observers that the password store may have been modified via | 247 // observers that the password store may have been modified via |
258 // NotifyLoginsChanged(). Note that there is no guarantee that the called | 248 // NotifyLoginsChanged(). Note that there is no guarantee that the called |
259 // method will actually modify the password store data. | 249 // method will actually modify the password store data. |
260 void WrapModificationTask(ModificationTask task); | 250 void WrapModificationTask(ModificationTask task); |
261 | 251 |
262 // Temporary specializations of WrapModificationTask for a better stack trace. | 252 // Temporary specializations of WrapModificationTask for a better stack trace. |
263 void AddLoginInternal(const autofill::PasswordForm& form); | 253 void AddLoginInternal(const autofill::PasswordForm& form); |
264 void UpdateLoginInternal(const autofill::PasswordForm& form); | 254 void UpdateLoginInternal(const autofill::PasswordForm& form); |
265 void RemoveLoginInternal(const autofill::PasswordForm& form); | 255 void RemoveLoginInternal(const autofill::PasswordForm& form); |
266 void RemoveLoginsCreatedBetweenInternal(base::Time delete_begin, | 256 void RemoveLoginsCreatedBetweenInternal(base::Time delete_begin, |
267 base::Time delete_end); | 257 base::Time delete_end); |
268 void RemoveLoginsSyncedBetweenInternal(base::Time delete_begin, | 258 void RemoveLoginsSyncedBetweenInternal(base::Time delete_begin, |
269 base::Time delete_end); | 259 base::Time delete_end); |
270 | 260 |
271 // Copies |matched_forms| into the request's result vector, then calls | 261 // Moves |matched_forms| into the request's result vector, then calls |
272 // |ForwardLoginsResult|. Temporarily used as an adapter between the API of | 262 // |ForwardLoginsResult|. Temporarily used as an adapter between the API of |
273 // |GetLoginsImpl| and |PasswordStoreConsumer|. | 263 // |GetLoginsImpl| and |PasswordStoreConsumer|. |
274 // TODO(dubroy): Get rid of this. | 264 // TODO(dubroy): Get rid of this. |
275 static void CopyAndForwardLoginsResult( | 265 static void MoveAndForwardLoginsResult( |
276 PasswordStore::GetLoginsRequest* request, | 266 scoped_ptr<PasswordStore::GetLoginsRequest> request, |
277 ScopedVector<autofill::PasswordForm> matched_forms); | 267 ScopedVector<autofill::PasswordForm> matched_forms); |
278 | 268 |
279 #if defined(PASSWORD_MANAGER_ENABLE_SYNC) | 269 #if defined(PASSWORD_MANAGER_ENABLE_SYNC) |
280 // Creates PasswordSyncableService instance on the background thread. | 270 // Creates PasswordSyncableService instance on the background thread. |
281 void InitSyncableService( | 271 void InitSyncableService( |
282 const syncer::SyncableService::StartSyncFlare& flare); | 272 const syncer::SyncableService::StartSyncFlare& flare); |
283 | 273 |
284 // Deletes PasswordSyncableService instance on the background thread. | 274 // Deletes PasswordSyncableService instance on the background thread. |
285 void DestroySyncableService(); | 275 void DestroySyncableService(); |
286 #endif | 276 #endif |
287 | 277 |
288 // The observers. | 278 // The observers. |
289 scoped_refptr<ObserverListThreadSafe<Observer>> observers_; | 279 scoped_refptr<ObserverListThreadSafe<Observer>> observers_; |
290 | 280 |
291 scoped_ptr<PasswordSyncableService> syncable_service_; | 281 scoped_ptr<PasswordSyncableService> syncable_service_; |
292 | 282 |
293 bool shutdown_called_; | 283 bool shutdown_called_; |
294 | 284 |
295 DISALLOW_COPY_AND_ASSIGN(PasswordStore); | 285 DISALLOW_COPY_AND_ASSIGN(PasswordStore); |
296 }; | 286 }; |
297 | 287 |
298 } // namespace password_manager | 288 } // namespace password_manager |
299 | 289 |
300 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ | 290 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_STORE_H_ |
OLD | NEW |