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.
5 #include "sync/test/fake_server/permanent_entity.h"
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "sync/internal_api/public/base/model_type.h"
12 #include "sync/protocol/sync.pb.h"
13 #include "sync/test/fake_server/fake_server_entity.h"
17 using syncer::ModelType;
19 // The parent tag for children of the root entity. Entities with this parent are
20 // referred to as top level enities.
21 static const char kRootParentTag[] = "0";
23 namespace fake_server {
25 PermanentEntity::~PermanentEntity() { }
28 FakeServerEntity* PermanentEntity::Create(const ModelType& model_type,
29 const string& server_tag,
31 const string& parent_server_tag) {
32 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
34 CHECK(!server_tag.empty()) << "A PermanentEntity must have a server tag.";
35 CHECK(!name.empty()) << "The entity must have a non-empty name.";
36 CHECK(!parent_server_tag.empty()) << "A PermanentEntity must have a parent "
38 CHECK(parent_server_tag != kRootParentTag) << "Top-level entities should not "
39 << "be created with this factory.";
41 string id = FakeServerEntity::CreateId(model_type, server_tag);
42 string parent_id = FakeServerEntity::CreateId(model_type, parent_server_tag);
43 sync_pb::EntitySpecifics entity_specifics;
44 AddDefaultFieldValue(model_type, &entity_specifics);
45 return new PermanentEntity(id,
54 FakeServerEntity* PermanentEntity::CreateTopLevel(const ModelType& model_type) {
55 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
57 string server_tag = syncer::ModelTypeToRootTag(model_type);
58 string name = syncer::ModelTypeToString(model_type);
59 string id = FakeServerEntity::GetTopLevelId(model_type);
60 sync_pb::EntitySpecifics entity_specifics;
61 AddDefaultFieldValue(model_type, &entity_specifics);
62 return new PermanentEntity(id,
71 FakeServerEntity* PermanentEntity::CreateUpdatedNigoriEntity(
72 const sync_pb::SyncEntity& client_entity,
73 FakeServerEntity* current_server_entity) {
74 ModelType model_type = current_server_entity->GetModelType();
75 CHECK(model_type == syncer::NIGORI) << "This factory only supports NIGORI "
78 return new PermanentEntity(current_server_entity->GetId(),
80 current_server_entity->GetName(),
81 current_server_entity->GetParentId(),
82 syncer::ModelTypeToRootTag(model_type),
83 client_entity.specifics());
86 PermanentEntity::PermanentEntity(const string& id,
87 const ModelType& model_type,
89 const string& parent_id,
90 const string& server_defined_unique_tag,
91 const sync_pb::EntitySpecifics& specifics)
92 : FakeServerEntity(id, model_type, 0, name),
93 server_defined_unique_tag_(server_defined_unique_tag),
94 parent_id_(parent_id),
95 specifics_(specifics) { }
97 string PermanentEntity::GetParentId() const {
101 void PermanentEntity::SerializeAsProto(sync_pb::SyncEntity* proto) {
102 FakeServerEntity::SerializeBaseProtoFields(proto);
104 sync_pb::EntitySpecifics* specifics = proto->mutable_specifics();
105 specifics->CopyFrom(specifics_);
107 proto->set_parent_id_string(parent_id_);
108 proto->set_server_defined_unique_tag(server_defined_unique_tag_);
111 bool PermanentEntity::IsDeleted() const {
115 bool PermanentEntity::IsFolder() const {
119 } // namespace fake_server