| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ | 5 #ifndef CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ |
| 6 #define CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ | 6 #define CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" |
| 9 #include "base/memory/weak_ptr.h" |
| 10 #include "base/observer_list.h" |
| 8 #include "base/task.h" | 11 #include "base/task.h" |
| 9 #include "chrome/browser/sync/profile_sync_service_observer.h" | 12 #include "chrome/browser/sync/glue/data_type_manager.h" |
| 10 #include "chrome/browser/sync/syncable/model_type.h" | 13 #include "chrome/browser/sync/syncable/model_type.h" |
| 11 #include "content/common/notification_observer.h" | 14 #include "content/common/notification_observer.h" |
| 12 #include "content/common/notification_registrar.h" | 15 #include "content/common/notification_registrar.h" |
| 13 | 16 |
| 14 class ProfileSyncService; | 17 class ProfileSyncService; |
| 15 | 18 |
| 19 namespace sync_api { |
| 20 struct UserShare; |
| 21 } // namespace sync_api |
| 22 |
| 16 namespace browser_sync { | 23 namespace browser_sync { |
| 17 | 24 |
| 18 class DataTypeManager; | 25 // Interface for anything that wants to know when the migrator's state |
| 26 // changes. |
| 27 class MigrationObserver { |
| 28 public: |
| 29 virtual void OnMigrationStateChange() = 0; |
| 30 |
| 31 protected: |
| 32 virtual ~MigrationObserver(); |
| 33 }; |
| 19 | 34 |
| 20 // A class to perform migration of a datatype pursuant to the 'MIGRATION_DONE' | 35 // A class to perform migration of a datatype pursuant to the 'MIGRATION_DONE' |
| 21 // code in the sync protocol definition (protocol/sync.proto). | 36 // code in the sync protocol definition (protocol/sync.proto). |
| 22 class BackendMigrator : public NotificationObserver, | 37 class BackendMigrator : public NotificationObserver { |
| 23 public ProfileSyncServiceObserver { | |
| 24 public: | 38 public: |
| 25 enum State { | 39 enum State { |
| 26 IDLE, | 40 IDLE, |
| 27 WAITING_TO_START, // Waiting for previous configuration to finish. | 41 WAITING_TO_START, // Waiting for previous configuration to finish. |
| 28 DISABLING_TYPES, // Exit criteria: SYNC_CONFIGURE_DONE for enabled | 42 DISABLING_TYPES, // Exit criteria: SYNC_CONFIGURE_DONE for |
| 29 // types _excluding_ |to_migrate_|. | 43 // enabled types _excluding_ |to_migrate_| and |
| 30 WAITING_FOR_PURGE, // Exit criteria: SyncCycleEnded for enabled types | 44 // empty download progress markers for types |
| 31 // excluding |to_migrate| | 45 // in |to_migrate_|. |
| 32 REENABLING_TYPES, // Exit criteria: SYNC_CONFIGURE_DONE for enabled | 46 REENABLING_TYPES, // Exit criteria: SYNC_CONFIGURE_DONE for enabled |
| 33 // types. | 47 // types. |
| 34 }; | 48 }; |
| 35 | 49 |
| 36 BackendMigrator(ProfileSyncService* service, DataTypeManager* manager); | 50 // TODO(akalin): Remove the dependency on |user_share|. |
| 51 BackendMigrator(const std::string& name, |
| 52 sync_api::UserShare* user_share, |
| 53 ProfileSyncService* service, |
| 54 DataTypeManager* manager); |
| 37 virtual ~BackendMigrator(); | 55 virtual ~BackendMigrator(); |
| 38 | 56 |
| 39 // Starts a sequence of events that will disable and reenable |types|. | 57 // Starts a sequence of events that will disable and reenable |types|. |
| 40 void MigrateTypes(const syncable::ModelTypeSet& types); | 58 void MigrateTypes(const syncable::ModelTypeSet& types); |
| 41 | 59 |
| 42 // ProfileSyncServiceObserver implementation. | 60 void AddMigrationObserver(MigrationObserver* observer); |
| 43 virtual void OnStateChanged(); | 61 bool HasMigrationObserver(MigrationObserver* observer) const; |
| 62 void RemoveMigrationObserver(MigrationObserver* observer); |
| 44 | 63 |
| 45 // NotificationObserver implementation. | 64 // NotificationObserver implementation. |
| 46 virtual void Observe(int type, | 65 virtual void Observe(int type, |
| 47 const NotificationSource& source, | 66 const NotificationSource& source, |
| 48 const NotificationDetails& details); | 67 const NotificationDetails& details) OVERRIDE; |
| 49 | 68 |
| 50 State state() const; | 69 State state() const; |
| 51 | 70 |
| 71 // Returns the types that are currently pending migration (if any). |
| 72 syncable::ModelTypeSet GetPendingMigrationTypesForTest() const; |
| 73 |
| 52 private: | 74 private: |
| 53 bool HasStartedMigrating() const; | 75 void ChangeState(State new_state); |
| 76 |
| 77 // Must be called only in state WAITING_TO_START. If ready to |
| 78 // start, meaning the data type manager is configured, calls |
| 79 // RestartMigration() and returns true. Otherwise, does nothing and |
| 80 // returns false. |
| 81 bool TryStart(); |
| 82 |
| 83 // Restarts migration, interrupting any existing migration. |
| 84 void RestartMigration(); |
| 85 |
| 86 // Called by Observe(). |
| 87 void OnConfigureDone(const DataTypeManager::ConfigureResult& result); |
| 88 |
| 89 const std::string name_; |
| 90 sync_api::UserShare* user_share_; |
| 91 ProfileSyncService* service_; |
| 92 DataTypeManager* manager_; |
| 54 | 93 |
| 55 State state_; | 94 State state_; |
| 56 ProfileSyncService* service_; | 95 |
| 57 DataTypeManager* manager_; | |
| 58 NotificationRegistrar registrar_; | 96 NotificationRegistrar registrar_; |
| 59 | 97 |
| 98 ObserverList<MigrationObserver> migration_observers_; |
| 99 |
| 60 syncable::ModelTypeSet to_migrate_; | 100 syncable::ModelTypeSet to_migrate_; |
| 61 bool restart_migration_; | |
| 62 | 101 |
| 63 // We use this to gracefully re-start migrations. | 102 base::WeakPtrFactory<BackendMigrator> weak_ptr_factory_; |
| 64 ScopedRunnableMethodFactory<BackendMigrator> method_factory_; | |
| 65 | 103 |
| 66 DISALLOW_COPY_AND_ASSIGN(BackendMigrator); | 104 DISALLOW_COPY_AND_ASSIGN(BackendMigrator); |
| 67 }; | 105 }; |
| 68 | 106 |
| 69 } // namespace browser_sync | 107 } // namespace browser_sync |
| 70 | 108 |
| 71 #endif // CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ | 109 #endif // CHROME_BROWSER_SYNC_BACKEND_MIGRATOR_H_ |
| OLD | NEW |