| 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/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "sync/internal_api/public/base_node.h" | 13 #include "sync/internal_api/public/base_node.h" |
| 14 #include "sync/internal_api/public/syncable/model_type.h" | 14 #include "sync/internal_api/public/syncable/model_type.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 csync { | 18 namespace syncer { |
| 19 | 19 |
| 20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( | 20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( |
| 21 Wrapper* wrapper) { | 21 Wrapper* wrapper) { |
| 22 *wrapper = new sync_pb::SyncEntity(); | 22 *wrapper = new sync_pb::SyncEntity(); |
| 23 } | 23 } |
| 24 | 24 |
| 25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( | 25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( |
| 26 Wrapper* wrapper) { | 26 Wrapper* wrapper) { |
| 27 delete *wrapper; | 27 delete *wrapper; |
| 28 } | 28 } |
| 29 | 29 |
| 30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap( | 30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap( |
| 31 const Wrapper& wrapper) { | 31 const Wrapper& wrapper) { |
| 32 return *wrapper; | 32 return *wrapper; |
| 33 } | 33 } |
| 34 | 34 |
| 35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( | 35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( |
| 36 Wrapper* wrapper) { | 36 Wrapper* wrapper) { |
| 37 return *wrapper; | 37 return *wrapper; |
| 38 } | 38 } |
| 39 | 39 |
| 40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, | 40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, |
| 41 sync_pb::SyncEntity* t2) { | 41 sync_pb::SyncEntity* t2) { |
| 42 t1->Swap(t2); | 42 t1->Swap(t2); |
| 43 } | 43 } |
| 44 | 44 |
| 45 SyncData::SyncData() | 45 SyncData::SyncData() |
| 46 : is_valid_(false), | 46 : is_valid_(false), |
| 47 id_(csync::kInvalidId) {} | 47 id_(syncer::kInvalidId) {} |
| 48 | 48 |
| 49 SyncData::SyncData(int64 id, sync_pb::SyncEntity* entity) | 49 SyncData::SyncData(int64 id, sync_pb::SyncEntity* entity) |
| 50 : is_valid_(true), | 50 : is_valid_(true), |
| 51 id_(id), | 51 id_(id), |
| 52 immutable_entity_(entity) {} | 52 immutable_entity_(entity) {} |
| 53 | 53 |
| 54 SyncData::~SyncData() {} | 54 SyncData::~SyncData() {} |
| 55 | 55 |
| 56 // Static. | 56 // Static. |
| 57 SyncData SyncData::CreateLocalDelete( | 57 SyncData SyncData::CreateLocalDelete( |
| 58 const std::string& sync_tag, | 58 const std::string& sync_tag, |
| 59 syncable::ModelType datatype) { | 59 syncable::ModelType datatype) { |
| 60 sync_pb::EntitySpecifics specifics; | 60 sync_pb::EntitySpecifics specifics; |
| 61 syncable::AddDefaultFieldValue(datatype, &specifics); | 61 syncable::AddDefaultFieldValue(datatype, &specifics); |
| 62 return CreateLocalData(sync_tag, "", specifics); | 62 return CreateLocalData(sync_tag, "", specifics); |
| 63 } | 63 } |
| 64 | 64 |
| 65 // Static. | 65 // Static. |
| 66 SyncData SyncData::CreateLocalData( | 66 SyncData SyncData::CreateLocalData( |
| 67 const std::string& sync_tag, | 67 const std::string& sync_tag, |
| 68 const std::string& non_unique_title, | 68 const std::string& non_unique_title, |
| 69 const sync_pb::EntitySpecifics& specifics) { | 69 const sync_pb::EntitySpecifics& specifics) { |
| 70 sync_pb::SyncEntity entity; | 70 sync_pb::SyncEntity entity; |
| 71 entity.set_client_defined_unique_tag(sync_tag); | 71 entity.set_client_defined_unique_tag(sync_tag); |
| 72 entity.set_non_unique_name(non_unique_title); | 72 entity.set_non_unique_name(non_unique_title); |
| 73 entity.mutable_specifics()->CopyFrom(specifics); | 73 entity.mutable_specifics()->CopyFrom(specifics); |
| 74 return SyncData(csync::kInvalidId, &entity); | 74 return SyncData(syncer::kInvalidId, &entity); |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Static. | 77 // Static. |
| 78 SyncData SyncData::CreateRemoteData( | 78 SyncData SyncData::CreateRemoteData( |
| 79 int64 id, const sync_pb::EntitySpecifics& specifics) { | 79 int64 id, const sync_pb::EntitySpecifics& specifics) { |
| 80 DCHECK_NE(id, csync::kInvalidId); | 80 DCHECK_NE(id, syncer::kInvalidId); |
| 81 sync_pb::SyncEntity entity; | 81 sync_pb::SyncEntity entity; |
| 82 entity.mutable_specifics()->CopyFrom(specifics); | 82 entity.mutable_specifics()->CopyFrom(specifics); |
| 83 return SyncData(id, &entity); | 83 return SyncData(id, &entity); |
| 84 } | 84 } |
| 85 | 85 |
| 86 bool SyncData::IsValid() const { | 86 bool SyncData::IsValid() const { |
| 87 return is_valid_; | 87 return is_valid_; |
| 88 } | 88 } |
| 89 | 89 |
| 90 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 90 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 105 DCHECK(immutable_entity_.Get().has_non_unique_name()); | 105 DCHECK(immutable_entity_.Get().has_non_unique_name()); |
| 106 return immutable_entity_.Get().non_unique_name(); | 106 return immutable_entity_.Get().non_unique_name(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 int64 SyncData::GetRemoteId() const { | 109 int64 SyncData::GetRemoteId() const { |
| 110 DCHECK(!IsLocal()); | 110 DCHECK(!IsLocal()); |
| 111 return id_; | 111 return id_; |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool SyncData::IsLocal() const { | 114 bool SyncData::IsLocal() const { |
| 115 return id_ == csync::kInvalidId; | 115 return id_ == syncer::kInvalidId; |
| 116 } | 116 } |
| 117 | 117 |
| 118 std::string SyncData::ToString() const { | 118 std::string SyncData::ToString() const { |
| 119 if (!IsValid()) | 119 if (!IsValid()) |
| 120 return "<Invalid SyncData>"; | 120 return "<Invalid SyncData>"; |
| 121 | 121 |
| 122 std::string type = syncable::ModelTypeToString(GetDataType()); | 122 std::string type = syncable::ModelTypeToString(GetDataType()); |
| 123 std::string specifics; | 123 std::string specifics; |
| 124 scoped_ptr<DictionaryValue> value( | 124 scoped_ptr<DictionaryValue> value( |
| 125 csync::EntitySpecificsToValue(GetSpecifics())); | 125 syncer::EntitySpecificsToValue(GetSpecifics())); |
| 126 base::JSONWriter::WriteWithOptions(value.get(), | 126 base::JSONWriter::WriteWithOptions(value.get(), |
| 127 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 127 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 128 &specifics); | 128 &specifics); |
| 129 | 129 |
| 130 if (IsLocal()) { | 130 if (IsLocal()) { |
| 131 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + | 131 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + |
| 132 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; | 132 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; |
| 133 } | 133 } |
| 134 | 134 |
| 135 std::string id = base::Int64ToString(GetRemoteId()); | 135 std::string id = base::Int64ToString(GetRemoteId()); |
| 136 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + | 136 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + |
| 137 ", id: " + id + "}"; | 137 ", id: " + id + "}"; |
| 138 } | 138 } |
| 139 | 139 |
| 140 void PrintTo(const SyncData& sync_data, std::ostream* os) { | 140 void PrintTo(const SyncData& sync_data, std::ostream* os) { |
| 141 *os << sync_data.ToString(); | 141 *os << sync_data.ToString(); |
| 142 } | 142 } |
| 143 | 143 |
| 144 } // namespace csync | 144 } // namespace syncer |
| OLD | NEW |