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

Side by Side Diff: chrome/browser/webdata/keyword_table.cc

Issue 9071014: Database usage adjustment for .../history (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 11 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 (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
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
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) { 229 if (!s.is_valid()) {
231 NOTREACHED() << "Statement prepare failed";
232 return NULL; 230 return NULL;
233 } 231 }
234 s.BindInt64(0, backup_id); 232 s.BindInt64(0, backup_id);
235 if (!s.Step()) { 233 if (!s.Step()) {
236 LOG(ERROR) << "No default search provider with backup id."; 234 LOG(ERROR) << "No default search provider with backup id.";
Scott Hess - ex-Googler 2012/01/03 23:41:57 Drop the is_valid(), and use LOG_IF(s.Succeeded(),
Greg Billock 2012/01/04 19:20:20 Done.
237 return NULL; 235 return NULL;
238 } 236 }
239 237
240 scoped_ptr<TemplateURL> template_url(new TemplateURL()); 238 scoped_ptr<TemplateURL> template_url(new TemplateURL());
241 GetURLFromStatement(s, template_url.get()); 239 GetURLFromStatement(s, template_url.get());
242 // ID has no meaning for the backup and should be 0 in case the TemplateURL 240 // ID has no meaning for the backup and should be 0 in case the TemplateURL
243 // will be added to keywords if missing. 241 // will be added to keywords if missing.
244 template_url->set_id(0); 242 template_url->set_id(0);
245 243
246 if (!s.Succeeded()) { 244 if (!s.Succeeded()) {
247 LOG(ERROR) << "Statement has not succeeded."; 245 LOG(ERROR) << "Statement has not succeeded.";
Scott Hess - ex-Googler 2012/01/03 23:41:57 Actually, I don't understand what this statement i
Greg Billock 2012/01/04 19:20:20 Yes, this is amiss. It is dead code. It could be t
248 return NULL; 246 return NULL;
249 } 247 }
250 return template_url.release(); 248 return template_url.release();
251 } 249 }
252 250
253 bool KeywordTable::DidDefaultSearchProviderChange() { 251 bool KeywordTable::DidDefaultSearchProviderChange() {
254 if (!IsBackupSignatureValid()) { 252 if (!IsBackupSignatureValid()) {
255 UMA_HISTOGRAM_ENUMERATION( 253 UMA_HISTOGRAM_ENUMERATION(
256 protector::kProtectorHistogramDefaultSearchProvider, 254 protector::kProtectorHistogramDefaultSearchProvider,
257 protector::kProtectorErrorBackupInvalid, 255 protector::kProtectorErrorBackupInvalid,
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 } 391 }
394 392
395 bool KeywordTable::MigrateToVersion41RewriteDefaultSearchProviderBackup() { 393 bool KeywordTable::MigrateToVersion41RewriteDefaultSearchProviderBackup() {
396 // Due to crbug.com/101815 version 40 may contain corrupt or empty 394 // Due to crbug.com/101815 version 40 may contain corrupt or empty
397 // signature. So ignore the signature and simply rewrite it. 395 // signature. So ignore the signature and simply rewrite it.
398 return MigrateToVersion40AddDefaultSearchProviderBackup(); 396 return MigrateToVersion40AddDefaultSearchProviderBackup();
399 } 397 }
400 398
401 bool KeywordTable::MigrateToVersion42AddFullDefaultSearchProviderBackup() { 399 bool KeywordTable::MigrateToVersion42AddFullDefaultSearchProviderBackup() {
402 sql::Transaction transaction(db_); 400 sql::Transaction transaction(db_);
403 if (!transaction.Begin()) { 401 if (!transaction.Begin())
404 NOTREACHED() << "Failed to start transaction";
405 return false; 402 return false;
406 }
407 403
408 int64 id = 0; 404 int64 id = 0;
409 if (!UpdateDefaultSearchProviderIDBackup(&id)) 405 if (!UpdateDefaultSearchProviderIDBackup(&id))
410 return false; 406 return false;
411 407
412 std::string keyword_backup; 408 std::string keyword_backup;
413 if (!UpdateDefaultSearchProviderBackup(id, &keyword_backup)) 409 if (!UpdateDefaultSearchProviderBackup(id, &keyword_backup))
414 return false; 410 return false;
415 411
416 std::string keywords; 412 std::string keywords;
417 if (!GetTableContents("keywords", &keywords)) { 413 if (!GetTableContents("keywords", &keywords))
Scott Hess - ex-Googler 2012/01/03 23:41:57 Looks like failing this one would have caused many
Greg Billock 2012/01/04 19:20:20 :-) On 2012/01/03 23:41:57, shess wrote:
418 NOTREACHED() << "Can't get keywords table contents to sign";
419 return false; 414 return false;
420 }
421 415
422 std::string data_to_sign = base::Int64ToString(id) + 416 std::string data_to_sign = base::Int64ToString(id) +
423 keyword_backup + 417 keyword_backup +
424 keywords; 418 keywords;
425 std::string signature = protector::SignSetting(data_to_sign); 419 std::string signature = protector::SignSetting(data_to_sign);
426 if (signature.empty()) { 420 if (signature.empty())
Scott Hess - ex-Googler 2012/01/03 23:41:57 Is the NOTREACHED() in this case WRT sql/ API, or
Greg Billock 2012/01/04 19:20:20 Misfire. I should have kept NOTREACHED and done in
427 NOTREACHED() << "Signature is empty";
428 return false; 421 return false;
429 } 422 if (!meta_table_->SetValue(kBackupSignatureKey, signature))
430 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) {
431 NOTREACHED() << "Failed to write signature.";
432 return false; 423 return false;
433 }
434 424
435 return transaction.Commit(); 425 return transaction.Commit();
436 } 426 }
437 427
438 bool KeywordTable::MigrateToVersion43AddKeywordsBackupTable() { 428 bool KeywordTable::MigrateToVersion43AddKeywordsBackupTable() {
439 return meta_table_->SetValue(kDefaultSearchBackupKey, std::string()) && 429 return meta_table_->SetValue(kDefaultSearchBackupKey, std::string()) &&
440 UpdateBackupSignature(); 430 UpdateBackupSignature();
441 } 431 }
442 432
443 bool KeywordTable::GetSignatureData(std::string* backup) { 433 bool KeywordTable::GetSignatureData(std::string* backup) {
(...skipping 17 matching lines...) Expand all
461 bool KeywordTable::GetTableContents(const char* table_name, 451 bool KeywordTable::GetTableContents(const char* table_name,
462 std::string* contents) { 452 std::string* contents) {
463 DCHECK(contents); 453 DCHECK(contents);
464 std::string table_data; 454 std::string table_data;
465 455
466 std::string query = 456 std::string query =
467 "SELECT " + std::string(kKeywordColumnsConcatenated) + 457 "SELECT " + std::string(kKeywordColumnsConcatenated) +
468 " FROM " + std::string(table_name) + " ORDER BY id ASC"; 458 " FROM " + std::string(table_name) + " ORDER BY id ASC";
469 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name), 459 sql::Statement s(db_->GetCachedStatement(sql::StatementID(table_name),
470 query.c_str())); 460 query.c_str()));
471 if (!s) {
472 NOTREACHED() << "Statement prepare failed";
473 return false;
474 }
475 while (s.Step()) 461 while (s.Step())
476 table_data += s.ColumnString(0); 462 table_data += s.ColumnString(0);
477 if (!s.Succeeded()) { 463 if (!s.Succeeded())
478 NOTREACHED() << "Statement execution failed";
479 return false; 464 return false;
480 } 465
481 *contents = table_data; 466 *contents = table_data;
482 return true; 467 return true;
483 } 468 }
484 469
485 bool KeywordTable::UpdateBackupSignature() { 470 bool KeywordTable::UpdateBackupSignature() {
486 sql::Transaction transaction(db_); 471 sql::Transaction transaction(db_);
487 if (!transaction.Begin()) { 472 if (!transaction.Begin())
488 NOTREACHED() << "Failed to start transaction";
489 return false; 473 return false;
490 }
491 474
492 int64 id = 0; 475 int64 id = 0;
493 if (!UpdateDefaultSearchProviderIDBackup(&id)) { 476 if (!UpdateDefaultSearchProviderIDBackup(&id)) {
494 LOG(ERROR) << "Failed to update default search id backup."; 477 LOG(ERROR) << "Failed to update default search id backup.";
495 return false; 478 return false;
496 } 479 }
497 480
498 // Backup of all keywords. 481 // Backup of all keywords.
499 if (db_->DoesTableExist("keywords_backup") && 482 if (db_->DoesTableExist("keywords_backup") &&
500 !db_->Execute("DROP TABLE keywords_backup")) 483 !db_->Execute("DROP TABLE keywords_backup")) {
501 return false; 484 return false;
485 }
502 486
503 if (!db_->Execute( 487 if (!db_->Execute(
504 "CREATE TABLE keywords_backup AS " 488 "CREATE TABLE keywords_backup AS "
505 "SELECT id, short_name, keyword, favicon_url, url, " 489 "SELECT id, short_name, keyword, favicon_url, url, "
506 "safe_for_autoreplace, originating_url, date_created, " 490 "safe_for_autoreplace, originating_url, date_created, "
507 "usage_count, input_encodings, show_in_default_list, " 491 "usage_count, input_encodings, show_in_default_list, "
508 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, " 492 "suggest_url, prepopulate_id, autogenerate_keyword, logo_id, "
509 "created_by_policy, instant_url, last_modified, sync_guid " 493 "created_by_policy, instant_url, last_modified, sync_guid "
510 "FROM keywords ORDER BY id ASC")) { 494 "FROM keywords ORDER BY id ASC")) {
511 LOG(ERROR) << "Failed to create keywords_backup table."; 495 LOG(ERROR) << "Failed to create keywords_backup table.";
512 return false; 496 return false;
513 } 497 }
514 498
515 std::string data_to_sign; 499 std::string data_to_sign;
516 if (!GetSignatureData(&data_to_sign)) { 500 if (!GetSignatureData(&data_to_sign)) {
517 LOG(ERROR) << "No data to sign."; 501 LOG(ERROR) << "No data to sign.";
518 return false; 502 return false;
519 } 503 }
520 504
521 std::string signature = protector::SignSetting(data_to_sign); 505 std::string signature = protector::SignSetting(data_to_sign);
522 if (signature.empty()) { 506 if (signature.empty()) {
523 LOG(ERROR) << "Signature is empty"; 507 LOG(ERROR) << "Signature is empty";
524 return false; 508 return false;
525 } 509 }
526 510
527 if (!meta_table_->SetValue(kBackupSignatureKey, signature)) { 511 if (!meta_table_->SetValue(kBackupSignatureKey, signature))
528 NOTREACHED() << "Failed to write signature.";
529 return false; 512 return false;
530 }
531 513
532 return transaction.Commit(); 514 return transaction.Commit();
533 } 515 }
534 516
535 bool KeywordTable::IsBackupSignatureValid() { 517 bool KeywordTable::IsBackupSignatureValid() {
536 std::string signature; 518 std::string signature;
537 std::string signature_data; 519 std::string signature_data;
538 return meta_table_->GetValue(kBackupSignatureKey, &signature) && 520 return meta_table_->GetValue(kBackupSignatureKey, &signature) &&
539 GetSignatureData(&signature_data) && 521 GetSignatureData(&signature_data) &&
540 protector::IsSettingValid(signature_data, signature); 522 protector::IsSettingValid(signature_data, signature);
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
591 url->set_sync_guid(s.ColumnString(18)); 573 url->set_sync_guid(s.ColumnString(18));
592 } 574 }
593 575
594 bool KeywordTable::GetKeywordAsString(TemplateURLID id, 576 bool KeywordTable::GetKeywordAsString(TemplateURLID id,
595 const std::string& table_name, 577 const std::string& table_name,
596 std::string* result) { 578 std::string* result) {
597 std::string query = 579 std::string query =
598 "SELECT " + std::string(kKeywordColumnsConcatenated) + 580 "SELECT " + std::string(kKeywordColumnsConcatenated) +
599 " FROM " + table_name + " WHERE id=?"; 581 " FROM " + table_name + " WHERE id=?";
600 sql::Statement s(db_->GetUniqueStatement(query.c_str())); 582 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); 583 s.BindInt64(0, id);
584
606 if (!s.Step()) { 585 if (!s.Step()) {
607 LOG(WARNING) << "No keyword with id: " << id << ", ignoring."; 586 LOG(WARNING) << "No keyword with id: " << id << ", ignoring.";
608 return true; 587 return true;
609 } 588 }
610 589
611 if (!s.Succeeded()) { 590 if (!s.Succeeded())
612 NOTREACHED() << "Statement failed.";
613 return false; 591 return false;
614 }
615 592
616 *result = s.ColumnString(0); 593 *result = s.ColumnString(0);
617 return true; 594 return true;
618 } 595 }
619 596
620 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) { 597 bool KeywordTable::UpdateDefaultSearchProviderIDBackup(TemplateURLID* id) {
621 DCHECK(id); 598 DCHECK(id);
622 int64 default_search_id = GetDefaultSearchProviderID(); 599 int64 default_search_id = GetDefaultSearchProviderID();
623 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, 600 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey,
624 default_search_id)) { 601 default_search_id)) {
(...skipping 14 matching lines...) Expand all
639 return false; 616 return false;
640 } 617 }
641 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) { 618 if (!meta_table_->SetValue(kDefaultSearchBackupKey, backup_url)) {
642 LOG(WARNING) << "Failed to update the keyword backup"; 619 LOG(WARNING) << "Failed to update the keyword backup";
643 return false; 620 return false;
644 } 621 }
645 622
646 *backup = backup_url; 623 *backup = backup_url;
647 return true; 624 return true;
648 } 625 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698