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 "sync/test/fake_server/permanent_entity.h"
6
7 #include <string>
8
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"
14
15 using std::string;
16
17 using syncer::ModelType;
18
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";
22
23 namespace fake_server {
24
~PermanentEntity()25 PermanentEntity::~PermanentEntity() { }
26
27 // static
Create(const ModelType & model_type,const string & server_tag,const string & name,const string & parent_server_tag)28 FakeServerEntity* PermanentEntity::Create(const ModelType& model_type,
29 const string& server_tag,
30 const string& name,
31 const string& parent_server_tag) {
32 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
33 << "invalid.";
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 "
37 << "server tag.";
38 CHECK(parent_server_tag != kRootParentTag) << "Top-level entities should not "
39 << "be created with this factory.";
40
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,
46 model_type,
47 name,
48 parent_id,
49 server_tag,
50 entity_specifics);
51 }
52
53 // static
CreateTopLevel(const ModelType & model_type)54 FakeServerEntity* PermanentEntity::CreateTopLevel(const ModelType& model_type) {
55 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
56 << "invalid.";
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,
63 model_type,
64 name,
65 kRootParentTag,
66 server_tag,
67 entity_specifics);
68 }
69
70 // static
CreateUpdatedNigoriEntity(const sync_pb::SyncEntity & client_entity,FakeServerEntity * current_server_entity)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 "
76 << "entities.";
77
78 return new PermanentEntity(current_server_entity->GetId(),
79 model_type,
80 current_server_entity->GetName(),
81 current_server_entity->GetParentId(),
82 syncer::ModelTypeToRootTag(model_type),
83 client_entity.specifics());
84 }
85
PermanentEntity(const string & id,const ModelType & model_type,const string & name,const string & parent_id,const string & server_defined_unique_tag,const sync_pb::EntitySpecifics & specifics)86 PermanentEntity::PermanentEntity(const string& id,
87 const ModelType& model_type,
88 const string& name,
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) { }
96
GetParentId() const97 string PermanentEntity::GetParentId() const {
98 return parent_id_;
99 }
100
SerializeAsProto(sync_pb::SyncEntity * proto)101 void PermanentEntity::SerializeAsProto(sync_pb::SyncEntity* proto) {
102 FakeServerEntity::SerializeBaseProtoFields(proto);
103
104 sync_pb::EntitySpecifics* specifics = proto->mutable_specifics();
105 specifics->CopyFrom(specifics_);
106
107 proto->set_parent_id_string(parent_id_);
108 proto->set_server_defined_unique_tag(server_defined_unique_tag_);
109 }
110
IsDeleted() const111 bool PermanentEntity::IsDeleted() const {
112 return false;
113 }
114
IsFolder() const115 bool PermanentEntity::IsFolder() const {
116 return true;
117 }
118
119 } // namespace fake_server
120