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