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