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.
5 #include "sync/api/sync_data.h"
9 #include "base/json/json_writer.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/base_node.h"
15 #include "sync/protocol/proto_value_conversions.h"
16 #include "sync/protocol/sync.pb.h"
20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(
22 *wrapper = new sync_pb::SyncEntity();
25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(
30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap(
31 const Wrapper& wrapper) {
35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
41 sync_pb::SyncEntity* t2) {
49 SyncData::SyncData(int64 id,
50 sync_pb::SyncEntity* entity,
51 const base::Time& remote_modification_time)
54 remote_modification_time_(remote_modification_time),
55 immutable_entity_(entity) {}
57 SyncData::~SyncData() {}
60 SyncData SyncData::CreateLocalDelete(
61 const std::string& sync_tag,
63 sync_pb::EntitySpecifics specifics;
64 AddDefaultFieldValue(datatype, &specifics);
65 return CreateLocalData(sync_tag, std::string(), specifics);
69 SyncData SyncData::CreateLocalData(
70 const std::string& sync_tag,
71 const std::string& non_unique_title,
72 const sync_pb::EntitySpecifics& specifics) {
73 sync_pb::SyncEntity entity;
74 entity.set_client_defined_unique_tag(sync_tag);
75 entity.set_non_unique_name(non_unique_title);
76 entity.mutable_specifics()->CopyFrom(specifics);
77 return SyncData(kInvalidId, &entity, base::Time());
81 SyncData SyncData::CreateRemoteData(
82 int64 id, const sync_pb::EntitySpecifics& specifics,
83 const base::Time& modification_time) {
84 DCHECK_NE(id, kInvalidId);
85 sync_pb::SyncEntity entity;
86 entity.mutable_specifics()->CopyFrom(specifics);
87 return SyncData(id, &entity, modification_time);
90 bool SyncData::IsValid() const {
94 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
95 return immutable_entity_.Get().specifics();
98 ModelType SyncData::GetDataType() const {
99 return GetModelTypeFromSpecifics(GetSpecifics());
102 const std::string& SyncData::GetTag() const {
104 return immutable_entity_.Get().client_defined_unique_tag();
107 const std::string& SyncData::GetTitle() const {
108 // TODO(zea): set this for data coming from the syncer too.
109 DCHECK(immutable_entity_.Get().has_non_unique_name());
110 return immutable_entity_.Get().non_unique_name();
113 const base::Time& SyncData::GetRemoteModifiedTime() const {
115 return remote_modification_time_;
118 int64 SyncData::GetRemoteId() const {
123 bool SyncData::IsLocal() const {
124 return id_ == kInvalidId;
127 std::string SyncData::ToString() const {
129 return "<Invalid SyncData>";
131 std::string type = ModelTypeToString(GetDataType());
132 std::string specifics;
133 scoped_ptr<base::DictionaryValue> value(
134 EntitySpecificsToValue(GetSpecifics()));
135 base::JSONWriter::WriteWithOptions(value.get(),
136 base::JSONWriter::OPTIONS_PRETTY_PRINT,
140 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() +
141 ", title: " + GetTitle() + ", specifics: " + specifics + "}";
144 std::string id = base::Int64ToString(GetRemoteId());
145 return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
149 void PrintTo(const SyncData& sync_data, std::ostream* os) {
150 *os << sync_data.ToString();
153 } // namespace syncer