Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(84)

Side by Side Diff: components/sync_driver/generic_change_processor.cc

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

Powered by Google App Engine
This is Rietveld 408576698