Update V8 to version 4.7.56.
[chromium-blink-merge.git] / sync / test / fake_server / unique_client_entity.cc
blob9f40916b599197ad8cb5de0f3d8914ca24f33967
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::GetModelTypeFromSpecifics;
21 using syncer::ModelType;
22 using syncer::syncable::GenerateSyncableHash;
24 namespace fake_server {
26 namespace {
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;
35 } // namespace
37 UniqueClientEntity::UniqueClientEntity(
38 const string& id,
39 ModelType model_type,
40 int64 version,
41 const string& name,
42 const string& client_defined_unique_tag,
43 const sync_pb::EntitySpecifics& specifics,
44 int64 creation_time,
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() { }
55 // static
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()));
69 // static
70 scoped_ptr<FakeServerEntity> UniqueClientEntity::CreateForInjection(
71 const string& name,
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,
78 model_type,
79 kUnusedVersion,
80 name,
81 client_defined_unique_tag,
82 entity_specifics,
83 kDefaultTime,
84 kDefaultTime));
87 // static
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
97 // root node.
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