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