data_type_controller.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. #ifndef COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
  5. #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "base/values.h"
  14. #include "components/sync/base/model_type.h"
  15. #include "components/sync/engine/shutdown_reason.h"
  16. #include "components/sync/model/data_type_error_handler.h"
  17. namespace syncer {
  18. struct ConfigureContext;
  19. struct DataTypeActivationResponse;
  20. struct TypeEntitiesCount;
  21. class SyncError;
  22. // DataTypeControllers are responsible for managing the state of a single data
  23. // type. They are not thread safe and should only be used on the UI thread.
  24. class DataTypeController : public base::SupportsWeakPtr<DataTypeController> {
  25. public:
  26. // TODO(crbug.com/967677): Should MODEL_LOADED be renamed to
  27. // MODEL_READY_TO_CONNECT?
  28. enum State {
  29. NOT_RUNNING, // The controller has never been started or has previously
  30. // been stopped. Must be in this state to start.
  31. MODEL_STARTING, // The model is loading.
  32. MODEL_LOADED, // The model has finished loading and is ready to connect.
  33. RUNNING, // The controller is running and the data type is
  34. // in sync with the cloud.
  35. STOPPING, // The controller is in the process of stopping
  36. // and is waiting for dependent services to stop.
  37. FAILED // The controller was started but encountered an error.
  38. };
  39. // Note: This seems like it should be a OnceCallback, but it can actually be
  40. // called multiple times in the case of errors.
  41. using ModelLoadCallback =
  42. base::RepeatingCallback<void(ModelType, const SyncError&)>;
  43. using StopCallback = base::OnceClosure;
  44. using AllNodesCallback =
  45. base::OnceCallback<void(const ModelType, base::Value::List)>;
  46. using TypeMap = std::map<ModelType, std::unique_ptr<DataTypeController>>;
  47. using TypeVector = std::vector<std::unique_ptr<DataTypeController>>;
  48. static std::string StateToString(State state);
  49. virtual ~DataTypeController();
  50. // Begins asynchronous operation of loading the model to get it ready for
  51. // activation. Once the models are loaded the callback will be invoked with
  52. // the result. If the models are already loaded it is safe to call the
  53. // callback right away. Else the callback needs to be stored and called when
  54. // the models are ready.
  55. virtual void LoadModels(const ConfigureContext& configure_context,
  56. const ModelLoadCallback& model_load_callback) = 0;
  57. // Called by DataTypeManager once the local model has loaded (MODEL_LOADED),
  58. // in order to enable the sync engine's propagation of sync changes between
  59. // the server and the local processor. Upon return, the controller assumes
  60. // that the caller will take care of actually instrumenting the sync engine.
  61. virtual std::unique_ptr<DataTypeActivationResponse> Connect() = 0;
  62. // Stops the data type. If LoadModels() has not completed it will enter
  63. // STOPPING state first and eventually STOPPED. Once stopped, |callback| will
  64. // be run. |callback| must not be null.
  65. //
  66. // NOTE: Stop() should be called after sync backend machinery has stopped
  67. // routing changes to this data type. Stop() should ensure the data type
  68. // logic shuts down gracefully by flushing remaining changes and calling
  69. // StopSyncing on the SyncableService. This assumes no changes will ever
  70. // propagate from sync again from point where Stop() is called.
  71. virtual void Stop(ShutdownReason shutdown_reason, StopCallback callback) = 0;
  72. // Name of this data type. For logging purposes only.
  73. std::string name() const { return ModelTypeToDebugString(type()); }
  74. // Current state of the data type controller.
  75. virtual State state() const = 0;
  76. // Unique model type for this data type controller.
  77. ModelType type() const { return type_; }
  78. // Whether preconditions are met for the datatype to start. This is useful for
  79. // example if the datatype depends on certain user preferences other than the
  80. // ones for sync settings themselves.
  81. enum class PreconditionState {
  82. kPreconditionsMet,
  83. kMustStopAndClearData,
  84. kMustStopAndKeepData,
  85. };
  86. virtual PreconditionState GetPreconditionState() const;
  87. // Returns whether the datatype knows how to, and wants to, run in
  88. // transport-only mode (see syncer::SyncMode enum).
  89. virtual bool ShouldRunInTransportOnlyMode() const = 0;
  90. // Returns a ListValue representing all nodes for this data type through
  91. // |callback| on this thread. Can only be called if state() != NOT_RUNNING.
  92. // Used for populating nodes in Sync Node Browser of chrome://sync-internals.
  93. virtual void GetAllNodes(AllNodesCallback callback) = 0;
  94. // Collects TypeEntitiesCount for this datatype and passes them to |callback|.
  95. // Used to display entity counts in chrome://sync-internals.
  96. virtual void GetTypeEntitiesCount(
  97. base::OnceCallback<void(const TypeEntitiesCount&)> callback) const = 0;
  98. // Records entities count and estimated memory usage of the type into
  99. // histograms. Can be called only if state() != NOT_RUNNING.
  100. virtual void RecordMemoryUsageAndCountsHistograms() = 0;
  101. protected:
  102. explicit DataTypeController(ModelType type);
  103. // Allows subclasses to DCHECK that they're on the correct sequence.
  104. // TODO(crbug.com/846238): Rename this to CalledOnValidSequence.
  105. bool CalledOnValidThread() const;
  106. private:
  107. // The type this object is responsible for controlling.
  108. const ModelType type_;
  109. // Used to check that functions are called on the correct sequence.
  110. base::SequenceChecker sequence_checker_;
  111. };
  112. } // namespace syncer
  113. #endif // COMPONENTS_SYNC_DRIVER_DATA_TYPE_CONTROLLER_H__