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