backend_migrator.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. // Copyright (c) 2012 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_BACKEND_MIGRATOR_H_
  5. #define COMPONENTS_SYNC_DRIVER_BACKEND_MIGRATOR_H_
  6. #include <string>
  7. #include "base/compiler_specific.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/observer_list.h"
  11. #include "components/sync/base/model_type.h"
  12. #include "components/sync/driver/data_type_manager.h"
  13. namespace syncer {
  14. // Interface for anything that wants to know when the migrator's state
  15. // changes.
  16. class MigrationObserver {
  17. public:
  18. virtual void OnMigrationStateChange() = 0;
  19. protected:
  20. virtual ~MigrationObserver();
  21. };
  22. // A class to perform migration of a datatype pursuant to the 'MIGRATION_DONE'
  23. // code in the sync protocol definition (protocol/sync.proto).
  24. class BackendMigrator {
  25. public:
  26. enum State {
  27. IDLE,
  28. WAITING_TO_START, // Waiting for previous configuration to finish.
  29. DISABLING_TYPES, // Exit criteria: OnConfigureDone for enabled types
  30. // _excluding_ |to_migrate_| and empty download progress
  31. // markers for types in |to_migrate_|.
  32. REENABLING_TYPES, // Exit criteria: OnConfigureDone for enabled types.
  33. };
  34. BackendMigrator(const std::string& name,
  35. DataTypeManager* manager,
  36. const base::RepeatingClosure& reconfigure_callback,
  37. const base::RepeatingClosure& migration_done_callback);
  38. BackendMigrator(const BackendMigrator&) = delete;
  39. BackendMigrator& operator=(const BackendMigrator&) = delete;
  40. virtual ~BackendMigrator();
  41. // Starts a sequence of events that will disable and reenable |types|.
  42. void MigrateTypes(ModelTypeSet types);
  43. void AddMigrationObserver(MigrationObserver* observer);
  44. void RemoveMigrationObserver(MigrationObserver* observer);
  45. State state() const;
  46. // Called from SyncServiceImpl to notify us of configure done.
  47. // Note: We receive these notifications only when our state is not IDLE.
  48. void OnConfigureDone(const DataTypeManager::ConfigureResult& result);
  49. // Returns the types that are currently pending migration (if any).
  50. ModelTypeSet GetPendingMigrationTypesForTest() const;
  51. private:
  52. void ChangeState(State new_state);
  53. // Must be called only in state WAITING_TO_START. If ready to
  54. // start, meaning the data type manager is configured, calls
  55. // RestartMigration() and returns true. Otherwise, does nothing and
  56. // returns false.
  57. bool TryStart();
  58. // Restarts migration, interrupting any existing migration.
  59. void RestartMigration();
  60. // Called by OnConfigureDone().
  61. void OnConfigureDoneImpl(const DataTypeManager::ConfigureResult& result);
  62. const std::string name_;
  63. raw_ptr<DataTypeManager> manager_;
  64. const base::RepeatingClosure reconfigure_callback_;
  65. const base::RepeatingClosure migration_done_callback_;
  66. State state_;
  67. base::ObserverList<MigrationObserver>::Unchecked migration_observers_;
  68. ModelTypeSet to_migrate_;
  69. base::WeakPtrFactory<BackendMigrator> weak_ptr_factory_{this};
  70. };
  71. } // namespace syncer
  72. #endif // COMPONENTS_SYNC_DRIVER_BACKEND_MIGRATOR_H_