This source file includes following definitions.
- UpdateContainsNewVersion
- VerifyTagConsistency
- VerifyUpdate
- ReverifyEntry
- ProcessUpdate
- ProcessDownloadedUpdates
- ExpireEntriesByVersion
#include "sync/engine/process_updates_util.h"
#include "base/location.h"
#include "sync/engine/syncer_proto_util.h"
#include "sync/engine/syncer_types.h"
#include "sync/engine/syncer_util.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/model_neutral_mutable_entry.h"
#include "sync/syncable/syncable_model_neutral_write_transaction.h"
#include "sync/syncable/syncable_proto_util.h"
#include "sync/syncable/syncable_util.h"
#include "sync/util/cryptographer.h"
namespace syncer {
using sessions::StatusController;
using syncable::GET_BY_ID;
namespace {
bool UpdateContainsNewVersion(syncable::BaseTransaction *trans,
const sync_pb::SyncEntity &update) {
int64 existing_version = -1;
syncable::Entry existing_entry(trans, GET_BY_ID,
SyncableIdFromProto(update.id_string()));
if (existing_entry.good())
existing_version = existing_entry.GetBaseVersion();
if (!existing_entry.good() && update.deleted()) {
return false;
}
if (existing_entry.good() &&
!existing_entry.GetUniqueClientTag().empty() &&
existing_entry.GetIsDel() &&
update.deleted()) {
return false;
}
return existing_version < update.version();
}
VerifyResult VerifyTagConsistency(
const sync_pb::SyncEntity& entry,
const syncable::ModelNeutralMutableEntry& same_id) {
if (entry.has_client_defined_unique_tag() &&
entry.client_defined_unique_tag() !=
same_id.GetUniqueClientTag()) {
return VERIFY_FAIL;
}
return VERIFY_UNDECIDED;
}
VerifyResult VerifyUpdate(
syncable::ModelNeutralWriteTransaction* trans,
const sync_pb::SyncEntity& entry,
ModelType requested_type) {
syncable::Id id = SyncableIdFromProto(entry.id_string());
VerifyResult result = VERIFY_FAIL;
const bool deleted = entry.has_deleted() && entry.deleted();
const bool is_directory = IsFolder(entry);
const ModelType model_type = GetModelType(entry);
if (!id.ServerKnows()) {
LOG(ERROR) << "Illegal negative id in received updates";
return result;
}
{
const std::string name = SyncerProtoUtil::NameFromSyncEntity(entry);
if (name.empty() && !deleted) {
LOG(ERROR) << "Zero length name in non-deleted update";
return result;
}
}
syncable::ModelNeutralMutableEntry same_id(trans, GET_BY_ID, id);
result = VerifyNewEntry(entry, &same_id, deleted);
ModelType placement_type = !deleted ? GetModelType(entry)
: same_id.good() ? same_id.GetModelType() : UNSPECIFIED;
if (VERIFY_UNDECIDED == result) {
result = VerifyTagConsistency(entry, same_id);
}
if (VERIFY_UNDECIDED == result) {
if (deleted) {
if (IsRealDataType(placement_type) && requested_type != placement_type) {
result = VERIFY_SKIP;
} else {
result = VERIFY_SUCCESS;
}
}
}
if (VERIFY_UNDECIDED == result) {
result = VerifyUpdateConsistency(trans, entry, deleted,
is_directory, model_type, &same_id);
}
if (VERIFY_UNDECIDED == result)
result = VERIFY_SUCCESS;
return result;
}
bool ReverifyEntry(syncable::ModelNeutralWriteTransaction* trans,
const sync_pb::SyncEntity& entry,
syncable::ModelNeutralMutableEntry* same_id) {
const bool deleted = entry.has_deleted() && entry.deleted();
const bool is_directory = IsFolder(entry);
const ModelType model_type = GetModelType(entry);
return VERIFY_SUCCESS == VerifyUpdateConsistency(trans,
entry,
deleted,
is_directory,
model_type,
same_id);
}
void ProcessUpdate(
const sync_pb::SyncEntity& update,
const Cryptographer* cryptographer,
syncable::ModelNeutralWriteTransaction* const trans) {
const syncable::Id& server_id = SyncableIdFromProto(update.id_string());
const std::string name = SyncerProtoUtil::NameFromSyncEntity(update);
syncable::Id local_id = FindLocalIdToUpdate(trans, update);
if (local_id.IsNull()) {
return;
}
CreateNewEntry(trans, local_id);
syncable::ModelNeutralMutableEntry target_entry(trans, GET_BY_ID, local_id);
if (!ReverifyEntry(trans, update, &target_entry)) {
return;
}
if (local_id != server_id) {
DCHECK(!update.deleted());
ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
if (target_entry.GetIsUnsynced() || target_entry.GetBaseVersion() > 0) {
target_entry.PutBaseVersion(update.version());
}
target_entry.PutIsUnappliedUpdate(true);
}
bool position_matches = false;
if (target_entry.ShouldMaintainPosition() && !update.deleted()) {
std::string update_tag = GetUniqueBookmarkTagFromUpdate(update);
if (UniquePosition::IsValidSuffix(update_tag)) {
position_matches = GetUpdatePosition(update, update_tag).Equals(
target_entry.GetServerUniquePosition());
} else {
NOTREACHED();
}
} else {
position_matches = true;
}
if (!update.deleted() && !target_entry.GetServerIsDel() &&
(SyncableIdFromProto(update.parent_id_string()) ==
target_entry.GetServerParentId()) &&
position_matches &&
update.has_specifics() && update.specifics().has_encrypted() &&
!cryptographer->CanDecrypt(update.specifics().encrypted())) {
sync_pb::EntitySpecifics prev_specifics =
target_entry.GetServerSpecifics();
if (!target_entry.GetIsUnappliedUpdate() &&
!IsRealDataType(GetModelTypeFromSpecifics(
target_entry.GetBaseServerSpecifics())) &&
(!prev_specifics.has_encrypted() ||
cryptographer->CanDecrypt(prev_specifics.encrypted()))) {
DVLOG(2) << "Storing previous server specifcs: "
<< prev_specifics.SerializeAsString();
target_entry.PutBaseServerSpecifics(prev_specifics);
}
} else if (IsRealDataType(GetModelTypeFromSpecifics(
target_entry.GetBaseServerSpecifics()))) {
target_entry.PutBaseServerSpecifics(
sync_pb::EntitySpecifics());
}
UpdateServerFieldsFromUpdate(&target_entry, update, name);
return;
}
}
void ProcessDownloadedUpdates(
syncable::Directory* dir,
syncable::ModelNeutralWriteTransaction* trans,
ModelType type,
const SyncEntityList& applicable_updates,
sessions::StatusController* status) {
for (SyncEntityList::const_iterator update_it = applicable_updates.begin();
update_it != applicable_updates.end(); ++update_it) {
DCHECK_EQ(type, GetModelType(**update_it));
if (!UpdateContainsNewVersion(trans, **update_it))
status->increment_num_reflected_updates_downloaded_by(1);
if ((*update_it)->deleted())
status->increment_num_tombstone_updates_downloaded_by(1);
VerifyResult verify_result = VerifyUpdate(trans, **update_it, type);
if (verify_result != VERIFY_SUCCESS && verify_result != VERIFY_UNDELETE)
continue;
ProcessUpdate(**update_it, dir->GetCryptographer(trans), trans);
}
}
void ExpireEntriesByVersion(syncable::Directory* dir,
syncable::ModelNeutralWriteTransaction* trans,
ModelType type,
int64 version_watermark) {
syncable::Directory::Metahandles handles;
dir->GetMetaHandlesOfType(trans, type, &handles);
for (size_t i = 0; i < handles.size(); ++i) {
syncable::ModelNeutralMutableEntry entry(trans, syncable::GET_BY_HANDLE,
handles[i]);
if (!entry.good() || !entry.GetId().ServerKnows() ||
entry.GetUniqueServerTag() == ModelTypeToRootTag(type) ||
entry.GetIsUnappliedUpdate() || entry.GetIsUnsynced() ||
entry.GetIsDel() || entry.GetServerIsDel() ||
entry.GetBaseVersion() >= version_watermark) {
continue;
}
entry.PutServerIsDel(true);
entry.PutServerVersion(version_watermark);
entry.PutIsUnappliedUpdate(true);
}
}
}