OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/password_manager/password_store_x.h" | 5 #include "chrome/browser/password_manager/password_store_x.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "chrome/browser/chrome_notification_types.h" | 15 #include "chrome/browser/chrome_notification_types.h" |
16 #include "components/password_manager/core/browser/password_store_change.h" | 16 #include "components/password_manager/core/browser/password_store_change.h" |
17 #include "components/password_manager/core/common/password_manager_pref_names.h" | 17 #include "components/password_manager/core/common/password_manager_pref_names.h" |
18 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/notification_service.h" | 20 #include "content/public/browser/notification_service.h" |
21 | 21 |
22 using autofill::PasswordForm; | 22 using autofill::PasswordForm; |
23 using content::BrowserThread; | 23 using content::BrowserThread; |
24 using password_manager::PasswordStoreChange; | 24 using password_manager::PasswordStoreChange; |
25 using password_manager::PasswordStoreChangeList; | 25 using password_manager::PasswordStoreChangeList; |
26 using password_manager::PasswordStoreDefault; | 26 using password_manager::PasswordStoreDefault; |
27 using std::vector; | |
28 | 27 |
29 namespace { | 28 namespace { |
30 | 29 |
31 bool AddLoginToBackend(const scoped_ptr<PasswordStoreX::NativeBackend>& backend, | 30 bool AddLoginToBackend(const scoped_ptr<PasswordStoreX::NativeBackend>& backend, |
32 const PasswordForm& form, | 31 const PasswordForm& form, |
33 PasswordStoreChangeList* changes) { | 32 PasswordStoreChangeList* changes) { |
34 *changes = backend->AddLogin(form); | 33 *changes = backend->AddLogin(form); |
35 return (!changes->empty() && | 34 return (!changes->empty() && |
36 changes->back().type() == PasswordStoreChange::ADD); | 35 changes->back().type() == PasswordStoreChange::ADD); |
37 } | 36 } |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 } | 122 } |
124 | 123 |
125 namespace { | 124 namespace { |
126 struct LoginLessThan { | 125 struct LoginLessThan { |
127 bool operator()(const PasswordForm* a, const PasswordForm* b) { | 126 bool operator()(const PasswordForm* a, const PasswordForm* b) { |
128 return a->origin < b->origin; | 127 return a->origin < b->origin; |
129 } | 128 } |
130 }; | 129 }; |
131 } // anonymous namespace | 130 } // anonymous namespace |
132 | 131 |
133 void PasswordStoreX::SortLoginsByOrigin(NativeBackend::PasswordFormList* list) { | 132 void PasswordStoreX::SortLoginsByOrigin( |
| 133 std::vector<autofill::PasswordForm*>* list) { |
134 // In login_database.cc, the query has ORDER BY origin_url. Simulate that. | 134 // In login_database.cc, the query has ORDER BY origin_url. Simulate that. |
135 std::sort(list->begin(), list->end(), LoginLessThan()); | 135 std::sort(list->begin(), list->end(), LoginLessThan()); |
136 } | 136 } |
137 | 137 |
138 void PasswordStoreX::GetLoginsImpl( | 138 void PasswordStoreX::GetLoginsImpl( |
139 const autofill::PasswordForm& form, | 139 const autofill::PasswordForm& form, |
140 AuthorizationPromptPolicy prompt_policy, | 140 AuthorizationPromptPolicy prompt_policy, |
141 const ConsumerCallbackRunner& callback_runner) { | 141 const ConsumerCallbackRunner& callback_runner) { |
142 CheckMigration(); | 142 CheckMigration(); |
143 std::vector<autofill::PasswordForm*> matched_forms; | 143 ScopedVector<autofill::PasswordForm> matched_forms; |
144 if (use_native_backend() && backend_->GetLogins(form, &matched_forms)) { | 144 if (use_native_backend() && backend_->GetLogins(form, &matched_forms)) { |
145 SortLoginsByOrigin(&matched_forms); | 145 SortLoginsByOrigin(&matched_forms.get()); |
146 // The native backend may succeed and return no data even while locked, if | 146 // The native backend may succeed and return no data even while locked, if |
147 // the query did not match anything stored. So we continue to allow fallback | 147 // the query did not match anything stored. So we continue to allow fallback |
148 // until we perform a write operation, or until a read returns actual data. | 148 // until we perform a write operation, or until a read returns actual data. |
149 if (matched_forms.size() > 0) | 149 if (matched_forms.size() > 0) |
150 allow_fallback_ = false; | 150 allow_fallback_ = false; |
151 } else if (allow_default_store()) { | 151 } else if (allow_default_store()) { |
152 DCHECK(matched_forms.empty()); | 152 DCHECK(matched_forms.empty()); |
153 PasswordStoreDefault::GetLoginsImpl(form, prompt_policy, callback_runner); | 153 PasswordStoreDefault::GetLoginsImpl(form, prompt_policy, callback_runner); |
154 return; | 154 return; |
155 } | 155 } |
156 // The consumer will be left hanging unless we reply. | 156 // The consumer will be left hanging unless we reply. |
157 callback_runner.Run(matched_forms); | 157 callback_runner.Run(matched_forms.Pass()); |
158 } | 158 } |
159 | 159 |
160 void PasswordStoreX::GetAutofillableLoginsImpl(GetLoginsRequest* request) { | 160 void PasswordStoreX::GetAutofillableLoginsImpl(GetLoginsRequest* request) { |
161 CheckMigration(); | 161 CheckMigration(); |
| 162 ScopedVector<autofill::PasswordForm> obtained_forms; |
162 if (use_native_backend() && | 163 if (use_native_backend() && |
163 backend_->GetAutofillableLogins(request->result())) { | 164 backend_->GetAutofillableLogins(&obtained_forms)) { |
| 165 request->result()->swap(obtained_forms.get()); |
164 SortLoginsByOrigin(request->result()); | 166 SortLoginsByOrigin(request->result()); |
165 // See GetLoginsImpl() for why we disallow fallback conditionally here. | 167 // See GetLoginsImpl() for why we disallow fallback conditionally here. |
166 if (request->result()->size() > 0) | 168 if (request->result()->size() > 0) |
167 allow_fallback_ = false; | 169 allow_fallback_ = false; |
168 } else if (allow_default_store()) { | 170 } else if (allow_default_store()) { |
169 PasswordStoreDefault::GetAutofillableLoginsImpl(request); | 171 PasswordStoreDefault::GetAutofillableLoginsImpl(request); |
170 return; | 172 return; |
171 } | 173 } |
172 // The consumer will be left hanging unless we reply. | 174 // The consumer will be left hanging unless we reply. |
173 ForwardLoginsResult(request); | 175 ForwardLoginsResult(request); |
174 } | 176 } |
175 | 177 |
176 void PasswordStoreX::GetBlacklistLoginsImpl(GetLoginsRequest* request) { | 178 void PasswordStoreX::GetBlacklistLoginsImpl(GetLoginsRequest* request) { |
177 CheckMigration(); | 179 CheckMigration(); |
178 if (use_native_backend() && | 180 ScopedVector<autofill::PasswordForm> obtained_forms; |
179 backend_->GetBlacklistLogins(request->result())) { | 181 if (use_native_backend() && backend_->GetBlacklistLogins(&obtained_forms)) { |
| 182 request->result()->swap(obtained_forms.get()); |
180 SortLoginsByOrigin(request->result()); | 183 SortLoginsByOrigin(request->result()); |
181 // See GetLoginsImpl() for why we disallow fallback conditionally here. | 184 // See GetLoginsImpl() for why we disallow fallback conditionally here. |
182 if (request->result()->size() > 0) | 185 if (request->result()->size() > 0) |
183 allow_fallback_ = false; | 186 allow_fallback_ = false; |
184 } else if (allow_default_store()) { | 187 } else if (allow_default_store()) { |
185 PasswordStoreDefault::GetBlacklistLoginsImpl(request); | 188 PasswordStoreDefault::GetBlacklistLoginsImpl(request); |
186 return; | 189 return; |
187 } | 190 } |
188 // The consumer will be left hanging unless we reply. | 191 // The consumer will be left hanging unless we reply. |
189 ForwardLoginsResult(request); | 192 ForwardLoginsResult(request); |
190 } | 193 } |
191 | 194 |
192 bool PasswordStoreX::FillAutofillableLogins(vector<PasswordForm*>* forms) { | 195 bool PasswordStoreX::FillAutofillableLogins( |
| 196 ScopedVector<autofill::PasswordForm>* forms) { |
193 CheckMigration(); | 197 CheckMigration(); |
194 if (use_native_backend() && backend_->GetAutofillableLogins(forms)) { | 198 if (use_native_backend() && backend_->GetAutofillableLogins(forms)) { |
195 // See GetLoginsImpl() for why we disallow fallback conditionally here. | 199 // See GetLoginsImpl() for why we disallow fallback conditionally here. |
196 if (forms->size() > 0) | 200 if (forms->size() > 0) |
197 allow_fallback_ = false; | 201 allow_fallback_ = false; |
198 return true; | 202 return true; |
199 } | 203 } |
200 if (allow_default_store()) | 204 if (allow_default_store()) |
201 return PasswordStoreDefault::FillAutofillableLogins(forms); | 205 return PasswordStoreDefault::FillAutofillableLogins(forms); |
202 return false; | 206 return false; |
203 } | 207 } |
204 | 208 |
205 bool PasswordStoreX::FillBlacklistLogins(vector<PasswordForm*>* forms) { | 209 bool PasswordStoreX::FillBlacklistLogins( |
| 210 ScopedVector<autofill::PasswordForm>* forms) { |
206 CheckMigration(); | 211 CheckMigration(); |
207 if (use_native_backend() && backend_->GetBlacklistLogins(forms)) { | 212 if (use_native_backend() && backend_->GetBlacklistLogins(forms)) { |
208 // See GetLoginsImpl() for why we disallow fallback conditionally here. | 213 // See GetLoginsImpl() for why we disallow fallback conditionally here. |
209 if (forms->size() > 0) | 214 if (forms->size() > 0) |
210 allow_fallback_ = false; | 215 allow_fallback_ = false; |
211 return true; | 216 return true; |
212 } | 217 } |
213 if (allow_default_store()) | 218 if (allow_default_store()) |
214 return PasswordStoreDefault::FillBlacklistLogins(forms); | 219 return PasswordStoreDefault::FillBlacklistLogins(forms); |
215 return false; | 220 return false; |
(...skipping 27 matching lines...) Expand all Loading... |
243 "Falling back on default (unencrypted) store."; | 248 "Falling back on default (unencrypted) store."; |
244 backend_.reset(NULL); | 249 backend_.reset(NULL); |
245 // Don't warn again. We'll use the default store because backend_ is NULL. | 250 // Don't warn again. We'll use the default store because backend_ is NULL. |
246 allow_fallback_ = false; | 251 allow_fallback_ = false; |
247 } | 252 } |
248 return !backend_.get(); | 253 return !backend_.get(); |
249 } | 254 } |
250 | 255 |
251 ssize_t PasswordStoreX::MigrateLogins() { | 256 ssize_t PasswordStoreX::MigrateLogins() { |
252 DCHECK(backend_.get()); | 257 DCHECK(backend_.get()); |
253 vector<PasswordForm*> forms; | 258 ScopedVector<autofill::PasswordForm> forms; |
254 bool ok = PasswordStoreDefault::FillAutofillableLogins(&forms) && | 259 bool ok = PasswordStoreDefault::FillAutofillableLogins(&forms) && |
255 PasswordStoreDefault::FillBlacklistLogins(&forms); | 260 PasswordStoreDefault::FillBlacklistLogins(&forms); |
256 if (ok) { | 261 if (ok) { |
257 // We add all the passwords (and blacklist entries) to the native backend | 262 // We add all the passwords (and blacklist entries) to the native backend |
258 // before attempting to remove any from the login database, to make sure we | 263 // before attempting to remove any from the login database, to make sure we |
259 // don't somehow end up with some of the passwords in one store and some in | 264 // don't somehow end up with some of the passwords in one store and some in |
260 // another. We'll always have at least one intact store this way. | 265 // another. We'll always have at least one intact store this way. |
261 for (size_t i = 0; i < forms.size(); ++i) { | 266 for (size_t i = 0; i < forms.size(); ++i) { |
262 PasswordStoreChangeList changes; | 267 PasswordStoreChangeList changes; |
263 if (!AddLoginToBackend(backend_, *forms[i], &changes)) { | 268 if (!AddLoginToBackend(backend_, *forms[i], &changes)) { |
(...skipping 12 matching lines...) Expand all Loading... |
276 PasswordStoreDefault::RemoveLoginImpl(*forms[i]); | 281 PasswordStoreDefault::RemoveLoginImpl(*forms[i]); |
277 } | 282 } |
278 // Finally, delete the database file itself. We remove the passwords from | 283 // Finally, delete the database file itself. We remove the passwords from |
279 // it before deleting the file just in case there is some problem deleting | 284 // it before deleting the file just in case there is some problem deleting |
280 // the file (e.g. directory is not writable, but file is), which would | 285 // the file (e.g. directory is not writable, but file is), which would |
281 // otherwise cause passwords to re-migrate next (or maybe every) time. | 286 // otherwise cause passwords to re-migrate next (or maybe every) time. |
282 DeleteAndRecreateDatabaseFile(); | 287 DeleteAndRecreateDatabaseFile(); |
283 } | 288 } |
284 } | 289 } |
285 ssize_t result = ok ? forms.size() : -1; | 290 ssize_t result = ok ? forms.size() : -1; |
286 STLDeleteElements(&forms); | |
287 return result; | 291 return result; |
288 } | 292 } |
OLD | NEW |