password_store.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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#ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
6#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
7#pragma once
8
9#include <set>
10#include <vector>
11
12#include "base/ref_counted.h"
13#include "base/thread.h"
14#include "base/time.h"
15#include "webkit/glue/password_form.h"
16
17class Profile;
18class Task;
19
20namespace browser_sync {
21class PasswordDataTypeController;
22class PasswordModelAssociator;
23class PasswordModelWorker;
24};
25
26class PasswordStoreConsumer {
27 public:
28  virtual ~PasswordStoreConsumer() {}
29  // Call this when the request is finished. If there are no results, call it
30  // anyway with an empty vector.
31  virtual void OnPasswordStoreRequestDone(
32      int handle, const std::vector<webkit_glue::PasswordForm*>& result) = 0;
33};
34
35// Interface for storing form passwords in a platform-specific secure way.
36// The login request/manipulation API is not threadsafe and must be used
37// from the UI thread.
38class PasswordStore : public base::RefCountedThreadSafe<PasswordStore> {
39 public:
40  PasswordStore();
41
42  // Reimplement this to add custom initialization. Always call this too.
43  virtual bool Init();
44
45  // Adds the given PasswordForm to the secure password store asynchronously.
46  virtual void AddLogin(const webkit_glue::PasswordForm& form);
47
48  // Updates the matching PasswordForm in the secure password store (async).
49  void UpdateLogin(const webkit_glue::PasswordForm& form);
50
51  // Removes the matching PasswordForm from the secure password store (async).
52  void RemoveLogin(const webkit_glue::PasswordForm& form);
53
54  // Removes all logins created in the given date range.
55  void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
56                                  const base::Time& delete_end);
57
58  // Searches for a matching PasswordForm and returns a handle so the async
59  // request can be tracked. Implement the PasswordStoreConsumer interface to
60  // be notified on completion.
61  virtual int GetLogins(const webkit_glue::PasswordForm& form,
62                        PasswordStoreConsumer* consumer);
63
64  // Gets the complete list of PasswordForms that are not blacklist entries--and
65  // are thus auto-fillable--and returns a handle so the async request can be
66  // tracked. Implement the PasswordStoreConsumer interface to be notified
67  // on completion.
68  int GetAutofillableLogins(PasswordStoreConsumer* consumer);
69
70  // Gets the complete list of PasswordForms that are blacklist entries, and
71  // returns a handle so the async request can be tracked. Implement the
72  // PasswordStoreConsumer interface to be notified on completion.
73  int GetBlacklistLogins(PasswordStoreConsumer* consumer);
74
75  // Cancels a previous Get*Logins query (async)
76  void CancelLoginsQuery(int handle);
77
78  // Reports usage metrics for the database.
79  virtual void ReportMetrics();
80
81 protected:
82  friend class base::RefCountedThreadSafe<PasswordStore>;
83  friend class browser_sync::PasswordDataTypeController;
84  friend class browser_sync::PasswordModelAssociator;
85  friend class browser_sync::PasswordModelWorker;
86
87  virtual ~PasswordStore() {}
88
89  // Simple container class that represents a login lookup request.
90  class GetLoginsRequest {
91   public:
92    GetLoginsRequest(PasswordStoreConsumer* c,
93                     int handle);
94
95    // The consumer to notify when this request is complete.
96    PasswordStoreConsumer* consumer;
97    // A unique handle for the request
98    int handle;
99    // The message loop that the request was made from.  We send the result
100    // back to the consumer in this same message loop.
101    MessageLoop* message_loop;
102
103   private:
104    DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
105  };
106
107  // Schedule the given task to be run in the PasswordStore's own thread.
108  virtual void ScheduleTask(Task* task);
109
110  // These will be run in PasswordStore's own thread.
111  // Synchronous implementation that reports usage metrics.
112  virtual void ReportMetricsImpl() = 0;
113  // Synchronous implementation to add the given login.
114  virtual void AddLoginImpl(const webkit_glue::PasswordForm& form) = 0;
115  // Synchronous implementation to update the given login.
116  virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form) = 0;
117  // Synchronous implementation to remove the given login.
118  virtual void RemoveLoginImpl(const webkit_glue::PasswordForm& form) = 0;
119  // Synchronous implementation to remove the given logins.
120  virtual void RemoveLoginsCreatedBetweenImpl(const base::Time& delete_begin,
121                                              const base::Time& delete_end) = 0;
122  // Should find all PasswordForms with the same signon_realm. The results
123  // will then be scored by the PasswordFormManager. Once they are found
124  // (or not), the consumer should be notified.
125  virtual void GetLoginsImpl(GetLoginsRequest* request,
126                             const webkit_glue::PasswordForm& form) = 0;
127  // Finds all non-blacklist PasswordForms, and notifies the consumer.
128  virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) = 0;
129  // Finds all blacklist PasswordForms, and notifies the consumer.
130  virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
131
132  // Finds all non-blacklist PasswordForms, and fills the vector.
133  virtual bool FillAutofillableLogins(
134      std::vector<webkit_glue::PasswordForm*>* forms) = 0;
135  // Finds all blacklist PasswordForms, and fills the vector.
136  virtual bool FillBlacklistLogins(
137      std::vector<webkit_glue::PasswordForm*>* forms) = 0;
138
139  // Notifies the consumer that a Get*Logins() request is complete.
140  virtual void NotifyConsumer(
141      GetLoginsRequest* request,
142      const std::vector<webkit_glue::PasswordForm*> forms);
143
144 private:
145  // Called by NotifyConsumer, but runs in the consumer's thread.  Will not
146  // call the consumer if the request was canceled.  This extra layer is here so
147  // that PasswordStoreConsumer doesn't have to be reference counted (we assume
148  // consumers will cancel their requests before they are destroyed).
149  void NotifyConsumerImpl(PasswordStoreConsumer* consumer, int handle,
150                          const std::vector<webkit_glue::PasswordForm*> forms);
151
152  // Returns a new request handle tracked in pending_requests_.
153  int GetNewRequestHandle();
154
155  // Next handle to return from Get*Logins() to allow callers to track
156  // their request.
157  int handle_;
158
159  // List of pending request handles.  Handles are removed from the set when
160  // they finish or are canceled.
161  std::set<int> pending_requests_;
162
163  DISALLOW_COPY_AND_ASSIGN(PasswordStore);
164};
165
166#endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
167