| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/sync/api/sync_data.h" | 5 #include "chrome/browser/sync/api/sync_data.h" |
| 6 | 6 |
| 7 #include "chrome/browser/sync/protocol/sync.pb.h" | 7 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 8 | 8 |
| 9 SyncData::SharedSyncEntity::SharedSyncEntity( | 9 void SyncData::ImmutableSyncEntityTraits::CreateAndDefaultInitialize( |
| 10 sync_pb::SyncEntity* sync_entity) | 10 Container* container) { |
| 11 : sync_entity_(new sync_pb::SyncEntity()){ | 11 *container = new sync_pb::SyncEntity(); |
| 12 sync_entity_->Swap(sync_entity); | |
| 13 } | 12 } |
| 14 | 13 |
| 15 const sync_pb::SyncEntity& SyncData::SharedSyncEntity::sync_entity() const { | 14 void SyncData::ImmutableSyncEntityTraits::Destroy(Container* container) { |
| 16 return *sync_entity_; | 15 delete *container; |
| 17 } | 16 } |
| 18 | 17 |
| 19 SyncData::SharedSyncEntity::~SharedSyncEntity() {} | 18 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap( |
| 20 | 19 const Container& container) { |
| 21 | 20 return *container; |
| 22 SyncData::SyncData() | |
| 23 : is_local_(true) { | |
| 24 } | 21 } |
| 25 | 22 |
| 26 SyncData::~SyncData() { | 23 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( |
| 24 Container* container) { |
| 25 return *container; |
| 27 } | 26 } |
| 28 | 27 |
| 28 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, |
| 29 sync_pb::SyncEntity* t2) { |
| 30 t1->Swap(t2); |
| 31 } |
| 32 |
| 33 SyncData::SyncData() : is_valid_(false), is_local_(true) {} |
| 34 |
| 35 SyncData::SyncData(sync_pb::SyncEntity* entity, bool is_local) |
| 36 : is_valid_(false), is_local_(is_local), immutable_entity_(entity) {} |
| 37 |
| 38 SyncData::~SyncData() {} |
| 39 |
| 29 // Static. | 40 // Static. |
| 30 SyncData SyncData::CreateLocalData(const std::string& sync_tag) { | 41 SyncData SyncData::CreateLocalData(const std::string& sync_tag) { |
| 31 sync_pb::SyncEntity entity; | 42 sync_pb::SyncEntity entity; |
| 32 entity.set_client_defined_unique_tag(sync_tag); | 43 entity.set_client_defined_unique_tag(sync_tag); |
| 33 SyncData a; | 44 return SyncData(&entity, true); |
| 34 a.shared_entity_ = new SharedSyncEntity(&entity); | |
| 35 a.is_local_ = true; | |
| 36 return a; | |
| 37 } | 45 } |
| 38 | 46 |
| 39 // Static. | 47 // Static. |
| 40 SyncData SyncData::CreateLocalData( | 48 SyncData SyncData::CreateLocalData( |
| 41 const std::string& sync_tag, | 49 const std::string& sync_tag, |
| 42 const std::string& non_unique_title, | 50 const std::string& non_unique_title, |
| 43 const sync_pb::EntitySpecifics& specifics) { | 51 const sync_pb::EntitySpecifics& specifics) { |
| 44 sync_pb::SyncEntity entity; | 52 sync_pb::SyncEntity entity; |
| 45 entity.set_client_defined_unique_tag(sync_tag); | 53 entity.set_client_defined_unique_tag(sync_tag); |
| 46 entity.set_non_unique_name(non_unique_title); | 54 entity.set_non_unique_name(non_unique_title); |
| 47 entity.mutable_specifics()->CopyFrom(specifics); | 55 entity.mutable_specifics()->CopyFrom(specifics); |
| 48 SyncData a; | 56 return SyncData(&entity, true); |
| 49 a.shared_entity_ = new SharedSyncEntity(&entity); | |
| 50 a.is_local_ = true; | |
| 51 return a; | |
| 52 } | 57 } |
| 53 | 58 |
| 54 // Static. | 59 // Static. |
| 55 SyncData SyncData::CreateRemoteData(const sync_pb::SyncEntity& entity) { | 60 SyncData SyncData::CreateRemoteData(const sync_pb::SyncEntity& entity) { |
| 56 // TODO(zea): eventually use this for building changes from the original sync | 61 // TODO(zea): eventually use this for building changes from the original sync |
| 57 // entities if possible. | 62 // entities if possible. |
| 58 NOTIMPLEMENTED(); | 63 NOTIMPLEMENTED(); |
| 59 return SyncData(); | 64 return SyncData(); |
| 60 } | 65 } |
| 61 | 66 |
| 62 // Static. | 67 // Static. |
| 63 SyncData SyncData::CreateRemoteData( | 68 SyncData SyncData::CreateRemoteData( |
| 64 const sync_pb::EntitySpecifics& specifics) { | 69 const sync_pb::EntitySpecifics& specifics) { |
| 65 sync_pb::SyncEntity entity; | 70 sync_pb::SyncEntity entity; |
| 66 entity.mutable_specifics()->CopyFrom(specifics); | 71 entity.mutable_specifics()->CopyFrom(specifics); |
| 67 SyncData a; | 72 return SyncData(&entity, false); |
| 68 a.shared_entity_ = new SharedSyncEntity(&entity); | |
| 69 a.is_local_ = false; | |
| 70 return a; | |
| 71 } | 73 } |
| 72 | 74 |
| 73 bool SyncData::IsValid() const { | 75 bool SyncData::IsValid() const { |
| 74 return (shared_entity_.get() != NULL); | 76 return is_valid_; |
| 75 } | 77 } |
| 76 | 78 |
| 77 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 79 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
| 78 return shared_entity_->sync_entity().specifics(); | 80 return immutable_entity_.Get().specifics(); |
| 79 } | 81 } |
| 80 | 82 |
| 81 syncable::ModelType SyncData::GetDataType() const { | 83 syncable::ModelType SyncData::GetDataType() const { |
| 82 return syncable::GetModelTypeFromSpecifics(GetSpecifics()); | 84 return syncable::GetModelTypeFromSpecifics(GetSpecifics()); |
| 83 } | 85 } |
| 84 | 86 |
| 85 const std::string& SyncData::GetTag() const { | 87 const std::string& SyncData::GetTag() const { |
| 86 DCHECK(is_local_); | 88 DCHECK(is_local_); |
| 87 return shared_entity_->sync_entity().client_defined_unique_tag(); | 89 return immutable_entity_.Get().client_defined_unique_tag(); |
| 88 } | 90 } |
| 89 | 91 |
| 90 const std::string& SyncData::GetTitle() const { | 92 const std::string& SyncData::GetTitle() const { |
| 91 // TODO(zea): set this for data coming from the syncer too. | 93 // TODO(zea): set this for data coming from the syncer too. |
| 92 DCHECK(shared_entity_->sync_entity().has_non_unique_name()); | 94 DCHECK(immutable_entity_.Get().has_non_unique_name()); |
| 93 return shared_entity_->sync_entity().non_unique_name(); | 95 return immutable_entity_.Get().non_unique_name(); |
| 94 } | 96 } |
| 95 | 97 |
| 96 bool SyncData::IsLocal() const { | 98 bool SyncData::IsLocal() const { |
| 97 return is_local_; | 99 return is_local_; |
| 98 } | 100 } |
| OLD | NEW |