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/api/attachments/attachment_service.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 using sync_pb::AttachmentId; | |
23 | |
24 namespace { | |
25 | |
26 template<typename T> | |
27 AttachmentId AttachmentIdOf(const Attachment& attachment) { | |
28 return attachment.GetId(); | |
29 } | |
30 | |
31 } // namespace | |
32 | |
18 namespace syncer { | 33 namespace syncer { |
19 | 34 |
20 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( | 35 void SyncData::ImmutableSyncEntityTraits::InitializeWrapper( |
21 Wrapper* wrapper) { | 36 Wrapper* wrapper) { |
22 *wrapper = new sync_pb::SyncEntity(); | 37 *wrapper = new sync_pb::SyncEntity(); |
23 } | 38 } |
24 | 39 |
25 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( | 40 void SyncData::ImmutableSyncEntityTraits::DestroyWrapper( |
26 Wrapper* wrapper) { | 41 Wrapper* wrapper) { |
27 delete *wrapper; | 42 delete *wrapper; |
(...skipping 11 matching lines...) Expand all Loading... | |
39 | 54 |
40 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, | 55 void SyncData::ImmutableSyncEntityTraits::Swap(sync_pb::SyncEntity* t1, |
41 sync_pb::SyncEntity* t2) { | 56 sync_pb::SyncEntity* t2) { |
42 t1->Swap(t2); | 57 t1->Swap(t2); |
43 } | 58 } |
44 | 59 |
45 SyncData::SyncData() | 60 SyncData::SyncData() |
46 : is_valid_(false), | 61 : is_valid_(false), |
47 id_(kInvalidId) {} | 62 id_(kInvalidId) {} |
48 | 63 |
49 SyncData::SyncData(int64 id, | 64 SyncData::SyncData( |
50 sync_pb::SyncEntity* entity, | 65 int64 id, |
51 const base::Time& remote_modification_time) | 66 sync_pb::SyncEntity* entity, |
67 const base::Time& remote_modification_time, | |
68 const WeakHandle<syncer::AttachmentService>& attachment_service) | |
52 : is_valid_(true), | 69 : is_valid_(true), |
53 id_(id), | 70 id_(id), |
54 remote_modification_time_(remote_modification_time), | 71 remote_modification_time_(remote_modification_time), |
55 immutable_entity_(entity) {} | 72 immutable_entity_(entity), |
73 attachment_service_(attachment_service) {} | |
56 | 74 |
57 SyncData::~SyncData() {} | 75 SyncData::~SyncData() {} |
58 | 76 |
59 // Static. | 77 // Static. |
60 SyncData SyncData::CreateLocalDelete( | 78 SyncData SyncData::CreateLocalDelete( |
61 const std::string& sync_tag, | 79 const std::string& sync_tag, |
62 ModelType datatype) { | 80 ModelType datatype) { |
63 sync_pb::EntitySpecifics specifics; | 81 sync_pb::EntitySpecifics specifics; |
64 AddDefaultFieldValue(datatype, &specifics); | 82 AddDefaultFieldValue(datatype, &specifics); |
65 return CreateLocalData(sync_tag, std::string(), specifics); | 83 return CreateLocalData(sync_tag, std::string(), specifics); |
66 } | 84 } |
67 | 85 |
68 // Static. | 86 // Static. |
69 SyncData SyncData::CreateLocalData( | 87 SyncData SyncData::CreateLocalData( |
70 const std::string& sync_tag, | 88 const std::string& sync_tag, |
71 const std::string& non_unique_title, | 89 const std::string& non_unique_title, |
72 const sync_pb::EntitySpecifics& specifics) { | 90 const sync_pb::EntitySpecifics& specifics) { |
91 syncer::AttachmentList attachments; | |
92 return CreateLocalDataWithAttachments( | |
93 sync_tag, non_unique_title, specifics, attachments); | |
94 } | |
95 | |
96 // TODO(maniscalco): What should happen if the same Attachment appears in the | |
97 // list twice? Document the behavior and add a test case. | |
98 // | |
99 // Static. | |
100 SyncData SyncData::CreateLocalDataWithAttachments( | |
101 const std::string& sync_tag, | |
102 const std::string& non_unique_title, | |
103 const sync_pb::EntitySpecifics& specifics, | |
104 const AttachmentList& attachments) { | |
73 sync_pb::SyncEntity entity; | 105 sync_pb::SyncEntity entity; |
74 entity.set_client_defined_unique_tag(sync_tag); | 106 entity.set_client_defined_unique_tag(sync_tag); |
75 entity.set_non_unique_name(non_unique_title); | 107 entity.set_non_unique_name(non_unique_title); |
76 entity.mutable_specifics()->CopyFrom(specifics); | 108 entity.mutable_specifics()->CopyFrom(specifics); |
77 return SyncData(kInvalidId, &entity, base::Time()); | 109 std::transform(attachments.begin(), |
110 attachments.end(), | |
111 RepeatedFieldBackInserter(entity.mutable_attachment_id()), | |
112 AttachmentIdOf<Attachment>); | |
113 // TODO(maniscalco): Actually pass the attachments to the ctor and make them | |
114 // available to the AttachmentService once this SyncData gets passed into | |
115 // GenericChangeProcesso::ProcessSyncChanges. | |
116 return SyncData(kInvalidId, | |
117 &entity, | |
118 base::Time(), | |
119 WeakHandle<AttachmentService>()); | |
78 } | 120 } |
79 | 121 |
80 // Static. | 122 // Static. |
81 SyncData SyncData::CreateRemoteData( | 123 SyncData SyncData::CreateRemoteData( |
82 int64 id, const sync_pb::EntitySpecifics& specifics, | 124 int64 id, |
83 const base::Time& modification_time) { | 125 const sync_pb::EntitySpecifics& specifics, |
126 const base::Time& modification_time, | |
127 const WeakHandle<AttachmentService>& attachment_service, | |
128 const AttachmentIdList& attachment_ids) { | |
84 DCHECK_NE(id, kInvalidId); | 129 DCHECK_NE(id, kInvalidId); |
85 sync_pb::SyncEntity entity; | 130 sync_pb::SyncEntity entity; |
86 entity.mutable_specifics()->CopyFrom(specifics); | 131 entity.mutable_specifics()->CopyFrom(specifics); |
87 return SyncData(id, &entity, modification_time); | 132 std::copy(attachment_ids.begin(), |
133 attachment_ids.end(), | |
134 google::protobuf::RepeatedFieldBackInserter( | |
135 entity.mutable_attachment_id())); | |
136 return SyncData(id, &entity, modification_time, attachment_service); | |
137 } | |
138 | |
139 // Static. | |
140 SyncData SyncData::CreateRemoteData(int64 id, | |
141 const sync_pb::EntitySpecifics& specifics, | |
142 const base::Time& modification_time) { | |
143 return CreateRemoteData(id, | |
144 specifics, | |
145 modification_time, | |
146 WeakHandle<AttachmentService>(), | |
147 AttachmentIdList()); | |
88 } | 148 } |
89 | 149 |
90 bool SyncData::IsValid() const { | 150 bool SyncData::IsValid() const { |
91 return is_valid_; | 151 return is_valid_; |
92 } | 152 } |
93 | 153 |
94 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { | 154 const sync_pb::EntitySpecifics& SyncData::GetSpecifics() const { |
95 return immutable_entity_.Get().specifics(); | 155 return immutable_entity_.Get().specifics(); |
96 } | 156 } |
97 | 157 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
143 | 203 |
144 std::string id = base::Int64ToString(GetRemoteId()); | 204 std::string id = base::Int64ToString(GetRemoteId()); |
145 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + | 205 return "{ isLocal: false, type: " + type + ", specifics: " + specifics + |
146 ", id: " + id + "}"; | 206 ", id: " + id + "}"; |
147 } | 207 } |
148 | 208 |
149 void PrintTo(const SyncData& sync_data, std::ostream* os) { | 209 void PrintTo(const SyncData& sync_data, std::ostream* os) { |
150 *os << sync_data.ToString(); | 210 *os << sync_data.ToString(); |
151 } | 211 } |
152 | 212 |
213 AttachmentIdList SyncData::GetAttachmentIds() const { | |
214 AttachmentIdList result; | |
215 const sync_pb::SyncEntity& entity = immutable_entity_.Get(); | |
216 std::copy(entity.attachment_id().begin(), | |
217 entity.attachment_id().end(), | |
218 std::back_inserter(result)); | |
219 return result; | |
220 } | |
221 | |
222 void SyncData::GetAttachments(const AttachmentIdList& attachment_ids, | |
223 const AttachmentService::GetCallback& callback) { | |
224 DCHECK(!IsLocal()); | |
225 attachment_service_.Call( | |
226 FROM_HERE, &AttachmentService::GetAttachments, attachment_ids, callback); | |
227 } | |
228 | |
229 void SyncData::DropAttachments( | |
230 const AttachmentIdList& attachment_ids, | |
231 const AttachmentService::DropCallback& callback) { | |
232 DCHECK(!IsLocal()); | |
233 attachment_service_.Call( | |
tim (not reviewing)
2014/03/10 22:55:58
As much as I do like the succinct-ness of WeakHand
maniscalco
2014/03/18 20:49:18
In some ways the explicit proxy object approach se
tim (not reviewing)
2014/03/19 22:32:48
I agree with everything. At the same time I would
maniscalco
2014/03/25 21:40:30
SGTM. I've gotten rid of WeakHandle and created A
| |
234 FROM_HERE, &AttachmentService::DropAttachments, attachment_ids, callback); | |
235 } | |
236 | |
153 } // namespace syncer | 237 } // namespace syncer |
OLD | NEW |