ServiceWorker: Consolidate version manipulation functions in SWProviderContext
[chromium-blink-merge.git] / sync / test / fake_server / fake_server_entity.cc
blobf3fc604fa838dfbab62d39535f84f41a28d62517
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.
5 #include "sync/test/fake_server/fake_server_entity.h"
7 #include <limits>
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/guid.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/stringprintf.h"
19 #include "net/base/net_errors.h"
20 #include "net/http/http_status_code.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/protocol/sync.pb.h"
24 using std::string;
25 using std::vector;
27 using syncer::ModelType;
29 // The separator used when formatting IDs.
31 // We chose the underscore character because it doesn't conflict with the
32 // special characters used by base/base64.h's encoding, which is also used in
33 // the construction of some IDs.
34 const char kIdSeparator[] = "_";
36 namespace fake_server {
38 FakeServerEntity::~FakeServerEntity() { }
40 const std::string& FakeServerEntity::GetId() const {
41 return id_;
44 ModelType FakeServerEntity::GetModelType() const {
45 return model_type_;
48 int64 FakeServerEntity::GetVersion() const {
49 return version_;
52 void FakeServerEntity::SetVersion(int64 version) {
53 version_ = version;
56 const std::string& FakeServerEntity::GetName() const {
57 return name_;
60 // static
61 string FakeServerEntity::CreateId(const ModelType& model_type,
62 const string& inner_id) {
63 int field_number = GetSpecificsFieldNumberFromModelType(model_type);
64 return base::StringPrintf("%d%s%s",
65 field_number,
66 kIdSeparator,
67 inner_id.c_str());
70 // static
71 std::string FakeServerEntity::GetTopLevelId(const ModelType& model_type) {
72 return FakeServerEntity::CreateId(
73 model_type,
74 syncer::ModelTypeToRootTag(model_type));
77 // static
78 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) {
79 vector<string> tokens;
80 size_t token_count = Tokenize(id, kIdSeparator, &tokens);
82 int field_number;
83 if (token_count != 2 || !base::StringToInt(tokens[0], &field_number)) {
84 return syncer::UNSPECIFIED;
87 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number);
90 FakeServerEntity::FakeServerEntity(const string& id,
91 const ModelType& model_type,
92 int64 version,
93 const string& name)
94 : model_type_(model_type),
95 id_(id),
96 version_(version),
97 name_(name) {}
99 void FakeServerEntity::SerializeBaseProtoFields(
100 sync_pb::SyncEntity* sync_entity) {
101 // FakeServerEntity fields
102 sync_entity->set_id_string(id_);
103 sync_entity->set_version(version_);
104 sync_entity->set_name(name_);
106 // Data via accessors
107 sync_entity->set_deleted(IsDeleted());
108 sync_entity->set_folder(IsFolder());
111 } // namespace fake_server