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
15 using std::string;
16
17 using syncer::ModelType;
18
19 namespace fake_server {
20
~UniqueClientEntity()21 UniqueClientEntity::~UniqueClientEntity() { }
22
23 // static
CreateNew(const sync_pb::SyncEntity & client_entity)24 FakeServerEntity* UniqueClientEntity::CreateNew(
25 const sync_pb::SyncEntity& client_entity) {
26 CHECK(client_entity.has_client_defined_unique_tag())
27 << "A UniqueClientEntity must have a client-defined unique tag.";
28 ModelType model_type =
29 syncer::GetModelTypeFromSpecifics(client_entity.specifics());
30 string id = client_entity.version() == 0 ?
31 FakeServerEntity::CreateId(model_type, base::GenerateGUID()) :
32 client_entity.id_string();
33 return new UniqueClientEntity(id,
34 model_type,
35 client_entity.version(),
36 client_entity.name(),
37 client_entity.parent_id_string(),
38 client_entity.client_defined_unique_tag(),
39 client_entity.specifics(),
40 client_entity.ctime(),
41 client_entity.mtime());
42 }
43
44 // static
CreateUpdatedVersion(const sync_pb::SyncEntity & client_entity,FakeServerEntity * current_server_entity)45 FakeServerEntity* UniqueClientEntity::CreateUpdatedVersion(
46 const sync_pb::SyncEntity& client_entity,
47 FakeServerEntity* current_server_entity) {
48 return new UniqueClientEntity(client_entity.id_string(),
49 current_server_entity->GetModelType(),
50 client_entity.version(),
51 client_entity.name(),
52 client_entity.parent_id_string(),
53 client_entity.client_defined_unique_tag(),
54 client_entity.specifics(),
55 client_entity.ctime(),
56 client_entity.mtime());
57 }
58
UniqueClientEntity(const string & id,const ModelType & model_type,int64 version,const string & name,const string & parent_id,const string & client_defined_unique_tag,const sync_pb::EntitySpecifics & specifics,int64 creation_time,int64 last_modified_time)59 UniqueClientEntity::UniqueClientEntity(
60 const string& id,
61 const ModelType& model_type,
62 int64 version,
63 const string& name,
64 const string& parent_id,
65 const string& client_defined_unique_tag,
66 const sync_pb::EntitySpecifics& specifics,
67 int64 creation_time,
68 int64 last_modified_time)
69 : FakeServerEntity(id, model_type, version, name),
70 parent_id_(parent_id),
71 client_defined_unique_tag_(client_defined_unique_tag),
72 specifics_(specifics),
73 creation_time_(creation_time),
74 last_modified_time_(last_modified_time) { }
75
GetParentId() const76 string UniqueClientEntity::GetParentId() const {
77 // The parent ID for this type of entity should always be its ModelType's
78 // root node.
79 return parent_id_;
80 }
81
SerializeAsProto()82 sync_pb::SyncEntity* UniqueClientEntity::SerializeAsProto() {
83 sync_pb::SyncEntity* sync_entity = new sync_pb::SyncEntity();
84 FakeServerEntity::SerializeBaseProtoFields(sync_entity);
85
86 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics();
87 specifics->CopyFrom(specifics_);
88
89 sync_entity->set_parent_id_string(parent_id_);
90 sync_entity->set_client_defined_unique_tag(client_defined_unique_tag_);
91 sync_entity->set_ctime(creation_time_);
92 sync_entity->set_mtime(last_modified_time_);
93
94 return sync_entity;
95 }
96
IsDeleted() const97 bool UniqueClientEntity::IsDeleted() const {
98 return false;
99 }
100
IsFolder() const101 bool UniqueClientEntity::IsFolder() const {
102 return false;
103 }
104
105 } // namespace fake_server
106