| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/syncable/entry_kernel.h" | 5 #include "components/sync/syncable/entry_kernel.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/json/string_escape.h" | 11 #include "base/json/string_escape.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "sync/protocol/proto_value_conversions.h" | 13 #include "components/sync/base/cryptographer.h" |
| 13 #include "sync/syncable/syncable_columns.h" | 14 #include "components/sync/protocol/proto_value_conversions.h" |
| 14 #include "sync/syncable/syncable_enum_conversions.h" | 15 #include "components/sync/syncable/syncable_columns.h" |
| 15 #include "sync/util/cryptographer.h" | 16 #include "components/sync/syncable/syncable_enum_conversions.h" |
| 16 | 17 |
| 17 namespace syncer { | 18 namespace syncer { |
| 18 namespace syncable { | 19 namespace syncable { |
| 19 | 20 |
| 20 EntryKernel::EntryKernel() : dirty_(false) { | 21 EntryKernel::EntryKernel() : dirty_(false) { |
| 21 // Everything else should already be default-initialized. | 22 // Everything else should already be default-initialized. |
| 22 for (int i = 0; i < INT64_FIELDS_COUNT; ++i) { | 23 for (int i = 0; i < INT64_FIELDS_COUNT; ++i) { |
| 23 int64_fields[i] = 0; | 24 int64_fields[i] = 0; |
| 24 } | 25 } |
| 25 } | 26 } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 | 76 |
| 76 // Utility function to loop through a set of enum values and add the | 77 // Utility function to loop through a set of enum values and add the |
| 77 // field keys/values in the kernel to the given dictionary. | 78 // field keys/values in the kernel to the given dictionary. |
| 78 // | 79 // |
| 79 // V should be convertible to Value. | 80 // V should be convertible to Value. |
| 80 template <class T, class U, class V> | 81 template <class T, class U, class V> |
| 81 void SetFieldValues(const EntryKernel& kernel, | 82 void SetFieldValues(const EntryKernel& kernel, |
| 82 base::DictionaryValue* dictionary_value, | 83 base::DictionaryValue* dictionary_value, |
| 83 const char* (*enum_key_fn)(T), | 84 const char* (*enum_key_fn)(T), |
| 84 V* (*enum_value_fn)(U), | 85 V* (*enum_value_fn)(U), |
| 85 int field_key_min, int field_key_max) { | 86 int field_key_min, |
| 87 int field_key_max) { |
| 86 DCHECK_LE(field_key_min, field_key_max); | 88 DCHECK_LE(field_key_min, field_key_max); |
| 87 for (int i = field_key_min; i <= field_key_max; ++i) { | 89 for (int i = field_key_min; i <= field_key_max; ++i) { |
| 88 T field = static_cast<T>(i); | 90 T field = static_cast<T>(i); |
| 89 const std::string& key = enum_key_fn(field); | 91 const std::string& key = enum_key_fn(field); |
| 90 V* value = enum_value_fn(kernel.ref(field)); | 92 V* value = enum_value_fn(kernel.ref(field)); |
| 91 dictionary_value->Set(key, value); | 93 dictionary_value->Set(key, value); |
| 92 } | 94 } |
| 93 } | 95 } |
| 94 | 96 |
| 95 void SetEncryptableProtoValues( | 97 void SetEncryptableProtoValues(const EntryKernel& kernel, |
| 96 const EntryKernel& kernel, | 98 Cryptographer* cryptographer, |
| 97 Cryptographer* cryptographer, | 99 base::DictionaryValue* dictionary_value, |
| 98 base::DictionaryValue* dictionary_value, | 100 int field_key_min, |
| 99 int field_key_min, int field_key_max) { | 101 int field_key_max) { |
| 100 DCHECK_LE(field_key_min, field_key_max); | 102 DCHECK_LE(field_key_min, field_key_max); |
| 101 for (int i = field_key_min; i <= field_key_max; ++i) { | 103 for (int i = field_key_min; i <= field_key_max; ++i) { |
| 102 ProtoField field = static_cast<ProtoField>(i); | 104 ProtoField field = static_cast<ProtoField>(i); |
| 103 const std::string& key = GetProtoFieldString(field); | 105 const std::string& key = GetProtoFieldString(field); |
| 104 | 106 |
| 105 std::unique_ptr<base::DictionaryValue> value; | 107 std::unique_ptr<base::DictionaryValue> value; |
| 106 sync_pb::EntitySpecifics decrypted; | 108 sync_pb::EntitySpecifics decrypted; |
| 107 const sync_pb::EncryptedData& encrypted = kernel.ref(field).encrypted(); | 109 const sync_pb::EncryptedData& encrypted = kernel.ref(field).encrypted(); |
| 108 if (cryptographer && | 110 if (cryptographer && kernel.ref(field).has_encrypted() && |
| 109 kernel.ref(field).has_encrypted() && | |
| 110 cryptographer->CanDecrypt(encrypted) && | 111 cryptographer->CanDecrypt(encrypted) && |
| 111 cryptographer->Decrypt(encrypted, &decrypted)) { | 112 cryptographer->Decrypt(encrypted, &decrypted)) { |
| 112 value = EntitySpecificsToValue(decrypted); | 113 value = EntitySpecificsToValue(decrypted); |
| 113 value->SetBoolean("encrypted", true); | 114 value->SetBoolean("encrypted", true); |
| 114 } else { | 115 } else { |
| 115 value = EntitySpecificsToValue(kernel.ref(field)); | 116 value = EntitySpecificsToValue(kernel.ref(field)); |
| 116 } | 117 } |
| 117 dictionary_value->Set(key, std::move(value)); | 118 dictionary_value->Set(key, std::move(value)); |
| 118 } | 119 } |
| 119 } | 120 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 base::DictionaryValue* EntryKernel::ToValue( | 155 base::DictionaryValue* EntryKernel::ToValue( |
| 155 Cryptographer* cryptographer) const { | 156 Cryptographer* cryptographer) const { |
| 156 base::DictionaryValue* kernel_info = new base::DictionaryValue(); | 157 base::DictionaryValue* kernel_info = new base::DictionaryValue(); |
| 157 kernel_info->SetBoolean("isDirty", is_dirty()); | 158 kernel_info->SetBoolean("isDirty", is_dirty()); |
| 158 ModelType dataType = GetServerModelType(); | 159 ModelType dataType = GetServerModelType(); |
| 159 if (!IsRealDataType(dataType)) | 160 if (!IsRealDataType(dataType)) |
| 160 dataType = GetModelType(); | 161 dataType = GetModelType(); |
| 161 kernel_info->Set("modelType", ModelTypeToValue(dataType)); | 162 kernel_info->Set("modelType", ModelTypeToValue(dataType)); |
| 162 | 163 |
| 163 // Int64 fields. | 164 // Int64 fields. |
| 164 SetFieldValues(*this, kernel_info, | 165 SetFieldValues(*this, kernel_info, &GetMetahandleFieldString, &Int64ToValue, |
| 165 &GetMetahandleFieldString, &Int64ToValue, | |
| 166 INT64_FIELDS_BEGIN, META_HANDLE); | 166 INT64_FIELDS_BEGIN, META_HANDLE); |
| 167 SetFieldValues(*this, kernel_info, | 167 SetFieldValues(*this, kernel_info, &GetBaseVersionString, &Int64ToValue, |
| 168 &GetBaseVersionString, &Int64ToValue, | |
| 169 META_HANDLE + 1, BASE_VERSION); | 168 META_HANDLE + 1, BASE_VERSION); |
| 170 SetFieldValues(*this, kernel_info, | 169 SetFieldValues(*this, kernel_info, &GetInt64FieldString, &Int64ToValue, |
| 171 &GetInt64FieldString, &Int64ToValue, | |
| 172 BASE_VERSION + 1, INT64_FIELDS_END - 1); | 170 BASE_VERSION + 1, INT64_FIELDS_END - 1); |
| 173 | 171 |
| 174 // Time fields. | 172 // Time fields. |
| 175 SetFieldValues(*this, kernel_info, | 173 SetFieldValues(*this, kernel_info, &GetTimeFieldString, &TimeToValue, |
| 176 &GetTimeFieldString, &TimeToValue, | |
| 177 TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1); | 174 TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1); |
| 178 | 175 |
| 179 // ID fields. | 176 // ID fields. |
| 180 SetFieldValues(*this, kernel_info, | 177 SetFieldValues(*this, kernel_info, &GetIdFieldString, &IdToValue, |
| 181 &GetIdFieldString, &IdToValue, | |
| 182 ID_FIELDS_BEGIN, ID_FIELDS_END - 1); | 178 ID_FIELDS_BEGIN, ID_FIELDS_END - 1); |
| 183 | 179 |
| 184 // Bit fields. | 180 // Bit fields. |
| 185 SetFieldValues(*this, kernel_info, | 181 SetFieldValues(*this, kernel_info, &GetIndexedBitFieldString, &BooleanToValue, |
| 186 &GetIndexedBitFieldString, &BooleanToValue, | |
| 187 BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1); | 182 BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1); |
| 188 SetFieldValues(*this, kernel_info, | 183 SetFieldValues(*this, kernel_info, &GetIsDelFieldString, &BooleanToValue, |
| 189 &GetIsDelFieldString, &BooleanToValue, | |
| 190 INDEXED_BIT_FIELDS_END, IS_DEL); | 184 INDEXED_BIT_FIELDS_END, IS_DEL); |
| 191 SetFieldValues(*this, kernel_info, | 185 SetFieldValues(*this, kernel_info, &GetBitFieldString, &BooleanToValue, |
| 192 &GetBitFieldString, &BooleanToValue, | |
| 193 IS_DEL + 1, BIT_FIELDS_END - 1); | 186 IS_DEL + 1, BIT_FIELDS_END - 1); |
| 194 | 187 |
| 195 // String fields. | 188 // String fields. |
| 196 { | 189 { |
| 197 // Pick out the function overload we want. | 190 // Pick out the function overload we want. |
| 198 SetFieldValues(*this, kernel_info, | 191 SetFieldValues(*this, kernel_info, &GetStringFieldString, &StringToValue, |
| 199 &GetStringFieldString, &StringToValue, | |
| 200 STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1); | 192 STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1); |
| 201 } | 193 } |
| 202 | 194 |
| 203 // Proto fields. | 195 // Proto fields. |
| 204 SetEncryptableProtoValues(*this, cryptographer, kernel_info, | 196 SetEncryptableProtoValues(*this, cryptographer, kernel_info, |
| 205 PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1); | 197 PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1); |
| 206 | 198 |
| 207 // UniquePosition fields | 199 // UniquePosition fields |
| 208 SetFieldValues(*this, kernel_info, | 200 SetFieldValues(*this, kernel_info, &GetUniquePositionFieldString, |
| 209 &GetUniquePositionFieldString, &UniquePositionToValue, | 201 &UniquePositionToValue, UNIQUE_POSITION_FIELDS_BEGIN, |
| 210 UNIQUE_POSITION_FIELDS_BEGIN, UNIQUE_POSITION_FIELDS_END - 1); | 202 UNIQUE_POSITION_FIELDS_END - 1); |
| 211 | 203 |
| 212 // AttachmentMetadata fields | 204 // AttachmentMetadata fields |
| 213 SetFieldValues(*this, | 205 SetFieldValues(*this, kernel_info, &GetAttachmentMetadataFieldString, |
| 214 kernel_info, | 206 &AttachmentMetadataToValue, ATTACHMENT_METADATA_FIELDS_BEGIN, |
| 215 &GetAttachmentMetadataFieldString, | |
| 216 &AttachmentMetadataToValue, | |
| 217 ATTACHMENT_METADATA_FIELDS_BEGIN, | |
| 218 ATTACHMENT_METADATA_FIELDS_END - 1); | 207 ATTACHMENT_METADATA_FIELDS_END - 1); |
| 219 | 208 |
| 220 // Bit temps. | 209 // Bit temps. |
| 221 SetFieldValues(*this, kernel_info, | 210 SetFieldValues(*this, kernel_info, &GetBitTempString, &BooleanToValue, |
| 222 &GetBitTempString, &BooleanToValue, | |
| 223 BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1); | 211 BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1); |
| 224 | 212 |
| 225 return kernel_info; | 213 return kernel_info; |
| 226 } | 214 } |
| 227 | 215 |
| 228 base::ListValue* EntryKernelMutationMapToValue( | 216 base::ListValue* EntryKernelMutationMapToValue( |
| 229 const EntryKernelMutationMap& mutations) { | 217 const EntryKernelMutationMap& mutations) { |
| 230 base::ListValue* list = new base::ListValue(); | 218 base::ListValue* list = new base::ListValue(); |
| 231 for (EntryKernelMutationMap::const_iterator it = mutations.begin(); | 219 for (EntryKernelMutationMap::const_iterator it = mutations.begin(); |
| 232 it != mutations.end(); ++it) { | 220 it != mutations.end(); ++it) { |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 287 os << "TempFlags: "; | 275 os << "TempFlags: "; |
| 288 for (; i < BIT_TEMPS_END; ++i) { | 276 for (; i < BIT_TEMPS_END; ++i) { |
| 289 if (kernel->ref(static_cast<BitTemp>(i))) | 277 if (kernel->ref(static_cast<BitTemp>(i))) |
| 290 os << "#" << i - BIT_TEMPS_BEGIN << ", "; | 278 os << "#" << i - BIT_TEMPS_BEGIN << ", "; |
| 291 } | 279 } |
| 292 return os; | 280 return os; |
| 293 } | 281 } |
| 294 | 282 |
| 295 } // namespace syncable | 283 } // namespace syncable |
| 296 } // namespace syncer | 284 } // namespace syncer |
| OLD | NEW |