Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(175)

Side by Side Diff: chrome/browser/password_manager/password_store.h

Issue 6646051: Fix DCHECK, memory leak, and refactor PasswordStore to use CancelableRequest (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix ~ListPopulator ordering. Created 9 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_ 5 #ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_ 6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <set>
10 #include <vector> 9 #include <vector>
11 10
11 #include "base/callback.h"
12 #include "base/observer_list.h" 12 #include "base/observer_list.h"
13 #include "base/ref_counted.h" 13 #include "base/ref_counted.h"
14 #include "base/threading/thread.h" 14 #include "base/threading/thread.h"
15 #include "base/time.h" 15 #include "base/time.h"
16 #include "content/browser/cancelable_request.h"
16 #include "webkit/glue/password_form.h" 17 #include "webkit/glue/password_form.h"
17 18
18 class Profile;
19 class Task; 19 class Task;
20 20
21 namespace browser_sync { 21 namespace browser_sync {
22 class PasswordDataTypeController; 22 class PasswordDataTypeController;
23 class PasswordModelAssociator; 23 class PasswordModelAssociator;
24 class PasswordModelWorker; 24 class PasswordModelWorker;
25 }; 25 };
26 26
27 // Reads from the PasswordStore are done asynchrously on a separate
28 // thread. PasswordStoreConsumer provides the virtual callback method, which is
29 // guaranteed to be executed on this (the UI) thread. It also provides the
30 // CancelableRequestConsumer member, which cancels any outstanding requests upon
31 // destruction.
27 class PasswordStoreConsumer { 32 class PasswordStoreConsumer {
28 public: 33 public:
29 virtual ~PasswordStoreConsumer() {}
30 // Call this when the request is finished. If there are no results, call it 34 // Call this when the request is finished. If there are no results, call it
31 // anyway with an empty vector. 35 // anyway with an empty vector.
32 virtual void OnPasswordStoreRequestDone( 36 virtual void OnPasswordStoreRequestDone(
33 int handle, const std::vector<webkit_glue::PasswordForm*>& result) = 0; 37 CancelableRequestProvider::Handle handle,
38 const std::vector<webkit_glue::PasswordForm*>& result) = 0;
39
40 // The CancelableRequest framework needs both a callback (the
41 // PasswordStoreConsumer::OnPasswordStoreRequestDone) as well as a
42 // CancelableRequestConsumer. This accessor makes the API simpler for callers
43 // as PasswordStore can get both from the PasswordStoreConsumer.
44 CancelableRequestConsumerBase* cancelable_consumer() {
45 return &cancelable_consumer_;
46 }
47
48 protected:
49 virtual ~PasswordStoreConsumer();
50
51 private:
52 CancelableRequestConsumer cancelable_consumer_;
34 }; 53 };
35 54
36 // Interface for storing form passwords in a platform-specific secure way. 55 // Interface for storing form passwords in a platform-specific secure way.
37 // The login request/manipulation API is not threadsafe and must be used 56 // The login request/manipulation API is not threadsafe and must be used
38 // from the UI thread. 57 // from the UI thread.
39 class PasswordStore : public base::RefCountedThreadSafe<PasswordStore> { 58 class PasswordStore
59 : public base::RefCountedThreadSafe<PasswordStore>,
60 public CancelableRequestProvider {
40 public: 61 public:
62 static const Handle kInvalidHandle;
James Hawkins 2011/03/21 17:53:11 nit: Document this constant.
James Hawkins 2011/03/21 17:53:11 I'm not going to ask you to change this because it
Sheridan Rawlins 2011/03/21 18:36:42 I created a factory method in CancelableRequestPro
63
64 typedef Callback2<Handle,
65 const std::vector<webkit_glue::PasswordForm*>&>::Type
66 GetLoginsCallback;
67
68 // PasswordForm vector elements are meant to be owned by the
69 // PasswordStoreConsumer. However, if the request is canceled after the
70 // allocation, then the request must take care of the deletion.
71 // TODO(scr) If we can convert vector<PasswordForm*> to
72 // ScopedVector<PasswordForm>, then we can move the following class to merely
73 // a typedef. At the moment, a subclass of CancelableRequest1 is required to
74 // provide a destructor, which cleans up after canceled requests by deleting
75 // vector elements.
76 class GetLoginsRequest : public CancelableRequest1<
77 GetLoginsCallback, std::vector<webkit_glue::PasswordForm*> > {
78 public:
79 explicit GetLoginsRequest(GetLoginsCallback* callback);
80 virtual ~GetLoginsRequest();
81
82 private:
83 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
84 };
85
41 // An interface used to notify clients (observers) of this object that data in 86 // An interface used to notify clients (observers) of this object that data in
42 // the password store has changed. Register the observer via 87 // the password store has changed. Register the observer via
43 // PasswordStore::SetObserver. 88 // PasswordStore::SetObserver.
44 class Observer { 89 class Observer {
45 public: 90 public:
46 // Notifies the observer that password data changed in some way. 91 // Notifies the observer that password data changed in some way.
47 virtual void OnLoginsChanged() = 0; 92 virtual void OnLoginsChanged() = 0;
48 93
49 protected: 94 protected:
50 virtual ~Observer() {} 95 virtual ~Observer() {}
(...skipping 11 matching lines...) Expand all
62 void UpdateLogin(const webkit_glue::PasswordForm& form); 107 void UpdateLogin(const webkit_glue::PasswordForm& form);
63 108
64 // Removes the matching PasswordForm from the secure password store (async). 109 // Removes the matching PasswordForm from the secure password store (async).
65 void RemoveLogin(const webkit_glue::PasswordForm& form); 110 void RemoveLogin(const webkit_glue::PasswordForm& form);
66 111
67 // Removes all logins created in the given date range. 112 // Removes all logins created in the given date range.
68 void RemoveLoginsCreatedBetween(const base::Time& delete_begin, 113 void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
69 const base::Time& delete_end); 114 const base::Time& delete_end);
70 115
71 // Searches for a matching PasswordForm and returns a handle so the async 116 // Searches for a matching PasswordForm and returns a handle so the async
72 // request can be tracked. Implement the PasswordStoreConsumer interface to 117 // request can be tracked. Implement the Consumer interface to be
James Hawkins 2011/03/21 17:53:11 s/Consumer/PasswordStoreConsumer/
Sheridan Rawlins 2011/03/21 18:36:42 Done.
73 // be notified on completion. 118 // notified on completion.
74 virtual int GetLogins(const webkit_glue::PasswordForm& form, 119 virtual Handle GetLogins(const webkit_glue::PasswordForm& form,
75 PasswordStoreConsumer* consumer); 120 PasswordStoreConsumer* consumer);
76 121
77 // Gets the complete list of PasswordForms that are not blacklist entries--and 122 // Gets the complete list of PasswordForms that are not blacklist entries--and
78 // are thus auto-fillable--and returns a handle so the async request can be 123 // are thus auto-fillable--and returns a handle so the async request can be
79 // tracked. Implement the PasswordStoreConsumer interface to be notified 124 // tracked. Implement the PasswordStoreConsumer interface to be notified on
80 // on completion. 125 // completion.
81 int GetAutofillableLogins(PasswordStoreConsumer* consumer); 126 Handle GetAutofillableLogins(PasswordStoreConsumer* consumer);
82 127
83 // Gets the complete list of PasswordForms that are blacklist entries, and 128 // Gets the complete list of PasswordForms that are blacklist entries, and
84 // returns a handle so the async request can be tracked. Implement the 129 // returns a handle so the async request can be tracked. Implement the
85 // PasswordStoreConsumer interface to be notified on completion. 130 // PasswordStoreConsumer interface to be notified on completion.
86 int GetBlacklistLogins(PasswordStoreConsumer* consumer); 131 Handle GetBlacklistLogins(PasswordStoreConsumer* consumer);
87
88 // Cancels a previous Get*Logins query (async)
89 void CancelLoginsQuery(int handle);
90 132
91 // Reports usage metrics for the database. 133 // Reports usage metrics for the database.
92 void ReportMetrics(); 134 void ReportMetrics();
93 135
94 // Adds an observer to be notified when the password store data changes. 136 // Adds an observer to be notified when the password store data changes.
95 void AddObserver(Observer* observer); 137 void AddObserver(Observer* observer);
96 138
97 // Removes |observer| from the observer list. 139 // Removes |observer| from the observer list.
98 void RemoveObserver(Observer* observer); 140 void RemoveObserver(Observer* observer);
99 141
142 // Since Handle is supposed to be opaque, provide validity method.
143 static bool handle_is_valid(Handle handle) {
James Hawkins 2011/03/21 17:53:11 This is not an accessor method; as such, the metho
Sheridan Rawlins 2011/03/21 18:36:42 Done.
144 return handle != kInvalidHandle;
145 }
146
100 protected: 147 protected:
101 friend class base::RefCountedThreadSafe<PasswordStore>; 148 friend class base::RefCountedThreadSafe<PasswordStore>;
102 friend class browser_sync::PasswordDataTypeController; 149 friend class browser_sync::PasswordDataTypeController;
103 friend class browser_sync::PasswordModelAssociator; 150 friend class browser_sync::PasswordModelAssociator;
104 friend class browser_sync::PasswordModelWorker; 151 friend class browser_sync::PasswordModelWorker;
105 friend class LivePasswordsSyncTest; 152 friend class LivePasswordsSyncTest;
106 153
107 virtual ~PasswordStore(); 154 virtual ~PasswordStore();
108 155
109 // Simple container class that represents a login lookup request. 156 // Schedule the given |task| to be run in the PasswordStore's own thread.
110 class GetLoginsRequest {
111 public:
112 GetLoginsRequest(PasswordStoreConsumer* c,
113 int handle);
114
115 // The consumer to notify when this request is complete.
116 PasswordStoreConsumer* consumer;
117 // A unique handle for the request
118 int handle;
119 // The message loop that the request was made from. We send the result
120 // back to the consumer in this same message loop.
121 MessageLoop* message_loop;
122
123 private:
124 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
125 };
126
127 // Schedule the given task to be run in the PasswordStore's own thread.
128 virtual void ScheduleTask(Task* task); 157 virtual void ScheduleTask(Task* task);
129 158
130 // These will be run in PasswordStore's own thread. 159 // These will be run in PasswordStore's own thread.
131 // Synchronous implementation that reports usage metrics. 160 // Synchronous implementation that reports usage metrics.
132 virtual void ReportMetricsImpl() = 0; 161 virtual void ReportMetricsImpl() = 0;
133 // Synchronous implementation to add the given login. 162 // Synchronous implementation to add the given login.
134 virtual void AddLoginImpl(const webkit_glue::PasswordForm& form) = 0; 163 virtual void AddLoginImpl(const webkit_glue::PasswordForm& form) = 0;
135 // Synchronous implementation to update the given login. 164 // Synchronous implementation to update the given login.
136 virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form) = 0; 165 virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form) = 0;
137 // Synchronous implementation to remove the given login. 166 // Synchronous implementation to remove the given login.
(...skipping 11 matching lines...) Expand all
149 // Finds all blacklist PasswordForms, and notifies the consumer. 178 // Finds all blacklist PasswordForms, and notifies the consumer.
150 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0; 179 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
151 180
152 // Finds all non-blacklist PasswordForms, and fills the vector. 181 // Finds all non-blacklist PasswordForms, and fills the vector.
153 virtual bool FillAutofillableLogins( 182 virtual bool FillAutofillableLogins(
154 std::vector<webkit_glue::PasswordForm*>* forms) = 0; 183 std::vector<webkit_glue::PasswordForm*>* forms) = 0;
155 // Finds all blacklist PasswordForms, and fills the vector. 184 // Finds all blacklist PasswordForms, and fills the vector.
156 virtual bool FillBlacklistLogins( 185 virtual bool FillBlacklistLogins(
157 std::vector<webkit_glue::PasswordForm*>* forms) = 0; 186 std::vector<webkit_glue::PasswordForm*>* forms) = 0;
158 187
159 // Notifies the consumer that a Get*Logins() request is complete. 188 // Dispatches the result to the PasswordStoreConsumer on the original caller's
160 virtual void NotifyConsumer( 189 // thread so the callback can be executed there. This should be the UI
161 GetLoginsRequest* request, 190 // thread.
162 const std::vector<webkit_glue::PasswordForm*>& forms); 191 virtual void ForwardLoginsResult(GetLoginsRequest* request);
192
193 // Schedule the given |func| to be run in the PasswordStore's own thread with
194 // responses delivered to |consumer| on the current thread.
195 template<typename BackendFunc>
196 Handle Schedule(BackendFunc func, PasswordStoreConsumer* consumer);
197
198 // Schedule the given |func| to be run in the PasswordStore's own thread with
199 // argument |a| and responses delivered to |consumer| on the current thread.
200 template<typename BackendFunc, typename ArgA>
201 Handle Schedule(BackendFunc func, PasswordStoreConsumer* consumer,
202 const ArgA& a);
163 203
164 private: 204 private:
165 // Called by NotifyConsumer, but runs in the consumer's thread. Will not
166 // call the consumer if the request was canceled. This extra layer is here so
167 // that PasswordStoreConsumer doesn't have to be reference counted (we assume
168 // consumers will cancel their requests before they are destroyed).
169 void NotifyConsumerImpl(PasswordStoreConsumer* consumer, int handle,
170 const std::vector<webkit_glue::PasswordForm*>& forms);
171
172 // Returns a new request handle tracked in pending_requests_.
173 int GetNewRequestHandle();
174
175 // Wrapper method called on the destination thread (DB for non-mac) that calls 205 // Wrapper method called on the destination thread (DB for non-mac) that calls
176 // the method specified in |task| and then calls back into the source thread 206 // the method specified in |task| and then calls back into the source thread
177 // to notify observers that the password store may have been modified via 207 // to notify observers that the password store may have been modified via
178 // NotifyLoginsChanged(). Note that there is no guarantee that the called 208 // NotifyLoginsChanged(). Note that there is no guarantee that the called
179 // method will actually modify the password store data. |task| may not be 209 // method will actually modify the password store data. |task| may not be
180 // NULL. This method owns and will delete |task|. 210 // NULL. This method owns and will delete |task|.
181 void WrapModificationTask(Task* task); 211 void WrapModificationTask(Task* task);
182 212
183 // Called by WrapModificationTask() once the underlying data-modifying 213 // Called by WrapModificationTask() once the underlying data-modifying
184 // operation has been performed. Notifies observers that password store data 214 // operation has been performed. Notifies observers that password store data
185 // may have been changed. 215 // may have been changed.
186 void NotifyLoginsChanged(); 216 void NotifyLoginsChanged();
187 217
188 // Next handle to return from Get*Logins() to allow callers to track
189 // their request.
190 int handle_;
191
192 // List of pending request handles. Handles are removed from the set when
193 // they finish or are canceled.
194 std::set<int> pending_requests_;
195
196 // The observers. 218 // The observers.
197 ObserverList<Observer> observers_; 219 ObserverList<Observer> observers_;
198 220
199 DISALLOW_COPY_AND_ASSIGN(PasswordStore); 221 DISALLOW_COPY_AND_ASSIGN(PasswordStore);
200 }; 222 };
201 223
202 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_ 224 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698