#include "sync/engine/process_updates_command.h"
#include <vector>
#include "base/basictypes.h"
#include "base/location.h"
#include "sync/engine/syncer.h"
#include "sync/engine/syncer_proto_util.h"
#include "sync/engine/syncer_util.h"
#include "sync/sessions/sync_session.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/syncable_proto_util.h"
#include "sync/syncable/syncable_util.h"
#include "sync/syncable/write_transaction.h"
#include "sync/util/cryptographer.h"
using std::vector;
namespace syncer {
using sessions::SyncSession;
using sessions::StatusController;
using sessions::UpdateProgress;
ProcessUpdatesCommand::ProcessUpdatesCommand() {}
ProcessUpdatesCommand::~ProcessUpdatesCommand() {}
std::set<ModelSafeGroup> ProcessUpdatesCommand::GetGroupsToChange(
const sessions::SyncSession& session) const {
return session.GetEnabledGroupsWithVerifiedUpdates();
}
SyncerError ProcessUpdatesCommand::ModelChangingExecuteImpl(
SyncSession* session) {
syncable::Directory* dir = session->context()->directory();
const sessions::UpdateProgress* progress =
session->status_controller().update_progress();
if (!progress)
return SYNCER_OK;
syncable::WriteTransaction trans(FROM_HERE, syncable::SYNCER, dir);
vector<sessions::VerifiedUpdate>::const_iterator it;
for (it = progress->VerifiedUpdatesBegin();
it != progress->VerifiedUpdatesEnd();
++it) {
const sync_pb::SyncEntity& update = it->second;
if (it->first != VERIFY_SUCCESS && it->first != VERIFY_UNDELETE)
continue;
switch (ProcessUpdate(update,
dir->GetCryptographer(&trans),
&trans)) {
case SUCCESS_PROCESSED:
case SUCCESS_STORED:
break;
default:
NOTREACHED();
break;
}
}
StatusController* status = session->mutable_status_controller();
status->mutable_update_progress()->ClearVerifiedUpdates();
return SYNCER_OK;
}
namespace {
bool ReverifyEntry(syncable::WriteTransaction* trans,
const sync_pb::SyncEntity& entry,
syncable::MutableEntry* 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,
same_id,
deleted,
is_directory,
model_type);
}
}
ServerUpdateProcessingResult ProcessUpdatesCommand::ProcessUpdate(
const sync_pb::SyncEntity& update,
const Cryptographer* cryptographer,
syncable::WriteTransaction* 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 SUCCESS_PROCESSED;
}
CreateNewEntry(trans, local_id);
syncable::MutableEntry target_entry(trans, syncable::GET_BY_ID, local_id);
if (!ReverifyEntry(trans, update, &target_entry)) {
return SUCCESS_PROCESSED;
}
if (local_id != server_id) {
DCHECK(!update.deleted());
ChangeEntryIDAndUpdateChildren(trans, &target_entry, server_id);
if (target_entry.Get(syncable::IS_UNSYNCED) ||
target_entry.Get(syncable::BASE_VERSION) > 0) {
target_entry.Put(syncable::BASE_VERSION, update.version());
}
target_entry.Put(syncable::IS_UNAPPLIED_UPDATE, true);
}
if (!update.deleted() && !target_entry.Get(syncable::SERVER_IS_DEL) &&
(SyncableIdFromProto(update.parent_id_string()) ==
target_entry.Get(syncable::SERVER_PARENT_ID)) &&
(update.position_in_parent() ==
target_entry.Get(syncable::SERVER_POSITION_IN_PARENT)) &&
update.has_specifics() && update.specifics().has_encrypted() &&
!cryptographer->CanDecrypt(update.specifics().encrypted())) {
sync_pb::EntitySpecifics prev_specifics =
target_entry.Get(syncable::SERVER_SPECIFICS);
if (!target_entry.Get(syncable::IS_UNAPPLIED_UPDATE) &&
!IsRealDataType(GetModelTypeFromSpecifics(
target_entry.Get(syncable::BASE_SERVER_SPECIFICS))) &&
(!prev_specifics.has_encrypted() ||
cryptographer->CanDecrypt(prev_specifics.encrypted()))) {
DVLOG(2) << "Storing previous server specifcs: "
<< prev_specifics.SerializeAsString();
target_entry.Put(syncable::BASE_SERVER_SPECIFICS, prev_specifics);
}
} else if (IsRealDataType(GetModelTypeFromSpecifics(
target_entry.Get(syncable::BASE_SERVER_SPECIFICS)))) {
target_entry.Put(syncable::BASE_SERVER_SPECIFICS,
sync_pb::EntitySpecifics());
}
UpdateServerFieldsFromUpdate(&target_entry, update, name);
return SUCCESS_PROCESSED;
}
}