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 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int)); |
59 return false; | 60 base::ReadBigEndian(storage_key.data(), &storage_key_int); |
brettw
2017/06/05 17:31:15
The commit message says you changed this to big en
Gang Wu
2017/06/05 20:04:14
Changing to big endian is from a comment from firs
| |
60 } | 61 // Make sure storage_key_int is set. |
62 DCHECK_NE(storage_key_int, 0); | |
63 | |
61 sql::Statement s(GetDB().GetUniqueStatement( | 64 sql::Statement s(GetDB().GetUniqueStatement( |
62 "INSERT OR REPLACE INTO typed_url_sync_metadata " | 65 "INSERT OR REPLACE INTO typed_url_sync_metadata " |
63 "(storage_key, value) VALUES(?, ?)")); | 66 "(storage_key, value) VALUES(?, ?)")); |
64 s.BindInt64(0, storage_key_int); | 67 s.BindInt64(0, storage_key_int); |
65 s.BindString(1, metadata.SerializeAsString()); | 68 s.BindString(1, metadata.SerializeAsString()); |
66 | 69 |
67 return s.Run(); | 70 return s.Run(); |
68 } | 71 } |
69 | 72 |
70 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( | 73 bool TypedURLSyncMetadataDatabase::ClearSyncMetadata( |
71 syncer::ModelType model_type, | 74 syncer::ModelType model_type, |
72 const std::string& storage_key) { | 75 const std::string& storage_key) { |
73 DCHECK_EQ(model_type, syncer::TYPED_URLS) | 76 DCHECK_EQ(model_type, syncer::TYPED_URLS) |
74 << "Only the TYPED_URLS model type is supported"; | 77 << "Only the TYPED_URLS model type is supported"; |
75 | 78 |
76 int64_t storage_key_int = 0; | 79 int64_t storage_key_int = 0; |
77 if (!base::StringToInt64(storage_key, &storage_key_int)) { | 80 DCHECK_EQ(storage_key.size(), sizeof(storage_key_int)); |
78 return false; | 81 base::ReadBigEndian(storage_key.data(), &storage_key_int); |
79 } | 82 // Make sure storage_key_int is set. |
83 DCHECK_NE(storage_key_int, 0); | |
84 | |
80 sql::Statement s(GetDB().GetUniqueStatement( | 85 sql::Statement s(GetDB().GetUniqueStatement( |
81 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); | 86 "DELETE FROM typed_url_sync_metadata WHERE storage_key=?")); |
82 s.BindInt64(0, storage_key_int); | 87 s.BindInt64(0, storage_key_int); |
83 | 88 |
84 return s.Run(); | 89 return s.Run(); |
85 } | 90 } |
86 | 91 |
87 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( | 92 bool TypedURLSyncMetadataDatabase::UpdateModelTypeState( |
88 syncer::ModelType model_type, | 93 syncer::ModelType model_type, |
89 const sync_pb::ModelTypeState& model_type_state) { | 94 const sync_pb::ModelTypeState& model_type_state) { |
(...skipping 25 matching lines...) Expand all Loading... | |
115 return true; | 120 return true; |
116 } | 121 } |
117 | 122 |
118 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( | 123 bool TypedURLSyncMetadataDatabase::GetAllSyncEntityMetadata( |
119 syncer::MetadataBatch* metadata_batch) { | 124 syncer::MetadataBatch* metadata_batch) { |
120 DCHECK(metadata_batch); | 125 DCHECK(metadata_batch); |
121 sql::Statement s(GetDB().GetUniqueStatement( | 126 sql::Statement s(GetDB().GetUniqueStatement( |
122 "SELECT storage_key, value FROM typed_url_sync_metadata")); | 127 "SELECT storage_key, value FROM typed_url_sync_metadata")); |
123 | 128 |
124 while (s.Step()) { | 129 while (s.Step()) { |
125 std::string storage_key = base::Int64ToString(s.ColumnInt64(0)); | 130 std::string storage_key(sizeof(URLID), 0); |
131 base::WriteBigEndian<URLID>(&storage_key[0], s.ColumnInt64(0)); | |
126 std::string serialized_metadata = s.ColumnString(1); | 132 std::string serialized_metadata = s.ColumnString(1); |
127 sync_pb::EntityMetadata entity_metadata; | 133 sync_pb::EntityMetadata entity_metadata; |
128 if (entity_metadata.ParseFromString(serialized_metadata)) { | 134 if (entity_metadata.ParseFromString(serialized_metadata)) { |
129 metadata_batch->AddMetadata(storage_key, entity_metadata); | 135 metadata_batch->AddMetadata(storage_key, entity_metadata); |
130 } else { | 136 } else { |
131 DLOG(WARNING) << "Failed to deserialize TYPED_URLS model type " | 137 DLOG(WARNING) << "Failed to deserialize TYPED_URLS model type " |
132 "sync_pb::EntityMetadata."; | 138 "sync_pb::EntityMetadata."; |
133 return false; | 139 return false; |
134 } | 140 } |
135 } | 141 } |
136 return true; | 142 return true; |
137 } | 143 } |
138 | 144 |
139 bool TypedURLSyncMetadataDatabase::GetModelTypeState( | 145 bool TypedURLSyncMetadataDatabase::GetModelTypeState( |
140 sync_pb::ModelTypeState* state) { | 146 sync_pb::ModelTypeState* state) { |
141 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); | 147 DCHECK_GT(GetMetaTable().GetVersionNumber(), 0); |
142 std::string serialized_state; | 148 std::string serialized_state; |
143 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { | 149 if (!GetMetaTable().GetValue(kTypedURLModelTypeStateKey, &serialized_state)) { |
144 return true; | 150 return true; |
145 } | 151 } |
146 | 152 |
147 return state->ParseFromString(serialized_state); | 153 return state->ParseFromString(serialized_state); |
148 } | 154 } |
149 | 155 |
150 } // namespace history | 156 } // namespace history |
OLD | NEW |