OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "sync/test/fake_server/fake_server_entity.h" | |
6 | |
7 #include <stdint.h> | |
8 | |
9 #include <limits> | |
10 #include <memory> | |
11 #include <string> | |
12 #include <vector> | |
13 | |
14 #include "base/guid.h" | |
15 #include "base/logging.h" | |
16 #include "base/memory/ref_counted.h" | |
17 #include "base/strings/string_number_conversions.h" | |
18 #include "base/strings/string_split.h" | |
19 #include "base/strings/string_util.h" | |
20 #include "base/strings/stringprintf.h" | |
21 #include "net/base/net_errors.h" | |
22 #include "net/http/http_status_code.h" | |
23 #include "sync/internal_api/public/base/model_type.h" | |
24 #include "sync/protocol/sync.pb.h" | |
25 | |
26 using std::string; | |
27 using std::vector; | |
28 | |
29 using syncer::ModelType; | |
30 | |
31 // The separator used when formatting IDs. | |
32 // | |
33 // We chose the underscore character because it doesn't conflict with the | |
34 // special characters used by base/base64.h's encoding, which is also used in | |
35 // the construction of some IDs. | |
36 const char kIdSeparator[] = "_"; | |
37 | |
38 namespace fake_server { | |
39 | |
40 FakeServerEntity::~FakeServerEntity() { } | |
41 | |
42 const std::string& FakeServerEntity::GetId() const { | |
43 return id_; | |
44 } | |
45 | |
46 ModelType FakeServerEntity::GetModelType() const { | |
47 return model_type_; | |
48 } | |
49 | |
50 int64_t FakeServerEntity::GetVersion() const { | |
51 return version_; | |
52 } | |
53 | |
54 void FakeServerEntity::SetVersion(int64_t version) { | |
55 version_ = version; | |
56 } | |
57 | |
58 const std::string& FakeServerEntity::GetName() const { | |
59 return name_; | |
60 } | |
61 | |
62 void FakeServerEntity::SetName(const std::string& name) { | |
63 name_ = name; | |
64 } | |
65 | |
66 void FakeServerEntity::SetSpecifics( | |
67 const sync_pb::EntitySpecifics& updated_specifics) { | |
68 specifics_ = updated_specifics; | |
69 } | |
70 | |
71 bool FakeServerEntity::IsDeleted() const { | |
72 return false; | |
73 } | |
74 | |
75 bool FakeServerEntity::IsFolder() const { | |
76 return false; | |
77 } | |
78 | |
79 bool FakeServerEntity::IsPermanent() const { | |
80 return false; | |
81 } | |
82 | |
83 // static | |
84 string FakeServerEntity::CreateId(const ModelType& model_type, | |
85 const string& inner_id) { | |
86 int field_number = GetSpecificsFieldNumberFromModelType(model_type); | |
87 return base::StringPrintf("%d%s%s", | |
88 field_number, | |
89 kIdSeparator, | |
90 inner_id.c_str()); | |
91 } | |
92 | |
93 // static | |
94 std::string FakeServerEntity::GetTopLevelId(const ModelType& model_type) { | |
95 return FakeServerEntity::CreateId( | |
96 model_type, | |
97 syncer::ModelTypeToRootTag(model_type)); | |
98 } | |
99 | |
100 // static | |
101 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) { | |
102 vector<base::StringPiece> tokens = base::SplitStringPiece( | |
103 id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
104 | |
105 int field_number; | |
106 if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) { | |
107 return syncer::UNSPECIFIED; | |
108 } | |
109 | |
110 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number); | |
111 } | |
112 | |
113 FakeServerEntity::FakeServerEntity(const string& id, | |
114 const ModelType& model_type, | |
115 int64_t version, | |
116 const string& name) | |
117 : id_(id), model_type_(model_type), version_(version), name_(name) {} | |
118 | |
119 void FakeServerEntity::SerializeBaseProtoFields( | |
120 sync_pb::SyncEntity* sync_entity) const { | |
121 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics(); | |
122 specifics->CopyFrom(specifics_); | |
123 | |
124 // FakeServerEntity fields | |
125 sync_entity->set_id_string(id_); | |
126 sync_entity->set_version(version_); | |
127 sync_entity->set_name(name_); | |
128 | |
129 // Data via accessors | |
130 sync_entity->set_deleted(IsDeleted()); | |
131 sync_entity->set_folder(IsFolder()); | |
132 | |
133 if (RequiresParentId()) | |
134 sync_entity->set_parent_id_string(GetParentId()); | |
135 } | |
136 | |
137 } // namespace fake_server | |
OLD | NEW |