This source file includes following definitions.
- FindLocalIdToUpdate
- AttemptToUpdateEntry
- GetUniqueBookmarkTagFromUpdate
- GetUpdatePosition
- UpdateBookmarkSpecifics
- UpdateBookmarkPositioning
- UpdateServerFieldsFromUpdate
- CreateNewEntry
- UpdateLocalDataFromServerData
- ValidateCommitEntry
- MarkDeletedChildrenSynced
- VerifyNewEntry
- VerifyUpdateConsistency
- VerifyUndelete
#include "sync/engine/syncer_util.h"
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include "base/base64.h"
#include "base/location.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_number_conversions.h"
#include "sync/engine/conflict_resolver.h"
#include "sync/engine/syncer_proto_util.h"
#include "sync/engine/syncer_types.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/internal_api/public/base/unique_position.h"
#include "sync/protocol/bookmark_specifics.pb.h"
#include "sync/protocol/password_specifics.pb.h"
#include "sync/protocol/sync.pb.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/entry.h"
#include "sync/syncable/model_neutral_mutable_entry.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/syncable_changes_version.h"
#include "sync/syncable/syncable_model_neutral_write_transaction.h"
#include "sync/syncable/syncable_proto_util.h"
#include "sync/syncable/syncable_read_transaction.h"
#include "sync/syncable/syncable_util.h"
#include "sync/syncable/syncable_write_transaction.h"
#include "sync/util/cryptographer.h"
#include "sync/util/time.h"
namespace syncer {
using syncable::BASE_SERVER_SPECIFICS;
using syncable::BASE_VERSION;
using syncable::CHANGES_VERSION;
using syncable::CREATE_NEW_UPDATE_ITEM;
using syncable::CTIME;
using syncable::Directory;
using syncable::Entry;
using syncable::GET_BY_HANDLE;
using syncable::GET_BY_ID;
using syncable::ID;
using syncable::IS_DEL;
using syncable::IS_DIR;
using syncable::IS_UNAPPLIED_UPDATE;
using syncable::IS_UNSYNCED;
using syncable::Id;
using syncable::META_HANDLE;
using syncable::MTIME;
using syncable::MutableEntry;
using syncable::NON_UNIQUE_NAME;
using syncable::PARENT_ID;
using syncable::SERVER_CTIME;
using syncable::SERVER_IS_DEL;
using syncable::SERVER_IS_DIR;
using syncable::SERVER_MTIME;
using syncable::SERVER_NON_UNIQUE_NAME;
using syncable::SERVER_PARENT_ID;
using syncable::SERVER_SPECIFICS;
using syncable::SERVER_UNIQUE_POSITION;
using syncable::SERVER_VERSION;
using syncable::SPECIFICS;
using syncable::SYNCER;
using syncable::UNIQUE_BOOKMARK_TAG;
using syncable::UNIQUE_CLIENT_TAG;
using syncable::UNIQUE_POSITION;
using syncable::UNIQUE_SERVER_TAG;
using syncable::WriteTransaction;
syncable::Id FindLocalIdToUpdate(
syncable::BaseTransaction* trans,
const sync_pb::SyncEntity& update) {
const std::string& client_id = trans->directory()->cache_guid();
const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
if (update.has_client_defined_unique_tag() &&
!update.client_defined_unique_tag().empty()) {
syncable::Entry local_entry(trans, syncable::GET_BY_CLIENT_TAG,
update.client_defined_unique_tag());
if (local_entry.good()) {
if (local_entry.GetId().ServerKnows()) {
if (local_entry.GetId() != update_id) {
LOG(WARNING) << "Duplicated client tag.";
if (local_entry.GetId() < update_id) {
return syncable::GetNullId();
}
}
return local_entry.GetId();
} else {
DCHECK(0 == local_entry.GetBaseVersion() ||
CHANGES_VERSION == local_entry.GetBaseVersion());
return local_entry.GetId();
}
}
} else if (update.has_originator_cache_guid() &&
update.originator_cache_guid() == client_id) {
syncable::Id client_item_id = syncable::Id::CreateFromClientString(
update.originator_client_item_id());
DCHECK(!client_item_id.ServerKnows());
syncable::Entry local_entry(trans, GET_BY_ID, client_item_id);
if (local_entry.good() && !local_entry.GetIsDel()) {
int64 old_version = local_entry.GetBaseVersion();
int64 new_version = update.version();
DCHECK_LE(old_version, 0);
DCHECK_GT(new_version, 0);
DCHECK(local_entry.GetIsUnsynced());
DCHECK(!local_entry.GetId().ServerKnows());
DVLOG(1) << "Reuniting lost commit response IDs. server id: "
<< update_id << " local id: " << local_entry.GetId()
<< " new version: " << new_version;
return local_entry.GetId();
}
}
return update_id;
}
UpdateAttemptResponse AttemptToUpdateEntry(
syncable::WriteTransaction* const trans,
syncable::MutableEntry* const entry,
Cryptographer* cryptographer) {
CHECK(entry->good());
if (!entry->GetIsUnappliedUpdate())
return SUCCESS;
syncable::Id id = entry->GetId();
const sync_pb::EntitySpecifics& specifics = entry->GetServerSpecifics();
if (specifics.has_encrypted() &&
!cryptographer->CanDecrypt(specifics.encrypted())) {
DVLOG(1) << "Received an undecryptable "
<< ModelTypeToString(entry->GetServerModelType())
<< " update, returning conflict_encryption.";
return CONFLICT_ENCRYPTION;
} else if (specifics.has_password() &&
entry->GetUniqueServerTag().empty()) {
const sync_pb::PasswordSpecifics& password = specifics.password();
if (!cryptographer->CanDecrypt(password.encrypted())) {
DVLOG(1) << "Received an undecryptable password update, returning "
<< "conflict_encryption.";
return CONFLICT_ENCRYPTION;
}
}
if (!entry->GetServerIsDel()) {
syncable::Id new_parent = entry->GetServerParentId();
Entry parent(trans, GET_BY_ID, new_parent);
if (!parent.good() || parent.GetIsDel() || !parent.GetIsDir()) {
DVLOG(1) << "Entry has bad parent, returning conflict_hierarchy.";
return CONFLICT_HIERARCHY;
}
if (entry->GetParentId() != new_parent) {
if (!entry->GetIsDel() && !IsLegalNewParent(trans, id, new_parent)) {
DVLOG(1) << "Not updating item " << id
<< ", illegal new parent (would cause loop).";
return CONFLICT_HIERARCHY;
}
}
} else if (entry->GetIsDir()) {
Directory::Metahandles handles;
trans->directory()->GetChildHandlesById(trans, id, &handles);
if (!handles.empty()) {
DVLOG(1) << "Not deleting directory; it's not empty " << *entry;
return CONFLICT_HIERARCHY;
}
}
if (entry->GetIsUnsynced()) {
DVLOG(1) << "Skipping update, returning conflict for: " << id
<< " ; it's unsynced.";
return CONFLICT_SIMPLE;
}
if (specifics.has_encrypted()) {
DVLOG(2) << "Received a decryptable "
<< ModelTypeToString(entry->GetServerModelType())
<< " update, applying normally.";
} else {
DVLOG(2) << "Received an unencrypted "
<< ModelTypeToString(entry->GetServerModelType())
<< " update, applying normally.";
}
UpdateLocalDataFromServerData(trans, entry);
return SUCCESS;
}
std::string GetUniqueBookmarkTagFromUpdate(const sync_pb::SyncEntity& update) {
if (!update.has_originator_cache_guid() ||
!update.has_originator_client_item_id()) {
return std::string();
}
return syncable::GenerateSyncableBookmarkHash(
update.originator_cache_guid(), update.originator_client_item_id());
}
UniquePosition GetUpdatePosition(const sync_pb::SyncEntity& update,
const std::string& suffix) {
DCHECK(UniquePosition::IsValidSuffix(suffix));
if (!(SyncerProtoUtil::ShouldMaintainPosition(update))) {
return UniquePosition::CreateInvalid();
} else if (update.has_unique_position()) {
return UniquePosition::FromProto(update.unique_position());
} else if (update.has_position_in_parent()) {
return UniquePosition::FromInt64(update.position_in_parent(), suffix);
} else {
return UniquePosition::CreateInvalid();
}
}
namespace {
void UpdateBookmarkSpecifics(const std::string& singleton_tag,
const std::string& url,
const std::string& favicon_bytes,
syncable::ModelNeutralMutableEntry* local_entry) {
if (singleton_tag == "google_chrome")
return;
sync_pb::EntitySpecifics pb;
sync_pb::BookmarkSpecifics* bookmark = pb.mutable_bookmark();
if (!url.empty())
bookmark->set_url(url);
if (!favicon_bytes.empty())
bookmark->set_favicon(favicon_bytes);
local_entry->PutServerSpecifics(pb);
}
void UpdateBookmarkPositioning(
const sync_pb::SyncEntity& update,
syncable::ModelNeutralMutableEntry* local_entry) {
std::string bookmark_tag = GetUniqueBookmarkTagFromUpdate(update);
if (UniquePosition::IsValidSuffix(bookmark_tag)) {
local_entry->PutUniqueBookmarkTag(bookmark_tag);
}
UniquePosition update_pos =
GetUpdatePosition(update, local_entry->GetUniqueBookmarkTag());
if (update_pos.IsValid()) {
local_entry->PutServerUniquePosition(update_pos);
} else {
NOTREACHED();
}
}
}
void UpdateServerFieldsFromUpdate(
syncable::ModelNeutralMutableEntry* target,
const sync_pb::SyncEntity& update,
const std::string& name) {
if (update.deleted()) {
if (target->GetServerIsDel()) {
return;
}
target->PutServerIsDel(true);
if (!target->GetUniqueClientTag().empty()) {
target->PutServerVersion(0);
} else {
target->PutServerVersion(
std::max(target->GetServerVersion(), target->GetBaseVersion()) + 1);
}
target->PutIsUnappliedUpdate(true);
return;
}
DCHECK_EQ(target->GetId(), SyncableIdFromProto(update.id_string()))
<< "ID Changing not supported here";
target->PutServerParentId(SyncableIdFromProto(update.parent_id_string()));
target->PutServerNonUniqueName(name);
target->PutServerVersion(update.version());
target->PutServerCtime(ProtoTimeToTime(update.ctime()));
target->PutServerMtime(ProtoTimeToTime(update.mtime()));
target->PutServerIsDir(IsFolder(update));
if (update.has_server_defined_unique_tag()) {
const std::string& tag = update.server_defined_unique_tag();
target->PutUniqueServerTag(tag);
}
if (update.has_client_defined_unique_tag()) {
const std::string& tag = update.client_defined_unique_tag();
target->PutUniqueClientTag(tag);
}
if (update.has_specifics()) {
DCHECK_NE(GetModelType(update), UNSPECIFIED)
<< "Storing unrecognized datatype in sync database.";
target->PutServerSpecifics(update.specifics());
} else if (update.has_bookmarkdata()) {
const sync_pb::SyncEntity::BookmarkData& bookmark = update.bookmarkdata();
UpdateBookmarkSpecifics(update.server_defined_unique_tag(),
bookmark.bookmark_url(),
bookmark.bookmark_favicon(),
target);
}
if (SyncerProtoUtil::ShouldMaintainPosition(update)) {
UpdateBookmarkPositioning(update, target);
}
target->PutServerIsDel(update.deleted());
if (update.version() > target->GetBaseVersion()) {
target->PutIsUnappliedUpdate(true);
}
}
void CreateNewEntry(syncable::ModelNeutralWriteTransaction *trans,
const syncable::Id& id) {
syncable::Entry entry(trans, GET_BY_ID, id);
if (!entry.good()) {
syncable::ModelNeutralMutableEntry new_entry(
trans,
syncable::CREATE_NEW_UPDATE_ITEM,
id);
}
}
void UpdateLocalDataFromServerData(
syncable::WriteTransaction* trans,
syncable::MutableEntry* entry) {
DCHECK(!entry->GetIsUnsynced());
DCHECK(entry->GetIsUnappliedUpdate());
DVLOG(2) << "Updating entry : " << *entry;
entry->PutSpecifics(entry->GetServerSpecifics());
entry->PutBaseServerSpecifics(sync_pb::EntitySpecifics());
entry->PutIsDir(entry->GetServerIsDir());
if (entry->GetServerIsDel()) {
entry->PutIsDel(true);
} else {
entry->PutNonUniqueName(entry->GetServerNonUniqueName());
entry->PutParentId(entry->GetServerParentId());
entry->PutUniquePosition(entry->GetServerUniquePosition());
entry->PutIsDel(false);
}
entry->PutCtime(entry->GetServerCtime());
entry->PutMtime(entry->GetServerMtime());
entry->PutBaseVersion(entry->GetServerVersion());
entry->PutIsDel(entry->GetServerIsDel());
entry->PutIsUnappliedUpdate(false);
}
VerifyCommitResult ValidateCommitEntry(syncable::Entry* entry) {
syncable::Id id = entry->GetId();
if (id == entry->GetParentId()) {
CHECK(id.IsRoot()) << "Non-root item is self parenting." << *entry;
LOG(ERROR) << "Root item became unsynced " << *entry;
return VERIFY_UNSYNCABLE;
}
if (entry->IsRoot()) {
LOG(ERROR) << "Permanent item became unsynced " << *entry;
return VERIFY_UNSYNCABLE;
}
if (entry->GetIsDel() && !entry->GetId().ServerKnows()) {
return VERIFY_UNSYNCABLE;
}
return VERIFY_OK;
}
void MarkDeletedChildrenSynced(
syncable::Directory* dir,
syncable::BaseWriteTransaction* trans,
std::set<syncable::Id>* deleted_folders) {
if (deleted_folders->empty())
return;
Directory::Metahandles handles;
dir->GetUnsyncedMetaHandles(trans, &handles);
if (handles.empty())
return;
Directory::Metahandles::iterator it;
for (it = handles.begin() ; it != handles.end() ; ++it) {
syncable::ModelNeutralMutableEntry entry(trans, GET_BY_HANDLE, *it);
if (!entry.GetIsUnsynced() || !entry.GetIsDel())
continue;
syncable::Id id = entry.GetParentId();
while (id != trans->root_id()) {
if (deleted_folders->find(id) != deleted_folders->end()) {
entry.PutIsUnsynced(false);
break;
}
Entry parent(trans, GET_BY_ID, id);
if (!parent.good() || !parent.GetIsDel())
break;
id = parent.GetParentId();
}
}
}
VerifyResult VerifyNewEntry(
const sync_pb::SyncEntity& update,
syncable::Entry* target,
const bool deleted) {
if (target->good()) {
return VERIFY_UNDECIDED;
}
if (deleted) {
return VERIFY_SKIP;
}
return VERIFY_SUCCESS;
}
VerifyResult VerifyUpdateConsistency(
syncable::ModelNeutralWriteTransaction* trans,
const sync_pb::SyncEntity& update,
const bool deleted,
const bool is_directory,
ModelType model_type,
syncable::ModelNeutralMutableEntry* target) {
CHECK(target->good());
const syncable::Id& update_id = SyncableIdFromProto(update.id_string());
if (deleted)
return VERIFY_SUCCESS;
if (model_type == UNSPECIFIED) {
return VERIFY_SKIP;
}
if (target->GetServerVersion() > 0) {
if (is_directory != target->GetServerIsDir() ||
model_type != target->GetServerModelType()) {
if (target->GetIsDel()) {
return VERIFY_SKIP;
} else {
LOG(ERROR) << "Server update doesn't agree with previous updates. ";
LOG(ERROR) << " Entry: " << *target;
LOG(ERROR) << " Update: "
<< SyncerProtoUtil::SyncEntityDebugString(update);
return VERIFY_FAIL;
}
}
if (!deleted && (target->GetId() == update_id) &&
(target->GetServerIsDel() ||
(!target->GetIsUnsynced() && target->GetIsDel() &&
target->GetBaseVersion() > 0))) {
VerifyResult result = VerifyUndelete(trans, update, target);
if (VERIFY_UNDECIDED != result)
return result;
}
}
if (target->GetBaseVersion() > 0) {
if (is_directory != target->GetIsDir() ||
model_type != target->GetModelType()) {
LOG(ERROR) << "Server update doesn't agree with committed item. ";
LOG(ERROR) << " Entry: " << *target;
LOG(ERROR) << " Update: "
<< SyncerProtoUtil::SyncEntityDebugString(update);
return VERIFY_FAIL;
}
if (target->GetId() == update_id) {
if (target->GetServerVersion() > update.version()) {
LOG(WARNING) << "We've already seen a more recent version.";
LOG(WARNING) << " Entry: " << *target;
LOG(WARNING) << " Update: "
<< SyncerProtoUtil::SyncEntityDebugString(update);
return VERIFY_SKIP;
}
}
}
return VERIFY_SUCCESS;
}
VerifyResult VerifyUndelete(syncable::ModelNeutralWriteTransaction* trans,
const sync_pb::SyncEntity& update,
syncable::ModelNeutralMutableEntry* target) {
CHECK(target->good());
DVLOG(1) << "Server update is attempting undelete. " << *target
<< "Update:" << SyncerProtoUtil::SyncEntityDebugString(update);
if (target->GetIsDel()) {
if (target->GetUniqueClientTag().empty())
LOG(WARNING) << "Doing move-aside undeletion on client-tagged item.";
target->PutId(trans->directory()->NextId());
target->PutUniqueClientTag(std::string());
target->PutBaseVersion(CHANGES_VERSION);
target->PutServerVersion(0);
return VERIFY_SUCCESS;
}
if (update.version() < target->GetServerVersion()) {
LOG(WARNING) << "Update older than current server version for "
<< *target << " Update:"
<< SyncerProtoUtil::SyncEntityDebugString(update);
return VERIFY_SUCCESS;
}
return VERIFY_UNDECIDED;
}
}