Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / password_manager / password_store.h
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 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
7
8 #include <vector>
9
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"
19
20 class PasswordStore;
21 class PasswordStoreConsumer;
22 class PasswordSyncableService;
23 class Task;
24
25 namespace autofill {
26 struct PasswordForm;
27 }
28
29 namespace browser_sync {
30 class PasswordChangeProcessor;
31 class PasswordDataTypeController;
32 class PasswordModelAssociator;
33 class PasswordModelWorker;
34 }
35
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);
40 }
41
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 {
46  public:
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 {
50     ALLOW_PROMPT,
51     DISALLOW_PROMPT
52   };
53
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 {
58    public:
59     explicit GetLoginsRequest(PasswordStoreConsumer* consumer);
60     virtual ~GetLoginsRequest();
61
62     void set_ignore_logins_cutoff(const base::Time& cutoff) {
63       ignore_logins_cutoff_ = cutoff;
64     }
65
66     // Removes any logins in the result list that were saved before the cutoff.
67     void ApplyIgnoreLoginsCutoff();
68
69     // Forward the result to the consumer on the original message loop.
70     void ForwardResult();
71
72     std::vector<autofill::PasswordForm*>* result() const {
73       return result_.get();
74     }
75
76    private:
77     // See GetLogins(). Logins older than this will be removed from the reply.
78     base::Time ignore_logins_cutoff_;
79
80     base::WeakPtr<PasswordStoreConsumer> consumer_weak_;
81
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
85     // backend methods.
86     scoped_ptr< std::vector<autofill::PasswordForm*> > result_;
87
88     base::ThreadChecker thread_checker_;
89     scoped_refptr<base::MessageLoopProxy> origin_loop_;
90
91     DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
92   };
93
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.
97   class Observer {
98    public:
99     // Notifies the observer that password data changed in some way.
100     virtual void OnLoginsChanged() = 0;
101
102    protected:
103     virtual ~Observer() {}
104   };
105
106   PasswordStore();
107
108   // Reimplement this to add custom initialization. Always call this too.
109   virtual bool Init();
110
111   // Adds the given PasswordForm to the secure password store asynchronously.
112   virtual void AddLogin(const autofill::PasswordForm& form);
113
114   // Updates the matching PasswordForm in the secure password store (async).
115   void UpdateLogin(const autofill::PasswordForm& form);
116
117   // Removes the matching PasswordForm from the secure password store (async).
118   void RemoveLogin(const autofill::PasswordForm& form);
119
120   // Removes all logins created in the given date range.
121   void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
122                                   const base::Time& delete_end);
123
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);
135
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);
140
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);
145
146   // Reports usage metrics for the database.
147   void ReportMetrics();
148
149   // Adds an observer to be notified when the password store data changes.
150   void AddObserver(Observer* observer);
151
152   // Removes |observer| from the observer list.
153   void RemoveObserver(Observer* observer);
154
155  protected:
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&);
172
173   virtual ~PasswordStore();
174
175   // Schedules the given |task| to be run on the PasswordStore's TaskRunner.
176   bool ScheduleTask(const base::Closure& task);
177
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();
182
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;
195
196   typedef base::Callback<void(const std::vector<autofill::PasswordForm*>&)>
197       ConsumerCallbackRunner;  // Owns all PasswordForms in the vector.
198
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;
206
207   // Finds all non-blacklist PasswordForms, and notifies the consumer.
208   virtual void GetAutofillableLoginsImpl(GetLoginsRequest* request) = 0;
209
210   // Finds all blacklist PasswordForms, and notifies the consumer.
211   virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
212
213   // Finds all non-blacklist PasswordForms, and fills the vector.
214   virtual bool FillAutofillableLogins(
215       std::vector<autofill::PasswordForm*>* forms) = 0;
216
217   // Finds all blacklist PasswordForms, and fills the vector.
218   virtual bool FillBlacklistLogins(
219       std::vector<autofill::PasswordForm*>* forms) = 0;
220
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);
224
225   // Log UMA stats for number of bulk deletions.
226   void LogStatsForBulkDeletion(int num_deletions);
227
228  private:
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);
233
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);
240
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
244   // store.
245   void PostNotifyLoginsChanged();
246
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();
251
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);
259
260   // The observers.
261   ObserverList<Observer> observers_;
262
263   DISALLOW_COPY_AND_ASSIGN(PasswordStore);
264 };
265
266 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_