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

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: use non-zero tests until http://crbug.com/77650 is addressed. 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/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/observer_list.h" 13 #include "base/observer_list.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 "webkit/glue/password_form.h" 16 #include "content/browser/cancelable_request.h"
17 17
18 class Profile; 18 class PasswordStoreConsumer;
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 class PasswordStoreConsumer { 27 namespace webkit_glue {
28 public: 28 struct PasswordForm;
29 virtual ~PasswordStoreConsumer() {}
30 // Call this when the request is finished. If there are no results, call it
31 // anyway with an empty vector.
32 virtual void OnPasswordStoreRequestDone(
33 int handle, const std::vector<webkit_glue::PasswordForm*>& result) = 0;
34 }; 29 };
35 30
36 // Interface for storing form passwords in a platform-specific secure way. 31 // Interface for storing form passwords in a platform-specific secure way.
37 // The login request/manipulation API is not threadsafe and must be used 32 // The login request/manipulation API is not threadsafe and must be used
38 // from the UI thread. 33 // from the UI thread.
39 class PasswordStore : public base::RefCountedThreadSafe<PasswordStore> { 34 class PasswordStore
35 : public base::RefCountedThreadSafe<PasswordStore>,
36 public CancelableRequestProvider {
40 public: 37 public:
38 typedef Callback2<Handle,
39 const std::vector<webkit_glue::PasswordForm*>&>::Type
40 GetLoginsCallback;
41
42 // PasswordForm vector elements are meant to be owned by the
43 // PasswordStoreConsumer. However, if the request is canceled after the
44 // allocation, then the request must take care of the deletion.
45 // TODO(scr) If we can convert vector<PasswordForm*> to
46 // ScopedVector<PasswordForm>, then we can move the following class to merely
47 // a typedef. At the moment, a subclass of CancelableRequest1 is required to
48 // provide a destructor, which cleans up after canceled requests by deleting
49 // vector elements.
50 class GetLoginsRequest : public CancelableRequest1<
51 GetLoginsCallback, std::vector<webkit_glue::PasswordForm*> > {
52 public:
53 explicit GetLoginsRequest(GetLoginsCallback* callback);
54 virtual ~GetLoginsRequest();
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(GetLoginsRequest);
58 };
59
41 // An interface used to notify clients (observers) of this object that data in 60 // An interface used to notify clients (observers) of this object that data in
42 // the password store has changed. Register the observer via 61 // the password store has changed. Register the observer via
43 // PasswordStore::SetObserver. 62 // PasswordStore::SetObserver.
44 class Observer { 63 class Observer {
45 public: 64 public:
46 // Notifies the observer that password data changed in some way. 65 // Notifies the observer that password data changed in some way.
47 virtual void OnLoginsChanged() = 0; 66 virtual void OnLoginsChanged() = 0;
48 67
49 protected: 68 protected:
50 virtual ~Observer() {} 69 virtual ~Observer() {}
(...skipping 11 matching lines...) Expand all
62 void UpdateLogin(const webkit_glue::PasswordForm& form); 81 void UpdateLogin(const webkit_glue::PasswordForm& form);
63 82
64 // Removes the matching PasswordForm from the secure password store (async). 83 // Removes the matching PasswordForm from the secure password store (async).
65 void RemoveLogin(const webkit_glue::PasswordForm& form); 84 void RemoveLogin(const webkit_glue::PasswordForm& form);
66 85
67 // Removes all logins created in the given date range. 86 // Removes all logins created in the given date range.
68 void RemoveLoginsCreatedBetween(const base::Time& delete_begin, 87 void RemoveLoginsCreatedBetween(const base::Time& delete_begin,
69 const base::Time& delete_end); 88 const base::Time& delete_end);
70 89
71 // Searches for a matching PasswordForm and returns a handle so the async 90 // Searches for a matching PasswordForm and returns a handle so the async
72 // request can be tracked. Implement the PasswordStoreConsumer interface to 91 // request can be tracked. Implement the PasswordStoreConsumer interface to be
73 // be notified on completion. 92 // notified on completion.
74 virtual int GetLogins(const webkit_glue::PasswordForm& form, 93 virtual Handle GetLogins(const webkit_glue::PasswordForm& form,
75 PasswordStoreConsumer* consumer); 94 PasswordStoreConsumer* consumer);
76 95
77 // Gets the complete list of PasswordForms that are not blacklist entries--and 96 // 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 97 // are thus auto-fillable--and returns a handle so the async request can be
79 // tracked. Implement the PasswordStoreConsumer interface to be notified 98 // tracked. Implement the PasswordStoreConsumer interface to be notified on
80 // on completion. 99 // completion.
81 int GetAutofillableLogins(PasswordStoreConsumer* consumer); 100 Handle GetAutofillableLogins(PasswordStoreConsumer* consumer);
82 101
83 // Gets the complete list of PasswordForms that are blacklist entries, and 102 // Gets the complete list of PasswordForms that are blacklist entries, and
84 // returns a handle so the async request can be tracked. Implement the 103 // returns a handle so the async request can be tracked. Implement the
85 // PasswordStoreConsumer interface to be notified on completion. 104 // PasswordStoreConsumer interface to be notified on completion.
86 int GetBlacklistLogins(PasswordStoreConsumer* consumer); 105 Handle GetBlacklistLogins(PasswordStoreConsumer* consumer);
87
88 // Cancels a previous Get*Logins query (async)
89 void CancelLoginsQuery(int handle);
90 106
91 // Reports usage metrics for the database. 107 // Reports usage metrics for the database.
92 void ReportMetrics(); 108 void ReportMetrics();
93 109
94 // Adds an observer to be notified when the password store data changes. 110 // Adds an observer to be notified when the password store data changes.
95 void AddObserver(Observer* observer); 111 void AddObserver(Observer* observer);
96 112
97 // Removes |observer| from the observer list. 113 // Removes |observer| from the observer list.
98 void RemoveObserver(Observer* observer); 114 void RemoveObserver(Observer* observer);
99 115
100 protected: 116 protected:
101 friend class base::RefCountedThreadSafe<PasswordStore>; 117 friend class base::RefCountedThreadSafe<PasswordStore>;
102 friend class browser_sync::PasswordDataTypeController; 118 friend class browser_sync::PasswordDataTypeController;
103 friend class browser_sync::PasswordModelAssociator; 119 friend class browser_sync::PasswordModelAssociator;
104 friend class browser_sync::PasswordModelWorker; 120 friend class browser_sync::PasswordModelWorker;
105 friend class LivePasswordsSyncTest; 121 friend class LivePasswordsSyncTest;
106 122
107 virtual ~PasswordStore(); 123 virtual ~PasswordStore();
108 124
109 // Simple container class that represents a login lookup request. 125 // 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); 126 virtual void ScheduleTask(Task* task);
129 127
130 // These will be run in PasswordStore's own thread. 128 // These will be run in PasswordStore's own thread.
131 // Synchronous implementation that reports usage metrics. 129 // Synchronous implementation that reports usage metrics.
132 virtual void ReportMetricsImpl() = 0; 130 virtual void ReportMetricsImpl() = 0;
133 // Synchronous implementation to add the given login. 131 // Synchronous implementation to add the given login.
134 virtual void AddLoginImpl(const webkit_glue::PasswordForm& form) = 0; 132 virtual void AddLoginImpl(const webkit_glue::PasswordForm& form) = 0;
135 // Synchronous implementation to update the given login. 133 // Synchronous implementation to update the given login.
136 virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form) = 0; 134 virtual void UpdateLoginImpl(const webkit_glue::PasswordForm& form) = 0;
137 // Synchronous implementation to remove the given login. 135 // Synchronous implementation to remove the given login.
(...skipping 11 matching lines...) Expand all
149 // Finds all blacklist PasswordForms, and notifies the consumer. 147 // Finds all blacklist PasswordForms, and notifies the consumer.
150 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0; 148 virtual void GetBlacklistLoginsImpl(GetLoginsRequest* request) = 0;
151 149
152 // Finds all non-blacklist PasswordForms, and fills the vector. 150 // Finds all non-blacklist PasswordForms, and fills the vector.
153 virtual bool FillAutofillableLogins( 151 virtual bool FillAutofillableLogins(
154 std::vector<webkit_glue::PasswordForm*>* forms) = 0; 152 std::vector<webkit_glue::PasswordForm*>* forms) = 0;
155 // Finds all blacklist PasswordForms, and fills the vector. 153 // Finds all blacklist PasswordForms, and fills the vector.
156 virtual bool FillBlacklistLogins( 154 virtual bool FillBlacklistLogins(
157 std::vector<webkit_glue::PasswordForm*>* forms) = 0; 155 std::vector<webkit_glue::PasswordForm*>* forms) = 0;
158 156
159 // Notifies the consumer that a Get*Logins() request is complete. 157 // Dispatches the result to the PasswordStoreConsumer on the original caller's
160 virtual void NotifyConsumer( 158 // thread so the callback can be executed there. This should be the UI
161 GetLoginsRequest* request, 159 // thread.
162 const std::vector<webkit_glue::PasswordForm*>& forms); 160 virtual void ForwardLoginsResult(GetLoginsRequest* request);
161
162 // Schedule the given |func| to be run in the PasswordStore's own thread with
163 // responses delivered to |consumer| on the current thread.
164 template<typename BackendFunc>
165 Handle Schedule(BackendFunc func, PasswordStoreConsumer* consumer);
166
167 // Schedule the given |func| to be run in the PasswordStore's own thread with
168 // argument |a| and responses delivered to |consumer| on the current thread.
169 template<typename BackendFunc, typename ArgA>
170 Handle Schedule(BackendFunc func, PasswordStoreConsumer* consumer,
171 const ArgA& a);
163 172
164 private: 173 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 174 // 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 175 // 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 176 // to notify observers that the password store may have been modified via
178 // NotifyLoginsChanged(). Note that there is no guarantee that the called 177 // NotifyLoginsChanged(). Note that there is no guarantee that the called
179 // method will actually modify the password store data. |task| may not be 178 // method will actually modify the password store data. |task| may not be
180 // NULL. This method owns and will delete |task|. 179 // NULL. This method owns and will delete |task|.
181 void WrapModificationTask(Task* task); 180 void WrapModificationTask(Task* task);
182 181
183 // Called by WrapModificationTask() once the underlying data-modifying 182 // Called by WrapModificationTask() once the underlying data-modifying
184 // operation has been performed. Notifies observers that password store data 183 // operation has been performed. Notifies observers that password store data
185 // may have been changed. 184 // may have been changed.
186 void NotifyLoginsChanged(); 185 void NotifyLoginsChanged();
187 186
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. 187 // The observers.
197 ObserverList<Observer> observers_; 188 ObserverList<Observer> observers_;
198 189
199 DISALLOW_COPY_AND_ASSIGN(PasswordStore); 190 DISALLOW_COPY_AND_ASSIGN(PasswordStore);
200 }; 191 };
201 192
202 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_ 193 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_STORE_H_
OLDNEW
« no previous file with comments | « chrome/browser/password_manager/password_form_manager.cc ('k') | chrome/browser/password_manager/password_store.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698