Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 scoped_ptr<TemplateURL> template_url(new TemplateURL()); |
|
Ivan Korotkov
2012/01/13 09:53:41
Looks like there is no more need in scoped_ptr her
Greg Billock
2012/01/13 20:23:19
Done.
| |
| 241 GetURLFromStatement(s, template_url.get()); | 238 GetURLFromStatement(s, template_url.get()); |
| 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()) { | |
| 247 LOG(ERROR) << "Statement has not succeeded."; | |
| 248 return NULL; | |
| 249 } | |
| 250 return template_url.release(); | 243 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."; |
| (...skipping 133 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::GetSignatureData(std::string* backup) { | 428 bool KeywordTable::GetSignatureData(std::string* backup) { |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 461 bool KeywordTable::GetTableContents(const char* table_name, | 446 bool KeywordTable::GetTableContents(const char* table_name, |
| 462 std::string* contents) { | 447 std::string* contents) { |
| 463 DCHECK(contents); | 448 DCHECK(contents); |
| 464 std::string table_data; | 449 std::string table_data; |
| 465 | 450 |
| 466 std::string query = | 451 std::string query = |
| 467 "SELECT " + std::string(kKeywordColumnsConcatenated) + | 452 "SELECT " + std::string(kKeywordColumnsConcatenated) + |
| 468 " FROM " + std::string(table_name) + " ORDER BY id ASC"; | 453 " FROM " + std::string(table_name) + " ORDER BY id ASC"; |
| 469 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name), | 454 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name), |
| 470 query.c_str())); | 455 query.c_str())); |
| 471 if (!s) { | |
| 472 NOTREACHED() << "Statement prepare failed"; | |
| 473 return false; | |
| 474 } | |
| 475 while (s.Step()) | 456 while (s.Step()) |
| 476 table_data += s.ColumnString(0); | 457 table_data += s.ColumnString(0); |
| 477 if (!s.Succeeded()) { | 458 if (!s.Succeeded()) |
| 478 NOTREACHED() << "Statement execution failed"; | |
| 479 return false; | 459 return false; |
| 480 } | 460 |
| 481 *contents = table_data; | 461 *contents = table_data; |
| 482 return true; | 462 return true; |
| 483 } | 463 } |
| 484 | 464 |
| 485 bool KeywordTable::UpdateBackupSignature() { | 465 bool KeywordTable::UpdateBackupSignature() { |
| 486 sql::Transaction transaction(db_); | 466 sql::Transaction transaction(db_); |
| 487 if (!transaction.Begin()) { | 467 if (!transaction.Begin()) |
| 488 NOTREACHED() << "Failed to start transaction"; | |
| 489 return false; | 468 return false; |
| 490 } | |
| 491 | 469 |
| 492 int64 id = 0; | 470 int64 id = 0; |
| 493 if (!UpdateDefaultSearchProviderIDBackup(&id)) { | 471 if (!UpdateDefaultSearchProviderIDBackup(&id)) { |
| 494 LOG(ERROR) << "Failed to update default search id backup."; | 472 LOG(ERROR) << "Failed to update default search id backup."; |
| 495 return false; | 473 return false; |
| 496 } | 474 } |
| 497 | 475 |
| 498 // Backup of all keywords. | 476 // Backup of all keywords. |
| 499 if (db_->DoesTableExist("keywords_backup") && | 477 if (db_->DoesTableExist("keywords_backup") && |
| 500 !db_->Execute("DROP TABLE keywords_backup")) | 478 !db_->Execute("DROP TABLE keywords_backup")) { |
| 501 return false; | 479 return false; |
| 480 } | |
| 502 | 481 |
| 503 if (!db_->Execute( | 482 if (!db_->Execute( |
| 504 "CREATE TABLE keywords_backup AS " | 483 "CREATE TABLE keywords_backup AS " |
| 505 "SELECT id, short_name, keyword, favicon_url, url, " | 484 "SELECT id, short_name, keyword, favicon_url, url, " |
| 506 "safe_for_autoreplace, originating_url, date_created, " | 485 "safe_for_autoreplace, originating_url, date_created, " |
| 507 "usage_count, input_encodings, show_in_default_list, " | 486 "usage_count, input_encodings, show_in_default_list, " |
| 508 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " | 487 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " |
| 509 "created_by_policy, instant_url, last_modified, sync_guid " | 488 "created_by_policy, instant_url, last_modified, sync_guid " |
| 510 "FROM keywords ORDER BY id ASC")) { | 489 "FROM keywords ORDER BY id ASC")) { |
| 511 LOG(ERROR) << "Failed to create keywords_backup table."; | 490 LOG(ERROR) << "Failed to create keywords_backup table."; |
| 512 return false; | 491 return false; |
| 513 } | 492 } |
| 514 | 493 |
| 515 std::string data_to_sign; | 494 std::string data_to_sign; |
| 516 if (!GetSignatureData(&data_to_sign)) { | 495 if (!GetSignatureData(&data_to_sign)) { |
| 517 LOG(ERROR) << "No data to sign."; | 496 LOG(ERROR) << "No data to sign."; |
| 518 return false; | 497 return false; |
| 519 } | 498 } |
| 520 | 499 |
| 521 std::string signature = protector::SignSetting(data_to_sign); | 500 std::string signature = protector::SignSetting(data_to_sign); |
| 522 if (signature.empty()) { | 501 if (signature.empty()) { |
| 523 LOG(ERROR) << "Signature is empty"; | 502 LOG(ERROR) << "Signature is empty"; |
| 524 return false; | 503 return false; |
| 525 } | 504 } |
| 526 | 505 |
| 527 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) { | 506 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) |
| 528 NOTREACHED() << "Failed to write signature."; | |
| 529 return false; | 507 return false; |
| 530 } | |
| 531 | 508 |
| 532 return transaction.Commit(); | 509 return transaction.Commit(); |
| 533 } | 510 } |
| 534 | 511 |
| 535 bool KeywordTable::IsBackupSignatureValid() { | 512 bool KeywordTable::IsBackupSignatureValid() { |
| 536 std::string signature; | 513 std::string signature; |
| 537 std::string signature_data; | 514 std::string signature_data; |
| 538 return meta_table_->GetValue(kBackupSignatureKey, &signature) && | 515 return meta_table_->GetValue(kBackupSignatureKey, &signature) && |
| 539 GetSignatureData(&signature_data) && | 516 GetSignatureData(&signature_data) && |
| 540 protector::IsSettingValid(signature_data, signature); | 517 protector::IsSettingValid(signature_data, signature); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 591 url->set_sync_guid(s.ColumnString(18)); | 568 url->set_sync_guid(s.ColumnString(18)); |
| 592 } | 569 } |
| 593 | 570 |
| 594 bool KeywordTable::GetKeywordAsString(TemplateURLID id, | 571 bool KeywordTable::GetKeywordAsString(TemplateURLID id, |
| 595 const std::string& table_name, | 572 const std::string& table_name, |
| 596 std::string* result) { | 573 std::string* result) { |
| 597 std::string query = | 574 std::string query = |
| 598 "SELECT " + std::string(kKeywordColumnsConcatenated) + | 575 "SELECT " + std::string(kKeywordColumnsConcatenated) + |
| 599 " FROM " + table_name + " WHERE id=?"; | 576 " FROM " + table_name + " WHERE id=?"; |
| 600 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 577 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 601 if (!s) { | |
| 602 NOTREACHED() << "Statement prepare failed"; | |
| 603 return false; | |
| 604 } | |
| 605 s.BindInt64(0, id); | 578 s.BindInt64(0, id); |
| 579 | |
| 606 if (!s.Step()) { | 580 if (!s.Step()) { |
| 607 LOG(WARNING) << "No keyword with id: " << id << ", ignoring."; | 581 LOG(WARNING) << "No keyword with id: " << id << ", ignoring."; |
|
Ivan Korotkov
2012/01/13 09:53:41
Shouldn't this be a LOG_IF(..., s.Succeeded(), ...
Greg Billock
2012/01/13 20:23:19
Yeah, that's probably better to maintain semantics
| |
| 608 return true; | 582 return true; |
| 609 } | 583 } |
| 610 | 584 |
| 611 if (!s.Succeeded()) { | 585 if (!s.Succeeded()) |
| 612 NOTREACHED() << "Statement failed."; | |
| 613 return false; | 586 return false; |
| 614 } | |
| 615 | 587 |
| 616 *result = s.ColumnString(0); | 588 *result = s.ColumnString(0); |
| 617 return true; | 589 return true; |
| 618 } | 590 } |
| 619 | 591 |
| 620 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) { | 592 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) { |
| 621 DCHECK(id); | 593 DCHECK(id); |
| 622 int64 default_search_id = GetDefaultSearchProviderID(); | 594 int64 default_search_id = GetDefaultSearchProviderID(); |
| 623 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, | 595 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, |
| 624 default_search_id)) { | 596 default_search_id)) { |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 639 return false; | 611 return false; |
| 640 } | 612 } |
| 641 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) { | 613 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) { |
| 642 LOG(WARNING) << "Failed to update the keyword backup"; | 614 LOG(WARNING) << "Failed to update the keyword backup"; |
| 643 return false; | 615 return false; |
| 644 } | 616 } |
| 645 | 617 |
| 646 *backup = backup_url; | 618 *backup = backup_url; |
| 647 return true; | 619 return true; |
| 648 } | 620 } |
| OLD | NEW |