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

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

Issue 217063005: Separate SyncData methods into three groups, local, remote, and common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@syncapi
Patch Set: Created 6 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sync/api/sync_data.h" 5 #include "sync/api/sync_data.h"
6 6
7 #include <ostream> 7 #include <ostream>
8 8
9 #include "base/json/json_writer.h" 9 #include "base/json/json_writer.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
56 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( 56 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
57 Wrapper* wrapper) { 57 Wrapper* wrapper) {
58 return *wrapper; 58 return *wrapper;
59 } 59 }
60 60
61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, 61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
62 sync_pb::SyncEntity* t2) { 62 sync_pb::SyncEntity* t2) {
63 t1->Swap(t2); 63 t1->Swap(t2);
64 } 64 }
65 65
66 SyncData::SyncData() : is_valid_(false), id_(kInvalidId) {} 66 SyncData::SyncData()
67 : local_(NULL), remote_(NULL), is_valid_(false), id_(kInvalidId) {}
67 68
68 SyncData::SyncData( 69 SyncData::SyncData(int64 id,
69 int64 id, 70 sync_pb::SyncEntity* entity,
70 sync_pb::SyncEntity* entity, 71 AttachmentList* attachments,
71 AttachmentList* attachments, 72 const base::Time& remote_modification_time,
72 const base::Time& remote_modification_time, 73 const syncer::AttachmentServiceProxy& attachment_service)
73 const syncer::AttachmentServiceProxy& attachment_service) 74 : local_(this),
74 : is_valid_(true), 75 remote_(this),
76 is_valid_(true),
75 id_(id), 77 id_(id),
76 remote_modification_time_(remote_modification_time), 78 remote_modification_time_(remote_modification_time),
77 immutable_entity_(entity), 79 immutable_entity_(entity),
78 attachments_(attachments), 80 attachments_(attachments),
79 attachment_service_(attachment_service) {} 81 attachment_service_(attachment_service) {
82 if (IsLocal()) {
83 remote_ = Remote(NULL);
84 } else {
85 local_ = Local(NULL);
86 }
87 }
80 88
81 SyncData::~SyncData() {} 89 SyncData::~SyncData() {}
82 90
83 // Static. 91 // Static.
84 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag, 92 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag,
85 ModelType datatype) { 93 ModelType datatype) {
86 sync_pb::EntitySpecifics specifics; 94 sync_pb::EntitySpecifics specifics;
87 AddDefaultFieldValue(datatype, &specifics); 95 AddDefaultFieldValue(datatype, &specifics);
88 return CreateLocalData(sync_tag, std::string(), specifics); 96 return CreateLocalData(sync_tag, std::string(), specifics);
89 } 97 }
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 bool SyncData::IsValid() const { return is_valid_; } 166 bool SyncData::IsValid() const { return is_valid_; }
159 167
160 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { 168 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
161 return immutable_entity_.Get().specifics(); 169 return immutable_entity_.Get().specifics();
162 } 170 }
163 171
164 ModelType SyncData::GetDataType() const { 172 ModelType SyncData::GetDataType() const {
165 return GetModelTypeFromSpecifics(GetSpecifics()); 173 return GetModelTypeFromSpecifics(GetSpecifics());
166 } 174 }
167 175
168 const std::string& SyncData::GetTag() const {
169 DCHECK(IsLocal());
170 return immutable_entity_.Get().client_defined_unique_tag();
171 }
172
173 const std::string& SyncData::GetTitle() const { 176 const std::string& SyncData::GetTitle() const {
174 // TODO(zea): set this for data coming from the syncer too. 177 // TODO(zea): set this for data coming from the syncer too.
175 DCHECK(immutable_entity_.Get().has_non_unique_name()); 178 DCHECK(immutable_entity_.Get().has_non_unique_name());
176 return immutable_entity_.Get().non_unique_name(); 179 return immutable_entity_.Get().non_unique_name();
177 } 180 }
178 181
179 const base::Time& SyncData::GetRemoteModifiedTime() const {
180 DCHECK(!IsLocal());
181 return remote_modification_time_;
182 }
183
184 int64 SyncData::GetRemoteId() const {
185 DCHECK(!IsLocal());
186 return id_;
187 }
188
189 bool SyncData::IsLocal() const { return id_ == kInvalidId; } 182 bool SyncData::IsLocal() const { return id_ == kInvalidId; }
190 183
191 std::string SyncData::ToString() const { 184 std::string SyncData::ToString() const {
192 if (!IsValid()) 185 if (!IsValid())
193 return "<Invalid SyncData>"; 186 return "<Invalid SyncData>";
194 187
195 std::string type = ModelTypeToString(GetDataType()); 188 std::string type = ModelTypeToString(GetDataType());
196 std::string specifics; 189 std::string specifics;
197 scoped_ptr<base::DictionaryValue> value( 190 scoped_ptr<base::DictionaryValue> value(
198 EntitySpecificsToValue(GetSpecifics())); 191 EntitySpecificsToValue(GetSpecifics()));
199 base::JSONWriter::WriteWithOptions( 192 base::JSONWriter::WriteWithOptions(
200 value.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &specifics); 193 value.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &specifics);
201 194
202 if (IsLocal()) { 195 if (IsLocal()) {
203 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + 196 return "{ isLocal: true, type: " + type + ", tag: " + local().GetTag() +
204 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; 197 ", title: " + GetTitle() + ", specifics: " + specifics + "}";
205 } 198 }
206 199
207 std::string id = base::Int64ToString(GetRemoteId()); 200 std::string id = base::Int64ToString(remote().GetRemoteId());
208 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + 201 return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
209 ", id: " + id + "}"; 202 ", id: " + id + "}";
210 } 203 }
211 204
212 void PrintTo(const SyncData& sync_data, std::ostream* os) { 205 void PrintTo(const SyncData& sync_data, std::ostream* os) {
213 *os << sync_data.ToString(); 206 *os << sync_data.ToString();
214 } 207 }
215 208
216 AttachmentIdList SyncData::GetAttachmentIds() const { 209 AttachmentIdList SyncData::GetAttachmentIds() const {
217 AttachmentIdList result; 210 AttachmentIdList result;
218 const sync_pb::SyncEntity& entity = immutable_entity_.Get(); 211 const sync_pb::SyncEntity& entity = immutable_entity_.Get();
219 std::transform(entity.attachment_id().begin(), 212 std::transform(entity.attachment_id().begin(),
220 entity.attachment_id().end(), 213 entity.attachment_id().end(),
221 std::back_inserter(result), 214 std::back_inserter(result),
222 ProtoToId); 215 ProtoToId);
223 return result; 216 return result;
224 } 217 }
225 218
226 const AttachmentList& SyncData::GetLocalAttachmentsForUpload() const { 219 const AttachmentList& SyncData::Local::GetLocalAttachmentsForUpload() const {
227 DCHECK(IsLocal()); 220 return sync_data_->attachments_.Get();
228 return attachments_.Get();
229 } 221 }
230 222
231 void SyncData::GetOrDownloadAttachments( 223 const std::string& SyncData::Local::GetTag() const {
224 return sync_data_->immutable_entity_.Get().client_defined_unique_tag();
225 }
226
227 const SyncData::Local& SyncData::local() const {
228 DCHECK(IsLocal());
229 return local_;
230 }
231
232 const base::Time& SyncData::Remote::GetRemoteModifiedTime() const {
233 return sync_data_->remote_modification_time_;
234 }
235
236 int64 SyncData::Remote::GetRemoteId() const {
237 return sync_data_->id_;
238 }
239
240 void SyncData::Remote::GetOrDownloadAttachments(
232 const AttachmentIdList& attachment_ids, 241 const AttachmentIdList& attachment_ids,
233 const AttachmentService::GetOrDownloadCallback& callback) { 242 const AttachmentService::GetOrDownloadCallback& callback) {
234 DCHECK(!IsLocal()); 243 sync_data_->attachment_service_.GetOrDownloadAttachments(attachment_ids,
235 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); 244 callback);
236 } 245 }
237 246
238 void SyncData::DropAttachments( 247 void SyncData::Remote::DropAttachments(
239 const AttachmentIdList& attachment_ids, 248 const AttachmentIdList& attachment_ids,
240 const AttachmentService::DropCallback& callback) { 249 const AttachmentService::DropCallback& callback) {
250 sync_data_->attachment_service_.DropAttachments(attachment_ids, callback);
251 }
252
253 SyncData::Remote& SyncData::remote() {
241 DCHECK(!IsLocal()); 254 DCHECK(!IsLocal());
242 attachment_service_.DropAttachments(attachment_ids, callback); 255 return remote_;
256 }
257
258 const SyncData::Remote& SyncData::remote() const {
259 DCHECK(!IsLocal());
260 return remote_;
243 } 261 }
244 262
245 } // namespace syncer 263 } // namespace syncer
OLDNEW
« sync/api/sync_data.h ('K') | « sync/api/sync_data.h ('k') | sync/api/sync_data_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698