| 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 "components/sync/syncable/entry_kernel.h" | 5 #include "components/sync/syncable/entry_kernel.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/trace_event/memory_usage_estimator.h" | 12 #include "base/trace_event/memory_usage_estimator.h" |
| 12 #include "components/sync/base/cryptographer.h" | 13 #include "components/sync/base/cryptographer.h" |
| 13 #include "components/sync/protocol/proto_memory_estimations.h" | 14 #include "components/sync/protocol/proto_memory_estimations.h" |
| 14 #include "components/sync/protocol/proto_value_conversions.h" | 15 #include "components/sync/protocol/proto_value_conversions.h" |
| 15 #include "components/sync/syncable/syncable_columns.h" | 16 #include "components/sync/syncable/syncable_columns.h" |
| 16 #include "components/sync/syncable/syncable_enum_conversions.h" | 17 #include "components/sync/syncable/syncable_enum_conversions.h" |
| 17 | 18 |
| 18 namespace syncer { | 19 namespace syncer { |
| 19 namespace syncable { | 20 namespace syncable { |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 namespace { | 76 namespace { |
| 76 | 77 |
| 77 // Utility function to loop through a set of enum values and add the | 78 // Utility function to loop through a set of enum values and add the |
| 78 // field keys/values in the kernel to the given dictionary. | 79 // field keys/values in the kernel to the given dictionary. |
| 79 // | 80 // |
| 80 // V should be convertible to Value. | 81 // V should be convertible to Value. |
| 81 template <class T, class U, class V> | 82 template <class T, class U, class V> |
| 82 void SetFieldValues(const EntryKernel& kernel, | 83 void SetFieldValues(const EntryKernel& kernel, |
| 83 base::DictionaryValue* dictionary_value, | 84 base::DictionaryValue* dictionary_value, |
| 84 const char* (*enum_key_fn)(T), | 85 const char* (*enum_key_fn)(T), |
| 85 V* (*enum_value_fn)(U), | 86 std::unique_ptr<V> (*enum_value_fn)(U), |
| 86 int field_key_min, | 87 int field_key_min, |
| 87 int field_key_max) { | 88 int field_key_max) { |
| 88 DCHECK_LE(field_key_min, field_key_max); | 89 DCHECK_LE(field_key_min, field_key_max); |
| 89 for (int i = field_key_min; i <= field_key_max; ++i) { | 90 for (int i = field_key_min; i <= field_key_max; ++i) { |
| 90 T field = static_cast<T>(i); | 91 T field = static_cast<T>(i); |
| 91 const std::string& key = enum_key_fn(field); | 92 const std::string& key = enum_key_fn(field); |
| 92 V* value = enum_value_fn(kernel.ref(field)); | 93 std::unique_ptr<V> value = enum_value_fn(kernel.ref(field)); |
| 93 dictionary_value->Set(key, value); | 94 dictionary_value->Set(key, std::move(value)); |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 | 97 |
| 97 void SetEncryptableProtoValues(const EntryKernel& kernel, | 98 void SetEncryptableProtoValues(const EntryKernel& kernel, |
| 98 Cryptographer* cryptographer, | 99 Cryptographer* cryptographer, |
| 99 base::DictionaryValue* dictionary_value, | 100 base::DictionaryValue* dictionary_value, |
| 100 int field_key_min, | 101 int field_key_min, |
| 101 int field_key_max) { | 102 int field_key_max) { |
| 102 DCHECK_LE(field_key_min, field_key_max); | 103 DCHECK_LE(field_key_min, field_key_max); |
| 103 for (int i = field_key_min; i <= field_key_max; ++i) { | 104 for (int i = field_key_min; i <= field_key_max; ++i) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 value->SetBoolean("encrypted", true); | 115 value->SetBoolean("encrypted", true); |
| 115 } else { | 116 } else { |
| 116 value = EntitySpecificsToValue(kernel.ref(field)); | 117 value = EntitySpecificsToValue(kernel.ref(field)); |
| 117 } | 118 } |
| 118 dictionary_value->Set(key, std::move(value)); | 119 dictionary_value->Set(key, std::move(value)); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Helper functions for SetFieldValues(). | 123 // Helper functions for SetFieldValues(). |
| 123 | 124 |
| 124 base::Value* Int64ToValue(int64_t i) { | 125 std::unique_ptr<base::Value> Int64ToValue(int64_t i) { |
| 125 return new base::Value(base::Int64ToString(i)); | 126 return base::MakeUnique<base::Value>(base::Int64ToString(i)); |
| 126 } | 127 } |
| 127 | 128 |
| 128 base::Value* TimeToValue(const base::Time& t) { | 129 std::unique_ptr<base::Value> TimeToValue(const base::Time& t) { |
| 129 return new base::Value(GetTimeDebugString(t)); | 130 return base::MakeUnique<base::Value>(GetTimeDebugString(t)); |
| 130 } | 131 } |
| 131 | 132 |
| 132 base::Value* IdToValue(const Id& id) { | 133 std::unique_ptr<base::Value> IdToValue(const Id& id) { |
| 133 return id.ToValue(); | 134 return id.ToValue(); |
| 134 } | 135 } |
| 135 | 136 |
| 136 base::Value* BooleanToValue(bool bool_val) { | 137 std::unique_ptr<base::Value> BooleanToValue(bool bool_val) { |
| 137 return new base::Value(bool_val); | 138 return base::MakeUnique<base::Value>(bool_val); |
| 138 } | 139 } |
| 139 | 140 |
| 140 base::Value* StringToValue(const std::string& str) { | 141 std::unique_ptr<base::Value> StringToValue(const std::string& str) { |
| 141 return new base::Value(str); | 142 return base::MakeUnique<base::Value>(str); |
| 142 } | 143 } |
| 143 | 144 |
| 144 base::Value* UniquePositionToValue(const UniquePosition& pos) { | 145 std::unique_ptr<base::Value> UniquePositionToValue(const UniquePosition& pos) { |
| 145 return new base::Value(pos.ToDebugString()); | 146 return base::MakeUnique<base::Value>(pos.ToDebugString()); |
| 146 } | 147 } |
| 147 | 148 |
| 148 base::Value* AttachmentMetadataToValue(const sync_pb::AttachmentMetadata& a) { | 149 std::unique_ptr<base::Value> AttachmentMetadataToValue( |
| 149 return new base::Value(a.SerializeAsString()); | 150 const sync_pb::AttachmentMetadata& a) { |
| 151 return base::MakeUnique<base::Value>(a.SerializeAsString()); |
| 150 } | 152 } |
| 151 | 153 |
| 152 // Estimates memory usage of ProtoValuePtr<T> arrays where consecutive | 154 // Estimates memory usage of ProtoValuePtr<T> arrays where consecutive |
| 153 // elements can share the same value. | 155 // elements can share the same value. |
| 154 template <class T, size_t N> | 156 template <class T, size_t N> |
| 155 size_t EstimateSharedMemoryUsage(ProtoValuePtr<T> const (&ptrs)[N]) { | 157 size_t EstimateSharedMemoryUsage(ProtoValuePtr<T> const (&ptrs)[N]) { |
| 156 size_t memory_usage = 0; | 158 size_t memory_usage = 0; |
| 157 const T* last_value = nullptr; | 159 const T* last_value = nullptr; |
| 158 for (const auto& ptr : ptrs) { | 160 for (const auto& ptr : ptrs) { |
| 159 if (last_value != &ptr.value()) { | 161 if (last_value != &ptr.value()) { |
| 160 memory_usage += EstimateMemoryUsage(ptr); | 162 memory_usage += EstimateMemoryUsage(ptr); |
| 161 last_value = &ptr.value(); | 163 last_value = &ptr.value(); |
| 162 } | 164 } |
| 163 } | 165 } |
| 164 return memory_usage; | 166 return memory_usage; |
| 165 } | 167 } |
| 166 | 168 |
| 167 } // namespace | 169 } // namespace |
| 168 | 170 |
| 169 base::DictionaryValue* EntryKernel::ToValue( | 171 std::unique_ptr<base::DictionaryValue> EntryKernel::ToValue( |
| 170 Cryptographer* cryptographer) const { | 172 Cryptographer* cryptographer) const { |
| 171 base::DictionaryValue* kernel_info = new base::DictionaryValue(); | 173 auto kernel_info = base::MakeUnique<base::DictionaryValue>(); |
| 172 kernel_info->SetBoolean("isDirty", is_dirty()); | 174 kernel_info->SetBoolean("isDirty", is_dirty()); |
| 173 ModelType dataType = GetServerModelType(); | 175 ModelType dataType = GetServerModelType(); |
| 174 if (!IsRealDataType(dataType)) | 176 if (!IsRealDataType(dataType)) |
| 175 dataType = GetModelType(); | 177 dataType = GetModelType(); |
| 176 kernel_info->Set("modelType", ModelTypeToValue(dataType)); | 178 kernel_info->Set("modelType", ModelTypeToValue(dataType)); |
| 177 | 179 |
| 178 // Int64 fields. | 180 // Int64 fields. |
| 179 SetFieldValues(*this, kernel_info, &GetMetahandleFieldString, &Int64ToValue, | 181 SetFieldValues(*this, kernel_info.get(), &GetMetahandleFieldString, |
| 180 INT64_FIELDS_BEGIN, META_HANDLE); | 182 &Int64ToValue, INT64_FIELDS_BEGIN, META_HANDLE); |
| 181 SetFieldValues(*this, kernel_info, &GetBaseVersionString, &Int64ToValue, | 183 SetFieldValues(*this, kernel_info.get(), &GetBaseVersionString, &Int64ToValue, |
| 182 META_HANDLE + 1, BASE_VERSION); | 184 META_HANDLE + 1, BASE_VERSION); |
| 183 SetFieldValues(*this, kernel_info, &GetInt64FieldString, &Int64ToValue, | 185 SetFieldValues(*this, kernel_info.get(), &GetInt64FieldString, &Int64ToValue, |
| 184 BASE_VERSION + 1, INT64_FIELDS_END - 1); | 186 BASE_VERSION + 1, INT64_FIELDS_END - 1); |
| 185 | 187 |
| 186 // Time fields. | 188 // Time fields. |
| 187 SetFieldValues(*this, kernel_info, &GetTimeFieldString, &TimeToValue, | 189 SetFieldValues(*this, kernel_info.get(), &GetTimeFieldString, &TimeToValue, |
| 188 TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1); | 190 TIME_FIELDS_BEGIN, TIME_FIELDS_END - 1); |
| 189 | 191 |
| 190 // ID fields. | 192 // ID fields. |
| 191 SetFieldValues(*this, kernel_info, &GetIdFieldString, &IdToValue, | 193 SetFieldValues(*this, kernel_info.get(), &GetIdFieldString, &IdToValue, |
| 192 ID_FIELDS_BEGIN, ID_FIELDS_END - 1); | 194 ID_FIELDS_BEGIN, ID_FIELDS_END - 1); |
| 193 | 195 |
| 194 // Bit fields. | 196 // Bit fields. |
| 195 SetFieldValues(*this, kernel_info, &GetIndexedBitFieldString, &BooleanToValue, | 197 SetFieldValues(*this, kernel_info.get(), &GetIndexedBitFieldString, |
| 196 BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1); | 198 &BooleanToValue, BIT_FIELDS_BEGIN, INDEXED_BIT_FIELDS_END - 1); |
| 197 SetFieldValues(*this, kernel_info, &GetIsDelFieldString, &BooleanToValue, | 199 SetFieldValues(*this, kernel_info.get(), &GetIsDelFieldString, |
| 198 INDEXED_BIT_FIELDS_END, IS_DEL); | 200 &BooleanToValue, INDEXED_BIT_FIELDS_END, IS_DEL); |
| 199 SetFieldValues(*this, kernel_info, &GetBitFieldString, &BooleanToValue, | 201 SetFieldValues(*this, kernel_info.get(), &GetBitFieldString, &BooleanToValue, |
| 200 IS_DEL + 1, BIT_FIELDS_END - 1); | 202 IS_DEL + 1, BIT_FIELDS_END - 1); |
| 201 | 203 |
| 202 // String fields. | 204 // String fields. |
| 203 { | 205 { |
| 204 // Pick out the function overload we want. | 206 // Pick out the function overload we want. |
| 205 SetFieldValues(*this, kernel_info, &GetStringFieldString, &StringToValue, | 207 SetFieldValues(*this, kernel_info.get(), &GetStringFieldString, |
| 206 STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1); | 208 &StringToValue, STRING_FIELDS_BEGIN, STRING_FIELDS_END - 1); |
| 207 } | 209 } |
| 208 | 210 |
| 209 // Proto fields. | 211 // Proto fields. |
| 210 SetEncryptableProtoValues(*this, cryptographer, kernel_info, | 212 SetEncryptableProtoValues(*this, cryptographer, kernel_info.get(), |
| 211 PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1); | 213 PROTO_FIELDS_BEGIN, PROTO_FIELDS_END - 1); |
| 212 | 214 |
| 213 // UniquePosition fields | 215 // UniquePosition fields |
| 214 SetFieldValues(*this, kernel_info, &GetUniquePositionFieldString, | 216 SetFieldValues(*this, kernel_info.get(), &GetUniquePositionFieldString, |
| 215 &UniquePositionToValue, UNIQUE_POSITION_FIELDS_BEGIN, | 217 &UniquePositionToValue, UNIQUE_POSITION_FIELDS_BEGIN, |
| 216 UNIQUE_POSITION_FIELDS_END - 1); | 218 UNIQUE_POSITION_FIELDS_END - 1); |
| 217 | 219 |
| 218 // AttachmentMetadata fields | 220 // AttachmentMetadata fields |
| 219 SetFieldValues(*this, kernel_info, &GetAttachmentMetadataFieldString, | 221 SetFieldValues(*this, kernel_info.get(), &GetAttachmentMetadataFieldString, |
| 220 &AttachmentMetadataToValue, ATTACHMENT_METADATA_FIELDS_BEGIN, | 222 &AttachmentMetadataToValue, ATTACHMENT_METADATA_FIELDS_BEGIN, |
| 221 ATTACHMENT_METADATA_FIELDS_END - 1); | 223 ATTACHMENT_METADATA_FIELDS_END - 1); |
| 222 | 224 |
| 223 // Bit temps. | 225 // Bit temps. |
| 224 SetFieldValues(*this, kernel_info, &GetBitTempString, &BooleanToValue, | 226 SetFieldValues(*this, kernel_info.get(), &GetBitTempString, &BooleanToValue, |
| 225 BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1); | 227 BIT_TEMPS_BEGIN, BIT_TEMPS_END - 1); |
| 226 | 228 |
| 227 return kernel_info; | 229 return kernel_info; |
| 228 } | 230 } |
| 229 | 231 |
| 230 size_t EntryKernel::EstimateMemoryUsage() const { | 232 size_t EntryKernel::EstimateMemoryUsage() const { |
| 231 if (memory_usage_ == kMemoryUsageUnknown) { | 233 if (memory_usage_ == kMemoryUsageUnknown) { |
| 232 using base::trace_event::EstimateMemoryUsage; | 234 using base::trace_event::EstimateMemoryUsage; |
| 233 memory_usage_ = EstimateMemoryUsage(string_fields) + | 235 memory_usage_ = EstimateMemoryUsage(string_fields) + |
| 234 EstimateSharedMemoryUsage(specifics_fields) + | 236 EstimateSharedMemoryUsage(specifics_fields) + |
| 235 EstimateMemoryUsage(id_fields) + | 237 EstimateMemoryUsage(id_fields) + |
| 236 EstimateMemoryUsage(unique_position_fields) + | 238 EstimateMemoryUsage(unique_position_fields) + |
| 237 EstimateSharedMemoryUsage(attachment_metadata_fields); | 239 EstimateSharedMemoryUsage(attachment_metadata_fields); |
| 238 } | 240 } |
| 239 return memory_usage_; | 241 return memory_usage_; |
| 240 } | 242 } |
| 241 | 243 |
| 242 std::unique_ptr<base::ListValue> EntryKernelMutationMapToValue( | 244 std::unique_ptr<base::ListValue> EntryKernelMutationMapToValue( |
| 243 const EntryKernelMutationMap& mutations) { | 245 const EntryKernelMutationMap& mutations) { |
| 244 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 246 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 245 for (EntryKernelMutationMap::const_iterator it = mutations.begin(); | 247 for (EntryKernelMutationMap::const_iterator it = mutations.begin(); |
| 246 it != mutations.end(); ++it) { | 248 it != mutations.end(); ++it) { |
| 247 list->Append(EntryKernelMutationToValue(it->second)); | 249 list->Append(EntryKernelMutationToValue(it->second)); |
| 248 } | 250 } |
| 249 return list; | 251 return list; |
| 250 } | 252 } |
| 251 | 253 |
| 252 std::unique_ptr<base::DictionaryValue> EntryKernelMutationToValue( | 254 std::unique_ptr<base::DictionaryValue> EntryKernelMutationToValue( |
| 253 const EntryKernelMutation& mutation) { | 255 const EntryKernelMutation& mutation) { |
| 254 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 256 auto dict = base::MakeUnique<base::DictionaryValue>(); |
| 255 dict->Set("original", mutation.original.ToValue(nullptr)); | 257 dict->Set("original", mutation.original.ToValue(nullptr)); |
| 256 dict->Set("mutated", mutation.mutated.ToValue(nullptr)); | 258 dict->Set("mutated", mutation.mutated.ToValue(nullptr)); |
| 257 return dict; | 259 return dict; |
| 258 } | 260 } |
| 259 | 261 |
| 260 std::ostream& operator<<(std::ostream& os, const EntryKernel& entry_kernel) { | 262 std::ostream& operator<<(std::ostream& os, const EntryKernel& entry_kernel) { |
| 261 int i; | 263 int i; |
| 262 EntryKernel* const kernel = const_cast<EntryKernel*>(&entry_kernel); | 264 EntryKernel* const kernel = const_cast<EntryKernel*>(&entry_kernel); |
| 263 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { | 265 for (i = BEGIN_FIELDS; i < INT64_FIELDS_END; ++i) { |
| 264 os << g_metas_columns[i].name << ": " | 266 os << g_metas_columns[i].name << ": " |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 301 os << "TempFlags: "; | 303 os << "TempFlags: "; |
| 302 for (; i < BIT_TEMPS_END; ++i) { | 304 for (; i < BIT_TEMPS_END; ++i) { |
| 303 if (kernel->ref(static_cast<BitTemp>(i))) | 305 if (kernel->ref(static_cast<BitTemp>(i))) |
| 304 os << "#" << i - BIT_TEMPS_BEGIN << ", "; | 306 os << "#" << i - BIT_TEMPS_BEGIN << ", "; |
| 305 } | 307 } |
| 306 return os; | 308 return os; |
| 307 } | 309 } |
| 308 | 310 |
| 309 } // namespace syncable | 311 } // namespace syncable |
| 310 } // namespace syncer | 312 } // namespace syncer |
| OLD | NEW |