| 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 case sync_pb::LoopbackServerEntity_Type_UNKNOWN: |
| 68 CHECK(false) << "Unknown type encountered"; |
| 69 return std::unique_ptr<LoopbackServerEntity>(nullptr); |
| 70 } |
| 71 NOTREACHED(); |
| 72 return std::unique_ptr<LoopbackServerEntity>(nullptr); |
| 73 } |
| 74 |
| 75 const std::string& LoopbackServerEntity::GetId() const { |
| 76 return id_; |
| 77 } |
| 78 |
| 79 ModelType LoopbackServerEntity::GetModelType() const { |
| 80 return model_type_; |
| 81 } |
| 82 |
| 83 int64_t LoopbackServerEntity::GetVersion() const { |
| 84 return version_; |
| 85 } |
| 86 |
| 87 void LoopbackServerEntity::SetVersion(int64_t version) { |
| 88 version_ = version; |
| 89 } |
| 90 |
| 91 const std::string& LoopbackServerEntity::GetName() const { |
| 92 return name_; |
| 93 } |
| 94 |
| 95 void LoopbackServerEntity::SetName(const std::string& name) { |
| 96 name_ = name; |
| 97 } |
| 98 |
| 99 void LoopbackServerEntity::SetSpecifics( |
| 100 const sync_pb::EntitySpecifics& updated_specifics) { |
| 101 specifics_ = updated_specifics; |
| 102 } |
| 103 |
| 104 sync_pb::EntitySpecifics LoopbackServerEntity::GetSpecifics() const { |
| 105 return specifics_; |
| 106 } |
| 107 |
| 108 bool LoopbackServerEntity::IsDeleted() const { |
| 109 return false; |
| 110 } |
| 111 |
| 112 bool LoopbackServerEntity::IsFolder() const { |
| 113 return false; |
| 114 } |
| 115 |
| 116 bool LoopbackServerEntity::IsPermanent() const { |
| 117 return false; |
| 118 } |
| 119 |
| 120 sync_pb::LoopbackServerEntity_Type |
| 121 LoopbackServerEntity::GetLoopbackServerEntityType() const { |
| 122 NOTREACHED(); |
| 123 return sync_pb::LoopbackServerEntity_Type_UNKNOWN; |
| 124 } |
| 125 |
| 126 // static |
| 127 string LoopbackServerEntity::CreateId(const ModelType& model_type, |
| 128 const string& inner_id) { |
| 129 int field_number = GetSpecificsFieldNumberFromModelType(model_type); |
| 130 return base::StringPrintf("%d%s%s", field_number, kIdSeparator, |
| 131 inner_id.c_str()); |
| 132 } |
| 133 |
| 134 // static |
| 135 std::string LoopbackServerEntity::GetTopLevelId(const ModelType& model_type) { |
| 136 return LoopbackServerEntity::CreateId(model_type, |
| 137 syncer::ModelTypeToRootTag(model_type)); |
| 138 } |
| 139 |
| 140 // static |
| 141 ModelType LoopbackServerEntity::GetModelTypeFromId(const string& id) { |
| 142 vector<base::StringPiece> tokens = base::SplitStringPiece( |
| 143 id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 144 |
| 145 int field_number; |
| 146 if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) { |
| 147 return syncer::UNSPECIFIED; |
| 148 } |
| 149 |
| 150 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number); |
| 151 } |
| 152 |
| 153 LoopbackServerEntity::LoopbackServerEntity(const string& id, |
| 154 const ModelType& model_type, |
| 155 int64_t version, |
| 156 const string& name) |
| 157 : id_(id), model_type_(model_type), version_(version), name_(name) {} |
| 158 |
| 159 void LoopbackServerEntity::SerializeBaseProtoFields( |
| 160 sync_pb::SyncEntity* sync_entity) const { |
| 161 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics(); |
| 162 specifics->CopyFrom(specifics_); |
| 163 |
| 164 // LoopbackServerEntity fields |
| 165 sync_entity->set_id_string(id_); |
| 166 sync_entity->set_version(version_); |
| 167 sync_entity->set_name(name_); |
| 168 |
| 169 // Data via accessors |
| 170 sync_entity->set_deleted(IsDeleted()); |
| 171 sync_entity->set_folder(IsFolder()); |
| 172 |
| 173 if (RequiresParentId()) |
| 174 sync_entity->set_parent_id_string(GetParentId()); |
| 175 } |
| 176 |
| 177 void LoopbackServerEntity::SerializeAsLoopbackServerEntity( |
| 178 sync_pb::LoopbackServerEntity* entity) const { |
| 179 entity->set_type(GetLoopbackServerEntityType()); |
| 180 entity->set_model_type(static_cast<int64_t>(GetModelType())); |
| 181 SerializeAsProto(entity->mutable_entity()); |
| 182 } |
| 183 |
| 184 } // namespace syncer |
| OLD | NEW |