OLD | NEW |
| (Empty) |
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 <stdint.h> | |
8 | |
9 #include <memory> | |
10 #include <string> | |
11 | |
12 #include "base/guid.h" | |
13 #include "sync/internal_api/public/base/model_type.h" | |
14 #include "sync/protocol/sync.pb.h" | |
15 #include "sync/syncable/syncable_util.h" | |
16 #include "sync/test/fake_server/fake_server_entity.h" | |
17 #include "sync/test/fake_server/permanent_entity.h" | |
18 | |
19 using std::string; | |
20 | |
21 using syncer::GetModelTypeFromSpecifics; | |
22 using syncer::ModelType; | |
23 using syncer::syncable::GenerateSyncableHash; | |
24 | |
25 namespace fake_server { | |
26 | |
27 namespace { | |
28 | |
29 // A version must be passed when creating a FakeServerEntity, but this value | |
30 // is overrideen immediately when saving the entity in FakeServer. | |
31 const int64_t kUnusedVersion = 0L; | |
32 | |
33 // Default time (creation and last modified) used when creating entities. | |
34 const int64_t kDefaultTime = 1234L; | |
35 | |
36 } // namespace | |
37 | |
38 UniqueClientEntity::UniqueClientEntity( | |
39 const string& id, | |
40 ModelType model_type, | |
41 int64_t version, | |
42 const string& name, | |
43 const string& client_defined_unique_tag, | |
44 const sync_pb::EntitySpecifics& specifics, | |
45 int64_t creation_time, | |
46 int64_t last_modified_time) | |
47 : FakeServerEntity(id, model_type, version, name), | |
48 client_defined_unique_tag_(client_defined_unique_tag), | |
49 creation_time_(creation_time), | |
50 last_modified_time_(last_modified_time) { | |
51 SetSpecifics(specifics); | |
52 } | |
53 | |
54 UniqueClientEntity::~UniqueClientEntity() { } | |
55 | |
56 // static | |
57 std::unique_ptr<FakeServerEntity> UniqueClientEntity::Create( | |
58 const sync_pb::SyncEntity& client_entity) { | |
59 CHECK(client_entity.has_client_defined_unique_tag()) | |
60 << "A UniqueClientEntity must have a client-defined unique tag."; | |
61 ModelType model_type = | |
62 syncer::GetModelTypeFromSpecifics(client_entity.specifics()); | |
63 string id = EffectiveIdForClientTaggedEntity(client_entity); | |
64 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( | |
65 id, model_type, client_entity.version(), client_entity.name(), | |
66 client_entity.client_defined_unique_tag(), client_entity.specifics(), | |
67 client_entity.ctime(), client_entity.mtime())); | |
68 } | |
69 | |
70 // static | |
71 std::unique_ptr<FakeServerEntity> UniqueClientEntity::CreateForInjection( | |
72 const string& name, | |
73 const sync_pb::EntitySpecifics& entity_specifics) { | |
74 ModelType model_type = GetModelTypeFromSpecifics(entity_specifics); | |
75 string client_defined_unique_tag = GenerateSyncableHash(model_type, name); | |
76 string id = FakeServerEntity::CreateId(model_type, client_defined_unique_tag); | |
77 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( | |
78 id, model_type, kUnusedVersion, name, client_defined_unique_tag, | |
79 entity_specifics, kDefaultTime, kDefaultTime)); | |
80 } | |
81 | |
82 // static | |
83 std::string UniqueClientEntity::EffectiveIdForClientTaggedEntity( | |
84 const sync_pb::SyncEntity& entity) { | |
85 return FakeServerEntity::CreateId( | |
86 syncer::GetModelTypeFromSpecifics(entity.specifics()), | |
87 entity.client_defined_unique_tag()); | |
88 } | |
89 | |
90 bool UniqueClientEntity::RequiresParentId() const { | |
91 return false; | |
92 } | |
93 | |
94 string UniqueClientEntity::GetParentId() const { | |
95 // The parent ID for this type of entity should always be its ModelType's | |
96 // root node. | |
97 return FakeServerEntity::GetTopLevelId(GetModelType()); | |
98 } | |
99 | |
100 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity* proto) const { | |
101 FakeServerEntity::SerializeBaseProtoFields(proto); | |
102 | |
103 proto->set_client_defined_unique_tag(client_defined_unique_tag_); | |
104 proto->set_ctime(creation_time_); | |
105 proto->set_mtime(last_modified_time_); | |
106 } | |
107 | |
108 } // namespace fake_server | |
OLD | NEW |