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

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

Issue 187303006: Update sync API to support attachments. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@attachmentapi
Patch Set: Rename GetAttachments to GetLocalAttachmentsForUpload. Created 6 years, 8 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 | « sync/api/sync_data.h ('k') | 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
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"
11 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h" 12 #include "base/values.h"
13 #include "sync/api/attachments/attachment_service_proxy.h"
13 #include "sync/internal_api/public/base/model_type.h" 14 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/base_node.h" 15 #include "sync/internal_api/public/base_node.h"
15 #include "sync/protocol/proto_value_conversions.h" 16 #include "sync/protocol/proto_value_conversions.h"
16 #include "sync/protocol/sync.pb.h" 17 #include "sync/protocol/sync.pb.h"
17 18
19 using syncer::Attachment;
20 using syncer::AttachmentIdList;
21 using syncer::AttachmentList;
22
23 namespace {
24
25 sync_pb::AttachmentIdProto AttachmentToProto(
26 const syncer::Attachment& attachment) {
27 return attachment.GetId().GetProto();
28 }
29
30 sync_pb::AttachmentIdProto IdToProto(
31 const syncer::AttachmentId& attachment_id) {
32 return attachment_id.GetProto();
33 }
34
35 syncer::AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) {
36 return syncer::AttachmentId::CreateFromProto(proto);
37 }
38
39 } // namespace
40
18 namespace syncer { 41 namespace syncer {
19 42
20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( 43 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) {
21 Wrapper* wrapper) {
22 *wrapper = new sync_pb::SyncEntity(); 44 *wrapper = new sync_pb::SyncEntity();
23 } 45 }
24 46
25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( 47 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(Wrapper* wrapper) {
26 Wrapper* wrapper) {
27 delete *wrapper; 48 delete *wrapper;
28 } 49 }
29 50
30 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap( 51 const sync_pb::SyncEntity& SyncData::ImmutableSyncEntityTraits::Unwrap(
31 const Wrapper& wrapper) { 52 const Wrapper& wrapper) {
32 return *wrapper; 53 return *wrapper;
33 } 54 }
34 55
35 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable( 56 sync_pb::SyncEntity* SyncData::ImmutableSyncEntityTraits::UnwrapMutable(
36 Wrapper* wrapper) { 57 Wrapper* wrapper) {
37 return *wrapper; 58 return *wrapper;
38 } 59 }
39 60
40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, 61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1,
41 sync_pb::SyncEntity* t2) { 62 sync_pb::SyncEntity* t2) {
42 t1->Swap(t2); 63 t1->Swap(t2);
43 } 64 }
44 65
45 SyncData::SyncData() 66 SyncData::SyncData() : is_valid_(false), id_(kInvalidId) {}
46 : is_valid_(false),
47 id_(kInvalidId) {}
48 67
49 SyncData::SyncData(int64 id, 68 SyncData::SyncData(
50 sync_pb::SyncEntity* entity, 69 int64 id,
51 const base::Time& remote_modification_time) 70 sync_pb::SyncEntity* entity,
71 AttachmentList* attachments,
72 const base::Time& remote_modification_time,
73 const syncer::AttachmentServiceProxy& attachment_service)
52 : is_valid_(true), 74 : is_valid_(true),
53 id_(id), 75 id_(id),
54 remote_modification_time_(remote_modification_time), 76 remote_modification_time_(remote_modification_time),
55 immutable_entity_(entity) {} 77 immutable_entity_(entity),
78 attachments_(attachments),
79 attachment_service_(attachment_service) {}
56 80
57 SyncData::~SyncData() {} 81 SyncData::~SyncData() {}
58 82
59 // Static. 83 // Static.
60 SyncData SyncData::CreateLocalDelete( 84 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag,
61 const std::string& sync_tag, 85 ModelType datatype) {
62 ModelType datatype) {
63 sync_pb::EntitySpecifics specifics; 86 sync_pb::EntitySpecifics specifics;
64 AddDefaultFieldValue(datatype, &specifics); 87 AddDefaultFieldValue(datatype, &specifics);
65 return CreateLocalData(sync_tag, std::string(), specifics); 88 return CreateLocalData(sync_tag, std::string(), specifics);
66 } 89 }
67 90
68 // Static. 91 // Static.
69 SyncData SyncData::CreateLocalData( 92 SyncData SyncData::CreateLocalData(const std::string& sync_tag,
93 const std::string& non_unique_title,
94 const sync_pb::EntitySpecifics& specifics) {
95 syncer::AttachmentList attachments;
96 return CreateLocalDataWithAttachments(
97 sync_tag, non_unique_title, specifics, attachments);
98 }
99
100 // TODO(maniscalco): What should happen if the same Attachment appears in the
101 // list twice? Document the behavior and add a test case.
102 //
103 // Static.
104 SyncData SyncData::CreateLocalDataWithAttachments(
70 const std::string& sync_tag, 105 const std::string& sync_tag,
71 const std::string& non_unique_title, 106 const std::string& non_unique_title,
72 const sync_pb::EntitySpecifics& specifics) { 107 const sync_pb::EntitySpecifics& specifics,
108 const AttachmentList& attachments) {
73 sync_pb::SyncEntity entity; 109 sync_pb::SyncEntity entity;
74 entity.set_client_defined_unique_tag(sync_tag); 110 entity.set_client_defined_unique_tag(sync_tag);
75 entity.set_non_unique_name(non_unique_title); 111 entity.set_non_unique_name(non_unique_title);
76 entity.mutable_specifics()->CopyFrom(specifics); 112 entity.mutable_specifics()->CopyFrom(specifics);
77 return SyncData(kInvalidId, &entity, base::Time()); 113 std::transform(attachments.begin(),
114 attachments.end(),
115 RepeatedFieldBackInserter(entity.mutable_attachment_id()),
116 AttachmentToProto);
117 // TODO(maniscalco): Actually pass the attachments to the ctor and make them
118 // available to the AttachmentService once this SyncData gets passed into
119 // GenericChangeProcesso::ProcessSyncChanges (bug 354530).
120 AttachmentList copy_of_attachments(attachments);
121 return SyncData(kInvalidId,
122 &entity,
123 &copy_of_attachments,
124 base::Time(),
125 AttachmentServiceProxy());
78 } 126 }
79 127
80 // Static. 128 // Static.
81 SyncData SyncData::CreateRemoteData( 129 SyncData SyncData::CreateRemoteData(
82 int64 id, const sync_pb::EntitySpecifics& specifics, 130 int64 id,
83 const base::Time& modification_time) { 131 const sync_pb::EntitySpecifics& specifics,
132 const base::Time& modification_time,
133 const AttachmentIdList& attachment_ids,
134 const AttachmentServiceProxy& attachment_service) {
84 DCHECK_NE(id, kInvalidId); 135 DCHECK_NE(id, kInvalidId);
85 sync_pb::SyncEntity entity; 136 sync_pb::SyncEntity entity;
86 entity.mutable_specifics()->CopyFrom(specifics); 137 entity.mutable_specifics()->CopyFrom(specifics);
87 return SyncData(id, &entity, modification_time); 138 std::transform(attachment_ids.begin(),
139 attachment_ids.end(),
140 RepeatedFieldBackInserter(entity.mutable_attachment_id()),
141 IdToProto);
142 AttachmentList attachments;
143 return SyncData(
144 id, &entity, &attachments, modification_time, attachment_service);
88 } 145 }
89 146
90 bool SyncData::IsValid() const { 147 // Static.
91 return is_valid_; 148 SyncData SyncData::CreateRemoteData(int64 id,
149 const sync_pb::EntitySpecifics& specifics,
150 const base::Time& modification_time) {
151 return CreateRemoteData(id,
152 specifics,
153 modification_time,
154 AttachmentIdList(),
155 AttachmentServiceProxy());
92 } 156 }
93 157
158 bool SyncData::IsValid() const { return is_valid_; }
159
94 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { 160 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const {
95 return immutable_entity_.Get().specifics(); 161 return immutable_entity_.Get().specifics();
96 } 162 }
97 163
98 ModelType SyncData::GetDataType() const { 164 ModelType SyncData::GetDataType() const {
99 return GetModelTypeFromSpecifics(GetSpecifics()); 165 return GetModelTypeFromSpecifics(GetSpecifics());
100 } 166 }
101 167
102 const std::string& SyncData::GetTag() const { 168 const std::string& SyncData::GetTag() const {
103 DCHECK(IsLocal()); 169 DCHECK(IsLocal());
104 return immutable_entity_.Get().client_defined_unique_tag(); 170 return immutable_entity_.Get().client_defined_unique_tag();
105 } 171 }
106 172
107 const std::string& SyncData::GetTitle() const { 173 const std::string& SyncData::GetTitle() const {
108 // TODO(zea): set this for data coming from the syncer too. 174 // TODO(zea): set this for data coming from the syncer too.
109 DCHECK(immutable_entity_.Get().has_non_unique_name()); 175 DCHECK(immutable_entity_.Get().has_non_unique_name());
110 return immutable_entity_.Get().non_unique_name(); 176 return immutable_entity_.Get().non_unique_name();
111 } 177 }
112 178
113 const base::Time& SyncData::GetRemoteModifiedTime() const { 179 const base::Time& SyncData::GetRemoteModifiedTime() const {
114 DCHECK(!IsLocal()); 180 DCHECK(!IsLocal());
115 return remote_modification_time_; 181 return remote_modification_time_;
116 } 182 }
117 183
118 int64 SyncData::GetRemoteId() const { 184 int64 SyncData::GetRemoteId() const {
119 DCHECK(!IsLocal()); 185 DCHECK(!IsLocal());
120 return id_; 186 return id_;
121 } 187 }
122 188
123 bool SyncData::IsLocal() const { 189 bool SyncData::IsLocal() const { return id_ == kInvalidId; }
124 return id_ == kInvalidId;
125 }
126 190
127 std::string SyncData::ToString() const { 191 std::string SyncData::ToString() const {
128 if (!IsValid()) 192 if (!IsValid())
129 return "<Invalid SyncData>"; 193 return "<Invalid SyncData>";
130 194
131 std::string type = ModelTypeToString(GetDataType()); 195 std::string type = ModelTypeToString(GetDataType());
132 std::string specifics; 196 std::string specifics;
133 scoped_ptr<base::DictionaryValue> value( 197 scoped_ptr<base::DictionaryValue> value(
134 EntitySpecificsToValue(GetSpecifics())); 198 EntitySpecificsToValue(GetSpecifics()));
135 base::JSONWriter::WriteWithOptions(value.get(), 199 base::JSONWriter::WriteWithOptions(
136 base::JSONWriter::OPTIONS_PRETTY_PRINT, 200 value.get(), base::JSONWriter::OPTIONS_PRETTY_PRINT, &specifics);
137 &specifics);
138 201
139 if (IsLocal()) { 202 if (IsLocal()) {
140 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() + 203 return "{ isLocal: true, type: " + type + ", tag: " + GetTag() +
141 ", title: " + GetTitle() + ", specifics: " + specifics + "}"; 204 ", title: " + GetTitle() + ", specifics: " + specifics + "}";
142 } 205 }
143 206
144 std::string id = base::Int64ToString(GetRemoteId()); 207 std::string id = base::Int64ToString(GetRemoteId());
145 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + 208 return "{ isLocal: false, type: " + type + ", specifics: " + specifics +
146 ", id: " + id + "}"; 209 ", id: " + id + "}";
147 } 210 }
148 211
149 void PrintTo(const SyncData& sync_data, std::ostream* os) { 212 void PrintTo(const SyncData& sync_data, std::ostream* os) {
150 *os << sync_data.ToString(); 213 *os << sync_data.ToString();
151 } 214 }
152 215
216 AttachmentIdList SyncData::GetAttachmentIds() const {
217 AttachmentIdList result;
218 const sync_pb::SyncEntity& entity = immutable_entity_.Get();
219 std::transform(entity.attachment_id().begin(),
220 entity.attachment_id().end(),
221 std::back_inserter(result),
222 ProtoToId);
223 return result;
224 }
225
226 const AttachmentList& SyncData::GetLocalAttachmentsForUpload() const {
227 DCHECK(IsLocal());
228 return attachments_.Get();
229 }
230
231 void SyncData::GetOrDownloadAttachments(
232 const AttachmentIdList& attachment_ids,
233 const AttachmentService::GetOrDownloadCallback& callback) {
234 DCHECK(!IsLocal());
235 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback);
236 }
237
238 void SyncData::DropAttachments(
239 const AttachmentIdList& attachment_ids,
240 const AttachmentService::DropCallback& callback) {
241 DCHECK(!IsLocal());
242 attachment_service_.DropAttachments(attachment_ids, callback);
243 }
244
153 } // namespace syncer 245 } // namespace syncer
OLDNEW
« no previous file with comments | « 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