Don't switch browser locale on secondary user login
[chromium-blink-merge.git] / sync / engine / syncer_util.cc
blob3960baaa6fcff37c68072e6fed6a46b5d5decb36
1 // Copyright 2012 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.
5 #include "sync/engine/syncer_util.h"
7 #include <algorithm>
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/base64.h"
13 #include "base/location.h"
14 #include "base/metrics/histogram.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "sync/engine/conflict_resolver.h"
17 #include "sync/engine/syncer_proto_util.h"
18 #include "sync/engine/syncer_types.h"
19 #include "sync/internal_api/public/base/model_type.h"
20 #include "sync/internal_api/public/base/unique_position.h"
21 #include "sync/protocol/bookmark_specifics.pb.h"
22 #include "sync/protocol/password_specifics.pb.h"
23 #include "sync/protocol/sync.pb.h"
24 #include "sync/syncable/directory.h"
25 #include "sync/syncable/entry.h"
26 #include "sync/syncable/mutable_entry.h"
27 #include "sync/syncable/syncable_changes_version.h"
28 #include "sync/syncable/syncable_proto_util.h"
29 #include "sync/syncable/syncable_read_transaction.h"
30 #include "sync/syncable/syncable_util.h"
31 #include "sync/syncable/syncable_write_transaction.h"
32 #include "sync/util/cryptographer.h"
33 #include "sync/util/time.h"
35 namespace syncer {
37 using syncable::BASE_SERVER_SPECIFICS;
38 using syncable::BASE_VERSION;
39 using syncable::CHANGES_VERSION;
40 using syncable::CREATE_NEW_UPDATE_ITEM;
41 using syncable::CTIME;
42 using syncable::Directory;
43 using syncable::Entry;
44 using syncable::GET_BY_HANDLE;
45 using syncable::GET_BY_ID;
46 using syncable::ID;
47 using syncable::IS_DEL;
48 using syncable::IS_DIR;
49 using syncable::IS_UNAPPLIED_UPDATE;
50 using syncable::IS_UNSYNCED;
51 using syncable::Id;
52 using syncable::META_HANDLE;
53 using syncable::MTIME;
54 using syncable::MutableEntry;
55 using syncable::NON_UNIQUE_NAME;
56 using syncable::PARENT_ID;
57 using syncable::SERVER_CTIME;
58 using syncable::SERVER_IS_DEL;
59 using syncable::SERVER_IS_DIR;
60 using syncable::SERVER_MTIME;
61 using syncable::SERVER_NON_UNIQUE_NAME;
62 using syncable::SERVER_PARENT_ID;
63 using syncable::SERVER_SPECIFICS;
64 using syncable::SERVER_UNIQUE_POSITION;
65 using syncable::SERVER_VERSION;
66 using syncable::SPECIFICS;
67 using syncable::SYNCER;
68 using syncable::UNIQUE_BOOKMARK_TAG;
69 using syncable::UNIQUE_CLIENT_TAG;
70 using syncable::UNIQUE_POSITION;
71 using syncable::UNIQUE_SERVER_TAG;
72 using syncable::WriteTransaction;
74 syncable::Id FindLocalIdToUpdate(
75 syncable::BaseTransaction* trans,
76 const sync_pb::SyncEntity& update) {
77 // Expected entry points of this function:
78 // SyncEntity has NOT been applied to SERVER fields.
79 // SyncEntity has NOT been applied to LOCAL fields.
80 // DB has not yet been modified, no entries created for this update.
82 const std::string& client_id = trans->directory()->cache_guid();
83 const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
85 if (update.has_client_defined_unique_tag() &&
86 !update.client_defined_unique_tag().empty()) {
87 // When a server sends down a client tag, the following cases can occur:
88 // 1) Client has entry for tag already, ID is server style, matches
89 // 2) Client has entry for tag already, ID is server, doesn't match.
90 // 3) Client has entry for tag already, ID is local, (never matches)
91 // 4) Client has no entry for tag
93 // Case 1, we don't have to do anything since the update will
94 // work just fine. Update will end up in the proper entry, via ID lookup.
95 // Case 2 - Happens very rarely due to lax enforcement of client tags
96 // on the server, if two clients commit the same tag at the same time.
97 // When this happens, we pick the lexically-least ID and ignore all other
98 // items.
99 // Case 3 - We need to replace the local ID with the server ID so that
100 // this update gets targeted at the correct local entry; we expect conflict
101 // resolution to occur.
102 // Case 4 - Perfect. Same as case 1.
104 syncable::Entry local_entry(trans, syncable::GET_BY_CLIENT_TAG,
105 update.client_defined_unique_tag());
107 // The SyncAPI equivalent of this function will return !good if IS_DEL.
108 // The syncable version will return good even if IS_DEL.
109 // TODO(chron): Unit test the case with IS_DEL and make sure.
110 if (local_entry.good()) {
111 if (local_entry.GetId().ServerKnows()) {
112 if (local_entry.GetId() != update_id) {
113 // Case 2.
114 LOG(WARNING) << "Duplicated client tag.";
115 if (local_entry.GetId() < update_id) {
116 // Signal an error; drop this update on the floor. Note that
117 // we don't server delete the item, because we don't allow it to
118 // exist locally at all. So the item will remain orphaned on
119 // the server, and we won't pay attention to it.
120 return syncable::GetNullId();
123 // Target this change to the existing local entry; later,
124 // we'll change the ID of the local entry to update_id
125 // if needed.
126 return local_entry.GetId();
127 } else {
128 // Case 3: We have a local entry with the same client tag.
129 // We should change the ID of the local entry to the server entry.
130 // This will result in an server ID with base version == 0, but that's
131 // a legal state for an item with a client tag. By changing the ID,
132 // update will now be applied to local_entry.
133 DCHECK(0 == local_entry.GetBaseVersion() ||
134 CHANGES_VERSION == local_entry.GetBaseVersion());
135 return local_entry.GetId();
138 } else if (update.has_originator_cache_guid() &&
139 update.originator_cache_guid() == client_id) {
140 // If a commit succeeds, but the response does not come back fast enough
141 // then the syncer might assume that it was never committed.
142 // The server will track the client that sent up the original commit and
143 // return this in a get updates response. When this matches a local
144 // uncommitted item, we must mutate our local item and version to pick up
145 // the committed version of the same item whose commit response was lost.
146 // There is however still a race condition if the server has not
147 // completed the commit by the time the syncer tries to get updates
148 // again. To mitigate this, we need to have the server time out in
149 // a reasonable span, our commit batches have to be small enough
150 // to process within our HTTP response "assumed alive" time.
152 // We need to check if we have an entry that didn't get its server
153 // id updated correctly. The server sends down a client ID
154 // and a local (negative) id. If we have a entry by that
155 // description, we should update the ID and version to the
156 // server side ones to avoid multiple copies of the same thing.
158 syncable::Id client_item_id = syncable::Id::CreateFromClientString(
159 update.originator_client_item_id());
160 DCHECK(!client_item_id.ServerKnows());
161 syncable::Entry local_entry(trans, GET_BY_ID, client_item_id);
163 // If it exists, then our local client lost a commit response. Use
164 // the local entry.
165 if (local_entry.good() && !local_entry.GetIsDel()) {
166 int64 old_version = local_entry.GetBaseVersion();
167 int64 new_version = update.version();
168 DCHECK_LE(old_version, 0);
169 DCHECK_GT(new_version, 0);
170 // Otherwise setting the base version could cause a consistency failure.
171 // An entry should never be version 0 and SYNCED.
172 DCHECK(local_entry.GetIsUnsynced());
174 // Just a quick sanity check.
175 DCHECK(!local_entry.GetId().ServerKnows());
177 DVLOG(1) << "Reuniting lost commit response IDs. server id: "
178 << update_id << " local id: " << local_entry.GetId()
179 << " new version: " << new_version;
181 return local_entry.GetId();
184 // Fallback: target an entry having the server ID, creating one if needed.
185 return update_id;
188 UpdateAttemptResponse AttemptToUpdateEntry(
189 syncable::WriteTransaction* const trans,
190 syncable::MutableEntry* const entry,
191 Cryptographer* cryptographer) {
192 CHECK(entry->good());
193 if (!entry->GetIsUnappliedUpdate())
194 return SUCCESS; // No work to do.
195 syncable::Id id = entry->GetId();
196 const sync_pb::EntitySpecifics& specifics = entry->GetServerSpecifics();
198 // Only apply updates that we can decrypt. If we can't decrypt the update, it
199 // is likely because the passphrase has not arrived yet. Because the
200 // passphrase may not arrive within this GetUpdates, we can't just return
201 // conflict, else we try to perform normal conflict resolution prematurely or
202 // the syncer may get stuck. As such, we return CONFLICT_ENCRYPTION, which is
203 // treated as an unresolvable conflict. See the description in syncer_types.h.
204 // This prevents any unsynced changes from commiting and postpones conflict
205 // resolution until all data can be decrypted.
206 if (specifics.has_encrypted() &&
207 !cryptographer->CanDecrypt(specifics.encrypted())) {
208 // We can't decrypt this node yet.
209 DVLOG(1) << "Received an undecryptable "
210 << ModelTypeToString(entry->GetServerModelType())
211 << " update, returning conflict_encryption.";
212 return CONFLICT_ENCRYPTION;
213 } else if (specifics.has_password() &&
214 entry->GetUniqueServerTag().empty()) {
215 // Passwords use their own legacy encryption scheme.
216 const sync_pb::PasswordSpecifics& password = specifics.password();
217 if (!cryptographer->CanDecrypt(password.encrypted())) {
218 DVLOG(1) << "Received an undecryptable password update, returning "
219 << "conflict_encryption.";
220 return CONFLICT_ENCRYPTION;
224 if (!entry->GetServerIsDel()) {
225 syncable::Id new_parent = entry->GetServerParentId();
226 Entry parent(trans, GET_BY_ID, new_parent);
227 // A note on non-directory parents:
228 // We catch most unfixable tree invariant errors at update receipt time,
229 // however we deal with this case here because we may receive the child
230 // first then the illegal parent. Instead of dealing with it twice in
231 // different ways we deal with it once here to reduce the amount of code and
232 // potential errors.
233 if (!parent.good() || parent.GetIsDel() || !parent.GetIsDir()) {
234 DVLOG(1) << "Entry has bad parent, returning conflict_hierarchy.";
235 return CONFLICT_HIERARCHY;
237 if (entry->GetParentId() != new_parent) {
238 if (!entry->GetIsDel() && !IsLegalNewParent(trans, id, new_parent)) {
239 DVLOG(1) << "Not updating item " << id
240 << ", illegal new parent (would cause loop).";
241 return CONFLICT_HIERARCHY;
244 } else if (entry->GetIsDir()) {
245 Directory::Metahandles handles;
246 trans->directory()->GetChildHandlesById(trans, id, &handles);
247 if (!handles.empty()) {
248 // If we have still-existing children, then we need to deal with
249 // them before we can process this change.
250 DVLOG(1) << "Not deleting directory; it's not empty " << *entry;
251 return CONFLICT_HIERARCHY;
255 if (entry->GetIsUnsynced()) {
256 DVLOG(1) << "Skipping update, returning conflict for: " << id
257 << " ; it's unsynced.";
258 return CONFLICT_SIMPLE;
261 if (specifics.has_encrypted()) {
262 DVLOG(2) << "Received a decryptable "
263 << ModelTypeToString(entry->GetServerModelType())
264 << " update, applying normally.";
265 } else {
266 DVLOG(2) << "Received an unencrypted "
267 << ModelTypeToString(entry->GetServerModelType())
268 << " update, applying normally.";
271 UpdateLocalDataFromServerData(trans, entry);
273 return SUCCESS;
276 std::string GetUniqueBookmarkTagFromUpdate(const sync_pb::SyncEntity& update) {
277 if (!update.has_originator_cache_guid() ||
278 !update.has_originator_client_item_id()) {
279 return std::string();
282 return syncable::GenerateSyncableBookmarkHash(
283 update.originator_cache_guid(), update.originator_client_item_id());
286 UniquePosition GetUpdatePosition(const sync_pb::SyncEntity& update,
287 const std::string& suffix) {
288 DCHECK(UniquePosition::IsValidSuffix(suffix));
289 if (!(SyncerProtoUtil::ShouldMaintainPosition(update))) {
290 return UniquePosition::CreateInvalid();
291 } else if (update.has_unique_position()) {
292 return UniquePosition::FromProto(update.unique_position());
293 } else if (update.has_position_in_parent()) {
294 return UniquePosition::FromInt64(update.position_in_parent(), suffix);
295 } else {
296 return UniquePosition::CreateInvalid();
300 namespace {
302 // Helper to synthesize a new-style sync_pb::EntitySpecifics for use locally,
303 // when the server speaks only the old sync_pb::SyncEntity_BookmarkData-based
304 // protocol.
305 void UpdateBookmarkSpecifics(const std::string& singleton_tag,
306 const std::string& url,
307 const std::string& favicon_bytes,
308 MutableEntry* local_entry) {
309 // In the new-style protocol, the server no longer sends bookmark info for
310 // the "google_chrome" folder. Mimic that here.
311 if (singleton_tag == "google_chrome")
312 return;
313 sync_pb::EntitySpecifics pb;
314 sync_pb::BookmarkSpecifics* bookmark = pb.mutable_bookmark();
315 if (!url.empty())
316 bookmark->set_url(url);
317 if (!favicon_bytes.empty())
318 bookmark->set_favicon(favicon_bytes);
319 local_entry->PutServerSpecifics(pb);
322 void UpdateBookmarkPositioning(const sync_pb::SyncEntity& update,
323 MutableEntry* local_entry) {
324 // Update our unique bookmark tag. In many cases this will be identical to
325 // the tag we already have. However, clients that have recently upgraded to
326 // versions that support unique positions will have incorrect tags. See the
327 // v86 migration logic in directory_backing_store.cc for more information.
329 // Both the old and new values are unique to this element. Applying this
330 // update will not risk the creation of conflicting unique tags.
331 std::string bookmark_tag = GetUniqueBookmarkTagFromUpdate(update);
332 if (UniquePosition::IsValidSuffix(bookmark_tag)) {
333 local_entry->PutUniqueBookmarkTag(bookmark_tag);
336 // Update our position.
337 UniquePosition update_pos =
338 GetUpdatePosition(update, local_entry->GetUniqueBookmarkTag());
339 if (update_pos.IsValid()) {
340 local_entry->PutServerUniquePosition(update_pos);
341 } else {
342 // TODO(sync): This and other cases of unexpected input should be handled
343 // better.
344 NOTREACHED();
348 } // namespace
350 void UpdateServerFieldsFromUpdate(
351 MutableEntry* target,
352 const sync_pb::SyncEntity& update,
353 const std::string& name) {
354 if (update.deleted()) {
355 if (target->GetServerIsDel()) {
356 // If we already think the item is server-deleted, we're done.
357 // Skipping these cases prevents our committed deletions from coming
358 // back and overriding subsequent undeletions. For non-deleted items,
359 // the version number check has a similar effect.
360 return;
362 // The server returns very lightweight replies for deletions, so we don't
363 // clobber a bunch of fields on delete.
364 target->PutServerIsDel(true);
365 if (!target->GetUniqueClientTag().empty()) {
366 // Items identified by the client unique tag are undeletable; when
367 // they're deleted, they go back to version 0.
368 target->PutServerVersion(0);
369 } else {
370 // Otherwise, fake a server version by bumping the local number.
371 target->PutServerVersion(
372 std::max(target->GetServerVersion(), target->GetBaseVersion()) + 1);
374 target->PutIsUnappliedUpdate(true);
375 return;
378 DCHECK_EQ(target->GetId(), SyncableIdFromProto(update.id_string()))
379 << "ID Changing not supported here";
380 target->PutServerParentId(SyncableIdFromProto(update.parent_id_string()));
381 target->PutServerNonUniqueName(name);
382 target->PutServerVersion(update.version());
383 target->PutServerCtime(ProtoTimeToTime(update.ctime()));
384 target->PutServerMtime(ProtoTimeToTime(update.mtime()));
385 target->PutServerIsDir(IsFolder(update));
386 if (update.has_server_defined_unique_tag()) {
387 const std::string& tag = update.server_defined_unique_tag();
388 target->PutUniqueServerTag(tag);
390 if (update.has_client_defined_unique_tag()) {
391 const std::string& tag = update.client_defined_unique_tag();
392 target->PutUniqueClientTag(tag);
394 // Store the datatype-specific part as a protobuf.
395 if (update.has_specifics()) {
396 DCHECK_NE(GetModelType(update), UNSPECIFIED)
397 << "Storing unrecognized datatype in sync database.";
398 target->PutServerSpecifics(update.specifics());
399 } else if (update.has_bookmarkdata()) {
400 // Legacy protocol response for bookmark data.
401 const sync_pb::SyncEntity::BookmarkData& bookmark = update.bookmarkdata();
402 UpdateBookmarkSpecifics(update.server_defined_unique_tag(),
403 bookmark.bookmark_url(),
404 bookmark.bookmark_favicon(),
405 target);
407 if (SyncerProtoUtil::ShouldMaintainPosition(update)) {
408 UpdateBookmarkPositioning(update, target);
411 target->PutServerIsDel(update.deleted());
412 // We only mark the entry as unapplied if its version is greater than the
413 // local data. If we're processing the update that corresponds to one of our
414 // commit we don't apply it as time differences may occur.
415 if (update.version() > target->GetBaseVersion()) {
416 target->PutIsUnappliedUpdate(true);
420 // Creates a new Entry iff no Entry exists with the given id.
421 void CreateNewEntry(syncable::WriteTransaction *trans,
422 const syncable::Id& id) {
423 syncable::MutableEntry entry(trans, GET_BY_ID, id);
424 if (!entry.good()) {
425 syncable::MutableEntry new_entry(trans, syncable::CREATE_NEW_UPDATE_ITEM,
426 id);
430 // This function is called on an entry when we can update the user-facing data
431 // from the server data.
432 void UpdateLocalDataFromServerData(
433 syncable::WriteTransaction* trans,
434 syncable::MutableEntry* entry) {
435 DCHECK(!entry->GetIsUnsynced());
436 DCHECK(entry->GetIsUnappliedUpdate());
438 DVLOG(2) << "Updating entry : " << *entry;
439 // Start by setting the properties that determine the model_type.
440 entry->PutSpecifics(entry->GetServerSpecifics());
441 // Clear the previous server specifics now that we're applying successfully.
442 entry->PutBaseServerSpecifics(sync_pb::EntitySpecifics());
443 entry->PutIsDir(entry->GetServerIsDir());
444 // This strange dance around the IS_DEL flag avoids problems when setting
445 // the name.
446 // TODO(chron): Is this still an issue? Unit test this codepath.
447 if (entry->GetServerIsDel()) {
448 entry->PutIsDel(true);
449 } else {
450 entry->PutNonUniqueName(entry->GetServerNonUniqueName());
451 entry->PutParentId(entry->GetServerParentId());
452 entry->PutUniquePosition(entry->GetServerUniquePosition());
453 entry->PutIsDel(false);
456 entry->PutCtime(entry->GetServerCtime());
457 entry->PutMtime(entry->GetServerMtime());
458 entry->PutBaseVersion(entry->GetServerVersion());
459 entry->PutIsDel(entry->GetServerIsDel());
460 entry->PutIsUnappliedUpdate(false);
463 VerifyCommitResult ValidateCommitEntry(syncable::Entry* entry) {
464 syncable::Id id = entry->GetId();
465 if (id == entry->GetParentId()) {
466 CHECK(id.IsRoot()) << "Non-root item is self parenting." << *entry;
467 // If the root becomes unsynced it can cause us problems.
468 LOG(ERROR) << "Root item became unsynced " << *entry;
469 return VERIFY_UNSYNCABLE;
471 if (entry->IsRoot()) {
472 LOG(ERROR) << "Permanent item became unsynced " << *entry;
473 return VERIFY_UNSYNCABLE;
475 if (entry->GetIsDel() && !entry->GetId().ServerKnows()) {
476 // Drop deleted uncommitted entries.
477 return VERIFY_UNSYNCABLE;
479 return VERIFY_OK;
482 void MarkDeletedChildrenSynced(
483 syncable::Directory* dir,
484 std::set<syncable::Id>* deleted_folders) {
485 // There's two options here.
486 // 1. Scan deleted unsynced entries looking up their pre-delete tree for any
487 // of the deleted folders.
488 // 2. Take each folder and do a tree walk of all entries underneath it.
489 // #2 has a lower big O cost, but writing code to limit the time spent inside
490 // the transaction during each step is simpler with 1. Changing this decision
491 // may be sensible if this code shows up in profiling.
492 if (deleted_folders->empty())
493 return;
494 Directory::Metahandles handles;
496 syncable::ReadTransaction trans(FROM_HERE, dir);
497 dir->GetUnsyncedMetaHandles(&trans, &handles);
499 if (handles.empty())
500 return;
501 Directory::Metahandles::iterator it;
502 for (it = handles.begin() ; it != handles.end() ; ++it) {
503 // Single transaction / entry we deal with.
504 WriteTransaction trans(FROM_HERE, SYNCER, dir);
505 MutableEntry entry(&trans, GET_BY_HANDLE, *it);
506 if (!entry.GetIsUnsynced() || !entry.GetIsDel())
507 continue;
508 syncable::Id id = entry.GetParentId();
509 while (id != trans.root_id()) {
510 if (deleted_folders->find(id) != deleted_folders->end()) {
511 // We've synced the deletion of this deleted entries parent.
512 entry.PutIsUnsynced(false);
513 break;
515 Entry parent(&trans, GET_BY_ID, id);
516 if (!parent.good() || !parent.GetIsDel())
517 break;
518 id = parent.GetParentId();
523 VerifyResult VerifyNewEntry(
524 const sync_pb::SyncEntity& update,
525 syncable::Entry* target,
526 const bool deleted) {
527 if (target->good()) {
528 // Not a new update.
529 return VERIFY_UNDECIDED;
531 if (deleted) {
532 // Deletion of an item we've never seen can be ignored.
533 return VERIFY_SKIP;
536 return VERIFY_SUCCESS;
539 // Assumes we have an existing entry; check here for updates that break
540 // consistency rules.
541 VerifyResult VerifyUpdateConsistency(
542 syncable::WriteTransaction* trans,
543 const sync_pb::SyncEntity& update,
544 syncable::MutableEntry* target,
545 const bool deleted,
546 const bool is_directory,
547 ModelType model_type) {
549 CHECK(target->good());
550 const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
552 // If the update is a delete, we don't really need to worry at this stage.
553 if (deleted)
554 return VERIFY_SUCCESS;
556 if (model_type == UNSPECIFIED) {
557 // This update is to an item of a datatype we don't recognize. The server
558 // shouldn't have sent it to us. Throw it on the ground.
559 return VERIFY_SKIP;
562 if (target->GetServerVersion() > 0) {
563 // Then we've had an update for this entry before.
564 if (is_directory != target->GetServerIsDir() ||
565 model_type != target->GetServerModelType()) {
566 if (target->GetIsDel()) { // If we've deleted the item, we don't care.
567 return VERIFY_SKIP;
568 } else {
569 LOG(ERROR) << "Server update doesn't agree with previous updates. ";
570 LOG(ERROR) << " Entry: " << *target;
571 LOG(ERROR) << " Update: "
572 << SyncerProtoUtil::SyncEntityDebugString(update);
573 return VERIFY_FAIL;
577 if (!deleted && (target->GetId() == update_id) &&
578 (target->GetServerIsDel() ||
579 (!target->GetIsUnsynced() && target->GetIsDel() &&
580 target->GetBaseVersion() > 0))) {
581 // An undelete. The latter case in the above condition is for
582 // when the server does not give us an update following the
583 // commit of a delete, before undeleting.
584 // Undeletion is common for items that reuse the client-unique tag.
585 VerifyResult result = VerifyUndelete(trans, update, target);
586 if (VERIFY_UNDECIDED != result)
587 return result;
590 if (target->GetBaseVersion() > 0) {
591 // We've committed this update in the past.
592 if (is_directory != target->GetIsDir() ||
593 model_type != target->GetModelType()) {
594 LOG(ERROR) << "Server update doesn't agree with committed item. ";
595 LOG(ERROR) << " Entry: " << *target;
596 LOG(ERROR) << " Update: "
597 << SyncerProtoUtil::SyncEntityDebugString(update);
598 return VERIFY_FAIL;
600 if (target->GetId() == update_id) {
601 if (target->GetServerVersion() > update.version()) {
602 LOG(WARNING) << "We've already seen a more recent version.";
603 LOG(WARNING) << " Entry: " << *target;
604 LOG(WARNING) << " Update: "
605 << SyncerProtoUtil::SyncEntityDebugString(update);
606 return VERIFY_SKIP;
610 return VERIFY_SUCCESS;
613 // Assumes we have an existing entry; verify an update that seems to be
614 // expressing an 'undelete'
615 VerifyResult VerifyUndelete(syncable::WriteTransaction* trans,
616 const sync_pb::SyncEntity& update,
617 syncable::MutableEntry* target) {
618 // TODO(nick): We hit this path for items deleted items that the server
619 // tells us to re-create; only deleted items with positive base versions
620 // will hit this path. However, it's not clear how such an undeletion
621 // would actually succeed on the server; in the protocol, a base
622 // version of 0 is required to undelete an object. This codepath
623 // should be deprecated in favor of client-tag style undeletion
624 // (where items go to version 0 when they're deleted), or else
625 // removed entirely (if this type of undeletion is indeed impossible).
626 CHECK(target->good());
627 DVLOG(1) << "Server update is attempting undelete. " << *target
628 << "Update:" << SyncerProtoUtil::SyncEntityDebugString(update);
629 // Move the old one aside and start over. It's too tricky to get the old one
630 // back into a state that would pass CheckTreeInvariants().
631 if (target->GetIsDel()) {
632 if (target->GetUniqueClientTag().empty())
633 LOG(WARNING) << "Doing move-aside undeletion on client-tagged item.";
634 target->PutId(trans->directory()->NextId());
635 target->PutUniqueClientTag(std::string());
636 target->PutBaseVersion(CHANGES_VERSION);
637 target->PutServerVersion(0);
638 return VERIFY_SUCCESS;
640 if (update.version() < target->GetServerVersion()) {
641 LOG(WARNING) << "Update older than current server version for "
642 << *target << " Update:"
643 << SyncerProtoUtil::SyncEntityDebugString(update);
644 return VERIFY_SUCCESS; // Expected in new sync protocol.
646 return VERIFY_UNDECIDED;
649 } // namespace syncer