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

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

Issue 23189021: sync: Gracefully handle very early shutdown (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix sync_client compile Created 7 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 "chrome/browser/sync/glue/sync_backend_host.h" 5 #include "chrome/browser/sync/glue/sync_backend_host.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 // This includes refreshing encryption, setting up the device info change 191 // This includes refreshing encryption, setting up the device info change
192 // processor, etc. 192 // processor, etc.
193 void DoInitialProcessControlTypes(); 193 void DoInitialProcessControlTypes();
194 194
195 // Some parts of DoInitialProcessControlTypes() may be executed on a different 195 // Some parts of DoInitialProcessControlTypes() may be executed on a different
196 // thread. This function asynchronously continues the work started in 196 // thread. This function asynchronously continues the work started in
197 // DoInitialProcessControlTypes() once that other thread gets back to us. 197 // DoInitialProcessControlTypes() once that other thread gets back to us.
198 void DoFinishInitialProcessControlTypes(); 198 void DoFinishInitialProcessControlTypes();
199 199
200 // The shutdown order is a bit complicated: 200 // The shutdown order is a bit complicated:
201 // 1) Call DoStopSyncManagerForShutdown() from |frontend_loop_| to request 201 // 1) Call the SyncManagerStopHandle's RequestStop() from |frontend_loop_| to
202 // sync manager to stop as soon as possible. 202 // request sync manager to stop as soon as possible.
203 // 2) Post DoShutdown() to sync loop to clean up backend state, save 203 // 2) Post DoShutdown() to sync loop to clean up backend state, save
204 // directory and destroy sync manager. 204 // directory and destroy sync manager.
205 void DoStopSyncManagerForShutdown();
206 void DoShutdown(bool sync_disabled); 205 void DoShutdown(bool sync_disabled);
207 void DoDestroySyncManager(); 206 void DoDestroySyncManager();
208 207
209 // Configuration methods that must execute on sync loop. 208 // Configuration methods that must execute on sync loop.
210 void DoConfigureSyncer( 209 void DoConfigureSyncer(
211 syncer::ConfigureReason reason, 210 syncer::ConfigureReason reason,
212 const DoConfigureSyncerTypes& config_types, 211 const DoConfigureSyncerTypes& config_types,
213 const syncer::ModelSafeRoutingInfo routing_info, 212 const syncer::ModelSafeRoutingInfo routing_info,
214 const base::Callback<void(syncer::ModelTypeSet, 213 const base::Callback<void(syncer::ModelTypeSet,
215 syncer::ModelTypeSet)>& ready_task, 214 syncer::ModelTypeSet)>& ready_task,
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 credentials, 393 credentials,
395 invalidator_->GetInvalidatorClientId(), 394 invalidator_->GetInvalidatorClientId(),
396 sync_manager_factory.Pass(), 395 sync_manager_factory.Pass(),
397 delete_sync_data_folder, 396 delete_sync_data_folder,
398 sync_prefs_->GetEncryptionBootstrapToken(), 397 sync_prefs_->GetEncryptionBootstrapToken(),
399 sync_prefs_->GetKeystoreEncryptionBootstrapToken(), 398 sync_prefs_->GetKeystoreEncryptionBootstrapToken(),
400 scoped_ptr<InternalComponentsFactory>( 399 scoped_ptr<InternalComponentsFactory>(
401 new InternalComponentsFactoryImpl(factory_switches)).Pass(), 400 new InternalComponentsFactoryImpl(factory_switches)).Pass(),
402 unrecoverable_error_handler.Pass(), 401 unrecoverable_error_handler.Pass(),
403 report_unrecoverable_error_function, 402 report_unrecoverable_error_function,
404 !cl->HasSwitch(switches::kSyncDisableOAuth2Token))); 403 !cl->HasSwitch(switches::kSyncDisableOAuth2Token),
404 &cancellation_signal_));
405 InitCore(init_opts.Pass()); 405 InitCore(init_opts.Pass());
406 } 406 }
407 407
408 void SyncBackendHost::UpdateCredentials(const SyncCredentials& credentials) { 408 void SyncBackendHost::UpdateCredentials(const SyncCredentials& credentials) {
409 DCHECK(registrar_->sync_thread()->IsRunning()); 409 DCHECK(registrar_->sync_thread()->IsRunning());
410 registrar_->sync_thread()->message_loop()->PostTask(FROM_HERE, 410 registrar_->sync_thread()->message_loop()->PostTask(FROM_HERE,
411 base::Bind(&SyncBackendHost::Core::DoUpdateCredentials, 411 base::Bind(&SyncBackendHost::Core::DoUpdateCredentials,
412 core_.get(), 412 core_.get(),
413 credentials)); 413 credentials));
414 } 414 }
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 // clicks OK, immediately reopens the advanced settings dialog, and gets an 485 // clicks OK, immediately reopens the advanced settings dialog, and gets an
486 // unnecessary prompt for a passphrase. 486 // unnecessary prompt for a passphrase.
487 // Note: It is not guaranteed that the passphrase will be accepted by the 487 // Note: It is not guaranteed that the passphrase will be accepted by the
488 // syncer thread, since we could receive a new nigori node while the task is 488 // syncer thread, since we could receive a new nigori node while the task is
489 // pending. This scenario is a valid race, and SetDecryptionPassphrase can 489 // pending. This scenario is a valid race, and SetDecryptionPassphrase can
490 // trigger a new OnPassphraseRequired if it needs to. 490 // trigger a new OnPassphraseRequired if it needs to.
491 NotifyPassphraseAccepted(); 491 NotifyPassphraseAccepted();
492 return true; 492 return true;
493 } 493 }
494 494
495 void SyncBackendHost::StopSyncManagerForShutdown() {
496 DCHECK_GT(initialization_state_, NOT_ATTEMPTED);
497 if (initialization_state_ == CREATING_SYNC_MANAGER) {
498 // We post here to implicitly wait for the SyncManager to be created,
499 // if needed. We have to wait, since we need to shutdown immediately,
500 // and we need to tell the SyncManager so it can abort any activity
501 // (net I/O, data application).
502 DCHECK(registrar_->sync_thread()->IsRunning());
503 registrar_->sync_thread()->message_loop()->PostTask(FROM_HERE,
504 base::Bind(&SyncBackendHost::Core::DoStopSyncManagerForShutdown,
505 core_.get()));
506 } else {
507 core_->DoStopSyncManagerForShutdown();
508 }
509 }
510
511 void SyncBackendHost::StopSyncingForShutdown() { 495 void SyncBackendHost::StopSyncingForShutdown() {
512 DCHECK_EQ(base::MessageLoop::current(), frontend_loop_); 496 DCHECK_EQ(base::MessageLoop::current(), frontend_loop_);
497 DCHECK_GT(initialization_state_, NOT_ATTEMPTED);
513 498
514 // Immediately stop sending messages to the frontend. 499 // Immediately stop sending messages to the frontend.
515 frontend_ = NULL; 500 frontend_ = NULL;
516 501
517 // Stop listening for and forwarding locally-triggered sync refresh requests. 502 // Stop listening for and forwarding locally-triggered sync refresh requests.
518 notification_registrar_.RemoveAll(); 503 notification_registrar_.RemoveAll();
519 504
520 DCHECK(registrar_->sync_thread()->IsRunning()); 505 DCHECK(registrar_->sync_thread()->IsRunning());
521 506
522 registrar_->RequestWorkerStopOnUIThread(); 507 registrar_->RequestWorkerStopOnUIThread();
523 508
524 StopSyncManagerForShutdown(); 509 cancellation_signal_.RequestStop();
525 } 510 }
526 511
527 scoped_ptr<base::Thread> SyncBackendHost::Shutdown(ShutdownOption option) { 512 scoped_ptr<base::Thread> SyncBackendHost::Shutdown(ShutdownOption option) {
528 // StopSyncingForShutdown() (which nulls out |frontend_|) should be 513 // StopSyncingForShutdown() (which nulls out |frontend_|) should be
529 // called first. 514 // called first.
530 DCHECK(!frontend_); 515 DCHECK(!frontend_);
531 DCHECK(registrar_->sync_thread()->IsRunning()); 516 DCHECK(registrar_->sync_thread()->IsRunning());
532 517
533 bool sync_disabled = (option == DISABLE_AND_CLAIM_THREAD); 518 bool sync_disabled = (option == DISABLE_AND_CLAIM_THREAD);
534 bool sync_thread_claimed = 519 bool sync_thread_claimed =
(...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after
868 const syncer::SyncCredentials& credentials, 853 const syncer::SyncCredentials& credentials,
869 const std::string& invalidator_client_id, 854 const std::string& invalidator_client_id,
870 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory, 855 scoped_ptr<syncer::SyncManagerFactory> sync_manager_factory,
871 bool delete_sync_data_folder, 856 bool delete_sync_data_folder,
872 const std::string& restored_key_for_bootstrapping, 857 const std::string& restored_key_for_bootstrapping,
873 const std::string& restored_keystore_key_for_bootstrapping, 858 const std::string& restored_keystore_key_for_bootstrapping,
874 scoped_ptr<InternalComponentsFactory> internal_components_factory, 859 scoped_ptr<InternalComponentsFactory> internal_components_factory,
875 scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler, 860 scoped_ptr<syncer::UnrecoverableErrorHandler> unrecoverable_error_handler,
876 syncer::ReportUnrecoverableErrorFunction 861 syncer::ReportUnrecoverableErrorFunction
877 report_unrecoverable_error_function, 862 report_unrecoverable_error_function,
878 bool use_oauth2_token) 863 bool use_oauth2_token,
864 syncer::CancellationSignal* cancellation_signal)
879 : sync_loop(sync_loop), 865 : sync_loop(sync_loop),
880 registrar(registrar), 866 registrar(registrar),
881 routing_info(routing_info), 867 routing_info(routing_info),
882 workers(workers), 868 workers(workers),
883 extensions_activity(extensions_activity), 869 extensions_activity(extensions_activity),
884 event_handler(event_handler), 870 event_handler(event_handler),
885 service_url(service_url), 871 service_url(service_url),
886 make_http_bridge_factory_fn(make_http_bridge_factory_fn), 872 make_http_bridge_factory_fn(make_http_bridge_factory_fn),
887 credentials(credentials), 873 credentials(credentials),
888 invalidator_client_id(invalidator_client_id), 874 invalidator_client_id(invalidator_client_id),
889 sync_manager_factory(sync_manager_factory.Pass()), 875 sync_manager_factory(sync_manager_factory.Pass()),
890 delete_sync_data_folder(delete_sync_data_folder), 876 delete_sync_data_folder(delete_sync_data_folder),
891 restored_key_for_bootstrapping(restored_key_for_bootstrapping), 877 restored_key_for_bootstrapping(restored_key_for_bootstrapping),
892 restored_keystore_key_for_bootstrapping( 878 restored_keystore_key_for_bootstrapping(
893 restored_keystore_key_for_bootstrapping), 879 restored_keystore_key_for_bootstrapping),
894 internal_components_factory(internal_components_factory.Pass()), 880 internal_components_factory(internal_components_factory.Pass()),
895 unrecoverable_error_handler(unrecoverable_error_handler.Pass()), 881 unrecoverable_error_handler(unrecoverable_error_handler.Pass()),
896 report_unrecoverable_error_function( 882 report_unrecoverable_error_function(
897 report_unrecoverable_error_function), 883 report_unrecoverable_error_function),
898 use_oauth2_token(use_oauth2_token) { 884 use_oauth2_token(use_oauth2_token),
885 cancellation_signal(cancellation_signal) {
899 } 886 }
900 887
901 SyncBackendHost::DoInitializeOptions::~DoInitializeOptions() {} 888 SyncBackendHost::DoInitializeOptions::~DoInitializeOptions() {}
902 889
903 SyncBackendHost::Core::Core(const std::string& name, 890 SyncBackendHost::Core::Core(const std::string& name,
904 const base::FilePath& sync_data_folder_path, 891 const base::FilePath& sync_data_folder_path,
905 const base::WeakPtr<SyncBackendHost>& backend) 892 const base::WeakPtr<SyncBackendHost>& backend)
906 : name_(name), 893 : name_(name),
907 sync_data_folder_path_(sync_data_folder_path), 894 sync_data_folder_path_(sync_data_folder_path),
908 host_(backend), 895 host_(backend),
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after
1153 options->extensions_activity, 1140 options->extensions_activity,
1154 options->registrar /* as SyncManager::ChangeDelegate */, 1141 options->registrar /* as SyncManager::ChangeDelegate */,
1155 options->credentials, 1142 options->credentials,
1156 options->invalidator_client_id, 1143 options->invalidator_client_id,
1157 options->restored_key_for_bootstrapping, 1144 options->restored_key_for_bootstrapping,
1158 options->restored_keystore_key_for_bootstrapping, 1145 options->restored_keystore_key_for_bootstrapping,
1159 options->internal_components_factory.get(), 1146 options->internal_components_factory.get(),
1160 &encryptor_, 1147 &encryptor_,
1161 options->unrecoverable_error_handler.Pass(), 1148 options->unrecoverable_error_handler.Pass(),
1162 options->report_unrecoverable_error_function, 1149 options->report_unrecoverable_error_function,
1163 options->use_oauth2_token); 1150 options->use_oauth2_token,
1151 options->cancellation_signal);
1164 1152
1165 // |sync_manager_| may end up being NULL here in tests (in 1153 // |sync_manager_| may end up being NULL here in tests (in
1166 // synchronous initialization mode). 1154 // synchronous initialization mode).
1167 // 1155 //
1168 // TODO(akalin): Fix this behavior (see http://crbug.com/140354). 1156 // TODO(akalin): Fix this behavior (see http://crbug.com/140354).
1169 if (sync_manager_) { 1157 if (sync_manager_) {
1170 // Now check the command line to see if we need to simulate an 1158 // Now check the command line to see if we need to simulate an
1171 // unrecoverable error for testing purpose. Note the error is thrown 1159 // unrecoverable error for testing purpose. Note the error is thrown
1172 // only if the initialization succeeded. Also it makes sense to use this 1160 // only if the initialization succeeded. Also it makes sense to use this
1173 // flag only when restarting the browser with an account already setup. If 1161 // flag only when restarting the browser with an account already setup. If
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
1263 DCHECK_EQ(base::MessageLoop::current(), sync_loop_); 1251 DCHECK_EQ(base::MessageLoop::current(), sync_loop_);
1264 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase( 1252 sync_manager_->GetEncryptionHandler()->SetDecryptionPassphrase(
1265 passphrase); 1253 passphrase);
1266 } 1254 }
1267 1255
1268 void SyncBackendHost::Core::DoEnableEncryptEverything() { 1256 void SyncBackendHost::Core::DoEnableEncryptEverything() {
1269 DCHECK_EQ(base::MessageLoop::current(), sync_loop_); 1257 DCHECK_EQ(base::MessageLoop::current(), sync_loop_);
1270 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything(); 1258 sync_manager_->GetEncryptionHandler()->EnableEncryptEverything();
1271 } 1259 }
1272 1260
1273 void SyncBackendHost::Core::DoStopSyncManagerForShutdown() {
1274 if (sync_manager_)
1275 sync_manager_->StopSyncingForShutdown();
1276 }
1277
1278 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) { 1261 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) {
1279 DCHECK_EQ(base::MessageLoop::current(), sync_loop_); 1262 DCHECK_EQ(base::MessageLoop::current(), sync_loop_);
1280 // It's safe to do this even if the type was never activated. 1263 // It's safe to do this even if the type was never activated.
1281 registrar_->DeactivateDataType(syncer::DEVICE_INFO); 1264 registrar_->DeactivateDataType(syncer::DEVICE_INFO);
1282 synced_device_tracker_.reset(); 1265 synced_device_tracker_.reset();
1283 1266
1284 DoDestroySyncManager(); 1267 DoDestroySyncManager();
1285 1268
1286 registrar_ = NULL; 1269 registrar_ = NULL;
1287 1270
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after
1597 1580
1598 base::MessageLoop* SyncBackendHost::GetSyncLoopForTesting() { 1581 base::MessageLoop* SyncBackendHost::GetSyncLoopForTesting() {
1599 return registrar_->sync_thread()->message_loop(); 1582 return registrar_->sync_thread()->message_loop();
1600 } 1583 }
1601 1584
1602 #undef SDVLOG 1585 #undef SDVLOG
1603 1586
1604 #undef SLOG 1587 #undef SLOG
1605 1588
1606 } // namespace browser_sync 1589 } // namespace browser_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698