Chromium Code Reviews| 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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 74 columns.push_back("image_url"); | 74 columns.push_back("image_url"); |
| 75 columns.push_back("search_url_post_params"); | 75 columns.push_back("search_url_post_params"); |
| 76 columns.push_back("suggest_url_post_params"); | 76 columns.push_back("suggest_url_post_params"); |
| 77 columns.push_back("instant_url_post_params"); | 77 columns.push_back("instant_url_post_params"); |
| 78 columns.push_back("image_url_post_params"); | 78 columns.push_back("image_url_post_params"); |
| 79 } | 79 } |
| 80 if (version >= 53) { | 80 if (version >= 53) { |
| 81 // Column added in version 53. | 81 // Column added in version 53. |
| 82 columns.push_back("new_tab_url"); | 82 columns.push_back("new_tab_url"); |
| 83 } | 83 } |
| 84 if (version >= 66) { | |
| 85 // Column added in version 66. | |
| 86 columns.push_back("engine_type"); | |
| 87 } | |
| 84 | 88 |
| 85 return base::JoinString(columns, std::string(concatenated ? " || " : ", ")); | 89 return base::JoinString(columns, std::string(concatenated ? " || " : ", ")); |
| 86 } | 90 } |
| 87 | 91 |
| 88 | 92 |
| 89 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all | 93 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
| 90 // the columns in the keyword table. |id_column| is the slot number to bind | 94 // the columns in the keyword table. |id_column| is the slot number to bind |
| 91 // |data|'s |id| to; |starting_column| is the slot number of the first of a | 95 // |data|'s |id| to; |starting_column| is the slot number of the first of a |
| 92 // contiguous set of slots to bind all the other fields to. | 96 // contiguous set of slots to bind all the other fields to. |
| 93 void BindURLToStatement(const TemplateURLData& data, | 97 void BindURLToStatement(const TemplateURLData& data, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); | 131 s->BindInt64(starting_column + 14, data.last_modified.ToTimeT()); |
| 128 s->BindString(starting_column + 15, data.sync_guid); | 132 s->BindString(starting_column + 15, data.sync_guid); |
| 129 s->BindString(starting_column + 16, alternate_urls); | 133 s->BindString(starting_column + 16, alternate_urls); |
| 130 s->BindString(starting_column + 17, data.search_terms_replacement_key); | 134 s->BindString(starting_column + 17, data.search_terms_replacement_key); |
| 131 s->BindString(starting_column + 18, data.image_url); | 135 s->BindString(starting_column + 18, data.image_url); |
| 132 s->BindString(starting_column + 19, data.search_url_post_params); | 136 s->BindString(starting_column + 19, data.search_url_post_params); |
| 133 s->BindString(starting_column + 20, data.suggestions_url_post_params); | 137 s->BindString(starting_column + 20, data.suggestions_url_post_params); |
| 134 s->BindString(starting_column + 21, data.instant_url_post_params); | 138 s->BindString(starting_column + 21, data.instant_url_post_params); |
| 135 s->BindString(starting_column + 22, data.image_url_post_params); | 139 s->BindString(starting_column + 22, data.image_url_post_params); |
| 136 s->BindString(starting_column + 23, data.new_tab_url); | 140 s->BindString(starting_column + 23, data.new_tab_url); |
| 141 s->BindInt(starting_column + 24, data.engine_type); | |
| 137 } | 142 } |
| 138 | 143 |
| 139 WebDatabaseTable::TypeKey GetKey() { | 144 WebDatabaseTable::TypeKey GetKey() { |
| 140 // We just need a unique constant. Use the address of a static that | 145 // We just need a unique constant. Use the address of a static that |
| 141 // COMDAT folding won't touch in an optimizing linker. | 146 // COMDAT folding won't touch in an optimizing linker. |
| 142 static int table_key = 0; | 147 static int table_key = 0; |
| 143 return reinterpret_cast<void*>(&table_key); | 148 return reinterpret_cast<void*>(&table_key); |
| 144 } | 149 } |
| 145 | 150 |
| 146 } // namespace | 151 } // namespace |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 178 "instant_url VARCHAR," | 183 "instant_url VARCHAR," |
| 179 "last_modified INTEGER DEFAULT 0," | 184 "last_modified INTEGER DEFAULT 0," |
| 180 "sync_guid VARCHAR," | 185 "sync_guid VARCHAR," |
| 181 "alternate_urls VARCHAR," | 186 "alternate_urls VARCHAR," |
| 182 "search_terms_replacement_key VARCHAR," | 187 "search_terms_replacement_key VARCHAR," |
| 183 "image_url VARCHAR," | 188 "image_url VARCHAR," |
| 184 "search_url_post_params VARCHAR," | 189 "search_url_post_params VARCHAR," |
| 185 "suggest_url_post_params VARCHAR," | 190 "suggest_url_post_params VARCHAR," |
| 186 "instant_url_post_params VARCHAR," | 191 "instant_url_post_params VARCHAR," |
| 187 "image_url_post_params VARCHAR," | 192 "image_url_post_params VARCHAR," |
| 188 "new_tab_url VARCHAR)"); | 193 "new_tab_url VARCHAR," |
| 194 "engine_type INTEGER DEFAULT -1)"); | |
| 189 } | 195 } |
| 190 | 196 |
| 191 bool KeywordTable::IsSyncable() { | 197 bool KeywordTable::IsSyncable() { |
| 192 return true; | 198 return true; |
| 193 } | 199 } |
| 194 | 200 |
| 195 bool KeywordTable::MigrateToVersion(int version, | 201 bool KeywordTable::MigrateToVersion(int version, |
| 196 bool* update_compatible_version) { | 202 bool* update_compatible_version) { |
| 197 // Migrate if necessary. | 203 // Migrate if necessary. |
| 198 switch (version) { | 204 switch (version) { |
| 199 case 53: | 205 case 53: |
| 200 *update_compatible_version = true; | 206 *update_compatible_version = true; |
| 201 return MigrateToVersion53AddNewTabURLColumn(); | 207 return MigrateToVersion53AddNewTabURLColumn(); |
| 202 case 59: | 208 case 59: |
| 203 *update_compatible_version = true; | 209 *update_compatible_version = true; |
| 204 return MigrateToVersion59RemoveExtensionKeywords(); | 210 return MigrateToVersion59RemoveExtensionKeywords(); |
| 211 case 66: | |
| 212 *update_compatible_version = true; | |
| 213 return MigrateToVersion66AddEngineTypeColumn(); | |
| 205 } | 214 } |
| 206 | 215 |
| 207 return true; | 216 return true; |
| 208 } | 217 } |
| 209 | 218 |
| 210 bool KeywordTable::PerformOperations(const Operations& operations) { | 219 bool KeywordTable::PerformOperations(const Operations& operations) { |
| 211 sql::Transaction transaction(db_); | 220 sql::Transaction transaction(db_); |
| 212 if (!transaction.Begin()) | 221 if (!transaction.Begin()) |
| 213 return false; | 222 return false; |
| 214 | 223 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 282 bool KeywordTable::MigrateToVersion53AddNewTabURLColumn() { | 291 bool KeywordTable::MigrateToVersion53AddNewTabURLColumn() { |
| 283 return db_->Execute("ALTER TABLE keywords ADD COLUMN new_tab_url " | 292 return db_->Execute("ALTER TABLE keywords ADD COLUMN new_tab_url " |
| 284 "VARCHAR DEFAULT ''"); | 293 "VARCHAR DEFAULT ''"); |
| 285 } | 294 } |
| 286 | 295 |
| 287 bool KeywordTable::MigrateToVersion59RemoveExtensionKeywords() { | 296 bool KeywordTable::MigrateToVersion59RemoveExtensionKeywords() { |
| 288 return db_->Execute("DELETE FROM keywords " | 297 return db_->Execute("DELETE FROM keywords " |
| 289 "WHERE url LIKE 'chrome-extension://%'"); | 298 "WHERE url LIKE 'chrome-extension://%'"); |
| 290 } | 299 } |
| 291 | 300 |
| 301 bool KeywordTable::MigrateToVersion66AddEngineTypeColumn() { | |
| 302 return db_->Execute("ALTER TABLE keywords ADD COLUMN engine_type " | |
| 303 "INTEGER DEFAULT -1"); | |
| 304 } | |
| 305 | |
| 292 // static | 306 // static |
| 293 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, | 307 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
| 294 TemplateURLData* data) { | 308 TemplateURLData* data) { |
| 295 DCHECK(data); | 309 DCHECK(data); |
| 296 | 310 |
| 297 data->SetShortName(s.ColumnString16(1)); | 311 data->SetShortName(s.ColumnString16(1)); |
| 298 data->SetKeyword(s.ColumnString16(2)); | 312 data->SetKeyword(s.ColumnString16(2)); |
| 299 // Due to past bugs, we might have persisted entries with empty URLs. Avoid | 313 // Due to past bugs, we might have persisted entries with empty URLs. Avoid |
| 300 // reading these out. (GetKeywords() will delete these entries on return.) | 314 // reading these out. (GetKeywords() will delete these entries on return.) |
| 301 // NOTE: This code should only be needed as long as we might be reading such | 315 // NOTE: This code should only be needed as long as we might be reading such |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 315 data->originating_url = GURL(s.ColumnString(6)); | 329 data->originating_url = GURL(s.ColumnString(6)); |
| 316 data->show_in_default_list = s.ColumnBool(10); | 330 data->show_in_default_list = s.ColumnBool(10); |
| 317 data->safe_for_autoreplace = s.ColumnBool(5); | 331 data->safe_for_autoreplace = s.ColumnBool(5); |
| 318 data->input_encodings = base::SplitString( | 332 data->input_encodings = base::SplitString( |
| 319 s.ColumnString(9), ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 333 s.ColumnString(9), ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 320 data->id = s.ColumnInt64(0); | 334 data->id = s.ColumnInt64(0); |
| 321 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); | 335 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); |
| 322 data->last_modified = Time::FromTimeT(s.ColumnInt64(15)); | 336 data->last_modified = Time::FromTimeT(s.ColumnInt64(15)); |
| 323 data->created_by_policy = s.ColumnBool(13); | 337 data->created_by_policy = s.ColumnBool(13); |
| 324 data->usage_count = s.ColumnInt(8); | 338 data->usage_count = s.ColumnInt(8); |
| 339 data->engine_type = static_cast<SearchEngineType>(s.ColumnInt(25)); | |
| 325 data->prepopulate_id = s.ColumnInt(12); | 340 data->prepopulate_id = s.ColumnInt(12); |
| 326 data->sync_guid = s.ColumnString(16); | 341 data->sync_guid = s.ColumnString(16); |
| 327 | 342 |
| 328 data->alternate_urls.clear(); | 343 data->alternate_urls.clear(); |
| 329 base::JSONReader json_reader; | 344 base::JSONReader json_reader; |
| 330 std::unique_ptr<base::Value> value( | 345 std::unique_ptr<base::Value> value( |
| 331 json_reader.ReadToValue(s.ColumnString(17))); | 346 json_reader.ReadToValue(s.ColumnString(17))); |
| 332 base::ListValue* alternate_urls_value; | 347 base::ListValue* alternate_urls_value; |
| 333 if (value.get() && value->GetAsList(&alternate_urls_value)) { | 348 if (value.get() && value->GetAsList(&alternate_urls_value)) { |
| 334 std::string alternate_url; | 349 std::string alternate_url; |
| 335 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { | 350 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { |
| 336 if (alternate_urls_value->GetString(i, &alternate_url)) | 351 if (alternate_urls_value->GetString(i, &alternate_url)) |
| 337 data->alternate_urls.push_back(alternate_url); | 352 data->alternate_urls.push_back(alternate_url); |
| 338 } | 353 } |
| 339 } | 354 } |
| 340 | 355 |
| 341 data->search_terms_replacement_key = s.ColumnString(18); | 356 data->search_terms_replacement_key = s.ColumnString(18); |
| 342 | 357 |
| 343 return true; | 358 return true; |
| 344 } | 359 } |
| 345 | 360 |
| 346 bool KeywordTable::AddKeyword(const TemplateURLData& data) { | 361 bool KeywordTable::AddKeyword(const TemplateURLData& data) { |
| 347 DCHECK(data.id); | 362 DCHECK(data.id); |
| 348 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") " | 363 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") " |
| 349 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," | 364 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," |
| 350 " ?)"); | 365 " ?,?)"); |
| 351 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str())); | 366 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str())); |
| 352 BindURLToStatement(data, &s, 0, 1); | 367 BindURLToStatement(data, &s, 0, 1); |
| 353 | 368 |
| 354 return s.Run(); | 369 return s.Run(); |
| 355 } | 370 } |
| 356 | 371 |
| 357 bool KeywordTable::RemoveKeyword(TemplateURLID id) { | 372 bool KeywordTable::RemoveKeyword(TemplateURLID id) { |
| 358 DCHECK(id); | 373 DCHECK(id); |
| 359 sql::Statement s(db_->GetCachedStatement( | 374 sql::Statement s(db_->GetCachedStatement( |
| 360 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?")); | 375 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?")); |
| 361 s.BindInt64(0, id); | 376 s.BindInt64(0, id); |
| 362 | 377 |
| 363 return s.Run(); | 378 return s.Run(); |
| 364 } | 379 } |
| 365 | 380 |
| 366 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { | 381 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { |
| 367 DCHECK(data.id); | 382 DCHECK(data.id); |
| 368 sql::Statement s(db_->GetCachedStatement( | 383 sql::Statement s(db_->GetCachedStatement( |
| 369 SQL_FROM_HERE, | 384 SQL_FROM_HERE, |
| 370 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, " | 385 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, " |
| 371 "safe_for_autoreplace=?, originating_url=?, date_created=?, " | 386 "safe_for_autoreplace=?, originating_url=?, date_created=?, " |
| 372 "usage_count=?, input_encodings=?, show_in_default_list=?, " | 387 "usage_count=?, input_encodings=?, show_in_default_list=?, " |
| 373 "suggest_url=?, prepopulate_id=?, created_by_policy=?, instant_url=?, " | 388 "suggest_url=?, prepopulate_id=?, created_by_policy=?, instant_url=?, " |
| 374 "last_modified=?, sync_guid=?, alternate_urls=?, " | 389 "last_modified=?, sync_guid=?, alternate_urls=?, " |
| 375 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, " | 390 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, " |
| 376 "suggest_url_post_params=?, instant_url_post_params=?, " | 391 "suggest_url_post_params=?, instant_url_post_params=?, " |
| 377 "image_url_post_params=?, new_tab_url=? WHERE id=?")); | 392 "image_url_post_params=?, new_tab_url=?, engine_type=? WHERE id=?")); |
| 378 BindURLToStatement(data, &s, 24, 0); // "24" binds id() as the last item. | 393 BindURLToStatement(data, &s, 25, 0); // "24" binds id() as the last item. |
|
Peter Kasting
2016/05/17 04:43:04
Nit: 24 -> 25
Vitaly Baranov
2016/06/03 15:31:45
Done.
| |
| 379 | 394 |
| 380 return s.Run(); | 395 return s.Run(); |
| 381 } | 396 } |
| 382 | 397 |
| 383 bool KeywordTable::GetKeywordAsString(TemplateURLID id, | 398 bool KeywordTable::GetKeywordAsString(TemplateURLID id, |
| 384 const std::string& table_name, | 399 const std::string& table_name, |
| 385 std::string* result) { | 400 std::string* result) { |
| 386 std::string query("SELECT " + | 401 std::string query("SELECT " + |
| 387 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) + | 402 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) + |
| 388 " FROM " + table_name + " WHERE id=?"); | 403 " FROM " + table_name + " WHERE id=?"); |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 } | 493 } |
| 479 } | 494 } |
| 480 | 495 |
| 481 // Replace the old table with the new one. | 496 // Replace the old table with the new one. |
| 482 sql = "DROP TABLE " + name; | 497 sql = "DROP TABLE " + name; |
| 483 if (!db_->Execute(sql.c_str())) | 498 if (!db_->Execute(sql.c_str())) |
| 484 return false; | 499 return false; |
| 485 sql = "ALTER TABLE keywords_temp RENAME TO " + name; | 500 sql = "ALTER TABLE keywords_temp RENAME TO " + name; |
| 486 return db_->Execute(sql.c_str()); | 501 return db_->Execute(sql.c_str()); |
| 487 } | 502 } |
| OLD | NEW |