| 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 #include "chrome/browser/sync/backend_migrator.h" | 5 #include "chrome/browser/sync/backend_migrator.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/message_loop.h" |
| 9 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/sync/profile_sync_service.h" | 11 #include "chrome/browser/sync/profile_sync_service.h" |
| 11 #include "chrome/browser/sync/engine/configure_reason.h" | 12 #include "chrome/browser/sync/engine/configure_reason.h" |
| 12 #include "chrome/browser/sync/glue/data_type_manager.h" | |
| 13 #include "chrome/browser/sync/sessions/session_state.h" | 13 #include "chrome/browser/sync/sessions/session_state.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "content/browser/browser_thread.h" | |
| 16 #include "content/common/notification_details.h" | 15 #include "content/common/notification_details.h" |
| 17 #include "content/common/notification_source.h" | 16 #include "content/common/notification_source.h" |
| 18 | 17 |
| 19 using syncable::ModelTypeSet; | 18 using syncable::ModelTypeSet; |
| 20 | 19 |
| 21 namespace browser_sync { | 20 namespace browser_sync { |
| 22 | 21 |
| 23 using sessions::SyncSessionSnapshot; | 22 using sessions::SyncSessionSnapshot; |
| 23 using syncable::ModelTypeToString; |
| 24 | 24 |
| 25 BackendMigrator::BackendMigrator(ProfileSyncService* service, | 25 MigrationObserver::~MigrationObserver() {} |
| 26 |
| 27 BackendMigrator::BackendMigrator(const std::string& name, |
| 28 ProfileSyncService* service, |
| 26 DataTypeManager* manager) | 29 DataTypeManager* manager) |
| 27 : state_(IDLE), service_(service), manager_(manager), | 30 : name_(name), state_(IDLE), service_(service), manager_(manager), |
| 28 restart_migration_(false), | 31 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 29 method_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | |
| 30 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 32 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
| 31 Source<DataTypeManager>(manager_)); | 33 Source<DataTypeManager>(manager_)); |
| 32 service_->AddObserver(this); | |
| 33 } | 34 } |
| 34 | 35 |
| 35 BackendMigrator::~BackendMigrator() { | 36 BackendMigrator::~BackendMigrator() { |
| 36 service_->RemoveObserver(this); | |
| 37 } | 37 } |
| 38 | 38 |
| 39 bool BackendMigrator::HasStartedMigrating() const { | 39 // Helper macros to log with the syncer thread name; useful when there |
| 40 return state_ >= DISABLING_TYPES; | 40 // are multiple syncer threads involved. |
| 41 } | 41 |
| 42 #define SLOG(severity) LOG(severity) << name_ << ": " |
| 43 |
| 44 #define SVLOG(verbose_level) VLOG(verbose_level) << name_ << ": " |
| 42 | 45 |
| 43 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { | 46 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { |
| 47 const ModelTypeSet old_to_migrate = to_migrate_; |
| 44 { | 48 { |
| 45 ModelTypeSet temp; | 49 ModelTypeSet temp; |
| 46 std::set_union(to_migrate_.begin(), to_migrate_.end(), | 50 std::set_union(to_migrate_.begin(), to_migrate_.end(), |
| 47 types.begin(), types.end(), | 51 types.begin(), types.end(), |
| 48 std::inserter(temp, temp.end())); | 52 std::inserter(temp, temp.end())); |
| 49 to_migrate_ = temp; | 53 std::swap(temp, to_migrate_); |
| 50 } | 54 } |
| 51 | 55 SVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) |
| 52 if (HasStartedMigrating()) { | 56 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) |
| 53 VLOG(1) << "BackendMigrator::MigrateTypes: STARTED_MIGRATING early-out."; | 57 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 54 restart_migration_ = true; | 58 if (old_to_migrate == to_migrate_) { |
| 59 SVLOG(1) << "MigrateTypes called with no new types; ignoring"; |
| 55 return; | 60 return; |
| 56 } | 61 } |
| 57 | 62 |
| 58 if (manager_->state() != DataTypeManager::CONFIGURED) { | 63 if (state_ == IDLE) |
| 59 VLOG(1) << "BackendMigrator::MigrateTypes: manager CONFIGURED early-out."; | 64 ChangeState(WAITING_TO_START); |
| 60 state_ = WAITING_TO_START; | 65 |
| 66 if (state_ == WAITING_TO_START) { |
| 67 if (!TryStart()) |
| 68 SVLOG(1) << "Manager not configured; waiting"; |
| 61 return; | 69 return; |
| 62 } | 70 } |
| 63 | 71 |
| 72 DCHECK_GT(state_, WAITING_TO_START); |
| 73 // If we're already migrating, interrupt the current migration. |
| 74 RestartMigration(); |
| 75 } |
| 76 |
| 77 void BackendMigrator::AddMigrationObserver(MigrationObserver* observer) { |
| 78 migration_observers_.AddObserver(observer); |
| 79 } |
| 80 |
| 81 void BackendMigrator::RemoveMigrationObserver(MigrationObserver* observer) { |
| 82 migration_observers_.RemoveObserver(observer); |
| 83 } |
| 84 |
| 85 void BackendMigrator::ChangeState(State new_state) { |
| 86 state_ = new_state; |
| 87 FOR_EACH_OBSERVER(MigrationObserver, migration_observers_, |
| 88 OnMigrationStateChange()); |
| 89 } |
| 90 |
| 91 bool BackendMigrator::TryStart() { |
| 92 DCHECK_EQ(state_, WAITING_TO_START); |
| 93 if (manager_->state() == DataTypeManager::CONFIGURED) { |
| 94 RestartMigration(); |
| 95 return true; |
| 96 } |
| 97 return false; |
| 98 } |
| 99 |
| 100 void BackendMigrator::RestartMigration() { |
| 64 // We'll now disable any running types that need to be migrated. | 101 // We'll now disable any running types that need to be migrated. |
| 65 state_ = DISABLING_TYPES; | 102 ChangeState(DISABLING_TYPES); |
| 66 ModelTypeSet full_set; | 103 ModelTypeSet full_set; |
| 67 service_->GetPreferredDataTypes(&full_set); | 104 service_->GetPreferredDataTypes(&full_set); |
| 68 ModelTypeSet difference; | 105 ModelTypeSet difference; |
| 69 std::set_difference(full_set.begin(), full_set.end(), | 106 std::set_difference(full_set.begin(), full_set.end(), |
| 70 to_migrate_.begin(), to_migrate_.end(), | 107 to_migrate_.begin(), to_migrate_.end(), |
| 71 std::inserter(difference, difference.end())); | 108 std::inserter(difference, difference.end())); |
| 72 VLOG(1) << "BackendMigrator disabling types; calling Configure."; | 109 bool configure_with_nigori = to_migrate_.count(syncable::NIGORI) == 0; |
| 110 SVLOG(1) << "BackendMigrator disabling types " |
| 111 << ModelTypeSetToString(to_migrate_) << "; configuring " |
| 112 << ModelTypeSetToString(difference) |
| 113 << (configure_with_nigori ? " with nigori" : " without nigori"); |
| 73 | 114 |
| 74 // Add nigori for config or not based upon if the server told us to migrate | 115 // Add nigori for config or not based upon if the server told us to migrate |
| 75 // nigori or not. | 116 // nigori or not. |
| 76 if (to_migrate_.count(syncable::NIGORI) == 0) { | 117 if (configure_with_nigori) { |
| 77 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); | 118 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); |
| 78 } else { | 119 } else { |
| 79 manager_->ConfigureWithoutNigori(difference, | 120 manager_->ConfigureWithoutNigori(difference, |
| 80 sync_api::CONFIGURE_REASON_MIGRATION); | 121 sync_api::CONFIGURE_REASON_MIGRATION); |
| 81 } | 122 } |
| 82 } | 123 } |
| 83 | 124 |
| 84 void BackendMigrator::OnStateChanged() { | |
| 85 if (restart_migration_ == true) { | |
| 86 VLOG(1) << "BackendMigrator restarting migration in OnStateChanged."; | |
| 87 state_ = WAITING_TO_START; | |
| 88 restart_migration_ = false; | |
| 89 MigrateTypes(to_migrate_); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 if (state_ != WAITING_FOR_PURGE) | |
| 94 return; | |
| 95 | |
| 96 size_t num_empty_migrated_markers = 0; | |
| 97 const SyncSessionSnapshot* snap = service_->GetLastSessionSnapshot(); | |
| 98 for (ModelTypeSet::const_iterator it = to_migrate_.begin(); | |
| 99 it != to_migrate_.end(); ++it) { | |
| 100 if (snap->download_progress_markers[*it].empty()) | |
| 101 num_empty_migrated_markers++; | |
| 102 } | |
| 103 | |
| 104 if (num_empty_migrated_markers < to_migrate_.size()) | |
| 105 return; | |
| 106 | |
| 107 state_ = REENABLING_TYPES; | |
| 108 ModelTypeSet full_set; | |
| 109 service_->GetPreferredDataTypes(&full_set); | |
| 110 VLOG(1) << "BackendMigrator re-enabling types."; | |
| 111 | |
| 112 // Don't use |to_migrate_| for the re-enabling because the user may have | |
| 113 // chosen to disable types during the migration. | |
| 114 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION); | |
| 115 } | |
| 116 | |
| 117 void BackendMigrator::Observe(int type, | 125 void BackendMigrator::Observe(int type, |
| 118 const NotificationSource& source, | 126 const NotificationSource& source, |
| 119 const NotificationDetails& details) { | 127 const NotificationDetails& details) { |
| 120 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); | 128 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); |
| 121 if (state_ == IDLE) | 129 if (state_ == IDLE) |
| 122 return; | 130 return; |
| 123 | 131 |
| 124 const DataTypeManager::ConfigureResult* result = | 132 // |manager_|'s methods aren't re-entrant, and we're notified from |
| 125 Details<DataTypeManager::ConfigureResult>(details).ptr(); | 133 // them, so post a task to avoid problems. |
| 134 SVLOG(1) << "Posting OnConfigureDone from Observer"; |
| 135 MessageLoop::current()->PostTask( |
| 136 FROM_HERE, |
| 137 base::Bind(&BackendMigrator::OnConfigureDone, |
| 138 weak_ptr_factory_.GetWeakPtr(), |
| 139 *Details<DataTypeManager::ConfigureResult>(details).ptr())); |
| 140 } |
| 126 | 141 |
| 127 ModelTypeSet intersection; | 142 void BackendMigrator::OnConfigureDone( |
| 128 std::set_intersection(result->requested_types.begin(), | 143 const DataTypeManager::ConfigureResult& result) { |
| 129 result->requested_types.end(), to_migrate_.begin(), to_migrate_.end(), | 144 SVLOG(1) << "OnConfigureDone with requested types " |
| 130 std::inserter(intersection, intersection.end())); | 145 << ModelTypeSetToString(result.requested_types) |
| 131 | 146 << ", unsynced types " |
| 132 // The intersection check is to determine if our disable request was | 147 << ModelTypeSetToString(result.unsynced_types) |
| 133 // interrupted by a user changing preferred types. May still need to purge. | 148 << ", status " << result.status |
| 134 // It's pretty wild if we're in WAITING_FOR_PURGE here, because it would mean | 149 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 135 // that after our disable-config finished but before the purge, another config | 150 if (state_ == WAITING_TO_START) { |
| 136 // was posted externally _and completed_, which means somehow the nudge to | 151 if (!TryStart()) |
| 137 // purge was dropped, yet nudges are reliable. | 152 SVLOG(1) << "Manager still not configured; still waiting"; |
| 138 if (state_ == WAITING_TO_START || state_ == WAITING_FOR_PURGE || | |
| 139 (state_ == DISABLING_TYPES && !intersection.empty())) { | |
| 140 state_ = WAITING_TO_START; | |
| 141 restart_migration_ = false; | |
| 142 VLOG(1) << "BackendMigrator::Observe posting MigrateTypes."; | |
| 143 if (!BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 144 method_factory_.NewRunnableMethod(&BackendMigrator::MigrateTypes, | |
| 145 to_migrate_))) { | |
| 146 // Unittests need this. | |
| 147 // TODO(tim): Clean this up. | |
| 148 MigrateTypes(to_migrate_); | |
| 149 } | |
| 150 return; | 153 return; |
| 151 } | 154 } |
| 152 | 155 |
| 153 if (result->status != DataTypeManager::OK) { | 156 DCHECK_GT(state_, WAITING_TO_START); |
| 157 |
| 158 ModelTypeSet intersection; |
| 159 std::set_intersection( |
| 160 result.requested_types.begin(), result.requested_types.end(), |
| 161 to_migrate_.begin(), to_migrate_.end(), |
| 162 std::inserter(intersection, intersection.end())); |
| 163 // This intersection check is to determine if our disable request |
| 164 // was interrupted by a user changing preferred types. |
| 165 if (state_ == DISABLING_TYPES && !intersection.empty()) { |
| 166 SVLOG(1) << "Disable request interrupted by user changing types"; |
| 167 RestartMigration(); |
| 168 return; |
| 169 } |
| 170 |
| 171 if (result.status != DataTypeManager::OK) { |
| 154 // If this fails, and we're disabling types, a type may or may not be | 172 // If this fails, and we're disabling types, a type may or may not be |
| 155 // disabled until the user restarts the browser. If this wasn't an abort, | 173 // disabled until the user restarts the browser. If this wasn't an abort, |
| 156 // any failure will be reported as an unrecoverable error to the UI. If it | 174 // any failure will be reported as an unrecoverable error to the UI. If it |
| 157 // was an abort, then typically things are shutting down anyway. There isn't | 175 // was an abort, then typically things are shutting down anyway. There isn't |
| 158 // much we can do in any case besides wait until a restart to try again. | 176 // much we can do in any case besides wait until a restart to try again. |
| 159 // The server will send down MIGRATION_DONE again for types needing | 177 // The server will send down MIGRATION_DONE again for types needing |
| 160 // migration as the type will still be enabled on restart. | 178 // migration as the type will still be enabled on restart. |
| 161 LOG(WARNING) << "Unable to migrate, configuration failed!"; | 179 SLOG(WARNING) << "Unable to migrate, configuration failed!"; |
| 162 state_ = IDLE; | 180 ChangeState(IDLE); |
| 163 to_migrate_.clear(); | 181 to_migrate_.clear(); |
| 164 return; | 182 return; |
| 165 } | 183 } |
| 166 | 184 |
| 167 if (state_ == DISABLING_TYPES) { | 185 if (state_ == DISABLING_TYPES) { |
| 168 state_ = WAITING_FOR_PURGE; | 186 if (!std::includes(result.unsynced_types.begin(), |
| 169 VLOG(1) << "BackendMigrator waiting for purge."; | 187 result.unsynced_types.end(), |
| 188 to_migrate_.begin(), to_migrate_.end())) { |
| 189 SLOG(WARNING) << "Set of unsynced types: " |
| 190 << syncable::ModelTypeSetToString(result.unsynced_types) |
| 191 << " does not contain types to migrate: " |
| 192 << syncable::ModelTypeSetToString(to_migrate_) |
| 193 << "; not re-enabling yet"; |
| 194 return; |
| 195 } |
| 196 |
| 197 ChangeState(REENABLING_TYPES); |
| 198 // Don't use |to_migrate_| for the re-enabling because the user |
| 199 // may have chosen to disable types during the migration. |
| 200 ModelTypeSet full_set; |
| 201 service_->GetPreferredDataTypes(&full_set); |
| 202 SVLOG(1) << "BackendMigrator re-enabling types: " |
| 203 << syncable::ModelTypeSetToString(full_set); |
| 204 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION); |
| 170 } else if (state_ == REENABLING_TYPES) { | 205 } else if (state_ == REENABLING_TYPES) { |
| 171 // We're done! | 206 // We're done! |
| 172 state_ = IDLE; | 207 ChangeState(IDLE); |
| 173 | 208 |
| 174 std::stringstream ss; | 209 SVLOG(1) << "BackendMigrator: Migration complete for: " |
| 175 std::copy(to_migrate_.begin(), to_migrate_.end(), | 210 << syncable::ModelTypeSetToString(to_migrate_); |
| 176 std::ostream_iterator<syncable::ModelType>(ss, ",")); | |
| 177 VLOG(1) << "BackendMigrator: Migration complete for: " << ss.str(); | |
| 178 to_migrate_.clear(); | 211 to_migrate_.clear(); |
| 179 } | 212 } |
| 180 } | 213 } |
| 181 | 214 |
| 182 BackendMigrator::State BackendMigrator::state() const { | 215 BackendMigrator::State BackendMigrator::state() const { |
| 183 return state_; | 216 return state_; |
| 184 } | 217 } |
| 185 | 218 |
| 219 syncable::ModelTypeSet |
| 220 BackendMigrator::GetPendingMigrationTypesForTest() const { |
| 221 return to_migrate_; |
| 222 } |
| 223 |
| 224 #undef SVLOG |
| 225 |
| 226 #undef SLOG |
| 227 |
| 186 }; // namespace browser_sync | 228 }; // namespace browser_sync |
| OLD | NEW |