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