Roll libvpx 861f35:1fff3e
[chromium-blink-merge.git] / sync / test / fake_server / unique_client_entity.cc
blob994c9d0e4e5ccbcbdd16e1d8a1472da1a0a8ebf4
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"
7 #include <string>
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"
18 using std::string;
20 using syncer::ModelType;
21 using syncer::syncable::GenerateSyncableHash;
23 namespace fake_server {
25 namespace {
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;
34 } // namespace
36 UniqueClientEntity::UniqueClientEntity(
37 const string& id,
38 ModelType model_type,
39 int64 version,
40 const string& name,
41 const string& client_defined_unique_tag,
42 const sync_pb::EntitySpecifics& specifics,
43 int64 creation_time,
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() { }
53 // static
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,
62 model_type,
63 client_entity.version(),
64 client_entity.name(),
65 client_entity.client_defined_unique_tag(),
66 client_entity.specifics(),
67 client_entity.ctime(),
68 client_entity.mtime());
71 // static
72 scoped_ptr<FakeServerEntity> UniqueClientEntity::CreateForInjection(
73 ModelType model_type,
74 const string& name,
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,
80 model_type,
81 kUnusedVersion,
82 name,
83 client_defined_unique_tag,
84 entity_specifics,
85 kDefaultTime,
86 kDefaultTime));
89 // static
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
99 // root node.
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 {
116 return false;
119 bool UniqueClientEntity::IsFolder() const {
120 return false;
123 } // namespace fake_server