| 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 <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 columns.push_back("logo_id"); | 66 columns.push_back("logo_id"); |
| 67 } | 67 } |
| 68 columns.push_back("created_by_policy"); | 68 columns.push_back("created_by_policy"); |
| 69 columns.push_back("instant_url"); | 69 columns.push_back("instant_url"); |
| 70 columns.push_back("last_modified"); | 70 columns.push_back("last_modified"); |
| 71 columns.push_back("sync_guid"); | 71 columns.push_back("sync_guid"); |
| 72 if (version >= 47) { | 72 if (version >= 47) { |
| 73 // Column added in version 47. | 73 // Column added in version 47. |
| 74 columns.push_back("alternate_urls"); | 74 columns.push_back("alternate_urls"); |
| 75 } | 75 } |
| 76 if (version >= 48) { |
| 77 // Column added in version 48. |
| 78 columns.push_back("search_terms_replacement_key"); |
| 79 } |
| 76 | 80 |
| 77 return JoinString(columns, std::string(concatenated ? " || " : ", ")); | 81 return JoinString(columns, std::string(concatenated ? " || " : ", ")); |
| 78 } | 82 } |
| 79 | 83 |
| 80 | 84 |
| 81 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all | 85 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
| 82 // the columns in the keyword table. |id_column| is the slot number to bind | 86 // the columns in the keyword table. |id_column| is the slot number to bind |
| 83 // |data|'s |id| to; |starting_column| is the slot number of the first of a | 87 // |data|'s |id| to; |starting_column| is the slot number of the first of a |
| 84 // contiguous set of slots to bind all the other fields to. | 88 // contiguous set of slots to bind all the other fields to. |
| 85 void BindURLToStatement(const TemplateURLData& data, | 89 void BindURLToStatement(const TemplateURLData& data, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 111 s->BindInt(starting_column + 7, data.usage_count); | 115 s->BindInt(starting_column + 7, data.usage_count); |
| 112 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); | 116 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); |
| 113 s->BindBool(starting_column + 9, data.show_in_default_list); | 117 s->BindBool(starting_column + 9, data.show_in_default_list); |
| 114 s->BindString(starting_column + 10, data.suggestions_url); | 118 s->BindString(starting_column + 10, data.suggestions_url); |
| 115 s->BindInt(starting_column + 11, data.prepopulate_id); | 119 s->BindInt(starting_column + 11, data.prepopulate_id); |
| 116 s->BindBool(starting_column + 12, data.created_by_policy); | 120 s->BindBool(starting_column + 12, data.created_by_policy); |
| 117 s->BindString(starting_column + 13, data.instant_url); | 121 s->BindString(starting_column + 13, data.instant_url); |
| 118 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); | 122 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); |
| 119 s->BindString(starting_column + 15, data.sync_guid); | 123 s->BindString(starting_column + 15, data.sync_guid); |
| 120 s->BindString(starting_column + 16, alternate_urls); | 124 s->BindString(starting_column + 16, alternate_urls); |
| 125 s->BindString(starting_column + 17, data.search_terms_replacement_key); |
| 121 } | 126 } |
| 122 | 127 |
| 123 } // anonymous namespace | 128 } // anonymous namespace |
| 124 | 129 |
| 125 KeywordTable::KeywordTable(sql::Connection* db, sql::MetaTable* meta_table) | 130 KeywordTable::KeywordTable(sql::Connection* db, sql::MetaTable* meta_table) |
| 126 : WebDatabaseTable(db, meta_table), | 131 : WebDatabaseTable(db, meta_table), |
| 127 backup_overwritten_(false) { | 132 backup_overwritten_(false) { |
| 128 } | 133 } |
| 129 | 134 |
| 130 KeywordTable::~KeywordTable() {} | 135 KeywordTable::~KeywordTable() {} |
| (...skipping 11 matching lines...) Expand all Loading... |
| 142 "date_created INTEGER DEFAULT 0," | 147 "date_created INTEGER DEFAULT 0," |
| 143 "usage_count INTEGER DEFAULT 0," | 148 "usage_count INTEGER DEFAULT 0," |
| 144 "input_encodings VARCHAR," | 149 "input_encodings VARCHAR," |
| 145 "show_in_default_list INTEGER," | 150 "show_in_default_list INTEGER," |
| 146 "suggest_url VARCHAR," | 151 "suggest_url VARCHAR," |
| 147 "prepopulate_id INTEGER DEFAULT 0," | 152 "prepopulate_id INTEGER DEFAULT 0," |
| 148 "created_by_policy INTEGER DEFAULT 0," | 153 "created_by_policy INTEGER DEFAULT 0," |
| 149 "instant_url VARCHAR," | 154 "instant_url VARCHAR," |
| 150 "last_modified INTEGER DEFAULT 0," | 155 "last_modified INTEGER DEFAULT 0," |
| 151 "sync_guid VARCHAR," | 156 "sync_guid VARCHAR," |
| 152 "alternate_urls VARCHAR)") && | 157 "alternate_urls VARCHAR," |
| 158 "search_terms_replacement_key VARCHAR)") && |
| 153 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber)); | 159 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber)); |
| 154 } | 160 } |
| 155 | 161 |
| 156 bool KeywordTable::IsSyncable() { | 162 bool KeywordTable::IsSyncable() { |
| 157 return true; | 163 return true; |
| 158 } | 164 } |
| 159 | 165 |
| 160 bool KeywordTable::AddKeyword(const TemplateURLData& data) { | 166 bool KeywordTable::AddKeyword(const TemplateURLData& data) { |
| 161 DCHECK(data.id); | 167 DCHECK(data.id); |
| 162 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + | 168 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + |
| 163 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); | 169 ") VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"); |
| 164 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 170 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 165 BindURLToStatement(data, &s, 0, 1); | 171 BindURLToStatement(data, &s, 0, 1); |
| 166 | 172 |
| 167 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 173 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
| 168 } | 174 } |
| 169 | 175 |
| 170 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 176 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
| 171 DCHECK(id); | 177 DCHECK(id); |
| 172 sql::Statement s( | 178 sql::Statement s( |
| 173 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); | 179 db_->GetUniqueStatement("DELETE FROM keywords WHERE id = ?")); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 196 return succeeded; | 202 return succeeded; |
| 197 } | 203 } |
| 198 | 204 |
| 199 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { | 205 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { |
| 200 DCHECK(data.id); | 206 DCHECK(data.id); |
| 201 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " | 207 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " |
| 202 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " | 208 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " |
| 203 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " | 209 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " |
| 204 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " | 210 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " |
| 205 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=?, " | 211 "created_by_policy=?, instant_url=?, last_modified=?, sync_guid=?, " |
| 206 "alternate_urls=? WHERE id=?")); | 212 "alternate_urls=?, search_terms_replacement_key=? WHERE id=?")); |
| 207 BindURLToStatement(data, &s, 17, 0); // "17" binds id() as the last item. | 213 BindURLToStatement(data, &s, 18, 0); // "18" binds id() as the last item. |
| 208 | 214 |
| 209 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 215 return s.Run() && UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
| 210 } | 216 } |
| 211 | 217 |
| 212 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 218 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| 213 // Added for http://crbug.com/116952. | 219 // Added for http://crbug.com/116952. |
| 214 UMA_HISTOGRAM_COUNTS_100("Search.DefaultSearchProviderID", | 220 UMA_HISTOGRAM_COUNTS_100("Search.DefaultSearchProviderID", |
| 215 static_cast<int32>(id)); | 221 static_cast<int32>(id)); |
| 216 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 222 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 217 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); | 223 UpdateBackupSignature(WebDatabase::kCurrentVersionNumber); |
| (...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 // Old backup was invalid; drop the table entirely, which will trigger the | 438 // Old backup was invalid; drop the table entirely, which will trigger the |
| 433 // protector code to prompt the user and recreate the table. | 439 // protector code to prompt the user and recreate the table. |
| 434 if (db_->DoesTableExist("keywords_backup") && | 440 if (db_->DoesTableExist("keywords_backup") && |
| 435 !db_->Execute("DROP TABLE keywords_backup")) | 441 !db_->Execute("DROP TABLE keywords_backup")) |
| 436 return false; | 442 return false; |
| 437 } | 443 } |
| 438 | 444 |
| 439 return transaction.Commit(); | 445 return transaction.Commit(); |
| 440 } | 446 } |
| 441 | 447 |
| 448 bool KeywordTable::MigrateToVersion48AddSearchTermsReplacementKeyColumn() { |
| 449 sql::Transaction transaction(db_); |
| 450 |
| 451 // Fill the |search_terms_replacement_key| column with empty strings, |
| 452 // otherwise it breaks code relying on GetTableContents that concatenates the |
| 453 // strings from all the columns. |
| 454 if (!transaction.Begin() || |
| 455 !db_->Execute("ALTER TABLE keywords ADD COLUMN " |
| 456 "search_terms_replacement_key VARCHAR DEFAULT ''")) |
| 457 return false; |
| 458 |
| 459 if (IsBackupSignatureValid(47)) { |
| 460 // Migrate the keywords backup table as well. |
| 461 if (!db_->Execute("ALTER TABLE keywords_backup ADD COLUMN " |
| 462 "search_terms_replacement_key VARCHAR DEFAULT ''") || |
| 463 !SignBackup(48)) |
| 464 return false; |
| 465 } else { |
| 466 // Old backup was invalid; drop the table entirely, which will trigger the |
| 467 // protector code to prompt the user and recreate the table. |
| 468 if (db_->DoesTableExist("keywords_backup") && |
| 469 !db_->Execute("DROP TABLE keywords_backup")) |
| 470 return false; |
| 471 } |
| 472 |
| 473 return transaction.Commit(); |
| 474 } |
| 475 |
| 442 // static | 476 // static |
| 443 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, | 477 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
| 444 TemplateURLData* data) { | 478 TemplateURLData* data) { |
| 445 DCHECK(data); | 479 DCHECK(data); |
| 446 | 480 |
| 447 data->short_name = s.ColumnString16(1); | 481 data->short_name = s.ColumnString16(1); |
| 448 data->SetKeyword(s.ColumnString16(2)); | 482 data->SetKeyword(s.ColumnString16(2)); |
| 449 // Due to past bugs, we might have persisted entries with empty URLs. Avoid | 483 // Due to past bugs, we might have persisted entries with empty URLs. Avoid |
| 450 // reading these out. (GetKeywords() will delete these entries on return.) | 484 // reading these out. (GetKeywords() will delete these entries on return.) |
| 451 // NOTE: This code should only be needed as long as we might be reading such | 485 // NOTE: This code should only be needed as long as we might be reading such |
| (...skipping 21 matching lines...) Expand all Loading... |
| 473 scoped_ptr<Value> value(json_reader.ReadToValue(s.ColumnString(17))); | 507 scoped_ptr<Value> value(json_reader.ReadToValue(s.ColumnString(17))); |
| 474 ListValue* alternate_urls_value; | 508 ListValue* alternate_urls_value; |
| 475 if (value.get() && value->GetAsList(&alternate_urls_value)) { | 509 if (value.get() && value->GetAsList(&alternate_urls_value)) { |
| 476 std::string alternate_url; | 510 std::string alternate_url; |
| 477 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { | 511 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { |
| 478 if (alternate_urls_value->GetString(i, &alternate_url)) | 512 if (alternate_urls_value->GetString(i, &alternate_url)) |
| 479 data->alternate_urls.push_back(alternate_url); | 513 data->alternate_urls.push_back(alternate_url); |
| 480 } | 514 } |
| 481 } | 515 } |
| 482 | 516 |
| 517 data->search_terms_replacement_key = s.ColumnString(18); |
| 518 |
| 483 return true; | 519 return true; |
| 484 } | 520 } |
| 485 | 521 |
| 486 bool KeywordTable::GetSignatureData(int table_version, std::string* backup) { | 522 bool KeywordTable::GetSignatureData(int table_version, std::string* backup) { |
| 487 DCHECK(backup); | 523 DCHECK(backup); |
| 488 | 524 |
| 489 int64 backup_value = kInvalidTemplateURLID; | 525 int64 backup_value = kInvalidTemplateURLID; |
| 490 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { | 526 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { |
| 491 LOG(ERROR) << "No backup id for signing."; | 527 LOG(ERROR) << "No backup id for signing."; |
| 492 return false; | 528 return false; |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 684 } | 720 } |
| 685 } | 721 } |
| 686 | 722 |
| 687 // Replace the old table with the new one. | 723 // Replace the old table with the new one. |
| 688 sql = "DROP TABLE " + name; | 724 sql = "DROP TABLE " + name; |
| 689 if (!db_->Execute(sql.c_str())) | 725 if (!db_->Execute(sql.c_str())) |
| 690 return false; | 726 return false; |
| 691 sql = "ALTER TABLE keywords_temp RENAME TO " + name; | 727 sql = "ALTER TABLE keywords_temp RENAME TO " + name; |
| 692 return db_->Execute(sql.c_str()); | 728 return db_->Execute(sql.c_str()); |
| 693 } | 729 } |
| OLD | NEW |