| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
| 9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_split.h" | 10 #include "base/string_split.h" |
| 11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "chrome/browser/history/history_database.h" | 13 #include "chrome/browser/history/history_database.h" |
| 14 #include "chrome/browser/protector/histograms.h" |
| 15 #include "chrome/browser/protector/protector.h" |
| 14 #include "chrome/browser/search_engines/template_url.h" | 16 #include "chrome/browser/search_engines/template_url.h" |
| 15 #include "crypto/hmac.h" | |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 #include "sql/statement.h" | 18 #include "sql/statement.h" |
| 18 | 19 |
| 19 using base::Time; | 20 using base::Time; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 // ID of the url column in keywords. | 24 // ID of the url column in keywords. |
| 24 const int kUrlIdPosition = 18; | 25 const int kUrlIdPosition = 18; |
| 25 | 26 |
| 26 // Keys used in the meta table. | 27 // Keys used in the meta table. |
| 27 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; | 28 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; |
| 28 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; | 29 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; |
| 29 | 30 |
| 30 // Key for signing the default search provider backup. | |
| 31 // TOOD(avayvod): Add key for Google Chrome to internal repository. | |
| 32 const char kDefaultSearchProviderBackupSigningKey[] = | |
| 33 "Please, don't change default search engine!"; | |
| 34 | |
| 35 // Meta table key to store backup value for the default search provider. | 31 // Meta table key to store backup value for the default search provider. |
| 36 const char kDefaultSearchProviderBackupKey[] = | 32 const char kDefaultSearchProviderBackupKey[] = |
| 37 "Default Search Provider ID Backup"; | 33 "Default Search Provider ID Backup"; |
| 38 | 34 |
| 39 // Meta table key to store backup value signature for the default search | 35 // Meta table key to store backup value signature for the default search |
| 40 // provider. | 36 // provider. |
| 41 const char kDefaultSearchProviderBackupSignatureKey[] = | 37 const char kDefaultSearchProviderBackupSignatureKey[] = |
| 42 "Default Search Provider ID Backup Signature"; | 38 "Default Search Provider ID Backup Signature"; |
| 43 | 39 |
| 44 // Histogram name to report protection errors for the default search | |
| 45 // provider. | |
| 46 const char kProtectorHistogramDefaultSearchProvider[] = | |
| 47 "Protector.DefaultSearchProvider"; | |
| 48 | |
| 49 // Protector histogram values. TODO(avayvod): Move to protector.h/cc | |
| 50 enum ProtectorError { | |
| 51 kProtectorErrorBackupInvalid, | |
| 52 kProtectorErrorValueChanged, | |
| 53 | |
| 54 // This is for convenience only, must always be the last. | |
| 55 kProtectorErrorCount | |
| 56 }; | |
| 57 | |
| 58 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { | 40 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { |
| 59 s->BindString(0, UTF16ToUTF8(url.short_name())); | 41 s->BindString(0, UTF16ToUTF8(url.short_name())); |
| 60 s->BindString(1, UTF16ToUTF8(url.keyword())); | 42 s->BindString(1, UTF16ToUTF8(url.keyword())); |
| 61 GURL favicon_url = url.GetFaviconURL(); | 43 GURL favicon_url = url.GetFaviconURL(); |
| 62 if (!favicon_url.is_valid()) { | 44 if (!favicon_url.is_valid()) { |
| 63 s->BindString(2, std::string()); | 45 s->BindString(2, std::string()); |
| 64 } else { | 46 } else { |
| 65 s->BindString(2, history::HistoryDatabase::GURLToDatabaseURL( | 47 s->BindString(2, history::HistoryDatabase::GURLToDatabaseURL( |
| 66 url.GetFaviconURL())); | 48 url.GetFaviconURL())); |
| 67 } | 49 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 84 s->BindInt(13, url.logo_id()); | 66 s->BindInt(13, url.logo_id()); |
| 85 s->BindBool(14, url.created_by_policy()); | 67 s->BindBool(14, url.created_by_policy()); |
| 86 s->BindString(15, url.instant_url() ? url.instant_url()->url() : | 68 s->BindString(15, url.instant_url() ? url.instant_url()->url() : |
| 87 std::string()); | 69 std::string()); |
| 88 s->BindInt64(16, url.last_modified().ToTimeT()); | 70 s->BindInt64(16, url.last_modified().ToTimeT()); |
| 89 s->BindString(17, url.sync_guid()); | 71 s->BindString(17, url.sync_guid()); |
| 90 } | 72 } |
| 91 | 73 |
| 92 // Signs search provider id and returns its signature. | 74 // Signs search provider id and returns its signature. |
| 93 std::string GetSearchProviderIDSignature(int64 id) { | 75 std::string GetSearchProviderIDSignature(int64 id) { |
| 94 crypto::HMAC hmac(crypto::HMAC::SHA256); | 76 return protector::SignSetting(base::Int64ToString(id)); |
| 95 DCHECK(hmac.Init(kDefaultSearchProviderBackupSigningKey)); | |
| 96 | |
| 97 std::string id_to_sign(base::Int64ToString(id)); | |
| 98 std::vector<unsigned char> digest(hmac.DigestLength()); | |
| 99 DCHECK(hmac.Sign(id_to_sign, &digest[0], digest.size())); | |
| 100 | |
| 101 return std::string(&digest[0], &digest[0] + digest.size()); | |
| 102 } | 77 } |
| 103 | 78 |
| 104 // Checks if signature for search provider id is correct and returns the | 79 // Checks if signature for search provider id is correct and returns the |
| 105 // result. | 80 // result. |
| 106 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { | 81 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { |
| 107 return signature == GetSearchProviderIDSignature(id); | 82 return signature == GetSearchProviderIDSignature(id); |
| 108 } | 83 } |
| 109 | 84 |
| 110 } // anonymous namespace | 85 } // anonymous namespace |
| 111 | 86 |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 } | 249 } |
| 275 | 250 |
| 276 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 251 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| 277 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 252 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 278 SetDefaultSearchProviderBackupID(id); | 253 SetDefaultSearchProviderBackupID(id); |
| 279 } | 254 } |
| 280 | 255 |
| 281 int64 KeywordTable::GetDefaultSearchProviderID() { | 256 int64 KeywordTable::GetDefaultSearchProviderID() { |
| 282 int64 value = 0; | 257 int64 value = 0; |
| 283 meta_table_->GetValue(kDefaultSearchProviderKey, &value); | 258 meta_table_->GetValue(kDefaultSearchProviderKey, &value); |
| 259 return value; |
| 260 } |
| 261 |
| 262 int64 KeywordTable::GetDefaultSearchProviderIDBackup() { |
| 284 int64 backup_value = 0; | 263 int64 backup_value = 0; |
| 285 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); | 264 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); |
| 286 std::string backup_signature; | 265 std::string backup_signature; |
| 287 meta_table_->GetValue( | 266 meta_table_->GetValue( |
| 288 kDefaultSearchProviderBackupSignatureKey, &backup_signature); | 267 kDefaultSearchProviderBackupSignatureKey, &backup_signature); |
| 289 // For now only track how often signature or backup is lost or value is | 268 if (!IsSearchProviderIDValid(backup_value, backup_signature)) |
| 290 // changed. Don't change the value since UI is not working yet. | 269 return 0; |
| 270 return backup_value; |
| 271 } |
| 272 |
| 273 bool KeywordTable::DidDefaultSearchProviderChange() { |
| 274 int64 backup_value = 0; |
| 275 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); |
| 276 std::string backup_signature; |
| 277 meta_table_->GetValue( |
| 278 kDefaultSearchProviderBackupSignatureKey, &backup_signature); |
| 291 if (!IsSearchProviderIDValid(backup_value, backup_signature)) { | 279 if (!IsSearchProviderIDValid(backup_value, backup_signature)) { |
| 292 UMA_HISTOGRAM_ENUMERATION(kProtectorHistogramDefaultSearchProvider, | 280 UMA_HISTOGRAM_ENUMERATION( |
| 293 kProtectorErrorBackupInvalid, | 281 protector::kProtectorHistogramDefaultSearchProvider, |
| 294 kProtectorErrorCount); | 282 protector::kProtectorErrorBackupInvalid, |
| 295 SetDefaultSearchProviderBackupID(value); | 283 protector::kProtectorErrorCount); |
| 296 } else if (value != backup_value) { | 284 return true; |
| 297 UMA_HISTOGRAM_ENUMERATION(kProtectorHistogramDefaultSearchProvider, | 285 } else if (backup_value != GetDefaultSearchProviderID()) { |
| 298 kProtectorErrorValueChanged, | 286 UMA_HISTOGRAM_ENUMERATION( |
| 299 kProtectorErrorCount); | 287 protector::kProtectorHistogramDefaultSearchProvider, |
| 300 SetDefaultSearchProviderBackupID(value); | 288 protector::kProtectorErrorValueChanged, |
| 289 protector::kProtectorErrorCount); |
| 290 return true; |
| 301 } | 291 } |
| 302 return value; | 292 return false; |
| 303 } | 293 } |
| 304 | 294 |
| 305 bool KeywordTable::SetBuiltinKeywordVersion(int version) { | 295 bool KeywordTable::SetBuiltinKeywordVersion(int version) { |
| 306 return meta_table_->SetValue(kBuiltinKeywordVersion, version); | 296 return meta_table_->SetValue(kBuiltinKeywordVersion, version); |
| 307 } | 297 } |
| 308 | 298 |
| 309 int KeywordTable::GetBuiltinKeywordVersion() { | 299 int KeywordTable::GetBuiltinKeywordVersion() { |
| 310 int version = 0; | 300 int version = 0; |
| 311 if (!meta_table_->GetValue(kBuiltinKeywordVersion, &version)) | 301 if (!meta_table_->GetValue(kBuiltinKeywordVersion, &version)) |
| 312 return 0; | 302 return 0; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 return | 390 return |
| 401 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && | 391 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && |
| 402 SetDefaultSearchProviderBackupIDSignature(id); | 392 SetDefaultSearchProviderBackupIDSignature(id); |
| 403 } | 393 } |
| 404 | 394 |
| 405 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { | 395 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { |
| 406 return meta_table_->SetValue( | 396 return meta_table_->SetValue( |
| 407 kDefaultSearchProviderBackupSignatureKey, | 397 kDefaultSearchProviderBackupSignatureKey, |
| 408 GetSearchProviderIDSignature(id)); | 398 GetSearchProviderIDSignature(id)); |
| 409 } | 399 } |
| OLD | NEW |