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