| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( | 56 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( |
| 57 Wrapper* wrapper) { | 57 Wrapper* wrapper) { |
| 58 return *wrapper; | 58 return *wrapper; |
| 59 } | 59 } |
| 60 | 60 |
| 61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, | 61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, |
| 62 sync_pb::SyncEntity* t2) { | 62 sync_pb::SyncEntity* t2) { |
| 63 t1->Swap(t2); | 63 t1->Swap(t2); |
| 64 } | 64 } |
| 65 | 65 |
| 66 SyncData::SyncData() : is_valid_(false), id_(kInvalidId) {} | 66 SyncData::SyncData() : id_(kInvalidId), is_valid_(false) {} |
| 67 | 67 |
| 68 SyncData::SyncData( | 68 SyncData::SyncData(int64 id, |
| 69 int64 id, | 69 sync_pb::SyncEntity* entity, |
| 70 sync_pb::SyncEntity* entity, | 70 AttachmentList* attachments, |
| 71 AttachmentList* attachments, | 71 const base::Time& remote_modification_time, |
| 72 const base::Time& remote_modification_time, | 72 const syncer::AttachmentServiceProxy& attachment_service) |
| 73 const syncer::AttachmentServiceProxy& attachment_service) | 73 : id_(id), |
| 74 : is_valid_(true), | |
| 75 id_(id), | |
| 76 remote_modification_time_(remote_modification_time), | 74 remote_modification_time_(remote_modification_time), |
| 77 immutable_entity_(entity), | 75 immutable_entity_(entity), |
| 78 attachments_(attachments), | 76 attachments_(attachments), |
| 79 attachment_service_(attachment_service) {} | 77 attachment_service_(attachment_service), |
| 78 is_valid_(true) {} |
| 80 | 79 |
| 81 SyncData::~SyncData() {} | 80 SyncData::~SyncData() {} |
| 82 | 81 |
| 83 // Static. | 82 // Static. |
| 84 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag, | 83 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag, |
| 85 ModelType datatype) { | 84 ModelType datatype) { |
| 86 sync_pb::EntitySpecifics specifics; | 85 sync_pb::EntitySpecifics specifics; |
| 87 AddDefaultFieldValue(datatype, &specifics); | 86 AddDefaultFieldValue(datatype, &specifics); |
| 88 return CreateLocalData(sync_tag, std::string(), specifics); | 87 return CreateLocalData(sync_tag, std::string(), specifics); |
| 89 } | 88 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 bool SyncData::IsValid() const { return is_valid_; } | 157 bool SyncData::IsValid() const { return is_valid_; } |
| 159 | 158 |
| 160 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 159 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
| 161 return immutable_entity_.Get().specifics(); | 160 return immutable_entity_.Get().specifics(); |
| 162 } | 161 } |
| 163 | 162 |
| 164 ModelType SyncData::GetDataType() const { | 163 ModelType SyncData::GetDataType() const { |
| 165 return GetModelTypeFromSpecifics(GetSpecifics()); | 164 return GetModelTypeFromSpecifics(GetSpecifics()); |
| 166 } | 165 } |
| 167 | 166 |
| 168 const std::string& SyncData::GetTag() const { | |
| 169 DCHECK(IsLocal()); | |
| 170 return immutable_entity_.Get().client_defined_unique_tag(); | |
| 171 } | |
| 172 | |
| 173 const std::string& SyncData::GetTitle() const { | 167 const std::string& SyncData::GetTitle() const { |
| 174 // TODO(zea): set this for data coming from the syncer too. | 168 // TODO(zea): set this for data coming from the syncer too. |
| 175 DCHECK(immutable_entity_.Get().has_non_unique_name()); | 169 DCHECK(immutable_entity_.Get().has_non_unique_name()); |
| 176 return immutable_entity_.Get().non_unique_name(); | 170 return immutable_entity_.Get().non_unique_name(); |
| 177 } | 171 } |
| 178 | 172 |
| 179 const base::Time& SyncData::GetRemoteModifiedTime() const { | |
| 180 DCHECK(!IsLocal()); | |
| 181 return remote_modification_time_; | |
| 182 } | |
| 183 | |
| 184 int64 SyncData::GetRemoteId() const { | |
| 185 DCHECK(!IsLocal()); | |
| 186 return id_; | |
| 187 } | |
| 188 | |
| 189 bool SyncData::IsLocal() const { return id_ == kInvalidId; } | 173 bool SyncData::IsLocal() const { return id_ == kInvalidId; } |
| 190 | 174 |
| 191 std::string SyncData::ToString() const { | 175 std::string SyncData::ToString() const { |
| 192 if (!IsValid()) | 176 if (!IsValid()) |
| 193 return "<Invalid SyncData>"; | 177 return "<Invalid SyncData>"; |
| 194 | 178 |
| 195 std::string type = ModelTypeToString(GetDataType()); | 179 std::string type = ModelTypeToString(GetDataType()); |
| 196 std::string specifics; | 180 std::string specifics; |
| 197 scoped_ptr<base::DictionaryValue> value( | 181 scoped_ptr<base::DictionaryValue> value( |
| 198 EntitySpecificsToValue(GetSpecifics())); | 182 EntitySpecificsToValue(GetSpecifics())); |
| 199 base::JSONWriter::WriteWithOptions( | 183 base::JSONWriter::WriteWithOptions( |
| 200 value.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &specifics); | 184 value.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &specifics); |
| 201 | 185 |
| 202 if (IsLocal()) { | 186 if (IsLocal()) { |
| 203 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + | 187 SyncDataLocal sync_data_local(*this); |
| 204 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; | 188 return "{ isLocal: true, type: " + type + ", tag: " + |
| 189 sync_data_local.GetTag() + ", title: " + GetTitle() + |
| 190 ", specifics: " + specifics + "}"; |
| 205 } | 191 } |
| 206 | 192 |
| 207 std::string id = base::Int64ToString(GetRemoteId()); | 193 SyncDataRemote sync_data_remote(*this); |
| 194 std::string id = base::Int64ToString(sync_data_remote.GetId()); |
| 208 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + | 195 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + |
| 209 ", id: " + id + "}"; | 196 ", id: " + id + "}"; |
| 210 } | 197 } |
| 211 | 198 |
| 212 void PrintTo(const SyncData& sync_data, std::ostream* os) { | 199 void PrintTo(const SyncData& sync_data, std::ostream* os) { |
| 213 *os << sync_data.ToString(); | 200 *os << sync_data.ToString(); |
| 214 } | 201 } |
| 215 | 202 |
| 216 AttachmentIdList SyncData::GetAttachmentIds() const { | 203 AttachmentIdList SyncData::GetAttachmentIds() const { |
| 217 AttachmentIdList result; | 204 AttachmentIdList result; |
| 218 const sync_pb::SyncEntity& entity = immutable_entity_.Get(); | 205 const sync_pb::SyncEntity& entity = immutable_entity_.Get(); |
| 219 std::transform(entity.attachment_id().begin(), | 206 std::transform(entity.attachment_id().begin(), |
| 220 entity.attachment_id().end(), | 207 entity.attachment_id().end(), |
| 221 std::back_inserter(result), | 208 std::back_inserter(result), |
| 222 ProtoToId); | 209 ProtoToId); |
| 223 return result; | 210 return result; |
| 224 } | 211 } |
| 225 | 212 |
| 226 const AttachmentList& SyncData::GetLocalAttachmentsForUpload() const { | 213 SyncDataLocal::SyncDataLocal(const SyncData& sync_data) : SyncData(sync_data) { |
| 227 DCHECK(IsLocal()); | 214 DCHECK(sync_data.IsLocal()); |
| 215 } |
| 216 |
| 217 SyncDataLocal::~SyncDataLocal() {} |
| 218 |
| 219 const AttachmentList& SyncDataLocal::GetLocalAttachmentsForUpload() const { |
| 228 return attachments_.Get(); | 220 return attachments_.Get(); |
| 229 } | 221 } |
| 230 | 222 |
| 231 void SyncData::GetOrDownloadAttachments( | 223 const std::string& SyncDataLocal::GetTag() const { |
| 224 return immutable_entity_.Get().client_defined_unique_tag(); |
| 225 } |
| 226 |
| 227 SyncDataRemote::SyncDataRemote(const SyncData& sync_data) |
| 228 : SyncData(sync_data) { |
| 229 DCHECK(!sync_data.IsLocal()); |
| 230 } |
| 231 |
| 232 SyncDataRemote::~SyncDataRemote() {} |
| 233 |
| 234 const base::Time& SyncDataRemote::GetModifiedTime() const { |
| 235 return remote_modification_time_; |
| 236 } |
| 237 |
| 238 int64 SyncDataRemote::GetId() const { |
| 239 return id_; |
| 240 } |
| 241 |
| 242 void SyncDataRemote::GetOrDownloadAttachments( |
| 232 const AttachmentIdList& attachment_ids, | 243 const AttachmentIdList& attachment_ids, |
| 233 const AttachmentService::GetOrDownloadCallback& callback) { | 244 const AttachmentService::GetOrDownloadCallback& callback) { |
| 234 DCHECK(!IsLocal()); | |
| 235 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); | 245 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); |
| 236 } | 246 } |
| 237 | 247 |
| 238 void SyncData::DropAttachments( | 248 void SyncDataRemote::DropAttachments( |
| 239 const AttachmentIdList& attachment_ids, | 249 const AttachmentIdList& attachment_ids, |
| 240 const AttachmentService::DropCallback& callback) { | 250 const AttachmentService::DropCallback& callback) { |
| 241 DCHECK(!IsLocal()); | |
| 242 attachment_service_.DropAttachments(attachment_ids, callback); | 251 attachment_service_.DropAttachments(attachment_ids, callback); |
| 243 } | 252 } |
| 244 | 253 |
| 245 } // namespace syncer | 254 } // namespace syncer |
| OLD | NEW |