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::ModelType
;
21 using syncer::syncable::GenerateSyncableHash
;
23 namespace fake_server
{
27 // A version must be passed when creating a FakeServerEntity, but this value
28 // is overrideen immediately when saving the entity in FakeServer.
29 const int64 kUnusedVersion
= 0L;
31 // Default time (creation and last modified) used when creating entities.
32 const int64 kDefaultTime
= 1234L;
36 UniqueClientEntity::UniqueClientEntity(
41 const string
& client_defined_unique_tag
,
42 const sync_pb::EntitySpecifics
& specifics
,
44 int64 last_modified_time
)
45 : FakeServerEntity(id
, model_type
, version
, name
),
46 client_defined_unique_tag_(client_defined_unique_tag
),
47 specifics_(specifics
),
48 creation_time_(creation_time
),
49 last_modified_time_(last_modified_time
) { }
51 UniqueClientEntity::~UniqueClientEntity() { }
54 FakeServerEntity
* UniqueClientEntity::Create(
55 const sync_pb::SyncEntity
& client_entity
) {
56 CHECK(client_entity
.has_client_defined_unique_tag())
57 << "A UniqueClientEntity must have a client-defined unique tag.";
58 ModelType model_type
=
59 syncer::GetModelTypeFromSpecifics(client_entity
.specifics());
60 string id
= EffectiveIdForClientTaggedEntity(client_entity
);
61 return new UniqueClientEntity(id
,
63 client_entity
.version(),
65 client_entity
.client_defined_unique_tag(),
66 client_entity
.specifics(),
67 client_entity
.ctime(),
68 client_entity
.mtime());
72 scoped_ptr
<FakeServerEntity
> UniqueClientEntity::CreateForInjection(
75 const sync_pb::EntitySpecifics
& entity_specifics
) {
76 string client_defined_unique_tag
= GenerateSyncableHash(model_type
, name
);
77 string id
= FakeServerEntity::CreateId(model_type
, client_defined_unique_tag
);
78 return scoped_ptr
<FakeServerEntity
>(
79 new UniqueClientEntity(id
,
83 client_defined_unique_tag
,
90 std::string
UniqueClientEntity::EffectiveIdForClientTaggedEntity(
91 const sync_pb::SyncEntity
& entity
) {
92 return FakeServerEntity::CreateId(
93 syncer::GetModelTypeFromSpecifics(entity
.specifics()),
94 entity
.client_defined_unique_tag());
97 string
UniqueClientEntity::GetParentId() const {
98 // The parent ID for this type of entity should always be its ModelType's
100 return FakeServerEntity::GetTopLevelId(model_type_
);
103 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity
* proto
) {
104 FakeServerEntity::SerializeBaseProtoFields(proto
);
106 sync_pb::EntitySpecifics
* specifics
= proto
->mutable_specifics();
107 specifics
->CopyFrom(specifics_
);
109 proto
->set_parent_id_string(GetParentId());
110 proto
->set_client_defined_unique_tag(client_defined_unique_tag_
);
111 proto
->set_ctime(creation_time_
);
112 proto
->set_mtime(last_modified_time_
);
115 bool UniqueClientEntity::IsDeleted() const {
119 bool UniqueClientEntity::IsFolder() const {
123 } // namespace fake_server