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