Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(120)

Side by Side Diff: components/sync/api/sync_data.cc

Issue 2401223002: [Sync] Renaming sync/api* to sync/model*. (Closed)
Patch Set: Missed a comment in a DEPS file, and rebasing. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/sync/api/sync_data.h ('k') | components/sync/api/sync_data_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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 "components/sync/api/sync_data.h"
6
7 #include <algorithm>
8 #include <ostream>
9
10 #include "base/json/json_writer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "components/sync/core/base_node.h"
14 #include "components/sync/protocol/proto_value_conversions.h"
15 #include "components/sync/protocol/sync.pb.h"
16
17 namespace syncer {
18 namespace {
19
20 sync_pb::AttachmentIdProto IdToProto(const AttachmentId& attachment_id) {
21 return attachment_id.GetProto();
22 }
23
24 AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) {
25 return AttachmentId::CreateFromProto(proto);
26 }
27
28 // Return true iff |attachment_ids| contains duplicates.
29 bool ContainsDuplicateAttachments(const AttachmentIdList& attachment_ids) {
30 AttachmentIdSet id_set;
31 id_set.insert(attachment_ids.begin(), attachment_ids.end());
32 return id_set.size() != attachment_ids.size();
33 }
34
35 } // namespace
36
37 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) {
38 *wrapper = new sync_pb::SyncEntity();
39 }
40
41 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(Wrapper* wrapper) {
42 delete *wrapper;
43 }
44
45 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap(
46 const Wrapper& wrapper) {
47 return *wrapper;
48 }
49
50 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
51 Wrapper* wrapper) {
52 return *wrapper;
53 }
54
55 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
56 sync_pb::SyncEntity* t2) {
57 t1->Swap(t2);
58 }
59
60 SyncData::SyncData() : id_(kInvalidId), is_valid_(false) {}
61
62 SyncData::SyncData(int64_t id,
63 sync_pb::SyncEntity* entity,
64 const base::Time& remote_modification_time,
65 const AttachmentServiceProxy& attachment_service)
66 : id_(id),
67 remote_modification_time_(remote_modification_time),
68 immutable_entity_(entity),
69 attachment_service_(attachment_service),
70 is_valid_(true) {}
71
72 SyncData::SyncData(const SyncData& other) = default;
73
74 SyncData::~SyncData() {}
75
76 // Static.
77 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag,
78 ModelType datatype) {
79 sync_pb::EntitySpecifics specifics;
80 AddDefaultFieldValue(datatype, &specifics);
81 return CreateLocalData(sync_tag, std::string(), specifics);
82 }
83
84 // Static.
85 SyncData SyncData::CreateLocalData(const std::string& sync_tag,
86 const std::string& non_unique_title,
87 const sync_pb::EntitySpecifics& specifics) {
88 AttachmentIdList attachment_ids;
89 return CreateLocalDataWithAttachments(sync_tag, non_unique_title, specifics,
90 attachment_ids);
91 }
92
93 // Static.
94 SyncData SyncData::CreateLocalDataWithAttachments(
95 const std::string& sync_tag,
96 const std::string& non_unique_title,
97 const sync_pb::EntitySpecifics& specifics,
98 const AttachmentIdList& attachment_ids) {
99 DCHECK(!ContainsDuplicateAttachments(attachment_ids));
100 sync_pb::SyncEntity entity;
101 entity.set_client_defined_unique_tag(sync_tag);
102 entity.set_non_unique_name(non_unique_title);
103 entity.mutable_specifics()->CopyFrom(specifics);
104 std::transform(attachment_ids.begin(), attachment_ids.end(),
105 RepeatedFieldBackInserter(entity.mutable_attachment_id()),
106 IdToProto);
107 return SyncData(kInvalidId, &entity, base::Time(), AttachmentServiceProxy());
108 }
109
110 // Static.
111 SyncData SyncData::CreateRemoteData(
112 int64_t id,
113 const sync_pb::EntitySpecifics& specifics,
114 const base::Time& modification_time,
115 const AttachmentIdList& attachment_ids,
116 const AttachmentServiceProxy& attachment_service,
117 const std::string& client_tag_hash) {
118 DCHECK_NE(id, kInvalidId);
119 sync_pb::SyncEntity entity;
120 entity.mutable_specifics()->CopyFrom(specifics);
121 entity.set_client_defined_unique_tag(client_tag_hash);
122 std::transform(attachment_ids.begin(), attachment_ids.end(),
123 RepeatedFieldBackInserter(entity.mutable_attachment_id()),
124 IdToProto);
125 return SyncData(id, &entity, modification_time, attachment_service);
126 }
127
128 bool SyncData::IsValid() const {
129 return is_valid_;
130 }
131
132 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
133 return immutable_entity_.Get().specifics();
134 }
135
136 ModelType SyncData::GetDataType() const {
137 return GetModelTypeFromSpecifics(GetSpecifics());
138 }
139
140 const std::string& SyncData::GetTitle() const {
141 // TODO(zea): set this for data coming from the syncer too.
142 DCHECK(immutable_entity_.Get().has_non_unique_name());
143 return immutable_entity_.Get().non_unique_name();
144 }
145
146 bool SyncData::IsLocal() const {
147 return id_ == kInvalidId;
148 }
149
150 std::string SyncData::ToString() const {
151 if (!IsValid())
152 return "<Invalid SyncData>";
153
154 std::string type = ModelTypeToString(GetDataType());
155 std::string specifics;
156 base::JSONWriter::WriteWithOptions(*EntitySpecificsToValue(GetSpecifics()),
157 base::JSONWriter::OPTIONS_PRETTY_PRINT,
158 &specifics);
159
160 if (IsLocal()) {
161 SyncDataLocal sync_data_local(*this);
162 return "{ isLocal: true, type: " + type + ", tag: " +
163 sync_data_local.GetTag() + ", title: " + GetTitle() +
164 ", specifics: " + specifics + "}";
165 }
166
167 SyncDataRemote sync_data_remote(*this);
168 std::string id = base::Int64ToString(sync_data_remote.GetId());
169 return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
170 ", id: " + id + "}";
171 }
172
173 void PrintTo(const SyncData& sync_data, std::ostream* os) {
174 *os << sync_data.ToString();
175 }
176
177 AttachmentIdList SyncData::GetAttachmentIds() const {
178 AttachmentIdList result;
179 const sync_pb::SyncEntity& entity = immutable_entity_.Get();
180 std::transform(entity.attachment_id().begin(), entity.attachment_id().end(),
181 std::back_inserter(result), ProtoToId);
182 return result;
183 }
184
185 SyncDataLocal::SyncDataLocal(const SyncData& sync_data) : SyncData(sync_data) {
186 DCHECK(sync_data.IsLocal());
187 }
188
189 SyncDataLocal::~SyncDataLocal() {}
190
191 const std::string& SyncDataLocal::GetTag() const {
192 return immutable_entity_.Get().client_defined_unique_tag();
193 }
194
195 SyncDataRemote::SyncDataRemote(const SyncData& sync_data)
196 : SyncData(sync_data) {
197 DCHECK(!sync_data.IsLocal());
198 }
199
200 SyncDataRemote::~SyncDataRemote() {}
201
202 const base::Time& SyncDataRemote::GetModifiedTime() const {
203 return remote_modification_time_;
204 }
205
206 int64_t SyncDataRemote::GetId() const {
207 return id_;
208 }
209
210 const std::string& SyncDataRemote::GetClientTagHash() const {
211 // It seems that client_defined_unique_tag has a bit of an overloaded use,
212 // holding onto the un-hashed tag while local, and then the hashed value when
213 // communicating with the server. This usage is copying the latter of these
214 // cases, where this is the hashed tag value. The original tag is not sent to
215 // the server so we wouldn't be able to set this value anyways. The only way
216 // to recreate an un-hashed tag is for the service to do so with a specifics.
217 // Should only be used by sessions, see crbug.com/604657.
218 DCHECK_EQ(SESSIONS, GetDataType());
219 return immutable_entity_.Get().client_defined_unique_tag();
220 }
221
222 void SyncDataRemote::GetOrDownloadAttachments(
223 const AttachmentIdList& attachment_ids,
224 const AttachmentService::GetOrDownloadCallback& callback) {
225 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback);
226 }
227
228 } // namespace syncer
OLDNEW
« no previous file with comments | « components/sync/api/sync_data.h ('k') | components/sync/api/sync_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698