| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "components/sync/engine_impl/loopback_server/persistent_permanent_entit
y.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <string> |
| 9 |
| 10 #include "base/logging.h" |
| 11 #include "base/memory/ptr_util.h" |
| 12 #include "components/sync/base/model_type.h" |
| 13 #include "components/sync/engine_impl/loopback_server/loopback_server_entity.h" |
| 14 #include "components/sync/protocol/sync.pb.h" |
| 15 |
| 16 using std::string; |
| 17 |
| 18 using syncer::ModelType; |
| 19 |
| 20 namespace { |
| 21 // The parent tag for children of the root entity. Entities with this parent are |
| 22 // referred to as top level enities. |
| 23 static const char kRootParentTag[] = "0"; |
| 24 } // namespace |
| 25 |
| 26 namespace syncer { |
| 27 |
| 28 PersistentPermanentEntity::~PersistentPermanentEntity() {} |
| 29 |
| 30 // static |
| 31 std::unique_ptr<LoopbackServerEntity> PersistentPermanentEntity::Create( |
| 32 const ModelType& model_type, |
| 33 const string& server_tag, |
| 34 const string& name, |
| 35 const string& parent_server_tag) { |
| 36 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is " |
| 37 << "invalid."; |
| 38 CHECK(!server_tag.empty()) |
| 39 << "A PersistentPermanentEntity must have a server tag."; |
| 40 CHECK(!name.empty()) << "The entity must have a non-empty name."; |
| 41 CHECK(!parent_server_tag.empty()) |
| 42 << "A PersistentPermanentEntity must have a parent " |
| 43 << "server tag."; |
| 44 CHECK(parent_server_tag != kRootParentTag) << "Top-level entities should not " |
| 45 << "be created with this factory."; |
| 46 |
| 47 string id = LoopbackServerEntity::CreateId(model_type, server_tag); |
| 48 string parent_id = |
| 49 LoopbackServerEntity::CreateId(model_type, parent_server_tag); |
| 50 sync_pb::EntitySpecifics entity_specifics; |
| 51 AddDefaultFieldValue(model_type, &entity_specifics); |
| 52 return std::unique_ptr<LoopbackServerEntity>(new PersistentPermanentEntity( |
| 53 id, 0, model_type, name, parent_id, server_tag, entity_specifics)); |
| 54 } |
| 55 |
| 56 // static |
| 57 std::unique_ptr<LoopbackServerEntity> PersistentPermanentEntity::CreateTopLevel( |
| 58 const ModelType& model_type) { |
| 59 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is " |
| 60 << "invalid."; |
| 61 string server_tag = syncer::ModelTypeToRootTag(model_type); |
| 62 string name = syncer::ModelTypeToString(model_type); |
| 63 string id = LoopbackServerEntity::GetTopLevelId(model_type); |
| 64 sync_pb::EntitySpecifics entity_specifics; |
| 65 AddDefaultFieldValue(model_type, &entity_specifics); |
| 66 return std::unique_ptr<LoopbackServerEntity>(new PersistentPermanentEntity( |
| 67 id, 0, model_type, name, kRootParentTag, server_tag, entity_specifics)); |
| 68 } |
| 69 |
| 70 // static |
| 71 std::unique_ptr<LoopbackServerEntity> |
| 72 PersistentPermanentEntity::CreateUpdatedNigoriEntity( |
| 73 const sync_pb::SyncEntity& client_entity, |
| 74 const LoopbackServerEntity& current_server_entity) { |
| 75 ModelType model_type = current_server_entity.GetModelType(); |
| 76 CHECK(model_type == syncer::NIGORI) << "This factory only supports NIGORI " |
| 77 << "entities."; |
| 78 |
| 79 return base::WrapUnique<LoopbackServerEntity>(new PersistentPermanentEntity( |
| 80 current_server_entity.GetId(), current_server_entity.GetVersion(), |
| 81 model_type, current_server_entity.GetName(), |
| 82 current_server_entity.GetParentId(), |
| 83 syncer::ModelTypeToRootTag(model_type), client_entity.specifics())); |
| 84 } |
| 85 |
| 86 PersistentPermanentEntity::PersistentPermanentEntity( |
| 87 const string& id, |
| 88 int64_t version, |
| 89 const ModelType& model_type, |
| 90 const string& name, |
| 91 const string& parent_id, |
| 92 const string& server_defined_unique_tag, |
| 93 const sync_pb::EntitySpecifics& specifics) |
| 94 : LoopbackServerEntity(id, model_type, version, name), |
| 95 server_defined_unique_tag_(server_defined_unique_tag), |
| 96 parent_id_(parent_id) { |
| 97 SetSpecifics(specifics); |
| 98 } |
| 99 |
| 100 bool PersistentPermanentEntity::RequiresParentId() const { |
| 101 return true; |
| 102 } |
| 103 |
| 104 string PersistentPermanentEntity::GetParentId() const { |
| 105 return parent_id_; |
| 106 } |
| 107 |
| 108 void PersistentPermanentEntity::SerializeAsProto( |
| 109 sync_pb::SyncEntity* proto) const { |
| 110 LoopbackServerEntity::SerializeBaseProtoFields(proto); |
| 111 proto->set_server_defined_unique_tag(server_defined_unique_tag_); |
| 112 } |
| 113 |
| 114 bool PersistentPermanentEntity::IsFolder() const { |
| 115 return true; |
| 116 } |
| 117 |
| 118 bool PersistentPermanentEntity::IsPermanent() const { |
| 119 return true; |
| 120 } |
| 121 |
| 122 sync_pb::LoopbackServerEntity_Type |
| 123 PersistentPermanentEntity::GetLoopbackServerEntityType() const { |
| 124 return sync_pb::LoopbackServerEntity_Type_PERMANENT; |
| 125 } |
| 126 |
| 127 } // namespace syncer |
| OLD | NEW |