OLD | NEW |
---|---|
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/internal_api/public/attachments/attachment_service_proxy.h" | 13 #include "sync/internal_api/public/attachments/attachment_service_proxy.h" |
14 #include "sync/internal_api/public/base/model_type.h" | 14 #include "sync/internal_api/public/base/model_type.h" |
15 #include "sync/internal_api/public/base_node.h" | 15 #include "sync/internal_api/public/base_node.h" |
16 #include "sync/protocol/proto_value_conversions.h" | 16 #include "sync/protocol/proto_value_conversions.h" |
17 #include "sync/protocol/sync.pb.h" | 17 #include "sync/protocol/sync.pb.h" |
18 | 18 |
19 using syncer::Attachment; | |
20 using syncer::AttachmentIdList; | |
21 using syncer::AttachmentList; | |
22 | |
23 namespace { | 19 namespace { |
24 | 20 |
25 sync_pb::AttachmentIdProto AttachmentToProto( | |
26 const syncer::Attachment& attachment) { | |
27 return attachment.GetId().GetProto(); | |
28 } | |
29 | |
30 sync_pb::AttachmentIdProto IdToProto( | 21 sync_pb::AttachmentIdProto IdToProto( |
31 const syncer::AttachmentId& attachment_id) { | 22 const syncer::AttachmentId& attachment_id) { |
32 return attachment_id.GetProto(); | 23 return attachment_id.GetProto(); |
33 } | 24 } |
34 | 25 |
35 syncer::AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) { | 26 syncer::AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) { |
36 return syncer::AttachmentId::CreateFromProto(proto); | 27 return syncer::AttachmentId::CreateFromProto(proto); |
37 } | 28 } |
38 | 29 |
39 // Return true iff |attachments| contains one or more elements with the same | 30 // Return true iff |attachments| contains one or more elements with the same |
maniscalco
2014/09/12 23:23:34
|attachments| -> |attachment_ids|
pavely
2014/09/15 17:41:49
Done.
| |
40 // AttachmentId. | 31 // AttachmentId. |
41 bool ContainsDuplicateAttachments(const syncer::AttachmentList& attachments) { | 32 bool ContainsDuplicateAttachments( |
42 std::set<syncer::AttachmentId> id_set; | 33 const syncer::AttachmentIdList& attachment_ids) { |
43 AttachmentList::const_iterator iter = attachments.begin(); | 34 syncer::AttachmentIdSet id_set; |
44 AttachmentList::const_iterator end = attachments.end(); | 35 id_set.insert(attachment_ids.begin(), attachment_ids.end()); |
maniscalco
2014/09/12 23:23:33
Nice!
| |
45 for (; iter != end; ++iter) { | 36 return id_set.size() != attachment_ids.size(); |
46 if (id_set.find(iter->GetId()) != id_set.end()) { | |
47 return true; | |
48 } | |
49 id_set.insert(iter->GetId()); | |
50 } | |
51 return false; | |
52 } | 37 } |
53 | 38 |
54 } // namespace | 39 } // namespace |
55 | 40 |
56 namespace syncer { | 41 namespace syncer { |
57 | 42 |
58 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) { | 43 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) { |
59 *wrapper = new sync_pb::SyncEntity(); | 44 *wrapper = new sync_pb::SyncEntity(); |
60 } | 45 } |
61 | 46 |
(...skipping 13 matching lines...) Expand all Loading... | |
75 | 60 |
76 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, | 61 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, |
77 sync_pb::SyncEntity* t2) { | 62 sync_pb::SyncEntity* t2) { |
78 t1->Swap(t2); | 63 t1->Swap(t2); |
79 } | 64 } |
80 | 65 |
81 SyncData::SyncData() : id_(kInvalidId), is_valid_(false) {} | 66 SyncData::SyncData() : id_(kInvalidId), is_valid_(false) {} |
82 | 67 |
83 SyncData::SyncData(int64 id, | 68 SyncData::SyncData(int64 id, |
84 sync_pb::SyncEntity* entity, | 69 sync_pb::SyncEntity* entity, |
85 AttachmentList* attachments, | |
86 const base::Time& remote_modification_time, | 70 const base::Time& remote_modification_time, |
87 const syncer::AttachmentServiceProxy& attachment_service) | 71 const syncer::AttachmentServiceProxy& attachment_service) |
88 : id_(id), | 72 : id_(id), |
89 remote_modification_time_(remote_modification_time), | 73 remote_modification_time_(remote_modification_time), |
90 immutable_entity_(entity), | 74 immutable_entity_(entity), |
91 attachments_(attachments), | |
92 attachment_service_(attachment_service), | 75 attachment_service_(attachment_service), |
93 is_valid_(true) {} | 76 is_valid_(true) {} |
94 | 77 |
95 SyncData::~SyncData() {} | 78 SyncData::~SyncData() {} |
96 | 79 |
97 // Static. | 80 // Static. |
98 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag, | 81 SyncData SyncData::CreateLocalDelete(const std::string& sync_tag, |
99 ModelType datatype) { | 82 ModelType datatype) { |
100 sync_pb::EntitySpecifics specifics; | 83 sync_pb::EntitySpecifics specifics; |
101 AddDefaultFieldValue(datatype, &specifics); | 84 AddDefaultFieldValue(datatype, &specifics); |
102 return CreateLocalData(sync_tag, std::string(), specifics); | 85 return CreateLocalData(sync_tag, std::string(), specifics); |
103 } | 86 } |
104 | 87 |
105 // Static. | 88 // Static. |
106 SyncData SyncData::CreateLocalData(const std::string& sync_tag, | 89 SyncData SyncData::CreateLocalData(const std::string& sync_tag, |
107 const std::string& non_unique_title, | 90 const std::string& non_unique_title, |
108 const sync_pb::EntitySpecifics& specifics) { | 91 const sync_pb::EntitySpecifics& specifics) { |
109 syncer::AttachmentList attachments; | 92 syncer::AttachmentIdList attachment_ids; |
110 return CreateLocalDataWithAttachments( | 93 return CreateLocalDataWithAttachments( |
111 sync_tag, non_unique_title, specifics, attachments); | 94 sync_tag, non_unique_title, specifics, attachment_ids); |
112 } | 95 } |
113 | 96 |
114 // Static. | 97 // Static. |
115 SyncData SyncData::CreateLocalDataWithAttachments( | 98 SyncData SyncData::CreateLocalDataWithAttachments( |
116 const std::string& sync_tag, | 99 const std::string& sync_tag, |
117 const std::string& non_unique_title, | 100 const std::string& non_unique_title, |
118 const sync_pb::EntitySpecifics& specifics, | 101 const sync_pb::EntitySpecifics& specifics, |
119 const AttachmentList& attachments) { | 102 const AttachmentIdList& attachment_ids) { |
120 DCHECK(!ContainsDuplicateAttachments(attachments)); | 103 DCHECK(!ContainsDuplicateAttachments(attachment_ids)); |
121 sync_pb::SyncEntity entity; | 104 sync_pb::SyncEntity entity; |
122 entity.set_client_defined_unique_tag(sync_tag); | 105 entity.set_client_defined_unique_tag(sync_tag); |
123 entity.set_non_unique_name(non_unique_title); | 106 entity.set_non_unique_name(non_unique_title); |
124 entity.mutable_specifics()->CopyFrom(specifics); | 107 entity.mutable_specifics()->CopyFrom(specifics); |
125 std::transform(attachments.begin(), | 108 std::transform(attachment_ids.begin(), |
126 attachments.end(), | 109 attachment_ids.end(), |
127 RepeatedFieldBackInserter(entity.mutable_attachment_id()), | 110 RepeatedFieldBackInserter(entity.mutable_attachment_id()), |
128 AttachmentToProto); | 111 IdToProto); |
129 AttachmentList copy_of_attachments(attachments); | |
130 return SyncData(kInvalidId, | 112 return SyncData(kInvalidId, |
131 &entity, | 113 &entity, |
132 ©_of_attachments, | |
133 base::Time(), | 114 base::Time(), |
134 AttachmentServiceProxy()); | 115 AttachmentServiceProxy()); |
135 } | 116 } |
136 | 117 |
137 // Static. | 118 // Static. |
138 SyncData SyncData::CreateRemoteData( | 119 SyncData SyncData::CreateRemoteData( |
139 int64 id, | 120 int64 id, |
140 const sync_pb::EntitySpecifics& specifics, | 121 const sync_pb::EntitySpecifics& specifics, |
141 const base::Time& modification_time, | 122 const base::Time& modification_time, |
142 const AttachmentIdList& attachment_ids, | 123 const AttachmentIdList& attachment_ids, |
143 const AttachmentServiceProxy& attachment_service) { | 124 const AttachmentServiceProxy& attachment_service) { |
144 DCHECK_NE(id, kInvalidId); | 125 DCHECK_NE(id, kInvalidId); |
145 sync_pb::SyncEntity entity; | 126 sync_pb::SyncEntity entity; |
146 entity.mutable_specifics()->CopyFrom(specifics); | 127 entity.mutable_specifics()->CopyFrom(specifics); |
147 std::transform(attachment_ids.begin(), | 128 std::transform(attachment_ids.begin(), |
148 attachment_ids.end(), | 129 attachment_ids.end(), |
149 RepeatedFieldBackInserter(entity.mutable_attachment_id()), | 130 RepeatedFieldBackInserter(entity.mutable_attachment_id()), |
150 IdToProto); | 131 IdToProto); |
151 AttachmentList attachments; | 132 return SyncData(id, &entity, modification_time, attachment_service); |
152 return SyncData( | |
153 id, &entity, &attachments, modification_time, attachment_service); | |
154 } | 133 } |
155 | 134 |
156 bool SyncData::IsValid() const { return is_valid_; } | 135 bool SyncData::IsValid() const { return is_valid_; } |
157 | 136 |
158 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 137 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
159 return immutable_entity_.Get().specifics(); | 138 return immutable_entity_.Get().specifics(); |
160 } | 139 } |
161 | 140 |
162 ModelType SyncData::GetDataType() const { | 141 ModelType SyncData::GetDataType() const { |
163 return GetModelTypeFromSpecifics(GetSpecifics()); | 142 return GetModelTypeFromSpecifics(GetSpecifics()); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
208 ProtoToId); | 187 ProtoToId); |
209 return result; | 188 return result; |
210 } | 189 } |
211 | 190 |
212 SyncDataLocal::SyncDataLocal(const SyncData& sync_data) : SyncData(sync_data) { | 191 SyncDataLocal::SyncDataLocal(const SyncData& sync_data) : SyncData(sync_data) { |
213 DCHECK(sync_data.IsLocal()); | 192 DCHECK(sync_data.IsLocal()); |
214 } | 193 } |
215 | 194 |
216 SyncDataLocal::~SyncDataLocal() {} | 195 SyncDataLocal::~SyncDataLocal() {} |
217 | 196 |
218 const AttachmentList& SyncDataLocal::GetLocalAttachmentsForUpload() const { | |
219 return attachments_.Get(); | |
220 } | |
221 | |
222 const std::string& SyncDataLocal::GetTag() const { | 197 const std::string& SyncDataLocal::GetTag() const { |
223 return immutable_entity_.Get().client_defined_unique_tag(); | 198 return immutable_entity_.Get().client_defined_unique_tag(); |
224 } | 199 } |
225 | 200 |
226 SyncDataRemote::SyncDataRemote(const SyncData& sync_data) | 201 SyncDataRemote::SyncDataRemote(const SyncData& sync_data) |
227 : SyncData(sync_data) { | 202 : SyncData(sync_data) { |
228 DCHECK(!sync_data.IsLocal()); | 203 DCHECK(!sync_data.IsLocal()); |
229 } | 204 } |
230 | 205 |
231 SyncDataRemote::~SyncDataRemote() {} | 206 SyncDataRemote::~SyncDataRemote() {} |
(...skipping 12 matching lines...) Expand all Loading... | |
244 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); | 219 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); |
245 } | 220 } |
246 | 221 |
247 void SyncDataRemote::DropAttachments( | 222 void SyncDataRemote::DropAttachments( |
248 const AttachmentIdList& attachment_ids, | 223 const AttachmentIdList& attachment_ids, |
249 const AttachmentService::DropCallback& callback) { | 224 const AttachmentService::DropCallback& callback) { |
250 attachment_service_.DropAttachments(attachment_ids, callback); | 225 attachment_service_.DropAttachments(attachment_ids, callback); |
251 } | 226 } |
252 | 227 |
253 } // namespace syncer | 228 } // namespace syncer |
OLD | NEW |