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 creation_time_(creation_time
),
49 last_modified_time_(last_modified_time
) {
50 SetSpecifics(specifics
);
53 UniqueClientEntity::~UniqueClientEntity() { }
56 scoped_ptr
<FakeServerEntity
> UniqueClientEntity::Create(
57 const sync_pb::SyncEntity
& client_entity
) {
58 CHECK(client_entity
.has_client_defined_unique_tag())
59 << "A UniqueClientEntity must have a client-defined unique tag.";
60 ModelType model_type
=
61 syncer::GetModelTypeFromSpecifics(client_entity
.specifics());
62 string id
= EffectiveIdForClientTaggedEntity(client_entity
);
63 return scoped_ptr
<FakeServerEntity
>(new UniqueClientEntity(
64 id
, model_type
, client_entity
.version(), client_entity
.name(),
65 client_entity
.client_defined_unique_tag(), client_entity
.specifics(),
66 client_entity
.ctime(), client_entity
.mtime()));
70 scoped_ptr
<FakeServerEntity
> UniqueClientEntity::CreateForInjection(
72 const sync_pb::EntitySpecifics
& entity_specifics
) {
73 ModelType model_type
= GetModelTypeFromSpecifics(entity_specifics
);
74 string client_defined_unique_tag
= GenerateSyncableHash(model_type
, name
);
75 string id
= FakeServerEntity::CreateId(model_type
, client_defined_unique_tag
);
76 return scoped_ptr
<FakeServerEntity
>(
77 new UniqueClientEntity(id
,
81 client_defined_unique_tag
,
88 std::string
UniqueClientEntity::EffectiveIdForClientTaggedEntity(
89 const sync_pb::SyncEntity
& entity
) {
90 return FakeServerEntity::CreateId(
91 syncer::GetModelTypeFromSpecifics(entity
.specifics()),
92 entity
.client_defined_unique_tag());
95 string
UniqueClientEntity::GetParentId() const {
96 // The parent ID for this type of entity should always be its ModelType's
98 return FakeServerEntity::GetTopLevelId(GetModelType());
101 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity
* proto
) const {
102 FakeServerEntity::SerializeBaseProtoFields(proto
);
104 proto
->set_parent_id_string(GetParentId());
105 proto
->set_client_defined_unique_tag(client_defined_unique_tag_
);
106 proto
->set_ctime(creation_time_
);
107 proto
->set_mtime(last_modified_time_
);
110 } // namespace fake_server