summaryrefslogtreecommitdiffstats
path: root/sync/api/sync_data.cc
blob: 679c54771979fea5fbf8fded8d6d29fb891b2261 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/api/sync_data.h"

#include <ostream>

#include "base/json/json_writer.h"
#include "base/memory/scoped_ptr.h"
#include "base/string_number_conversions.h"
#include "base/values.h"
#include "sync/internal_api/base_node.h"
#include "sync/protocol/proto_value_conversions.h"
#include "sync/protocol/sync.pb.h"
#include "sync/syncable/model_type.h"

void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(
    Wrapper* wrapper) {
  *wrapper = new sync_pb::SyncEntity();
}

void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(
    Wrapper* wrapper) {
  delete *wrapper;
}

const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap(
    const Wrapper& wrapper) {
  return *wrapper;
}

sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
    Wrapper* wrapper) {
  return *wrapper;
}

void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
                                               sync_pb::SyncEntity* t2) {
  t1->Swap(t2);
}

SyncData::SyncData()
    : is_valid_(false),
      id_(sync_api::kInvalidId) {}

SyncData::SyncData(int64 id, sync_pb::SyncEntity* entity)
    : is_valid_(true),
      id_(id),
      immutable_entity_(entity) {}

SyncData::~SyncData() {}

// Static.
SyncData SyncData::CreateLocalDelete(
    const std::string& sync_tag,
    syncable::ModelType datatype) {
  sync_pb::EntitySpecifics specifics;
  syncable::AddDefaultFieldValue(datatype, &specifics);
  return CreateLocalData(sync_tag, "", specifics);
}

// Static.
SyncData SyncData::CreateLocalData(
    const std::string& sync_tag,
    const std::string& non_unique_title,
    const sync_pb::EntitySpecifics& specifics) {
  sync_pb::SyncEntity entity;
  entity.set_client_defined_unique_tag(sync_tag);
  entity.set_non_unique_name(non_unique_title);
  entity.mutable_specifics()->CopyFrom(specifics);
  return SyncData(sync_api::kInvalidId, &entity);
}

// Static.
SyncData SyncData::CreateRemoteData(
    int64 id, const sync_pb::EntitySpecifics& specifics) {
  DCHECK_NE(id, sync_api::kInvalidId);
  sync_pb::SyncEntity entity;
  entity.mutable_specifics()->CopyFrom(specifics);
  return SyncData(id, &entity);
}

bool SyncData::IsValid() const {
  return is_valid_;
}

const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
  return immutable_entity_.Get().specifics();
}

syncable::ModelType SyncData::GetDataType() const {
  return syncable::GetModelTypeFromSpecifics(GetSpecifics());
}

const std::string& SyncData::GetTag() const {
  DCHECK(IsLocal());
  return immutable_entity_.Get().client_defined_unique_tag();
}

const std::string& SyncData::GetTitle() const {
  // TODO(zea): set this for data coming from the syncer too.
  DCHECK(immutable_entity_.Get().has_non_unique_name());
  return immutable_entity_.Get().non_unique_name();
}

int64 SyncData::GetRemoteId() const {
  DCHECK(!IsLocal());
  return id_;
}

bool SyncData::IsLocal() const {
  return id_ == sync_api::kInvalidId;
}

std::string SyncData::ToString() const {
  if (!IsValid())
    return "<Invalid SyncData>";

  std::string type = syncable::ModelTypeToString(GetDataType());
  std::string specifics;
  scoped_ptr<DictionaryValue> value(
      browser_sync::EntitySpecificsToValue(GetSpecifics()));
  base::JSONWriter::WriteWithOptions(value.get(),
                                     base::JSONWriter::OPTIONS_PRETTY_PRINT,
                                     &specifics);

  if (IsLocal()) {
    return "{ isLocal: true, type: " + type + ", tag: " + GetTag() +
        ", title: " + GetTitle() + ", specifics: " + specifics + "}";
  }

  std::string id = base::Int64ToString(GetRemoteId());
  return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
      ", id: " + id + "}";
}

void PrintTo(const SyncData& sync_data, std::ostream* os) {
  *os << sync_data.ToString();
}