model_load_manager.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_MODEL_LOAD_MANAGER_H_
  5. #define COMPONENTS_SYNC_DRIVER_MODEL_LOAD_MANAGER_H_
  6. #include "base/memory/raw_ptr.h"
  7. #include "base/memory/weak_ptr.h"
  8. #include "components/sync/driver/configure_context.h"
  9. #include "components/sync/driver/data_type_controller.h"
  10. #include "components/sync/engine/shutdown_reason.h"
  11. namespace syncer {
  12. struct ConfigureContext;
  13. // Interface for ModelLoadManager to pass the results of async operations
  14. // back to DataTypeManager.
  15. class ModelLoadManagerDelegate {
  16. public:
  17. // Called when all desired types are loaded, i.e. are ready to be configured
  18. // with ModelTypeConfigurer. A data type is ready when its progress marker is
  19. // available, which is the case once the local model has been loaded.
  20. // This function is called at most once after each call to
  21. // ModelLoadManager::Initialize().
  22. virtual void OnAllDataTypesReadyForConfigure() = 0;
  23. // Called when the ModelLoadManager has decided it must stop |type|, likely
  24. // because it is no longer a desired data type, sync is shutting down, or some
  25. // error occurred during loading.
  26. virtual void OnSingleDataTypeWillStop(ModelType type,
  27. const SyncError& error) = 0;
  28. virtual ~ModelLoadManagerDelegate() = default;
  29. };
  30. // |ModelLoadManager| instructs DataTypeControllers to load models and to stop
  31. // (DataTypeManager is responsible for activating/deactivating data types).
  32. // Since the operations are async it uses an interface to inform DataTypeManager
  33. // of the results of the operations.
  34. // This class is owned by DataTypeManager.
  35. class ModelLoadManager {
  36. public:
  37. ModelLoadManager(const DataTypeController::TypeMap* controllers,
  38. ModelLoadManagerDelegate* delegate);
  39. ModelLoadManager(const ModelLoadManager&) = delete;
  40. ModelLoadManager& operator=(const ModelLoadManager&) = delete;
  41. virtual ~ModelLoadManager();
  42. // Stops any data types that are *not* in |preferred_types_without_errors|,
  43. // then kicks off loading of all |preferred_types_without_errors|.
  44. // |preferred_types_without_errors| must be a subset of |preferred_types|.
  45. // |preferred_types| contains all types selected by the user.
  46. void Initialize(ModelTypeSet preferred_types_without_errors,
  47. ModelTypeSet preferred_types,
  48. const ConfigureContext& context);
  49. // Can be called at any time. Synchronously stops all datatypes.
  50. void Stop(ShutdownReason shutdown_reason);
  51. // Stops an individual datatype |type| for |shutdown_reason|. |error| must be
  52. // an actual error (i.e. not UNSET).
  53. void StopDatatype(ModelType type,
  54. ShutdownReason shutdown_reason,
  55. SyncError error);
  56. private:
  57. // Start loading non-running types that are in
  58. // |preferred_types_without_errors_|.
  59. void LoadDesiredTypes();
  60. // Callback that will be invoked when the model for |type| finishes loading.
  61. // This callback is passed to the controller's |LoadModels| method.
  62. void ModelLoadCallback(ModelType type, const SyncError& error);
  63. // A helper to stop an individual datatype.
  64. void StopDatatypeImpl(const SyncError& error,
  65. ShutdownReason shutdown_reason,
  66. DataTypeController* dtc,
  67. DataTypeController::StopCallback callback);
  68. // Calls delegate's OnAllDataTypesReadyForConfigure if all datatypes from
  69. // |preferred_types_without_errors_| are loaded. Ensures that
  70. // OnAllDataTypesReadyForConfigure is called at most once for every call to
  71. // Initialize().
  72. void NotifyDelegateIfReadyForConfigure();
  73. // Set of all registered controllers.
  74. const raw_ptr<const DataTypeController::TypeMap> controllers_;
  75. // The delegate in charge of handling model load results.
  76. const raw_ptr<ModelLoadManagerDelegate> delegate_;
  77. ConfigureContext configure_context_;
  78. // Data types that are enabled.
  79. ModelTypeSet preferred_types_without_errors_;
  80. // Data types that are loaded.
  81. ModelTypeSet loaded_types_;
  82. bool notified_about_ready_for_configure_ = false;
  83. base::WeakPtrFactory<ModelLoadManager> weak_ptr_factory_{this};
  84. };
  85. } // namespace syncer
  86. #endif // COMPONENTS_SYNC_DRIVER_MODEL_LOAD_MANAGER_H_