Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / sync / test / fake_server / fake_server_entity.cc
blob9d7f6baebf4ff4ff9bb3388546cafeb411696dfb
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/fake_server_entity.h"
7 #include <limits>
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/guid.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/string_util.h"
19 #include "base/strings/stringprintf.h"
20 #include "net/base/net_errors.h"
21 #include "net/http/http_status_code.h"
22 #include "sync/internal_api/public/base/model_type.h"
23 #include "sync/protocol/sync.pb.h"
25 using std::string;
26 using std::vector;
28 using syncer::ModelType;
30 // The separator used when formatting IDs.
32 // We chose the underscore character because it doesn't conflict with the
33 // special characters used by base/base64.h's encoding, which is also used in
34 // the construction of some IDs.
35 const char kIdSeparator[] = "_";
37 namespace fake_server {
39 FakeServerEntity::~FakeServerEntity() { }
41 const std::string& FakeServerEntity::GetId() const {
42 return id_;
45 ModelType FakeServerEntity::GetModelType() const {
46 return model_type_;
49 int64 FakeServerEntity::GetVersion() const {
50 return version_;
53 void FakeServerEntity::SetVersion(int64 version) {
54 version_ = version;
57 const std::string& FakeServerEntity::GetName() const {
58 return name_;
61 void FakeServerEntity::SetName(std::string name) {
62 name_ = name;
65 void FakeServerEntity::SetSpecifics(
66 const sync_pb::EntitySpecifics& updated_specifics) {
67 specifics_ = updated_specifics;
70 bool FakeServerEntity::IsDeleted() const {
71 return false;
74 bool FakeServerEntity::IsFolder() const {
75 return false;
78 bool FakeServerEntity::IsPermanent() const {
79 return false;
82 // static
83 string FakeServerEntity::CreateId(const ModelType& model_type,
84 const string& inner_id) {
85 int field_number = GetSpecificsFieldNumberFromModelType(model_type);
86 return base::StringPrintf("%d%s%s",
87 field_number,
88 kIdSeparator,
89 inner_id.c_str());
92 // static
93 std::string FakeServerEntity::GetTopLevelId(const ModelType& model_type) {
94 return FakeServerEntity::CreateId(
95 model_type,
96 syncer::ModelTypeToRootTag(model_type));
99 // static
100 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) {
101 vector<base::StringPiece> tokens = base::SplitStringPiece(
102 id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
104 int field_number;
105 if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) {
106 return syncer::UNSPECIFIED;
109 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number);
112 FakeServerEntity::FakeServerEntity(const string& id,
113 const ModelType& model_type,
114 int64 version,
115 const string& name)
116 : id_(id),
117 model_type_(model_type),
118 version_(version),
119 name_(name) {}
121 void FakeServerEntity::SerializeBaseProtoFields(
122 sync_pb::SyncEntity* sync_entity) const {
123 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics();
124 specifics->CopyFrom(specifics_);
126 // FakeServerEntity fields
127 sync_entity->set_id_string(id_);
128 sync_entity->set_version(version_);
129 sync_entity->set_name(name_);
131 // Data via accessors
132 sync_entity->set_deleted(IsDeleted());
133 sync_entity->set_folder(IsFolder());
136 } // namespace fake_server