| 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/webdata/keyword_table.h" | 5 #include "chrome/browser/webdata/keyword_table.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/metrics/stats_counters.h" | 10 #include "base/metrics/stats_counters.h" |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 "usage_count INTEGER DEFAULT 0," | 117 "usage_count INTEGER DEFAULT 0," |
| 118 "input_encodings VARCHAR," | 118 "input_encodings VARCHAR," |
| 119 "suggest_url VARCHAR," | 119 "suggest_url VARCHAR," |
| 120 "prepopulate_id INTEGER DEFAULT 0," | 120 "prepopulate_id INTEGER DEFAULT 0," |
| 121 "autogenerate_keyword INTEGER DEFAULT 0," | 121 "autogenerate_keyword INTEGER DEFAULT 0," |
| 122 "logo_id INTEGER DEFAULT 0," | 122 "logo_id INTEGER DEFAULT 0," |
| 123 "created_by_policy INTEGER DEFAULT 0," | 123 "created_by_policy INTEGER DEFAULT 0," |
| 124 "instant_url VARCHAR," | 124 "instant_url VARCHAR," |
| 125 "last_modified INTEGER DEFAULT 0," | 125 "last_modified INTEGER DEFAULT 0," |
| 126 "sync_guid VARCHAR)")) { | 126 "sync_guid VARCHAR)")) { |
| 127 NOTREACHED(); | |
| 128 return false; | 127 return false; |
| 129 } | 128 } |
| 130 if (!UpdateBackupSignature()) | 129 if (!UpdateBackupSignature()) |
| 131 return false; | 130 return false; |
| 132 } | 131 } |
| 133 return true; | 132 return true; |
| 134 } | 133 } |
| 135 | 134 |
| 136 bool KeywordTable::IsSyncable() { | 135 bool KeywordTable::IsSyncable() { |
| 137 return true; | 136 return true; |
| 138 } | 137 } |
| 139 | 138 |
| 140 bool KeywordTable::AddKeyword(const TemplateURL& url) { | 139 bool KeywordTable::AddKeyword(const TemplateURL& url) { |
| 141 DCHECK(url.id()); | 140 DCHECK(url.id()); |
| 142 // Be sure to change kUrlIdPosition if you add columns | 141 // Be sure to change kUrlIdPosition if you add columns |
| 143 sql::Statement s(db_->GetUniqueStatement( | 142 sql::Statement s(db_->GetUniqueStatement( |
| 144 "INSERT INTO keywords " | 143 "INSERT INTO keywords " |
| 145 "(short_name, keyword, favicon_url, url, safe_for_autoreplace, " | 144 "(short_name, keyword, favicon_url, url, safe_for_autoreplace, " |
| 146 "originating_url, date_created, usage_count, input_encodings, " | 145 "originating_url, date_created, usage_count, input_encodings, " |
| 147 "show_in_default_list, suggest_url, prepopulate_id, " | 146 "show_in_default_list, suggest_url, prepopulate_id, " |
| 148 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " | 147 "autogenerate_keyword, logo_id, created_by_policy, instant_url, " |
| 149 "last_modified, sync_guid, id) VALUES " | 148 "last_modified, sync_guid, id) VALUES " |
| 150 "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); | 149 "(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")); |
| 151 BindURLToStatement(url, &s); | 150 BindURLToStatement(url, &s); |
| 152 s.BindInt64(kUrlIdPosition, url.id()); | 151 s.BindInt64(kUrlIdPosition, url.id()); |
| 153 | 152 |
| 154 if (!s.Run()) { | 153 if (!s.Run()) |
| 155 return false; | 154 return false; |
| 156 } | 155 |
| 157 return UpdateBackupSignature(); | 156 return UpdateBackupSignature(); |
| 158 } | 157 } |
| 159 | 158 |
| 160 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 159 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
| 161 DCHECK(id); | 160 DCHECK(id); |
| 162 sql::Statement s( | 161 sql::Statement s( |
| 163 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); | 162 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); |
| 164 s.BindInt64(0, id); | 163 s.BindInt64(0, id); |
| 165 | 164 |
| 166 return s.Run() && UpdateBackupSignature(); | 165 return s.Run() && UpdateBackupSignature(); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 LOG(ERROR) << "No default search id backup found."; | 219 LOG(ERROR) << "No default search id backup found."; |
| 221 return NULL; | 220 return NULL; |
| 222 } | 221 } |
| 223 sql::Statement s(db_->GetUniqueStatement( | 222 sql::Statement s(db_->GetUniqueStatement( |
| 224 "SELECT id, short_name, keyword, favicon_url, url, " | 223 "SELECT id, short_name, keyword, favicon_url, url, " |
| 225 "safe_for_autoreplace, originating_url, date_created, " | 224 "safe_for_autoreplace, originating_url, date_created, " |
| 226 "usage_count, input_encodings, show_in_default_list, " | 225 "usage_count, input_encodings, show_in_default_list, " |
| 227 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " | 226 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " |
| 228 "created_by_policy, instant_url, last_modified, sync_guid " | 227 "created_by_policy, instant_url, last_modified, sync_guid " |
| 229 "FROM keywords_backup WHERE id=?")); | 228 "FROM keywords_backup WHERE id=?")); |
| 230 if (!s) { | |
| 231 NOTREACHED() << "Statement prepare failed"; | |
| 232 return NULL; | |
| 233 } | |
| 234 s.BindInt64(0, backup_id); | 229 s.BindInt64(0, backup_id); |
| 230 |
| 235 if (!s.Step()) { | 231 if (!s.Step()) { |
| 236 LOG(ERROR) << "No default search provider with backup id."; | 232 LOG_IF(ERROR, s.Succeeded()) |
| 233 << "No default search provider with backup id."; |
| 237 return NULL; | 234 return NULL; |
| 238 } | 235 } |
| 239 | 236 |
| 240 scoped_ptr<TemplateURL> template_url(new TemplateURL()); | 237 TemplateURL* template_url = new TemplateURL(); |
| 241 GetURLFromStatement(s, template_url.get()); | 238 GetURLFromStatement(s, template_url); |
| 242 // ID has no meaning for the backup and should be 0 in case the TemplateURL | 239 // ID has no meaning for the backup and should be 0 in case the TemplateURL |
| 243 // will be added to keywords if missing. | 240 // will be added to keywords if missing. |
| 244 template_url->set_id(0); | 241 template_url->set_id(0); |
| 245 | 242 |
| 246 if (!s.Succeeded()) { | 243 return template_url; |
| 247 LOG(ERROR) << "Statement has not succeeded."; | |
| 248 return NULL; | |
| 249 } | |
| 250 return template_url.release(); | |
| 251 } | 244 } |
| 252 | 245 |
| 253 bool KeywordTable::DidDefaultSearchProviderChange() { | 246 bool KeywordTable::DidDefaultSearchProviderChange() { |
| 254 if (!IsBackupSignatureValid()) { | 247 if (!IsBackupSignatureValid()) { |
| 255 UMA_HISTOGRAM_ENUMERATION( | 248 UMA_HISTOGRAM_ENUMERATION( |
| 256 protector::kProtectorHistogramDefaultSearchProvider, | 249 protector::kProtectorHistogramDefaultSearchProvider, |
| 257 protector::kProtectorErrorBackupInvalid, | 250 protector::kProtectorErrorBackupInvalid, |
| 258 protector::kProtectorErrorCount); | 251 protector::kProtectorErrorCount); |
| 259 LOG(ERROR) << "Backup signature is invalid."; | 252 LOG(ERROR) << "Backup signature is invalid."; |
| 260 return true; | 253 return true; |
| (...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 } | 386 } |
| 394 | 387 |
| 395 bool KeywordTable::MigrateToVersion41RewriteDefaultSearchProviderBackup() { | 388 bool KeywordTable::MigrateToVersion41RewriteDefaultSearchProviderBackup() { |
| 396 // Due to crbug.com/101815 version 40 may contain corrupt or empty | 389 // Due to crbug.com/101815 version 40 may contain corrupt or empty |
| 397 // signature. So ignore the signature and simply rewrite it. | 390 // signature. So ignore the signature and simply rewrite it. |
| 398 return MigrateToVersion40AddDefaultSearchProviderBackup(); | 391 return MigrateToVersion40AddDefaultSearchProviderBackup(); |
| 399 } | 392 } |
| 400 | 393 |
| 401 bool KeywordTable::MigrateToVersion42AddFullDefaultSearchProviderBackup() { | 394 bool KeywordTable::MigrateToVersion42AddFullDefaultSearchProviderBackup() { |
| 402 sql::Transaction transaction(db_); | 395 sql::Transaction transaction(db_); |
| 403 if (!transaction.Begin()) { | 396 if (!transaction.Begin()) |
| 404 NOTREACHED() << "Failed to start transaction"; | |
| 405 return false; | 397 return false; |
| 406 } | |
| 407 | 398 |
| 408 int64 id = 0; | 399 int64 id = 0; |
| 409 if (!UpdateDefaultSearchProviderIDBackup(&id)) | 400 if (!UpdateDefaultSearchProviderIDBackup(&id)) |
| 410 return false; | 401 return false; |
| 411 | 402 |
| 412 std::string keyword_backup; | 403 std::string keyword_backup; |
| 413 if (!UpdateDefaultSearchProviderBackup(id, &keyword_backup)) | 404 if (!UpdateDefaultSearchProviderBackup(id, &keyword_backup)) |
| 414 return false; | 405 return false; |
| 415 | 406 |
| 416 std::string keywords; | 407 std::string keywords; |
| 417 if (!GetTableContents("keywords", &keywords)) { | 408 if (!GetTableContents("keywords", &keywords)) |
| 418 NOTREACHED() << "Can't get keywords table contents to sign"; | |
| 419 return false; | 409 return false; |
| 420 } | |
| 421 | 410 |
| 422 std::string data_to_sign = base::Int64ToString(id) + | 411 std::string data_to_sign = base::Int64ToString(id) + |
| 423 keyword_backup + | 412 keyword_backup + |
| 424 keywords; | 413 keywords; |
| 425 std::string signature = protector::SignSetting(data_to_sign); | 414 std::string signature = protector::SignSetting(data_to_sign); |
| 426 if (signature.empty()) { | 415 if (signature.empty()) |
| 427 NOTREACHED() << "Signature is empty"; | 416 NOTREACHED() << "Signature is empty"; |
| 428 return false; | 417 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) |
| 429 } | |
| 430 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) { | |
| 431 NOTREACHED() << "Failed to write signature."; | 418 NOTREACHED() << "Failed to write signature."; |
| 432 return false; | |
| 433 } | |
| 434 | 419 |
| 435 return transaction.Commit(); | 420 return transaction.Commit(); |
| 436 } | 421 } |
| 437 | 422 |
| 438 bool KeywordTable::MigrateToVersion43AddKeywordsBackupTable() { | 423 bool KeywordTable::MigrateToVersion43AddKeywordsBackupTable() { |
| 439 return meta_table_->SetValue(kDefaultSearchBackupKey, std::string()) && | 424 return meta_table_->SetValue(kDefaultSearchBackupKey, std::string()) && |
| 440 UpdateBackupSignature(); | 425 UpdateBackupSignature(); |
| 441 } | 426 } |
| 442 | 427 |
| 443 bool KeywordTable::MigrateToVersion44UpdateKeywordsBackup() { | 428 bool KeywordTable::MigrateToVersion44UpdateKeywordsBackup() { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 465 bool KeywordTable::GetTableContents(const char* table_name, | 450 bool KeywordTable::GetTableContents(const char* table_name, |
| 466 std::string* contents) { | 451 std::string* contents) { |
| 467 DCHECK(contents); | 452 DCHECK(contents); |
| 468 std::string table_data; | 453 std::string table_data; |
| 469 | 454 |
| 470 std::string query = | 455 std::string query = |
| 471 "SELECT " + std::string(kKeywordColumnsConcatenated) + | 456 "SELECT " + std::string(kKeywordColumnsConcatenated) + |
| 472 " FROM " + std::string(table_name) + " ORDER BY id ASC"; | 457 " FROM " + std::string(table_name) + " ORDER BY id ASC"; |
| 473 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name), | 458 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name), |
| 474 query.c_str())); | 459 query.c_str())); |
| 475 if (!s) { | |
| 476 NOTREACHED() << "Statement prepare failed"; | |
| 477 return false; | |
| 478 } | |
| 479 while (s.Step()) | 460 while (s.Step()) |
| 480 table_data += s.ColumnString(0); | 461 table_data += s.ColumnString(0); |
| 481 if (!s.Succeeded()) { | 462 if (!s.Succeeded()) |
| 482 NOTREACHED() << "Statement execution failed"; | |
| 483 return false; | 463 return false; |
| 484 } | 464 |
| 485 *contents = table_data; | 465 *contents = table_data; |
| 486 return true; | 466 return true; |
| 487 } | 467 } |
| 488 | 468 |
| 489 bool KeywordTable::UpdateBackupSignature() { | 469 bool KeywordTable::UpdateBackupSignature() { |
| 490 sql::Transaction transaction(db_); | 470 sql::Transaction transaction(db_); |
| 491 if (!transaction.Begin()) { | 471 if (!transaction.Begin()) |
| 492 NOTREACHED() << "Failed to start transaction"; | |
| 493 return false; | 472 return false; |
| 494 } | |
| 495 | 473 |
| 496 int64 id = 0; | 474 int64 id = 0; |
| 497 if (!UpdateDefaultSearchProviderIDBackup(&id)) { | 475 if (!UpdateDefaultSearchProviderIDBackup(&id)) { |
| 498 LOG(ERROR) << "Failed to update default search id backup."; | 476 LOG(ERROR) << "Failed to update default search id backup."; |
| 499 return false; | 477 return false; |
| 500 } | 478 } |
| 501 | 479 |
| 502 // Backup of all keywords. | 480 // Backup of all keywords. |
| 503 if (db_->DoesTableExist("keywords_backup") && | 481 if (db_->DoesTableExist("keywords_backup") && |
| 504 !db_->Execute("DROP TABLE keywords_backup")) | 482 !db_->Execute("DROP TABLE keywords_backup")) { |
| 505 return false; | 483 return false; |
| 484 } |
| 506 | 485 |
| 507 if (!db_->Execute( | 486 if (!db_->Execute( |
| 508 "CREATE TABLE keywords_backup AS " | 487 "CREATE TABLE keywords_backup AS " |
| 509 "SELECT id, short_name, keyword, favicon_url, url, " | 488 "SELECT id, short_name, keyword, favicon_url, url, " |
| 510 "safe_for_autoreplace, originating_url, date_created, " | 489 "safe_for_autoreplace, originating_url, date_created, " |
| 511 "usage_count, input_encodings, show_in_default_list, " | 490 "usage_count, input_encodings, show_in_default_list, " |
| 512 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " | 491 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " |
| 513 "created_by_policy, instant_url, last_modified, sync_guid " | 492 "created_by_policy, instant_url, last_modified, sync_guid " |
| 514 "FROM keywords ORDER BY id ASC")) { | 493 "FROM keywords ORDER BY id ASC")) { |
| 515 LOG(ERROR) << "Failed to create keywords_backup table."; | 494 LOG(ERROR) << "Failed to create keywords_backup table."; |
| 516 return false; | 495 return false; |
| 517 } | 496 } |
| 518 | 497 |
| 519 std::string data_to_sign; | 498 std::string data_to_sign; |
| 520 if (!GetSignatureData(&data_to_sign)) { | 499 if (!GetSignatureData(&data_to_sign)) { |
| 521 LOG(ERROR) << "No data to sign."; | 500 LOG(ERROR) << "No data to sign."; |
| 522 return false; | 501 return false; |
| 523 } | 502 } |
| 524 | 503 |
| 525 std::string signature = protector::SignSetting(data_to_sign); | 504 std::string signature = protector::SignSetting(data_to_sign); |
| 526 if (signature.empty()) { | 505 if (signature.empty()) { |
| 527 LOG(ERROR) << "Signature is empty"; | 506 LOG(ERROR) << "Signature is empty"; |
| 528 return false; | 507 return false; |
| 529 } | 508 } |
| 530 | 509 |
| 531 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) { | 510 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) |
| 532 NOTREACHED() << "Failed to write signature."; | |
| 533 return false; | 511 return false; |
| 534 } | |
| 535 | 512 |
| 536 return transaction.Commit(); | 513 return transaction.Commit(); |
| 537 } | 514 } |
| 538 | 515 |
| 539 bool KeywordTable::IsBackupSignatureValid() { | 516 bool KeywordTable::IsBackupSignatureValid() { |
| 540 std::string signature; | 517 std::string signature; |
| 541 std::string signature_data; | 518 std::string signature_data; |
| 542 return meta_table_->GetValue(kBackupSignatureKey, &signature) && | 519 return meta_table_->GetValue(kBackupSignatureKey, &signature) && |
| 543 GetSignatureData(&signature_data) && | 520 GetSignatureData(&signature_data) && |
| 544 protector::IsSettingValid(signature_data, signature); | 521 protector::IsSettingValid(signature_data, signature); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 595 url->set_sync_guid(s.ColumnString(18)); | 572 url->set_sync_guid(s.ColumnString(18)); |
| 596 } | 573 } |
| 597 | 574 |
| 598 bool KeywordTable::GetKeywordAsString(TemplateURLID id, | 575 bool KeywordTable::GetKeywordAsString(TemplateURLID id, |
| 599 const std::string& table_name, | 576 const std::string& table_name, |
| 600 std::string* result) { | 577 std::string* result) { |
| 601 std::string query = | 578 std::string query = |
| 602 "SELECT " + std::string(kKeywordColumnsConcatenated) + | 579 "SELECT " + std::string(kKeywordColumnsConcatenated) + |
| 603 " FROM " + table_name + " WHERE id=?"; | 580 " FROM " + table_name + " WHERE id=?"; |
| 604 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 581 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 605 if (!s) { | |
| 606 NOTREACHED() << "Statement prepare failed"; | |
| 607 return false; | |
| 608 } | |
| 609 s.BindInt64(0, id); | 582 s.BindInt64(0, id); |
| 583 |
| 610 if (!s.Step()) { | 584 if (!s.Step()) { |
| 611 LOG(WARNING) << "No keyword with id: " << id << ", ignoring."; | 585 LOG_IF(WARNING, s.Succeeded()) |
| 586 << "No keyword with id: " << id << ", ignoring."; |
| 612 return true; | 587 return true; |
| 613 } | 588 } |
| 614 | 589 |
| 615 if (!s.Succeeded()) { | 590 if (!s.Succeeded()) |
| 616 NOTREACHED() << "Statement failed."; | |
| 617 return false; | 591 return false; |
| 618 } | |
| 619 | 592 |
| 620 *result = s.ColumnString(0); | 593 *result = s.ColumnString(0); |
| 621 return true; | 594 return true; |
| 622 } | 595 } |
| 623 | 596 |
| 624 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) { | 597 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) { |
| 625 DCHECK(id); | 598 DCHECK(id); |
| 626 int64 default_search_id = GetDefaultSearchProviderID(); | 599 int64 default_search_id = GetDefaultSearchProviderID(); |
| 627 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, | 600 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, |
| 628 default_search_id)) { | 601 default_search_id)) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 643 return false; | 616 return false; |
| 644 } | 617 } |
| 645 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) { | 618 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) { |
| 646 LOG(WARNING) << "Failed to update the keyword backup"; | 619 LOG(WARNING) << "Failed to update the keyword backup"; |
| 647 return false; | 620 return false; |
| 648 } | 621 } |
| 649 | 622 |
| 650 *backup = backup_url; | 623 *backup = backup_url; |
| 651 return true; | 624 return true; |
| 652 } | 625 } |
| OLD | NEW |