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