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/unique_client_entity.h"
6
7 #include <string>
8
9 #include "base/basictypes.h"
10 #include "base/guid.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 #include "sync/test/fake_server/permanent_entity.h"
15
16 using std::string;
17
18 using syncer::ModelType;
19
20 namespace fake_server {
21
~UniqueClientEntity()22 UniqueClientEntity::~UniqueClientEntity() { }
23
24 // static
Create(const sync_pb::SyncEntity & client_entity)25 FakeServerEntity* UniqueClientEntity::Create(
26 const sync_pb::SyncEntity& client_entity) {
27 CHECK(client_entity.has_client_defined_unique_tag())
28 << "A UniqueClientEntity must have a client-defined unique tag.";
29 ModelType model_type =
30 syncer::GetModelTypeFromSpecifics(client_entity.specifics());
31 string id = EffectiveIdForClientTaggedEntity(client_entity);
32 return new UniqueClientEntity(id,
33 model_type,
34 client_entity.version(),
35 client_entity.name(),
36 client_entity.client_defined_unique_tag(),
37 client_entity.specifics(),
38 client_entity.ctime(),
39 client_entity.mtime());
40 }
41
42 // static
EffectiveIdForClientTaggedEntity(const sync_pb::SyncEntity & entity)43 std::string UniqueClientEntity::EffectiveIdForClientTaggedEntity(
44 const sync_pb::SyncEntity& entity) {
45 return FakeServerEntity::CreateId(
46 syncer::GetModelTypeFromSpecifics(entity.specifics()),
47 entity.client_defined_unique_tag());
48 }
49
UniqueClientEntity(const string & id,const ModelType & model_type,int64 version,const string & name,const string & client_defined_unique_tag,const sync_pb::EntitySpecifics & specifics,int64 creation_time,int64 last_modified_time)50 UniqueClientEntity::UniqueClientEntity(
51 const string& id,
52 const ModelType& model_type,
53 int64 version,
54 const string& name,
55 const string& client_defined_unique_tag,
56 const sync_pb::EntitySpecifics& specifics,
57 int64 creation_time,
58 int64 last_modified_time)
59 : FakeServerEntity(id, model_type, version, name),
60 client_defined_unique_tag_(client_defined_unique_tag),
61 specifics_(specifics),
62 creation_time_(creation_time),
63 last_modified_time_(last_modified_time) { }
64
GetParentId() const65 string UniqueClientEntity::GetParentId() const {
66 // The parent ID for this type of entity should always be its ModelType's
67 // root node.
68 return FakeServerEntity::GetTopLevelId(model_type_);
69 }
70
SerializeAsProto(sync_pb::SyncEntity * proto)71 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity* proto) {
72 FakeServerEntity::SerializeBaseProtoFields(proto);
73
74 sync_pb::EntitySpecifics* specifics = proto->mutable_specifics();
75 specifics->CopyFrom(specifics_);
76
77 proto->set_parent_id_string(GetParentId());
78 proto->set_client_defined_unique_tag(client_defined_unique_tag_);
79 proto->set_ctime(creation_time_);
80 proto->set_mtime(last_modified_time_);
81 }
82
IsDeleted() const83 bool UniqueClientEntity::IsDeleted() const {
84 return false;
85 }
86
IsFolder() const87 bool UniqueClientEntity::IsFolder() const {
88 return false;
89 }
90
91 } // namespace fake_server
92