Chromium Code Reviews| 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/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/string_split.h" | 9 #include "base/string_split.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/history/history_database.h" | 12 #include "chrome/browser/history/history_database.h" |
| 13 #include "chrome/browser/protector/protector.h" | |
| 14 #include "chrome/browser/protector/keys.h" | |
|
Ivan Korotkov
2011/10/19 20:24:37
Sort please.
whywhat
2011/10/20 20:50:10
Done.
| |
| 13 #include "chrome/browser/search_engines/template_url.h" | 15 #include "chrome/browser/search_engines/template_url.h" |
| 14 #include "crypto/hmac.h" | |
| 15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
| 16 #include "sql/statement.h" | 17 #include "sql/statement.h" |
| 17 | 18 |
| 18 using base::Time; | 19 using base::Time; |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // ID of the url column in keywords. | 23 // ID of the url column in keywords. |
| 23 const int kUrlIdPosition = 18; | 24 const int kUrlIdPosition = 18; |
| 24 | 25 |
| 25 // Keys used in the meta table. | 26 // Keys used in the meta table. |
| 26 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; | 27 const char kDefaultSearchProviderKey[] = "Default Search Provider ID"; |
| 27 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; | 28 const char kBuiltinKeywordVersion[] = "Builtin Keyword Version"; |
| 28 | 29 |
| 29 // Key for signing the default search provider backup. | |
| 30 // TOOD(avayvod): Add key for Google Chrome to internal repository. | |
| 31 const char kDefaultSearchProviderBackupSigningKey[] = | |
| 32 "Please, don't change default search engine!"; | |
| 33 | |
| 34 // Meta table key to store backup value for the default search provider. | 30 // Meta table key to store backup value for the default search provider. |
| 35 const char kDefaultSearchProviderBackupKey[] = | 31 const char kDefaultSearchProviderBackupKey[] = |
| 36 "Default Search Provider ID Backup"; | 32 "Default Search Provider ID Backup"; |
| 37 | 33 |
| 38 // Meta table key to store backup value signature for the default search | 34 // Meta table key to store backup value signature for the default search |
| 39 // provider. | 35 // provider. |
| 40 const char kDefaultSearchProviderBackupSignatureKey[] = | 36 const char kDefaultSearchProviderBackupSignatureKey[] = |
| 41 "Default Search Provider ID Backup Signature"; | 37 "Default Search Provider ID Backup Signature"; |
| 42 | 38 |
| 43 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { | 39 void BindURLToStatement(const TemplateURL& url, sql::Statement* s) { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 69 s->BindInt(13, url.logo_id()); | 65 s->BindInt(13, url.logo_id()); |
| 70 s->BindBool(14, url.created_by_policy()); | 66 s->BindBool(14, url.created_by_policy()); |
| 71 s->BindString(15, url.instant_url() ? url.instant_url()->url() : | 67 s->BindString(15, url.instant_url() ? url.instant_url()->url() : |
| 72 std::string()); | 68 std::string()); |
| 73 s->BindInt64(16, url.last_modified().ToTimeT()); | 69 s->BindInt64(16, url.last_modified().ToTimeT()); |
| 74 s->BindString(17, url.sync_guid()); | 70 s->BindString(17, url.sync_guid()); |
| 75 } | 71 } |
| 76 | 72 |
| 77 // Signs search provider id and returns its signature. | 73 // Signs search provider id and returns its signature. |
| 78 std::string GetSearchProviderIDSignature(int64 id) { | 74 std::string GetSearchProviderIDSignature(int64 id) { |
| 79 crypto::HMAC hmac(crypto::HMAC::SHA256); | 75 return protector::Sign(base::Int64ToString(id)); |
| 80 DCHECK(hmac.Init(kDefaultSearchProviderBackupSigningKey)); | |
| 81 | |
| 82 std::string id_to_sign(base::Int64ToString(id)); | |
| 83 std::vector<unsigned char> digest(hmac.DigestLength()); | |
| 84 DCHECK(hmac.Sign(id_to_sign, &digest[0], digest.size())); | |
| 85 | |
| 86 return std::string(&digest[0], &digest[0] + digest.size()); | |
| 87 } | 76 } |
| 88 | 77 |
| 89 // Checks if signature for search provider id is correct and returns the | 78 // Checks if signature for search provider id is correct and returns the |
| 90 // result. | 79 // result. |
| 91 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { | 80 bool IsSearchProviderIDValid(int64 id, const std::string& signature) { |
| 92 return signature == GetSearchProviderIDSignature(id); | 81 return signature == GetSearchProviderIDSignature(id); |
| 93 } | 82 } |
| 94 | 83 |
| 95 } // anonymous namespace | 84 } // anonymous namespace |
| 96 | 85 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 "sync_guid=? WHERE id=?")); | 240 "sync_guid=? WHERE id=?")); |
| 252 if (!s) { | 241 if (!s) { |
| 253 NOTREACHED() << "Statement prepare failed"; | 242 NOTREACHED() << "Statement prepare failed"; |
| 254 return false; | 243 return false; |
| 255 } | 244 } |
| 256 BindURLToStatement(url, &s); | 245 BindURLToStatement(url, &s); |
| 257 s.BindInt64(kUrlIdPosition, url.id()); | 246 s.BindInt64(kUrlIdPosition, url.id()); |
| 258 return s.Run(); | 247 return s.Run(); |
| 259 } | 248 } |
| 260 | 249 |
| 250 string16 KeywordTable::GetKeywordShortName(int64 id) { | |
| 251 sql::Statement s(db_->GetUniqueStatement( | |
| 252 "SELECT short_name FROM keywords WHERE id=?")); | |
| 253 if (!s) { | |
| 254 NOTREACHED() << "Statement prepare failed"; | |
| 255 return string16(); | |
| 256 } | |
| 257 s.BindInt64(0, id); | |
| 258 if (s.Step()) { | |
| 259 std::string short_name(s.ColumnString(0)); | |
| 260 DCHECK(!short_name.empty()); | |
| 261 return UTF8ToUTF16(short_name); | |
| 262 } | |
| 263 LOG(WARNING) << "Keyword with id " << id << " is not found."; | |
| 264 return string16(); | |
| 265 } | |
| 266 | |
| 261 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { | 267 bool KeywordTable::SetDefaultSearchProviderID(int64 id) { |
| 262 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && | 268 return meta_table_->SetValue(kDefaultSearchProviderKey, id) && |
| 263 SetDefaultSearchProviderBackupID(id); | 269 SetDefaultSearchProviderBackupID(id); |
| 264 } | 270 } |
| 265 | 271 |
| 266 int64 KeywordTable::GetDefaultSearchProviderID() { | 272 int64 KeywordTable::GetDefaultSearchProviderID() { |
| 267 int64 value = 0; | 273 int64 value = 0; |
| 268 meta_table_->GetValue(kDefaultSearchProviderKey, &value); | 274 meta_table_->GetValue(kDefaultSearchProviderKey, &value); |
| 269 int64 backup_value = 0; | 275 int64 backup_value = 0; |
| 270 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); | 276 meta_table_->GetValue(kDefaultSearchProviderBackupKey, &backup_value); |
| 271 std::string backup_signature; | 277 std::string backup_signature; |
| 272 meta_table_->GetValue( | 278 meta_table_->GetValue( |
| 273 kDefaultSearchProviderBackupSignatureKey, &backup_signature); | 279 kDefaultSearchProviderBackupSignatureKey, &backup_signature); |
| 274 if (!IsSearchProviderIDValid(backup_value, backup_signature)) { | 280 if (!IsSearchProviderIDValid(backup_value, backup_signature)) { |
| 275 // TODO(avayvod): Notify UI about the setting having been hijacked and | 281 scoped_refptr<protector::DefaultSearchProviderChange> change( |
| 276 // the backup value lost. | 282 new protector::DefaultSearchProviderChange(this, 0, value)); |
| 277 return value; | 283 protector::NotifyUserOfChange(change); |
| 284 SetDefaultSearchProviderID(0); | |
| 285 return 0; | |
| 278 } | 286 } |
| 279 if (value != backup_value) { | 287 if (value != backup_value) { |
| 280 // TODO(avayvod): Notify UI about the setting having been hijacked. | 288 scoped_refptr<protector::DefaultSearchProviderChange> change( |
| 289 new protector::DefaultSearchProviderChange(this, backup_value, value)); | |
| 290 protector::NotifyUserOfChange(change); | |
| 291 SetDefaultSearchProviderID(backup_value); | |
| 281 return backup_value; | 292 return backup_value; |
| 282 } | 293 } |
| 283 return value; | 294 return value; |
| 284 } | 295 } |
| 285 | 296 |
| 286 bool KeywordTable::SetBuiltinKeywordVersion(int version) { | 297 bool KeywordTable::SetBuiltinKeywordVersion(int version) { |
| 287 return meta_table_->SetValue(kBuiltinKeywordVersion, version); | 298 return meta_table_->SetValue(kBuiltinKeywordVersion, version); |
| 288 } | 299 } |
| 289 | 300 |
| 290 int KeywordTable::GetBuiltinKeywordVersion() { | 301 int KeywordTable::GetBuiltinKeywordVersion() { |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 379 return | 390 return |
| 380 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && | 391 meta_table_->SetValue(kDefaultSearchProviderBackupKey, id) && |
| 381 SetDefaultSearchProviderBackupIDSignature(id); | 392 SetDefaultSearchProviderBackupIDSignature(id); |
| 382 } | 393 } |
| 383 | 394 |
| 384 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { | 395 bool KeywordTable::SetDefaultSearchProviderBackupIDSignature(int64 id) { |
| 385 return meta_table_->SetValue( | 396 return meta_table_->SetValue( |
| 386 kDefaultSearchProviderBackupSignatureKey, | 397 kDefaultSearchProviderBackupSignatureKey, |
| 387 GetSearchProviderIDSignature(id)); | 398 GetSearchProviderIDSignature(id)); |
| 388 } | 399 } |
| OLD | NEW |