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 "sync/test/fake_server/fake_server_entity.h"
6
7 #include <limits>
8 #include <string>
9 #include <vector>
10
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"
23
24 using std::string;
25 using std::vector;
26
27 using syncer::ModelType;
28
29 // The separator used when formatting IDs.
30 const char kIdSeparator[] = "/";
31
32 namespace fake_server {
33
~FakeServerEntity()34 FakeServerEntity::~FakeServerEntity() { }
35
GetId() const36 const std::string& FakeServerEntity::GetId() const {
37 return id_;
38 }
39
GetModelType() const40 ModelType FakeServerEntity::GetModelType() const {
41 return model_type_;
42 }
43
GetVersion() const44 int64 FakeServerEntity::GetVersion() const {
45 return version_;
46 }
47
SetVersion(int64 version)48 void FakeServerEntity::SetVersion(int64 version) {
49 version_ = version;
50 }
51
GetName() const52 const std::string& FakeServerEntity::GetName() const {
53 return name_;
54 }
55
56 // static
CreateId(const ModelType & model_type,const string & inner_id)57 string FakeServerEntity::CreateId(const ModelType& model_type,
58 const string& inner_id) {
59 int field_number = GetSpecificsFieldNumberFromModelType(model_type);
60 return base::StringPrintf("%d%s%s",
61 field_number,
62 kIdSeparator,
63 inner_id.c_str());
64 }
65
66 // static
GetModelTypeFromId(const string & id)67 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) {
68 vector<string> tokens;
69 size_t token_count = Tokenize(id, kIdSeparator, &tokens);
70
71 int field_number;
72 if (token_count != 2 || !base::StringToInt(tokens[0], &field_number)) {
73 return syncer::UNSPECIFIED;
74 }
75
76 return syncer::GetModelTypeFromSpecificsFieldNumber(field_number);
77 }
78
FakeServerEntity(const string & id,const ModelType & model_type,int64 version,const string & name)79 FakeServerEntity::FakeServerEntity(const string& id,
80 const ModelType& model_type,
81 int64 version,
82 const string& name)
83 : id_(id),
84 model_type_(model_type),
85 version_(version),
86 name_(name) {}
87
SerializeBaseProtoFields(sync_pb::SyncEntity * sync_entity)88 void FakeServerEntity::SerializeBaseProtoFields(
89 sync_pb::SyncEntity* sync_entity) {
90 // FakeServerEntity fields
91 sync_entity->set_id_string(id_);
92 sync_entity->set_version(version_);
93 sync_entity->set_name(name_);
94
95 // Data via accessors
96 sync_entity->set_deleted(IsDeleted());
97 sync_entity->set_folder(IsFolder());
98 }
99
100 } // namespace fake_server
101