| 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> | |
| 8 | |
| 9 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 10 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 11 #include "base/tracked_objects.h" | 9 #include "base/tracked_objects.h" |
| 12 #include "chrome/browser/sync/internal_api/configure_reason.h" | 10 #include "chrome/browser/sync/internal_api/configure_reason.h" |
| 13 #include "chrome/browser/sync/internal_api/read_transaction.h" | 11 #include "chrome/browser/sync/internal_api/read_transaction.h" |
| 14 #include "chrome/browser/sync/profile_sync_service.h" | 12 #include "chrome/browser/sync/profile_sync_service.h" |
| 15 #include "chrome/browser/sync/protocol/sync.pb.h" | 13 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 16 #include "chrome/browser/sync/sessions/session_state.h" | 14 #include "chrome/browser/sync/sessions/session_state.h" |
| 17 #include "chrome/browser/sync/syncable/directory_manager.h" | 15 #include "chrome/browser/sync/syncable/directory_manager.h" |
| 18 #include "chrome/common/chrome_notification_types.h" | 16 #include "chrome/common/chrome_notification_types.h" |
| 19 #include "content/public/browser/notification_details.h" | 17 #include "content/public/browser/notification_details.h" |
| 20 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 21 | 19 |
| 22 using syncable::ModelTypeSet; | 20 using syncable::ModelEnumSet; |
| 23 | 21 |
| 24 namespace browser_sync { | 22 namespace browser_sync { |
| 25 | 23 |
| 26 using sessions::SyncSessionSnapshot; | 24 using sessions::SyncSessionSnapshot; |
| 27 using syncable::ModelTypeToString; | 25 using syncable::ModelTypeToString; |
| 28 | 26 |
| 29 MigrationObserver::~MigrationObserver() {} | 27 MigrationObserver::~MigrationObserver() {} |
| 30 | 28 |
| 31 BackendMigrator::BackendMigrator(const std::string& name, | 29 BackendMigrator::BackendMigrator(const std::string& name, |
| 32 sync_api::UserShare* user_share, | 30 sync_api::UserShare* user_share, |
| 33 ProfileSyncService* service, | 31 ProfileSyncService* service, |
| 34 DataTypeManager* manager) | 32 DataTypeManager* manager) |
| 35 : name_(name), user_share_(user_share), service_(service), | 33 : name_(name), user_share_(user_share), service_(service), |
| 36 manager_(manager), state_(IDLE), | 34 manager_(manager), state_(IDLE), |
| 37 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 35 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 38 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, | 36 registrar_.Add(this, chrome::NOTIFICATION_SYNC_CONFIGURE_DONE, |
| 39 content::Source<DataTypeManager>(manager_)); | 37 content::Source<DataTypeManager>(manager_)); |
| 40 } | 38 } |
| 41 | 39 |
| 42 BackendMigrator::~BackendMigrator() { | 40 BackendMigrator::~BackendMigrator() { |
| 43 } | 41 } |
| 44 | 42 |
| 45 // Helper macros to log with the syncer thread name; useful when there | 43 // Helper macros to log with the syncer thread name; useful when there |
| 46 // are multiple syncer threads involved. | 44 // are multiple syncer threads involved. |
| 47 | 45 |
| 48 #define SLOG(severity) LOG(severity) << name_ << ": " | 46 #define SLOG(severity) LOG(severity) << name_ << ": " |
| 49 | 47 |
| 50 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " | 48 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " |
| 51 | 49 |
| 52 void BackendMigrator::MigrateTypes(const syncable::ModelTypeSet& types) { | 50 void BackendMigrator::MigrateTypes(syncable::ModelEnumSet types) { |
| 53 const ModelTypeSet old_to_migrate = to_migrate_; | 51 const ModelEnumSet old_to_migrate = to_migrate_; |
| 54 { | 52 to_migrate_.PutAll(types); |
| 55 ModelTypeSet temp; | 53 SDVLOG(1) << "MigrateTypes called with " << ModelEnumSetToString(types) |
| 56 std::set_union(to_migrate_.begin(), to_migrate_.end(), | 54 << ", old_to_migrate = " << ModelEnumSetToString(old_to_migrate) |
| 57 types.begin(), types.end(), | 55 << ", to_migrate_ = " << ModelEnumSetToString(to_migrate_); |
| 58 std::inserter(temp, temp.end())); | 56 if (old_to_migrate.Equals(to_migrate_)) { |
| 59 std::swap(temp, to_migrate_); | |
| 60 } | |
| 61 SDVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) | |
| 62 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) | |
| 63 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); | |
| 64 if (old_to_migrate == to_migrate_) { | |
| 65 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; | 57 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; |
| 66 return; | 58 return; |
| 67 } | 59 } |
| 68 | 60 |
| 69 if (state_ == IDLE) | 61 if (state_ == IDLE) |
| 70 ChangeState(WAITING_TO_START); | 62 ChangeState(WAITING_TO_START); |
| 71 | 63 |
| 72 if (state_ == WAITING_TO_START) { | 64 if (state_ == WAITING_TO_START) { |
| 73 if (!TryStart()) | 65 if (!TryStart()) |
| 74 SDVLOG(1) << "Manager not configured; waiting"; | 66 SDVLOG(1) << "Manager not configured; waiting"; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 104 if (manager_->state() == DataTypeManager::CONFIGURED) { | 96 if (manager_->state() == DataTypeManager::CONFIGURED) { |
| 105 RestartMigration(); | 97 RestartMigration(); |
| 106 return true; | 98 return true; |
| 107 } | 99 } |
| 108 return false; | 100 return false; |
| 109 } | 101 } |
| 110 | 102 |
| 111 void BackendMigrator::RestartMigration() { | 103 void BackendMigrator::RestartMigration() { |
| 112 // We'll now disable any running types that need to be migrated. | 104 // We'll now disable any running types that need to be migrated. |
| 113 ChangeState(DISABLING_TYPES); | 105 ChangeState(DISABLING_TYPES); |
| 114 ModelTypeSet full_set; | 106 const ModelEnumSet full_set = service_->GetPreferredDataTypes(); |
| 115 service_->GetPreferredDataTypes(&full_set); | 107 const ModelEnumSet difference = Difference(full_set, to_migrate_); |
| 116 ModelTypeSet difference; | 108 bool configure_with_nigori = !to_migrate_.Has(syncable::NIGORI); |
| 117 std::set_difference(full_set.begin(), full_set.end(), | |
| 118 to_migrate_.begin(), to_migrate_.end(), | |
| 119 std::inserter(difference, difference.end())); | |
| 120 bool configure_with_nigori = to_migrate_.count(syncable::NIGORI) == 0; | |
| 121 SDVLOG(1) << "BackendMigrator disabling types " | 109 SDVLOG(1) << "BackendMigrator disabling types " |
| 122 << ModelTypeSetToString(to_migrate_) << "; configuring " | 110 << ModelEnumSetToString(to_migrate_) << "; configuring " |
| 123 << ModelTypeSetToString(difference) | 111 << ModelEnumSetToString(difference) |
| 124 << (configure_with_nigori ? " with nigori" : " without nigori"); | 112 << (configure_with_nigori ? " with nigori" : " without nigori"); |
| 125 | 113 |
| 126 // Add nigori for config or not based upon if the server told us to migrate | 114 // Add nigori for config or not based upon if the server told us to migrate |
| 127 // nigori or not. | 115 // nigori or not. |
| 128 if (configure_with_nigori) { | 116 if (configure_with_nigori) { |
| 129 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); | 117 manager_->Configure(difference, sync_api::CONFIGURE_REASON_MIGRATION); |
| 130 } else { | 118 } else { |
| 131 manager_->ConfigureWithoutNigori(difference, | 119 manager_->ConfigureWithoutNigori(difference, |
| 132 sync_api::CONFIGURE_REASON_MIGRATION); | 120 sync_api::CONFIGURE_REASON_MIGRATION); |
| 133 } | 121 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 146 MessageLoop::current()->PostTask( | 134 MessageLoop::current()->PostTask( |
| 147 FROM_HERE, | 135 FROM_HERE, |
| 148 base::Bind(&BackendMigrator::OnConfigureDone, | 136 base::Bind(&BackendMigrator::OnConfigureDone, |
| 149 weak_ptr_factory_.GetWeakPtr(), | 137 weak_ptr_factory_.GetWeakPtr(), |
| 150 *content::Details<DataTypeManager::ConfigureResult>( | 138 *content::Details<DataTypeManager::ConfigureResult>( |
| 151 details).ptr())); | 139 details).ptr())); |
| 152 } | 140 } |
| 153 | 141 |
| 154 namespace { | 142 namespace { |
| 155 | 143 |
| 156 syncable::ModelTypeSet GetUnsyncedDataTypes(sync_api::UserShare* user_share) { | 144 syncable::ModelEnumSet GetUnsyncedDataTypes(sync_api::UserShare* user_share) { |
| 157 sync_api::ReadTransaction trans(FROM_HERE, user_share); | 145 sync_api::ReadTransaction trans(FROM_HERE, user_share); |
| 158 syncable::ModelTypeSet unsynced_data_types; | 146 syncable::ModelEnumSet unsynced_data_types; |
| 159 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 147 for (int i = syncable::FIRST_REAL_MODEL_TYPE; |
| 160 i < syncable::MODEL_TYPE_COUNT; ++i) { | 148 i < syncable::MODEL_TYPE_COUNT; ++i) { |
| 161 syncable::ModelType type = syncable::ModelTypeFromInt(i); | 149 syncable::ModelType type = syncable::ModelTypeFromInt(i); |
| 162 sync_pb::DataTypeProgressMarker progress_marker; | 150 sync_pb::DataTypeProgressMarker progress_marker; |
| 163 trans.GetLookup()->GetDownloadProgress(type, &progress_marker); | 151 trans.GetLookup()->GetDownloadProgress(type, &progress_marker); |
| 164 if (progress_marker.token().empty()) { | 152 if (progress_marker.token().empty()) { |
| 165 unsynced_data_types.insert(type); | 153 unsynced_data_types.Put(type); |
| 166 } | 154 } |
| 167 } | 155 } |
| 168 return unsynced_data_types; | 156 return unsynced_data_types; |
| 169 } | 157 } |
| 170 | 158 |
| 171 } // namespace | 159 } // namespace |
| 172 | 160 |
| 173 void BackendMigrator::OnConfigureDone( | 161 void BackendMigrator::OnConfigureDone( |
| 174 const DataTypeManager::ConfigureResult& result) { | 162 const DataTypeManager::ConfigureResult& result) { |
| 175 SDVLOG(1) << "OnConfigureDone with requested types " | 163 SDVLOG(1) << "OnConfigureDone with requested types " |
| 176 << ModelTypeSetToString(result.requested_types) | 164 << ModelEnumSetToString(result.requested_types) |
| 177 << ", status " << result.status | 165 << ", status " << result.status |
| 178 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_); | 166 << ", and to_migrate_ = " << ModelEnumSetToString(to_migrate_); |
| 179 if (state_ == WAITING_TO_START) { | 167 if (state_ == WAITING_TO_START) { |
| 180 if (!TryStart()) | 168 if (!TryStart()) |
| 181 SDVLOG(1) << "Manager still not configured; still waiting"; | 169 SDVLOG(1) << "Manager still not configured; still waiting"; |
| 182 return; | 170 return; |
| 183 } | 171 } |
| 184 | 172 |
| 185 DCHECK_GT(state_, WAITING_TO_START); | 173 DCHECK_GT(state_, WAITING_TO_START); |
| 186 | 174 |
| 187 ModelTypeSet intersection; | 175 const ModelEnumSet intersection = |
| 188 std::set_intersection( | 176 Intersection(result.requested_types, to_migrate_); |
| 189 result.requested_types.begin(), result.requested_types.end(), | |
| 190 to_migrate_.begin(), to_migrate_.end(), | |
| 191 std::inserter(intersection, intersection.end())); | |
| 192 // This intersection check is to determine if our disable request | 177 // This intersection check is to determine if our disable request |
| 193 // was interrupted by a user changing preferred types. | 178 // was interrupted by a user changing preferred types. |
| 194 if (state_ == DISABLING_TYPES && !intersection.empty()) { | 179 if (state_ == DISABLING_TYPES && !intersection.Empty()) { |
| 195 SDVLOG(1) << "Disable request interrupted by user changing types"; | 180 SDVLOG(1) << "Disable request interrupted by user changing types"; |
| 196 RestartMigration(); | 181 RestartMigration(); |
| 197 return; | 182 return; |
| 198 } | 183 } |
| 199 | 184 |
| 200 if (result.status != DataTypeManager::OK) { | 185 if (result.status != DataTypeManager::OK) { |
| 201 // If this fails, and we're disabling types, a type may or may not be | 186 // If this fails, and we're disabling types, a type may or may not be |
| 202 // disabled until the user restarts the browser. If this wasn't an abort, | 187 // disabled until the user restarts the browser. If this wasn't an abort, |
| 203 // any failure will be reported as an unrecoverable error to the UI. If it | 188 // any failure will be reported as an unrecoverable error to the UI. If it |
| 204 // was an abort, then typically things are shutting down anyway. There isn't | 189 // was an abort, then typically things are shutting down anyway. There isn't |
| 205 // much we can do in any case besides wait until a restart to try again. | 190 // much we can do in any case besides wait until a restart to try again. |
| 206 // The server will send down MIGRATION_DONE again for types needing | 191 // The server will send down MIGRATION_DONE again for types needing |
| 207 // migration as the type will still be enabled on restart. | 192 // migration as the type will still be enabled on restart. |
| 208 SLOG(WARNING) << "Unable to migrate, configuration failed!"; | 193 SLOG(WARNING) << "Unable to migrate, configuration failed!"; |
| 209 ChangeState(IDLE); | 194 ChangeState(IDLE); |
| 210 to_migrate_.clear(); | 195 to_migrate_.Clear(); |
| 211 return; | 196 return; |
| 212 } | 197 } |
| 213 | 198 |
| 214 if (state_ == DISABLING_TYPES) { | 199 if (state_ == DISABLING_TYPES) { |
| 215 const syncable::ModelTypeSet& unsynced_types = | 200 const syncable::ModelEnumSet unsynced_types = |
| 216 GetUnsyncedDataTypes(user_share_); | 201 GetUnsyncedDataTypes(user_share_); |
| 217 if (!std::includes(unsynced_types.begin(), unsynced_types.end(), | 202 if (!unsynced_types.HasAll(to_migrate_)) { |
| 218 to_migrate_.begin(), to_migrate_.end())) { | |
| 219 SLOG(WARNING) << "Set of unsynced types: " | 203 SLOG(WARNING) << "Set of unsynced types: " |
| 220 << syncable::ModelTypeSetToString(unsynced_types) | 204 << syncable::ModelEnumSetToString(unsynced_types) |
| 221 << " does not contain types to migrate: " | 205 << " does not contain types to migrate: " |
| 222 << syncable::ModelTypeSetToString(to_migrate_) | 206 << syncable::ModelEnumSetToString(to_migrate_) |
| 223 << "; not re-enabling yet"; | 207 << "; not re-enabling yet"; |
| 224 return; | 208 return; |
| 225 } | 209 } |
| 226 | 210 |
| 227 ChangeState(REENABLING_TYPES); | 211 ChangeState(REENABLING_TYPES); |
| 228 // Don't use |to_migrate_| for the re-enabling because the user | 212 // Don't use |to_migrate_| for the re-enabling because the user |
| 229 // may have chosen to disable types during the migration. | 213 // may have chosen to disable types during the migration. |
| 230 ModelTypeSet full_set; | 214 const ModelEnumSet full_set = service_->GetPreferredDataTypes(); |
| 231 service_->GetPreferredDataTypes(&full_set); | |
| 232 SDVLOG(1) << "BackendMigrator re-enabling types: " | 215 SDVLOG(1) << "BackendMigrator re-enabling types: " |
| 233 << syncable::ModelTypeSetToString(full_set); | 216 << syncable::ModelEnumSetToString(full_set); |
| 234 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION); | 217 manager_->Configure(full_set, sync_api::CONFIGURE_REASON_MIGRATION); |
| 235 } else if (state_ == REENABLING_TYPES) { | 218 } else if (state_ == REENABLING_TYPES) { |
| 236 // We're done! | 219 // We're done! |
| 237 ChangeState(IDLE); | 220 ChangeState(IDLE); |
| 238 | 221 |
| 239 SDVLOG(1) << "BackendMigrator: Migration complete for: " | 222 SDVLOG(1) << "BackendMigrator: Migration complete for: " |
| 240 << syncable::ModelTypeSetToString(to_migrate_); | 223 << syncable::ModelEnumSetToString(to_migrate_); |
| 241 to_migrate_.clear(); | 224 to_migrate_.Clear(); |
| 242 } | 225 } |
| 243 } | 226 } |
| 244 | 227 |
| 245 BackendMigrator::State BackendMigrator::state() const { | 228 BackendMigrator::State BackendMigrator::state() const { |
| 246 return state_; | 229 return state_; |
| 247 } | 230 } |
| 248 | 231 |
| 249 syncable::ModelTypeSet | 232 syncable::ModelEnumSet |
| 250 BackendMigrator::GetPendingMigrationTypesForTest() const { | 233 BackendMigrator::GetPendingMigrationTypesForTest() const { |
| 251 return to_migrate_; | 234 return to_migrate_; |
| 252 } | 235 } |
| 253 | 236 |
| 254 #undef SDVLOG | 237 #undef SDVLOG |
| 255 | 238 |
| 256 #undef SLOG | 239 #undef SLOG |
| 257 | 240 |
| 258 }; // namespace browser_sync | 241 }; // namespace browser_sync |
| OLD | NEW |