Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(320)

Side by Side Diff: chrome/browser/sync/glue/sync_backend_host.cc

Issue 10911073: NOT FOR COMMIT: Add DeviceInfo type and ChangeProcessor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixes from review comments Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "build/build_config.h" 23 #include "build/build_config.h"
25 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
26 #include "chrome/browser/signin/token_service.h" 25 #include "chrome/browser/signin/token_service.h"
27 #include "chrome/browser/sync/glue/bridged_invalidator.h" 26 #include "chrome/browser/sync/glue/bridged_invalidator.h"
28 #include "chrome/browser/sync/glue/change_processor.h" 27 #include "chrome/browser/sync/glue/change_processor.h"
29 #include "chrome/browser/sync/glue/chrome_encryptor.h" 28 #include "chrome/browser/sync/glue/chrome_encryptor.h"
30 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h" 29 #include "chrome/browser/sync/glue/chrome_sync_notification_bridge.h"
31 #include "chrome/browser/sync/glue/sync_backend_registrar.h" 30 #include "chrome/browser/sync/glue/sync_backend_registrar.h"
31 #include "chrome/browser/sync/glue/synced_device_tracker.h"
32 #include "chrome/browser/sync/invalidations/invalidator_storage.h" 32 #include "chrome/browser/sync/invalidations/invalidator_storage.h"
33 #include "chrome/browser/sync/sync_prefs.h" 33 #include "chrome/browser/sync/sync_prefs.h"
34 #include "chrome/common/chrome_notification_types.h" 34 #include "chrome/common/chrome_notification_types.h"
35 #include "chrome/common/chrome_switches.h" 35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/chrome_version_info.h" 36 #include "chrome/common/chrome_version_info.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 "google_apis/gaia/gaia_constants.h" 40 #include "google_apis/gaia/gaia_constants.h"
41 #include "jingle/notifier/base/notification_method.h" 41 #include "jingle/notifier/base/notification_method.h"
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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_;
Nicolas Zea 2012/09/14 19:15:08 There should probably be tests/changes in sync_bac
rlarocque 2012/09/15 01:36:37 Unfortunately, we can't test it there. Those unit
Nicolas Zea 2012/09/17 17:54:00 How about passing in the sync guid when you initia
rlarocque 2012/09/19 21:37:58 Fixed by adding an accessor to the cache_guid to U
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 831 matching lines...) Expand 10 before | Expand all | Expand 10 after
1094 #endif 1100 #endif
1095 1101
1096 sync_manager_ = options.sync_manager_factory->CreateSyncManager(name_); 1102 sync_manager_ = options.sync_manager_factory->CreateSyncManager(name_);
1097 sync_manager_->AddObserver(this); 1103 sync_manager_->AddObserver(this);
1098 sync_manager_->Init( 1104 sync_manager_->Init(
1099 sync_data_folder_path_, 1105 sync_data_folder_path_,
1100 options.event_handler, 1106 options.event_handler,
1101 options.service_url.host() + options.service_url.path(), 1107 options.service_url.host() + options.service_url.path(),
1102 options.service_url.EffectiveIntPort(), 1108 options.service_url.EffectiveIntPort(),
1103 options.service_url.SchemeIsSecure(), 1109 options.service_url.SchemeIsSecure(),
1104 BrowserThread::GetBlockingPool(),
1105 options.make_http_bridge_factory_fn.Run().Pass(), 1110 options.make_http_bridge_factory_fn.Run().Pass(),
1106 options.workers, 1111 options.workers,
1107 options.extensions_activity_monitor, 1112 options.extensions_activity_monitor,
1108 options.registrar /* as SyncManager::ChangeDelegate */, 1113 options.registrar /* as SyncManager::ChangeDelegate */,
1109 options.credentials, 1114 options.credentials,
1110 scoped_ptr<syncer::Invalidator>(new BridgedInvalidator( 1115 scoped_ptr<syncer::Invalidator>(new BridgedInvalidator(
1111 options.chrome_sync_notification_bridge, 1116 options.chrome_sync_notification_bridge,
1112 options.invalidator_factory->CreateInvalidator(), 1117 options.invalidator_factory->CreateInvalidator(),
1113 kDefaultInvalidatorState)), 1118 kDefaultInvalidatorState)),
1114 options.restored_key_for_bootstrapping, 1119 options.restored_key_for_bootstrapping,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1158 sync_manager_->UpdateRegisteredInvalidationIds(this, ids); 1163 sync_manager_->UpdateRegisteredInvalidationIds(this, ids);
1159 } 1164 }
1160 } 1165 }
1161 1166
1162 void SyncBackendHost::Core::DoStartSyncing( 1167 void SyncBackendHost::Core::DoStartSyncing(
1163 const syncer::ModelSafeRoutingInfo& routing_info) { 1168 const syncer::ModelSafeRoutingInfo& routing_info) {
1164 DCHECK_EQ(MessageLoop::current(), sync_loop_); 1169 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1165 sync_manager_->StartSyncingNormally(routing_info); 1170 sync_manager_->StartSyncingNormally(routing_info);
1166 } 1171 }
1167 1172
1168 void SyncBackendHost::Core::DoAssociateNigori() {
1169 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1170 sync_manager_->GetEncryptionHandler()->Init();
1171 host_.Call(FROM_HERE,
1172 &SyncBackendHost::HandleInitializationCompletedOnFrontendLoop,
1173 true);
1174 }
1175
1176 void SyncBackendHost::Core::DoSetEncryptionPassphrase( 1173 void SyncBackendHost::Core::DoSetEncryptionPassphrase(
1177 const std::string& passphrase, 1174 const std::string& passphrase,
1178 bool is_explicit) { 1175 bool is_explicit) {
1179 DCHECK_EQ(MessageLoop::current(), sync_loop_); 1176 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1180 sync_manager_->GetEncryptionHandler()->SetEncryptionPassphrase( 1177 sync_manager_->GetEncryptionHandler()->SetEncryptionPassphrase(
1181 passphrase, is_explicit); 1178 passphrase, is_explicit);
1182 } 1179 }
1183 1180
1181 void SyncBackendHost::Core::DoInitialProcessMetadata() {
1182 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1183
1184 // Initialize encryption.
1185 sync_manager_->GetEncryptionHandler()->Init();
1186
1187 // Initialize device info.
1188 if (sync_manager_->GetUserShare()) { // NULL in some tests.
1189 registrar_->ActivateDataType(syncer::DEVICE_INFO,
1190 syncer::GROUP_PASSIVE,
1191 &synced_device_tracker_,
1192 sync_manager_->GetUserShare());
1193 synced_device_tracker_.InitLocalDeviceInfo(
Nicolas Zea 2012/09/14 19:15:08 indent
rlarocque 2012/09/15 01:36:37 Done.
1194 base::Bind(&SyncBackendHost::Core::FinishInitialProcessMetadata,
1195 this));
1196 } else {
1197 FinishInitialProcessMetadata();
1198 }
1199 }
1200
1201 void SyncBackendHost::Core::FinishInitialProcessMetadata() {
1202 host_.Call(
1203 FROM_HERE,
1204 &SyncBackendHost::HandleInitializationCompletedOnFrontendLoop,
1205 true);
1206 }
1207
1184 void SyncBackendHost::Core::DoSetDecryptionPassphrase( 1208 void SyncBackendHost::Core::DoSetDecryptionPassphrase(
1185 const std::string& passphrase) { 1209 const std::string& passphrase) {
1186 DCHECK_EQ(MessageLoop::current(), sync_loop_); 1210 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1187 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase( 1211 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase(
1188 passphrase); 1212 passphrase);
1189 } 1213 }
1190 1214
1191 void SyncBackendHost::Core::DoEnableEncryptEverything() { 1215 void SyncBackendHost::Core::DoEnableEncryptEverything() {
1192 DCHECK_EQ(MessageLoop::current(), sync_loop_); 1216 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1193 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything(); 1217 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything();
1194 } 1218 }
1195 1219
1196 void SyncBackendHost::Core::DoStopSyncManagerForShutdown( 1220 void SyncBackendHost::Core::DoStopSyncManagerForShutdown(
1197 const base::Closure& closure) { 1221 const base::Closure& closure) {
1198 if (sync_manager_.get()) { 1222 if (sync_manager_.get()) {
1199 sync_manager_->StopSyncingForShutdown(closure); 1223 sync_manager_->StopSyncingForShutdown(closure);
1200 } else { 1224 } else {
1201 sync_loop_->PostTask(FROM_HERE, closure); 1225 sync_loop_->PostTask(FROM_HERE, closure);
1202 } 1226 }
1203 } 1227 }
1204 1228
1205 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) { 1229 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) {
1206 DCHECK_EQ(MessageLoop::current(), sync_loop_); 1230 DCHECK_EQ(MessageLoop::current(), sync_loop_);
1207 DoDestroySyncManager(); 1231 DoDestroySyncManager();
1208 1232
1233 // It's safe to do this even if the type was never activated.
1234 registrar_->DeactivateDataType(syncer::DEVICE_INFO);
1235
1209 chrome_sync_notification_bridge_ = NULL; 1236 chrome_sync_notification_bridge_ = NULL;
1210 registrar_ = NULL; 1237 registrar_ = NULL;
1211 1238
1212 if (sync_disabled) 1239 if (sync_disabled)
1213 DeleteSyncDataFolder(); 1240 DeleteSyncDataFolder();
1214 1241
1215 sync_loop_ = NULL; 1242 sync_loop_ = NULL;
1216 1243
1217 host_.Reset(); 1244 host_.Reset();
1218 } 1245 }
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1340 1367
1341 // Run initialization state machine. 1368 // Run initialization state machine.
1342 switch (initialization_state_) { 1369 switch (initialization_state_) {
1343 case NOT_INITIALIZED: 1370 case NOT_INITIALIZED:
1344 // This configuration should result in a download request if the nigori 1371 // This configuration should result in a download request if the nigori
1345 // type's initial_sync_ended bit is unset. If the download request 1372 // type's initial_sync_ended bit is unset. If the download request
1346 // contains progress markers, there is a risk that the server will try to 1373 // contains progress markers, there is a risk that the server will try to
1347 // trigger migration. That would be disastrous, so we must rely on the 1374 // trigger migration. That would be disastrous, so we must rely on the
1348 // sync manager to ensure that this type never has both progress markers 1375 // sync manager to ensure that this type never has both progress markers
1349 // and !initial_sync_ended. 1376 // and !initial_sync_ended.
1350 initialization_state_ = DOWNLOADING_NIGORI; 1377 initialization_state_ = DOWNLOADING_METADATA;
1351 ConfigureDataTypes( 1378 ConfigureDataTypes(
1352 syncer::CONFIGURE_REASON_NEW_CLIENT, 1379 syncer::CONFIGURE_REASON_NEW_CLIENT,
1353 syncer::ModelTypeSet(syncer::ControlTypes()), 1380 syncer::ModelTypeSet(syncer::ControlTypes()),
1354 syncer::ModelTypeSet(), 1381 syncer::ModelTypeSet(),
1355 // Calls back into this function. 1382 // Calls back into this function.
1356 base::Bind( 1383 base::Bind(
1357 &SyncBackendHost:: 1384 &SyncBackendHost::
1358 HandleNigoriConfigurationCompletedOnFrontendLoop, 1385 HandleNigoriConfigurationCompletedOnFrontendLoop,
1359 weak_ptr_factory_.GetWeakPtr()), 1386 weak_ptr_factory_.GetWeakPtr()),
1360 base::Bind(&SyncBackendHost::OnNigoriDownloadRetry, 1387 base::Bind(&SyncBackendHost::OnNigoriDownloadRetry,
1361 weak_ptr_factory_.GetWeakPtr())); 1388 weak_ptr_factory_.GetWeakPtr()));
1362 break; 1389 break;
1363 case DOWNLOADING_NIGORI: 1390 case DOWNLOADING_METADATA:
1364 initialization_state_ = ASSOCIATING_NIGORI; 1391 initialization_state_ = PROCESSING_METADATA;
1365 // Triggers OnEncryptedTypesChanged() and OnEncryptionComplete() 1392 // Updates encryption and other metadata. Will call
1366 // if necessary. 1393 // OnEncryptedTypesChanged() and OnEncryptionComplete() if necessary.
1367 sync_thread_.message_loop()->PostTask( 1394 InitialProcessMetadata(
1368 FROM_HERE, 1395 base::Bind(
1369 base::Bind(&SyncBackendHost::Core::DoAssociateNigori, 1396 &SyncBackendHost::
1370 core_.get())); 1397 HandleInitializationCompletedOnFrontendLoop,
1398 weak_ptr_factory_.GetWeakPtr(), true));
1371 break; 1399 break;
1372 case ASSOCIATING_NIGORI: 1400 case PROCESSING_METADATA:
1373 initialization_state_ = INITIALIZED; 1401 initialization_state_ = INITIALIZED;
1374 // Now that we've downloaded the nigori node, we can see if there are any 1402 // Now that we've downloaded the nigori node, we can see if there are any
1375 // experimental types to enable. This should be done before we inform 1403 // experimental types to enable. This should be done before we inform
1376 // the frontend to ensure they're visible in the customize screen. 1404 // the frontend to ensure they're visible in the customize screen.
1377 AddExperimentalTypes(); 1405 AddExperimentalTypes();
1378 frontend_->OnBackendInitialized(js_backend_, true); 1406 frontend_->OnBackendInitialized(js_backend_, true);
1379 js_backend_.Reset(); 1407 js_backend_.Reset();
1380 break; 1408 break;
1381 default: 1409 default:
1382 NOTREACHED(); 1410 NOTREACHED();
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
1537 1565
1538 frontend_->OnConnectionStatusChange(status); 1566 frontend_->OnConnectionStatusChange(status);
1539 } 1567 }
1540 1568
1541 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop( 1569 void SyncBackendHost::HandleNigoriConfigurationCompletedOnFrontendLoop(
1542 const syncer::ModelTypeSet failed_configuration_types) { 1570 const syncer::ModelTypeSet failed_configuration_types) {
1543 HandleInitializationCompletedOnFrontendLoop( 1571 HandleInitializationCompletedOnFrontendLoop(
1544 failed_configuration_types.Empty()); 1572 failed_configuration_types.Empty());
1545 } 1573 }
1546 1574
1575 void SyncBackendHost::InitialProcessMetadata(
1576 const base::Closure& done_callback) {
1577 DCHECK_EQ(MessageLoop::current(), frontend_loop_);
1578 // Then forward the request to the sync thread.
1579 sync_thread_.message_loop()->PostTask(
1580 FROM_HERE,
1581 base::Bind(&SyncBackendHost::Core::DoInitialProcessMetadata,
1582 core_.get()));
1583 }
1584
1547 #undef SDVLOG 1585 #undef SDVLOG
1548 1586
1549 #undef SLOG 1587 #undef SLOG
1550 1588
1551 } // namespace browser_sync 1589 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698