Chromium Code Reviews| 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 "components/sync/engine_impl/loopback_server/loopback_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/ptr_util.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "base/strings/string_number_conversions.h" | |
| 19 #include "base/strings/string_split.h" | |
| 20 #include "base/strings/string_util.h" | |
| 21 #include "base/strings/stringprintf.h" | |
| 22 #include "components/sync/base/model_type.h" | |
| 23 #include "components/sync/engine_impl/loopback_server/persistent_bookmark_entity .h" | |
| 24 #include "components/sync/engine_impl/loopback_server/persistent_permanent_entit y.h" | |
| 25 #include "components/sync/engine_impl/loopback_server/persistent_tombstone_entit y.h" | |
| 26 #include "components/sync/engine_impl/loopback_server/persistent_unique_client_e ntity.h" | |
| 27 #include "components/sync/protocol/sync.pb.h" | |
| 28 #include "net/base/net_errors.h" | |
| 29 #include "net/http/http_status_code.h" | |
| 30 | |
| 31 using std::string; | |
| 32 using std::vector; | |
| 33 | |
| 34 using syncer::ModelType; | |
| 35 | |
| 36 namespace { | |
| 37 // The separator used when formatting IDs. | |
| 38 // | |
| 39 // We chose the underscore character because it doesn't conflict with the | |
| 40 // special characters used by base/base64.h's encoding, which is also used in | |
| 41 // the construction of some IDs. | |
| 42 const char kIdSeparator[] = "_"; | |
| 43 } // namespace | |
| 44 | |
| 45 namespace syncer { | |
| 46 | |
| 47 LoopbackServerEntity::~LoopbackServerEntity() {} | |
| 48 | |
| 49 // static | |
| 50 std::unique_ptr<LoopbackServerEntity> | |
| 51 LoopbackServerEntity::CreateEntityFromProto( | |
| 52 const sync_pb::LoopbackServerEntity& entity) { | |
| 53 switch (entity.type()) { | |
| 54 case sync_pb::LoopbackServerEntity_Type_TOMBSTONE: | |
| 55 return PersistentTombstoneEntity::Create(entity.entity()); | |
| 56 case sync_pb::LoopbackServerEntity_Type_PERMANENT: | |
| 57 return base::MakeUnique<PersistentPermanentEntity>( | |
| 58 entity.entity().id_string(), entity.entity().version(), | |
| 59 syncer::GetModelType(entity.entity()), entity.entity().name(), | |
| 60 entity.entity().parent_id_string(), | |
| 61 entity.entity().server_defined_unique_tag(), | |
| 62 entity.entity().specifics()); | |
| 63 case sync_pb::LoopbackServerEntity_Type_BOOKMARK: | |
| 64 return PersistentBookmarkEntity::CreateFromEntity(entity.entity()); | |
| 65 case sync_pb::LoopbackServerEntity_Type_UNIQUE: | |
| 66 return PersistentUniqueClientEntity::Create(entity.entity()); | |
| 67 default: | |
|
pavely
2016/10/19 18:21:29
You can handle UNKNOWN case with CHECK(false) and
| |
| 68 CHECK(false) << "Unknown type encountered"; | |
| 69 return std::unique_ptr<LoopbackServerEntity>(nullptr); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 const std::string& LoopbackServerEntity::GetId() const { | |
| 74 return id_; | |
| 75 } | |
| 76 | |
| 77 ModelType LoopbackServerEntity::GetModelType() const { | |
| 78 return model_type_; | |
| 79 } | |
| 80 | |
| 81 int64_t LoopbackServerEntity::GetVersion() const { | |
| 82 return version_; | |
| 83 } | |
| 84 | |
| 85 void LoopbackServerEntity::SetVersion(int64_t version) { | |
| 86 version_ = version; | |
| 87 } | |
| 88 | |
| 89 const std::string& LoopbackServerEntity::GetName() const { | |
| 90 return name_; | |
| 91 } | |
| 92 | |
| 93 void LoopbackServerEntity::SetName(const std::string& name) { | |
| 94 name_ = name; | |
| 95 } | |
| 96 | |
| 97 void LoopbackServerEntity::SetSpecifics( | |
| 98 const sync_pb::EntitySpecifics& updated_specifics) { | |
| 99 specifics_ = updated_specifics; | |
| 100 } | |
| 101 | |
| 102 sync_pb::EntitySpecifics LoopbackServerEntity::GetSpecifics() const { | |
| 103 return specifics_; | |
| 104 } | |
| 105 | |
| 106 bool LoopbackServerEntity::IsDeleted() const { | |
| 107 return false; | |
| 108 } | |
| 109 | |
| 110 bool LoopbackServerEntity::IsFolder() const { | |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 bool LoopbackServerEntity::IsPermanent() const { | |
| 115 return false; | |
| 116 } | |
| 117 | |
| 118 sync_pb::LoopbackServerEntity_Type | |
| 119 LoopbackServerEntity::GetLoopbackServerEntityType() const { | |
| 120 NOTREACHED(); | |
| 121 return sync_pb::LoopbackServerEntity_Type_UNKNOWN; | |
| 122 } | |
| 123 | |
| 124 // static | |
| 125 string LoopbackServerEntity::CreateId(const ModelType& model_type, | |
| 126 const string& inner_id) { | |
| 127 int field_number = GetSpecificsFieldNumberFromModelType(model_type); | |
| 128 return base::StringPrintf("%d%s%s", field_number, kIdSeparator, | |
| 129 inner_id.c_str()); | |
| 130 } | |
| 131 | |
| 132 // static | |
| 133 std::string LoopbackServerEntity::GetTopLevelId(const ModelType& model_type) { | |
| 134 return LoopbackServerEntity::CreateId(model_type, | |
| 135 syncer::ModelTypeToRootTag(model_type)); | |
| 136 } | |
| 137 | |
| 138 // static | |
| 139 ModelType LoopbackServerEntity::GetModelTypeFromId(const string& id) { | |
| 140 vector<base::StringPiece> tokens = base::SplitStringPiece( | |
| 141 id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 142 | |
| 143 int field_number; | |
| 144 if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) { | |
| 145 return syncer::UNSPECIFIED; | |
| 146 } | |
| 147 | |
| 148 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number); | |
| 149 } | |
| 150 | |
| 151 LoopbackServerEntity::LoopbackServerEntity(const string& id, | |
| 152 const ModelType& model_type, | |
| 153 int64_t version, | |
| 154 const string& name) | |
| 155 : id_(id), model_type_(model_type), version_(version), name_(name) {} | |
| 156 | |
| 157 void LoopbackServerEntity::SerializeBaseProtoFields( | |
| 158 sync_pb::SyncEntity* sync_entity) const { | |
| 159 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics(); | |
| 160 specifics->CopyFrom(specifics_); | |
| 161 | |
| 162 // LoopbackServerEntity fields | |
| 163 sync_entity->set_id_string(id_); | |
| 164 sync_entity->set_version(version_); | |
| 165 sync_entity->set_name(name_); | |
| 166 | |
| 167 // Data via accessors | |
| 168 sync_entity->set_deleted(IsDeleted()); | |
| 169 sync_entity->set_folder(IsFolder()); | |
| 170 | |
| 171 if (RequiresParentId()) | |
| 172 sync_entity->set_parent_id_string(GetParentId()); | |
| 173 } | |
| 174 | |
| 175 void LoopbackServerEntity::SerializeAsLoopbackServerEntity( | |
| 176 sync_pb::LoopbackServerEntity* entity) const { | |
| 177 entity->set_type(GetLoopbackServerEntityType()); | |
| 178 entity->set_model_type(static_cast<int64_t>(GetModelType())); | |
| 179 SerializeAsProto(entity->mutable_entity()); | |
| 180 } | |
| 181 | |
| 182 } // namespace syncer | |
| OLD | NEW |