// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "sync/internal_api/sync_rollback_manager_base.h" #include "sync/internal_api/public/base/model_type.h" #include "sync/internal_api/public/internal_components_factory.h" #include "sync/internal_api/public/read_node.h" #include "sync/internal_api/public/read_transaction.h" #include "sync/internal_api/public/util/syncer_error.h" #include "sync/internal_api/public/write_transaction.h" #include "sync/syncable/directory_backing_store.h" #include "sync/syncable/mutable_entry.h" namespace { // Permanent bookmark folders as defined in bookmark_model_associator.cc. const char kBookmarkBarTag[] = "bookmark_bar"; const char kMobileBookmarksTag[] = "synced_bookmarks"; const char kOtherBookmarksTag[] = "other_bookmarks"; } // anonymous namespace namespace syncer { SyncRollbackManagerBase::SyncRollbackManagerBase() : report_unrecoverable_error_function_(NULL), weak_ptr_factory_(this) { } SyncRollbackManagerBase::~SyncRollbackManagerBase() { } void SyncRollbackManagerBase::Init( const base::FilePath& database_location, const WeakHandle& event_handler, const std::string& sync_server_and_path, int sync_server_port, bool use_ssl, scoped_ptr post_factory, const std::vector >& workers, ExtensionsActivity* extensions_activity, SyncManager::ChangeDelegate* change_delegate, const SyncCredentials& credentials, const std::string& invalidator_client_id, const std::string& restored_key_for_bootstrapping, const std::string& restored_keystore_key_for_bootstrapping, InternalComponentsFactory* internal_components_factory, Encryptor* encryptor, scoped_ptr unrecoverable_error_handler, ReportUnrecoverableErrorFunction report_unrecoverable_error_function, CancelationSignal* cancelation_signal) { unrecoverable_error_handler_ = unrecoverable_error_handler.Pass(); report_unrecoverable_error_function_ = report_unrecoverable_error_function; if (!InitBackupDB(database_location, internal_components_factory)) { NotifyInitializationFailure(); return; } NotifyInitializationSuccess(); } void SyncRollbackManagerBase::ThrowUnrecoverableError() { NOTREACHED(); } ModelTypeSet SyncRollbackManagerBase::InitialSyncEndedTypes() { return share_.directory->InitialSyncEndedTypes(); } ModelTypeSet SyncRollbackManagerBase::GetTypesWithEmptyProgressMarkerToken( ModelTypeSet types) { ModelTypeSet inited_types = share_.directory->InitialSyncEndedTypes(); types.RemoveAll(inited_types); return types; } bool SyncRollbackManagerBase::PurgePartiallySyncedTypes() { NOTREACHED(); return true; } void SyncRollbackManagerBase::UpdateCredentials( const SyncCredentials& credentials) { NOTREACHED(); } void SyncRollbackManagerBase::StartSyncingNormally( const ModelSafeRoutingInfo& routing_info){ } void SyncRollbackManagerBase::ConfigureSyncer( ConfigureReason reason, ModelTypeSet to_download, ModelTypeSet to_purge, ModelTypeSet to_journal, ModelTypeSet to_unapply, const ModelSafeRoutingInfo& new_routing_info, const base::Closure& ready_task, const base::Closure& retry_task) { DCHECK(to_purge.Empty()); DCHECK(to_journal.Empty()); DCHECK(to_unapply.Empty()); for (ModelTypeSet::Iterator type = to_download.First(); type.Good(); type.Inc()) { if (InitTypeRootNode(type.Get())) { if (type.Get() == BOOKMARKS) { InitBookmarkFolder(kBookmarkBarTag); InitBookmarkFolder(kMobileBookmarksTag); InitBookmarkFolder(kOtherBookmarksTag); } } } ready_task.Run(); } void SyncRollbackManagerBase::OnInvalidatorStateChange(InvalidatorState state) { } void SyncRollbackManagerBase::OnIncomingInvalidation( const ObjectIdInvalidationMap& invalidation_map) { NOTREACHED(); } void SyncRollbackManagerBase::AddObserver(SyncManager::Observer* observer) { observers_.AddObserver(observer); } void SyncRollbackManagerBase::RemoveObserver(SyncManager::Observer* observer) { observers_.RemoveObserver(observer); } SyncStatus SyncRollbackManagerBase::GetDetailedStatus() const { return SyncStatus(); } void SyncRollbackManagerBase::SaveChanges() { } void SyncRollbackManagerBase::ShutdownOnSyncThread() { if (share_.directory) { SaveChanges(); share_.directory->Close(); share_.directory.reset(); } } UserShare* SyncRollbackManagerBase::GetUserShare() { return &share_; } const std::string SyncRollbackManagerBase::cache_guid() { return share_.directory->cache_guid(); } bool SyncRollbackManagerBase::ReceivedExperiment(Experiments* experiments) { return false; } bool SyncRollbackManagerBase::HasUnsyncedItems() { ReadTransaction trans(FROM_HERE, &share_); syncable::Directory::Metahandles unsynced; share_.directory->GetUnsyncedMetaHandles(trans.GetWrappedTrans(), &unsynced); return !unsynced.empty(); } SyncEncryptionHandler* SyncRollbackManagerBase::GetEncryptionHandler() { return NULL; } void SyncRollbackManagerBase::RefreshTypes(ModelTypeSet types) { } void SyncRollbackManagerBase::HandleTransactionCompleteChangeEvent( ModelTypeSet models_with_changes) { } ModelTypeSet SyncRollbackManagerBase::HandleTransactionEndingChangeEvent( const syncable::ImmutableWriteTransactionInfo& write_transaction_info, syncable::BaseTransaction* trans) { return ModelTypeSet(); } void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncApi( const syncable::ImmutableWriteTransactionInfo& write_transaction_info, syncable::BaseTransaction* trans, std::vector* entries_changed) { } void SyncRollbackManagerBase::HandleCalculateChangesChangeEventFromSyncer( const syncable::ImmutableWriteTransactionInfo& write_transaction_info, syncable::BaseTransaction* trans, std::vector* entries_changed) { } void SyncRollbackManagerBase::OnTransactionWrite( const syncable::ImmutableWriteTransactionInfo& write_transaction_info, ModelTypeSet models_with_changes) { } void SyncRollbackManagerBase::NotifyInitializationSuccess() { FOR_EACH_OBSERVER( SyncManager::Observer, observers_, OnInitializationComplete( MakeWeakHandle(base::WeakPtr()), MakeWeakHandle(base::WeakPtr()), true, InitialSyncEndedTypes())); } void SyncRollbackManagerBase::NotifyInitializationFailure() { FOR_EACH_OBSERVER( SyncManager::Observer, observers_, OnInitializationComplete( MakeWeakHandle(base::WeakPtr()), MakeWeakHandle(base::WeakPtr()), false, InitialSyncEndedTypes())); } std::string SyncRollbackManagerBase::GetOwnerName() const { return ""; } syncer::SyncCoreProxy* SyncRollbackManagerBase::GetSyncCoreProxy() { return NULL; } ScopedVector SyncRollbackManagerBase::GetBufferedProtocolEvents() { return ScopedVector().Pass(); } scoped_ptr SyncRollbackManagerBase::GetAllNodesForType( syncer::ModelType type) { ReadTransaction trans(FROM_HERE, GetUserShare()); scoped_ptr nodes( trans.GetDirectory()->GetNodeDetailsForType(trans.GetWrappedTrans(), type)); return nodes.Pass(); } bool SyncRollbackManagerBase::InitBackupDB( const base::FilePath& sync_folder, InternalComponentsFactory* internal_components_factory) { base::FilePath backup_db_path = sync_folder.Append( syncable::Directory::kSyncDatabaseFilename); scoped_ptr backing_store = internal_components_factory->BuildDirectoryBackingStore( "backup", backup_db_path).Pass(); DCHECK(backing_store.get()); share_.directory.reset( new syncable::Directory( backing_store.release(), unrecoverable_error_handler_.get(), report_unrecoverable_error_function_, NULL, NULL)); return syncable::OPENED == share_.directory->Open( "backup", this, MakeWeakHandle(weak_ptr_factory_.GetWeakPtr())); } bool SyncRollbackManagerBase::InitTypeRootNode(ModelType type) { WriteTransaction trans(FROM_HERE, &share_); ReadNode root(&trans); if (BaseNode::INIT_OK == root.InitByTagLookup(ModelTypeToRootTag(type))) return true; syncable::MutableEntry entry(trans.GetWrappedWriteTrans(), syncable::CREATE_NEW_UPDATE_ITEM, syncable::Id::CreateFromServerId( ModelTypeToString(type))); if (!entry.good()) return false; entry.PutParentId(syncable::Id()); entry.PutBaseVersion(1); entry.PutUniqueServerTag(ModelTypeToRootTag(type)); entry.PutNonUniqueName(ModelTypeToString(type)); entry.PutIsDel(false); entry.PutIsDir(true); sync_pb::EntitySpecifics specifics; AddDefaultFieldValue(type, &specifics); entry.PutSpecifics(specifics); return true; } void SyncRollbackManagerBase::InitBookmarkFolder(const std::string& folder) { WriteTransaction trans(FROM_HERE, &share_); syncable::Entry bookmark_root(trans.GetWrappedTrans(), syncable::GET_BY_SERVER_TAG, ModelTypeToRootTag(BOOKMARKS)); if (!bookmark_root.good()) return; syncable::MutableEntry entry(trans.GetWrappedWriteTrans(), syncable::CREATE_NEW_UPDATE_ITEM, syncable::Id::CreateFromServerId(folder)); if (!entry.good()) return; entry.PutParentId(bookmark_root.GetId()); entry.PutBaseVersion(1); entry.PutUniqueServerTag(folder); entry.PutNonUniqueName(folder); entry.PutIsDel(false); entry.PutIsDir(true); sync_pb::EntitySpecifics specifics; AddDefaultFieldValue(BOOKMARKS, &specifics); entry.PutSpecifics(specifics); } } // namespace syncer