| 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/test/fake_server/fake_server_entity.h" | |
| 6 | |
| 7 #include <limits> | |
| 8 #include <memory> | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/guid.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/strings/string_number_conversions.h" | |
| 15 #include "base/strings/string_split.h" | |
| 16 #include "base/strings/string_util.h" | |
| 17 #include "base/strings/stringprintf.h" | |
| 18 #include "net/base/net_errors.h" | |
| 19 #include "net/http/http_status_code.h" | |
| 20 | |
| 21 using std::string; | |
| 22 using std::vector; | |
| 23 using syncer::ModelType; | |
| 24 | |
| 25 // The separator used when formatting IDs. | |
| 26 // | |
| 27 // 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 | |
| 29 // the construction of some IDs. | |
| 30 const char kIdSeparator[] = "_"; | |
| 31 | |
| 32 namespace fake_server { | |
| 33 | |
| 34 FakeServerEntity::~FakeServerEntity() {} | |
| 35 | |
| 36 int64_t FakeServerEntity::GetVersion() const { | |
| 37 return version_; | |
| 38 } | |
| 39 | |
| 40 void FakeServerEntity::SetVersion(int64_t version) { | |
| 41 version_ = version; | |
| 42 } | |
| 43 | |
| 44 const std::string& FakeServerEntity::GetName() const { | |
| 45 return name_; | |
| 46 } | |
| 47 | |
| 48 void FakeServerEntity::SetName(const std::string& name) { | |
| 49 name_ = name; | |
| 50 } | |
| 51 | |
| 52 void FakeServerEntity::SetSpecifics( | |
| 53 const sync_pb::EntitySpecifics& updated_specifics) { | |
| 54 specifics_ = updated_specifics; | |
| 55 } | |
| 56 | |
| 57 bool FakeServerEntity::IsDeleted() const { | |
| 58 return false; | |
| 59 } | |
| 60 | |
| 61 bool FakeServerEntity::IsFolder() const { | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 bool FakeServerEntity::IsPermanent() const { | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 // static | |
| 70 string FakeServerEntity::CreateId(const ModelType& model_type, | |
| 71 const string& inner_id) { | |
| 72 int field_number = GetSpecificsFieldNumberFromModelType(model_type); | |
| 73 return base::StringPrintf("%d%s%s", field_number, kIdSeparator, | |
| 74 inner_id.c_str()); | |
| 75 } | |
| 76 | |
| 77 // static | |
| 78 std::string FakeServerEntity::GetTopLevelId(const ModelType& model_type) { | |
| 79 return FakeServerEntity::CreateId(model_type, | |
| 80 syncer::ModelTypeToRootTag(model_type)); | |
| 81 } | |
| 82 | |
| 83 // static | |
| 84 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) { | |
| 85 vector<base::StringPiece> tokens = base::SplitStringPiece( | |
| 86 id, kIdSeparator, base::KEEP_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 87 | |
| 88 int field_number; | |
| 89 if (tokens.size() != 2 || !base::StringToInt(tokens[0], &field_number)) { | |
| 90 return syncer::UNSPECIFIED; | |
| 91 } | |
| 92 | |
| 93 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number); | |
| 94 } | |
| 95 | |
| 96 FakeServerEntity::FakeServerEntity(const string& id, | |
| 97 const string& client_defined_unique_tag, | |
| 98 const ModelType& model_type, | |
| 99 int64_t version, | |
| 100 const string& 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 | |
| 110 void FakeServerEntity::SerializeBaseProtoFields( | |
| 111 sync_pb::SyncEntity* sync_entity) const { | |
| 112 sync_pb::EntitySpecifics* specifics = sync_entity->mutable_specifics(); | |
| 113 specifics->CopyFrom(specifics_); | |
| 114 | |
| 115 // FakeServerEntity fields | |
| 116 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_); | |
| 120 sync_entity->set_name(name_); | |
| 121 | |
| 122 // Data via accessors | |
| 123 sync_entity->set_deleted(IsDeleted()); | |
| 124 sync_entity->set_folder(IsFolder()); | |
| 125 | |
| 126 if (RequiresParentId()) | |
| 127 sync_entity->set_parent_id_string(GetParentId()); | |
| 128 } | |
| 129 | |
| 130 } // namespace fake_server | |
| OLD | NEW |