| 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" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 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_(syncer::kInvalidId) {} | 47 id_(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 syncer::ModelType datatype) { | 59 ModelType datatype) { |
| 60 sync_pb::EntitySpecifics specifics; | 60 sync_pb::EntitySpecifics specifics; |
| 61 syncer::AddDefaultFieldValue(datatype, &specifics); | 61 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(syncer::kInvalidId, &entity); | 74 return SyncData(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, syncer::kInvalidId); | 80 DCHECK_NE(id, 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 { |
| 91 return immutable_entity_.Get().specifics(); | 91 return immutable_entity_.Get().specifics(); |
| 92 } | 92 } |
| 93 | 93 |
| 94 syncer::ModelType SyncData::GetDataType() const { | 94 ModelType SyncData::GetDataType() const { |
| 95 return syncer::GetModelTypeFromSpecifics(GetSpecifics()); | 95 return GetModelTypeFromSpecifics(GetSpecifics()); |
| 96 } | 96 } |
| 97 | 97 |
| 98 const std::string& SyncData::GetTag() const { | 98 const std::string& SyncData::GetTag() const { |
| 99 DCHECK(IsLocal()); | 99 DCHECK(IsLocal()); |
| 100 return immutable_entity_.Get().client_defined_unique_tag(); | 100 return immutable_entity_.Get().client_defined_unique_tag(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 const std::string& SyncData::GetTitle() const { | 103 const std::string& SyncData::GetTitle() const { |
| 104 // TODO(zea): set this for data coming from the syncer too. | 104 // TODO(zea): set this for data coming from the syncer too. |
| 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_ == syncer::kInvalidId; | 115 return id_ == 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 = syncer::ModelTypeToString(GetDataType()); | 122 std::string type = ModelTypeToString(GetDataType()); |
| 123 std::string specifics; | 123 std::string specifics; |
| 124 scoped_ptr<DictionaryValue> value( | 124 scoped_ptr<DictionaryValue> value(EntitySpecificsToValue(GetSpecifics())); |
| 125 syncer::EntitySpecificsToValue(GetSpecifics())); | |
| 126 base::JSONWriter::WriteWithOptions(value.get(), | 125 base::JSONWriter::WriteWithOptions(value.get(), |
| 127 base::JSONWriter::OPTIONS_PRETTY_PRINT, | 126 base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 128 &specifics); | 127 &specifics); |
| 129 | 128 |
| 130 if (IsLocal()) { | 129 if (IsLocal()) { |
| 131 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + | 130 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + |
| 132 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; | 131 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; |
| 133 } | 132 } |
| 134 | 133 |
| 135 std::string id = base::Int64ToString(GetRemoteId()); | 134 std::string id = base::Int64ToString(GetRemoteId()); |
| 136 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + | 135 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + |
| 137 ", id: " + id + "}"; | 136 ", id: " + id + "}"; |
| 138 } | 137 } |
| 139 | 138 |
| 140 void PrintTo(const SyncData& sync_data, std::ostream* os) { | 139 void PrintTo(const SyncData& sync_data, std::ostream* os) { |
| 141 *os << sync_data.ToString(); | 140 *os << sync_data.ToString(); |
| 142 } | 141 } |
| 143 | 142 |
| 144 } // namespace syncer | 143 } // namespace syncer |
| OLD | NEW |