Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / sync / test / fake_server / permanent_entity.cc
blob15deea11b6ec3a6f3cd0023666b8761d2804f132
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/permanent_entity.h"
7 #include <string>
9 #include "base/basictypes.h"
10 #include "base/logging.h"
11 #include "sync/internal_api/public/base/model_type.h"
12 #include "sync/protocol/sync.pb.h"
13 #include "sync/test/fake_server/fake_server_entity.h"
15 using std::string;
17 using syncer::ModelType;
19 // The parent tag for children of the root entity. Entities with this parent are
20 // referred to as top level enities.
21 static const char kRootParentTag[] = "0";
23 namespace fake_server {
25 PermanentEntity::~PermanentEntity() { }
27 // static
28 scoped_ptr<FakeServerEntity> PermanentEntity::Create(
29 const ModelType& model_type,
30 const string& server_tag,
31 const string& name,
32 const string& parent_server_tag) {
33 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
34 << "invalid.";
35 CHECK(!server_tag.empty()) << "A PermanentEntity must have a server tag.";
36 CHECK(!name.empty()) << "The entity must have a non-empty name.";
37 CHECK(!parent_server_tag.empty()) << "A PermanentEntity must have a parent "
38 << "server tag.";
39 CHECK(parent_server_tag != kRootParentTag) << "Top-level entities should not "
40 << "be created with this factory.";
42 string id = FakeServerEntity::CreateId(model_type, server_tag);
43 string parent_id = FakeServerEntity::CreateId(model_type, parent_server_tag);
44 sync_pb::EntitySpecifics entity_specifics;
45 AddDefaultFieldValue(model_type, &entity_specifics);
46 return scoped_ptr<FakeServerEntity>(new PermanentEntity(
47 id, model_type, name, parent_id, server_tag, entity_specifics));
50 // static
51 scoped_ptr<FakeServerEntity> PermanentEntity::CreateTopLevel(
52 const ModelType& model_type) {
53 CHECK(model_type != syncer::UNSPECIFIED) << "The entity's ModelType is "
54 << "invalid.";
55 string server_tag = syncer::ModelTypeToRootTag(model_type);
56 string name = syncer::ModelTypeToString(model_type);
57 string id = FakeServerEntity::GetTopLevelId(model_type);
58 sync_pb::EntitySpecifics entity_specifics;
59 AddDefaultFieldValue(model_type, &entity_specifics);
60 return scoped_ptr<FakeServerEntity>(new PermanentEntity(
61 id, model_type, name, kRootParentTag, server_tag, entity_specifics));
64 // static
65 scoped_ptr<FakeServerEntity> PermanentEntity::CreateUpdatedNigoriEntity(
66 const sync_pb::SyncEntity& client_entity,
67 const FakeServerEntity& current_server_entity) {
68 ModelType model_type = current_server_entity.GetModelType();
69 CHECK(model_type == syncer::NIGORI) << "This factory only supports NIGORI "
70 << "entities.";
72 return make_scoped_ptr<FakeServerEntity>(new PermanentEntity(
73 current_server_entity.GetId(), model_type,
74 current_server_entity.GetName(), current_server_entity.GetParentId(),
75 syncer::ModelTypeToRootTag(model_type), client_entity.specifics()));
78 PermanentEntity::PermanentEntity(const string& id,
79 const ModelType& model_type,
80 const string& name,
81 const string& parent_id,
82 const string& server_defined_unique_tag,
83 const sync_pb::EntitySpecifics& specifics)
84 : FakeServerEntity(id, model_type, 0, name),
85 server_defined_unique_tag_(server_defined_unique_tag),
86 parent_id_(parent_id) {
87 SetSpecifics(specifics);
90 string PermanentEntity::GetParentId() const {
91 return parent_id_;
94 void PermanentEntity::SerializeAsProto(sync_pb::SyncEntity* proto) const {
95 FakeServerEntity::SerializeBaseProtoFields(proto);
97 proto->set_parent_id_string(parent_id_);
98 proto->set_server_defined_unique_tag(server_defined_unique_tag_);
101 bool PermanentEntity::IsFolder() const {
102 return true;
105 bool PermanentEntity::IsPermanent() const {
106 return true;
109 } // namespace fake_server