| 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/bind.h" |
| 10 #include "base/message_loop.h" |
| 9 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 10 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
| 11 #include "chrome/browser/sync/engine/configure_reason.h" | 13 #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" | 14 #include "chrome/browser/sync/sessions/session_state.h" |
| 14 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "content/browser/browser_thread.h" | |
| 16 #include "content/common/notification_details.h" | 16 #include "content/common/notification_details.h" |
| 17 #include "content/common/notification_source.h" | 17 #include "content/common/notification_source.h" |
| 18 | 18 |
| 19 using syncable::ModelTypeSet; | 19 using syncable::ModelTypeSet; |
| 20 | 20 |
| 21 namespace browser_sync { | 21 namespace browser_sync { |
| 22 | 22 |
| 23 using sessions::SyncSessionSnapshot; | 23 using sessions::SyncSessionSnapshot; |
| 24 using syncable::ModelTypeToString; |
| 24 | 25 |
| 25 BackendMigrator::BackendMigrator(ProfileSyncService* service, | 26 BackendMigrator::BackendMigrator(ProfileSyncService* service, |
| 26 DataTypeManager* manager) | 27 DataTypeManager* manager) |
| 27 : state_(IDLE), service_(service), manager_(manager), | 28 : state_(IDLE), service_(service), manager_(manager), |
| 28 restart_migration_(false), | 29 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, | 30 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
| 31 Source<DataTypeManager>(manager_)); | 31 Source<DataTypeManager>(manager_)); |
| 32 service_->AddObserver(this); | |
| 33 } | 32 } |
| 34 | 33 |
| 35 BackendMigrator::~BackendMigrator() { | 34 BackendMigrator::~BackendMigrator() { |
| 36 service_->RemoveObserver(this); | |
| 37 } | |
| 38 | |
| 39 bool BackendMigrator::HasStartedMigrating() const { | |
| 40 return state_ >= DISABLING_TYPES; | |
| 41 } | 35 } |
| 42 | 36 |
| 43 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { | 37 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { |
| 38 const ModelTypeSet old_to_migrate = to_migrate_; |
| 44 { | 39 { |
| 45 ModelTypeSet temp; | 40 ModelTypeSet temp; |
| 46 std::set_union(to_migrate_.begin(), to_migrate_.end(), | 41 std::set_union(to_migrate_.begin(), to_migrate_.end(), |
| 47 types.begin(), types.end(), | 42 types.begin(), types.end(), |
| 48 std::inserter(temp, temp.end())); | 43 std::inserter(temp, temp.end())); |
| 49 to_migrate_ = temp; | 44 std::swap(temp, to_migrate_); |
| 50 } | 45 } |
| 51 | 46 VLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) |
| 52 if (HasStartedMigrating()) { | 47 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) |
| 53 VLOG(1) << "BackendMigrator::MigrateTypes: STARTED_MIGRATING early-out."; | 48 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 54 restart_migration_ = true; | 49 if (old_to_migrate == to_migrate_) { |
| 50 VLOG(1) << "MigrateTypes called with no new types; ignoring"; |
| 55 return; | 51 return; |
| 56 } | 52 } |
| 57 | 53 |
| 58 if (manager_->state() != DataTypeManager::CONFIGURED) { | 54 if (state_ == IDLE) |
| 59 VLOG(1) << "BackendMigrator::MigrateTypes: manager CONFIGURED early-out."; | |
| 60 state_ = WAITING_TO_START; | 55 state_ = WAITING_TO_START; |
| 56 |
| 57 if (state_ == WAITING_TO_START) { |
| 58 if (!TryStart()) |
| 59 VLOG(1) << "Manager not configured; waiting"; |
| 61 return; | 60 return; |
| 62 } | 61 } |
| 63 | 62 |
| 63 DCHECK_GT(state_, WAITING_TO_START); |
| 64 // If we're already migrating, interrupt the current migration. |
| 65 RestartMigration(); |
| 66 } |
| 67 |
| 68 bool BackendMigrator::TryStart() { |
| 69 DCHECK_EQ(state_, WAITING_TO_START); |
| 70 if (manager_->state() == DataTypeManager::CONFIGURED) { |
| 71 RestartMigration(); |
| 72 return true; |
| 73 } |
| 74 return false; |
| 75 } |
| 76 |
| 77 void BackendMigrator::RestartMigration() { |
| 64 // We'll now disable any running types that need to be migrated. | 78 // We'll now disable any running types that need to be migrated. |
| 65 state_ = DISABLING_TYPES; | 79 state_ = DISABLING_TYPES; |
| 66 ModelTypeSet full_set; | 80 ModelTypeSet full_set; |
| 67 service_->GetPreferredDataTypes(&full_set); | 81 service_->GetPreferredDataTypes(&full_set); |
| 68 ModelTypeSet difference; | 82 ModelTypeSet difference; |
| 69 std::set_difference(full_set.begin(), full_set.end(), | 83 std::set_difference(full_set.begin(), full_set.end(), |
| 70 to_migrate_.begin(), to_migrate_.end(), | 84 to_migrate_.begin(), to_migrate_.end(), |
| 71 std::inserter(difference, difference.end())); | 85 std::inserter(difference, difference.end())); |
| 72 VLOG(1) << "BackendMigrator disabling types; calling Configure."; | 86 bool configure_with_nigori = to_migrate_.count(syncable::NIGORI) == 0; |
| 87 VLOG(1) << "BackendMigrator disabling types " |
| 88 << ModelTypeSetToString(to_migrate_) << "; configuring " |
| 89 << ModelTypeSetToString(difference) |
| 90 << (configure_with_nigori ? " with nigori" : " without nigori"); |
| 73 | 91 |
| 74 // Add nigori for config or not based upon if the server told us to migrate | 92 // Add nigori for config or not based upon if the server told us to migrate |
| 75 // nigori or not. | 93 // nigori or not. |
| 76 if (to_migrate_.count(syncable::NIGORI) == 0) { | 94 if (configure_with_nigori) { |
| 77 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); | 95 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); |
| 78 } else { | 96 } else { |
| 79 manager_->ConfigureWithoutNigori(difference, | 97 manager_->ConfigureWithoutNigori(difference, |
| 80 sync_api::CONFIGURE_REASON_MIGRATION); | 98 sync_api::CONFIGURE_REASON_MIGRATION); |
| 81 } | 99 } |
| 82 } | 100 } |
| 83 | 101 |
| 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, | 102 void BackendMigrator::Observe(int type, |
| 118 const NotificationSource& source, | 103 const NotificationSource& source, |
| 119 const NotificationDetails& details) { | 104 const NotificationDetails& details) { |
| 120 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); | 105 DCHECK_EQ(chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, type); |
| 121 if (state_ == IDLE) | 106 if (state_ == IDLE) |
| 122 return; | 107 return; |
| 123 | 108 |
| 124 const DataTypeManager::ConfigureResult* result = | 109 // |manager_|'s methods aren't re-entrant, and we're notified from |
| 125 Details<DataTypeManager::ConfigureResult>(details).ptr(); | 110 // them, so post a task to avoid problems. |
| 111 VLOG(1) << "Posting OnConfigureDone from Observer"; |
| 112 MessageLoop::current()->PostTask( |
| 113 FROM_HERE, |
| 114 base::Bind(&BackendMigrator::OnConfigureDone, |
| 115 weak_ptr_factory_.GetWeakPtr(), |
| 116 *Details<DataTypeManager::ConfigureResult>(details).ptr())); |
| 117 } |
| 126 | 118 |
| 127 ModelTypeSet intersection; | 119 void BackendMigrator::OnConfigureDone( |
| 128 std::set_intersection(result->requested_types.begin(), | 120 const DataTypeManager::ConfigureResult& result) { |
| 129 result->requested_types.end(), to_migrate_.begin(), to_migrate_.end(), | 121 VLOG(1) << "OnConfigureDone with requested types " |
| 130 std::inserter(intersection, intersection.end())); | 122 << ModelTypeSetToString(result.requested_types) |
| 131 | 123 << ", purged types " << ModelTypeSetToString(result.purged_types) |
| 132 // The intersection check is to determine if our disable request was | 124 << ", status " << result.status |
| 133 // interrupted by a user changing preferred types. May still need to purge. | 125 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 134 // It's pretty wild if we're in WAITING_FOR_PURGE here, because it would mean | 126 if (state_ == WAITING_TO_START) { |
| 135 // that after our disable-config finished but before the purge, another config | 127 if (!TryStart()) |
| 136 // was posted externally _and completed_, which means somehow the nudge to | 128 VLOG(1) << "Manager still not configured; still waiting"; |
| 137 // purge was dropped, yet nudges are reliable. | |
| 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; | 129 return; |
| 151 } | 130 } |
| 152 | 131 |
| 153 if (result->status != DataTypeManager::OK) { | 132 DCHECK_GT(state_, WAITING_TO_START); |
| 133 |
| 134 ModelTypeSet intersection; |
| 135 std::set_intersection( |
| 136 result.requested_types.begin(), result.requested_types.end(), |
| 137 to_migrate_.begin(), to_migrate_.end(), |
| 138 std::inserter(intersection, intersection.end())); |
| 139 // This intersection check is to determine if our disable request |
| 140 // was interrupted by a user changing preferred types. |
| 141 if (state_ == DISABLING_TYPES && !intersection.empty()) { |
| 142 VLOG(1) << "Disable request interrupted by user changing types"; |
| 143 RestartMigration(); |
| 144 return; |
| 145 } |
| 146 |
| 147 if (result.status != DataTypeManager::OK) { |
| 154 // If this fails, and we're disabling types, a type may or may not be | 148 // 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, | 149 // 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 | 150 // 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 | 151 // 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. | 152 // 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 | 153 // The server will send down MIGRATION_DONE again for types needing |
| 160 // migration as the type will still be enabled on restart. | 154 // migration as the type will still be enabled on restart. |
| 161 LOG(WARNING) << "Unable to migrate, configuration failed!"; | 155 LOG(WARNING) << "Unable to migrate, configuration failed!"; |
| 162 state_ = IDLE; | 156 state_ = IDLE; |
| 163 to_migrate_.clear(); | 157 to_migrate_.clear(); |
| 164 return; | 158 return; |
| 165 } | 159 } |
| 166 | 160 |
| 167 if (state_ == DISABLING_TYPES) { | 161 if (state_ == DISABLING_TYPES) { |
| 168 state_ = WAITING_FOR_PURGE; | 162 if (!std::includes(result.purged_types.begin(), |
| 169 VLOG(1) << "BackendMigrator waiting for purge."; | 163 result.purged_types.end(), |
| 164 to_migrate_.begin(), to_migrate_.end())) { |
| 165 LOG(WARNING) << "Set of purged types: " |
| 166 << syncable::ModelTypeSetToString(result.purged_types) |
| 167 << " does not contain types to migrate: " |
| 168 << syncable::ModelTypeSetToString(to_migrate_) |
| 169 << "; not re-enabling yet"; |
| 170 return; |
| 171 } |
| 172 |
| 173 state_ = REENABLING_TYPES; |
| 174 // Don't use |to_migrate_| for the re-enabling because the user |
| 175 // may have chosen to disable types during the migration. |
| 176 ModelTypeSet full_set; |
| 177 service_->GetPreferredDataTypes(&full_set); |
| 178 VLOG(1) << "BackendMigrator re-enabling types: " |
| 179 << syncable::ModelTypeSetToString(full_set); |
| 180 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION); |
| 170 } else if (state_ == REENABLING_TYPES) { | 181 } else if (state_ == REENABLING_TYPES) { |
| 171 // We're done! | 182 // We're done! |
| 172 state_ = IDLE; | 183 state_ = IDLE; |
| 173 | 184 |
| 174 std::stringstream ss; | 185 VLOG(1) << "BackendMigrator: Migration complete for: " |
| 175 std::copy(to_migrate_.begin(), to_migrate_.end(), | 186 << syncable::ModelTypeSetToString(to_migrate_); |
| 176 std::ostream_iterator<syncable::ModelType>(ss, ",")); | |
| 177 VLOG(1) << "BackendMigrator: Migration complete for: " << ss.str(); | |
| 178 to_migrate_.clear(); | 187 to_migrate_.clear(); |
| 179 } | 188 } |
| 180 } | 189 } |
| 181 | 190 |
| 182 BackendMigrator::State BackendMigrator::state() const { | 191 BackendMigrator::State BackendMigrator::state() const { |
| 183 return state_; | 192 return state_; |
| 184 } | 193 } |
| 185 | 194 |
| 186 }; // namespace browser_sync | 195 }; // namespace browser_sync |
| OLD | NEW |