| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sync/syncable/entry_kernel.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/json/string_escape.h" | |
| 11 #include "base/strings/string_number_conversions.h" | |
| 12 #include "sync/protocol/proto_value_conversions.h" | |
| 13 #include "sync/syncable/syncable_columns.h" | |
| 14 #include "sync/syncable/syncable_enum_conversions.h" | |
| 15 #include "sync/util/cryptographer.h" | |
| 16 | |
| 17 namespace syncer { | |
| 18 namespace syncable { | |
| 19 | |
| 20 EntryKernel::EntryKernel() : dirty_(false) { | |
| 21 // Everything else should already be default-initialized. | |
| 22 for (int i = 0; i < INT64_FIELDS_COUNT; ++i) { | |
| 23 int64_fields[i] = 0; | |
| 24 } | |
| 25 } | |
| 26 | |
| 27 EntryKernel::EntryKernel(const EntryKernel& other) = default; | |
| 28 | |
| 29 EntryKernel::~EntryKernel() {} | |
| 30 | |
| 31 ModelType EntryKernel::GetModelType() const { | |
| 32 ModelType specifics_type = GetModelTypeFromSpecifics(ref(SPECIFICS)); | |
| 33 if (specifics_type != UNSPECIFIED) | |
| 34 return specifics_type; | |
| 35 if (ref(ID).IsRoot()) | |
| 36 return TOP_LEVEL_FOLDER; | |
| 37 // Loose check for server-created top-level folders that aren't | |
| 38 // bound to a particular model type. | |
| 39 if (!ref(UNIQUE_SERVER_TAG).empty() && ref(SERVER_IS_DIR)) | |
| 40 return TOP_LEVEL_FOLDER; | |
| 41 | |
| 42 return UNSPECIFIED; | |
| 43 } | |
| 44 | |
| 45 ModelType EntryKernel::GetServerModelType() const { | |
| 46 ModelType specifics_type = GetModelTypeFromSpecifics(ref(SERVER_SPECIFICS)); | |
| 47 if (specifics_type != UNSPECIFIED) | |
| 48 return specifics_type; | |
| 49 if (ref(ID).IsRoot()) | |
| 50 return TOP_LEVEL_FOLDER; | |
| 51 // Loose check for server-created top-level folders that aren't | |
| 52 // bound to a particular model type. | |
| 53 if (!ref(UNIQUE_SERVER_TAG).empty() && ref(SERVER_IS_DIR)) | |
| 54 return TOP_LEVEL_FOLDER; | |
| 55 | |
| 56 return UNSPECIFIED; | |
| 57 } | |
| 58 | |
| 59 bool EntryKernel::ShouldMaintainPosition() const { | |
| 60 // We maintain positions for all bookmarks, except those that are | |
| 61 // server-created top-level folders. | |
| 62 return TypeSupportsOrdering(GetModelTypeFromSpecifics(ref(SPECIFICS))) && | |
| 63 !(!ref(UNIQUE_SERVER_TAG).empty() && ref(IS_DIR)); | |
| 64 } | |
| 65 | |
| 66 bool EntryKernel::ShouldMaintainHierarchy() const { | |
| 67 // We maintain hierarchy for bookmarks and top-level folders, | |
| 68 // but no other types. Note that the Nigori node consists of a single | |
| 69 // top-level folder, so it's included in this set. | |
| 70 return TypeSupportsHierarchy(GetModelTypeFromSpecifics(ref(SPECIFICS))) || | |
| 71 (!ref(UNIQUE_SERVER_TAG).empty()); | |
| 72 } | |
| 73 | |
| 74 namespace { | |
| 75 | |
| 76 // 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 // | |
| 79 // V should be convertible to Value. | |
| 80 template <class T, class U, class V> | |
| 81 void SetFieldValues(const EntryKernel& kernel, | |
| 82 base::DictionaryValue* dictionary_value, | |
| 83 const char* (*enum_key_fn)(T), | |
| 84 V* (*enum_value_fn)(U), | |
| 85 int field_key_min, int field_key_max) { | |
| 86 DCHECK_LE(field_key_min, field_key_max); | |
| 87 for (int i = field_key_min; i <= field_key_max; ++i) { | |
| 88 T field = static_cast<T>(i); | |
| 89 const std::string& key = enum_key_fn(field); | |
| 90 V* value = enum_value_fn(kernel.ref(field)); | |
| 91 dictionary_value->Set(key, value); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 void SetEncryptableProtoValues( | |
| 96 const EntryKernel& kernel, | |
| 97 Cryptographer* cryptographer, | |
| 98 base::DictionaryValue* dictionary_value, | |
| 99 int field_key_min, int field_key_max) { | |
| 100 DCHECK_LE(field_key_min, field_key_max); | |
| 101 for (int i = field_key_min; i <= field_key_max; ++i) { | |
| 102 ProtoField field = static_cast<ProtoField>(i); | |
| 103 const std::string& key = GetProtoFieldString(field); | |
| 104 | |
| 105 std::unique_ptr<base::DictionaryValue> value; | |
| 106 sync_pb::EntitySpecifics decrypted; | |
| 107 const sync_pb::EncryptedData& encrypted = kernel.ref(field).encrypted(); | |
| 108 if (cryptographer && | |
| 109 kernel.ref(field).has_encrypted() && | |
| 110 cryptographer->CanDecrypt(encrypted) && | |
| 111 cryptographer->Decrypt(encrypted, &decrypted)) { | |
| 112 value = EntitySpecificsToValue(decrypted); | |
| 113 value->SetBoolean("encrypted", true); | |
| 114 } else { | |
| 115 value = EntitySpecificsToValue(kernel.ref(field)); | |
| 116 } | |
| 117 dictionary_value->Set(key, std::move(value)); | |
| 118 } | |
| 119 } | |
| 120 | |
| 121 // Helper functions for SetFieldValues(). | |
| 122 | |
| 123 base::StringValue* Int64ToValue(int64_t i) { | |
| 124 return new base::StringValue(base::Int64ToString(i)); | |
| 125 } | |
| 126 | |
| 127 base::StringValue* TimeToValue(const base::Time& t) { | |
| 128 return new base::StringValue(GetTimeDebugString(t)); | |
| 129 } | |
| 130 | |
| 131 base::StringValue* IdToValue(const Id& id) { | |
| 132 return id.ToValue(); | |
| 133 } | |
| 134 | |
| 135 base::FundamentalValue* BooleanToValue(bool bool_val) { | |
| 136 return new base::FundamentalValue(bool_val); | |
| 137 } | |
| 138 | |
| 139 base::StringValue* StringToValue(const std::string& str) { | |
| 140 return new base::StringValue(str); | |
| 141 } | |
| 142 | |
| 143 base::StringValue* UniquePositionToValue(const UniquePosition& pos) { | |
| 144 return new base::StringValue(pos.ToDebugString()); | |
| 145 } | |
| 146 | |
| 147 base::StringValue* AttachmentMetadataToValue( | |
| 148 const sync_pb::AttachmentMetadata& a) { | |
| 149 return new base::StringValue(a.SerializeAsString()); | |
| 150 } | |
| 151 | |
| 152 } // namespace | |
| 153 | |
| 154 base::DictionaryValue* EntryKernel::ToValue( | |
| 155 Cryptographer* cryptographer) const { | |
| 156 base::DictionaryValue* kernel_info = new base::DictionaryValue(); | |
| 157 kernel_info->SetBoolean("isDirty", is_dirty()); | |
| 158 ModelType dataType = GetServerModelType(); | |
| 159 if (!IsRealDataType(dataType)) | |
| 160 dataType = GetModelType(); | |
| 161 kernel_info->Set("modelType", ModelTypeToValue(dataType)); | |
| 162 | |
| 163 // Int64 fields. | |
| 164 SetFieldValues(*this, kernel_info, | |
| 165 &GetMetahandleFieldString, &Int64ToValue, | |
| 166 INT64_FIELDS_BEGIN, META_HANDLE); | |
| 167 SetFieldValues(*this, kernel_info, | |
| 168 &GetBaseVersionString, &Int64ToValue, | |
| 169 META_HANDLE + 1, BASE_VERSION); | |
| 170 SetFieldValues(*this, kernel_info, | |
| 171 &GetInt64FieldString, &Int64ToValue, | |
| 172 BASE_VERSION + 1, INT64_FIELDS_END - 1); | |
| 173 | |
| 174 // Time fields. | |
| 175 SetFieldValues(*this, kernel_info, | |
| 176 &GetTimeFieldString, &TimeToValue, | |
| 177 TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1); | |
| 178 | |
| 179 // ID fields. | |
| 180 SetFieldValues(*this, kernel_info, | |
| 181 &GetIdFieldString, &IdToValue, | |
| 182 ID_FIELDS_BEGIN, ID_FIELDS_END - 1); | |
| 183 | |
| 184 // Bit fields. | |
| 185 SetFieldValues(*this, kernel_info, | |
| 186 &GetIndexedBitFieldString, &BooleanToValue, | |
| 187 BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1); | |
| 188 SetFieldValues(*this, kernel_info, | |
| 189 &GetIsDelFieldString, &BooleanToValue, | |
| 190 INDEXED_BIT_FIELDS_END, IS_DEL); | |
| 191 SetFieldValues(*this, kernel_info, | |
| 192 &GetBitFieldString, &BooleanToValue, | |
| 193 IS_DEL + 1, BIT_FIELDS_END - 1); | |
| 194 | |
| 195 // String fields. | |
| 196 { | |
| 197 // Pick out the function overload we want. | |
| 198 SetFieldValues(*this, kernel_info, | |
| 199 &GetStringFieldString, &StringToValue, | |
| 200 STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1); | |
| 201 } | |
| 202 | |
| 203 // Proto fields. | |
| 204 SetEncryptableProtoValues(*this, cryptographer, kernel_info, | |
| 205 PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1); | |
| 206 | |
| 207 // UniquePosition fields | |
| 208 SetFieldValues(*this, kernel_info, | |
| 209 &GetUniquePositionFieldString, &UniquePositionToValue, | |
| 210 UNIQUE_POSITION_FIELDS_BEGIN, UNIQUE_POSITION_FIELDS_END - 1); | |
| 211 | |
| 212 // AttachmentMetadata fields | |
| 213 SetFieldValues(*this, | |
| 214 kernel_info, | |
| 215 &GetAttachmentMetadataFieldString, | |
| 216 &AttachmentMetadataToValue, | |
| 217 ATTACHMENT_METADATA_FIELDS_BEGIN, | |
| 218 ATTACHMENT_METADATA_FIELDS_END - 1); | |
| 219 | |
| 220 // Bit temps. | |
| 221 SetFieldValues(*this, kernel_info, | |
| 222 &GetBitTempString, &BooleanToValue, | |
| 223 BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1); | |
| 224 | |
| 225 return kernel_info; | |
| 226 } | |
| 227 | |
| 228 base::ListValue* EntryKernelMutationMapToValue( | |
| 229 const EntryKernelMutationMap& mutations) { | |
| 230 base::ListValue* list = new base::ListValue(); | |
| 231 for (EntryKernelMutationMap::const_iterator it = mutations.begin(); | |
| 232 it != mutations.end(); ++it) { | |
| 233 list->Append(EntryKernelMutationToValue(it->second)); | |
| 234 } | |
| 235 return list; | |
| 236 } | |
| 237 | |
| 238 base::DictionaryValue* EntryKernelMutationToValue( | |
| 239 const EntryKernelMutation& mutation) { | |
| 240 base::DictionaryValue* dict = new base::DictionaryValue(); | |
| 241 dict->Set("original", mutation.original.ToValue(NULL)); | |
| 242 dict->Set("mutated", mutation.mutated.ToValue(NULL)); | |
| 243 return dict; | |
| 244 } | |
| 245 | |
| 246 std::ostream& operator<<(std::ostream& os, const EntryKernel& entry_kernel) { | |
| 247 int i; | |
| 248 EntryKernel* const kernel = const_cast<EntryKernel*>(&entry_kernel); | |
| 249 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | |
| 250 os << g_metas_columns[i].name << ": " | |
| 251 << kernel->ref(static_cast<Int64Field>(i)) << ", "; | |
| 252 } | |
| 253 for (; i < TIME_FIELDS_END; ++i) { | |
| 254 os << g_metas_columns[i].name << ": " | |
| 255 << GetTimeDebugString(kernel->ref(static_cast<TimeField>(i))) << ", "; | |
| 256 } | |
| 257 for (; i < ID_FIELDS_END; ++i) { | |
| 258 os << g_metas_columns[i].name << ": " | |
| 259 << kernel->ref(static_cast<IdField>(i)) << ", "; | |
| 260 } | |
| 261 os << "Flags: "; | |
| 262 for (; i < BIT_FIELDS_END; ++i) { | |
| 263 if (kernel->ref(static_cast<BitField>(i))) | |
| 264 os << g_metas_columns[i].name << ", "; | |
| 265 } | |
| 266 for (; i < STRING_FIELDS_END; ++i) { | |
| 267 const std::string& field = kernel->ref(static_cast<StringField>(i)); | |
| 268 os << g_metas_columns[i].name << ": " << field << ", "; | |
| 269 } | |
| 270 for (; i < PROTO_FIELDS_END; ++i) { | |
| 271 std::string escaped_str = base::EscapeBytesAsInvalidJSONString( | |
| 272 kernel->ref(static_cast<ProtoField>(i)).SerializeAsString(), false); | |
| 273 os << g_metas_columns[i].name << ": " << escaped_str << ", "; | |
| 274 } | |
| 275 for (; i < UNIQUE_POSITION_FIELDS_END; ++i) { | |
| 276 os << g_metas_columns[i].name << ": " | |
| 277 << kernel->ref(static_cast<UniquePositionField>(i)).ToDebugString() | |
| 278 << ", "; | |
| 279 } | |
| 280 for (; i < ATTACHMENT_METADATA_FIELDS_END; ++i) { | |
| 281 std::string escaped_str = base::EscapeBytesAsInvalidJSONString( | |
| 282 kernel->ref(static_cast<AttachmentMetadataField>(i)) | |
| 283 .SerializeAsString(), | |
| 284 false); | |
| 285 os << g_metas_columns[i].name << ": " << escaped_str << ", "; | |
| 286 } | |
| 287 os << "TempFlags: "; | |
| 288 for (; i < BIT_TEMPS_END; ++i) { | |
| 289 if (kernel->ref(static_cast<BitTemp>(i))) | |
| 290 os << "#" << i - BIT_TEMPS_BEGIN << ", "; | |
| 291 } | |
| 292 return os; | |
| 293 } | |
| 294 | |
| 295 } // namespace syncable | |
| 296 } // namespace syncer | |
| OLD | NEW |