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

Side by Side Diff: components/search_engines/keyword_table.cc

Issue 2498053002: Add field to monitor last visited time for each search engine (Closed)
Patch Set: Add unit test for last_visited field. Created 4 years, 1 month 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
OLDNEW
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
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 add in version 69.
Peter Kasting 2016/11/21 03:35:08 Nit: added
ltian 2016/11/28 22:08:02 Done.
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
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
(...skipping 30 matching lines...) Expand all
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)");
Peter Kasting 2016/11/21 03:35:08 Nit: No leading space
ltian 2016/11/28 22:08:02 Done.
Peter Kasting 2016/12/01 07:38:24 No... I meant "no leading space inside the quotati
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 *update_compatible_version = true;
Peter Kasting 2016/11/21 03:35:08 Doesn't seem like you should set this, as we don't
ltian 2016/11/28 22:08:02 Done.
217 return MigrateToVersion69AddLastVisitedColumn();
209 } 218 }
210 219
211 return true; 220 return true;
212 } 221 }
213 222
214 bool KeywordTable::PerformOperations(const Operations& operations) { 223 bool KeywordTable::PerformOperations(const Operations& operations) {
215 sql::Transaction transaction(db_); 224 sql::Transaction transaction(db_);
216 if (!transaction.Begin()) 225 if (!transaction.Begin())
217 return false; 226 return false;
218 227
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 "search_url_post_params VARCHAR," 337 "search_url_post_params VARCHAR,"
329 "suggest_url_post_params VARCHAR," 338 "suggest_url_post_params VARCHAR,"
330 "instant_url_post_params VARCHAR," 339 "instant_url_post_params VARCHAR,"
331 "image_url_post_params VARCHAR," 340 "image_url_post_params VARCHAR,"
332 "new_tab_url VARCHAR)") && 341 "new_tab_url VARCHAR)") &&
333 db_->Execute(clone_query) && db_->Execute("DROP TABLE keywords") && 342 db_->Execute(clone_query) && db_->Execute("DROP TABLE keywords") &&
334 db_->Execute("ALTER TABLE temp_keywords RENAME TO keywords") && 343 db_->Execute("ALTER TABLE temp_keywords RENAME TO keywords") &&
335 transaction.Commit(); 344 transaction.Commit();
336 } 345 }
337 346
347 bool KeywordTable::MigrateToVersion69AddLastVisitedColumn() {
348 return db_->Execute("ALTER TABLE keywords ADD COLUMN last_visited "
349 "INTEGER DEFAULT 0");
350 }
351
338 // static 352 // static
339 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, 353 bool KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s,
340 TemplateURLData* data) { 354 TemplateURLData* data) {
341 DCHECK(data); 355 DCHECK(data);
342 356
343 data->SetShortName(s.ColumnString16(1)); 357 data->SetShortName(s.ColumnString16(1));
344 data->SetKeyword(s.ColumnString16(2)); 358 data->SetKeyword(s.ColumnString16(2));
345 // Due to past bugs, we might have persisted entries with empty URLs. Avoid 359 // Due to past bugs, we might have persisted entries with empty URLs. Avoid
346 // reading these out. (GetKeywords() will delete these entries on return.) 360 // 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 361 // NOTE: This code should only be needed as long as we might be reading such
(...skipping 29 matching lines...) Expand all
377 base::ListValue* alternate_urls_value; 391 base::ListValue* alternate_urls_value;
378 if (value.get() && value->GetAsList(&alternate_urls_value)) { 392 if (value.get() && value->GetAsList(&alternate_urls_value)) {
379 std::string alternate_url; 393 std::string alternate_url;
380 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) { 394 for (size_t i = 0; i < alternate_urls_value->GetSize(); ++i) {
381 if (alternate_urls_value->GetString(i, &alternate_url)) 395 if (alternate_urls_value->GetString(i, &alternate_url))
382 data->alternate_urls.push_back(alternate_url); 396 data->alternate_urls.push_back(alternate_url);
383 } 397 }
384 } 398 }
385 399
386 data->search_terms_replacement_key = s.ColumnString(17); 400 data->search_terms_replacement_key = s.ColumnString(17);
401 data->last_visited = Time::FromTimeT(s.ColumnInt64(24));
387 402
388 return true; 403 return true;
389 } 404 }
390 405
391 bool KeywordTable::AddKeyword(const TemplateURLData& data) { 406 bool KeywordTable::AddKeyword(const TemplateURLData& data) {
392 DCHECK(data.id); 407 DCHECK(data.id);
393 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") " 408 std::string query("INSERT INTO keywords (" + GetKeywordColumns() + ") "
394 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," 409 "VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,"
395 " ?)"); 410 " ?,?)");
396 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str())); 411 sql::Statement s(db_->GetCachedStatement(SQL_FROM_HERE, query.c_str()));
397 BindURLToStatement(data, &s, 0, 1); 412 BindURLToStatement(data, &s, 0, 1);
398 413
399 return s.Run(); 414 return s.Run();
400 } 415 }
401 416
402 bool KeywordTable::RemoveKeyword(TemplateURLID id) { 417 bool KeywordTable::RemoveKeyword(TemplateURLID id) {
403 DCHECK(id); 418 DCHECK(id);
404 sql::Statement s(db_->GetCachedStatement( 419 sql::Statement s(db_->GetCachedStatement(
405 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?")); 420 SQL_FROM_HERE, "DELETE FROM keywords WHERE id = ?"));
406 s.BindInt64(0, id); 421 s.BindInt64(0, id);
407 422
408 return s.Run(); 423 return s.Run();
409 } 424 }
410 425
411 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) { 426 bool KeywordTable::UpdateKeyword(const TemplateURLData& data) {
412 DCHECK(data.id); 427 DCHECK(data.id);
413 sql::Statement s(db_->GetCachedStatement( 428 sql::Statement s(db_->GetCachedStatement(
414 SQL_FROM_HERE, 429 SQL_FROM_HERE,
415 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, " 430 "UPDATE keywords SET short_name=?, keyword=?, favicon_url=?, url=?, "
416 "safe_for_autoreplace=?, originating_url=?, date_created=?, " 431 "safe_for_autoreplace=?, originating_url=?, date_created=?, "
417 "usage_count=?, input_encodings=?, suggest_url=?, " 432 "usage_count=?, input_encodings=?, suggest_url=?, "
418 "prepopulate_id=?, created_by_policy=?, instant_url=?, " 433 "prepopulate_id=?, created_by_policy=?, instant_url=?, "
419 "last_modified=?, sync_guid=?, alternate_urls=?, " 434 "last_modified=?, sync_guid=?, alternate_urls=?, "
420 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, " 435 "search_terms_replacement_key=?, image_url=?, search_url_post_params=?, "
421 "suggest_url_post_params=?, instant_url_post_params=?, " 436 "suggest_url_post_params=?, instant_url_post_params=?, "
422 "image_url_post_params=?, new_tab_url=? WHERE id=?")); 437 "image_url_post_params=?, new_tab_url=?, last_visited=? WHERE id=?"));
423 BindURLToStatement(data, &s, 23, 0); // "23" binds id() as the last item. 438 BindURLToStatement(data, &s, 24, 0); // "24" binds id() as the last item.
424 439
425 return s.Run(); 440 return s.Run();
426 } 441 }
427 442
428 bool KeywordTable::GetKeywordAsString(TemplateURLID id, 443 bool KeywordTable::GetKeywordAsString(TemplateURLID id,
429 const std::string& table_name, 444 const std::string& table_name,
430 std::string* result) { 445 std::string* result) {
431 std::string query("SELECT " + 446 std::string query("SELECT " +
432 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) + 447 ColumnsForVersion(WebDatabase::kCurrentVersionNumber, true) +
433 " FROM " + table_name + " WHERE id=?"); 448 " FROM " + table_name + " WHERE id=?");
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
523 } 538 }
524 } 539 }
525 540
526 // Replace the old table with the new one. 541 // Replace the old table with the new one.
527 sql = "DROP TABLE " + name; 542 sql = "DROP TABLE " + name;
528 if (!db_->Execute(sql.c_str())) 543 if (!db_->Execute(sql.c_str()))
529 return false; 544 return false;
530 sql = "ALTER TABLE keywords_temp RENAME TO " + name; 545 sql = "ALTER TABLE keywords_temp RENAME TO " + name;
531 return db_->Execute(sql.c_str()); 546 return db_->Execute(sql.c_str());
532 } 547 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698