This source file includes following definitions.
- state_
- LoadModels
- OnModelLoaded
- StartModels
- StopModels
- StartAssociating
- Stop
- name
- state
- OnSingleDatatypeUnrecoverableError
- sync_service_
- StartDone
- StartDoneImpl
- RecordAssociationTime
- RecordStartFailure
- AbortModelLoad
- DisableImpl
- StartAssociationAsync
- StartAssociationWithSharedChangeProcessor
- ClearSharedChangeProcessor
- StopLocalServiceAsync
- StopLocalService
#include "chrome/browser/sync/glue/non_ui_data_type_controller.h"
#include "base/logging.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/shared_change_processor_ref.h"
#include "chrome/browser/sync/profile_sync_components_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "content/public/browser/browser_thread.h"
#include "sync/api/sync_error.h"
#include "sync/api/syncable_service.h"
#include "sync/internal_api/public/base/model_type.h"
#include "sync/util/data_type_histogram.h"
using content::BrowserThread;
namespace browser_sync {
NonUIDataTypeController::NonUIDataTypeController(
scoped_refptr<base::MessageLoopProxy> ui_thread,
const base::Closure& error_callback,
ProfileSyncComponentsFactory* profile_sync_factory,
Profile* profile,
ProfileSyncService* sync_service)
: DataTypeController(ui_thread, error_callback),
profile_sync_factory_(profile_sync_factory),
profile_(profile),
sync_service_(sync_service),
state_(NOT_RUNNING) {
}
void NonUIDataTypeController::LoadModels(
const ModelLoadCallback& model_load_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!model_load_callback.is_null());
if (state() != NOT_RUNNING) {
model_load_callback.Run(type(),
syncer::SyncError(FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"Model already running",
type()));
return;
}
state_ = MODEL_STARTING;
DCHECK(!shared_change_processor_.get());
shared_change_processor_ =
profile_sync_factory_->CreateSharedChangeProcessor();
DCHECK(shared_change_processor_.get());
model_load_callback_ = model_load_callback;
if (!StartModels()) {
DCHECK(state() == MODEL_STARTING || state() == NOT_RUNNING);
return;
}
OnModelLoaded();
}
void NonUIDataTypeController::OnModelLoaded() {
DCHECK_EQ(state_, MODEL_STARTING);
DCHECK(!model_load_callback_.is_null());
state_ = MODEL_LOADED;
ModelLoadCallback model_load_callback = model_load_callback_;
model_load_callback_.Reset();
model_load_callback.Run(type(), syncer::SyncError());
}
bool NonUIDataTypeController::StartModels() {
DCHECK_EQ(state_, MODEL_STARTING);
return true;
}
void NonUIDataTypeController::StopModels() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
}
void NonUIDataTypeController::StartAssociating(
const StartCallback& start_callback) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(!start_callback.is_null());
DCHECK_EQ(state_, MODEL_LOADED);
state_ = ASSOCIATING;
start_callback_ = start_callback;
if (!StartAssociationAsync()) {
syncer::SyncError error(
FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"Failed to post StartAssociation",
type());
syncer::SyncMergeResult local_merge_result(type());
local_merge_result.set_error(error);
StartDoneImpl(ASSOCIATION_FAILED,
NOT_RUNNING,
local_merge_result,
syncer::SyncMergeResult(type()));
DCHECK(!shared_change_processor_.get());
return;
}
}
void NonUIDataTypeController::Stop() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (state() == NOT_RUNNING) {
NOTREACHED();
return;
}
ClearSharedChangeProcessor();
switch (state()) {
case MODEL_STARTING:
state_ = STOPPING;
AbortModelLoad();
return;
case ASSOCIATING:
state_ = STOPPING;
StartDoneImpl(ABORTED,
NOT_RUNNING,
syncer::SyncMergeResult(type()),
syncer::SyncMergeResult(type()));
break;
case MODEL_LOADED:
case DISABLED:
state_ = NOT_RUNNING;
StopModels();
return;
default:
DCHECK_EQ(state(), RUNNING);
state_ = STOPPING;
StopModels();
break;
}
sync_service_->DeactivateDataType(type());
StopLocalServiceAsync();
state_ = NOT_RUNNING;
}
std::string NonUIDataTypeController::name() const {
return syncer::ModelTypeToString(type());
}
DataTypeController::State NonUIDataTypeController::state() const {
return state_;
}
void NonUIDataTypeController::OnSingleDatatypeUnrecoverableError(
const tracked_objects::Location& from_here, const std::string& message) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
RecordUnrecoverableError(from_here, message);
BrowserThread::PostTask(BrowserThread::UI, from_here,
base::Bind(&NonUIDataTypeController::DisableImpl,
this,
from_here,
message));
}
NonUIDataTypeController::NonUIDataTypeController()
: DataTypeController(base::MessageLoopProxy::current(), base::Closure()),
profile_sync_factory_(NULL),
profile_(NULL),
sync_service_(NULL) {}
NonUIDataTypeController::~NonUIDataTypeController() {}
void NonUIDataTypeController::StartDone(
DataTypeController::StartResult start_result,
const syncer::SyncMergeResult& local_merge_result,
const syncer::SyncMergeResult& syncer_merge_result) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
DataTypeController::State new_state;
if (IsSuccessfulResult(start_result)) {
new_state = RUNNING;
} else {
new_state = (start_result == ASSOCIATION_FAILED ? DISABLED : NOT_RUNNING);
}
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(&NonUIDataTypeController::StartDoneImpl,
this,
start_result,
new_state,
local_merge_result,
syncer_merge_result));
}
void NonUIDataTypeController::StartDoneImpl(
DataTypeController::StartResult start_result,
DataTypeController::State new_state,
const syncer::SyncMergeResult& local_merge_result,
const syncer::SyncMergeResult& syncer_merge_result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (IsUnrecoverableResult(start_result))
RecordUnrecoverableError(FROM_HERE, "StartFailed");
if (new_state != RUNNING && state() != NOT_RUNNING && state() != STOPPING) {
ClearSharedChangeProcessor();
StopLocalServiceAsync();
}
if (state_ == NOT_RUNNING) {
DCHECK(start_callback_.is_null());
return;
}
state_ = new_state;
if (state_ != RUNNING) {
StopModels();
RecordStartFailure(start_result);
}
StartCallback callback = start_callback_;
start_callback_.Reset();
callback.Run(start_result, local_merge_result, syncer_merge_result);
}
void NonUIDataTypeController::RecordAssociationTime(base::TimeDelta time) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
#define PER_DATA_TYPE_MACRO(type_str) \
UMA_HISTOGRAM_TIMES("Sync." type_str "AssociationTime", time);
SYNC_DATA_TYPE_HISTOGRAM(type());
#undef PER_DATA_TYPE_MACRO
}
void NonUIDataTypeController::RecordStartFailure(StartResult result) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
UMA_HISTOGRAM_ENUMERATION("Sync.DataTypeStartFailures",
ModelTypeToHistogramInt(type()),
syncer::MODEL_TYPE_COUNT);
#define PER_DATA_TYPE_MACRO(type_str) \
UMA_HISTOGRAM_ENUMERATION("Sync." type_str "StartFailure", result, \
MAX_START_RESULT);
SYNC_DATA_TYPE_HISTOGRAM(type());
#undef PER_DATA_TYPE_MACRO
}
void NonUIDataTypeController::AbortModelLoad() {
state_ = NOT_RUNNING;
StopModels();
ModelLoadCallback model_load_callback = model_load_callback_;
model_load_callback_.Reset();
model_load_callback.Run(type(),
syncer::SyncError(FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"ABORTED",
type()));
}
void NonUIDataTypeController::DisableImpl(
const tracked_objects::Location& from_here,
const std::string& message) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
sync_service_->DisableBrokenDatatype(type(), from_here, message);
}
bool NonUIDataTypeController::StartAssociationAsync() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK_EQ(state(), ASSOCIATING);
return PostTaskOnBackendThread(
FROM_HERE,
base::Bind(
&NonUIDataTypeController::StartAssociationWithSharedChangeProcessor,
this,
shared_change_processor_));
}
void NonUIDataTypeController::
StartAssociationWithSharedChangeProcessor(
const scoped_refptr<SharedChangeProcessor>& shared_change_processor) {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
DCHECK(shared_change_processor.get());
syncer::SyncMergeResult local_merge_result(type());
syncer::SyncMergeResult syncer_merge_result(type());
base::WeakPtrFactory<syncer::SyncMergeResult> weak_ptr_factory(
&syncer_merge_result);
local_service_ = shared_change_processor->Connect(
profile_sync_factory_,
sync_service_,
this,
type(),
weak_ptr_factory.GetWeakPtr());
if (!local_service_.get()) {
syncer::SyncError error(FROM_HERE,
syncer::SyncError::DATATYPE_ERROR,
"Failed to connect to syncer.",
type());
local_merge_result.set_error(error);
StartDone(ASSOCIATION_FAILED,
local_merge_result,
syncer_merge_result);
return;
}
if (!shared_change_processor->CryptoReadyIfNecessary()) {
StartDone(NEEDS_CRYPTO,
local_merge_result,
syncer_merge_result);
return;
}
bool sync_has_nodes = false;
if (!shared_change_processor->SyncModelHasUserCreatedNodes(&sync_has_nodes)) {
syncer::SyncError error(FROM_HERE,
syncer::SyncError::UNRECOVERABLE_ERROR,
"Failed to load sync nodes",
type());
local_merge_result.set_error(error);
StartDone(UNRECOVERABLE_ERROR,
local_merge_result,
syncer_merge_result);
return;
}
base::TimeTicks start_time = base::TimeTicks::Now();
syncer::SyncDataList initial_sync_data;
syncer::SyncError error =
shared_change_processor->GetAllSyncDataReturnError(
type(), &initial_sync_data);
if (error.IsSet()) {
local_merge_result.set_error(error);
StartDone(ASSOCIATION_FAILED,
local_merge_result,
syncer_merge_result);
return;
}
syncer_merge_result.set_num_items_before_association(
initial_sync_data.size());
local_merge_result =
local_service_->MergeDataAndStartSyncing(
type(),
initial_sync_data,
scoped_ptr<syncer::SyncChangeProcessor>(
new SharedChangeProcessorRef(shared_change_processor)),
scoped_ptr<syncer::SyncErrorFactory>(
new SharedChangeProcessorRef(shared_change_processor)));
RecordAssociationTime(base::TimeTicks::Now() - start_time);
if (local_merge_result.error().IsSet()) {
StartDone(ASSOCIATION_FAILED,
local_merge_result,
syncer_merge_result);
return;
}
syncer_merge_result.set_num_items_after_association(
shared_change_processor->GetSyncCount());
shared_change_processor->ActivateDataType(model_safe_group());
StartDone(!sync_has_nodes ? OK_FIRST_RUN : OK,
local_merge_result,
syncer_merge_result);
}
void NonUIDataTypeController::ClearSharedChangeProcessor() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (shared_change_processor_.get()) {
shared_change_processor_->Disconnect();
shared_change_processor_ = NULL;
}
}
void NonUIDataTypeController::StopLocalServiceAsync() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
PostTaskOnBackendThread(
FROM_HERE,
base::Bind(&NonUIDataTypeController::StopLocalService, this));
}
void NonUIDataTypeController::StopLocalService() {
DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
if (local_service_.get())
local_service_->StopSyncing(type());
local_service_.reset();
}
}