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/login_database.h" | 5 #include "chrome/browser/password_manager/login_database.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
| 10 #include "base/command_line.h" |
10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
11 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
14 #include "base/pickle.h" | 15 #include "base/pickle.h" |
15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
16 #include "base/time.h" | 17 #include "base/time.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
18 #include "sql/statement.h" | 21 #include "sql/statement.h" |
19 #include "sql/transaction.h" | 22 #include "sql/transaction.h" |
20 | 23 |
21 using content::PasswordForm; | 24 using content::PasswordForm; |
22 | 25 |
23 static const int kCurrentVersionNumber = 3; | 26 static const int kCurrentVersionNumber = 3; |
24 static const int kCompatibleVersionNumber = 1; | 27 static const int kCompatibleVersionNumber = 1; |
25 | 28 |
26 namespace { | 29 namespace { |
27 | 30 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 if (!MigrateOldVersionsAsNeeded()) { | 96 if (!MigrateOldVersionsAsNeeded()) { |
94 LOG(WARNING) << "Unable to migrate database"; | 97 LOG(WARNING) << "Unable to migrate database"; |
95 db_.Close(); | 98 db_.Close(); |
96 return false; | 99 return false; |
97 } | 100 } |
98 | 101 |
99 if (!transaction.Commit()) { | 102 if (!transaction.Commit()) { |
100 db_.Close(); | 103 db_.Close(); |
101 return false; | 104 return false; |
102 } | 105 } |
| 106 |
| 107 psl_domain_matching_ = CommandLine::ForCurrentProcess()->HasSwitch( |
| 108 switches::kEnableAutofillPSLDomainMatching); |
| 109 |
103 return true; | 110 return true; |
104 } | 111 } |
105 | 112 |
106 bool LoginDatabase::MigrateOldVersionsAsNeeded() { | 113 bool LoginDatabase::MigrateOldVersionsAsNeeded() { |
107 switch (meta_table_.GetVersionNumber()) { | 114 switch (meta_table_.GetVersionNumber()) { |
108 case 1: | 115 case 1: |
109 if (!db_.Execute("ALTER TABLE logins " | 116 if (!db_.Execute("ALTER TABLE logins " |
110 "ADD COLUMN password_type INTEGER") || | 117 "ADD COLUMN password_type INTEGER") || |
111 !db_.Execute("ALTER TABLE logins " | 118 !db_.Execute("ALTER TABLE logins " |
112 "ADD COLUMN possible_usernames BLOB")) { | 119 "ADD COLUMN possible_usernames BLOB")) { |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 form->origin = GURL(tmp); | 334 form->origin = GURL(tmp); |
328 tmp = s.ColumnString(COLUMN_ACTION_URL); | 335 tmp = s.ColumnString(COLUMN_ACTION_URL); |
329 form->action = GURL(tmp); | 336 form->action = GURL(tmp); |
330 form->username_element = s.ColumnString16(COLUMN_USERNAME_ELEMENT); | 337 form->username_element = s.ColumnString16(COLUMN_USERNAME_ELEMENT); |
331 form->username_value = s.ColumnString16(COLUMN_USERNAME_VALUE); | 338 form->username_value = s.ColumnString16(COLUMN_USERNAME_VALUE); |
332 form->password_element = s.ColumnString16(COLUMN_PASSWORD_ELEMENT); | 339 form->password_element = s.ColumnString16(COLUMN_PASSWORD_ELEMENT); |
333 form->password_value = decrypted_password; | 340 form->password_value = decrypted_password; |
334 form->submit_element = s.ColumnString16(COLUMN_SUBMIT_ELEMENT); | 341 form->submit_element = s.ColumnString16(COLUMN_SUBMIT_ELEMENT); |
335 tmp = s.ColumnString(COLUMN_SIGNON_REALM); | 342 tmp = s.ColumnString(COLUMN_SIGNON_REALM); |
336 form->signon_realm = tmp; | 343 form->signon_realm = tmp; |
| 344 form->original_signon_realm = ""; |
| 345 form->is_psl_origin_match = false; |
337 form->ssl_valid = (s.ColumnInt(COLUMN_SSL_VALID) > 0); | 346 form->ssl_valid = (s.ColumnInt(COLUMN_SSL_VALID) > 0); |
338 form->preferred = (s.ColumnInt(COLUMN_PREFERRED) > 0); | 347 form->preferred = (s.ColumnInt(COLUMN_PREFERRED) > 0); |
339 form->date_created = base::Time::FromTimeT( | 348 form->date_created = base::Time::FromTimeT( |
340 s.ColumnInt64(COLUMN_DATE_CREATED)); | 349 s.ColumnInt64(COLUMN_DATE_CREATED)); |
341 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0); | 350 form->blacklisted_by_user = (s.ColumnInt(COLUMN_BLACKLISTED_BY_USER) > 0); |
342 int scheme_int = s.ColumnInt(COLUMN_SCHEME); | 351 int scheme_int = s.ColumnInt(COLUMN_SCHEME); |
343 DCHECK((scheme_int >= 0) && (scheme_int <= PasswordForm::SCHEME_OTHER)); | 352 DCHECK((scheme_int >= 0) && (scheme_int <= PasswordForm::SCHEME_OTHER)); |
344 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int); | 353 form->scheme = static_cast<PasswordForm::Scheme>(scheme_int); |
345 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE); | 354 int type_int = s.ColumnInt(COLUMN_PASSWORD_TYPE); |
346 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_GENERATED); | 355 DCHECK(type_int >= 0 && type_int <= PasswordForm::TYPE_GENERATED); |
347 form->type = static_cast<PasswordForm::Type>(type_int); | 356 form->type = static_cast<PasswordForm::Type>(type_int); |
348 Pickle pickle( | 357 Pickle pickle( |
349 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAMES)), | 358 static_cast<const char*>(s.ColumnBlob(COLUMN_POSSIBLE_USERNAMES)), |
350 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); | 359 s.ColumnByteLength(COLUMN_POSSIBLE_USERNAMES)); |
351 form->other_possible_usernames = DeserializeVector(pickle); | 360 form->other_possible_usernames = DeserializeVector(pickle); |
352 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); | 361 form->times_used = s.ColumnInt(COLUMN_TIMES_USED); |
353 return true; | 362 return true; |
354 } | 363 } |
355 | 364 |
356 bool LoginDatabase::GetLogins(const PasswordForm& form, | 365 bool LoginDatabase::GetLogins(const PasswordForm& form, |
357 std::vector<PasswordForm*>* forms) const { | 366 std::vector<PasswordForm*>* forms) const { |
358 DCHECK(forms); | 367 DCHECK(forms); |
359 // You *must* change LoginTableColumns if this query changes. | 368 // You *must* change LoginTableColumns if this query changes. |
360 sql::Statement s(db_.GetCachedStatement(SQL_FROM_HERE, | 369 std::string signon_realm_operator = psl_domain_matching_ ? "regexp" : "=="; |
361 "SELECT origin_url, action_url, " | 370 std::string sql_query = "SELECT origin_url, action_url, " |
362 "username_element, username_value, " | 371 "username_element, username_value, " |
363 "password_element, password_value, submit_element, " | 372 "password_element, password_value, submit_element, " |
364 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " | 373 "signon_realm, ssl_valid, preferred, date_created, blacklisted_by_user, " |
365 "scheme, password_type, possible_usernames, times_used " | 374 "scheme, password_type, possible_usernames, times_used " |
366 "FROM logins WHERE signon_realm == ? ")); | 375 "FROM logins WHERE signon_realm " + signon_realm_operator + " ? "; |
367 s.BindString(0, form.signon_realm); | 376 sql::Statement s; |
| 377 if (psl_domain_matching_) { |
| 378 // TODO(nyquist) Re-enable usage of cached statements if possible. |
| 379 // s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); |
| 380 s.Assign(db_.GetUniqueStatement(sql_query.c_str())); |
| 381 GURL signon_realm(form.signon_realm); |
| 382 std::string domain = |
| 383 net::registry_controlled_domains::GetDomainAndRegistry( |
| 384 signon_realm, |
| 385 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 386 std::string regexp = "(.+)://(.*)" + domain + "(:\\d+)?/"; |
| 387 s.BindString(0, regexp); |
| 388 } else { |
| 389 s.Assign(db_.GetCachedStatement(SQL_FROM_HERE, sql_query.c_str())); |
| 390 s.BindString(0, form.signon_realm); |
| 391 } |
368 | 392 |
369 while (s.Step()) { | 393 while (s.Step()) { |
370 scoped_ptr<PasswordForm> new_form(new PasswordForm()); | 394 scoped_ptr<PasswordForm> new_form(new PasswordForm()); |
371 if (!InitPasswordFormFromStatement(new_form.get(), s)) | 395 if (!InitPasswordFormFromStatement(new_form.get(), s)) |
372 return false; | 396 return false; |
| 397 if (form.signon_realm != new_form->signon_realm) { |
| 398 new_form->is_psl_origin_match = true; |
| 399 new_form->origin = form.origin; |
| 400 new_form->original_signon_realm = new_form->signon_realm; |
| 401 new_form->signon_realm = form.signon_realm; |
| 402 new_form->action = form.action; |
| 403 } |
373 forms->push_back(new_form.release()); | 404 forms->push_back(new_form.release()); |
374 } | 405 } |
375 return s.Succeeded(); | 406 return s.Succeeded(); |
376 } | 407 } |
377 | 408 |
378 bool LoginDatabase::GetLoginsCreatedBetween( | 409 bool LoginDatabase::GetLoginsCreatedBetween( |
379 const base::Time begin, | 410 const base::Time begin, |
380 const base::Time end, | 411 const base::Time end, |
381 std::vector<content::PasswordForm*>* forms) const { | 412 std::vector<content::PasswordForm*>* forms) const { |
382 DCHECK(forms); | 413 DCHECK(forms); |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { | 484 std::vector<string16> LoginDatabase::DeserializeVector(const Pickle& p) const { |
454 std::vector<string16> ret; | 485 std::vector<string16> ret; |
455 string16 str; | 486 string16 str; |
456 | 487 |
457 PickleIterator iterator(p); | 488 PickleIterator iterator(p); |
458 while (iterator.ReadString16(&str)) { | 489 while (iterator.ReadString16(&str)) { |
459 ret.push_back(str); | 490 ret.push_back(str); |
460 } | 491 } |
461 return ret; | 492 return ret; |
462 } | 493 } |
OLD | NEW |