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/unique_client_entity.h"
9 #include "base/basictypes.h"
10 #include "base/guid.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/protocol/sync.pb.h"
14 #include "sync/syncable/syncable_util.h"
15 #include "sync/test/fake_server/fake_server_entity.h"
16 #include "sync/test/fake_server/permanent_entity.h"
20 using syncer::GetModelTypeFromSpecifics
;
21 using syncer::ModelType
;
22 using syncer::syncable::GenerateSyncableHash
;
24 namespace fake_server
{
28 // A version must be passed when creating a FakeServerEntity, but this value
29 // is overrideen immediately when saving the entity in FakeServer.
30 const int64 kUnusedVersion
= 0L;
32 // Default time (creation and last modified) used when creating entities.
33 const int64 kDefaultTime
= 1234L;
37 UniqueClientEntity::UniqueClientEntity(
42 const string
& client_defined_unique_tag
,
43 const sync_pb::EntitySpecifics
& specifics
,
45 int64 last_modified_time
)
46 : FakeServerEntity(id
, model_type
, version
, name
),
47 client_defined_unique_tag_(client_defined_unique_tag
),
48 specifics_(specifics
),
49 creation_time_(creation_time
),
50 last_modified_time_(last_modified_time
) { }
52 UniqueClientEntity::~UniqueClientEntity() { }
55 FakeServerEntity
* UniqueClientEntity::Create(
56 const sync_pb::SyncEntity
& client_entity
) {
57 CHECK(client_entity
.has_client_defined_unique_tag())
58 << "A UniqueClientEntity must have a client-defined unique tag.";
59 ModelType model_type
=
60 syncer::GetModelTypeFromSpecifics(client_entity
.specifics());
61 string id
= EffectiveIdForClientTaggedEntity(client_entity
);
62 return new UniqueClientEntity(id
,
64 client_entity
.version(),
66 client_entity
.client_defined_unique_tag(),
67 client_entity
.specifics(),
68 client_entity
.ctime(),
69 client_entity
.mtime());
73 scoped_ptr
<FakeServerEntity
> UniqueClientEntity::CreateForInjection(
75 const sync_pb::EntitySpecifics
& entity_specifics
) {
76 ModelType model_type
= GetModelTypeFromSpecifics(entity_specifics
);
77 string client_defined_unique_tag
= GenerateSyncableHash(model_type
, name
);
78 string id
= FakeServerEntity::CreateId(model_type
, client_defined_unique_tag
);
79 return scoped_ptr
<FakeServerEntity
>(
80 new UniqueClientEntity(id
,
84 client_defined_unique_tag
,
91 std::string
UniqueClientEntity::EffectiveIdForClientTaggedEntity(
92 const sync_pb::SyncEntity
& entity
) {
93 return FakeServerEntity::CreateId(
94 syncer::GetModelTypeFromSpecifics(entity
.specifics()),
95 entity
.client_defined_unique_tag());
98 string
UniqueClientEntity::GetParentId() const {
99 // The parent ID for this type of entity should always be its ModelType's
101 return FakeServerEntity::GetTopLevelId(model_type_
);
104 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity
* proto
) {
105 FakeServerEntity::SerializeBaseProtoFields(proto
);
107 sync_pb::EntitySpecifics
* specifics
= proto
->mutable_specifics();
108 specifics
->CopyFrom(specifics_
);
110 proto
->set_parent_id_string(GetParentId());
111 proto
->set_client_defined_unique_tag(client_defined_unique_tag_
);
112 proto
->set_ctime(creation_time_
);
113 proto
->set_mtime(last_modified_time_
);
116 bool UniqueClientEntity::IsDeleted() const {
120 bool UniqueClientEntity::IsFolder() const {
124 } // namespace fake_server