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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/sync/glue/sync_backend_host.h" | 7 #include "chrome/browser/sync/glue/sync_backend_host.h" |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
16 #include "base/file_util.h" | 16 #include "base/file_util.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/metrics/histogram.h" | 18 #include "base/metrics/histogram.h" |
19 #include "base/threading/sequenced_worker_pool.h" | |
20 #include "base/threading/thread_restrictions.h" | 19 #include "base/threading/thread_restrictions.h" |
21 #include "base/timer.h" | 20 #include "base/timer.h" |
22 #include "base/tracked_objects.h" | 21 #include "base/tracked_objects.h" |
23 #include "base/utf_string_conversions.h" | 22 #include "base/utf_string_conversions.h" |
24 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/signin/token_service.h" | 24 #include "chrome/browser/signin/token_service.h" |
26 #include "chrome/browser/sync/glue/bridged_invalidator.h" | 25 #include "chrome/browser/sync/glue/bridged_invalidator.h" |
27 #include "chrome/browser/sync/glue/change_processor.h" | 26 #include "chrome/browser/sync/glue/change_processor.h" |
28 #include "chrome/browser/sync/glue/chrome_encryptor.h" | 27 #include "chrome/browser/sync/glue/chrome_encryptor.h" |
29 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" | 28 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" |
30 #include "chrome/browser/sync/glue/sync_backend_registrar.h" | 29 #include "chrome/browser/sync/glue/sync_backend_registrar.h" |
30 #include "chrome/browser/sync/glue/synced_device_tracker.h" | |
31 #include "chrome/browser/sync/invalidations/invalidator_storage.h" | 31 #include "chrome/browser/sync/invalidations/invalidator_storage.h" |
32 #include "chrome/browser/sync/sync_prefs.h" | 32 #include "chrome/browser/sync/sync_prefs.h" |
33 #include "chrome/common/chrome_notification_types.h" | 33 #include "chrome/common/chrome_notification_types.h" |
34 #include "chrome/common/chrome_switches.h" | 34 #include "chrome/common/chrome_switches.h" |
35 #include "chrome/common/chrome_version_info.h" | 35 #include "chrome/common/chrome_version_info.h" |
36 #include "chrome/common/net/gaia/gaia_constants.h" | 36 #include "chrome/common/net/gaia/gaia_constants.h" |
37 #include "content/public/browser/browser_thread.h" | 37 #include "content/public/browser/browser_thread.h" |
38 #include "content/public/browser/notification_service.h" | 38 #include "content/public/browser/notification_service.h" |
39 #include "content/public/common/content_client.h" | 39 #include "content/public/common/content_client.h" |
40 #include "jingle/notifier/base/notification_method.h" | 40 #include "jingle/notifier/base/notification_method.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 void DoSetEncryptionPassphrase(const std::string& passphrase, | 154 void DoSetEncryptionPassphrase(const std::string& passphrase, |
155 bool is_explicit); | 155 bool is_explicit); |
156 | 156 |
157 // Called to decrypt the pending keys. | 157 // Called to decrypt the pending keys. |
158 void DoSetDecryptionPassphrase(const std::string& passphrase); | 158 void DoSetDecryptionPassphrase(const std::string& passphrase); |
159 | 159 |
160 // Called to turn on encryption of all sync data as well as | 160 // Called to turn on encryption of all sync data as well as |
161 // reencrypt everything. | 161 // reencrypt everything. |
162 void DoEnableEncryptEverything(); | 162 void DoEnableEncryptEverything(); |
163 | 163 |
164 // Called to load sync encryption state and re-encrypt any types | 164 // Called to perform tasks which require the control data to be downloaded. |
165 // needing encryption as necessary. | 165 // This includes refreshing encryption, setting up the device info change |
166 void DoAssociateNigori(); | 166 // processor, etc. |
167 void DoInitialProcessMetadata(); | |
168 | |
169 void FinishInitialProcessMetadata(); | |
167 | 170 |
168 // The shutdown order is a bit complicated: | 171 // The shutdown order is a bit complicated: |
169 // 1) From |sync_thread_|, invoke the syncapi Shutdown call to do | 172 // 1) From |sync_thread_|, invoke the syncapi Shutdown call to do |
170 // a final SaveChanges, and close sqlite handles. | 173 // a final SaveChanges, and close sqlite handles. |
171 // 2) Then, from |frontend_loop_|, halt the sync_thread_ (which is | 174 // 2) Then, from |frontend_loop_|, halt the sync_thread_ (which is |
172 // a blocking call). This causes syncapi thread-exit handlers | 175 // a blocking call). This causes syncapi thread-exit handlers |
173 // to run and make use of cached pointers to various components | 176 // to run and make use of cached pointers to various components |
174 // owned implicitly by us. | 177 // owned implicitly by us. |
175 // 3) Destroy this Core. That will delete syncapi components in a | 178 // 3) Destroy this Core. That will delete syncapi components in a |
176 // safe order because the thread that was using them has exited | 179 // safe order because the thread that was using them has exited |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
243 // Our parent's notification bridge (not owned). Non-NULL only | 246 // Our parent's notification bridge (not owned). Non-NULL only |
244 // between calls to DoInitialize() and DoShutdown(). | 247 // between calls to DoInitialize() and DoShutdown(). |
245 ChromeSyncNotificationBridge* chrome_sync_notification_bridge_; | 248 ChromeSyncNotificationBridge* chrome_sync_notification_bridge_; |
246 | 249 |
247 // The timer used to periodically call SaveChanges. | 250 // The timer used to periodically call SaveChanges. |
248 scoped_ptr<base::RepeatingTimer<Core> > save_changes_timer_; | 251 scoped_ptr<base::RepeatingTimer<Core> > save_changes_timer_; |
249 | 252 |
250 // Our encryptor, which uses Chrome's encryption functions. | 253 // Our encryptor, which uses Chrome's encryption functions. |
251 ChromeEncryptor encryptor_; | 254 ChromeEncryptor encryptor_; |
252 | 255 |
256 // A special ChangeProcessor that tracks the DEVICE_INFO type for us. | |
257 SyncedDeviceTracker synced_device_tracker_; | |
258 | |
253 // The top-level syncapi entry point. Lives on the sync thread. | 259 // The top-level syncapi entry point. Lives on the sync thread. |
254 scoped_ptr<syncer::SyncManager> sync_manager_; | 260 scoped_ptr<syncer::SyncManager> sync_manager_; |
255 | 261 |
256 // Whether or not we registered with |sync_manager_| as an invalidation | 262 // Whether or not we registered with |sync_manager_| as an invalidation |
257 // handler. Necessary since we may end up trying to unregister before we | 263 // handler. Necessary since we may end up trying to unregister before we |
258 // register in tests (in synchronous initialization mode). | 264 // register in tests (in synchronous initialization mode). |
259 // | 265 // |
260 // TODO(akalin): Fix this behavior (see http://crbug.com/140354). | 266 // TODO(akalin): Fix this behavior (see http://crbug.com/140354). |
261 bool registered_as_invalidation_handler_; | 267 bool registered_as_invalidation_handler_; |
262 | 268 |
(...skipping 830 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1093 DCHECK(chrome_sync_notification_bridge_); | 1099 DCHECK(chrome_sync_notification_bridge_); |
1094 | 1100 |
1095 sync_manager_ = options.sync_manager_factory->CreateSyncManager(name_); | 1101 sync_manager_ = options.sync_manager_factory->CreateSyncManager(name_); |
1096 sync_manager_->AddObserver(this); | 1102 sync_manager_->AddObserver(this); |
1097 sync_manager_->Init( | 1103 sync_manager_->Init( |
1098 sync_data_folder_path_, | 1104 sync_data_folder_path_, |
1099 options.event_handler, | 1105 options.event_handler, |
1100 options.service_url.host() + options.service_url.path(), | 1106 options.service_url.host() + options.service_url.path(), |
1101 options.service_url.EffectiveIntPort(), | 1107 options.service_url.EffectiveIntPort(), |
1102 options.service_url.SchemeIsSecure(), | 1108 options.service_url.SchemeIsSecure(), |
1103 BrowserThread::GetBlockingPool(), | |
1104 options.make_http_bridge_factory_fn.Run().Pass(), | 1109 options.make_http_bridge_factory_fn.Run().Pass(), |
1105 options.workers, | 1110 options.workers, |
1106 options.extensions_activity_monitor, | 1111 options.extensions_activity_monitor, |
1107 options.registrar /* as SyncManager::ChangeDelegate */, | 1112 options.registrar /* as SyncManager::ChangeDelegate */, |
1108 options.credentials, | 1113 options.credentials, |
1109 scoped_ptr<syncer::Invalidator>(new BridgedInvalidator( | 1114 scoped_ptr<syncer::Invalidator>(new BridgedInvalidator( |
1110 options.chrome_sync_notification_bridge, | 1115 options.chrome_sync_notification_bridge, |
1111 options.invalidator_factory->CreateInvalidator())), | 1116 options.invalidator_factory->CreateInvalidator())), |
1112 options.restored_key_for_bootstrapping, | 1117 options.restored_key_for_bootstrapping, |
1113 options.restored_keystore_key_for_bootstrapping, | 1118 options.restored_keystore_key_for_bootstrapping, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1156 sync_manager_->UpdateRegisteredInvalidationIds(this, ids); | 1161 sync_manager_->UpdateRegisteredInvalidationIds(this, ids); |
1157 } | 1162 } |
1158 } | 1163 } |
1159 | 1164 |
1160 void SyncBackendHost::Core::DoStartSyncing( | 1165 void SyncBackendHost::Core::DoStartSyncing( |
1161 const syncer::ModelSafeRoutingInfo& routing_info) { | 1166 const syncer::ModelSafeRoutingInfo& routing_info) { |
1162 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 1167 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
1163 sync_manager_->StartSyncingNormally(routing_info); | 1168 sync_manager_->StartSyncingNormally(routing_info); |
1164 } | 1169 } |
1165 | 1170 |
1166 void SyncBackendHost::Core::DoAssociateNigori() { | |
1167 DCHECK_EQ(MessageLoop::current(), sync_loop_); | |
1168 sync_manager_->GetEncryptionHandler()->Init(); | |
1169 host_.Call(FROM_HERE, | |
1170 &SyncBackendHost::HandleInitializationCompletedOnFrontendLoop, | |
1171 true); | |
1172 } | |
1173 | |
1174 void SyncBackendHost::Core::DoSetEncryptionPassphrase( | 1171 void SyncBackendHost::Core::DoSetEncryptionPassphrase( |
1175 const std::string& passphrase, | 1172 const std::string& passphrase, |
1176 bool is_explicit) { | 1173 bool is_explicit) { |
1177 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 1174 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
1178 sync_manager_->GetEncryptionHandler()->SetEncryptionPassphrase( | 1175 sync_manager_->GetEncryptionHandler()->SetEncryptionPassphrase( |
1179 passphrase, is_explicit); | 1176 passphrase, is_explicit); |
1180 } | 1177 } |
1181 | 1178 |
1179 void SyncBackendHost::Core::DoInitialProcessMetadata() { | |
1180 DCHECK_EQ(MessageLoop::current(), sync_loop_); | |
1181 | |
1182 // Initialize encryption. | |
1183 sync_manager_->GetEncryptionHandler()->Init(); | |
1184 | |
1185 // Initialize device info. | |
1186 if (sync_manager_->GetUserShare()) { // NULL in some tests. | |
rlarocque
2012/09/08 01:20:44
FakeSyncManager::GetUserShare() returns NULL.
It'
| |
1187 registrar_->ActivateDataType(syncer::DEVICE_INFO, | |
1188 syncer::GROUP_PASSIVE, | |
1189 &synced_device_tracker_, | |
1190 sync_manager_->GetUserShare()); | |
1191 synced_device_tracker_.InitLocalDeviceInfo( | |
1192 base::Bind(&SyncBackendHost::Core::FinishInitialProcessMetadata, | |
1193 this)); | |
1194 } else { | |
1195 FinishInitialProcessMetadata(); | |
1196 } | |
1197 } | |
1198 | |
1199 void SyncBackendHost::Core::FinishInitialProcessMetadata() { | |
1200 host_.Call( | |
1201 FROM_HERE, | |
1202 &SyncBackendHost::HandleInitializationCompletedOnFrontendLoop, | |
1203 true); | |
1204 } | |
1205 | |
1182 void SyncBackendHost::Core::DoSetDecryptionPassphrase( | 1206 void SyncBackendHost::Core::DoSetDecryptionPassphrase( |
1183 const std::string& passphrase) { | 1207 const std::string& passphrase) { |
1184 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 1208 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
1185 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase( | 1209 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase( |
1186 passphrase); | 1210 passphrase); |
1187 } | 1211 } |
1188 | 1212 |
1189 void SyncBackendHost::Core::DoEnableEncryptEverything() { | 1213 void SyncBackendHost::Core::DoEnableEncryptEverything() { |
1190 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 1214 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
1191 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything(); | 1215 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything(); |
1192 } | 1216 } |
1193 | 1217 |
1194 void SyncBackendHost::Core::DoStopSyncManagerForShutdown( | 1218 void SyncBackendHost::Core::DoStopSyncManagerForShutdown( |
1195 const base::Closure& closure) { | 1219 const base::Closure& closure) { |
1196 if (sync_manager_.get()) { | 1220 if (sync_manager_.get()) { |
1197 sync_manager_->StopSyncingForShutdown(closure); | 1221 sync_manager_->StopSyncingForShutdown(closure); |
1198 } else { | 1222 } else { |
1199 sync_loop_->PostTask(FROM_HERE, closure); | 1223 sync_loop_->PostTask(FROM_HERE, closure); |
1200 } | 1224 } |
1201 } | 1225 } |
1202 | 1226 |
1203 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) { | 1227 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) { |
1204 DCHECK_EQ(MessageLoop::current(), sync_loop_); | 1228 DCHECK_EQ(MessageLoop::current(), sync_loop_); |
1205 DoDestroySyncManager(); | 1229 DoDestroySyncManager(); |
1206 | 1230 |
1231 // It's safe to do this even if the type was never activated. | |
1232 registrar_->DeactivateDataType(syncer::DEVICE_INFO); | |
1233 | |
1207 chrome_sync_notification_bridge_ = NULL; | 1234 chrome_sync_notification_bridge_ = NULL; |
1208 registrar_ = NULL; | 1235 registrar_ = NULL; |
1209 | 1236 |
1210 if (sync_disabled) | 1237 if (sync_disabled) |
1211 DeleteSyncDataFolder(); | 1238 DeleteSyncDataFolder(); |
1212 | 1239 |
1213 sync_loop_ = NULL; | 1240 sync_loop_ = NULL; |
1214 | 1241 |
1215 host_.Reset(); | 1242 host_.Reset(); |
1216 } | 1243 } |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1338 | 1365 |
1339 // Run initialization state machine. | 1366 // Run initialization state machine. |
1340 switch (initialization_state_) { | 1367 switch (initialization_state_) { |
1341 case NOT_INITIALIZED: | 1368 case NOT_INITIALIZED: |
1342 // This configuration should result in a download request if the nigori | 1369 // This configuration should result in a download request if the nigori |
1343 // type's initial_sync_ended bit is unset. If the download request | 1370 // type's initial_sync_ended bit is unset. If the download request |
1344 // contains progress markers, there is a risk that the server will try to | 1371 // contains progress markers, there is a risk that the server will try to |
1345 // trigger migration. That would be disastrous, so we must rely on the | 1372 // trigger migration. That would be disastrous, so we must rely on the |
1346 // sync manager to ensure that this type never has both progress markers | 1373 // sync manager to ensure that this type never has both progress markers |
1347 // and !initial_sync_ended. | 1374 // and !initial_sync_ended. |
1348 initialization_state_ = DOWNLOADING_NIGORI; | 1375 initialization_state_ = DOWNLOADING_METADATA; |
1349 ConfigureDataTypes( | 1376 ConfigureDataTypes( |
1350 syncer::CONFIGURE_REASON_NEW_CLIENT, | 1377 syncer::CONFIGURE_REASON_NEW_CLIENT, |
1351 syncer::ModelTypeSet(syncer::ControlTypes()), | 1378 syncer::ModelTypeSet(syncer::ControlTypes()), |
1352 syncer::ModelTypeSet(), | 1379 syncer::ModelTypeSet(), |
1353 // Calls back into this function. | 1380 // Calls back into this function. |
1354 base::Bind( | 1381 base::Bind( |
1355 &SyncBackendHost:: | 1382 &SyncBackendHost:: |
1356 HandleNigoriConfigurationCompletedOnFrontendLoop, | 1383 HandleNigoriConfigurationCompletedOnFrontendLoop, |
1357 weak_ptr_factory_.GetWeakPtr()), | 1384 weak_ptr_factory_.GetWeakPtr()), |
1358 base::Bind(&SyncBackendHost::OnNigoriDownloadRetry, | 1385 base::Bind(&SyncBackendHost::OnNigoriDownloadRetry, |
1359 weak_ptr_factory_.GetWeakPtr())); | 1386 weak_ptr_factory_.GetWeakPtr())); |
1360 break; | 1387 break; |
1361 case DOWNLOADING_NIGORI: | 1388 case DOWNLOADING_METADATA: |
1362 initialization_state_ = ASSOCIATING_NIGORI; | 1389 initialization_state_ = PROCESSING_METADATA; |
1363 // Triggers OnEncryptedTypesChanged() and OnEncryptionComplete() | 1390 // Updates encryption and other metadata. Will call |
1364 // if necessary. | 1391 // OnEncryptedTypesChanged() and OnEncryptionComplete() if necessary. |
1365 sync_thread_.message_loop()->PostTask( | 1392 InitialProcessMetadata( |
1366 FROM_HERE, | 1393 base::Bind( |
1367 base::Bind(&SyncBackendHost::Core::DoAssociateNigori, | 1394 &SyncBackendHost:: |
1368 core_.get())); | 1395 HandleInitializationCompletedOnFrontendLoop, |
1396 weak_ptr_factory_.GetWeakPtr(), true)); | |
1369 break; | 1397 break; |
1370 case ASSOCIATING_NIGORI: | 1398 case PROCESSING_METADATA: |
1371 initialization_state_ = INITIALIZED; | 1399 initialization_state_ = INITIALIZED; |
1372 // Now that we've downloaded the nigori node, we can see if there are any | 1400 // Now that we've downloaded the nigori node, we can see if there are any |
1373 // experimental types to enable. This should be done before we inform | 1401 // experimental types to enable. This should be done before we inform |
1374 // the frontend to ensure they're visible in the customize screen. | 1402 // the frontend to ensure they're visible in the customize screen. |
1375 AddExperimentalTypes(); | 1403 AddExperimentalTypes(); |
1376 frontend_->OnBackendInitialized(js_backend_, true); | 1404 frontend_->OnBackendInitialized(js_backend_, true); |
1377 js_backend_.Reset(); | 1405 js_backend_.Reset(); |
1378 break; | 1406 break; |
1379 default: | 1407 default: |
1380 NOTREACHED(); | 1408 NOTREACHED(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1542 | 1570 |
1543 frontend_->OnConnectionStatusChange(status); | 1571 frontend_->OnConnectionStatusChange(status); |
1544 } | 1572 } |
1545 | 1573 |
1546 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop( | 1574 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop( |
1547 const syncer::ModelTypeSet failed_configuration_types) { | 1575 const syncer::ModelTypeSet failed_configuration_types) { |
1548 HandleInitializationCompletedOnFrontendLoop( | 1576 HandleInitializationCompletedOnFrontendLoop( |
1549 failed_configuration_types.Empty()); | 1577 failed_configuration_types.Empty()); |
1550 } | 1578 } |
1551 | 1579 |
1580 void SyncBackendHost::InitialProcessMetadata( | |
1581 const base::Closure& done_callback) { | |
1582 DCHECK_EQ(MessageLoop::current(), frontend_loop_); | |
1583 // Then forward the request to the sync thread. | |
1584 sync_thread_.message_loop()->PostTask( | |
1585 FROM_HERE, | |
1586 base::Bind(&SyncBackendHost::Core::DoInitialProcessMetadata, | |
1587 core_.get())); | |
1588 } | |
1589 | |
1552 #undef SDVLOG | 1590 #undef SDVLOG |
1553 | 1591 |
1554 #undef SLOG | 1592 #undef SLOG |
1555 | 1593 |
1556 } // namespace browser_sync | 1594 } // namespace browser_sync |
OLD | NEW |