blob: 8787373d00cbfb75fe247eff421e6ccd574c549a [file] [log] [blame]
// 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 <memory>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/containers/flat_map.h"
#include "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "components/sync/base/model_type.h"
#include "components/sync/base/storage_option.h"
#include "components/sync/driver/configure_context.h"
#include "components/sync/driver/data_type_controller.h"
#include "components/sync/model/model_error.h"
#include "components/sync/model/model_type_controller_delegate.h"
#include "components/sync/model/sync_error.h"
namespace syncer {
struct DataTypeActivationResponse;
// DataTypeController implementation for Unified Sync and Storage model types.
class ModelTypeController : public DataTypeController {
ModelType type,
std::unique_ptr<ModelTypeControllerDelegate> delegate_on_disk);
// For datatypes that have support for STORAGE_IN_MEMORY.
ModelType type,
std::unique_ptr<ModelTypeControllerDelegate> delegate_on_disk,
std::unique_ptr<ModelTypeControllerDelegate> delegate_in_memory);
~ModelTypeController() override;
// Steals the activation response, only used for Nigori.
// TODO( Once all datatypes are in USS, we should redesign
// or remove RegisterWithBackend, and expose the activation response via
// LoadModels(), which is more natural in USS.
std::unique_ptr<DataTypeActivationResponse> ActivateManuallyForNigori();
// DataTypeController implementation.
bool ShouldLoadModelBeforeConfigure() const override;
void BeforeLoadModels(ModelTypeConfigurer* configurer) override;
void LoadModels(const ConfigureContext& configure_context,
const ModelLoadCallback& model_load_callback) override;
RegisterWithBackendResult RegisterWithBackend(
ModelTypeConfigurer* configurer) override;
void StartAssociating(StartCallback start_callback) override;
void ActivateDataType(ModelTypeConfigurer* configurer) override;
void DeactivateDataType(ModelTypeConfigurer* configurer) override;
void Stop(ShutdownReason shutdown_reason, StopCallback callback) override;
State state() const override;
void GetAllNodes(AllNodesCallback callback) override;
void GetStatusCounters(StatusCountersCallback callback) override;
void RecordMemoryUsageAndCountsHistograms() override;
void ReportModelError(SyncError::ErrorType error_type,
const ModelError& error);
void RecordStartFailure() const;
void RecordRunFailure() const;
void OnDelegateStarted(
std::unique_ptr<DataTypeActivationResponse> activation_response);
void TriggerCompletionCallbacks(const SyncError& error);
base::flat_map<StorageOption, std::unique_ptr<ModelTypeControllerDelegate>>
// State of this datatype controller.
State state_ = NOT_RUNNING;
// Owned by |delegate_map_|. Null while NOT_RUNNING.
ModelTypeControllerDelegate* delegate_ = nullptr;
// Callback for use when starting the datatype (usually MODEL_STARTING, but
// STOPPING if abort requested while starting).
ModelLoadCallback model_load_callback_;
// Callbacks for use when stopping the datatype (STOPPING), which also
// includes aborting a start. This is important because STOPPING is a state
// used to make sure we don't request two starts in parallel to the delegate,
// which is hard to support (most notably in ClientTagBasedProcessor). We
// use a vector because it's allowed to call Stop() multiple times (i.e. while
std::vector<StopCallback> model_stop_callbacks_;
SyncStopMetadataFate model_stop_metadata_fate_;
// Controller receives |activation_response_| from
// ClientTagBasedModelTypeProcessor callback and must temporarily own it until
// ActivateDataType is called.
std::unique_ptr<DataTypeActivationResponse> activation_response_;
// This is a hack to prevent reconfigurations from crashing, because USS
// activation is not idempotent. RegisterWithBackend only needs to actually do
// something the first time after the type is enabled.
// TODO( Remove this once the DTM handles things better.
bool activated_ = false;
} // namespace syncer