| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/search_engines/keyword_table.h" | 5 #include "components/search_engines/keyword_table.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 columns.push_back("image_url"); | 77 columns.push_back("image_url"); |
| 78 columns.push_back("search_url_post_params"); | 78 columns.push_back("search_url_post_params"); |
| 79 columns.push_back("suggest_url_post_params"); | 79 columns.push_back("suggest_url_post_params"); |
| 80 columns.push_back("instant_url_post_params"); | 80 columns.push_back("instant_url_post_params"); |
| 81 columns.push_back("image_url_post_params"); | 81 columns.push_back("image_url_post_params"); |
| 82 } | 82 } |
| 83 if (version >= 53) { | 83 if (version >= 53) { |
| 84 // Column added in version 53. | 84 // Column added in version 53. |
| 85 columns.push_back("new_tab_url"); | 85 columns.push_back("new_tab_url"); |
| 86 } | 86 } |
| 87 if (version >= 69) { |
| 88 // Column added in version 69. |
| 89 columns.push_back("last_visited"); |
| 90 } |
| 87 | 91 |
| 88 return base::JoinString(columns, std::string(concatenated ? " || " : ", ")); | 92 return base::JoinString(columns, std::string(concatenated ? " || " : ", ")); |
| 89 } | 93 } |
| 90 | 94 |
| 91 | 95 |
| 92 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all | 96 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
| 93 // the columns in the keyword table. |id_column| is the slot number to bind | 97 // the columns in the keyword table. |id_column| is the slot number to bind |
| 94 // |data|'s |id| to; |starting_column| is the slot number of the first of a | 98 // |data|'s |id| to; |starting_column| is the slot number of the first of a |
| 95 // contiguous set of slots to bind all the other fields to. | 99 // contiguous set of slots to bind all the other fields to. |
| 96 void BindURLToStatement(const TemplateURLData& data, | 100 void BindURLToStatement(const TemplateURLData& data, |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 s->BindInt64(starting_column + 13, data.last_modified.ToTimeT()); | 133 s->BindInt64(starting_column + 13, data.last_modified.ToTimeT()); |
| 130 s->BindString(starting_column + 14, data.sync_guid); | 134 s->BindString(starting_column + 14, data.sync_guid); |
| 131 s->BindString(starting_column + 15, alternate_urls); | 135 s->BindString(starting_column + 15, alternate_urls); |
| 132 s->BindString(starting_column + 16, data.search_terms_replacement_key); | 136 s->BindString(starting_column + 16, data.search_terms_replacement_key); |
| 133 s->BindString(starting_column + 17, data.image_url); | 137 s->BindString(starting_column + 17, data.image_url); |
| 134 s->BindString(starting_column + 18, data.search_url_post_params); | 138 s->BindString(starting_column + 18, data.search_url_post_params); |
| 135 s->BindString(starting_column + 19, data.suggestions_url_post_params); | 139 s->BindString(starting_column + 19, data.suggestions_url_post_params); |
| 136 s->BindString(starting_column + 20, data.instant_url_post_params); | 140 s->BindString(starting_column + 20, data.instant_url_post_params); |
| 137 s->BindString(starting_column + 21, data.image_url_post_params); | 141 s->BindString(starting_column + 21, data.image_url_post_params); |
| 138 s->BindString(starting_column + 22, data.new_tab_url); | 142 s->BindString(starting_column + 22, data.new_tab_url); |
| 143 s->BindInt64(starting_column + 23, data.last_visited.ToTimeT()); |
| 139 } | 144 } |
| 140 | 145 |
| 141 WebDatabaseTable::TypeKey GetKey() { | 146 WebDatabaseTable::TypeKey GetKey() { |
| 142 // We just need a unique constant. Use the address of a static that | 147 // We just need a unique constant. Use the address of a static that |
| 143 // COMDAT folding won't touch in an optimizing linker. | 148 // COMDAT folding won't touch in an optimizing linker. |
| 144 static int table_key = 0; | 149 static int table_key = 0; |
| 145 return reinterpret_cast<void*>(&table_key); | 150 return reinterpret_cast<void*>(&table_key); |
| 146 } | 151 } |
| 147 | 152 |
| 148 } // namespace | 153 } // namespace |
| 149 | 154 |
| 150 KeywordTable::KeywordTable() { | 155 KeywordTable::KeywordTable() { |
| 151 } | 156 } |
| 152 | 157 |
| 153 KeywordTable::~KeywordTable() {} | 158 KeywordTable::~KeywordTable() {} |
| 154 | 159 |
| 155 KeywordTable* KeywordTable::FromWebDatabase(WebDatabase* db) { | 160 KeywordTable* KeywordTable::FromWebDatabase(WebDatabase* db) { |
| 156 return static_cast<KeywordTable*>(db->GetTable(GetKey())); | 161 return static_cast<KeywordTable*>(db->GetTable(GetKey())); |
| 157 } | 162 } |
| 158 | 163 |
| 159 WebDatabaseTable::TypeKey KeywordTable::GetTypeKey() const { | 164 WebDatabaseTable::TypeKey KeywordTable::GetTypeKey() const { |
| 160 return GetKey(); | 165 return GetKey(); |
| 161 } | 166 } |
| 162 | 167 |
| 163 bool KeywordTable::CreateTablesIfNecessary() { | 168 bool KeywordTable::CreateTablesIfNecessary() { |
| 164 return db_->DoesTableExist("keywords") || | 169 return db_->DoesTableExist("keywords") || |
| 165 db_->Execute("CREATE TABLE keywords (" | 170 db_->Execute("CREATE TABLE keywords (" |
| 166 "id INTEGER PRIMARY KEY," | 171 "id INTEGER PRIMARY KEY," |
| 167 "short_name VARCHAR NOT NULL," | 172 "short_name VARCHAR NOT NULL," |
| 168 "keyword VARCHAR NOT NULL," | 173 "keyword VARCHAR NOT NULL," |
| 169 "favicon_url VARCHAR NOT NULL," | 174 "favicon_url VARCHAR NOT NULL," |
| 170 "url VARCHAR NOT NULL," | 175 "url VARCHAR NOT NULL," |
| 171 "safe_for_autoreplace INTEGER," | 176 "safe_for_autoreplace INTEGER," |
| 172 "originating_url VARCHAR," | 177 "originating_url VARCHAR," |
| 173 "date_created INTEGER DEFAULT 0," | 178 "date_created INTEGER DEFAULT 0," |
| 174 "usage_count INTEGER DEFAULT 0," | 179 "usage_count INTEGER DEFAULT 0," |
| 175 "input_encodings VARCHAR," | 180 "input_encodings VARCHAR," |
| 176 "suggest_url VARCHAR," | 181 "suggest_url VARCHAR," |
| 177 "prepopulate_id INTEGER DEFAULT 0," | 182 "prepopulate_id INTEGER DEFAULT 0," |
| 178 "created_by_policy INTEGER DEFAULT 0," | 183 "created_by_policy INTEGER DEFAULT 0," |
| 179 "instant_url VARCHAR," | 184 "instant_url VARCHAR," |
| 180 "last_modified INTEGER DEFAULT 0," | 185 "last_modified INTEGER DEFAULT 0," |
| 181 "sync_guid VARCHAR," | 186 "sync_guid VARCHAR," |
| 182 "alternate_urls VARCHAR," | 187 "alternate_urls VARCHAR," |
| 183 "search_terms_replacement_key VARCHAR," | 188 "search_terms_replacement_key VARCHAR," |
| 184 "image_url VARCHAR," | 189 "image_url VARCHAR," |
| 185 "search_url_post_params VARCHAR," | 190 "search_url_post_params VARCHAR," |
| 186 "suggest_url_post_params VARCHAR," | 191 "suggest_url_post_params VARCHAR," |
| 187 "instant_url_post_params VARCHAR," | 192 "instant_url_post_params VARCHAR," |
| 188 "image_url_post_params VARCHAR," | 193 "image_url_post_params VARCHAR," |
| 189 "new_tab_url VARCHAR)"); | 194 "new_tab_url VARCHAR," |
| 195 " last_visited INTEGER DEFAULT 0)"); |
| 190 } | 196 } |
| 191 | 197 |
| 192 bool KeywordTable::IsSyncable() { | 198 bool KeywordTable::IsSyncable() { |
| 193 return true; | 199 return true; |
| 194 } | 200 } |
| 195 | 201 |
| 196 bool KeywordTable::MigrateToVersion(int version, | 202 bool KeywordTable::MigrateToVersion(int version, |
| 197 bool* update_compatible_version) { | 203 bool* update_compatible_version) { |
| 198 // Migrate if necessary. | 204 // Migrate if necessary. |
| 199 switch (version) { | 205 switch (version) { |
| 200 case 53: | 206 case 53: |
| 201 *update_compatible_version = true; | 207 *update_compatible_version = true; |
| 202 return MigrateToVersion53AddNewTabURLColumn(); | 208 return MigrateToVersion53AddNewTabURLColumn(); |
| 203 case 59: | 209 case 59: |
| 204 *update_compatible_version = true; | 210 *update_compatible_version = true; |
| 205 return MigrateToVersion59RemoveExtensionKeywords(); | 211 return MigrateToVersion59RemoveExtensionKeywords(); |
| 206 case 68: | 212 case 68: |
| 207 *update_compatible_version = true; | 213 *update_compatible_version = true; |
| 208 return MigrateToVersion68RemoveShowInDefaultListColumn(); | 214 return MigrateToVersion68RemoveShowInDefaultListColumn(); |
| 215 case 69: |
| 216 return MigrateToVersion69AddLastVisitedColumn(); |
| 209 } | 217 } |
| 210 | 218 |
| 211 return true; | 219 return true; |
| 212 } | 220 } |
| 213 | 221 |
| 214 bool KeywordTable::PerformOperations(const Operations& operations) { | 222 bool KeywordTable::PerformOperations(const Operations& operations) { |
| 215 sql::Transaction transaction(db_); | 223 sql::Transaction transaction(db_); |
| 216 if (!transaction.Begin()) | 224 if (!transaction.Begin()) |
| 217 return false; | 225 return false; |
| 218 | 226 |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 328 "search_url_post_params VARCHAR," | 336 "search_url_post_params VARCHAR," |
| 329 "suggest_url_post_params VARCHAR," | 337 "suggest_url_post_params VARCHAR," |
| 330 "instant_url_post_params VARCHAR," | 338 "instant_url_post_params VARCHAR," |
| 331 "image_url_post_params VARCHAR," | 339 "image_url_post_params VARCHAR," |
| 332 "new_tab_url VARCHAR)") && | 340 "new_tab_url VARCHAR)") && |
| 333 db_->Execute(clone_query) && db_->Execute("DROP TABLE keywords") && | 341 db_->Execute(clone_query) && db_->Execute("DROP TABLE keywords") && |
| 334 db_->Execute("ALTER TABLE temp_keywords RENAME TO keywords") && | 342 db_->Execute("ALTER TABLE temp_keywords RENAME TO keywords") && |
| 335 transaction.Commit(); | 343 transaction.Commit(); |
| 336 } | 344 } |
| 337 | 345 |
| 346 bool KeywordTable::MigrateToVersion69AddLastVisitedColumn() { |
| 347 return db_->Execute("ALTER TABLE keywords ADD COLUMN last_visited " |
| 348 "INTEGER DEFAULT 0"); |
| 349 } |
| 350 |
| 338 // static | 351 // static |
| 339 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, | 352 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
| 340 TemplateURLData* data) { | 353 TemplateURLData* data) { |
| 341 DCHECK(data); | 354 DCHECK(data); |
| 342 | 355 |
| 343 data->SetShortName(s.ColumnString16(1)); | 356 data->SetShortName(s.ColumnString16(1)); |
| 344 data->SetKeyword(s.ColumnString16(2)); | 357 data->SetKeyword(s.ColumnString16(2)); |
| 345 // Due to past bugs, we might have persisted entries with empty URLs. Avoid | 358 // Due to past bugs, we might have persisted entries with empty URLs. Avoid |
| 346 // reading these out. (GetKeywords() will delete these entries on return.) | 359 // reading these out. (GetKeywords() will delete these entries on return.) |
| 347 // NOTE: This code should only be needed as long as we might be reading such | 360 // NOTE: This code should only be needed as long as we might be reading such |
| (...skipping 29 matching lines...) Expand all Loading... |
| 377 base::ListValue* alternate_urls_value; | 390 base::ListValue* alternate_urls_value; |
| 378 if (value.get() && value->GetAsList(&alternate_urls_value)) { | 391 if (value.get() && value->GetAsList(&alternate_urls_value)) { |
| 379 std::string alternate_url; | 392 std::string alternate_url; |
| 380 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { | 393 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { |
| 381 if (alternate_urls_value->GetString(i, &alternate_url)) | 394 if (alternate_urls_value->GetString(i, &alternate_url)) |
| 382 data->alternate_urls.push_back(alternate_url); | 395 data->alternate_urls.push_back(alternate_url); |
| 383 } | 396 } |
| 384 } | 397 } |
| 385 | 398 |
| 386 data->search_terms_replacement_key = s.ColumnString(17); | 399 data->search_terms_replacement_key = s.ColumnString(17); |
| 400 data->last_visited = Time::FromTimeT(s.ColumnInt64(24)); |
| 387 | 401 |
| 388 return true; | 402 return true; |
| 389 } | 403 } |
| 390 | 404 |
| 391 bool KeywordTable::AddKeyword(const TemplateURLData& data) { | 405 bool KeywordTable::AddKeyword(const TemplateURLData& data) { |
| 392 DCHECK(data.id); | 406 DCHECK(data.id); |
| 393 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") " | 407 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") " |
| 394 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," | 408 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," |
| 395 " ?)"); | 409 " ?,?)"); |
| 396 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str())); | 410 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str())); |
| 397 BindURLToStatement(data, &s, 0, 1); | 411 BindURLToStatement(data, &s, 0, 1); |
| 398 | 412 |
| 399 return s.Run(); | 413 return s.Run(); |
| 400 } | 414 } |
| 401 | 415 |
| 402 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 416 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
| 403 DCHECK(id); | 417 DCHECK(id); |
| 404 sql::Statement s(db_->GetCachedStatement( | 418 sql::Statement s(db_->GetCachedStatement( |
| 405 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?")); | 419 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?")); |
| 406 s.BindInt64(0, id); | 420 s.BindInt64(0, id); |
| 407 | 421 |
| 408 return s.Run(); | 422 return s.Run(); |
| 409 } | 423 } |
| 410 | 424 |
| 411 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { | 425 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { |
| 412 DCHECK(data.id); | 426 DCHECK(data.id); |
| 413 sql::Statement s(db_->GetCachedStatement( | 427 sql::Statement s(db_->GetCachedStatement( |
| 414 SQL_FROM_HERE, | 428 SQL_FROM_HERE, |
| 415 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, " | 429 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, " |
| 416 "safe_for_autoreplace=?, originating_url=?, date_created=?, " | 430 "safe_for_autoreplace=?, originating_url=?, date_created=?, " |
| 417 "usage_count=?, input_encodings=?, suggest_url=?, " | 431 "usage_count=?, input_encodings=?, suggest_url=?, " |
| 418 "prepopulate_id=?, created_by_policy=?, instant_url=?, " | 432 "prepopulate_id=?, created_by_policy=?, instant_url=?, " |
| 419 "last_modified=?, sync_guid=?, alternate_urls=?, " | 433 "last_modified=?, sync_guid=?, alternate_urls=?, " |
| 420 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, " | 434 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, " |
| 421 "suggest_url_post_params=?, instant_url_post_params=?, " | 435 "suggest_url_post_params=?, instant_url_post_params=?, " |
| 422 "image_url_post_params=?, new_tab_url=? WHERE id=?")); | 436 "image_url_post_params=?, new_tab_url=?, last_visited=? WHERE id=?")); |
| 423 BindURLToStatement(data, &s, 23, 0); // "23" binds id() as the last item. | 437 BindURLToStatement(data, &s, 24, 0); // "24" binds id() as the last item. |
| 424 | 438 |
| 425 return s.Run(); | 439 return s.Run(); |
| 426 } | 440 } |
| 427 | 441 |
| 428 bool KeywordTable::GetKeywordAsString(TemplateURLID id, | 442 bool KeywordTable::GetKeywordAsString(TemplateURLID id, |
| 429 const std::string& table_name, | 443 const std::string& table_name, |
| 430 std::string* result) { | 444 std::string* result) { |
| 431 std::string query("SELECT " + | 445 std::string query("SELECT " + |
| 432 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) + | 446 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) + |
| 433 " FROM " + table_name + " WHERE id=?"); | 447 " FROM " + table_name + " WHERE id=?"); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 } | 537 } |
| 524 } | 538 } |
| 525 | 539 |
| 526 // Replace the old table with the new one. | 540 // Replace the old table with the new one. |
| 527 sql = "DROP TABLE " + name; | 541 sql = "DROP TABLE " + name; |
| 528 if (!db_->Execute(sql.c_str())) | 542 if (!db_->Execute(sql.c_str())) |
| 529 return false; | 543 return false; |
| 530 sql = "ALTER TABLE keywords_temp RENAME TO " + name; | 544 sql = "ALTER TABLE keywords_temp RENAME TO " + name; |
| 531 return db_->Execute(sql.c_str()); | 545 return db_->Execute(sql.c_str()); |
| 532 } | 546 } |
| OLD | NEW |