Home | History | Annotate | Download | only in fake_server
      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 //
     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[] = "_";
     35 
     36 namespace fake_server {
     37 
     38 FakeServerEntity::~FakeServerEntity() { }
     39 
     40 const std::string& FakeServerEntity::GetId() const {
     41   return id_;
     42 }
     43 
     44 ModelType FakeServerEntity::GetModelType() const {
     45   return model_type_;
     46 }
     47 
     48 int64 FakeServerEntity::GetVersion() const {
     49   return version_;
     50 }
     51 
     52 void FakeServerEntity::SetVersion(int64 version) {
     53   version_ = version;
     54 }
     55 
     56 const std::string& FakeServerEntity::GetName() const {
     57   return name_;
     58 }
     59 
     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());
     68 }
     69 
     70 // static
     71 std::string FakeServerEntity::GetTopLevelId(const ModelType& model_type) {
     72   return FakeServerEntity::CreateId(
     73       model_type,
     74       syncer::ModelTypeToRootTag(model_type));
     75 }
     76 
     77 // static
     78 ModelType FakeServerEntity::GetModelTypeFromId(const string& id) {
     79   vector<string> tokens;
     80   size_t token_count = Tokenize(id, kIdSeparator, &tokens);
     81 
     82   int field_number;
     83   if (token_count != 2 || !base::StringToInt(tokens[0], &field_number)) {
     84     return syncer::UNSPECIFIED;
     85   }
     86 
     87   return syncer::GetModelTypeFromSpecificsFieldNumber(field_number);
     88 }
     89 
     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) {}
     98 
     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_);
    105 
    106   // Data via accessors
    107   sync_entity->set_deleted(IsDeleted());
    108   sync_entity->set_folder(IsFolder());
    109 }
    110 
    111 }  // namespace fake_server
    112