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

Side by Side Diff: components/password_manager/core/browser/login_database.cc

Issue 283563002: Password Login Database: report correct changes from AddLogin(). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed PasswordSyncableServiceTest.PasswordStoreChanges Created 6 years, 7 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/password_manager/core/browser/login_database.h" 5 #include "components/password_manager/core/browser/login_database.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <limits> 8 #include <limits>
9 9
10 #include "base/bind.h"
10 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
11 #include "base/logging.h" 12 #include "base/logging.h"
12 #include "base/metrics/histogram.h" 13 #include "base/metrics/histogram.h"
13 #include "base/pickle.h" 14 #include "base/pickle.h"
14 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
15 #include "base/time/time.h" 16 #include "base/time/time.h"
16 #include "components/autofill/core/common/password_form.h" 17 #include "components/autofill/core/common/password_form.h"
17 #include "sql/connection.h" 18 #include "sql/connection.h"
18 #include "sql/statement.h" 19 #include "sql/statement.h"
19 #include "sql/transaction.h" 20 #include "sql/transaction.h"
(...skipping 22 matching lines...) Expand all
42 COLUMN_DATE_CREATED, 43 COLUMN_DATE_CREATED,
43 COLUMN_BLACKLISTED_BY_USER, 44 COLUMN_BLACKLISTED_BY_USER,
44 COLUMN_SCHEME, 45 COLUMN_SCHEME,
45 COLUMN_PASSWORD_TYPE, 46 COLUMN_PASSWORD_TYPE,
46 COLUMN_POSSIBLE_USERNAMES, 47 COLUMN_POSSIBLE_USERNAMES,
47 COLUMN_TIMES_USED, 48 COLUMN_TIMES_USED,
48 COLUMN_FORM_DATA, 49 COLUMN_FORM_DATA,
49 COLUMN_USE_ADDITIONAL_AUTH 50 COLUMN_USE_ADDITIONAL_AUTH
50 }; 51 };
51 52
53 void BindAddStatement(const PasswordForm& form,
54 const std::string& encrypted_password,
55 const Pickle& usernames_pickle,
Garrett Casto 2014/05/14 07:46:35 Any reason why you have |username_pickle| as a sep
vasilii 2014/05/14 11:20:18 Done.
56 sql::Statement* s) {
57 s->BindString(COLUMN_ORIGIN_URL, form.origin.spec());
58 s->BindString(COLUMN_ACTION_URL, form.action.spec());
59 s->BindString16(COLUMN_USERNAME_ELEMENT, form.username_element);
60 s->BindString16(COLUMN_USERNAME_VALUE, form.username_value);
61 s->BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element);
62 s->BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(),
63 static_cast<int>(encrypted_password.length()));
64 s->BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element);
65 s->BindString(COLUMN_SIGNON_REALM, form.signon_realm);
66 s->BindInt(COLUMN_SSL_VALID, form.ssl_valid);
67 s->BindInt(COLUMN_PREFERRED, form.preferred);
68 s->BindInt64(COLUMN_DATE_CREATED, form.date_created.ToTimeT());
69 s->BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user);
70 s->BindInt(COLUMN_SCHEME, form.scheme);
71 s->BindInt(COLUMN_PASSWORD_TYPE, form.type);
72 s->BindBlob(COLUMN_POSSIBLE_USERNAMES,
73 usernames_pickle.data(),
74 usernames_pickle.size());
75 s->BindInt(COLUMN_TIMES_USED, form.times_used);
76 Pickle form_data_pickle;
77 autofill::SerializeFormData(form.form_data, &form_data_pickle);
78 s->BindBlob(COLUMN_FORM_DATA,
79 form_data_pickle.data(),
80 form_data_pickle.size());
81 s->BindInt(COLUMN_USE_ADDITIONAL_AUTH, form.use_additional_authentication);
82 }
83
84 void AddCallback(int err, sql::Statement* /*stmt*/) {
85 if (err == 19 /*SQLITE_CONSTRAINT*/)
86 DLOG(WARNING) << "LoginDatabase::AddLogin updated an existing form";
87 }
88
52 } // namespace 89 } // namespace
53 90
54 LoginDatabase::LoginDatabase() { 91 LoginDatabase::LoginDatabase() {
55 } 92 }
56 93
57 LoginDatabase::~LoginDatabase() { 94 LoginDatabase::~LoginDatabase() {
58 } 95 }
59 96
60 bool LoginDatabase::Init(const base::FilePath& db_path) { 97 bool LoginDatabase::Init(const base::FilePath& db_path) {
61 // Set pragmas for a small, private database (based on WebDatabase). 98 // Set pragmas for a small, private database (based on WebDatabase).
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
231 "PasswordManager.TimesGeneratedPasswordUsed", 268 "PasswordManager.TimesGeneratedPasswordUsed",
232 usage_statement.ColumnInt(1), 0, 100, 10); 269 usage_statement.ColumnInt(1), 0, 100, 10);
233 } else { 270 } else {
234 UMA_HISTOGRAM_CUSTOM_COUNTS( 271 UMA_HISTOGRAM_CUSTOM_COUNTS(
235 "PasswordManager.TimesPasswordUsed", 272 "PasswordManager.TimesPasswordUsed",
236 usage_statement.ColumnInt(1), 0, 100, 10); 273 usage_statement.ColumnInt(1), 0, 100, 10);
237 } 274 }
238 } 275 }
239 } 276 }
240 277
241 bool LoginDatabase::AddLogin(const PasswordForm& form) { 278 PasswordStoreChangeList LoginDatabase::AddLogin(const PasswordForm& form) {
279 PasswordStoreChangeList list;
242 std::string encrypted_password; 280 std::string encrypted_password;
243 if (EncryptedString(form.password_value, &encrypted_password) != 281 if (EncryptedString(form.password_value, &encrypted_password) !=
244 ENCRYPTION_RESULT_SUCCESS) 282 ENCRYPTION_RESULT_SUCCESS)
245 return false; 283 return list;
246 284
247 // You *must* change LoginTableColumns if this query changes. 285 // You *must* change LoginTableColumns if this query changes.
248 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, 286 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE,
287 "INSERT INTO logins "
288 "(origin_url, action_url, username_element, username_value, "
289 " password_element, password_value, submit_element, "
290 " signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, "
291 " scheme, password_type, possible_usernames, times_used, form_data, "
292 " use_additional_auth) VALUES "
293 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
294 BindAddStatement(form, encrypted_password,
295 SerializeVector(form.other_possible_usernames),
296 &s);
297 db_.set_error_callback(base::Bind(&AddCallback));
298 const bool success = s.Run();
299 db_.reset_error_callback();
300 if (success) {
301 list.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
302 return list;
303 }
304 // Repeat the same statement but with REPLACE semantic.
305 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE,
249 "INSERT OR REPLACE INTO logins " 306 "INSERT OR REPLACE INTO logins "
250 "(origin_url, action_url, username_element, username_value, " 307 "(origin_url, action_url, username_element, username_value, "
251 " password_element, password_value, submit_element, " 308 " password_element, password_value, submit_element, "
252 " signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " 309 " signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, "
253 " scheme, password_type, possible_usernames, times_used, form_data, " 310 " scheme, password_type, possible_usernames, times_used, form_data, "
254 " use_additional_auth) VALUES " 311 " use_additional_auth) VALUES "
255 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); 312 "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"));
256 s.BindString(COLUMN_ORIGIN_URL, form.origin.spec()); 313 BindAddStatement(form, encrypted_password,
257 s.BindString(COLUMN_ACTION_URL, form.action.spec()); 314 SerializeVector(form.other_possible_usernames),
258 s.BindString16(COLUMN_USERNAME_ELEMENT, form.username_element); 315 &s);
259 s.BindString16(COLUMN_USERNAME_VALUE, form.username_value); 316 if (s.Run()) {
260 s.BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element); 317 list.push_back(PasswordStoreChange(PasswordStoreChange::REMOVE, form));
261 s.BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(), 318 list.push_back(PasswordStoreChange(PasswordStoreChange::ADD, form));
262 static_cast<int>(encrypted_password.length())); 319 }
263 s.BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element); 320 return list;
264 s.BindString(COLUMN_SIGNON_REALM, form.signon_realm);
265 s.BindInt(COLUMN_SSL_VALID, form.ssl_valid);
266 s.BindInt(COLUMN_PREFERRED, form.preferred);
267 s.BindInt64(COLUMN_DATE_CREATED, form.date_created.ToTimeT());
268 s.BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user);
269 s.BindInt(COLUMN_SCHEME, form.scheme);
270 s.BindInt(COLUMN_PASSWORD_TYPE, form.type);
271 Pickle usernames_pickle = SerializeVector(form.other_possible_usernames);
272 s.BindBlob(COLUMN_POSSIBLE_USERNAMES,
273 usernames_pickle.data(),
274 usernames_pickle.size());
275 s.BindInt(COLUMN_TIMES_USED, form.times_used);
276 Pickle form_data_pickle;
277 autofill::SerializeFormData(form.form_data, &form_data_pickle);
278 s.BindBlob(COLUMN_FORM_DATA,
279 form_data_pickle.data(),
280 form_data_pickle.size());
281 s.BindInt(COLUMN_USE_ADDITIONAL_AUTH, form.use_additional_authentication);
282
283 return s.Run();
284 } 321 }
285 322
286 bool LoginDatabase::UpdateLogin(const PasswordForm& form, int* items_changed) { 323 bool LoginDatabase::UpdateLogin(const PasswordForm& form, int* items_changed) {
287 std::string encrypted_password; 324 std::string encrypted_password;
288 if (EncryptedString(form.password_value, &encrypted_password) != 325 if (EncryptedString(form.password_value, &encrypted_password) !=
289 ENCRYPTION_RESULT_SUCCESS) 326 ENCRYPTION_RESULT_SUCCESS)
290 return false; 327 return false;
291 328
292 // Replacement is necessary to deal with updating imported credentials. See 329 // Replacement is necessary to deal with updating imported credentials. See
293 // crbug.com/349138 for details. 330 // crbug.com/349138 for details.
(...skipping 306 matching lines...) Expand 10 before | Expand all | Expand 10 after
600 base::string16 str; 637 base::string16 str;
601 638
602 PickleIterator iterator(p); 639 PickleIterator iterator(p);
603 while (iterator.ReadString16(&str)) { 640 while (iterator.ReadString16(&str)) {
604 ret.push_back(str); 641 ret.push_back(str);
605 } 642 }
606 return ret; 643 return ret;
607 } 644 }
608 645
609 } // namespace password_manager 646 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698