Chromium Code Reviews| 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/api/sync_data.h" | 5 #include "sync/api/sync_data.h" |
| 6 | 6 |
| 7 #include <ostream> | 7 #include <ostream> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "sync/internal_api/public/base/model_type.h" | 13 #include "sync/internal_api/public/base/model_type.h" |
| 14 #include "sync/internal_api/public/base_node.h" | 14 #include "sync/internal_api/public/base_node.h" |
| 15 #include "sync/protocol/proto_value_conversions.h" | 15 #include "sync/protocol/proto_value_conversions.h" |
| 16 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
| 17 | 17 |
| 18 namespace syncer { | 18 namespace syncer { |
| 19 | 19 |
| 20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( | 20 struct SyncDataCore { |
| 21 Wrapper* wrapper) { | 21 SyncDataCore(); |
| 22 *wrapper = new sync_pb::SyncEntity(); | 22 SyncDataCore(ModelType datatype, |
| 23 int64 id, | |
| 24 base::Time remote_modification_time, | |
| 25 const std::string& context, | |
| 26 const sync_pb::SyncEntity& entity); | |
| 27 ~SyncDataCore(); | |
| 28 | |
| 29 // The datatype associated with this sync data. | |
| 30 ModelType datatype; | |
| 31 | |
| 32 // Equal to kInvalidId iff this is local. | |
| 33 int64 id; | |
| 34 | |
| 35 // This is only valid if IsLocal() is false, and may be null if the | |
| 36 // SyncData represents a deleted item. | |
| 37 base::Time remote_modification_time; | |
| 38 | |
| 39 // The sync context for this datatype. Only valid for non-entity based data. | |
| 40 std::string context; | |
| 41 | |
| 42 // The sync entity associated with this data. Only valid for non-context | |
| 43 // data. | |
| 44 sync_pb::SyncEntity entity; | |
| 45 }; | |
| 46 | |
| 47 SyncDataCore::SyncDataCore() | |
| 48 : datatype(UNSPECIFIED), | |
| 49 id(kInvalidId) {} | |
| 50 | |
| 51 SyncDataCore::SyncDataCore(ModelType datatype, | |
| 52 int64 id, | |
| 53 base::Time remote_modification_time, | |
| 54 const std::string& context, | |
| 55 const sync_pb::SyncEntity& entity) | |
| 56 : datatype(datatype), | |
| 57 id(id), | |
| 58 remote_modification_time(remote_modification_time), | |
| 59 context(context), | |
| 60 entity(entity) { | |
| 23 } | 61 } |
| 24 | 62 |
| 25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( | 63 SyncDataCore::~SyncDataCore() {} |
| 64 | |
| 65 void SyncData::ImmutableSyncDataCoreTraits::InitializeWrapper( | |
| 66 Wrapper* wrapper) { | |
| 67 *wrapper = new SyncDataCore(); | |
| 68 } | |
| 69 | |
| 70 void SyncData::ImmutableSyncDataCoreTraits::DestroyWrapper( | |
| 26 Wrapper* wrapper) { | 71 Wrapper* wrapper) { |
| 27 delete *wrapper; | 72 delete *wrapper; |
| 28 } | 73 } |
| 29 | 74 |
| 30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap( | 75 const SyncDataCore& SyncData::ImmutableSyncDataCoreTraits::Unwrap( |
| 31 const Wrapper& wrapper) { | 76 const Wrapper& wrapper) { |
| 32 return *wrapper; | 77 return *wrapper; |
| 33 } | 78 } |
| 34 | 79 |
| 35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( | 80 SyncDataCore* SyncData::ImmutableSyncDataCoreTraits::UnwrapMutable( |
| 36 Wrapper* wrapper) { | 81 Wrapper* wrapper) { |
| 37 return *wrapper; | 82 return *wrapper; |
| 38 } | 83 } |
| 39 | 84 |
| 40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, | 85 void SyncData::ImmutableSyncDataCoreTraits::Swap(SyncDataCore* t1, |
| 41 sync_pb::SyncEntity* t2) { | 86 SyncDataCore* t2) { |
| 42 t1->Swap(t2); | 87 SyncDataCore temp(*t1); |
| 88 *t1 = *t2; | |
| 89 *t2 = *t1; | |
| 43 } | 90 } |
| 44 | 91 |
| 45 SyncData::SyncData() | 92 SyncData::SyncData() |
| 46 : is_valid_(false), | 93 : is_valid_(false) {} |
| 47 id_(kInvalidId) {} | |
| 48 | 94 |
| 49 SyncData::SyncData(int64 id, | 95 SyncData::SyncData(SyncDataCore* core) |
| 50 sync_pb::SyncEntity* entity, | 96 : is_valid_(true), immutable_data_(core) {} |
| 51 const base::Time& remote_modification_time) | |
| 52 : is_valid_(true), | |
| 53 id_(id), | |
| 54 remote_modification_time_(remote_modification_time), | |
| 55 immutable_entity_(entity) {} | |
| 56 | 97 |
| 57 SyncData::~SyncData() {} | 98 SyncData::~SyncData() {} |
| 58 | 99 |
| 59 // Static. | 100 // Static. |
| 60 SyncData SyncData::CreateLocalDelete( | 101 SyncData SyncData::CreateLocalDelete( |
| 61 const std::string& sync_tag, | 102 const std::string& sync_tag, |
| 62 ModelType datatype) { | 103 ModelType datatype) { |
| 63 sync_pb::EntitySpecifics specifics; | 104 sync_pb::EntitySpecifics specifics; |
| 64 AddDefaultFieldValue(datatype, &specifics); | 105 AddDefaultFieldValue(datatype, &specifics); |
| 65 return CreateLocalData(sync_tag, std::string(), specifics); | 106 return CreateLocalData(sync_tag, std::string(), specifics); |
| 66 } | 107 } |
| 67 | 108 |
| 68 // Static. | 109 // Static. |
| 69 SyncData SyncData::CreateLocalData( | 110 SyncData SyncData::CreateLocalData( |
| 70 const std::string& sync_tag, | 111 const std::string& sync_tag, |
| 71 const std::string& non_unique_title, | 112 const std::string& non_unique_title, |
| 72 const sync_pb::EntitySpecifics& specifics) { | 113 const sync_pb::EntitySpecifics& specifics) { |
| 73 sync_pb::SyncEntity entity; | 114 SyncDataCore core(GetModelTypeFromSpecifics(specifics), |
| 74 entity.set_client_defined_unique_tag(sync_tag); | 115 kInvalidId, |
| 75 entity.set_non_unique_name(non_unique_title); | 116 base::Time(), |
| 76 entity.mutable_specifics()->CopyFrom(specifics); | 117 "", |
| 77 return SyncData(kInvalidId, &entity, base::Time()); | 118 sync_pb::SyncEntity()); |
| 119 core.entity.set_client_defined_unique_tag(sync_tag); | |
| 120 core.entity.set_non_unique_name(non_unique_title); | |
| 121 core.entity.mutable_specifics()->CopyFrom(specifics); | |
| 122 return SyncData(&core); | |
| 78 } | 123 } |
| 79 | 124 |
| 80 // Static. | 125 // Static. |
| 81 SyncData SyncData::CreateRemoteData( | 126 SyncData SyncData::CreateRemoteData( |
| 82 int64 id, const sync_pb::EntitySpecifics& specifics, | 127 int64 id, const sync_pb::EntitySpecifics& specifics, |
| 83 const base::Time& modification_time) { | 128 const base::Time& modification_time) { |
| 84 DCHECK_NE(id, kInvalidId); | 129 DCHECK_NE(id, kInvalidId); |
| 85 sync_pb::SyncEntity entity; | 130 SyncDataCore core(GetModelTypeFromSpecifics(specifics), |
| 86 entity.mutable_specifics()->CopyFrom(specifics); | 131 id, |
| 87 return SyncData(id, &entity, modification_time); | 132 modification_time, |
| 133 "", | |
| 134 sync_pb::SyncEntity()); | |
| 135 core.entity.mutable_specifics()->CopyFrom(specifics); | |
| 136 return SyncData(&core); | |
| 137 } | |
| 138 | |
| 139 // Static. | |
| 140 SyncData SyncData::CreateContext( | |
| 141 ModelType datatype, | |
| 142 const std::string& context) { | |
| 143 SyncDataCore core(datatype, | |
| 144 kInvalidId, | |
| 145 base::Time(), | |
| 146 context, | |
| 147 sync_pb::SyncEntity()); | |
| 148 return SyncData(&core); | |
| 88 } | 149 } |
| 89 | 150 |
| 90 bool SyncData::IsValid() const { | 151 bool SyncData::IsValid() const { |
| 91 return is_valid_; | 152 return is_valid_; |
| 92 } | 153 } |
| 93 | 154 |
| 94 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 155 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
| 95 return immutable_entity_.Get().specifics(); | 156 return immutable_data_.Get().entity.specifics(); |
| 96 } | 157 } |
| 97 | 158 |
| 98 ModelType SyncData::GetDataType() const { | 159 ModelType SyncData::GetDataType() const { |
| 99 return GetModelTypeFromSpecifics(GetSpecifics()); | 160 return GetModelTypeFromSpecifics(GetSpecifics()); |
| 100 } | 161 } |
| 101 | 162 |
| 102 const std::string& SyncData::GetTag() const { | 163 const std::string& SyncData::GetTag() const { |
| 103 DCHECK(IsLocal()); | 164 DCHECK(IsLocal()); |
| 104 return immutable_entity_.Get().client_defined_unique_tag(); | 165 return immutable_data_.Get().entity.client_defined_unique_tag(); |
| 105 } | 166 } |
| 106 | 167 |
| 107 const std::string& SyncData::GetTitle() const { | 168 const std::string& SyncData::GetTitle() const { |
| 108 // TODO(zea): set this for data coming from the syncer too. | 169 // TODO(zea): set this for data coming from the syncer too. |
| 109 DCHECK(immutable_entity_.Get().has_non_unique_name()); | 170 DCHECK(immutable_data_.Get().entity.has_non_unique_name()); |
| 110 return immutable_entity_.Get().non_unique_name(); | 171 return immutable_data_.Get().entity.non_unique_name(); |
| 172 } | |
| 173 | |
| 174 const std::string& SyncData::GetContext() const { | |
| 175 return immutable_data_.Get().context; | |
| 111 } | 176 } |
| 112 | 177 |
| 113 const base::Time& SyncData::GetRemoteModifiedTime() const { | 178 const base::Time& SyncData::GetRemoteModifiedTime() const { |
| 114 DCHECK(!IsLocal()); | 179 DCHECK(!IsLocal()); |
| 115 return remote_modification_time_; | 180 return immutable_data_.Get().remote_modification_time; |
| 116 } | 181 } |
| 117 | 182 |
| 118 int64 SyncData::GetRemoteId() const { | 183 int64 SyncData::GetRemoteId() const { |
| 119 DCHECK(!IsLocal()); | 184 DCHECK(!IsLocal()); |
| 120 return id_; | 185 return immutable_data_.Get().id; |
| 121 } | 186 } |
| 122 | 187 |
| 123 bool SyncData::IsLocal() const { | 188 bool SyncData::IsLocal() const { |
| 124 return id_ == kInvalidId; | 189 return immutable_data_.Get().id == kInvalidId; |
| 125 } | 190 } |
| 126 | 191 |
| 127 std::string SyncData::ToString() const { | 192 std::string SyncData::ToString() const { |
| 128 if (!IsValid()) | 193 if (!IsValid()) |
| 129 return "<Invalid SyncData>"; | 194 return "<Invalid SyncData>"; |
| 130 | 195 |
| 131 std::string type = ModelTypeToString(GetDataType()); | 196 std::string type = ModelTypeToString(GetDataType()); |
| 132 std::string specifics; | 197 std::string specifics; |
| 133 scoped_ptr<base::DictionaryValue> value( | 198 scoped_ptr<base::DictionaryValue> value( |
| 134 EntitySpecificsToValue(GetSpecifics())); | 199 EntitySpecificsToValue(GetSpecifics())); |
| 135 base::JSONWriter::WriteWithOptions(value.get(), | 200 base::JSONWriter::WriteWithOptions(value.get(), |
| 136 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 201 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 137 &specifics); | 202 &specifics); |
| 138 | 203 |
| 139 if (IsLocal()) { | 204 if (IsLocal()) { |
| 140 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + | 205 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + |
| 141 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; | 206 ", title: " + GetTitle() + ", context: " + GetContext() + |
| 207 "specifics: " + specifics + "}"; | |
|
maniscalco
2014/03/31 23:59:16
Need a comma and space before specifics?
Nicolas Zea
2014/04/02 18:34:36
Done.
| |
| 142 } | 208 } |
| 143 | 209 |
| 144 std::string id = base::Int64ToString(GetRemoteId()); | 210 std::string id = base::Int64ToString(GetRemoteId()); |
| 145 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + | 211 return "{ isLocal: false, type: " + type + ", context: " + GetContext() + |
| 146 ", id: " + id + "}"; | 212 ", specifics: " + specifics + ", id: " + id + "}"; |
| 147 } | 213 } |
| 148 | 214 |
| 149 void PrintTo(const SyncData& sync_data, std::ostream* os) { | 215 void PrintTo(const SyncData& sync_data, std::ostream* os) { |
| 150 *os << sync_data.ToString(); | 216 *os << sync_data.ToString(); |
| 151 } | 217 } |
| 152 | 218 |
| 153 } // namespace syncer | 219 } // namespace syncer |
| OLD | NEW |