| 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 "components/sync/driver/backend_migrator.h" | 5 #include "components/sync/driver/backend_migrator.h" |
| 6 | 6 |
| 7 #include "base/location.h" | 7 #include "base/location.h" |
| 8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
| 11 #include "base/tracked_objects.h" | 11 #include "base/tracked_objects.h" |
| 12 #include "components/sync/core/configure_reason.h" | 12 #include "components/sync/core/configure_reason.h" |
| 13 #include "components/sync/core/read_transaction.h" | 13 #include "components/sync/core/read_transaction.h" |
| 14 #include "components/sync/driver/sync_service.h" | 14 #include "components/sync/driver/sync_service.h" |
| 15 #include "components/sync/protocol/sync.pb.h" | 15 #include "components/sync/protocol/sync.pb.h" |
| 16 #include "components/sync/syncable/directory.h" // TODO(tim): Bug 131130. | 16 #include "components/sync/syncable/directory.h" // TODO(tim): Bug 131130. |
| 17 | 17 |
| 18 namespace syncer { | 18 using syncer::ModelTypeSet; |
| 19 |
| 20 namespace browser_sync { |
| 21 |
| 22 using syncer::ModelTypeToString; |
| 19 | 23 |
| 20 MigrationObserver::~MigrationObserver() {} | 24 MigrationObserver::~MigrationObserver() {} |
| 21 | 25 |
| 22 BackendMigrator::BackendMigrator(const std::string& name, | 26 BackendMigrator::BackendMigrator(const std::string& name, |
| 23 UserShare* user_share, | 27 syncer::UserShare* user_share, |
| 24 SyncService* service, | 28 sync_driver::SyncService* service, |
| 25 DataTypeManager* manager, | 29 sync_driver::DataTypeManager* manager, |
| 26 const base::Closure& migration_done_callback) | 30 const base::Closure& migration_done_callback) |
| 27 : name_(name), | 31 : name_(name), |
| 28 user_share_(user_share), | 32 user_share_(user_share), |
| 29 service_(service), | 33 service_(service), |
| 30 manager_(manager), | 34 manager_(manager), |
| 31 state_(IDLE), | 35 state_(IDLE), |
| 32 migration_done_callback_(migration_done_callback), | 36 migration_done_callback_(migration_done_callback), |
| 33 weak_ptr_factory_(this) {} | 37 weak_ptr_factory_(this) {} |
| 34 | 38 |
| 35 BackendMigrator::~BackendMigrator() {} | 39 BackendMigrator::~BackendMigrator() {} |
| 36 | 40 |
| 37 // 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 |
| 38 // are multiple syncer threads involved. | 42 // are multiple syncer threads involved. |
| 39 | 43 |
| 40 #define SLOG(severity) LOG(severity) << name_ << ": " | 44 #define SLOG(severity) LOG(severity) << name_ << ": " |
| 41 | 45 |
| 42 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " | 46 #define SDVLOG(verbose_level) DVLOG(verbose_level) << name_ << ": " |
| 43 | 47 |
| 44 void BackendMigrator::MigrateTypes(ModelTypeSet types) { | 48 void BackendMigrator::MigrateTypes(syncer::ModelTypeSet types) { |
| 45 const ModelTypeSet old_to_migrate = to_migrate_; | 49 const ModelTypeSet old_to_migrate = to_migrate_; |
| 46 to_migrate_.PutAll(types); | 50 to_migrate_.PutAll(types); |
| 47 SDVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) | 51 SDVLOG(1) << "MigrateTypes called with " << ModelTypeSetToString(types) |
| 48 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) | 52 << ", old_to_migrate = " << ModelTypeSetToString(old_to_migrate) |
| 49 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); | 53 << ", to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 50 if (old_to_migrate == to_migrate_) { | 54 if (old_to_migrate == to_migrate_) { |
| 51 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; | 55 SDVLOG(1) << "MigrateTypes called with no new types; ignoring"; |
| 52 return; | 56 return; |
| 53 } | 57 } |
| 54 | 58 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 80 } | 84 } |
| 81 | 85 |
| 82 void BackendMigrator::ChangeState(State new_state) { | 86 void BackendMigrator::ChangeState(State new_state) { |
| 83 state_ = new_state; | 87 state_ = new_state; |
| 84 FOR_EACH_OBSERVER(MigrationObserver, migration_observers_, | 88 FOR_EACH_OBSERVER(MigrationObserver, migration_observers_, |
| 85 OnMigrationStateChange()); | 89 OnMigrationStateChange()); |
| 86 } | 90 } |
| 87 | 91 |
| 88 bool BackendMigrator::TryStart() { | 92 bool BackendMigrator::TryStart() { |
| 89 DCHECK_EQ(state_, WAITING_TO_START); | 93 DCHECK_EQ(state_, WAITING_TO_START); |
| 90 if (manager_->state() == DataTypeManager::CONFIGURED) { | 94 if (manager_->state() == sync_driver::DataTypeManager::CONFIGURED) { |
| 91 RestartMigration(); | 95 RestartMigration(); |
| 92 return true; | 96 return true; |
| 93 } | 97 } |
| 94 return false; | 98 return false; |
| 95 } | 99 } |
| 96 | 100 |
| 97 void BackendMigrator::RestartMigration() { | 101 void BackendMigrator::RestartMigration() { |
| 98 // 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. |
| 99 ChangeState(DISABLING_TYPES); | 103 ChangeState(DISABLING_TYPES); |
| 100 SDVLOG(1) << "BackendMigrator disabling types " | 104 SDVLOG(1) << "BackendMigrator disabling types " |
| 101 << ModelTypeSetToString(to_migrate_); | 105 << ModelTypeSetToString(to_migrate_); |
| 102 | 106 |
| 103 manager_->PurgeForMigration(to_migrate_, CONFIGURE_REASON_MIGRATION); | 107 manager_->PurgeForMigration(to_migrate_, syncer::CONFIGURE_REASON_MIGRATION); |
| 104 } | 108 } |
| 105 | 109 |
| 106 void BackendMigrator::OnConfigureDone( | 110 void BackendMigrator::OnConfigureDone( |
| 107 const DataTypeManager::ConfigureResult& result) { | 111 const sync_driver::DataTypeManager::ConfigureResult& result) { |
| 108 if (state_ == IDLE) | 112 if (state_ == IDLE) |
| 109 return; | 113 return; |
| 110 | 114 |
| 111 // |manager_|'s methods aren't re-entrant, and we're notified from | 115 // |manager_|'s methods aren't re-entrant, and we're notified from |
| 112 // them, so post a task to avoid problems. | 116 // them, so post a task to avoid problems. |
| 113 SDVLOG(1) << "Posting OnConfigureDoneImpl"; | 117 SDVLOG(1) << "Posting OnConfigureDoneImpl"; |
| 114 base::ThreadTaskRunnerHandle::Get()->PostTask( | 118 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 115 FROM_HERE, base::Bind(&BackendMigrator::OnConfigureDoneImpl, | 119 FROM_HERE, base::Bind(&BackendMigrator::OnConfigureDoneImpl, |
| 116 weak_ptr_factory_.GetWeakPtr(), result)); | 120 weak_ptr_factory_.GetWeakPtr(), result)); |
| 117 } | 121 } |
| 118 | 122 |
| 119 namespace { | 123 namespace { |
| 120 | 124 |
| 121 ModelTypeSet GetUnsyncedDataTypes(UserShare* user_share) { | 125 syncer::ModelTypeSet GetUnsyncedDataTypes(syncer::UserShare* user_share) { |
| 122 ReadTransaction trans(FROM_HERE, user_share); | 126 syncer::ReadTransaction trans(FROM_HERE, user_share); |
| 123 ModelTypeSet unsynced_data_types; | 127 syncer::ModelTypeSet unsynced_data_types; |
| 124 for (int i = FIRST_REAL_MODEL_TYPE; i < MODEL_TYPE_COUNT; ++i) { | 128 for (int i = syncer::FIRST_REAL_MODEL_TYPE; i < syncer::MODEL_TYPE_COUNT; |
| 125 ModelType type = ModelTypeFromInt(i); | 129 ++i) { |
| 130 syncer::ModelType type = syncer::ModelTypeFromInt(i); |
| 126 sync_pb::DataTypeProgressMarker progress_marker; | 131 sync_pb::DataTypeProgressMarker progress_marker; |
| 127 trans.GetDirectory()->GetDownloadProgress(type, &progress_marker); | 132 trans.GetDirectory()->GetDownloadProgress(type, &progress_marker); |
| 128 if (progress_marker.token().empty()) { | 133 if (progress_marker.token().empty()) { |
| 129 unsynced_data_types.Put(type); | 134 unsynced_data_types.Put(type); |
| 130 } | 135 } |
| 131 } | 136 } |
| 132 return unsynced_data_types; | 137 return unsynced_data_types; |
| 133 } | 138 } |
| 134 | 139 |
| 135 } // namespace | 140 } // namespace |
| 136 | 141 |
| 137 void BackendMigrator::OnConfigureDoneImpl( | 142 void BackendMigrator::OnConfigureDoneImpl( |
| 138 const DataTypeManager::ConfigureResult& result) { | 143 const sync_driver::DataTypeManager::ConfigureResult& result) { |
| 139 SDVLOG(1) << "OnConfigureDone with requested types " | 144 SDVLOG(1) << "OnConfigureDone with requested types " |
| 140 << ModelTypeSetToString(result.requested_types) << ", status " | 145 << ModelTypeSetToString(result.requested_types) << ", status " |
| 141 << result.status | 146 << result.status |
| 142 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_); | 147 << ", and to_migrate_ = " << ModelTypeSetToString(to_migrate_); |
| 143 if (state_ == WAITING_TO_START) { | 148 if (state_ == WAITING_TO_START) { |
| 144 if (!TryStart()) | 149 if (!TryStart()) |
| 145 SDVLOG(1) << "Manager still not configured; still waiting"; | 150 SDVLOG(1) << "Manager still not configured; still waiting"; |
| 146 return; | 151 return; |
| 147 } | 152 } |
| 148 | 153 |
| 149 DCHECK_GT(state_, WAITING_TO_START); | 154 DCHECK_GT(state_, WAITING_TO_START); |
| 150 | 155 |
| 151 const ModelTypeSet intersection = | 156 const ModelTypeSet intersection = |
| 152 Intersection(result.requested_types, to_migrate_); | 157 Intersection(result.requested_types, to_migrate_); |
| 153 // This intersection check is to determine if our disable request | 158 // This intersection check is to determine if our disable request |
| 154 // was interrupted by a user changing preferred types. | 159 // was interrupted by a user changing preferred types. |
| 155 if (state_ == DISABLING_TYPES && !intersection.Empty()) { | 160 if (state_ == DISABLING_TYPES && !intersection.Empty()) { |
| 156 SDVLOG(1) << "Disable request interrupted by user changing types"; | 161 SDVLOG(1) << "Disable request interrupted by user changing types"; |
| 157 RestartMigration(); | 162 RestartMigration(); |
| 158 return; | 163 return; |
| 159 } | 164 } |
| 160 | 165 |
| 161 if (result.status != DataTypeManager::OK) { | 166 if (result.status != sync_driver::DataTypeManager::OK) { |
| 162 // If this fails, and we're disabling types, a type may or may not be | 167 // If this fails, and we're disabling types, a type may or may not be |
| 163 // disabled until the user restarts the browser. If this wasn't an abort, | 168 // disabled until the user restarts the browser. If this wasn't an abort, |
| 164 // any failure will be reported as an unrecoverable error to the UI. If it | 169 // any failure will be reported as an unrecoverable error to the UI. If it |
| 165 // was an abort, then typically things are shutting down anyway. There isn't | 170 // was an abort, then typically things are shutting down anyway. There isn't |
| 166 // much we can do in any case besides wait until a restart to try again. | 171 // much we can do in any case besides wait until a restart to try again. |
| 167 // The server will send down MIGRATION_DONE again for types needing | 172 // The server will send down MIGRATION_DONE again for types needing |
| 168 // migration as the type will still be enabled on restart. | 173 // migration as the type will still be enabled on restart. |
| 169 SLOG(WARNING) << "Unable to migrate, configuration failed!"; | 174 SLOG(WARNING) << "Unable to migrate, configuration failed!"; |
| 170 ChangeState(IDLE); | 175 ChangeState(IDLE); |
| 171 to_migrate_.Clear(); | 176 to_migrate_.Clear(); |
| 172 return; | 177 return; |
| 173 } | 178 } |
| 174 | 179 |
| 175 if (state_ == DISABLING_TYPES) { | 180 if (state_ == DISABLING_TYPES) { |
| 176 const ModelTypeSet unsynced_types = GetUnsyncedDataTypes(user_share_); | 181 const syncer::ModelTypeSet unsynced_types = |
| 182 GetUnsyncedDataTypes(user_share_); |
| 177 if (!unsynced_types.HasAll(to_migrate_)) { | 183 if (!unsynced_types.HasAll(to_migrate_)) { |
| 178 SLOG(WARNING) << "Set of unsynced types: " | 184 SLOG(WARNING) << "Set of unsynced types: " |
| 179 << ModelTypeSetToString(unsynced_types) | 185 << syncer::ModelTypeSetToString(unsynced_types) |
| 180 << " does not contain types to migrate: " | 186 << " does not contain types to migrate: " |
| 181 << ModelTypeSetToString(to_migrate_) | 187 << syncer::ModelTypeSetToString(to_migrate_) |
| 182 << "; not re-enabling yet"; | 188 << "; not re-enabling yet"; |
| 183 return; | 189 return; |
| 184 } | 190 } |
| 185 | 191 |
| 186 ChangeState(REENABLING_TYPES); | 192 ChangeState(REENABLING_TYPES); |
| 187 // Don't use |to_migrate_| for the re-enabling because the user | 193 // Don't use |to_migrate_| for the re-enabling because the user |
| 188 // may have chosen to disable types during the migration. | 194 // may have chosen to disable types during the migration. |
| 189 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); | 195 const ModelTypeSet full_set = service_->GetPreferredDataTypes(); |
| 190 SDVLOG(1) << "BackendMigrator re-enabling types: " | 196 SDVLOG(1) << "BackendMigrator re-enabling types: " |
| 191 << ModelTypeSetToString(full_set); | 197 << syncer::ModelTypeSetToString(full_set); |
| 192 manager_->Configure(full_set, CONFIGURE_REASON_MIGRATION); | 198 manager_->Configure(full_set, syncer::CONFIGURE_REASON_MIGRATION); |
| 193 } else if (state_ == REENABLING_TYPES) { | 199 } else if (state_ == REENABLING_TYPES) { |
| 194 // We're done! | 200 // We're done! |
| 195 ChangeState(IDLE); | 201 ChangeState(IDLE); |
| 196 | 202 |
| 197 SDVLOG(1) << "BackendMigrator: Migration complete for: " | 203 SDVLOG(1) << "BackendMigrator: Migration complete for: " |
| 198 << ModelTypeSetToString(to_migrate_); | 204 << syncer::ModelTypeSetToString(to_migrate_); |
| 199 to_migrate_.Clear(); | 205 to_migrate_.Clear(); |
| 200 | 206 |
| 201 if (!migration_done_callback_.is_null()) | 207 if (!migration_done_callback_.is_null()) |
| 202 migration_done_callback_.Run(); | 208 migration_done_callback_.Run(); |
| 203 } | 209 } |
| 204 } | 210 } |
| 205 | 211 |
| 206 BackendMigrator::State BackendMigrator::state() const { | 212 BackendMigrator::State BackendMigrator::state() const { |
| 207 return state_; | 213 return state_; |
| 208 } | 214 } |
| 209 | 215 |
| 210 ModelTypeSet BackendMigrator::GetPendingMigrationTypesForTest() const { | 216 syncer::ModelTypeSet BackendMigrator::GetPendingMigrationTypesForTest() const { |
| 211 return to_migrate_; | 217 return to_migrate_; |
| 212 } | 218 } |
| 213 | 219 |
| 214 #undef SDVLOG | 220 #undef SDVLOG |
| 215 | 221 |
| 216 #undef SLOG | 222 #undef SLOG |
| 217 | 223 |
| 218 }; // namespace browser_sync | 224 }; // namespace browser_sync |
| OLD | NEW |