OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_e
ntity.h" | 5 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_e
ntity.h" |
6 | 6 |
7 #include "base/guid.h" | 7 #include "base/guid.h" |
| 8 #include "base/rand_util.h" |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "components/sync/base/hash_util.h" |
8 #include "components/sync/engine_impl/loopback_server/persistent_permanent_entit
y.h" | 11 #include "components/sync/engine_impl/loopback_server/persistent_permanent_entit
y.h" |
9 #include "components/sync/protocol/sync.pb.h" | 12 #include "components/sync/protocol/sync.pb.h" |
10 | 13 |
11 using std::string; | 14 using std::string; |
12 | 15 |
13 namespace syncer { | 16 namespace syncer { |
14 | 17 |
15 PersistentUniqueClientEntity::PersistentUniqueClientEntity( | 18 PersistentUniqueClientEntity::PersistentUniqueClientEntity( |
16 const string& id, | 19 const string& id, |
17 ModelType model_type, | 20 ModelType model_type, |
18 int64_t version, | 21 int64_t version, |
19 const string& name, | 22 const string& name, |
20 const string& client_defined_unique_tag, | 23 const string& client_defined_unique_tag, |
21 const sync_pb::EntitySpecifics& specifics, | 24 const sync_pb::EntitySpecifics& specifics, |
22 int64_t creation_time, | 25 int64_t creation_time, |
23 int64_t last_modified_time) | 26 int64_t last_modified_time) |
24 : LoopbackServerEntity(id, model_type, version, name), | 27 : LoopbackServerEntity(id, model_type, version, name), |
25 client_defined_unique_tag_(client_defined_unique_tag), | 28 client_defined_unique_tag_(client_defined_unique_tag), |
26 creation_time_(creation_time), | 29 creation_time_(creation_time), |
27 last_modified_time_(last_modified_time) { | 30 last_modified_time_(last_modified_time) { |
28 SetSpecifics(specifics); | 31 SetSpecifics(specifics); |
29 } | 32 } |
30 | 33 |
31 PersistentUniqueClientEntity::~PersistentUniqueClientEntity() {} | 34 PersistentUniqueClientEntity::~PersistentUniqueClientEntity() {} |
32 | 35 |
33 // static | 36 // static |
34 std::unique_ptr<LoopbackServerEntity> PersistentUniqueClientEntity::Create( | 37 std::unique_ptr<LoopbackServerEntity> |
| 38 PersistentUniqueClientEntity::CreateFromEntity( |
35 const sync_pb::SyncEntity& client_entity) { | 39 const sync_pb::SyncEntity& client_entity) { |
36 CHECK(client_entity.has_client_defined_unique_tag()) | |
37 << "A PersistentUniqueClientEntity must have a client-defined unique " | |
38 "tag."; | |
39 ModelType model_type = GetModelTypeFromSpecifics(client_entity.specifics()); | 40 ModelType model_type = GetModelTypeFromSpecifics(client_entity.specifics()); |
40 string id = EffectiveIdForClientTaggedEntity(client_entity); | 41 CHECK_NE(client_entity.has_client_defined_unique_tag(), |
| 42 syncer::CommitOnlyTypes().Has(model_type)) |
| 43 << "A UniqueClientEntity should have a client-defined unique tag iff it " |
| 44 "is not a CommitOnly type."; |
| 45 // Without model type specific logic for each CommitOnly type, we cannot infer |
| 46 // a reasonable tag from the specifics. We need uniqueness for how the server |
| 47 // holds onto all objects, so simply make a new tag from a random number. |
| 48 string effective_tag = client_entity.has_client_defined_unique_tag() |
| 49 ? client_entity.client_defined_unique_tag() |
| 50 : base::Uint64ToString(base::RandUint64()); |
| 51 string id = LoopbackServerEntity::CreateId(model_type, effective_tag); |
41 return std::unique_ptr<LoopbackServerEntity>(new PersistentUniqueClientEntity( | 52 return std::unique_ptr<LoopbackServerEntity>(new PersistentUniqueClientEntity( |
42 id, model_type, client_entity.version(), client_entity.name(), | 53 id, model_type, client_entity.version(), client_entity.name(), |
43 client_entity.client_defined_unique_tag(), client_entity.specifics(), | 54 client_entity.client_defined_unique_tag(), client_entity.specifics(), |
44 client_entity.ctime(), client_entity.mtime())); | 55 client_entity.ctime(), client_entity.mtime())); |
45 } | 56 } |
46 | 57 |
47 // static | 58 // static |
48 std::string PersistentUniqueClientEntity::EffectiveIdForClientTaggedEntity( | 59 std::unique_ptr<LoopbackServerEntity> |
49 const sync_pb::SyncEntity& entity) { | 60 PersistentUniqueClientEntity::CreateFromEntitySpecifics( |
50 return LoopbackServerEntity::CreateId( | 61 const string& name, |
51 GetModelTypeFromSpecifics(entity.specifics()), | 62 const sync_pb::EntitySpecifics& entity_specifics) { |
52 entity.client_defined_unique_tag()); | 63 ModelType model_type = GetModelTypeFromSpecifics(entity_specifics); |
| 64 string client_defined_unique_tag = GenerateSyncableHash(model_type, name); |
| 65 string id = |
| 66 LoopbackServerEntity::CreateId(model_type, client_defined_unique_tag); |
| 67 return std::unique_ptr<LoopbackServerEntity>(new PersistentUniqueClientEntity( |
| 68 id, model_type, 0, name, client_defined_unique_tag, entity_specifics, |
| 69 1337, 1337)); |
53 } | 70 } |
54 | 71 |
55 bool PersistentUniqueClientEntity::RequiresParentId() const { | 72 bool PersistentUniqueClientEntity::RequiresParentId() const { |
56 return false; | 73 return false; |
57 } | 74 } |
58 | 75 |
59 string PersistentUniqueClientEntity::GetParentId() const { | 76 string PersistentUniqueClientEntity::GetParentId() const { |
60 // The parent ID for this type of entity should always be its ModelType's | 77 // The parent ID for this type of entity should always be its ModelType's |
61 // root node. | 78 // root node. |
62 return LoopbackServerEntity::GetTopLevelId(GetModelType()); | 79 return LoopbackServerEntity::GetTopLevelId(GetModelType()); |
63 } | 80 } |
64 | 81 |
65 sync_pb::LoopbackServerEntity_Type | 82 sync_pb::LoopbackServerEntity_Type |
66 PersistentUniqueClientEntity::GetLoopbackServerEntityType() const { | 83 PersistentUniqueClientEntity::GetLoopbackServerEntityType() const { |
67 return sync_pb::LoopbackServerEntity_Type_UNIQUE; | 84 return sync_pb::LoopbackServerEntity_Type_UNIQUE; |
68 } | 85 } |
69 | 86 |
70 void PersistentUniqueClientEntity::SerializeAsProto( | 87 void PersistentUniqueClientEntity::SerializeAsProto( |
71 sync_pb::SyncEntity* proto) const { | 88 sync_pb::SyncEntity* proto) const { |
72 LoopbackServerEntity::SerializeBaseProtoFields(proto); | 89 LoopbackServerEntity::SerializeBaseProtoFields(proto); |
73 | 90 |
74 proto->set_client_defined_unique_tag(client_defined_unique_tag_); | 91 proto->set_client_defined_unique_tag(client_defined_unique_tag_); |
75 proto->set_ctime(creation_time_); | 92 proto->set_ctime(creation_time_); |
76 proto->set_mtime(last_modified_time_); | 93 proto->set_mtime(last_modified_time_); |
77 } | 94 } |
78 | 95 |
79 } // namespace syncer | 96 } // namespace syncer |
OLD | NEW |