This source file includes following definitions.
- SetIsFolder
- SetTitle
- SetAppSpecifics
- SetAutofillSpecifics
- SetAutofillProfileSpecifics
- SetBookmarkSpecifics
- SetNigoriSpecifics
- SetPasswordSpecifics
- SetThemeSpecifics
- SetSessionSpecifics
- SetDeviceInfoSpecifics
- SetExperimentsSpecifics
- SetPriorityPreferenceSpecifics
- SetEntitySpecifics
- ResetFromSpecifics
- SetTypedUrlSpecifics
- SetExtensionSpecifics
- SetExternalId
- transaction_
- InitByIdLookup
- InitByClientTagLookup
- InitByTagLookup
- InitBookmarkByCreation
- SetPosition
- GetEntry
- GetTransaction
- GetMutableEntryForTest
- Tombstone
- Drop
- PutPredecessor
- MarkForSyncing
#include "sync/internal_api/public/write_node.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "sync/internal_api/public/base_transaction.h"
#include "sync/internal_api/public/write_transaction.h"
#include "sync/internal_api/syncapi_internal.h"
#include "sync/protocol/app_specifics.pb.h"
#include "sync/protocol/autofill_specifics.pb.h"
#include "sync/protocol/bookmark_specifics.pb.h"
#include "sync/protocol/extension_specifics.pb.h"
#include "sync/protocol/password_specifics.pb.h"
#include "sync/protocol/session_specifics.pb.h"
#include "sync/protocol/theme_specifics.pb.h"
#include "sync/protocol/typed_url_specifics.pb.h"
#include "sync/syncable/mutable_entry.h"
#include "sync/syncable/nigori_util.h"
#include "sync/syncable/syncable_util.h"
#include "sync/util/cryptographer.h"
using std::string;
using std::vector;
namespace syncer {
using syncable::kEncryptedString;
using syncable::SPECIFICS;
static const char kDefaultNameForNewNodes[] = " ";
void WriteNode::SetIsFolder(bool folder) {
if (entry_->GetIsDir() == folder)
return;
entry_->PutIsDir(folder);
MarkForSyncing();
}
void WriteNode::SetTitle(const std::wstring& title) {
DCHECK_NE(GetModelType(), UNSPECIFIED);
ModelType type = GetModelType();
bool needs_encryption = GetTransaction()->GetEncryptedTypes().Has(type) ||
entry_->GetSpecifics().has_encrypted();
std::string new_legal_title;
if (type != BOOKMARKS && needs_encryption) {
new_legal_title = kEncryptedString;
} else {
SyncAPINameToServerName(base::WideToUTF8(title), &new_legal_title);
base::TruncateUTF8ToByteSize(new_legal_title, 255, &new_legal_title);
}
std::string current_legal_title;
if (BOOKMARKS == type &&
entry_->GetSpecifics().has_encrypted()) {
current_legal_title = GetBookmarkSpecifics().title();
} else {
current_legal_title = entry_->GetNonUniqueName();
}
bool title_matches = (current_legal_title == new_legal_title);
bool encrypted_without_overwriting_name = (needs_encryption &&
entry_->GetNonUniqueName() != kEncryptedString);
if (title_matches && !encrypted_without_overwriting_name) {
DVLOG(2) << "Title matches, dropping change.";
return;
}
if (GetModelType() == BOOKMARKS) {
sync_pb::EntitySpecifics specifics = GetEntitySpecifics();
specifics.mutable_bookmark()->set_title(new_legal_title);
SetEntitySpecifics(specifics);
}
if (needs_encryption)
entry_->PutNonUniqueName(kEncryptedString);
else
entry_->PutNonUniqueName(new_legal_title);
DVLOG(1) << "Overwriting title of type "
<< ModelTypeToString(type)
<< " and marking for syncing.";
MarkForSyncing();
}
void WriteNode::SetAppSpecifics(
const sync_pb::AppSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_app()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetAutofillSpecifics(
const sync_pb::AutofillSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_autofill()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetAutofillProfileSpecifics(
const sync_pb::AutofillProfileSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_autofill_profile()->
CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetBookmarkSpecifics(
const sync_pb::BookmarkSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_bookmark()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetNigoriSpecifics(
const sync_pb::NigoriSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_nigori()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetPasswordSpecifics(
const sync_pb::PasswordSpecificsData& data) {
DCHECK_EQ(GetModelType(), PASSWORDS);
Cryptographer* cryptographer = GetTransaction()->GetCryptographer();
const sync_pb::EntitySpecifics& old_specifics = GetEntry()->GetSpecifics();
sync_pb::EntitySpecifics entity_specifics;
if (GetModelTypeFromSpecifics(old_specifics) == PASSWORDS) {
entity_specifics.CopyFrom(old_specifics);
} else {
AddDefaultFieldValue(PASSWORDS, &entity_specifics);
}
sync_pb::PasswordSpecifics* password_specifics =
entity_specifics.mutable_password();
if (!cryptographer->Encrypt(data, password_specifics->mutable_encrypted())) {
NOTREACHED() << "Failed to encrypt password, possibly due to sync node "
<< "corruption";
return;
}
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetThemeSpecifics(
const sync_pb::ThemeSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_theme()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetSessionSpecifics(
const sync_pb::SessionSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_session()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetDeviceInfoSpecifics(
const sync_pb::DeviceInfoSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_device_info()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetExperimentsSpecifics(
const sync_pb::ExperimentsSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_experiments()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetPriorityPreferenceSpecifics(
const sync_pb::PriorityPreferenceSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_priority_preference()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetEntitySpecifics(
const sync_pb::EntitySpecifics& new_value) {
ModelType new_specifics_type =
GetModelTypeFromSpecifics(new_value);
CHECK(!new_value.password().has_client_only_encrypted_data());
DCHECK_NE(new_specifics_type, UNSPECIFIED);
DVLOG(1) << "Writing entity specifics of type "
<< ModelTypeToString(new_specifics_type);
DCHECK_EQ(new_specifics_type, GetModelType());
const sync_pb::EntitySpecifics& old_specifics = entry_->GetSpecifics();
sync_pb::EntitySpecifics new_specifics;
new_specifics.CopyFrom(new_value);
new_specifics.mutable_unknown_fields()->MergeFrom(
old_specifics.unknown_fields());
if (!UpdateEntryWithEncryption(GetTransaction()->GetWrappedTrans(),
new_specifics,
entry_)) {
return;
}
if (entry_->GetSpecifics().has_encrypted()) {
SetUnencryptedSpecifics(new_value);
}
DCHECK_EQ(new_specifics_type, GetModelType());
}
void WriteNode::ResetFromSpecifics() {
SetEntitySpecifics(GetEntitySpecifics());
}
void WriteNode::SetTypedUrlSpecifics(
const sync_pb::TypedUrlSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_typed_url()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetExtensionSpecifics(
const sync_pb::ExtensionSpecifics& new_value) {
sync_pb::EntitySpecifics entity_specifics;
entity_specifics.mutable_extension()->CopyFrom(new_value);
SetEntitySpecifics(entity_specifics);
}
void WriteNode::SetExternalId(int64 id) {
if (GetExternalId() != id)
entry_->PutLocalExternalId(id);
}
WriteNode::WriteNode(WriteTransaction* transaction)
: entry_(NULL), transaction_(transaction) {
DCHECK(transaction);
}
WriteNode::~WriteNode() {
delete entry_;
}
BaseNode::InitByLookupResult WriteNode::InitByIdLookup(int64 id) {
DCHECK(!entry_) << "Init called twice";
DCHECK_NE(id, kInvalidId);
entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::GET_BY_HANDLE, id);
if (!entry_->good())
return INIT_FAILED_ENTRY_NOT_GOOD;
if (entry_->GetIsDel())
return INIT_FAILED_ENTRY_IS_DEL;
return DecryptIfNecessary() ? INIT_OK : INIT_FAILED_DECRYPT_IF_NECESSARY;
}
BaseNode::InitByLookupResult WriteNode::InitByClientTagLookup(
ModelType model_type,
const std::string& tag) {
DCHECK(!entry_) << "Init called twice";
if (tag.empty())
return INIT_FAILED_PRECONDITION;
const std::string hash = syncable::GenerateSyncableHash(model_type, tag);
entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::GET_BY_CLIENT_TAG, hash);
if (!entry_->good())
return INIT_FAILED_ENTRY_NOT_GOOD;
if (entry_->GetIsDel())
return INIT_FAILED_ENTRY_IS_DEL;
return DecryptIfNecessary() ? INIT_OK : INIT_FAILED_DECRYPT_IF_NECESSARY;
}
BaseNode::InitByLookupResult WriteNode::InitByTagLookup(
const std::string& tag) {
DCHECK(!entry_) << "Init called twice";
if (tag.empty())
return INIT_FAILED_PRECONDITION;
entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::GET_BY_SERVER_TAG, tag);
if (!entry_->good())
return INIT_FAILED_ENTRY_NOT_GOOD;
if (entry_->GetIsDel())
return INIT_FAILED_ENTRY_IS_DEL;
ModelType model_type = GetModelType();
DCHECK_EQ(model_type, NIGORI);
return INIT_OK;
}
bool WriteNode::InitBookmarkByCreation(const BaseNode& parent,
const BaseNode* predecessor) {
DCHECK(!entry_) << "Init called twice";
if (predecessor && predecessor->GetParentId() != parent.GetId()) {
DCHECK(false);
return false;
}
syncable::Id parent_id = parent.GetEntry()->GetId();
string dummy(kDefaultNameForNewNodes);
entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::CREATE, BOOKMARKS,
parent_id, dummy);
if (!entry_->good())
return false;
entry_->PutIsDir(true);
return PutPredecessor(predecessor);
}
WriteNode::InitUniqueByCreationResult WriteNode::InitUniqueByCreation(
ModelType model_type,
const BaseNode& parent,
const std::string& tag) {
DCHECK(!entry_);
if (tag.empty()) {
LOG(WARNING) << "InitUniqueByCreation failed due to empty tag.";
return INIT_FAILED_EMPTY_TAG;
}
const std::string hash = syncable::GenerateSyncableHash(model_type, tag);
syncable::Id parent_id = parent.GetEntry()->GetId();
string dummy(kDefaultNameForNewNodes);
scoped_ptr<syncable::MutableEntry> existing_entry(
new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::GET_BY_CLIENT_TAG, hash));
if (existing_entry->good()) {
if (existing_entry->GetIsDel()) {
existing_entry->PutIsDel(false);
existing_entry->PutNonUniqueName(dummy);
existing_entry->PutParentId(parent_id);
entry_ = existing_entry.release();
} else {
return INIT_FAILED_ENTRY_ALREADY_EXISTS;
}
} else {
entry_ = new syncable::MutableEntry(transaction_->GetWrappedWriteTrans(),
syncable::CREATE,
model_type, parent_id, dummy);
if (!entry_->good())
return INIT_FAILED_COULD_NOT_CREATE_ENTRY;
entry_->PutUniqueClientTag(hash);
}
entry_->PutIsDir(false);
bool success = PutPredecessor(NULL);
if (!success)
return INIT_FAILED_SET_PREDECESSOR;
return INIT_SUCCESS;
}
bool WriteNode::SetPosition(const BaseNode& new_parent,
const BaseNode* predecessor) {
if (predecessor && predecessor->GetParentId() != new_parent.GetId()) {
DCHECK(false);
return false;
}
syncable::Id new_parent_id = new_parent.GetEntry()->GetId();
if (new_parent_id == entry_->GetParentId()) {
const syncable::Id& old = entry_->GetPredecessorId();
if ((!predecessor && old.IsRoot()) ||
(predecessor && (old == predecessor->GetEntry()->GetId()))) {
return true;
}
}
entry_->PutParentId(new_parent_id);
return PutPredecessor(predecessor);
}
const syncable::Entry* WriteNode::GetEntry() const {
return entry_;
}
const BaseTransaction* WriteNode::GetTransaction() const {
return transaction_;
}
syncable::MutableEntry* WriteNode::GetMutableEntryForTest() {
return entry_;
}
void WriteNode::Tombstone() {
MarkForSyncing();
entry_->PutIsDel(true);
}
void WriteNode::Drop() {
if (entry_->GetId().ServerKnows()) {
entry_->PutIsDel(true);
}
}
bool WriteNode::PutPredecessor(const BaseNode* predecessor) {
syncable::Id predecessor_id = predecessor ?
predecessor->GetEntry()->GetId() : syncable::Id();
if (!entry_->PutPredecessor(predecessor_id))
return false;
MarkForSyncing();
return true;
}
void WriteNode::MarkForSyncing() {
syncable::MarkForSyncing(entry_);
}
}