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/test/fake_server/unique_client_entity.h" | 5 #include "components/sync/test/fake_server/unique_client_entity.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" |
8 #include "components/sync/base/hash_util.h" | 10 #include "components/sync/base/hash_util.h" |
9 #include "components/sync/protocol/sync.pb.h" | 11 #include "components/sync/protocol/sync.pb.h" |
10 #include "components/sync/test/fake_server/permanent_entity.h" | 12 #include "components/sync/test/fake_server/permanent_entity.h" |
11 | 13 |
12 using std::string; | 14 using std::string; |
13 using syncer::GenerateSyncableHash; | 15 using syncer::GenerateSyncableHash; |
14 using syncer::GetModelTypeFromSpecifics; | 16 using syncer::GetModelTypeFromSpecifics; |
15 using syncer::ModelType; | 17 using syncer::ModelType; |
16 | 18 |
17 namespace fake_server { | 19 namespace fake_server { |
(...skipping 26 matching lines...) Expand all Loading... |
44 creation_time_(creation_time), | 46 creation_time_(creation_time), |
45 last_modified_time_(last_modified_time) { | 47 last_modified_time_(last_modified_time) { |
46 SetSpecifics(specifics); | 48 SetSpecifics(specifics); |
47 } | 49 } |
48 | 50 |
49 UniqueClientEntity::~UniqueClientEntity() {} | 51 UniqueClientEntity::~UniqueClientEntity() {} |
50 | 52 |
51 // static | 53 // static |
52 std::unique_ptr<FakeServerEntity> UniqueClientEntity::Create( | 54 std::unique_ptr<FakeServerEntity> UniqueClientEntity::Create( |
53 const sync_pb::SyncEntity& client_entity) { | 55 const sync_pb::SyncEntity& client_entity) { |
54 CHECK(client_entity.has_client_defined_unique_tag()) | |
55 << "A UniqueClientEntity must have a client-defined unique tag."; | |
56 ModelType model_type = | 56 ModelType model_type = |
57 syncer::GetModelTypeFromSpecifics(client_entity.specifics()); | 57 syncer::GetModelTypeFromSpecifics(client_entity.specifics()); |
58 string id = EffectiveIdForClientTaggedEntity(client_entity); | 58 CHECK_NE(client_entity.has_client_defined_unique_tag(), |
| 59 syncer::CommitOnlyTypes().Has(model_type)) |
| 60 << "A UniqueClientEntity should have a client-defined unique tag iff it " |
| 61 "is not a CommitOnly type."; |
| 62 // Without model type specific logic for each CommitOnly type, we cannot infer |
| 63 // a reasonable tag from the specifics. We need uniqueness for how the server |
| 64 // holds onto all objects, so simply make a new tag from a random number. |
| 65 string effective_tag = client_entity.has_client_defined_unique_tag() |
| 66 ? client_entity.client_defined_unique_tag() |
| 67 : base::Uint64ToString(base::RandUint64()); |
| 68 string id = FakeServerEntity::CreateId(model_type, effective_tag); |
59 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( | 69 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( |
60 id, client_entity.client_defined_unique_tag(), model_type, | 70 id, effective_tag, model_type, client_entity.version(), |
61 client_entity.version(), client_entity.name(), client_entity.specifics(), | 71 client_entity.name(), client_entity.specifics(), client_entity.ctime(), |
62 client_entity.ctime(), client_entity.mtime())); | 72 client_entity.mtime())); |
63 } | 73 } |
64 | 74 |
65 // static | 75 // static |
66 std::unique_ptr<FakeServerEntity> UniqueClientEntity::CreateForInjection( | 76 std::unique_ptr<FakeServerEntity> UniqueClientEntity::CreateForInjection( |
67 const string& name, | 77 const string& name, |
68 const sync_pb::EntitySpecifics& entity_specifics) { | 78 const sync_pb::EntitySpecifics& entity_specifics) { |
69 ModelType model_type = GetModelTypeFromSpecifics(entity_specifics); | 79 ModelType model_type = GetModelTypeFromSpecifics(entity_specifics); |
70 string client_defined_unique_tag = GenerateSyncableHash(model_type, name); | 80 string client_defined_unique_tag = GenerateSyncableHash(model_type, name); |
71 string id = FakeServerEntity::CreateId(model_type, client_defined_unique_tag); | 81 string id = FakeServerEntity::CreateId(model_type, client_defined_unique_tag); |
72 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( | 82 return std::unique_ptr<FakeServerEntity>(new UniqueClientEntity( |
73 id, client_defined_unique_tag, model_type, kUnusedVersion, name, | 83 id, client_defined_unique_tag, model_type, kUnusedVersion, name, |
74 entity_specifics, kDefaultTime, kDefaultTime)); | 84 entity_specifics, kDefaultTime, kDefaultTime)); |
75 } | 85 } |
76 | 86 |
77 // static | |
78 std::string UniqueClientEntity::EffectiveIdForClientTaggedEntity( | |
79 const sync_pb::SyncEntity& entity) { | |
80 return FakeServerEntity::CreateId( | |
81 syncer::GetModelTypeFromSpecifics(entity.specifics()), | |
82 entity.client_defined_unique_tag()); | |
83 } | |
84 | |
85 bool UniqueClientEntity::RequiresParentId() const { | 87 bool UniqueClientEntity::RequiresParentId() const { |
86 return false; | 88 return false; |
87 } | 89 } |
88 | 90 |
89 string UniqueClientEntity::GetParentId() const { | 91 string UniqueClientEntity::GetParentId() const { |
90 // The parent ID for this type of entity should always be its ModelType's | 92 // The parent ID for this type of entity should always be its ModelType's |
91 // root node. | 93 // root node. |
92 return FakeServerEntity::GetTopLevelId(model_type()); | 94 return FakeServerEntity::GetTopLevelId(model_type()); |
93 } | 95 } |
94 | 96 |
95 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity* proto) const { | 97 void UniqueClientEntity::SerializeAsProto(sync_pb::SyncEntity* proto) const { |
96 FakeServerEntity::SerializeBaseProtoFields(proto); | 98 FakeServerEntity::SerializeBaseProtoFields(proto); |
97 | 99 |
98 proto->set_ctime(creation_time_); | 100 proto->set_ctime(creation_time_); |
99 proto->set_mtime(last_modified_time_); | 101 proto->set_mtime(last_modified_time_); |
100 } | 102 } |
101 | 103 |
102 } // namespace fake_server | 104 } // namespace fake_server |
OLD | NEW |