| 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=?, " |
| 163 "last_modified=?, sync_guid=? WHERE id=?")); | 166 "last_modified=?, sync_guid=? WHERE id=?")); |
| 164 BindURLToStatement(url, &s, 18, 0); // "18" binds id() as the last item. | 167 BindURLToStatement(url, &s, 18, 0); // "18" binds id() as the last item. |
| 165 | 168 |
| 166 return s.Run() && UpdateBackupSignature(); | 169 return s.Run() && UpdateBackupSignature(); |
| 167 } | 170 } |
| 168 | 171 |
| 169 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 172 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| 170 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 173 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 171 UpdateBackupSignature(); | 174 UpdateBackupSignature(); |
| 172 } | 175 } |
| 173 | 176 |
| 174 int64 KeywordTable::GetDefaultSearchProviderID() { | 177 int64 KeywordTable::GetDefaultSearchProviderID() { |
| 175 int64 value = kInvalidTemplateURLID; | 178 int64 value = kInvalidTemplateURLID; |
| 176 meta_table_->GetValue(kDefaultSearchProviderKey, &value); | 179 meta_table_->GetValue(kDefaultSearchProviderKey, &value); |
| 177 return value; | 180 return value; |
| 178 } | 181 } |
| 179 | 182 |
| 180 TemplateURL* KeywordTable::GetDefaultSearchProviderBackup() { | 183 bool KeywordTable::GetDefaultSearchProviderBackup(TemplateURLData* backup) { |
| 181 if (!IsBackupSignatureValid()) | 184 if (!IsBackupSignatureValid()) |
| 182 return NULL; | 185 return false; |
| 183 | 186 |
| 184 int64 backup_id = kInvalidTemplateURLID; | 187 int64 backup_id = kInvalidTemplateURLID; |
| 185 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id)) { | 188 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_id)) { |
| 186 LOG(ERROR) << "No default search id backup found."; | 189 LOG(ERROR) << "No default search id backup found."; |
| 187 return NULL; | 190 return false; |
| 188 } | 191 } |
| 189 std::string query("SELECT " + std::string(kKeywordColumns) + | 192 std::string query("SELECT " + std::string(kKeywordColumns) + |
| 190 " FROM keywords_backup WHERE id=?"); | 193 " FROM keywords_backup WHERE id=?"); |
| 191 sql::Statement s(db_->GetUniqueStatement(query.c_str())); | 194 sql::Statement s(db_->GetUniqueStatement(query.c_str())); |
| 192 s.BindInt64(0, backup_id); | 195 s.BindInt64(0, backup_id); |
| 193 | 196 |
| 194 if (!s.Step()) { | 197 if (!s.Step()) { |
| 195 LOG_IF(ERROR, s.Succeeded()) | 198 LOG_IF(ERROR, s.Succeeded()) |
| 196 << "No default search provider with backup id."; | 199 << "No default search provider with backup id."; |
| 197 return NULL; | 200 return NULL; |
| 198 } | 201 } |
| 199 | 202 |
| 200 TemplateURL* template_url = GetKeywordDataFromStatement(s); | 203 GetKeywordDataFromStatement(s, backup); |
| 201 // ID has no meaning for the backup and should be kInvalidTemplateURLID in | 204 // ID has no meaning for the backup and should be kInvalidTemplateURLID in |
| 202 // case the TemplateURL will be added to keywords if missing. | 205 // case the TemplateURL will be added to keywords if missing. |
| 203 template_url->set_id(kInvalidTemplateURLID); | 206 backup->id = kInvalidTemplateURLID; |
| 204 return template_url; | 207 return true; |
| 205 } | 208 } |
| 206 | 209 |
| 207 bool KeywordTable::DidDefaultSearchProviderChange() { | 210 bool KeywordTable::DidDefaultSearchProviderChange() { |
| 208 if (!IsBackupSignatureValid()) { | 211 if (!IsBackupSignatureValid()) { |
| 209 UMA_HISTOGRAM_ENUMERATION( | 212 UMA_HISTOGRAM_ENUMERATION( |
| 210 protector::kProtectorHistogramDefaultSearchProvider, | 213 protector::kProtectorHistogramDefaultSearchProvider, |
| 211 protector::kProtectorErrorBackupInvalid, | 214 protector::kProtectorErrorBackupInvalid, |
| 212 protector::kProtectorErrorCount); | 215 protector::kProtectorErrorCount); |
| 213 LOG(ERROR) << "Backup signature is invalid."; | 216 LOG(ERROR) << "Backup signature is invalid."; |
| 214 return true; | 217 return true; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 317 | 320 |
| 318 bool KeywordTable::MigrateToVersion39AddSyncGUIDColumn() { | 321 bool KeywordTable::MigrateToVersion39AddSyncGUIDColumn() { |
| 319 return db_->Execute("ALTER TABLE keywords ADD COLUMN sync_guid VARCHAR"); | 322 return db_->Execute("ALTER TABLE keywords ADD COLUMN sync_guid VARCHAR"); |
| 320 } | 323 } |
| 321 | 324 |
| 322 bool KeywordTable::MigrateToVersion44AddDefaultSearchProviderBackup() { | 325 bool KeywordTable::MigrateToVersion44AddDefaultSearchProviderBackup() { |
| 323 return IsBackupSignatureValid() || UpdateBackupSignature(); | 326 return IsBackupSignatureValid() || UpdateBackupSignature(); |
| 324 } | 327 } |
| 325 | 328 |
| 326 // static | 329 // static |
| 327 TemplateURL* KeywordTable::GetKeywordDataFromStatement( | 330 void KeywordTable::GetKeywordDataFromStatement(const sql::Statement& s, |
| 328 const sql::Statement& s) { | 331 TemplateURLData* data) { |
| 329 TemplateURL* url = new TemplateURL; | 332 DCHECK(data); |
| 330 url->set_id(s.ColumnInt64(0)); | 333 data->short_name = s.ColumnString16(1); |
| 331 url->set_short_name(s.ColumnString16(1)); | 334 data->SetKeyword(s.ColumnString16(2)); |
| 332 url->set_keyword(s.ColumnString16(2)); | 335 data->SetAutogenerateKeyword(s.ColumnBool(13)); |
| 333 std::string tmp(s.ColumnString(3)); | 336 data->SetURL(s.ColumnString(4)); |
| 334 if (!tmp.empty()) | 337 data->suggestions_url = s.ColumnString(11); |
| 335 url->set_favicon_url(GURL(tmp)); | 338 data->instant_url = s.ColumnString(16); |
| 336 url->SetURL(s.ColumnString(4)); | 339 data->favicon_url = GURL(s.ColumnString(3)); |
| 337 url->set_safe_for_autoreplace(s.ColumnBool(5)); | 340 data->originating_url = GURL(s.ColumnString(6)); |
| 338 tmp = s.ColumnString(6); | 341 data->show_in_default_list = s.ColumnBool(10); |
| 339 if (!tmp.empty()) | 342 data->safe_for_autoreplace = s.ColumnBool(5); |
| 340 url->set_originating_url(GURL(tmp)); | 343 base::SplitString(s.ColumnString(9), ';', &data->input_encodings); |
| 341 url->set_date_created(Time::FromTimeT(s.ColumnInt64(7))); | 344 data->id = s.ColumnInt64(0); |
| 342 url->set_usage_count(s.ColumnInt(8)); | 345 data->date_created = Time::FromTimeT(s.ColumnInt64(7)); |
| 343 std::vector<std::string> encodings; | 346 data->last_modified = Time::FromTimeT(s.ColumnInt64(17)); |
| 344 base::SplitString(s.ColumnString(9), ';', &encodings); | 347 data->created_by_policy = s.ColumnBool(15); |
| 345 url->set_input_encodings(encodings); | 348 data->usage_count = s.ColumnInt(8); |
| 346 url->set_show_in_default_list(s.ColumnBool(10)); | 349 data->prepopulate_id = s.ColumnInt(12); |
| 347 url->SetSuggestionsURL(s.ColumnString(11)); | 350 data->sync_guid = s.ColumnString(18); |
| 348 url->SetPrepopulateId(s.ColumnInt(12)); | |
| 349 url->set_autogenerate_keyword(s.ColumnBool(13)); | |
| 350 url->set_created_by_policy(s.ColumnBool(15)); | |
| 351 url->SetInstantURL(s.ColumnString(16)); | |
| 352 url->set_last_modified(Time::FromTimeT(s.ColumnInt64(17))); | |
| 353 url->set_sync_guid(s.ColumnString(18)); | |
| 354 | |
| 355 return url; | |
| 356 } | 351 } |
| 357 | 352 |
| 358 bool KeywordTable::GetSignatureData(std::string* backup) { | 353 bool KeywordTable::GetSignatureData(std::string* backup) { |
| 359 DCHECK(backup); | 354 DCHECK(backup); |
| 360 | 355 |
| 361 int64 backup_value = kInvalidTemplateURLID; | 356 int64 backup_value = kInvalidTemplateURLID; |
| 362 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { | 357 if (!meta_table_->GetValue(kDefaultSearchIDBackupKey, &backup_value)) { |
| 363 LOG(ERROR) << "No backup id for signing."; | 358 LOG(ERROR) << "No backup id for signing."; |
| 364 return false; | 359 return false; |
| 365 } | 360 } |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 int64 default_search_id = GetDefaultSearchProviderID(); | 458 int64 default_search_id = GetDefaultSearchProviderID(); |
| 464 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, | 459 if (!meta_table_->SetValue(kDefaultSearchIDBackupKey, |
| 465 default_search_id)) { | 460 default_search_id)) { |
| 466 LOG(ERROR) << "Can't write default search id backup."; | 461 LOG(ERROR) << "Can't write default search id backup."; |
| 467 return false; | 462 return false; |
| 468 } | 463 } |
| 469 | 464 |
| 470 *id = default_search_id; | 465 *id = default_search_id; |
| 471 return true; | 466 return true; |
| 472 } | 467 } |
| OLD | NEW |