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" |
(...skipping 18 matching lines...) Expand all Loading... |
29 | 29 |
30 sync_pb::AttachmentIdProto IdToProto( | 30 sync_pb::AttachmentIdProto IdToProto( |
31 const syncer::AttachmentId& attachment_id) { | 31 const syncer::AttachmentId& attachment_id) { |
32 return attachment_id.GetProto(); | 32 return attachment_id.GetProto(); |
33 } | 33 } |
34 | 34 |
35 syncer::AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) { | 35 syncer::AttachmentId ProtoToId(const sync_pb::AttachmentIdProto& proto) { |
36 return syncer::AttachmentId::CreateFromProto(proto); | 36 return syncer::AttachmentId::CreateFromProto(proto); |
37 } | 37 } |
38 | 38 |
| 39 // Return true iff |attachments| contains one or more elements with the same |
| 40 // AttachmentId. |
| 41 bool ContainsDuplicateAttachments(const syncer::AttachmentList& attachments) { |
| 42 std::set<syncer::AttachmentId> id_set; |
| 43 AttachmentList::const_iterator iter = attachments.begin(); |
| 44 AttachmentList::const_iterator end = attachments.end(); |
| 45 for (; iter != end; ++iter) { |
| 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 } |
| 53 |
39 } // namespace | 54 } // namespace |
40 | 55 |
41 namespace syncer { | 56 namespace syncer { |
42 | 57 |
43 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) { | 58 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper(Wrapper* wrapper) { |
44 *wrapper = new sync_pb::SyncEntity(); | 59 *wrapper = new sync_pb::SyncEntity(); |
45 } | 60 } |
46 | 61 |
47 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(Wrapper* wrapper) { | 62 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper(Wrapper* wrapper) { |
48 delete *wrapper; | 63 delete *wrapper; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 | 104 |
90 // Static. | 105 // Static. |
91 SyncData SyncData::CreateLocalData(const std::string& sync_tag, | 106 SyncData SyncData::CreateLocalData(const std::string& sync_tag, |
92 const std::string& non_unique_title, | 107 const std::string& non_unique_title, |
93 const sync_pb::EntitySpecifics& specifics) { | 108 const sync_pb::EntitySpecifics& specifics) { |
94 syncer::AttachmentList attachments; | 109 syncer::AttachmentList attachments; |
95 return CreateLocalDataWithAttachments( | 110 return CreateLocalDataWithAttachments( |
96 sync_tag, non_unique_title, specifics, attachments); | 111 sync_tag, non_unique_title, specifics, attachments); |
97 } | 112 } |
98 | 113 |
99 // TODO(maniscalco): What should happen if the same Attachment appears in the | |
100 // list twice? Document the behavior and add a test case. | |
101 // | |
102 // Static. | 114 // Static. |
103 SyncData SyncData::CreateLocalDataWithAttachments( | 115 SyncData SyncData::CreateLocalDataWithAttachments( |
104 const std::string& sync_tag, | 116 const std::string& sync_tag, |
105 const std::string& non_unique_title, | 117 const std::string& non_unique_title, |
106 const sync_pb::EntitySpecifics& specifics, | 118 const sync_pb::EntitySpecifics& specifics, |
107 const AttachmentList& attachments) { | 119 const AttachmentList& attachments) { |
| 120 DCHECK(!ContainsDuplicateAttachments(attachments)); |
108 sync_pb::SyncEntity entity; | 121 sync_pb::SyncEntity entity; |
109 entity.set_client_defined_unique_tag(sync_tag); | 122 entity.set_client_defined_unique_tag(sync_tag); |
110 entity.set_non_unique_name(non_unique_title); | 123 entity.set_non_unique_name(non_unique_title); |
111 entity.mutable_specifics()->CopyFrom(specifics); | 124 entity.mutable_specifics()->CopyFrom(specifics); |
112 std::transform(attachments.begin(), | 125 std::transform(attachments.begin(), |
113 attachments.end(), | 126 attachments.end(), |
114 RepeatedFieldBackInserter(entity.mutable_attachment_id()), | 127 RepeatedFieldBackInserter(entity.mutable_attachment_id()), |
115 AttachmentToProto); | 128 AttachmentToProto); |
116 // TODO(maniscalco): Actually pass the attachments to the ctor and make them | |
117 // available to the AttachmentService once this SyncData gets passed into | |
118 // GenericChangeProcesso::ProcessSyncChanges (bug 354530). | |
119 AttachmentList copy_of_attachments(attachments); | 129 AttachmentList copy_of_attachments(attachments); |
120 return SyncData(kInvalidId, | 130 return SyncData(kInvalidId, |
121 &entity, | 131 &entity, |
122 ©_of_attachments, | 132 ©_of_attachments, |
123 base::Time(), | 133 base::Time(), |
124 AttachmentServiceProxy()); | 134 AttachmentServiceProxy()); |
125 } | 135 } |
126 | 136 |
127 // Static. | 137 // Static. |
128 SyncData SyncData::CreateRemoteData( | 138 SyncData SyncData::CreateRemoteData( |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); | 244 attachment_service_.GetOrDownloadAttachments(attachment_ids, callback); |
235 } | 245 } |
236 | 246 |
237 void SyncDataRemote::DropAttachments( | 247 void SyncDataRemote::DropAttachments( |
238 const AttachmentIdList& attachment_ids, | 248 const AttachmentIdList& attachment_ids, |
239 const AttachmentService::DropCallback& callback) { | 249 const AttachmentService::DropCallback& callback) { |
240 attachment_service_.DropAttachments(attachment_ids, callback); | 250 attachment_service_.DropAttachments(attachment_ids, callback); |
241 } | 251 } |
242 | 252 |
243 } // namespace syncer | 253 } // namespace syncer |
OLD | NEW |