| 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 "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 29 matching lines...) Expand all Loading... |
| 40 | 40 |
| 41 // Keys used in the meta table. | 41 // Keys used in the meta table. |
| 42 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; | 42 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; |
| 43 | 43 |
| 44 const char kKeywordColumnsConcatenated[] = "id || short_name || keyword || " | 44 const char kKeywordColumnsConcatenated[] = "id || short_name || keyword || " |
| 45 "favicon_url || url || safe_for_autoreplace || originating_url || " | 45 "favicon_url || url || safe_for_autoreplace || originating_url || " |
| 46 "date_created || usage_count || input_encodings || show_in_default_list || " | 46 "date_created || usage_count || input_encodings || show_in_default_list || " |
| 47 "suggest_url || prepopulate_id || autogenerate_keyword || logo_id || " | 47 "suggest_url || prepopulate_id || autogenerate_keyword || logo_id || " |
| 48 "created_by_policy || instant_url || last_modified || sync_guid"; | 48 "created_by_policy || instant_url || last_modified || sync_guid"; |
| 49 | 49 |
| 50 // Inserts the data from |url| into |s|. |s| is assumed to have slots for all | 50 // Inserts the data from |data| into |s|. |s| is assumed to have slots for all |
| 51 // the columns in the keyword table. |id_column| is the slot number to bind | 51 // the columns in the keyword table. |id_column| is the slot number to bind |
| 52 // |url|'s id() to; |starting_column| is the slot number of the first of a | 52 // |data|'s id() to; |starting_column| is the slot number of the first of a |
| 53 // contiguous set of slots to bind all the other fields to. | 53 // contiguous set of slots to bind all the other fields to. |
| 54 void BindURLToStatement(const TemplateURL& url, | 54 void BindURLToStatement(const TemplateURL& url, |
| 55 sql::Statement* s, | 55 sql::Statement* s, |
| 56 int id_column, | 56 int id_column, |
| 57 int starting_column) { | 57 int starting_column) { |
| 58 s->BindInt64(id_column, url.id()); | 58 const TemplateURLData& data = url.data(); |
| 59 s->BindString16(starting_column, url.short_name()); | 59 s->BindInt64(id_column, data.id); |
| 60 s->BindString16(starting_column + 1, url.keyword()); | 60 s->BindString16(starting_column, data.short_name); |
| 61 s->BindString(starting_column + 2, url.favicon_url().is_valid() ? | 61 s->BindString16(starting_column + 1, data.keyword(&url)); |
| 62 history::HistoryDatabase::GURLToDatabaseURL(url.favicon_url()) : | 62 s->BindString(starting_column + 2, data.favicon_url.is_valid() ? |
| 63 history::HistoryDatabase::GURLToDatabaseURL(data.favicon_url) : |
| 63 std::string()); | 64 std::string()); |
| 64 s->BindString(starting_column + 3, url.url()); | 65 s->BindString(starting_column + 3, data.url()); |
| 65 s->BindBool(starting_column + 4, url.safe_for_autoreplace()); | 66 s->BindBool(starting_column + 4, data.safe_for_autoreplace); |
| 66 s->BindString(starting_column + 5, url.originating_url().is_valid() ? | 67 s->BindString(starting_column + 5, data.originating_url.is_valid() ? |
| 67 history::HistoryDatabase::GURLToDatabaseURL(url.originating_url()) : | 68 history::HistoryDatabase::GURLToDatabaseURL(data.originating_url) : |
| 68 std::string()); | 69 std::string()); |
| 69 s->BindInt64(starting_column + 6, url.date_created().ToTimeT()); | 70 s->BindInt64(starting_column + 6, data.date_created.ToTimeT()); |
| 70 s->BindInt(starting_column + 7, url.usage_count()); | 71 s->BindInt(starting_column + 7, data.usage_count); |
| 71 s->BindString(starting_column + 8, JoinString(url.input_encodings(), ';')); | 72 s->BindString(starting_column + 8, JoinString(data.input_encodings, ';')); |
| 72 s->BindBool(starting_column + 9, url.show_in_default_list()); | 73 s->BindBool(starting_column + 9, data.show_in_default_list); |
| 73 s->BindString(starting_column + 10, url.suggestions_url()); | 74 s->BindString(starting_column + 10, data.suggestions_url); |
| 74 s->BindInt(starting_column + 11, url.prepopulate_id()); | 75 s->BindInt(starting_column + 11, data.prepopulate_id); |
| 75 s->BindInt(starting_column + 12, url.autogenerate_keyword() ? 1 : 0); | 76 s->BindBool(starting_column + 12, data.autogenerate_keyword()); |
| 76 s->BindInt(starting_column + 13, 0); | 77 s->BindInt(starting_column + 13, 0); |
| 77 s->BindBool(starting_column + 14, url.created_by_policy()); | 78 s->BindBool(starting_column + 14, data.created_by_policy); |
| 78 s->BindString(starting_column + 15, url.instant_url()); | 79 s->BindString(starting_column + 15, data.instant_url); |
| 79 s->BindInt64(starting_column + 16, url.last_modified().ToTimeT()); | 80 s->BindInt64(starting_column + 16, data.last_modified.ToTimeT()); |
| 80 s->BindString(starting_column + 17, url.sync_guid()); | 81 s->BindString(starting_column + 17, data.sync_guid); |
| 81 } | 82 } |
| 82 | 83 |
| 83 // Signs search provider id and returns its signature. | 84 // Signs search provider id and returns its signature. |
| 84 std::string GetSearchProviderIDSignature(int64 id) { | 85 std::string GetSearchProviderIDSignature(int64 id) { |
| 85 return protector::SignSetting(base::Int64ToString(id)); | 86 return protector::SignSetting(base::Int64ToString(id)); |
| 86 } | 87 } |
| 87 | 88 |
| 88 // Checks if signature for search provider id is correct and returns the | 89 // Checks if signature for search provider id is correct and returns the |
| 89 // result. | 90 // result. |
| 90 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { | 91 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 141 s.BindInt64(0, id); | 142 s.BindInt64(0, id); |
| 142 | 143 |
| 143 return s.Run() && UpdateBackupSignature(); | 144 return s.Run() && UpdateBackupSignature(); |
| 144 } | 145 } |
| 145 | 146 |
| 146 bool KeywordTable::GetKeywords(Keywords* keywords) { | 147 bool KeywordTable::GetKeywords(Keywords* keywords) { |
| 147 std::string query("SELECT " + std::string(kKeywordColumns) + | 148 std::string query("SELECT " + std::string(kKeywordColumns) + |
| 148 " FROM keywords ORDER BY id ASC"); | 149 " FROM keywords ORDER BY id ASC"); |
| 149 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 150 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 150 | 151 |
| 151 while (s.Step()) | 152 while (s.Step()) { |
| 152 keywords->push_back(GetKeywordDataFromStatement(s)); | 153 keywords->push_back(TemplateURLData()); |
| 154 GetKeywordDataFromStatement(s, &keywords->back()); |
| 155 } |
| 153 return s.Succeeded(); | 156 return s.Succeeded(); |
| 154 } | 157 } |
| 155 | 158 |
| 156 bool KeywordTable::UpdateKeyword(const TemplateURL& url) { | 159 bool KeywordTable::UpdateKeyword(const TemplateURL& url) { |
| 157 DCHECK(url.id()); | 160 DCHECK(url.id()); |
| 158 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " | 161 sql::Statement s(db_->GetUniqueStatement("UPDATE keywords SET short_name=?, " |
| 159 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " | 162 "keyword=?, favicon_url=?, url=?, safe_for_autoreplace=?, " |
| 160 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " | 163 "originating_url=?, date_created=?, usage_count=?, input_encodings=?, " |
| 161 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " | 164 "show_in_default_list=?, suggest_url=?, prepopulate_id=?, " |
| 162 "autogenerate_keyword=?, logo_id=?, created_by_policy=?, instant_url=?, " | 165 "autogenerate_keyword=?, logo_id=?, created_by_policy=?, instant_url=?, " |
| (...skipping 10 matching lines...) Expand all Loading... |
| 173 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 176 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 174 UpdateBackupSignature(); | 177 UpdateBackupSignature(); |
| 175 } | 178 } |
| 176 | 179 |
| 177 int64 KeywordTable::GetDefaultSearchProviderID() { | 180 int64 KeywordTable::GetDefaultSearchProviderID() { |
| 178 int64 value = kInvalidTemplateURLID; | 181 int64 value = kInvalidTemplateURLID; |
| 179 meta_table_->GetValue(kDefaultSearchProviderKey, &value); | 182 meta_table_->GetValue(kDefaultSearchProviderKey, &value); |
| 180 return value; | 183 return value; |
| 181 } | 184 } |
| 182 | 185 |
| 183 TemplateURL* KeywordTable::GetDefaultSearchProviderBackup() { | 186 bool KeywordTable::GetDefaultSearchProviderBackup(TemplateURLData* backup) { |
| 184 if (!IsBackupSignatureValid()) | 187 if (!IsBackupSignatureValid()) |
| 185 return NULL; | 188 return false; |
| 186 | 189 |
| 187 int64 backup_id = kInvalidTemplateURLID; | 190 int64 backup_id = kInvalidTemplateURLID; |
| 188 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id)) { | 191 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id)) { |
| 189 LOG(ERROR) << "No default search id backup found."; | 192 LOG(ERROR) << "No default search id backup found."; |
| 190 return NULL; | 193 return false; |
| 191 } | 194 } |
| 192 std::string query("SELECT " + std::string(kKeywordColumns) + | 195 std::string query("SELECT " + std::string(kKeywordColumns) + |
| 193 " FROM keywords_backup WHERE id=?"); | 196 " FROM keywords_backup WHERE id=?"); |
| 194 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 197 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 195 s.BindInt64(0, backup_id); | 198 s.BindInt64(0, backup_id); |
| 196 | 199 |
| 197 if (!s.Step()) { | 200 if (!s.Step()) { |
| 198 LOG_IF(ERROR, s.Succeeded()) | 201 LOG_IF(ERROR, s.Succeeded()) |
| 199 << "No default search provider with backup id."; | 202 << "No default search provider with backup id."; |
| 200 return NULL; | 203 return NULL; |
| 201 } | 204 } |
| 202 | 205 |
| 203 TemplateURL* template_url = GetKeywordDataFromStatement(s); | 206 GetKeywordDataFromStatement(s, backup); |
| 204 // ID has no meaning for the backup and should be kInvalidTemplateURLID in | 207 // ID has no meaning for the backup and should be kInvalidTemplateURLID in |
| 205 // case the TemplateURL will be added to keywords if missing. | 208 // case the TemplateURL will be added to keywords if missing. |
| 206 template_url->set_id(kInvalidTemplateURLID); | 209 backup->id = kInvalidTemplateURLID; |
| 207 return template_url; | 210 return true; |
| 208 } | 211 } |
| 209 | 212 |
| 210 bool KeywordTable::DidDefaultSearchProviderChange() { | 213 bool KeywordTable::DidDefaultSearchProviderChange() { |
| 211 if (!IsBackupSignatureValid()) { | 214 if (!IsBackupSignatureValid()) { |
| 212 UMA_HISTOGRAM_ENUMERATION( | 215 UMA_HISTOGRAM_ENUMERATION( |
| 213 protector::kProtectorHistogramDefaultSearchProvider, | 216 protector::kProtectorHistogramDefaultSearchProvider, |
| 214 protector::kProtectorErrorBackupInvalid, | 217 protector::kProtectorErrorBackupInvalid, |
| 215 protector::kProtectorErrorCount); | 218 protector::kProtectorErrorCount); |
| 216 LOG(ERROR) << "Backup signature is invalid."; | 219 LOG(ERROR) << "Backup signature is invalid."; |
| 217 return true; | 220 return true; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 323 |
| 321 bool KeywordTable::MigrateToVersion39AddSyncGUIDColumn() { | 324 bool KeywordTable::MigrateToVersion39AddSyncGUIDColumn() { |
| 322 return db_->Execute("ALTER TABLE keywords ADD COLUMN sync_guid VARCHAR"); | 325 return db_->Execute("ALTER TABLE keywords ADD COLUMN sync_guid VARCHAR"); |
| 323 } | 326 } |
| 324 | 327 |
| 325 bool KeywordTable::MigrateToVersion44AddDefaultSearchProviderBackup() { | 328 bool KeywordTable::MigrateToVersion44AddDefaultSearchProviderBackup() { |
| 326 return IsBackupSignatureValid() || UpdateBackupSignature(); | 329 return IsBackupSignatureValid() || UpdateBackupSignature(); |
| 327 } | 330 } |
| 328 | 331 |
| 329 // static | 332 // static |
| 330 TemplateURL* KeywordTable::GetKeywordDataFromStatement( | 333 void KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
| 331 const sql::Statement& s) { | 334 TemplateURLData* data) { |
| 332 TemplateURL* url = new TemplateURL; | 335 DCHECK(data); |
| 333 url->set_id(s.ColumnInt64(0)); | 336 data->short_name = s.ColumnString16(1); |
| 334 url->set_short_name(s.ColumnString16(1)); | 337 data->SetKeyword(s.ColumnString16(2)); |
| 335 url->set_keyword(s.ColumnString16(2)); | 338 data->SetAutogenerateKeyword(s.ColumnBool(13)); |
| 336 std::string tmp(s.ColumnString(3)); | 339 data->SetURL(s.ColumnString(4)); |
| 337 if (!tmp.empty()) | 340 data->suggestions_url = s.ColumnString(11); |
| 338 url->set_favicon_url(GURL(tmp)); | 341 data->instant_url = s.ColumnString(16); |
| 339 url->SetURL(s.ColumnString(4)); | 342 data->favicon_url = GURL(s.ColumnString(3)); |
| 340 url->set_safe_for_autoreplace(s.ColumnBool(5)); | 343 data->originating_url = GURL(s.ColumnString(6)); |
| 341 tmp = s.ColumnString(6); | 344 data->show_in_default_list = s.ColumnBool(10); |
| 342 if (!tmp.empty()) | 345 data->safe_for_autoreplace = s.ColumnBool(5); |
| 343 url->set_originating_url(GURL(tmp)); | 346 base::SplitString(s.ColumnString(9), ';', &data->input_encodings); |
| 344 url->set_date_created(Time::FromTimeT(s.ColumnInt64(7))); | 347 data->id = s.ColumnInt64(0); |
| 345 url->set_usage_count(s.ColumnInt(8)); | 348 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); |
| 346 std::vector<std::string> encodings; | 349 data->last_modified = Time::FromTimeT(s.ColumnInt64(17)); |
| 347 base::SplitString(s.ColumnString(9), ';', &encodings); | 350 data->created_by_policy = s.ColumnBool(15); |
| 348 url->set_input_encodings(encodings); | 351 data->usage_count = s.ColumnInt(8); |
| 349 url->set_show_in_default_list(s.ColumnBool(10)); | 352 data->prepopulate_id = s.ColumnInt(12); |
| 350 url->SetSuggestionsURL(s.ColumnString(11)); | 353 data->sync_guid = s.ColumnString(18); |
| 351 url->SetPrepopulateId(s.ColumnInt(12)); | |
| 352 url->set_autogenerate_keyword(s.ColumnBool(13)); | |
| 353 url->set_created_by_policy(s.ColumnBool(15)); | |
| 354 url->SetInstantURL(s.ColumnString(16)); | |
| 355 url->set_last_modified(Time::FromTimeT(s.ColumnInt64(17))); | |
| 356 url->set_sync_guid(s.ColumnString(18)); | |
| 357 | |
| 358 return url; | |
| 359 } | 354 } |
| 360 | 355 |
| 361 bool KeywordTable::GetSignatureData(std::string* backup) { | 356 bool KeywordTable::GetSignatureData(std::string* backup) { |
| 362 DCHECK(backup); | 357 DCHECK(backup); |
| 363 | 358 |
| 364 int64 backup_value = kInvalidTemplateURLID; | 359 int64 backup_value = kInvalidTemplateURLID; |
| 365 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { | 360 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { |
| 366 LOG(ERROR) << "No backup id for signing."; | 361 LOG(ERROR) << "No backup id for signing."; |
| 367 return false; | 362 return false; |
| 368 } | 363 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 int64 default_search_id = GetDefaultSearchProviderID(); | 461 int64 default_search_id = GetDefaultSearchProviderID(); |
| 467 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, | 462 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, |
| 468 default_search_id)) { | 463 default_search_id)) { |
| 469 LOG(ERROR) << "Can't write default search id backup."; | 464 LOG(ERROR) << "Can't write default search id backup."; |
| 470 return false; | 465 return false; |
| 471 } | 466 } |
| 472 | 467 |
| 473 *id = default_search_id; | 468 *id = default_search_id; |
| 474 return true; | 469 return true; |
| 475 } | 470 } |
| OLD | NEW |