OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/sync/driver/generic_change_processor.h" | 5 #include "components/sync/driver/generic_change_processor.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <utility> | 10 #include <utility> |
(...skipping 10 matching lines...) Expand all Loading... |
21 #include "components/sync/core/base_node.h" | 21 #include "components/sync/core/base_node.h" |
22 #include "components/sync/core/change_record.h" | 22 #include "components/sync/core/change_record.h" |
23 #include "components/sync/core/read_node.h" | 23 #include "components/sync/core/read_node.h" |
24 #include "components/sync/core/read_transaction.h" | 24 #include "components/sync/core/read_transaction.h" |
25 #include "components/sync/core/write_node.h" | 25 #include "components/sync/core/write_node.h" |
26 #include "components/sync/core/write_transaction.h" | 26 #include "components/sync/core/write_transaction.h" |
27 #include "components/sync/driver/sync_api_component_factory.h" | 27 #include "components/sync/driver/sync_api_component_factory.h" |
28 #include "components/sync/driver/sync_client.h" | 28 #include "components/sync/driver/sync_client.h" |
29 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674. | 29 #include "components/sync/syncable/entry.h" // TODO(tim): Bug 123674. |
30 | 30 |
31 namespace sync_driver { | 31 namespace syncer { |
32 | 32 |
33 namespace { | 33 namespace { |
34 | 34 |
35 const int kContextSizeLimit = 1024; // Datatype context size limit. | 35 const int kContextSizeLimit = 1024; // Datatype context size limit. |
36 | 36 |
37 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics, | 37 void SetNodeSpecifics(const sync_pb::EntitySpecifics& entity_specifics, |
38 syncer::WriteNode* write_node) { | 38 WriteNode* write_node) { |
39 if (syncer::GetModelTypeFromSpecifics(entity_specifics) == | 39 if (GetModelTypeFromSpecifics(entity_specifics) == PASSWORDS) { |
40 syncer::PASSWORDS) { | |
41 write_node->SetPasswordSpecifics( | 40 write_node->SetPasswordSpecifics( |
42 entity_specifics.password().client_only_encrypted_data()); | 41 entity_specifics.password().client_only_encrypted_data()); |
43 } else { | 42 } else { |
44 write_node->SetEntitySpecifics(entity_specifics); | 43 write_node->SetEntitySpecifics(entity_specifics); |
45 } | 44 } |
46 } | 45 } |
47 | 46 |
48 // Helper function to convert AttachmentId to AttachmentMetadataRecord. | 47 // Helper function to convert AttachmentId to AttachmentMetadataRecord. |
49 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord( | 48 sync_pb::AttachmentMetadataRecord AttachmentIdToRecord( |
50 const syncer::AttachmentId& attachment_id) { | 49 const AttachmentId& attachment_id) { |
51 sync_pb::AttachmentMetadataRecord record; | 50 sync_pb::AttachmentMetadataRecord record; |
52 *record.mutable_id() = attachment_id.GetProto(); | 51 *record.mutable_id() = attachment_id.GetProto(); |
53 return record; | 52 return record; |
54 } | 53 } |
55 | 54 |
56 // Replace |write_nodes|'s attachment ids with |attachment_ids|. | 55 // Replace |write_nodes|'s attachment ids with |attachment_ids|. |
57 void SetAttachmentMetadata(const syncer::AttachmentIdList& attachment_ids, | 56 void SetAttachmentMetadata(const AttachmentIdList& attachment_ids, |
58 syncer::WriteNode* write_node) { | 57 WriteNode* write_node) { |
59 DCHECK(write_node); | 58 DCHECK(write_node); |
60 sync_pb::AttachmentMetadata attachment_metadata; | 59 sync_pb::AttachmentMetadata attachment_metadata; |
61 std::transform( | 60 std::transform( |
62 attachment_ids.begin(), attachment_ids.end(), | 61 attachment_ids.begin(), attachment_ids.end(), |
63 RepeatedFieldBackInserter(attachment_metadata.mutable_record()), | 62 RepeatedFieldBackInserter(attachment_metadata.mutable_record()), |
64 AttachmentIdToRecord); | 63 AttachmentIdToRecord); |
65 write_node->SetAttachmentMetadata(attachment_metadata); | 64 write_node->SetAttachmentMetadata(attachment_metadata); |
66 } | 65 } |
67 | 66 |
68 syncer::SyncData BuildRemoteSyncData( | 67 SyncData BuildRemoteSyncData( |
69 int64_t sync_id, | 68 int64_t sync_id, |
70 const syncer::ReadNode& read_node, | 69 const ReadNode& read_node, |
71 const syncer::AttachmentServiceProxy& attachment_service_proxy) { | 70 const AttachmentServiceProxy& attachment_service_proxy) { |
72 const syncer::AttachmentIdList& attachment_ids = read_node.GetAttachmentIds(); | 71 const AttachmentIdList& attachment_ids = read_node.GetAttachmentIds(); |
73 switch (read_node.GetModelType()) { | 72 switch (read_node.GetModelType()) { |
74 case syncer::PASSWORDS: { | 73 case PASSWORDS: { |
75 // Passwords must be accessed differently, to account for their | 74 // Passwords must be accessed differently, to account for their |
76 // encryption, and stored into a temporary EntitySpecifics. | 75 // encryption, and stored into a temporary EntitySpecifics. |
77 sync_pb::EntitySpecifics password_holder; | 76 sync_pb::EntitySpecifics password_holder; |
78 password_holder.mutable_password() | 77 password_holder.mutable_password() |
79 ->mutable_client_only_encrypted_data() | 78 ->mutable_client_only_encrypted_data() |
80 ->CopyFrom(read_node.GetPasswordSpecifics()); | 79 ->CopyFrom(read_node.GetPasswordSpecifics()); |
81 return syncer::SyncData::CreateRemoteData( | 80 return SyncData::CreateRemoteData( |
82 sync_id, password_holder, read_node.GetModificationTime(), | 81 sync_id, password_holder, read_node.GetModificationTime(), |
83 attachment_ids, attachment_service_proxy); | 82 attachment_ids, attachment_service_proxy); |
84 } | 83 } |
85 case syncer::SESSIONS: | 84 case SESSIONS: |
86 // Include tag hashes for sessions data type to allow discarding during | 85 // Include tag hashes for sessions data type to allow discarding during |
87 // merge if re-hashing by the service gives a different value. This is to | 86 // merge if re-hashing by the service gives a different value. This is to |
88 // allow removal of incorrectly hashed values, see crbug.com/604657. This | 87 // allow removal of incorrectly hashed values, see crbug.com/604657. This |
89 // cannot be done in the processor because only the service knows how to | 88 // cannot be done in the processor because only the service knows how to |
90 // generate a tag from the specifics. We don't set this value for other | 89 // generate a tag from the specifics. We don't set this value for other |
91 // data types because they shouldn't need it and it costs memory to hold | 90 // data types because they shouldn't need it and it costs memory to hold |
92 // another copy of this string around. | 91 // another copy of this string around. |
93 return syncer::SyncData::CreateRemoteData( | 92 return SyncData::CreateRemoteData( |
94 sync_id, read_node.GetEntitySpecifics(), | 93 sync_id, read_node.GetEntitySpecifics(), |
95 read_node.GetModificationTime(), attachment_ids, | 94 read_node.GetModificationTime(), attachment_ids, |
96 attachment_service_proxy, read_node.GetEntry()->GetUniqueClientTag()); | 95 attachment_service_proxy, read_node.GetEntry()->GetUniqueClientTag()); |
97 default: | 96 default: |
98 // Use the specifics directly, encryption has already been handled. | 97 // Use the specifics directly, encryption has already been handled. |
99 return syncer::SyncData::CreateRemoteData( | 98 return SyncData::CreateRemoteData(sync_id, read_node.GetEntitySpecifics(), |
100 sync_id, read_node.GetEntitySpecifics(), | 99 read_node.GetModificationTime(), |
101 read_node.GetModificationTime(), attachment_ids, | 100 attachment_ids, |
102 attachment_service_proxy); | 101 attachment_service_proxy); |
103 } | 102 } |
104 } | 103 } |
105 | 104 |
106 } // namespace | 105 } // namespace |
107 | 106 |
108 GenericChangeProcessor::GenericChangeProcessor( | 107 GenericChangeProcessor::GenericChangeProcessor( |
109 syncer::ModelType type, | 108 ModelType type, |
110 std::unique_ptr<syncer::DataTypeErrorHandler> error_handler, | 109 std::unique_ptr<DataTypeErrorHandler> error_handler, |
111 const base::WeakPtr<syncer::SyncableService>& local_service, | 110 const base::WeakPtr<SyncableService>& local_service, |
112 const base::WeakPtr<syncer::SyncMergeResult>& merge_result, | 111 const base::WeakPtr<SyncMergeResult>& merge_result, |
113 syncer::UserShare* user_share, | 112 UserShare* user_share, |
114 SyncClient* sync_client, | 113 SyncClient* sync_client, |
115 std::unique_ptr<syncer::AttachmentStoreForSync> attachment_store) | 114 std::unique_ptr<AttachmentStoreForSync> attachment_store) |
116 : ChangeProcessor(std::move(error_handler)), | 115 : ChangeProcessor(std::move(error_handler)), |
117 type_(type), | 116 type_(type), |
118 local_service_(local_service), | 117 local_service_(local_service), |
119 merge_result_(merge_result), | 118 merge_result_(merge_result), |
120 share_handle_(user_share), | 119 share_handle_(user_share), |
121 weak_ptr_factory_(this) { | 120 weak_ptr_factory_(this) { |
122 DCHECK(CalledOnValidThread()); | 121 DCHECK(CalledOnValidThread()); |
123 DCHECK_NE(type_, syncer::UNSPECIFIED); | 122 DCHECK_NE(type_, UNSPECIFIED); |
124 if (attachment_store) { | 123 if (attachment_store) { |
125 std::string store_birthday; | 124 std::string store_birthday; |
126 { | 125 { |
127 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 126 ReadTransaction trans(FROM_HERE, share_handle()); |
128 store_birthday = trans.GetStoreBirthday(); | 127 store_birthday = trans.GetStoreBirthday(); |
129 } | 128 } |
130 attachment_service_ = | 129 attachment_service_ = |
131 sync_client->GetSyncApiComponentFactory()->CreateAttachmentService( | 130 sync_client->GetSyncApiComponentFactory()->CreateAttachmentService( |
132 std::move(attachment_store), *user_share, store_birthday, type, | 131 std::move(attachment_store), *user_share, store_birthday, type, |
133 this); | 132 this); |
134 attachment_service_weak_ptr_factory_.reset( | 133 attachment_service_weak_ptr_factory_.reset( |
135 new base::WeakPtrFactory<syncer::AttachmentService>( | 134 new base::WeakPtrFactory<AttachmentService>(attachment_service_.get())); |
136 attachment_service_.get())); | 135 attachment_service_proxy_ = AttachmentServiceProxy( |
137 attachment_service_proxy_ = syncer::AttachmentServiceProxy( | |
138 base::ThreadTaskRunnerHandle::Get(), | 136 base::ThreadTaskRunnerHandle::Get(), |
139 attachment_service_weak_ptr_factory_->GetWeakPtr()); | 137 attachment_service_weak_ptr_factory_->GetWeakPtr()); |
140 UploadAllAttachmentsNotOnServer(); | 138 UploadAllAttachmentsNotOnServer(); |
141 } else { | 139 } else { |
142 attachment_service_proxy_ = syncer::AttachmentServiceProxy( | 140 attachment_service_proxy_ = |
143 base::ThreadTaskRunnerHandle::Get(), | 141 AttachmentServiceProxy(base::ThreadTaskRunnerHandle::Get(), |
144 base::WeakPtr<syncer::AttachmentService>()); | 142 base::WeakPtr<AttachmentService>()); |
145 } | 143 } |
146 } | 144 } |
147 | 145 |
148 GenericChangeProcessor::~GenericChangeProcessor() { | 146 GenericChangeProcessor::~GenericChangeProcessor() { |
149 DCHECK(CalledOnValidThread()); | 147 DCHECK(CalledOnValidThread()); |
150 } | 148 } |
151 | 149 |
152 void GenericChangeProcessor::ApplyChangesFromSyncModel( | 150 void GenericChangeProcessor::ApplyChangesFromSyncModel( |
153 const syncer::BaseTransaction* trans, | 151 const BaseTransaction* trans, |
154 int64_t model_version, | 152 int64_t model_version, |
155 const syncer::ImmutableChangeRecordList& changes) { | 153 const ImmutableChangeRecordList& changes) { |
156 DCHECK(CalledOnValidThread()); | 154 DCHECK(CalledOnValidThread()); |
157 DCHECK(syncer_changes_.empty()); | 155 DCHECK(syncer_changes_.empty()); |
158 for (syncer::ChangeRecordList::const_iterator it = changes.Get().begin(); | 156 for (ChangeRecordList::const_iterator it = changes.Get().begin(); |
159 it != changes.Get().end(); ++it) { | 157 it != changes.Get().end(); ++it) { |
160 if (it->action == syncer::ChangeRecord::ACTION_DELETE) { | 158 if (it->action == ChangeRecord::ACTION_DELETE) { |
161 std::unique_ptr<sync_pb::EntitySpecifics> specifics; | 159 std::unique_ptr<sync_pb::EntitySpecifics> specifics; |
162 if (it->specifics.has_password()) { | 160 if (it->specifics.has_password()) { |
163 DCHECK(it->extra.get()); | 161 DCHECK(it->extra.get()); |
164 specifics.reset(new sync_pb::EntitySpecifics(it->specifics)); | 162 specifics.reset(new sync_pb::EntitySpecifics(it->specifics)); |
165 specifics->mutable_password() | 163 specifics->mutable_password() |
166 ->mutable_client_only_encrypted_data() | 164 ->mutable_client_only_encrypted_data() |
167 ->CopyFrom(it->extra->unencrypted()); | 165 ->CopyFrom(it->extra->unencrypted()); |
168 } | 166 } |
169 const syncer::AttachmentIdList empty_list_of_attachment_ids; | 167 const AttachmentIdList empty_list_of_attachment_ids; |
170 syncer_changes_.push_back(syncer::SyncChange( | 168 syncer_changes_.push_back(SyncChange( |
171 FROM_HERE, syncer::SyncChange::ACTION_DELETE, | 169 FROM_HERE, SyncChange::ACTION_DELETE, |
172 syncer::SyncData::CreateRemoteData( | 170 SyncData::CreateRemoteData( |
173 it->id, specifics ? *specifics : it->specifics, base::Time(), | 171 it->id, specifics ? *specifics : it->specifics, base::Time(), |
174 empty_list_of_attachment_ids, attachment_service_proxy_))); | 172 empty_list_of_attachment_ids, attachment_service_proxy_))); |
175 } else { | 173 } else { |
176 syncer::SyncChange::SyncChangeType action = | 174 SyncChange::SyncChangeType action = |
177 (it->action == syncer::ChangeRecord::ACTION_ADD) | 175 (it->action == ChangeRecord::ACTION_ADD) ? SyncChange::ACTION_ADD |
178 ? syncer::SyncChange::ACTION_ADD | 176 : SyncChange::ACTION_UPDATE; |
179 : syncer::SyncChange::ACTION_UPDATE; | |
180 // Need to load specifics from node. | 177 // Need to load specifics from node. |
181 syncer::ReadNode read_node(trans); | 178 ReadNode read_node(trans); |
182 if (read_node.InitByIdLookup(it->id) != syncer::BaseNode::INIT_OK) { | 179 if (read_node.InitByIdLookup(it->id) != BaseNode::INIT_OK) { |
183 syncer::SyncError error( | 180 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
184 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 181 "Failed to look up data for received change with id " + |
185 "Failed to look up data for received change with id " + | 182 base::Int64ToString(it->id), |
186 base::Int64ToString(it->id), | 183 GetModelTypeFromSpecifics(it->specifics)); |
187 syncer::GetModelTypeFromSpecifics(it->specifics)); | |
188 error_handler()->OnUnrecoverableError(error); | 184 error_handler()->OnUnrecoverableError(error); |
189 return; | 185 return; |
190 } | 186 } |
191 syncer_changes_.push_back(syncer::SyncChange( | 187 syncer_changes_.push_back(SyncChange( |
192 FROM_HERE, action, | 188 FROM_HERE, action, |
193 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_))); | 189 BuildRemoteSyncData(it->id, read_node, attachment_service_proxy_))); |
194 } | 190 } |
195 } | 191 } |
196 } | 192 } |
197 | 193 |
198 void GenericChangeProcessor::CommitChangesFromSyncModel() { | 194 void GenericChangeProcessor::CommitChangesFromSyncModel() { |
199 DCHECK(CalledOnValidThread()); | 195 DCHECK(CalledOnValidThread()); |
200 if (syncer_changes_.empty()) | 196 if (syncer_changes_.empty()) |
201 return; | 197 return; |
202 if (!local_service_.get()) { | 198 if (!local_service_.get()) { |
203 syncer::ModelType type = syncer_changes_[0].sync_data().GetDataType(); | 199 ModelType type = syncer_changes_[0].sync_data().GetDataType(); |
204 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 200 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
205 "Local service destroyed.", type); | 201 "Local service destroyed.", type); |
206 error_handler()->OnUnrecoverableError(error); | 202 error_handler()->OnUnrecoverableError(error); |
207 return; | 203 return; |
208 } | 204 } |
209 syncer::SyncError error = | 205 SyncError error = |
210 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_); | 206 local_service_->ProcessSyncChanges(FROM_HERE, syncer_changes_); |
211 syncer_changes_.clear(); | 207 syncer_changes_.clear(); |
212 if (error.IsSet()) | 208 if (error.IsSet()) |
213 error_handler()->OnUnrecoverableError(error); | 209 error_handler()->OnUnrecoverableError(error); |
214 } | 210 } |
215 | 211 |
216 syncer::SyncDataList GenericChangeProcessor::GetAllSyncData( | 212 SyncDataList GenericChangeProcessor::GetAllSyncData(ModelType type) const { |
217 syncer::ModelType type) const { | |
218 DCHECK_EQ(type_, type); | 213 DCHECK_EQ(type_, type); |
219 // This is slow / memory intensive. Should be used sparingly by datatypes. | 214 // This is slow / memory intensive. Should be used sparingly by datatypes. |
220 syncer::SyncDataList data; | 215 SyncDataList data; |
221 GetAllSyncDataReturnError(&data); | 216 GetAllSyncDataReturnError(&data); |
222 return data; | 217 return data; |
223 } | 218 } |
224 | 219 |
225 syncer::SyncError GenericChangeProcessor::UpdateDataTypeContext( | 220 SyncError GenericChangeProcessor::UpdateDataTypeContext( |
226 syncer::ModelType type, | 221 ModelType type, |
227 syncer::SyncChangeProcessor::ContextRefreshStatus refresh_status, | 222 SyncChangeProcessor::ContextRefreshStatus refresh_status, |
228 const std::string& context) { | 223 const std::string& context) { |
229 DCHECK(syncer::ProtocolTypes().Has(type)); | 224 DCHECK(ProtocolTypes().Has(type)); |
230 DCHECK_EQ(type_, type); | 225 DCHECK_EQ(type_, type); |
231 | 226 |
232 if (context.size() > static_cast<size_t>(kContextSizeLimit)) { | 227 if (context.size() > static_cast<size_t>(kContextSizeLimit)) { |
233 return syncer::SyncError(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 228 return SyncError(FROM_HERE, SyncError::DATATYPE_ERROR, |
234 "Context size limit exceeded.", type); | 229 "Context size limit exceeded.", type); |
235 } | 230 } |
236 | 231 |
237 syncer::WriteTransaction trans(FROM_HERE, share_handle()); | 232 WriteTransaction trans(FROM_HERE, share_handle()); |
238 trans.SetDataTypeContext(type, refresh_status, context); | 233 trans.SetDataTypeContext(type, refresh_status, context); |
239 | 234 |
240 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can | 235 // TODO(zea): plumb a pointer to the PSS or SyncManagerImpl here so we can |
241 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|. | 236 // trigger a datatype nudge if |refresh_status == REFRESH_NEEDED|. |
242 | 237 |
243 return syncer::SyncError(); | 238 return SyncError(); |
244 } | 239 } |
245 | 240 |
246 void GenericChangeProcessor::AddLocalChangeObserver( | 241 void GenericChangeProcessor::AddLocalChangeObserver( |
247 syncer::LocalChangeObserver* observer) { | 242 LocalChangeObserver* observer) { |
248 local_change_observers_.AddObserver(observer); | 243 local_change_observers_.AddObserver(observer); |
249 } | 244 } |
250 | 245 |
251 void GenericChangeProcessor::RemoveLocalChangeObserver( | 246 void GenericChangeProcessor::RemoveLocalChangeObserver( |
252 syncer::LocalChangeObserver* observer) { | 247 LocalChangeObserver* observer) { |
253 local_change_observers_.RemoveObserver(observer); | 248 local_change_observers_.RemoveObserver(observer); |
254 } | 249 } |
255 | 250 |
256 void GenericChangeProcessor::OnAttachmentUploaded( | 251 void GenericChangeProcessor::OnAttachmentUploaded( |
257 const syncer::AttachmentId& attachment_id) { | 252 const AttachmentId& attachment_id) { |
258 syncer::WriteTransaction trans(FROM_HERE, share_handle()); | 253 WriteTransaction trans(FROM_HERE, share_handle()); |
259 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id); | 254 trans.UpdateEntriesMarkAttachmentAsOnServer(attachment_id); |
260 } | 255 } |
261 | 256 |
262 syncer::SyncError GenericChangeProcessor::GetAllSyncDataReturnError( | 257 SyncError GenericChangeProcessor::GetAllSyncDataReturnError( |
263 syncer::SyncDataList* current_sync_data) const { | 258 SyncDataList* current_sync_data) const { |
264 DCHECK(CalledOnValidThread()); | 259 DCHECK(CalledOnValidThread()); |
265 std::string type_name = syncer::ModelTypeToString(type_); | 260 std::string type_name = ModelTypeToString(type_); |
266 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 261 ReadTransaction trans(FROM_HERE, share_handle()); |
267 syncer::ReadNode root(&trans); | 262 ReadNode root(&trans); |
268 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { | 263 if (root.InitTypeRoot(type_) != BaseNode::INIT_OK) { |
269 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 264 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
270 "Server did not create the top-level " + type_name + | 265 "Server did not create the top-level " + type_name + |
271 " node. We might be running against an out-of-" | 266 " node. We might be running against an out-of-" |
272 "date server.", | 267 "date server.", |
273 type_); | 268 type_); |
274 return error; | 269 return error; |
275 } | 270 } |
276 | 271 |
277 // TODO(akalin): We'll have to do a tree traversal for bookmarks. | 272 // TODO(akalin): We'll have to do a tree traversal for bookmarks. |
278 DCHECK_NE(type_, syncer::BOOKMARKS); | 273 DCHECK_NE(type_, BOOKMARKS); |
279 | 274 |
280 std::vector<int64_t> child_ids; | 275 std::vector<int64_t> child_ids; |
281 root.GetChildIds(&child_ids); | 276 root.GetChildIds(&child_ids); |
282 | 277 |
283 for (std::vector<int64_t>::iterator it = child_ids.begin(); | 278 for (std::vector<int64_t>::iterator it = child_ids.begin(); |
284 it != child_ids.end(); ++it) { | 279 it != child_ids.end(); ++it) { |
285 syncer::ReadNode sync_child_node(&trans); | 280 ReadNode sync_child_node(&trans); |
286 if (sync_child_node.InitByIdLookup(*it) != syncer::BaseNode::INIT_OK) { | 281 if (sync_child_node.InitByIdLookup(*it) != BaseNode::INIT_OK) { |
287 syncer::SyncError error( | 282 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
288 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 283 "Failed to fetch child node for type " + type_name + ".", |
289 "Failed to fetch child node for type " + type_name + ".", type_); | 284 type_); |
290 return error; | 285 return error; |
291 } | 286 } |
292 current_sync_data->push_back(BuildRemoteSyncData( | 287 current_sync_data->push_back(BuildRemoteSyncData( |
293 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_)); | 288 sync_child_node.GetId(), sync_child_node, attachment_service_proxy_)); |
294 } | 289 } |
295 return syncer::SyncError(); | 290 return SyncError(); |
296 } | 291 } |
297 | 292 |
298 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const { | 293 bool GenericChangeProcessor::GetDataTypeContext(std::string* context) const { |
299 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 294 ReadTransaction trans(FROM_HERE, share_handle()); |
300 sync_pb::DataTypeContext context_proto; | 295 sync_pb::DataTypeContext context_proto; |
301 trans.GetDataTypeContext(type_, &context_proto); | 296 trans.GetDataTypeContext(type_, &context_proto); |
302 if (!context_proto.has_context()) | 297 if (!context_proto.has_context()) |
303 return false; | 298 return false; |
304 | 299 |
305 DCHECK_EQ(type_, syncer::GetModelTypeFromSpecificsFieldNumber( | 300 DCHECK_EQ(type_, |
306 context_proto.data_type_id())); | 301 GetModelTypeFromSpecificsFieldNumber(context_proto.data_type_id())); |
307 *context = context_proto.context(); | 302 *context = context_proto.context(); |
308 return true; | 303 return true; |
309 } | 304 } |
310 | 305 |
311 int GenericChangeProcessor::GetSyncCount() { | 306 int GenericChangeProcessor::GetSyncCount() { |
312 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 307 ReadTransaction trans(FROM_HERE, share_handle()); |
313 syncer::ReadNode root(&trans); | 308 ReadNode root(&trans); |
314 if (root.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) | 309 if (root.InitTypeRoot(type_) != BaseNode::INIT_OK) |
315 return 0; | 310 return 0; |
316 | 311 |
317 // Subtract one to account for type's root node. | 312 // Subtract one to account for type's root node. |
318 return root.GetTotalNodeCount() - 1; | 313 return root.GetTotalNodeCount() - 1; |
319 } | 314 } |
320 | 315 |
321 namespace { | 316 namespace { |
322 | 317 |
323 // WARNING: this code is sensitive to compiler optimizations. Be careful | 318 // WARNING: this code is sensitive to compiler optimizations. Be careful |
324 // modifying any code around an OnUnrecoverableError call, else the compiler | 319 // modifying any code around an OnUnrecoverableError call, else the compiler |
325 // attempts to merge it with other calls, losing useful information in | 320 // attempts to merge it with other calls, losing useful information in |
326 // breakpad uploads. | 321 // breakpad uploads. |
327 syncer::SyncError LogLookupFailure( | 322 SyncError LogLookupFailure(BaseNode::InitByLookupResult lookup_result, |
328 syncer::BaseNode::InitByLookupResult lookup_result, | 323 const tracked_objects::Location& from_here, |
329 const tracked_objects::Location& from_here, | 324 const std::string& error_prefix, |
330 const std::string& error_prefix, | 325 ModelType type, |
331 syncer::ModelType type, | 326 DataTypeErrorHandler* error_handler) { |
332 syncer::DataTypeErrorHandler* error_handler) { | |
333 switch (lookup_result) { | 327 switch (lookup_result) { |
334 case syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: { | 328 case BaseNode::INIT_FAILED_ENTRY_NOT_GOOD: { |
335 syncer::SyncError error; | 329 SyncError error; |
336 error.Reset( | 330 error.Reset( |
337 from_here, | 331 from_here, |
338 error_prefix + "could not find entry matching the lookup criteria.", | 332 error_prefix + "could not find entry matching the lookup criteria.", |
339 type); | 333 type); |
340 error_handler->OnUnrecoverableError(error); | 334 error_handler->OnUnrecoverableError(error); |
341 LOG(ERROR) << "Delete: Bad entry."; | 335 LOG(ERROR) << "Delete: Bad entry."; |
342 return error; | 336 return error; |
343 } | 337 } |
344 case syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL: { | 338 case BaseNode::INIT_FAILED_ENTRY_IS_DEL: { |
345 syncer::SyncError error; | 339 SyncError error; |
346 error.Reset(from_here, error_prefix + "entry is already deleted.", type); | 340 error.Reset(from_here, error_prefix + "entry is already deleted.", type); |
347 error_handler->OnUnrecoverableError(error); | 341 error_handler->OnUnrecoverableError(error); |
348 LOG(ERROR) << "Delete: Deleted entry."; | 342 LOG(ERROR) << "Delete: Deleted entry."; |
349 return error; | 343 return error; |
350 } | 344 } |
351 case syncer::BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY: { | 345 case BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY: { |
352 syncer::SyncError error; | 346 SyncError error; |
353 error.Reset(from_here, error_prefix + "unable to decrypt", type); | 347 error.Reset(from_here, error_prefix + "unable to decrypt", type); |
354 error_handler->OnUnrecoverableError(error); | 348 error_handler->OnUnrecoverableError(error); |
355 LOG(ERROR) << "Delete: Undecryptable entry."; | 349 LOG(ERROR) << "Delete: Undecryptable entry."; |
356 return error; | 350 return error; |
357 } | 351 } |
358 case syncer::BaseNode::INIT_FAILED_PRECONDITION: { | 352 case BaseNode::INIT_FAILED_PRECONDITION: { |
359 syncer::SyncError error; | 353 SyncError error; |
360 error.Reset(from_here, | 354 error.Reset(from_here, |
361 error_prefix + "a precondition was not met for calling init.", | 355 error_prefix + "a precondition was not met for calling init.", |
362 type); | 356 type); |
363 error_handler->OnUnrecoverableError(error); | 357 error_handler->OnUnrecoverableError(error); |
364 LOG(ERROR) << "Delete: Failed precondition."; | 358 LOG(ERROR) << "Delete: Failed precondition."; |
365 return error; | 359 return error; |
366 } | 360 } |
367 default: { | 361 default: { |
368 syncer::SyncError error; | 362 SyncError error; |
369 // Should have listed all the possible error cases above. | 363 // Should have listed all the possible error cases above. |
370 error.Reset(from_here, error_prefix + "unknown error", type); | 364 error.Reset(from_here, error_prefix + "unknown error", type); |
371 error_handler->OnUnrecoverableError(error); | 365 error_handler->OnUnrecoverableError(error); |
372 LOG(ERROR) << "Delete: Unknown error."; | 366 LOG(ERROR) << "Delete: Unknown error."; |
373 return error; | 367 return error; |
374 } | 368 } |
375 } | 369 } |
376 } | 370 } |
377 | 371 |
378 } // namespace | 372 } // namespace |
379 | 373 |
380 syncer::SyncError GenericChangeProcessor::AttemptDelete( | 374 SyncError GenericChangeProcessor::AttemptDelete( |
381 const syncer::SyncChange& change, | 375 const SyncChange& change, |
382 syncer::ModelType type, | 376 ModelType type, |
383 const std::string& type_str, | 377 const std::string& type_str, |
384 syncer::WriteNode* node, | 378 WriteNode* node, |
385 syncer::DataTypeErrorHandler* error_handler) { | 379 DataTypeErrorHandler* error_handler) { |
386 DCHECK_EQ(change.change_type(), syncer::SyncChange::ACTION_DELETE); | 380 DCHECK_EQ(change.change_type(), SyncChange::ACTION_DELETE); |
387 if (change.sync_data().IsLocal()) { | 381 if (change.sync_data().IsLocal()) { |
388 const std::string& tag = syncer::SyncDataLocal(change.sync_data()).GetTag(); | 382 const std::string& tag = SyncDataLocal(change.sync_data()).GetTag(); |
389 if (tag.empty()) { | 383 if (tag.empty()) { |
390 syncer::SyncError error(FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 384 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
391 "Failed to delete " + type_str + | 385 "Failed to delete " + type_str + |
392 " node. Local data, empty tag. " + | 386 " node. Local data, empty tag. " + |
393 change.location().ToString(), | 387 change.location().ToString(), |
394 type); | 388 type); |
395 error_handler->OnUnrecoverableError(error); | 389 error_handler->OnUnrecoverableError(error); |
396 NOTREACHED(); | 390 NOTREACHED(); |
397 return error; | 391 return error; |
398 } | 392 } |
399 | 393 |
400 syncer::BaseNode::InitByLookupResult result = | 394 BaseNode::InitByLookupResult result = |
401 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag); | 395 node->InitByClientTagLookup(change.sync_data().GetDataType(), tag); |
402 if (result != syncer::BaseNode::INIT_OK) { | 396 if (result != BaseNode::INIT_OK) { |
403 return LogLookupFailure(result, FROM_HERE, | 397 return LogLookupFailure(result, FROM_HERE, |
404 "Failed to delete " + type_str + | 398 "Failed to delete " + type_str + |
405 " node. Local data. " + | 399 " node. Local data. " + |
406 change.location().ToString(), | 400 change.location().ToString(), |
407 type, error_handler); | 401 type, error_handler); |
408 } | 402 } |
409 } else { | 403 } else { |
410 syncer::BaseNode::InitByLookupResult result = node->InitByIdLookup( | 404 BaseNode::InitByLookupResult result = |
411 syncer::SyncDataRemote(change.sync_data()).GetId()); | 405 node->InitByIdLookup(SyncDataRemote(change.sync_data()).GetId()); |
412 if (result != syncer::BaseNode::INIT_OK) { | 406 if (result != BaseNode::INIT_OK) { |
413 return LogLookupFailure(result, FROM_HERE, | 407 return LogLookupFailure(result, FROM_HERE, |
414 "Failed to delete " + type_str + | 408 "Failed to delete " + type_str + |
415 " node. Non-local data. " + | 409 " node. Non-local data. " + |
416 change.location().ToString(), | 410 change.location().ToString(), |
417 type, error_handler); | 411 type, error_handler); |
418 } | 412 } |
419 } | 413 } |
420 NotifyLocalChangeObservers(node->GetEntry(), change); | 414 NotifyLocalChangeObservers(node->GetEntry(), change); |
421 if (IsActOnceDataType(type)) | 415 if (IsActOnceDataType(type)) |
422 node->Drop(); | 416 node->Drop(); |
423 else | 417 else |
424 node->Tombstone(); | 418 node->Tombstone(); |
425 return syncer::SyncError(); | 419 return SyncError(); |
426 } | 420 } |
427 | 421 |
428 syncer::SyncError GenericChangeProcessor::ProcessSyncChanges( | 422 SyncError GenericChangeProcessor::ProcessSyncChanges( |
429 const tracked_objects::Location& from_here, | 423 const tracked_objects::Location& from_here, |
430 const syncer::SyncChangeList& list_of_changes) { | 424 const SyncChangeList& list_of_changes) { |
431 DCHECK(CalledOnValidThread()); | 425 DCHECK(CalledOnValidThread()); |
432 | 426 |
433 if (list_of_changes.empty()) { | 427 if (list_of_changes.empty()) { |
434 // No work. Exit without entering WriteTransaction. | 428 // No work. Exit without entering WriteTransaction. |
435 return syncer::SyncError(); | 429 return SyncError(); |
436 } | 430 } |
437 | 431 |
438 // Keep track of brand new attachments so we can persist them on this device | 432 // Keep track of brand new attachments so we can persist them on this device |
439 // and upload them to the server. | 433 // and upload them to the server. |
440 syncer::AttachmentIdSet new_attachments; | 434 AttachmentIdSet new_attachments; |
441 | 435 |
442 syncer::WriteTransaction trans(from_here, share_handle()); | 436 WriteTransaction trans(from_here, share_handle()); |
443 | 437 |
444 for (syncer::SyncChangeList::const_iterator iter = list_of_changes.begin(); | 438 for (SyncChangeList::const_iterator iter = list_of_changes.begin(); |
445 iter != list_of_changes.end(); ++iter) { | 439 iter != list_of_changes.end(); ++iter) { |
446 const syncer::SyncChange& change = *iter; | 440 const SyncChange& change = *iter; |
447 DCHECK_EQ(change.sync_data().GetDataType(), type_); | 441 DCHECK_EQ(change.sync_data().GetDataType(), type_); |
448 std::string type_str = syncer::ModelTypeToString(type_); | 442 std::string type_str = ModelTypeToString(type_); |
449 syncer::WriteNode sync_node(&trans); | 443 WriteNode sync_node(&trans); |
450 if (change.change_type() == syncer::SyncChange::ACTION_DELETE) { | 444 if (change.change_type() == SyncChange::ACTION_DELETE) { |
451 syncer::SyncError error = | 445 SyncError error = |
452 AttemptDelete(change, type_, type_str, &sync_node, error_handler()); | 446 AttemptDelete(change, type_, type_str, &sync_node, error_handler()); |
453 if (error.IsSet()) { | 447 if (error.IsSet()) { |
454 NOTREACHED(); | 448 NOTREACHED(); |
455 return error; | 449 return error; |
456 } | 450 } |
457 if (merge_result_.get()) { | 451 if (merge_result_.get()) { |
458 merge_result_->set_num_items_deleted( | 452 merge_result_->set_num_items_deleted( |
459 merge_result_->num_items_deleted() + 1); | 453 merge_result_->num_items_deleted() + 1); |
460 } | 454 } |
461 } else if (change.change_type() == syncer::SyncChange::ACTION_ADD) { | 455 } else if (change.change_type() == SyncChange::ACTION_ADD) { |
462 syncer::SyncError error = HandleActionAdd(change, type_str, trans, | 456 SyncError error = HandleActionAdd(change, type_str, trans, &sync_node, |
463 &sync_node, &new_attachments); | 457 &new_attachments); |
464 if (error.IsSet()) { | 458 if (error.IsSet()) { |
465 return error; | 459 return error; |
466 } | 460 } |
467 } else if (change.change_type() == syncer::SyncChange::ACTION_UPDATE) { | 461 } else if (change.change_type() == SyncChange::ACTION_UPDATE) { |
468 syncer::SyncError error = HandleActionUpdate( | 462 SyncError error = HandleActionUpdate(change, type_str, trans, &sync_node, |
469 change, type_str, trans, &sync_node, &new_attachments); | 463 &new_attachments); |
470 if (error.IsSet()) { | 464 if (error.IsSet()) { |
471 return error; | 465 return error; |
472 } | 466 } |
473 } else { | 467 } else { |
474 syncer::SyncError error( | 468 SyncError error(FROM_HERE, SyncError::DATATYPE_ERROR, |
475 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 469 "Received unset SyncChange in the change processor, " + |
476 "Received unset SyncChange in the change processor, " + | 470 change.location().ToString(), |
477 change.location().ToString(), | 471 type_); |
478 type_); | |
479 error_handler()->OnUnrecoverableError(error); | 472 error_handler()->OnUnrecoverableError(error); |
480 NOTREACHED(); | 473 NOTREACHED(); |
481 LOG(ERROR) << "Unset sync change."; | 474 LOG(ERROR) << "Unset sync change."; |
482 return error; | 475 return error; |
483 } | 476 } |
484 } | 477 } |
485 | 478 |
486 if (!new_attachments.empty()) { | 479 if (!new_attachments.empty()) { |
487 // If datatype uses attachments it should have supplied attachment store | 480 // If datatype uses attachments it should have supplied attachment store |
488 // which would initialize attachment_service_. Fail if it isn't so. | 481 // which would initialize attachment_service_. Fail if it isn't so. |
489 if (!attachment_service_.get()) { | 482 if (!attachment_service_.get()) { |
490 syncer::SyncError error( | 483 SyncError error( |
491 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 484 FROM_HERE, SyncError::DATATYPE_ERROR, |
492 "Datatype performs attachment operation without initializing " | 485 "Datatype performs attachment operation without initializing " |
493 "attachment store", | 486 "attachment store", |
494 type_); | 487 type_); |
495 error_handler()->OnUnrecoverableError(error); | 488 error_handler()->OnUnrecoverableError(error); |
496 NOTREACHED(); | 489 NOTREACHED(); |
497 return error; | 490 return error; |
498 } | 491 } |
499 syncer::AttachmentIdList ids_to_upload; | 492 AttachmentIdList ids_to_upload; |
500 ids_to_upload.reserve(new_attachments.size()); | 493 ids_to_upload.reserve(new_attachments.size()); |
501 std::copy(new_attachments.begin(), new_attachments.end(), | 494 std::copy(new_attachments.begin(), new_attachments.end(), |
502 std::back_inserter(ids_to_upload)); | 495 std::back_inserter(ids_to_upload)); |
503 attachment_service_->UploadAttachments(ids_to_upload); | 496 attachment_service_->UploadAttachments(ids_to_upload); |
504 } | 497 } |
505 | 498 |
506 return syncer::SyncError(); | 499 return SyncError(); |
507 } | 500 } |
508 | 501 |
509 // WARNING: this code is sensitive to compiler optimizations. Be careful | 502 // WARNING: this code is sensitive to compiler optimizations. Be careful |
510 // modifying any code around an OnUnrecoverableError call, else the compiler | 503 // modifying any code around an OnUnrecoverableError call, else the compiler |
511 // attempts to merge it with other calls, losing useful information in | 504 // attempts to merge it with other calls, losing useful information in |
512 // breakpad uploads. | 505 // breakpad uploads. |
513 syncer::SyncError GenericChangeProcessor::HandleActionAdd( | 506 SyncError GenericChangeProcessor::HandleActionAdd( |
514 const syncer::SyncChange& change, | 507 const SyncChange& change, |
515 const std::string& type_str, | 508 const std::string& type_str, |
516 const syncer::WriteTransaction& trans, | 509 const WriteTransaction& trans, |
517 syncer::WriteNode* sync_node, | 510 WriteNode* sync_node, |
518 syncer::AttachmentIdSet* new_attachments) { | 511 AttachmentIdSet* new_attachments) { |
519 // TODO(sync): Handle other types of creation (custom parents, folders, | 512 // TODO(sync): Handle other types of creation (custom parents, folders, |
520 // etc.). | 513 // etc.). |
521 const syncer::SyncDataLocal sync_data_local(change.sync_data()); | 514 const SyncDataLocal sync_data_local(change.sync_data()); |
522 syncer::WriteNode::InitUniqueByCreationResult result = | 515 WriteNode::InitUniqueByCreationResult result = |
523 sync_node->InitUniqueByCreation(sync_data_local.GetDataType(), | 516 sync_node->InitUniqueByCreation(sync_data_local.GetDataType(), |
524 sync_data_local.GetTag()); | 517 sync_data_local.GetTag()); |
525 if (result != syncer::WriteNode::INIT_SUCCESS) { | 518 if (result != WriteNode::INIT_SUCCESS) { |
526 std::string error_prefix = "Failed to create " + type_str + " node: " + | 519 std::string error_prefix = "Failed to create " + type_str + " node: " + |
527 change.location().ToString() + ", "; | 520 change.location().ToString() + ", "; |
528 switch (result) { | 521 switch (result) { |
529 case syncer::WriteNode::INIT_FAILED_EMPTY_TAG: { | 522 case WriteNode::INIT_FAILED_EMPTY_TAG: { |
530 syncer::SyncError error; | 523 SyncError error; |
531 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); | 524 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); |
532 error_handler()->OnUnrecoverableError(error); | 525 error_handler()->OnUnrecoverableError(error); |
533 LOG(ERROR) << "Create: Empty tag."; | 526 LOG(ERROR) << "Create: Empty tag."; |
534 return error; | 527 return error; |
535 } | 528 } |
536 case syncer::WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: { | 529 case WriteNode::INIT_FAILED_COULD_NOT_CREATE_ENTRY: { |
537 syncer::SyncError error; | 530 SyncError error; |
538 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_); | 531 error.Reset(FROM_HERE, error_prefix + "failed to create entry", type_); |
539 error_handler()->OnUnrecoverableError(error); | 532 error_handler()->OnUnrecoverableError(error); |
540 LOG(ERROR) << "Create: Could not create entry."; | 533 LOG(ERROR) << "Create: Could not create entry."; |
541 return error; | 534 return error; |
542 } | 535 } |
543 case syncer::WriteNode::INIT_FAILED_SET_PREDECESSOR: { | 536 case WriteNode::INIT_FAILED_SET_PREDECESSOR: { |
544 syncer::SyncError error; | 537 SyncError error; |
545 error.Reset(FROM_HERE, error_prefix + "failed to set predecessor", | 538 error.Reset(FROM_HERE, error_prefix + "failed to set predecessor", |
546 type_); | 539 type_); |
547 error_handler()->OnUnrecoverableError(error); | 540 error_handler()->OnUnrecoverableError(error); |
548 LOG(ERROR) << "Create: Bad predecessor."; | 541 LOG(ERROR) << "Create: Bad predecessor."; |
549 return error; | 542 return error; |
550 } | 543 } |
551 case syncer::WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: { | 544 case WriteNode::INIT_FAILED_DECRYPT_EXISTING_ENTRY: { |
552 syncer::SyncError error; | 545 SyncError error; |
553 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); | 546 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); |
554 error_handler()->OnUnrecoverableError(error); | 547 error_handler()->OnUnrecoverableError(error); |
555 LOG(ERROR) << "Create: Failed to decrypt."; | 548 LOG(ERROR) << "Create: Failed to decrypt."; |
556 return error; | 549 return error; |
557 } | 550 } |
558 default: { | 551 default: { |
559 syncer::SyncError error; | 552 SyncError error; |
560 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); | 553 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); |
561 error_handler()->OnUnrecoverableError(error); | 554 error_handler()->OnUnrecoverableError(error); |
562 LOG(ERROR) << "Create: Unknown error."; | 555 LOG(ERROR) << "Create: Unknown error."; |
563 return error; | 556 return error; |
564 } | 557 } |
565 } | 558 } |
566 } | 559 } |
567 NotifyLocalChangeObservers(sync_node->GetEntry(), change); | 560 NotifyLocalChangeObservers(sync_node->GetEntry(), change); |
568 | 561 |
569 sync_node->SetTitle(change.sync_data().GetTitle()); | 562 sync_node->SetTitle(change.sync_data().GetTitle()); |
570 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 563 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
571 | 564 |
572 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 565 AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
573 SetAttachmentMetadata(attachment_ids, sync_node); | 566 SetAttachmentMetadata(attachment_ids, sync_node); |
574 | 567 |
575 // Return any newly added attachments. | 568 // Return any newly added attachments. |
576 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); | 569 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
577 if (merge_result_.get()) { | 570 if (merge_result_.get()) { |
578 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); | 571 merge_result_->set_num_items_added(merge_result_->num_items_added() + 1); |
579 } | 572 } |
580 return syncer::SyncError(); | 573 return SyncError(); |
581 } | 574 } |
582 // WARNING: this code is sensitive to compiler optimizations. Be careful | 575 // WARNING: this code is sensitive to compiler optimizations. Be careful |
583 // modifying any code around an OnUnrecoverableError call, else the compiler | 576 // modifying any code around an OnUnrecoverableError call, else the compiler |
584 // attempts to merge it with other calls, losing useful information in | 577 // attempts to merge it with other calls, losing useful information in |
585 // breakpad uploads. | 578 // breakpad uploads. |
586 syncer::SyncError GenericChangeProcessor::HandleActionUpdate( | 579 SyncError GenericChangeProcessor::HandleActionUpdate( |
587 const syncer::SyncChange& change, | 580 const SyncChange& change, |
588 const std::string& type_str, | 581 const std::string& type_str, |
589 const syncer::WriteTransaction& trans, | 582 const WriteTransaction& trans, |
590 syncer::WriteNode* sync_node, | 583 WriteNode* sync_node, |
591 syncer::AttachmentIdSet* new_attachments) { | 584 AttachmentIdSet* new_attachments) { |
592 const syncer::SyncDataLocal sync_data_local(change.sync_data()); | 585 const SyncDataLocal sync_data_local(change.sync_data()); |
593 syncer::BaseNode::InitByLookupResult result = | 586 BaseNode::InitByLookupResult result = sync_node->InitByClientTagLookup( |
594 sync_node->InitByClientTagLookup(sync_data_local.GetDataType(), | 587 sync_data_local.GetDataType(), sync_data_local.GetTag()); |
595 sync_data_local.GetTag()); | 588 if (result != BaseNode::INIT_OK) { |
596 if (result != syncer::BaseNode::INIT_OK) { | |
597 std::string error_prefix = "Failed to load " + type_str + " node. " + | 589 std::string error_prefix = "Failed to load " + type_str + " node. " + |
598 change.location().ToString() + ", "; | 590 change.location().ToString() + ", "; |
599 if (result == syncer::BaseNode::INIT_FAILED_PRECONDITION) { | 591 if (result == BaseNode::INIT_FAILED_PRECONDITION) { |
600 syncer::SyncError error; | 592 SyncError error; |
601 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); | 593 error.Reset(FROM_HERE, error_prefix + "empty tag", type_); |
602 error_handler()->OnUnrecoverableError(error); | 594 error_handler()->OnUnrecoverableError(error); |
603 LOG(ERROR) << "Update: Empty tag."; | 595 LOG(ERROR) << "Update: Empty tag."; |
604 return error; | 596 return error; |
605 } else if (result == syncer::BaseNode::INIT_FAILED_ENTRY_NOT_GOOD) { | 597 } else if (result == BaseNode::INIT_FAILED_ENTRY_NOT_GOOD) { |
606 syncer::SyncError error; | 598 SyncError error; |
607 error.Reset(FROM_HERE, error_prefix + "bad entry", type_); | 599 error.Reset(FROM_HERE, error_prefix + "bad entry", type_); |
608 error_handler()->OnUnrecoverableError(error); | 600 error_handler()->OnUnrecoverableError(error); |
609 LOG(ERROR) << "Update: bad entry."; | 601 LOG(ERROR) << "Update: bad entry."; |
610 return error; | 602 return error; |
611 } else if (result == syncer::BaseNode::INIT_FAILED_ENTRY_IS_DEL) { | 603 } else if (result == BaseNode::INIT_FAILED_ENTRY_IS_DEL) { |
612 syncer::SyncError error; | 604 SyncError error; |
613 error.Reset(FROM_HERE, error_prefix + "deleted entry", type_); | 605 error.Reset(FROM_HERE, error_prefix + "deleted entry", type_); |
614 error_handler()->OnUnrecoverableError(error); | 606 error_handler()->OnUnrecoverableError(error); |
615 LOG(ERROR) << "Update: deleted entry."; | 607 LOG(ERROR) << "Update: deleted entry."; |
616 return error; | 608 return error; |
617 } else if (result == syncer::BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY) { | 609 } else if (result == BaseNode::INIT_FAILED_DECRYPT_IF_NECESSARY) { |
618 syncer::SyncError error; | 610 SyncError error; |
619 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); | 611 error.Reset(FROM_HERE, error_prefix + "failed to decrypt", type_); |
620 error_handler()->OnUnrecoverableError(error); | 612 error_handler()->OnUnrecoverableError(error); |
621 LOG(ERROR) << "Update: Failed to decrypt."; | 613 LOG(ERROR) << "Update: Failed to decrypt."; |
622 return error; | 614 return error; |
623 } else { | 615 } else { |
624 NOTREACHED(); | 616 NOTREACHED(); |
625 syncer::SyncError error; | 617 SyncError error; |
626 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); | 618 error.Reset(FROM_HERE, error_prefix + "unknown error", type_); |
627 error_handler()->OnUnrecoverableError(error); | 619 error_handler()->OnUnrecoverableError(error); |
628 LOG(ERROR) << "Update: Unknown error."; | 620 LOG(ERROR) << "Update: Unknown error."; |
629 return error; | 621 return error; |
630 } | 622 } |
631 } | 623 } |
632 | 624 |
633 NotifyLocalChangeObservers(sync_node->GetEntry(), change); | 625 NotifyLocalChangeObservers(sync_node->GetEntry(), change); |
634 | 626 |
635 sync_node->SetTitle(change.sync_data().GetTitle()); | 627 sync_node->SetTitle(change.sync_data().GetTitle()); |
636 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); | 628 SetNodeSpecifics(sync_data_local.GetSpecifics(), sync_node); |
637 syncer::AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); | 629 AttachmentIdList attachment_ids = sync_data_local.GetAttachmentIds(); |
638 SetAttachmentMetadata(attachment_ids, sync_node); | 630 SetAttachmentMetadata(attachment_ids, sync_node); |
639 | 631 |
640 // Return any newly added attachments. | 632 // Return any newly added attachments. |
641 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); | 633 new_attachments->insert(attachment_ids.begin(), attachment_ids.end()); |
642 | 634 |
643 if (merge_result_.get()) { | 635 if (merge_result_.get()) { |
644 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + | 636 merge_result_->set_num_items_modified(merge_result_->num_items_modified() + |
645 1); | 637 1); |
646 } | 638 } |
647 // TODO(sync): Support updating other parts of the sync node (title, | 639 // TODO(sync): Support updating other parts of the sync node (title, |
648 // successor, parent, etc.). | 640 // successor, parent, etc.). |
649 return syncer::SyncError(); | 641 return SyncError(); |
650 } | 642 } |
651 | 643 |
652 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 644 bool GenericChangeProcessor::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
653 DCHECK(CalledOnValidThread()); | 645 DCHECK(CalledOnValidThread()); |
654 DCHECK(has_nodes); | 646 DCHECK(has_nodes); |
655 std::string type_name = syncer::ModelTypeToString(type_); | 647 std::string type_name = ModelTypeToString(type_); |
656 std::string err_str = | 648 std::string err_str = |
657 "Server did not create the top-level " + type_name + | 649 "Server did not create the top-level " + type_name + |
658 " node. We might be running against an out-of-date server."; | 650 " node. We might be running against an out-of-date server."; |
659 *has_nodes = false; | 651 *has_nodes = false; |
660 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 652 ReadTransaction trans(FROM_HERE, share_handle()); |
661 syncer::ReadNode type_root_node(&trans); | 653 ReadNode type_root_node(&trans); |
662 if (type_root_node.InitTypeRoot(type_) != syncer::BaseNode::INIT_OK) { | 654 if (type_root_node.InitTypeRoot(type_) != BaseNode::INIT_OK) { |
663 LOG(ERROR) << err_str; | 655 LOG(ERROR) << err_str; |
664 return false; | 656 return false; |
665 } | 657 } |
666 | 658 |
667 // The sync model has user created nodes if the type's root node has any | 659 // The sync model has user created nodes if the type's root node has any |
668 // children. | 660 // children. |
669 *has_nodes = type_root_node.HasChildren(); | 661 *has_nodes = type_root_node.HasChildren(); |
670 return true; | 662 return true; |
671 } | 663 } |
672 | 664 |
673 bool GenericChangeProcessor::CryptoReadyIfNecessary() { | 665 bool GenericChangeProcessor::CryptoReadyIfNecessary() { |
674 DCHECK(CalledOnValidThread()); | 666 DCHECK(CalledOnValidThread()); |
675 // We only access the cryptographer while holding a transaction. | 667 // We only access the cryptographer while holding a transaction. |
676 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 668 ReadTransaction trans(FROM_HERE, share_handle()); |
677 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); | 669 const ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); |
678 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready(); | 670 return !encrypted_types.Has(type_) || trans.GetCryptographer()->is_ready(); |
679 } | 671 } |
680 | 672 |
681 void GenericChangeProcessor::StartImpl() {} | 673 void GenericChangeProcessor::StartImpl() {} |
682 | 674 |
683 syncer::UserShare* GenericChangeProcessor::share_handle() const { | 675 UserShare* GenericChangeProcessor::share_handle() const { |
684 DCHECK(CalledOnValidThread()); | 676 DCHECK(CalledOnValidThread()); |
685 return share_handle_; | 677 return share_handle_; |
686 } | 678 } |
687 | 679 |
688 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { | 680 void GenericChangeProcessor::UploadAllAttachmentsNotOnServer() { |
689 DCHECK(CalledOnValidThread()); | 681 DCHECK(CalledOnValidThread()); |
690 DCHECK(attachment_service_.get()); | 682 DCHECK(attachment_service_.get()); |
691 syncer::AttachmentIdList ids; | 683 AttachmentIdList ids; |
692 { | 684 { |
693 syncer::ReadTransaction trans(FROM_HERE, share_handle()); | 685 ReadTransaction trans(FROM_HERE, share_handle()); |
694 trans.GetAttachmentIdsToUpload(type_, &ids); | 686 trans.GetAttachmentIdsToUpload(type_, &ids); |
695 } | 687 } |
696 if (!ids.empty()) { | 688 if (!ids.empty()) { |
697 attachment_service_->UploadAttachments(ids); | 689 attachment_service_->UploadAttachments(ids); |
698 } | 690 } |
699 } | 691 } |
700 | 692 |
701 void GenericChangeProcessor::NotifyLocalChangeObservers( | 693 void GenericChangeProcessor::NotifyLocalChangeObservers( |
702 const syncer::syncable::Entry* current_entry, | 694 const syncable::Entry* current_entry, |
703 const syncer::SyncChange& change) { | 695 const SyncChange& change) { |
704 FOR_EACH_OBSERVER(syncer::LocalChangeObserver, local_change_observers_, | 696 FOR_EACH_OBSERVER(LocalChangeObserver, local_change_observers_, |
705 OnLocalChange(current_entry, change)); | 697 OnLocalChange(current_entry, change)); |
706 } | 698 } |
707 | 699 |
708 std::unique_ptr<syncer::AttachmentService> | 700 std::unique_ptr<AttachmentService> |
709 GenericChangeProcessor::GetAttachmentService() const { | 701 GenericChangeProcessor::GetAttachmentService() const { |
710 return std::unique_ptr<syncer::AttachmentService>( | 702 return std::unique_ptr<AttachmentService>( |
711 new syncer::AttachmentServiceProxy(attachment_service_proxy_)); | 703 new AttachmentServiceProxy(attachment_service_proxy_)); |
712 } | 704 } |
713 | 705 |
714 } // namespace sync_driver | 706 } // namespace syncer |
OLD | NEW |