Home | History | Annotate | Download | only in api
      1 // Copyright (c) 2012 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/api/sync_data.h"
      6 
      7 #include <ostream>
      8 
      9 #include "base/json/json_writer.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/strings/string_number_conversions.h"
     12 #include "base/values.h"
     13 #include "sync/internal_api/public/base/model_type.h"
     14 #include "sync/internal_api/public/base_node.h"
     15 #include "sync/protocol/proto_value_conversions.h"
     16 #include "sync/protocol/sync.pb.h"
     17 
     18 namespace syncer {
     19 
     20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(
     21     Wrapper* wrapper) {
     22   *wrapper = new sync_pb::SyncEntity();
     23 }
     24 
     25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(
     26     Wrapper* wrapper) {
     27   delete *wrapper;
     28 }
     29 
     30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap(
     31     const Wrapper& wrapper) {
     32   return *wrapper;
     33 }
     34 
     35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
     36     Wrapper* wrapper) {
     37   return *wrapper;
     38 }
     39 
     40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
     41                                                sync_pb::SyncEntity* t2) {
     42   t1->Swap(t2);
     43 }
     44 
     45 SyncData::SyncData()
     46     : is_valid_(false),
     47       id_(kInvalidId) {}
     48 
     49 SyncData::SyncData(int64 id,
     50                    sync_pb::SyncEntity* entity,
     51                    const base::Time& remote_modification_time)
     52     : is_valid_(true),
     53       id_(id),
     54       remote_modification_time_(remote_modification_time),
     55       immutable_entity_(entity) {}
     56 
     57 SyncData::~SyncData() {}
     58 
     59 // Static.
     60 SyncData SyncData::CreateLocalDelete(
     61     const std::string& sync_tag,
     62     ModelType datatype) {
     63   sync_pb::EntitySpecifics specifics;
     64   AddDefaultFieldValue(datatype, &specifics);
     65   return CreateLocalData(sync_tag, std::string(), specifics);
     66 }
     67 
     68 // Static.
     69 SyncData SyncData::CreateLocalData(
     70     const std::string& sync_tag,
     71     const std::string& non_unique_title,
     72     const sync_pb::EntitySpecifics& specifics) {
     73   sync_pb::SyncEntity entity;
     74   entity.set_client_defined_unique_tag(sync_tag);
     75   entity.set_non_unique_name(non_unique_title);
     76   entity.mutable_specifics()->CopyFrom(specifics);
     77   return SyncData(kInvalidId, &entity, base::Time());
     78 }
     79 
     80 // Static.
     81 SyncData SyncData::CreateRemoteData(
     82     int64 id, const sync_pb::EntitySpecifics& specifics,
     83     const base::Time& modification_time) {
     84   DCHECK_NE(id, kInvalidId);
     85   sync_pb::SyncEntity entity;
     86   entity.mutable_specifics()->CopyFrom(specifics);
     87   return SyncData(id, &entity, modification_time);
     88 }
     89 
     90 bool SyncData::IsValid() const {
     91   return is_valid_;
     92 }
     93 
     94 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
     95   return immutable_entity_.Get().specifics();
     96 }
     97 
     98 ModelType SyncData::GetDataType() const {
     99   return GetModelTypeFromSpecifics(GetSpecifics());
    100 }
    101 
    102 const std::string& SyncData::GetTag() const {
    103   DCHECK(IsLocal());
    104   return immutable_entity_.Get().client_defined_unique_tag();
    105 }
    106 
    107 const std::string& SyncData::GetTitle() const {
    108   // TODO(zea): set this for data coming from the syncer too.
    109   DCHECK(immutable_entity_.Get().has_non_unique_name());
    110   return immutable_entity_.Get().non_unique_name();
    111 }
    112 
    113 const base::Time& SyncData::GetRemoteModifiedTime() const {
    114   DCHECK(!IsLocal());
    115   return remote_modification_time_;
    116 }
    117 
    118 int64 SyncData::GetRemoteId() const {
    119   DCHECK(!IsLocal());
    120   return id_;
    121 }
    122 
    123 bool SyncData::IsLocal() const {
    124   return id_ == kInvalidId;
    125 }
    126 
    127 std::string SyncData::ToString() const {
    128   if (!IsValid())
    129     return "<Invalid SyncData>";
    130 
    131   std::string type = ModelTypeToString(GetDataType());
    132   std::string specifics;
    133   scoped_ptr<base::DictionaryValue> value(
    134       EntitySpecificsToValue(GetSpecifics()));
    135   base::JSONWriter::WriteWithOptions(value.get(),
    136                                      base::JSONWriter::OPTIONS_PRETTY_PRINT,
    137                                      &specifics);
    138 
    139   if (IsLocal()) {
    140     return "{ isLocal: true, type: " + type + ", tag: " + GetTag() +
    141         ", title: " + GetTitle() + ", specifics: " + specifics + "}";
    142   }
    143 
    144   std::string id = base::Int64ToString(GetRemoteId());
    145   return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
    146       ", id: " + id + "}";
    147 }
    148 
    149 void PrintTo(const SyncData& sync_data, std::ostream* os) {
    150   *os << sync_data.ToString();
    151 }
    152 
    153 }  // namespace syncer
    154