diff options
author | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-26 09:57:48 +0000 |
---|---|---|
committer | rlarocque@chromium.org <rlarocque@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-26 09:57:48 +0000 |
commit | a74bfd94b772bd2fd2fb6a4c86ab7fa28bae5e09 (patch) | |
tree | cd65affbec838521ffaaa82797e59e9d0d5b3bfe /components/sync_driver/non_blocking_data_type_controller.cc | |
parent | 599bdcf987a713e054225e9d9908374cd95c05c7 (diff) | |
download | chromium_src-a74bfd94b772bd2fd2fb6a4c86ab7fa28bae5e09.zip chromium_src-a74bfd94b772bd2fd2fb6a4c86ab7fa28bae5e09.tar.gz chromium_src-a74bfd94b772bd2fd2fb6a4c86ab7fa28bae5e09.tar.bz2 |
Introduce NonBlockingDataTypeController
The NonBlockingDataTypeController tracks the sync state of
a non-blocking data type and sends signals to its components from the UI
thread. It is responsible for enabling and disabling sync for a data
type according to user preference and the availbility of the sync
backend.
This CL also includes some changes to the NonBlockingDataTypeProcessor,
which is currently just a stub of what will eventually become
non-blocking sync's model thread component, so that it can be used in
testing NonBlockingDataTypeController.
BUG=351005
Review URL: https://codereview.chromium.org/249843002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@266344 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'components/sync_driver/non_blocking_data_type_controller.cc')
-rw-r--r-- | components/sync_driver/non_blocking_data_type_controller.cc | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/components/sync_driver/non_blocking_data_type_controller.cc b/components/sync_driver/non_blocking_data_type_controller.cc new file mode 100644 index 0000000..e2e0366 --- /dev/null +++ b/components/sync_driver/non_blocking_data_type_controller.cc @@ -0,0 +1,137 @@ +// 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 "components/sync_driver/non_blocking_data_type_controller.h" + +#include "base/bind.h" +#include "base/bind_helpers.h" +#include "base/location.h" +#include "sync/internal_api/public/non_blocking_type_processor.h" + +namespace browser_sync { + +NonBlockingDataTypeController::NonBlockingDataTypeController( + syncer::ModelType type, bool is_preferred) + : type_(type), + current_state_(DISCONNECTED), + is_preferred_(is_preferred) {} + +NonBlockingDataTypeController::~NonBlockingDataTypeController() {} + +void NonBlockingDataTypeController::InitializeProcessor( + scoped_refptr<base::SequencedTaskRunner> task_runner, + base::WeakPtr<syncer::NonBlockingTypeProcessor> processor) { + DCHECK(!IsTypeProcessorConnected()); + task_runner_ = task_runner; + processor_ = processor; + DCHECK(IsTypeProcessorConnected()); + + UpdateState(); +} + +void NonBlockingDataTypeController::InitializeSyncCoreProxy( + scoped_ptr<syncer::SyncCoreProxy> proxy) { + DCHECK(!IsSyncBackendConnected()); + proxy_ = proxy.Pass(); + + UpdateState(); +} + +void NonBlockingDataTypeController::ClearSyncCoreProxy() { + // Never had a sync core proxy to begin with. No change. + if (!proxy_) + return; + proxy_.reset(); + + UpdateState(); +} + +void NonBlockingDataTypeController::SetIsPreferred(bool is_preferred) { + is_preferred_ = is_preferred; + + UpdateState(); +} + +void NonBlockingDataTypeController::UpdateState() { + // Return immediately if no updates are necessary. + if (GetDesiredState() == current_state_) { + return; + } + + // Return immediately if the processor is not ready yet. + if (!IsTypeProcessorConnected()) { + return; + } + + // Send the appropriate state transition request to the processor. + switch (GetDesiredState()) { + case ENABLED: + SendEnableSignal(); + return; + case DISABLED: + SendDisableSignal(); + return; + case DISCONNECTED: + SendDisconnectSignal(); + return; + } + NOTREACHED(); + return; +} + +void NonBlockingDataTypeController::SendEnableSignal() { + DCHECK_EQ(ENABLED, GetDesiredState()); + DVLOG(1) << "Enabling non-blocking sync type " << ModelTypeToString(type_); + + task_runner_->PostTask( + FROM_HERE, + base::Bind(&syncer::NonBlockingTypeProcessor::Enable, + processor_, + base::Owned(proxy_->Clone().release()))); + current_state_ = ENABLED; +} + +void NonBlockingDataTypeController::SendDisableSignal() { + DCHECK_EQ(DISABLED, GetDesiredState()); + DVLOG(1) << "Disabling non-blocking sync type " << ModelTypeToString(type_); + task_runner_->PostTask( + FROM_HERE, + base::Bind(&syncer::NonBlockingTypeProcessor::Disable, processor_)); + current_state_ = DISABLED; +} + +void NonBlockingDataTypeController::SendDisconnectSignal() { + DCHECK_EQ(DISCONNECTED, GetDesiredState()); + DVLOG(1) << "Disconnecting non-blocking sync type " + << ModelTypeToString(type_); + task_runner_->PostTask( + FROM_HERE, + base::Bind(&syncer::NonBlockingTypeProcessor::Disconnect, processor_)); + current_state_ = DISCONNECTED; +} + +bool NonBlockingDataTypeController::IsPreferred() const { + return is_preferred_; +} + +bool NonBlockingDataTypeController::IsTypeProcessorConnected() const { + return task_runner_ != NULL; +} + +bool NonBlockingDataTypeController::IsSyncBackendConnected() const { + return proxy_; +} + +NonBlockingDataTypeController::TypeProcessorState +NonBlockingDataTypeController::GetDesiredState() const { + if (!IsPreferred()) { + return DISABLED; + } else if (!IsSyncBackendConnected() || !IsTypeProcessorConnected()) { + return DISCONNECTED; + } else { + return ENABLED; + } +} + +} // namespace browser_sync |