1 // Copyright 2014 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef COMPONENTS_SYNC_DRIVER_MODEL_TYPE_CONTROLLER_H_ 6 #define COMPONENTS_SYNC_DRIVER_MODEL_TYPE_CONTROLLER_H_ 7 8 #include <memory> 9 #include <string> 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/containers/flat_map.h" 14 #include "base/macros.h" 15 #include "base/memory/weak_ptr.h" 16 #include "components/sync/base/model_type.h" 17 #include "components/sync/base/sync_mode.h" 18 #include "components/sync/driver/configure_context.h" 19 #include "components/sync/driver/data_type_controller.h" 20 #include "components/sync/model/model_error.h" 21 #include "components/sync/model/model_type_controller_delegate.h" 22 #include "components/sync/model/sync_error.h" 23 24 namespace syncer { 25 26 struct DataTypeActivationResponse; 27 28 // DataTypeController implementation for Unified Sync and Storage model types. 29 class ModelTypeController : public DataTypeController { 30 public: 31 // For datatypes that do not run in transport-only mode. 32 ModelTypeController( 33 ModelType type, 34 std::unique_ptr<ModelTypeControllerDelegate> delegate_for_full_sync_mode); 35 // For datatypes that have support for STORAGE_IN_MEMORY. 36 ModelTypeController( 37 ModelType type, 38 std::unique_ptr<ModelTypeControllerDelegate> delegate_for_full_sync_mode, 39 std::unique_ptr<ModelTypeControllerDelegate> delegate_for_transport_mode); 40 ~ModelTypeController() override; 41 42 // Steals the activation response, only used for Nigori. 43 // TODO(crbug.com/967677): Once all datatypes are in USS, we should redesign 44 // or remove ActivateDataType, and expose the activation response via 45 // LoadModels(), which is more natural in USS. 46 std::unique_ptr<DataTypeActivationResponse> ActivateManuallyForNigori(); 47 48 // DataTypeController implementation. 49 void LoadModels(const ConfigureContext& configure_context, 50 const ModelLoadCallback& model_load_callback) override; 51 ActivateDataTypeResult ActivateDataType( 52 ModelTypeConfigurer* configurer) override; 53 void DeactivateDataType(ModelTypeConfigurer* configurer) override; 54 void Stop(ShutdownReason shutdown_reason, StopCallback callback) override; 55 State state() const override; 56 bool ShouldRunInTransportOnlyMode() const override; 57 void GetAllNodes(AllNodesCallback callback) override; 58 void GetTypeEntitiesCount(base::OnceCallback<void(const TypeEntitiesCount&)> 59 callback) const override; 60 void RecordMemoryUsageAndCountsHistograms() override; 61 62 ModelTypeControllerDelegate* GetDelegateForTesting(SyncMode sync_mode); 63 64 protected: 65 // Subclasses that use this constructor must call InitModelTypeController(). 66 explicit ModelTypeController(ModelType type); 67 68 // |delegate_for_transport_mode| may be null if the type does not run in 69 // transport mode. 70 void InitModelTypeController( 71 std::unique_ptr<ModelTypeControllerDelegate> delegate_for_full_sync_mode, 72 std::unique_ptr<ModelTypeControllerDelegate> delegate_for_transport_mode); 73 74 void ReportModelError(SyncError::ErrorType error_type, 75 const ModelError& error); 76 77 private: 78 void RecordStartFailure() const; 79 void RecordRunFailure() const; 80 void OnDelegateStarted( 81 std::unique_ptr<DataTypeActivationResponse> activation_response); 82 void TriggerCompletionCallbacks(const SyncError& error); 83 84 base::flat_map<SyncMode, std::unique_ptr<ModelTypeControllerDelegate>> 85 delegate_map_; 86 87 // State of this datatype controller. 88 State state_ = NOT_RUNNING; 89 90 // Owned by |delegate_map_|. Null while NOT_RUNNING. 91 ModelTypeControllerDelegate* delegate_ = nullptr; 92 93 // Callback for use when starting the datatype (usually MODEL_STARTING, but 94 // STOPPING if abort requested while starting). 95 ModelLoadCallback model_load_callback_; 96 97 // Callbacks for use when stopping the datatype (STOPPING), which also 98 // includes aborting a start. This is important because STOPPING is a state 99 // used to make sure we don't request two starts in parallel to the delegate, 100 // which is hard to support, most notably in ClientTagBasedModelTypeProcessor. 101 // We use a vector because it's allowed to call Stop() multiple times (i.e. 102 // while STOPPING). 103 std::vector<StopCallback> model_stop_callbacks_; 104 SyncStopMetadataFate model_stop_metadata_fate_; 105 106 // Controller receives |activation_response_| from 107 // ClientTagBasedModelTypeProcessor callback and must temporarily own it until 108 // ActivateDataType is called. 109 std::unique_ptr<DataTypeActivationResponse> activation_response_; 110 111 DISALLOW_COPY_AND_ASSIGN(ModelTypeController); 112 }; 113 114 } // namespace syncer 115 116 #endif // COMPONENTS_SYNC_DRIVER_MODEL_TYPE_CONTROLLER_H_ 117