Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(374)

Unified Diff: components/sync/engine_impl/loopback_server/loopback_server_entity.cc

Issue 2360703002: [Sync] Implements the loopback sync server. (Closed)
Patch Set: Address comment. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/sync/engine_impl/loopback_server/loopback_server_entity.cc
diff --git a/components/sync/engine_impl/loopback_server/loopback_server_entity.cc b/components/sync/engine_impl/loopback_server/loopback_server_entity.cc
new file mode 100644
index 0000000000000000000000000000000000000000..db5a193f8d40251c3a11bd6ca10cd806dbde0df4
--- /dev/null
+++ b/components/sync/engine_impl/loopback_server/loopback_server_entity.cc
@@ -0,0 +1,184 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/sync/engine_impl/loopback_server/loopback_server_entity.h"
+
+#include <stdint.h>
+
+#include <limits>
+#include <memory>
+#include <string>
+#include <vector>
+
+#include "base/guid.h"
+#include "base/logging.h"
+#include "base/memory/ptr_util.h"
+#include "base/memory/ref_counted.h"
+#include "base/strings/string_number_conversions.h"
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "components/sync/base/model_type.h"
+#include "components/sync/engine_impl/loopback_server/persistent_bookmark_entity.h"
+#include "components/sync/engine_impl/loopback_server/persistent_permanent_entity.h"
+#include "components/sync/engine_impl/loopback_server/persistent_tombstone_entity.h"
+#include "components/sync/engine_impl/loopback_server/persistent_unique_client_entity.h"
+#include "components/sync/protocol/sync.pb.h"
+#include "net/base/net_errors.h"
+#include "net/http/http_status_code.h"
+
+using std::string;
+using std::vector;
+
+using syncer::ModelType;
+
+namespace {
+// The separator used when formatting IDs.
+//
+// We chose the underscore character because it doesn't conflict with the
+// special characters used by base/base64.h's encoding, which is also used in
+// the construction of some IDs.
+const char kIdSeparator[] = "_";
+} // namespace
+
+namespace syncer {
+
+LoopbackServerEntity::~LoopbackServerEntity() {}
+
+// static
+std::unique_ptr<LoopbackServerEntity>
+LoopbackServerEntity::CreateEntityFromProto(
+ const sync_pb::LoopbackServerEntity& entity) {
+ switch (entity.type()) {
+ case sync_pb::LoopbackServerEntity_Type_TOMBSTONE:
+ return PersistentTombstoneEntity::Create(entity.entity());
+ case sync_pb::LoopbackServerEntity_Type_PERMANENT:
+ return base::MakeUnique<PersistentPermanentEntity>(
+ entity.entity().id_string(), entity.entity().version(),
+ syncer::GetModelType(entity.entity()), entity.entity().name(),
+ entity.entity().parent_id_string(),
+ entity.entity().server_defined_unique_tag(),
+ entity.entity().specifics());
+ case sync_pb::LoopbackServerEntity_Type_BOOKMARK:
+ return PersistentBookmarkEntity::CreateFromEntity(entity.entity());
+ case sync_pb::LoopbackServerEntity_Type_UNIQUE:
+ return PersistentUniqueClientEntity::Create(entity.entity());
+ case sync_pb::LoopbackServerEntity_Type_UNKNOWN:
+ CHECK(false) << "Unknown type encountered";
+ return std::unique_ptr<LoopbackServerEntity>(nullptr);
+ }
+ NOTREACHED();
+ return std::unique_ptr<LoopbackServerEntity>(nullptr);
+}
+
+const std::string& LoopbackServerEntity::GetId() const {
+ return id_;
+}
+
+ModelType LoopbackServerEntity::GetModelType() const {
+ return model_type_;
+}
+
+int64_t LoopbackServerEntity::GetVersion() const {
+ return version_;
+}
+
+void LoopbackServerEntity::SetVersion(int64_t version) {
+ version_ = version;
+}
+
+const std::string& LoopbackServerEntity::GetName() const {
+ return name_;
+}
+
+void LoopbackServerEntity::SetName(const std::string& name) {
+ name_ = name;
+}
+
+void LoopbackServerEntity::SetSpecifics(
+ const sync_pb::EntitySpecifics& updated_specifics) {
+ specifics_ = updated_specifics;
+}
+
+sync_pb::EntitySpecifics LoopbackServerEntity::GetSpecifics() const {
+ return specifics_;
+}
+
+bool LoopbackServerEntity::IsDeleted() const {
+ return false;
+}
+
+bool LoopbackServerEntity::IsFolder() const {
+ return false;
+}
+
+bool LoopbackServerEntity::IsPermanent() const {
+ return false;
+}
+
+sync_pb::LoopbackServerEntity_Type
+LoopbackServerEntity::GetLoopbackServerEntityType() const {
+ NOTREACHED();
+ return sync_pb::LoopbackServerEntity_Type_UNKNOWN;
+}
+
+// static
+string LoopbackServerEntity::CreateId(const ModelType& model_type,
+ const string& inner_id) {
+ int field_number = GetSpecificsFieldNumberFromModelType(model_type);
+ return base::StringPrintf("%d%s%s", field_number, kIdSeparator,
+ inner_id.c_str());
+}
+
+// static
+std::string LoopbackServerEntity::GetTopLevelId(const ModelType& model_type) {
+ return LoopbackServerEntity::CreateId(model_type,
+ syncer::ModelTypeToRootTag(model_type));
+}
+
+// static
+ModelType LoopbackServerEntity::GetModelTypeFromId(const string& id) {
+ vector<base::StringPiece> tokens = base::SplitStringPiece(
+ id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
+
+ int field_number;
+ if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) {
+ return syncer::UNSPECIFIED;
+ }
+
+ return syncer::GetModelTypeFromSpecificsFieldNumber(field_number);
+}
+
+LoopbackServerEntity::LoopbackServerEntity(const string& id,
+ const ModelType& model_type,
+ int64_t version,
+ const string& name)
+ : id_(id), model_type_(model_type), version_(version), name_(name) {}
+
+void LoopbackServerEntity::SerializeBaseProtoFields(
+ sync_pb::SyncEntity* sync_entity) const {
+ sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics();
+ specifics->CopyFrom(specifics_);
+
+ // LoopbackServerEntity fields
+ sync_entity->set_id_string(id_);
+ sync_entity->set_version(version_);
+ sync_entity->set_name(name_);
+
+ // Data via accessors
+ sync_entity->set_deleted(IsDeleted());
+ sync_entity->set_folder(IsFolder());
+
+ if (RequiresParentId())
+ sync_entity->set_parent_id_string(GetParentId());
+}
+
+void LoopbackServerEntity::SerializeAsLoopbackServerEntity(
+ sync_pb::LoopbackServerEntity* entity) const {
+ entity->set_type(GetLoopbackServerEntityType());
+ entity->set_model_type(static_cast<int64_t>(GetModelType()));
+ SerializeAsProto(entity->mutable_entity());
+}
+
+} // namespace syncer

Powered by Google App Engine
This is Rietveld 408576698