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

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

Issue 2747733004: [Password Manager] Send username correction votes (Closed)
Patch Set: Changes addressed to reviewer comments Created 3 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
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 <stddef.h> 7 #include <stddef.h>
8 #include <stdint.h> 8 #include <stdint.h>
9 #include <algorithm> 9 #include <algorithm>
10 #include <limits> 10 #include <limits>
(...skipping 26 matching lines...) Expand all
37 #include "sql/statement.h" 37 #include "sql/statement.h"
38 #include "sql/transaction.h" 38 #include "sql/transaction.h"
39 #include "url/origin.h" 39 #include "url/origin.h"
40 #include "url/url_constants.h" 40 #include "url/url_constants.h"
41 41
42 using autofill::PasswordForm; 42 using autofill::PasswordForm;
43 43
44 namespace password_manager { 44 namespace password_manager {
45 45
46 // The current version number of the login database schema. 46 // The current version number of the login database schema.
47 const int kCurrentVersionNumber = 18; 47 const int kCurrentVersionNumber = 19;
48 // The oldest version of the schema such that a legacy Chrome client using that 48 // The oldest version of the schema such that a legacy Chrome client using that
49 // version can still read/write the current database. 49 // version can still read/write the current database.
50 const int kCompatibleVersionNumber = 18; 50 const int kCompatibleVersionNumber = 19;
51 51
52 base::Pickle SerializeVector(const std::vector<base::string16>& vec) { 52 base::Pickle SerializePossibleUsernamePairs(
53 const autofill::PossibleUsernamesVector& vec) {
53 base::Pickle p; 54 base::Pickle p;
54 for (size_t i = 0; i < vec.size(); ++i) { 55 for (size_t i = 0; i < vec.size(); ++i) {
55 p.WriteString16(vec[i]); 56 p.WriteString16(vec[i].first);
57 p.WriteString16(vec[i].second);
56 } 58 }
57 return p; 59 return p;
58 } 60 }
59 61
60 std::vector<base::string16> DeserializeVector(const base::Pickle& p) { 62 autofill::PossibleUsernamesVector DeserializePossibleUsernamePairs(
61 std::vector<base::string16> ret; 63 const base::Pickle& p) {
62 base::string16 str; 64 autofill::PossibleUsernamesVector ret;
65 base::string16 value;
66 base::string16 field_name;
63 67
64 base::PickleIterator iterator(p); 68 base::PickleIterator iterator(p);
65 while (iterator.ReadString16(&str)) { 69 while (iterator.ReadString16(&value)) {
66 ret.push_back(str); 70 bool name_success = iterator.ReadString16(&field_name);
71 DCHECK(name_success);
72 ret.push_back(autofill::PossibleUsernamePair(value, field_name));
67 } 73 }
68 return ret; 74 return ret;
69 } 75 }
70 76
71 namespace { 77 namespace {
72 78
73 // Convenience enum for interacting with SQL queries that use all the columns. 79 // Convenience enum for interacting with SQL queries that use all the columns.
74 enum LoginTableColumns { 80 enum LoginTableColumns {
75 COLUMN_ORIGIN_URL = 0, 81 COLUMN_ORIGIN_URL = 0,
76 COLUMN_ACTION_URL, 82 COLUMN_ACTION_URL,
77 COLUMN_USERNAME_ELEMENT, 83 COLUMN_USERNAME_ELEMENT,
78 COLUMN_USERNAME_VALUE, 84 COLUMN_USERNAME_VALUE,
79 COLUMN_PASSWORD_ELEMENT, 85 COLUMN_PASSWORD_ELEMENT,
80 COLUMN_PASSWORD_VALUE, 86 COLUMN_PASSWORD_VALUE,
81 COLUMN_SUBMIT_ELEMENT, 87 COLUMN_SUBMIT_ELEMENT,
82 COLUMN_SIGNON_REALM, 88 COLUMN_SIGNON_REALM,
83 COLUMN_PREFERRED, 89 COLUMN_PREFERRED,
84 COLUMN_DATE_CREATED, 90 COLUMN_DATE_CREATED,
85 COLUMN_BLACKLISTED_BY_USER, 91 COLUMN_BLACKLISTED_BY_USER,
86 COLUMN_SCHEME, 92 COLUMN_SCHEME,
87 COLUMN_PASSWORD_TYPE, 93 COLUMN_PASSWORD_TYPE,
88 COLUMN_POSSIBLE_USERNAMES,
89 COLUMN_TIMES_USED, 94 COLUMN_TIMES_USED,
90 COLUMN_FORM_DATA, 95 COLUMN_FORM_DATA,
91 COLUMN_DATE_SYNCED, 96 COLUMN_DATE_SYNCED,
92 COLUMN_DISPLAY_NAME, 97 COLUMN_DISPLAY_NAME,
93 COLUMN_ICON_URL, 98 COLUMN_ICON_URL,
94 COLUMN_FEDERATION_URL, 99 COLUMN_FEDERATION_URL,
95 COLUMN_SKIP_ZERO_CLICK, 100 COLUMN_SKIP_ZERO_CLICK,
96 COLUMN_GENERATION_UPLOAD_STATUS, 101 COLUMN_GENERATION_UPLOAD_STATUS,
102 COLUMN_POSSIBLE_USERNAME_PAIRS,
97 COLUMN_NUM // Keep this last. 103 COLUMN_NUM // Keep this last.
98 }; 104 };
99 105
100 enum class HistogramSize { SMALL, LARGE }; 106 enum class HistogramSize { SMALL, LARGE };
101 107
102 // An enum for UMA reporting. Add values to the end only. 108 // An enum for UMA reporting. Add values to the end only.
103 enum DatabaseInitError { 109 enum DatabaseInitError {
104 INIT_OK, 110 INIT_OK,
105 OPEN_FILE_ERROR, 111 OPEN_FILE_ERROR,
106 START_TRANSACTION_ERROR, 112 START_TRANSACTION_ERROR,
(...skipping 17 matching lines...) Expand all
124 s->BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element); 130 s->BindString16(COLUMN_PASSWORD_ELEMENT, form.password_element);
125 s->BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(), 131 s->BindBlob(COLUMN_PASSWORD_VALUE, encrypted_password.data(),
126 static_cast<int>(encrypted_password.length())); 132 static_cast<int>(encrypted_password.length()));
127 s->BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element); 133 s->BindString16(COLUMN_SUBMIT_ELEMENT, form.submit_element);
128 s->BindString(COLUMN_SIGNON_REALM, form.signon_realm); 134 s->BindString(COLUMN_SIGNON_REALM, form.signon_realm);
129 s->BindInt(COLUMN_PREFERRED, form.preferred); 135 s->BindInt(COLUMN_PREFERRED, form.preferred);
130 s->BindInt64(COLUMN_DATE_CREATED, form.date_created.ToInternalValue()); 136 s->BindInt64(COLUMN_DATE_CREATED, form.date_created.ToInternalValue());
131 s->BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user); 137 s->BindInt(COLUMN_BLACKLISTED_BY_USER, form.blacklisted_by_user);
132 s->BindInt(COLUMN_SCHEME, form.scheme); 138 s->BindInt(COLUMN_SCHEME, form.scheme);
133 s->BindInt(COLUMN_PASSWORD_TYPE, form.type); 139 s->BindInt(COLUMN_PASSWORD_TYPE, form.type);
134 base::Pickle usernames_pickle =
135 SerializeVector(form.other_possible_usernames);
136 s->BindBlob(COLUMN_POSSIBLE_USERNAMES,
137 usernames_pickle.data(),
138 usernames_pickle.size());
139 s->BindInt(COLUMN_TIMES_USED, form.times_used); 140 s->BindInt(COLUMN_TIMES_USED, form.times_used);
140 base::Pickle form_data_pickle; 141 base::Pickle form_data_pickle;
141 autofill::SerializeFormData(form.form_data, &form_data_pickle); 142 autofill::SerializeFormData(form.form_data, &form_data_pickle);
142 s->BindBlob(COLUMN_FORM_DATA, 143 s->BindBlob(COLUMN_FORM_DATA,
143 form_data_pickle.data(), 144 form_data_pickle.data(),
144 form_data_pickle.size()); 145 form_data_pickle.size());
145 s->BindInt64(COLUMN_DATE_SYNCED, form.date_synced.ToInternalValue()); 146 s->BindInt64(COLUMN_DATE_SYNCED, form.date_synced.ToInternalValue());
146 s->BindString16(COLUMN_DISPLAY_NAME, form.display_name); 147 s->BindString16(COLUMN_DISPLAY_NAME, form.display_name);
147 s->BindString(COLUMN_ICON_URL, form.icon_url.spec()); 148 s->BindString(COLUMN_ICON_URL, form.icon_url.spec());
148 // An empty Origin serializes as "null" which would be strange to store here. 149 // An empty Origin serializes as "null" which would be strange to store here.
149 s->BindString(COLUMN_FEDERATION_URL, 150 s->BindString(COLUMN_FEDERATION_URL,
150 form.federation_origin.unique() 151 form.federation_origin.unique()
151 ? std::string() 152 ? std::string()
152 : form.federation_origin.Serialize()); 153 : form.federation_origin.Serialize());
153 s->BindInt(COLUMN_SKIP_ZERO_CLICK, form.skip_zero_click); 154 s->BindInt(COLUMN_SKIP_ZERO_CLICK, form.skip_zero_click);
154 s->BindInt(COLUMN_GENERATION_UPLOAD_STATUS, form.generation_upload_status); 155 s->BindInt(COLUMN_GENERATION_UPLOAD_STATUS, form.generation_upload_status);
156 base::Pickle usernames_pickle =
157 SerializePossibleUsernamePairs(form.other_possible_usernames);
158 s->BindBlob(COLUMN_POSSIBLE_USERNAME_PAIRS, usernames_pickle.data(),
159 usernames_pickle.size());
155 } 160 }
156 161
157 void AddCallback(int err, sql::Statement* /*stmt*/) { 162 void AddCallback(int err, sql::Statement* /*stmt*/) {
158 if (err == 19 /*SQLITE_CONSTRAINT*/) 163 if (err == 19 /*SQLITE_CONSTRAINT*/)
159 DLOG(WARNING) << "LoginDatabase::AddLogin updated an existing form"; 164 DLOG(WARNING) << "LoginDatabase::AddLogin updated an existing form";
160 } 165 }
161 166
162 bool DoesMatchConstraints(const PasswordForm& form) { 167 bool DoesMatchConstraints(const PasswordForm& form) {
163 if (!IsValidAndroidFacetURI(form.signon_realm) && form.origin.is_empty()) { 168 if (!IsValidAndroidFacetURI(form.signon_realm) && form.origin.is_empty()) {
164 DLOG(ERROR) << "Constraint violation: form.origin is empty"; 169 DLOG(ERROR) << "Constraint violation: form.origin is empty";
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 builder->SealVersion(); 429 builder->SealVersion();
425 // Version 17. 430 // Version 17.
426 version = builder->SealVersion(); 431 version = builder->SealVersion();
427 DCHECK_EQ(17u, version); 432 DCHECK_EQ(17u, version);
428 433
429 // Version 18. 434 // Version 18.
430 builder->DropColumn("ssl_valid"); 435 builder->DropColumn("ssl_valid");
431 version = builder->SealVersion(); 436 version = builder->SealVersion();
432 DCHECK_EQ(18u, version); 437 DCHECK_EQ(18u, version);
433 438
439 // Version 19.
440 builder->DropColumn("possible_usernames");
441 builder->AddColumn("possible_username_pairs", "BLOB");
442 version = builder->SealVersion();
443 DCHECK_EQ(19u, version);
444
434 DCHECK_EQ(static_cast<size_t>(COLUMN_NUM), builder->NumberOfColumns()) 445 DCHECK_EQ(static_cast<size_t>(COLUMN_NUM), builder->NumberOfColumns())
435 << "Adjust LoginTableColumns if you change column definitions here."; 446 << "Adjust LoginTableColumns if you change column definitions here.";
436 } 447 }
437 448
438 // Call this after having called InitializeBuilder, to migrate the database from 449 // Call this after having called InitializeBuilder, to migrate the database from
439 // the current version to kCurrentVersionNumber. 450 // the current version to kCurrentVersionNumber.
440 bool MigrateLogins(unsigned current_version, 451 bool MigrateLogins(unsigned current_version,
441 SQLTableBuilder* builder, 452 SQLTableBuilder* builder,
442 sql::Connection* db) { 453 sql::Connection* db) {
443 if (!builder->MigrateFrom(current_version, db)) 454 if (!builder->MigrateFrom(current_version, db))
(...skipping 393 matching lines...) Expand 10 before | Expand all | Expand 10 after
837 return PasswordStoreChangeList(); 848 return PasswordStoreChangeList();
838 849
839 #if defined(OS_IOS) 850 #if defined(OS_IOS)
840 DeleteEncryptedPassword(form); 851 DeleteEncryptedPassword(form);
841 #endif 852 #endif
842 // Replacement is necessary to deal with updating imported credentials. See 853 // Replacement is necessary to deal with updating imported credentials. See
843 // crbug.com/349138 for details. 854 // crbug.com/349138 for details.
844 DCHECK(!update_statement_.empty()); 855 DCHECK(!update_statement_.empty());
845 sql::Statement s( 856 sql::Statement s(
846 db_.GetCachedStatement(SQL_FROM_HERE, update_statement_.c_str())); 857 db_.GetCachedStatement(SQL_FROM_HERE, update_statement_.c_str()));
847 s.BindString(0, form.action.spec()); 858 int next_param = 0;
848 s.BindBlob(1, encrypted_password.data(), 859 s.BindString(next_param++, form.action.spec());
860 s.BindBlob(next_param++, encrypted_password.data(),
849 static_cast<int>(encrypted_password.length())); 861 static_cast<int>(encrypted_password.length()));
850 s.BindString16(2, form.submit_element); 862 s.BindString16(next_param++, form.submit_element);
851 s.BindInt(3, form.preferred); 863 s.BindInt(next_param++, form.preferred);
852 s.BindInt64(4, form.date_created.ToInternalValue()); 864 s.BindInt64(next_param++, form.date_created.ToInternalValue());
853 s.BindInt(5, form.blacklisted_by_user); 865 s.BindInt(next_param++, form.blacklisted_by_user);
854 s.BindInt(6, form.scheme); 866 s.BindInt(next_param++, form.scheme);
855 s.BindInt(7, form.type); 867 s.BindInt(next_param++, form.type);
856 base::Pickle pickle = SerializeVector(form.other_possible_usernames); 868 s.BindInt(next_param++, form.times_used);
857 s.BindBlob(8, pickle.data(), pickle.size());
858 s.BindInt(9, form.times_used);
859 base::Pickle form_data_pickle; 869 base::Pickle form_data_pickle;
860 autofill::SerializeFormData(form.form_data, &form_data_pickle); 870 autofill::SerializeFormData(form.form_data, &form_data_pickle);
861 s.BindBlob(10, form_data_pickle.data(), form_data_pickle.size()); 871 s.BindBlob(next_param++, form_data_pickle.data(), form_data_pickle.size());
862 s.BindInt64(11, form.date_synced.ToInternalValue()); 872 s.BindInt64(next_param++, form.date_synced.ToInternalValue());
863 s.BindString16(12, form.display_name); 873 s.BindString16(next_param++, form.display_name);
864 s.BindString(13, form.icon_url.spec()); 874 s.BindString(next_param++, form.icon_url.spec());
865 // An empty Origin serializes as "null" which would be strange to store here. 875 // An empty Origin serializes as "null" which would be strange to store here.
866 s.BindString(14, form.federation_origin.unique() 876 s.BindString(next_param++, form.federation_origin.unique()
867 ? std::string() 877 ? std::string()
868 : form.federation_origin.Serialize()); 878 : form.federation_origin.Serialize());
869 s.BindInt(15, form.skip_zero_click); 879 s.BindInt(next_param++, form.skip_zero_click);
870 s.BindInt(16, form.generation_upload_status); 880 s.BindInt(next_param++, form.generation_upload_status);
881 base::Pickle username_pickle =
882 SerializePossibleUsernamePairs(form.other_possible_usernames);
883 s.BindBlob(next_param++, username_pickle.data(), username_pickle.size());
884 // NOTE: Add new fields here unless the field is a part of the unique key.
885 // If so, add new field below.
871 886
872 // WHERE starts here. 887 // WHERE starts here.
873 s.BindString(17, form.origin.spec()); 888 s.BindString(next_param++, form.origin.spec());
874 s.BindString16(18, form.username_element); 889 s.BindString16(next_param++, form.username_element);
875 s.BindString16(19, form.username_value); 890 s.BindString16(next_param++, form.username_value);
876 s.BindString16(20, form.password_element); 891 s.BindString16(next_param++, form.password_element);
877 s.BindString(21, form.signon_realm); 892 s.BindString(next_param++, form.signon_realm);
893 // NOTE: Add new fields here only if the field is a part of the unique key.
894 // Otherwise, add the field above "WHERE starts here" comment.
878 895
879 if (!s.Run()) 896 if (!s.Run())
880 return PasswordStoreChangeList(); 897 return PasswordStoreChangeList();
881 898
882 PasswordStoreChangeList list; 899 PasswordStoreChangeList list;
883 if (db_.GetLastChangeCount()) 900 if (db_.GetLastChangeCount())
884 list.push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, form)); 901 list.push_back(PasswordStoreChange(PasswordStoreChange::UPDATE, form));
885 902
886 return list; 903 return list;
887 } 904 }
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
991 form->date_created = 1008 form->date_created =
992 base::Time::FromInternalValue(s.ColumnInt64(COLUMN_DATE_CREATED)); 1009 base::Time::FromInternalValue(s.ColumnInt64(COLUMN_DATE_CREATED));
993 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0); 1010 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0);
994 int scheme_int = s.ColumnInt(COLUMN_SCHEME); 1011 int scheme_int = s.ColumnInt(COLUMN_SCHEME);
995 DCHECK_LE(0, scheme_int); 1012 DCHECK_LE(0, scheme_int);
996 DCHECK_GE(PasswordForm::SCHEME_LAST, scheme_int); 1013 DCHECK_GE(PasswordForm::SCHEME_LAST, scheme_int);
997 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int); 1014 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int);
998 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE); 1015 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE);
999 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_LAST) << type_int; 1016 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_LAST) << type_int;
1000 form->type = static_cast<PasswordForm::Type>(type_int); 1017 form->type = static_cast<PasswordForm::Type>(type_int);
1001 if (s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)) { 1018 if (s.ColumnByteLength(COLUMN_POSSIBLE_USERNAME_PAIRS)) {
1002 base::Pickle pickle( 1019 base::Pickle pickle(
1003 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAMES)), 1020 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAME_PAIRS)),
1004 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); 1021 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAME_PAIRS));
1005 form->other_possible_usernames = DeserializeVector(pickle); 1022 form->other_possible_usernames = DeserializePossibleUsernamePairs(pickle);
1006 } 1023 }
1007 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); 1024 form->times_used = s.ColumnInt(COLUMN_TIMES_USED);
1008 if (s.ColumnByteLength(COLUMN_FORM_DATA)) { 1025 if (s.ColumnByteLength(COLUMN_FORM_DATA)) {
1009 base::Pickle form_data_pickle( 1026 base::Pickle form_data_pickle(
1010 static_cast<const char*>(s.ColumnBlob(COLUMN_FORM_DATA)), 1027 static_cast<const char*>(s.ColumnBlob(COLUMN_FORM_DATA)),
1011 s.ColumnByteLength(COLUMN_FORM_DATA)); 1028 s.ColumnByteLength(COLUMN_FORM_DATA));
1012 base::PickleIterator form_data_iter(form_data_pickle); 1029 base::PickleIterator form_data_iter(form_data_pickle);
1013 bool success = 1030 bool success =
1014 autofill::DeserializeFormData(&form_data_iter, &form->form_data); 1031 autofill::DeserializeFormData(&form_data_iter, &form->form_data);
1015 metrics_util::FormDeserializationStatus status = 1032 metrics_util::FormDeserializationStatus status =
(...skipping 294 matching lines...) Expand 10 before | Expand all | Expand 10 after
1310 DCHECK(blacklisted_statement_.empty()); 1327 DCHECK(blacklisted_statement_.empty());
1311 blacklisted_statement_ = 1328 blacklisted_statement_ =
1312 "SELECT " + all_column_names + 1329 "SELECT " + all_column_names +
1313 " FROM logins WHERE blacklisted_by_user == ? ORDER BY origin_url"; 1330 " FROM logins WHERE blacklisted_by_user == ? ORDER BY origin_url";
1314 DCHECK(encrypted_statement_.empty()); 1331 DCHECK(encrypted_statement_.empty());
1315 encrypted_statement_ = 1332 encrypted_statement_ =
1316 "SELECT password_value FROM logins WHERE " + all_unique_key_column_names; 1333 "SELECT password_value FROM logins WHERE " + all_unique_key_column_names;
1317 } 1334 }
1318 1335
1319 } // namespace password_manager 1336 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698