Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/history/core/browser/typed_url_sync_metadata_database.h" | 5 #include "components/history/core/browser/typed_url_sync_metadata_database.h" |
| 6 | 6 |
| 7 #include "base/big_endian.h" | |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "sql/statement.h" | 9 #include "sql/statement.h" |
| 10 | 10 |
| 11 namespace history { | 11 namespace history { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 // Key in sql::MetaTable, the value will be Serialization of sync | 15 // Key in sql::MetaTable, the value will be Serialization of sync |
| 16 // ModelTypeState, which is for tracking sync state of typed url datatype. | 16 // ModelTypeState, which is for tracking sync state of typed url datatype. |
| 17 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; | 17 const char kTypedURLModelTypeStateKey[] = "typed_url_model_type_state"; |
| 18 | 18 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 48 } | 48 } |
| 49 | 49 |
| 50 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( | 50 bool TypedURLSyncMetadataDatabase::UpdateSyncMetadata( |
| 51 syncer::ModelType model_type, | 51 syncer::ModelType model_type, |
| 52 const std::string& storage_key, | 52 const std::string& storage_key, |
| 53 const sync_pb::EntityMetadata& metadata) { | 53 const sync_pb::EntityMetadata& metadata) { |
| 54 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 54 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 55 << "Only the TYPED_URLS model type is supported"; | 55 << "Only the TYPED_URLS model type is supported"; |
| 56 | 56 |
| 57 int64_t storage_key_int = 0; | 57 int64_t storage_key_int = 0; |
| 58 if (!base::StringToInt64(storage_key, &storage_key_int)) { | 58 base::ReadBigEndian(storage_key.data(), &storage_key_int); |
|
pavely
2017/06/01 20:49:18
When deserializing could you DCHECK that size of s
Gang Wu
2017/06/02 00:00:24
Done.
| |
| 59 return false; | 59 |
| 60 } | |
| 61 sql::Statement s(GetDB().GetUniqueStatement( | 60 sql::Statement s(GetDB().GetUniqueStatement( |
| 62 "INSERT OR REPLACE INTO typed_url_sync_metadata " | 61 "INSERT OR REPLACE INTO typed_url_sync_metadata " |
| 63 "(storage_key, value) VALUES(?, ?)")); | 62 "(storage_key, value) VALUES(?, ?)")); |
| 64 s.BindInt64(0, storage_key_int); | 63 s.BindInt64(0, storage_key_int); |
| 65 s.BindString(1, metadata.SerializeAsString()); | 64 s.BindString(1, metadata.SerializeAsString()); |
| 66 | 65 |
| 67 return s.Run(); | 66 return s.Run(); |
| 68 } | 67 } |
| 69 | 68 |
| 70 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( | 69 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
| 71 syncer::ModelType model_type, | 70 syncer::ModelType model_type, |
| 72 const std::string& storage_key) { | 71 const std::string& storage_key) { |
| 73 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 72 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
| 74 << "Only the TYPED_URLS model type is supported"; | 73 << "Only the TYPED_URLS model type is supported"; |
| 75 | 74 |
| 76 int64_t storage_key_int = 0; | 75 int64_t storage_key_int = 0; |
| 77 if (!base::StringToInt64(storage_key, &storage_key_int)) { | 76 base::ReadBigEndian(storage_key.data(), &storage_key_int); |
| 78 return false; | 77 |
| 79 } | |
| 80 sql::Statement s(GetDB().GetUniqueStatement( | 78 sql::Statement s(GetDB().GetUniqueStatement( |
| 81 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); | 79 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); |
| 82 s.BindInt64(0, storage_key_int); | 80 s.BindInt64(0, storage_key_int); |
| 83 | 81 |
| 84 return s.Run(); | 82 return s.Run(); |
| 85 } | 83 } |
| 86 | 84 |
| 87 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( | 85 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( |
| 88 syncer::ModelType model_type, | 86 syncer::ModelType model_type, |
| 89 const sync_pb::ModelTypeState& model_type_state) { | 87 const sync_pb::ModelTypeState& model_type_state) { |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 115 return true; | 113 return true; |
| 116 } | 114 } |
| 117 | 115 |
| 118 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( | 116 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( |
| 119 syncer::MetadataBatch* metadata_batch) { | 117 syncer::MetadataBatch* metadata_batch) { |
| 120 DCHECK(metadata_batch); | 118 DCHECK(metadata_batch); |
| 121 sql::Statement s(GetDB().GetUniqueStatement( | 119 sql::Statement s(GetDB().GetUniqueStatement( |
| 122 "SELECT storage_key, value FROM typed_url_sync_metadata")); | 120 "SELECT storage_key, value FROM typed_url_sync_metadata")); |
| 123 | 121 |
| 124 while (s.Step()) { | 122 while (s.Step()) { |
| 125 std::string storage_key = base::Int64ToString(s.ColumnInt64(0)); | 123 std::string storage_key(8, 0); |
|
pavely
2017/06/01 20:49:18
Same comment as in typed_url_sync_bridge.cc
Gang Wu
2017/06/02 00:00:24
Done.
| |
| 124 base::WriteBigEndian(&storage_key[0], s.ColumnInt64(0)); | |
| 126 std::string serialized_metadata = s.ColumnString(1); | 125 std::string serialized_metadata = s.ColumnString(1); |
| 127 sync_pb::EntityMetadata entity_metadata; | 126 sync_pb::EntityMetadata entity_metadata; |
| 128 if (entity_metadata.ParseFromString(serialized_metadata)) { | 127 if (entity_metadata.ParseFromString(serialized_metadata)) { |
| 129 metadata_batch->AddMetadata(storage_key, entity_metadata); | 128 metadata_batch->AddMetadata(storage_key, entity_metadata); |
| 130 } else { | 129 } else { |
| 131 DLOG(WARNING) << "Failed to deserialize TYPED_URLS model type " | 130 DLOG(WARNING) << "Failed to deserialize TYPED_URLS model type " |
| 132 "sync_pb::EntityMetadata."; | 131 "sync_pb::EntityMetadata."; |
| 133 return false; | 132 return false; |
| 134 } | 133 } |
| 135 } | 134 } |
| 136 return true; | 135 return true; |
| 137 } | 136 } |
| 138 | 137 |
| 139 bool TypedURLSyncMetadataDatabase::GetModelTypeState( | 138 bool TypedURLSyncMetadataDatabase::GetModelTypeState( |
| 140 sync_pb::ModelTypeState* state) { | 139 sync_pb::ModelTypeState* state) { |
| 141 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); | 140 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
| 142 std::string serialized_state; | 141 std::string serialized_state; |
| 143 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { | 142 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { |
| 144 return true; | 143 return true; |
| 145 } | 144 } |
| 146 | 145 |
| 147 return state->ParseFromString(serialized_state); | 146 return state->ParseFromString(serialized_state); |
| 148 } | 147 } |
| 149 | 148 |
| 150 } // namespace history | 149 } // namespace history |
| OLD | NEW |