| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "base/string_number_conversions.h" | 8 #include "base/string_number_conversions.h" |
| 9 #include "base/tracked_objects.h" | 9 #include "base/tracked_objects.h" |
| 10 #include "chrome/browser/sync/profile_sync_service.h" | 10 #include "chrome/browser/sync/profile_sync_service.h" |
| 11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
| 12 #include "content/public/browser/notification_details.h" | 12 #include "content/public/browser/notification_details.h" |
| 13 #include "content/public/browser/notification_source.h" | 13 #include "content/public/browser/notification_source.h" |
| 14 #include "sync/internal_api/public/configure_reason.h" | 14 #include "sync/internal_api/public/configure_reason.h" |
| 15 #include "sync/internal_api/public/read_transaction.h" | 15 #include "sync/internal_api/public/read_transaction.h" |
| 16 #include "sync/protocol/sync.pb.h" | 16 #include "sync/protocol/sync.pb.h" |
| 17 #include "sync/syncable/directory.h" // TODO(tim): Bug 131130. | 17 #include "sync/syncable/directory.h" // TODO(tim): Bug 131130. |
| 18 | 18 |
| 19 using syncable::ModelTypeSet; | 19 using syncer::ModelTypeSet; |
| 20 | 20 |
| 21 namespace browser_sync { | 21 namespace browser_sync { |
| 22 | 22 |
| 23 using syncable::ModelTypeToString; | 23 using syncer::ModelTypeToString; |
| 24 | 24 |
| 25 MigrationObserver::~MigrationObserver() {} | 25 MigrationObserver::~MigrationObserver() {} |
| 26 | 26 |
| 27 BackendMigrator::BackendMigrator(const std::string& name, | 27 BackendMigrator::BackendMigrator(const std::string& name, |
| 28 syncer::UserShare* user_share, | 28 syncer::UserShare* user_share, |
| 29 ProfileSyncService* service, | 29 ProfileSyncService* service, |
| 30 DataTypeManager* manager, | 30 DataTypeManager* manager, |
| 31 const base::Closure &migration_done_callback) | 31 const base::Closure &migration_done_callback) |
| 32 : name_(name), user_share_(user_share), service_(service), | 32 : name_(name), user_share_(user_share), service_(service), |
| 33 manager_(manager), state_(IDLE), | 33 manager_(manager), state_(IDLE), |
| 34 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 34 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 35 migration_done_callback_(migration_done_callback) { | 35 migration_done_callback_(migration_done_callback) { |
| 36 } | 36 } |
| 37 | 37 |
| 38 BackendMigrator::~BackendMigrator() { | 38 BackendMigrator::~BackendMigrator() { |
| 39 } | 39 } |
| 40 | 40 |
| 41 // Helper macros to log with the syncer thread name; useful when there | 41 // Helper macros to log with the syncer thread name; useful when there |
| 42 // are multiple syncer threads involved. | 42 // are multiple syncer threads involved. |
| 43 | 43 |
| 44 #define SLOG(severity) LOG(severity) << name_ << ": " | 44 #define SLOG(severity) LOG(severity) << name_ << ": " |
| 45 | 45 |
| 46 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " | 46 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " |
| 47 | 47 |
| 48 void BackendMigrator::MigrateTypes(syncable::ModelTypeSet types) { | 48 void BackendMigrator::MigrateTypes(syncer::ModelTypeSet types) { |
| 49 const ModelTypeSet old_to_migrate = to_migrate_; | 49 const ModelTypeSet old_to_migrate = to_migrate_; |
| 50 to_migrate_.PutAll(types); | 50 to_migrate_.PutAll(types); |
| 51 SDVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) | 51 SDVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) |
| 52 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) | 52 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) |
| 53 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); | 53 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 54 if (old_to_migrate.Equals(to_migrate_)) { | 54 if (old_to_migrate.Equals(to_migrate_)) { |
| 55 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; | 55 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; |
| 56 return; | 56 return; |
| 57 } | 57 } |
| 58 | 58 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 return false; | 98 return false; |
| 99 } | 99 } |
| 100 | 100 |
| 101 void BackendMigrator::RestartMigration() { | 101 void BackendMigrator::RestartMigration() { |
| 102 // We'll now disable any running types that need to be migrated. | 102 // We'll now disable any running types that need to be migrated. |
| 103 ChangeState(DISABLING_TYPES); | 103 ChangeState(DISABLING_TYPES); |
| 104 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); | 104 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); |
| 105 const ModelTypeSet difference = Difference(full_set, to_migrate_); | 105 const ModelTypeSet difference = Difference(full_set, to_migrate_); |
| 106 bool configure_with_nigori = !to_migrate_.Has(syncable::NIGORI); | 106 bool configure_with_nigori = !to_migrate_.Has(syncer::NIGORI); |
| 107 SDVLOG(1) << "BackendMigrator disabling types " | 107 SDVLOG(1) << "BackendMigrator disabling types " |
| 108 << ModelTypeSetToString(to_migrate_) << "; configuring " | 108 << ModelTypeSetToString(to_migrate_) << "; configuring " |
| 109 << ModelTypeSetToString(difference) | 109 << ModelTypeSetToString(difference) |
| 110 << (configure_with_nigori ? " with nigori" : " without nigori"); | 110 << (configure_with_nigori ? " with nigori" : " without nigori"); |
| 111 | 111 |
| 112 // Add nigori for config or not based upon if the server told us to migrate | 112 // Add nigori for config or not based upon if the server told us to migrate |
| 113 // nigori or not. | 113 // nigori or not. |
| 114 if (configure_with_nigori) { | 114 if (configure_with_nigori) { |
| 115 manager_->Configure(difference, syncer::CONFIGURE_REASON_MIGRATION); | 115 manager_->Configure(difference, syncer::CONFIGURE_REASON_MIGRATION); |
| 116 } else { | 116 } else { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 128 // them, so post a task to avoid problems. | 128 // them, so post a task to avoid problems. |
| 129 SDVLOG(1) << "Posting OnConfigureDoneImpl"; | 129 SDVLOG(1) << "Posting OnConfigureDoneImpl"; |
| 130 MessageLoop::current()->PostTask( | 130 MessageLoop::current()->PostTask( |
| 131 FROM_HERE, | 131 FROM_HERE, |
| 132 base::Bind(&BackendMigrator::OnConfigureDoneImpl, | 132 base::Bind(&BackendMigrator::OnConfigureDoneImpl, |
| 133 weak_ptr_factory_.GetWeakPtr(), result)); | 133 weak_ptr_factory_.GetWeakPtr(), result)); |
| 134 } | 134 } |
| 135 | 135 |
| 136 namespace { | 136 namespace { |
| 137 | 137 |
| 138 syncable::ModelTypeSet GetUnsyncedDataTypes(syncer::UserShare* user_share) { | 138 syncer::ModelTypeSet GetUnsyncedDataTypes(syncer::UserShare* user_share) { |
| 139 syncer::ReadTransaction trans(FROM_HERE, user_share); | 139 syncer::ReadTransaction trans(FROM_HERE, user_share); |
| 140 syncable::ModelTypeSet unsynced_data_types; | 140 syncer::ModelTypeSet unsynced_data_types; |
| 141 for (int i = syncable::FIRST_REAL_MODEL_TYPE; | 141 for (int i = syncer::FIRST_REAL_MODEL_TYPE; |
| 142 i < syncable::MODEL_TYPE_COUNT; ++i) { | 142 i < syncer::MODEL_TYPE_COUNT; ++i) { |
| 143 syncable::ModelType type = syncable::ModelTypeFromInt(i); | 143 syncer::ModelType type = syncer::ModelTypeFromInt(i); |
| 144 sync_pb::DataTypeProgressMarker progress_marker; | 144 sync_pb::DataTypeProgressMarker progress_marker; |
| 145 trans.GetDirectory()->GetDownloadProgress(type, &progress_marker); | 145 trans.GetDirectory()->GetDownloadProgress(type, &progress_marker); |
| 146 if (progress_marker.token().empty()) { | 146 if (progress_marker.token().empty()) { |
| 147 unsynced_data_types.Put(type); | 147 unsynced_data_types.Put(type); |
| 148 } | 148 } |
| 149 } | 149 } |
| 150 return unsynced_data_types; | 150 return unsynced_data_types; |
| 151 } | 151 } |
| 152 | 152 |
| 153 } // namespace | 153 } // namespace |
| (...skipping 30 matching lines...) Expand all Loading... |
| 184 // much we can do in any case besides wait until a restart to try again. | 184 // much we can do in any case besides wait until a restart to try again. |
| 185 // The server will send down MIGRATION_DONE again for types needing | 185 // The server will send down MIGRATION_DONE again for types needing |
| 186 // migration as the type will still be enabled on restart. | 186 // migration as the type will still be enabled on restart. |
| 187 SLOG(WARNING) << "Unable to migrate, configuration failed!"; | 187 SLOG(WARNING) << "Unable to migrate, configuration failed!"; |
| 188 ChangeState(IDLE); | 188 ChangeState(IDLE); |
| 189 to_migrate_.Clear(); | 189 to_migrate_.Clear(); |
| 190 return; | 190 return; |
| 191 } | 191 } |
| 192 | 192 |
| 193 if (state_ == DISABLING_TYPES) { | 193 if (state_ == DISABLING_TYPES) { |
| 194 const syncable::ModelTypeSet unsynced_types = | 194 const syncer::ModelTypeSet unsynced_types = |
| 195 GetUnsyncedDataTypes(user_share_); | 195 GetUnsyncedDataTypes(user_share_); |
| 196 if (!unsynced_types.HasAll(to_migrate_)) { | 196 if (!unsynced_types.HasAll(to_migrate_)) { |
| 197 SLOG(WARNING) << "Set of unsynced types: " | 197 SLOG(WARNING) << "Set of unsynced types: " |
| 198 << syncable::ModelTypeSetToString(unsynced_types) | 198 << syncer::ModelTypeSetToString(unsynced_types) |
| 199 << " does not contain types to migrate: " | 199 << " does not contain types to migrate: " |
| 200 << syncable::ModelTypeSetToString(to_migrate_) | 200 << syncer::ModelTypeSetToString(to_migrate_) |
| 201 << "; not re-enabling yet"; | 201 << "; not re-enabling yet"; |
| 202 return; | 202 return; |
| 203 } | 203 } |
| 204 | 204 |
| 205 ChangeState(REENABLING_TYPES); | 205 ChangeState(REENABLING_TYPES); |
| 206 // Don't use |to_migrate_| for the re-enabling because the user | 206 // Don't use |to_migrate_| for the re-enabling because the user |
| 207 // may have chosen to disable types during the migration. | 207 // may have chosen to disable types during the migration. |
| 208 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); | 208 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); |
| 209 SDVLOG(1) << "BackendMigrator re-enabling types: " | 209 SDVLOG(1) << "BackendMigrator re-enabling types: " |
| 210 << syncable::ModelTypeSetToString(full_set); | 210 << syncer::ModelTypeSetToString(full_set); |
| 211 manager_->Configure(full_set, syncer::CONFIGURE_REASON_MIGRATION); | 211 manager_->Configure(full_set, syncer::CONFIGURE_REASON_MIGRATION); |
| 212 } else if (state_ == REENABLING_TYPES) { | 212 } else if (state_ == REENABLING_TYPES) { |
| 213 // We're done! | 213 // We're done! |
| 214 ChangeState(IDLE); | 214 ChangeState(IDLE); |
| 215 | 215 |
| 216 SDVLOG(1) << "BackendMigrator: Migration complete for: " | 216 SDVLOG(1) << "BackendMigrator: Migration complete for: " |
| 217 << syncable::ModelTypeSetToString(to_migrate_); | 217 << syncer::ModelTypeSetToString(to_migrate_); |
| 218 to_migrate_.Clear(); | 218 to_migrate_.Clear(); |
| 219 | 219 |
| 220 if (!migration_done_callback_.is_null()) | 220 if (!migration_done_callback_.is_null()) |
| 221 migration_done_callback_.Run(); | 221 migration_done_callback_.Run(); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 | 224 |
| 225 BackendMigrator::State BackendMigrator::state() const { | 225 BackendMigrator::State BackendMigrator::state() const { |
| 226 return state_; | 226 return state_; |
| 227 } | 227 } |
| 228 | 228 |
| 229 syncable::ModelTypeSet | 229 syncer::ModelTypeSet BackendMigrator::GetPendingMigrationTypesForTest() const { |
| 230 BackendMigrator::GetPendingMigrationTypesForTest() const { | |
| 231 return to_migrate_; | 230 return to_migrate_; |
| 232 } | 231 } |
| 233 | 232 |
| 234 #undef SDVLOG | 233 #undef SDVLOG |
| 235 | 234 |
| 236 #undef SLOG | 235 #undef SLOG |
| 237 | 236 |
| 238 }; // namespace browser_sync | 237 }; // namespace browser_sync |
| OLD | NEW |