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

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

Issue 7926001: [Sync] Move change-related methods out of SyncManager::Observer (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 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) 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 "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>
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 syncable::ModelTypeSet initial_types_with_nigori(initial_types); 108 syncable::ModelTypeSet initial_types_with_nigori(initial_types);
109 if (profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted)) 109 if (profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted))
110 initial_types_with_nigori.insert(syncable::NIGORI); 110 initial_types_with_nigori.insert(syncable::NIGORI);
111 111
112 registrar_.reset(new SyncBackendRegistrar(initial_types_with_nigori, 112 registrar_.reset(new SyncBackendRegistrar(initial_types_with_nigori,
113 name_, 113 name_,
114 profile_, 114 profile_,
115 sync_thread_.message_loop())); 115 sync_thread_.message_loop()));
116 116
117 InitCore(Core::DoInitializeOptions( 117 InitCore(Core::DoInitializeOptions(
118 sync_thread_.message_loop(),
118 registrar_.get(), 119 registrar_.get(),
119 event_handler, 120 event_handler,
120 sync_service_url, 121 sync_service_url,
121 profile_->GetRequestContext(), 122 profile_->GetRequestContext(),
122 credentials, 123 credentials,
123 delete_sync_data_folder, 124 delete_sync_data_folder,
124 RestoreEncryptionBootstrapToken(), 125 RestoreEncryptionBootstrapToken(),
125 false)); 126 false));
126 } 127 }
127 128
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 string16 SyncBackendHost::GetAuthenticatedUsername() const { 319 string16 SyncBackendHost::GetAuthenticatedUsername() const {
319 DCHECK(initialized()); 320 DCHECK(initialized());
320 return UTF8ToUTF16(core_->sync_manager()->GetAuthenticatedUsername()); 321 return UTF8ToUTF16(core_->sync_manager()->GetAuthenticatedUsername());
321 } 322 }
322 323
323 bool SyncBackendHost::HasUnsyncedItems() const { 324 bool SyncBackendHost::HasUnsyncedItems() const {
324 DCHECK(initialized()); 325 DCHECK(initialized());
325 return core_->sync_manager()->HasUnsyncedItems(); 326 return core_->sync_manager()->HasUnsyncedItems();
326 } 327 }
327 328
328 void SyncBackendHost::LogUnsyncedItems(int level) const {
329 DCHECK(initialized());
330 return core_->sync_manager()->LogUnsyncedItems(level);
331 }
332
333 bool SyncBackendHost::IsNigoriEnabled() const { 329 bool SyncBackendHost::IsNigoriEnabled() const {
334 return registrar_.get() && registrar_->IsNigoriEnabled(); 330 return registrar_.get() && registrar_->IsNigoriEnabled();
335 } 331 }
336 332
337 bool SyncBackendHost::IsUsingExplicitPassphrase() { 333 bool SyncBackendHost::IsUsingExplicitPassphrase() {
338 return initialized() && IsNigoriEnabled() && 334 return initialized() && IsNigoriEnabled() &&
339 core_->sync_manager()->InitialSyncEndedForAllEnabledTypes() && 335 core_->sync_manager()->InitialSyncEndedForAllEnabledTypes() &&
340 core_->sync_manager()->IsUsingExplicitPassphrase(); 336 core_->sync_manager()->IsUsingExplicitPassphrase();
341 } 337 }
342 338
343 bool SyncBackendHost::IsCryptographerReady( 339 bool SyncBackendHost::IsCryptographerReady(
344 const sync_api::BaseTransaction* trans) const { 340 const sync_api::BaseTransaction* trans) const {
345 return initialized() && trans->GetCryptographer()->is_ready(); 341 return initialized() && trans->GetCryptographer()->is_ready();
346 } 342 }
347 343
348 void SyncBackendHost::GetModelSafeRoutingInfo( 344 void SyncBackendHost::GetModelSafeRoutingInfo(
349 ModelSafeRoutingInfo* out) const { 345 ModelSafeRoutingInfo* out) const {
350 if (initialized()) { 346 if (initialized()) {
351 CHECK(registrar_.get()); 347 CHECK(registrar_.get());
352 registrar_->GetModelSafeRoutingInfo(out); 348 registrar_->GetModelSafeRoutingInfo(out);
353 } else { 349 } else {
354 NOTREACHED(); 350 NOTREACHED();
355 } 351 }
356 } 352 }
357 353
358 SyncBackendHost::Core::Core(const std::string& name, 354 SyncBackendHost::Core::Core(const std::string& name,
359 SyncBackendHost* backend) 355 SyncBackendHost* backend)
360 : name_(name), 356 : name_(name),
361 host_(backend), 357 host_(backend),
358 sync_loop_(NULL),
362 registrar_(NULL) { 359 registrar_(NULL) {
363 DCHECK(host_); 360 DCHECK(host_);
364 } 361 }
365 362
366 SyncBackendHost::Core::~Core() { 363 SyncBackendHost::Core::~Core() {
367 DCHECK(!sync_manager_.get()); 364 DCHECK(!sync_manager_.get());
368 } 365 DCHECK(!sync_loop_);
369
370 void SyncBackendHost::Core::OnChangesApplied(
371 syncable::ModelType model_type,
372 const sync_api::BaseTransaction* trans,
373 const sync_api::ImmutableChangeRecordList& changes) {
374 if (!host_ || !host_->frontend_) {
375 DCHECK(false) << "OnChangesApplied called after Shutdown?";
376 return;
377 }
378 ChangeProcessor* processor = registrar_->GetProcessor(model_type);
379 if (!processor)
380 return;
381
382 processor->ApplyChangesFromSyncModel(trans, changes);
383 }
384
385 void SyncBackendHost::Core::OnChangesComplete(
386 syncable::ModelType model_type) {
387 if (!host_ || !host_->frontend_) {
388 DCHECK(false) << "OnChangesComplete called after Shutdown?";
389 return;
390 }
391
392 ChangeProcessor* processor = registrar_->GetProcessor(model_type);
393 if (!processor)
394 return;
395
396 // This call just notifies the processor that it can commit, it already
397 // buffered any changes it plans to makes so needs no further information.
398 processor->CommitChangesFromSyncModel();
399 } 366 }
400 367
401 void SyncBackendHost::Core::OnSyncCycleCompleted( 368 void SyncBackendHost::Core::OnSyncCycleCompleted(
402 const SyncSessionSnapshot* snapshot) { 369 const SyncSessionSnapshot* snapshot) {
370 if (!sync_loop_)
371 return;
372 DCHECK_EQ(MessageLoop::current(), sync_loop_);
403 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 373 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
404 &Core::HandleSyncCycleCompletedOnFrontendLoop, 374 &Core::HandleSyncCycleCompletedOnFrontendLoop,
405 new SyncSessionSnapshot(*snapshot))); 375 new SyncSessionSnapshot(*snapshot)));
406 } 376 }
407 377
408 378
409 void SyncBackendHost::Core::OnInitializationComplete( 379 void SyncBackendHost::Core::OnInitializationComplete(
410 const WeakHandle<JsBackend>& js_backend, 380 const WeakHandle<JsBackend>& js_backend,
411 bool success) { 381 bool success) {
412 if (!host_ || !host_->frontend_) 382 if (!sync_loop_)
413 return; // We may have been told to Shutdown before initialization 383 return; // We may have been told to Shutdown before initialization
414 // completed. 384 // completed.
385 DCHECK_EQ(MessageLoop::current(), sync_loop_);
415 386
416 // We could be on some random sync backend thread, so MessageLoop::current()
417 // can definitely be null in here.
418 host_->frontend_loop_->PostTask(FROM_HERE, 387 host_->frontend_loop_->PostTask(FROM_HERE,
419 NewRunnableMethod(this, 388 NewRunnableMethod(this,
420 &Core::HandleInitializationCompletedOnFrontendLoop, 389 &Core::HandleInitializationCompletedOnFrontendLoop,
421 js_backend, success)); 390 js_backend, success));
422 391
423 // Initialization is complete, so we can schedule recurring SaveChanges. 392 // Initialization is complete, so we can schedule recurring SaveChanges.
424 host_->sync_thread_.message_loop()->PostTask(FROM_HERE, 393 sync_loop_->PostTask(FROM_HERE,
425 NewRunnableMethod(this, &Core::StartSavingChanges)); 394 NewRunnableMethod(this, &Core::StartSavingChanges));
426 } 395 }
427 396
428 void SyncBackendHost::Core::OnAuthError(const AuthError& auth_error) { 397 void SyncBackendHost::Core::OnAuthError(const AuthError& auth_error) {
398 if (!sync_loop_)
399 return;
400 DCHECK_EQ(MessageLoop::current(), sync_loop_);
429 // Post to our core loop so we can modify state. Could be on another thread. 401 // Post to our core loop so we can modify state. Could be on another thread.
430 host_->frontend_loop_->PostTask(FROM_HERE, 402 host_->frontend_loop_->PostTask(FROM_HERE,
431 NewRunnableMethod(this, &Core::HandleAuthErrorEventOnFrontendLoop, 403 NewRunnableMethod(this, &Core::HandleAuthErrorEventOnFrontendLoop,
432 auth_error)); 404 auth_error));
433 } 405 }
434 406
435 void SyncBackendHost::Core::OnPassphraseRequired( 407 void SyncBackendHost::Core::OnPassphraseRequired(
436 sync_api::PassphraseRequiredReason reason) { 408 sync_api::PassphraseRequiredReason reason) {
409 if (!sync_loop_)
410 return;
411 DCHECK_EQ(MessageLoop::current(), sync_loop_);
437 host_->frontend_loop_->PostTask(FROM_HERE, 412 host_->frontend_loop_->PostTask(FROM_HERE,
438 NewRunnableMethod(this, &Core::NotifyPassphraseRequired, reason)); 413 NewRunnableMethod(this, &Core::NotifyPassphraseRequired, reason));
439 } 414 }
440 415
441 void SyncBackendHost::Core::OnPassphraseAccepted( 416 void SyncBackendHost::Core::OnPassphraseAccepted(
442 const std::string& bootstrap_token) { 417 const std::string& bootstrap_token) {
418 if (!sync_loop_)
419 return;
420 DCHECK_EQ(MessageLoop::current(), sync_loop_);
443 host_->frontend_loop_->PostTask(FROM_HERE, 421 host_->frontend_loop_->PostTask(FROM_HERE,
444 NewRunnableMethod(this, &Core::NotifyPassphraseAccepted, 422 NewRunnableMethod(this, &Core::NotifyPassphraseAccepted,
445 bootstrap_token)); 423 bootstrap_token));
446 } 424 }
447 425
448 void SyncBackendHost::Core::OnStopSyncingPermanently() { 426 void SyncBackendHost::Core::OnStopSyncingPermanently() {
427 if (!sync_loop_)
428 return;
429 DCHECK_EQ(MessageLoop::current(), sync_loop_);
449 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 430 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
450 &Core::HandleStopSyncingPermanentlyOnFrontendLoop)); 431 &Core::HandleStopSyncingPermanentlyOnFrontendLoop));
451 } 432 }
452 433
453 void SyncBackendHost::Core::OnUpdatedToken(const std::string& token) { 434 void SyncBackendHost::Core::OnUpdatedToken(const std::string& token) {
435 if (!sync_loop_)
436 return;
437 DCHECK_EQ(MessageLoop::current(), sync_loop_);
454 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 438 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
455 &Core::NotifyUpdatedToken, token)); 439 &Core::NotifyUpdatedToken, token));
456 } 440 }
457 441
458 void SyncBackendHost::Core::OnClearServerDataFailed() { 442 void SyncBackendHost::Core::OnClearServerDataFailed() {
443 if (!sync_loop_)
444 return;
445 DCHECK_EQ(MessageLoop::current(), sync_loop_);
459 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 446 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
460 &Core::HandleClearServerDataFailedOnFrontendLoop)); 447 &Core::HandleClearServerDataFailedOnFrontendLoop));
461 } 448 }
462 449
463 void SyncBackendHost::Core::OnClearServerDataSucceeded() { 450 void SyncBackendHost::Core::OnClearServerDataSucceeded() {
451 if (!sync_loop_)
452 return;
453 DCHECK_EQ(MessageLoop::current(), sync_loop_);
464 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 454 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
465 &Core::HandleClearServerDataSucceededOnFrontendLoop)); 455 &Core::HandleClearServerDataSucceededOnFrontendLoop));
466 } 456 }
467 457
468 void SyncBackendHost::Core::OnEncryptionComplete( 458 void SyncBackendHost::Core::OnEncryptionComplete(
469 const syncable::ModelTypeSet& encrypted_types) { 459 const syncable::ModelTypeSet& encrypted_types) {
460 if (!sync_loop_)
461 return;
462 DCHECK_EQ(MessageLoop::current(), sync_loop_);
470 host_->frontend_loop_->PostTask( 463 host_->frontend_loop_->PostTask(
471 FROM_HERE, 464 FROM_HERE,
472 NewRunnableMethod(this, &Core::NotifyEncryptionComplete, 465 NewRunnableMethod(this, &Core::NotifyEncryptionComplete,
473 encrypted_types)); 466 encrypted_types));
474 } 467 }
475 468
476 void SyncBackendHost::Core::OnActionableError( 469 void SyncBackendHost::Core::OnActionableError(
477 const browser_sync::SyncProtocolError& sync_error) { 470 const browser_sync::SyncProtocolError& sync_error) {
478 if (!host_ || !host_->frontend_) 471 if (!sync_loop_)
479 return; 472 return;
473 DCHECK_EQ(MessageLoop::current(), sync_loop_);
480 host_->frontend_loop_->PostTask( 474 host_->frontend_loop_->PostTask(
481 FROM_HERE, 475 FROM_HERE,
482 NewRunnableMethod(this, &Core::HandleActionableErrorEventOnFrontendLoop, 476 NewRunnableMethod(this, &Core::HandleActionableErrorEventOnFrontendLoop,
483 sync_error)); 477 sync_error));
484 } 478 }
485 479
486 SyncBackendHost::Core::DoInitializeOptions::DoInitializeOptions( 480 SyncBackendHost::Core::DoInitializeOptions::DoInitializeOptions(
481 MessageLoop* sync_loop,
487 SyncBackendRegistrar* registrar, 482 SyncBackendRegistrar* registrar,
488 const WeakHandle<JsEventHandler>& event_handler, 483 const WeakHandle<JsEventHandler>& event_handler,
489 const GURL& service_url, 484 const GURL& service_url,
490 const scoped_refptr<net::URLRequestContextGetter>& 485 const scoped_refptr<net::URLRequestContextGetter>&
491 request_context_getter, 486 request_context_getter,
492 const sync_api::SyncCredentials& credentials, 487 const sync_api::SyncCredentials& credentials,
493 bool delete_sync_data_folder, 488 bool delete_sync_data_folder,
494 const std::string& restored_key_for_bootstrapping, 489 const std::string& restored_key_for_bootstrapping,
495 bool setup_for_test_mode) 490 bool setup_for_test_mode)
496 : registrar(registrar), 491 : sync_loop(sync_loop),
492 registrar(registrar),
497 event_handler(event_handler), 493 event_handler(event_handler),
498 service_url(service_url), 494 service_url(service_url),
499 request_context_getter(request_context_getter), 495 request_context_getter(request_context_getter),
500 credentials(credentials), 496 credentials(credentials),
501 delete_sync_data_folder(delete_sync_data_folder), 497 delete_sync_data_folder(delete_sync_data_folder),
502 restored_key_for_bootstrapping(restored_key_for_bootstrapping), 498 restored_key_for_bootstrapping(restored_key_for_bootstrapping),
503 setup_for_test_mode(setup_for_test_mode) { 499 setup_for_test_mode(setup_for_test_mode) {
504 } 500 }
505 501
506 SyncBackendHost::Core::DoInitializeOptions::~DoInitializeOptions() {} 502 SyncBackendHost::Core::DoInitializeOptions::~DoInitializeOptions() {}
(...skipping 22 matching lines...) Expand all
529 } 525 }
530 526
531 user_agent += version_info.Version(); 527 user_agent += version_info.Version();
532 user_agent += " (" + version_info.LastChange() + ")"; 528 user_agent += " (" + version_info.LastChange() + ")";
533 if (!version_info.IsOfficialBuild()) 529 if (!version_info.IsOfficialBuild())
534 user_agent += "-devel"; 530 user_agent += "-devel";
535 return user_agent; 531 return user_agent;
536 } 532 }
537 533
538 void SyncBackendHost::Core::DoInitialize(const DoInitializeOptions& options) { 534 void SyncBackendHost::Core::DoInitialize(const DoInitializeOptions& options) {
539 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 535 DCHECK(!sync_loop_);
536 sync_loop_ = options.sync_loop;
537 DCHECK(sync_loop_);
540 538
541 // Blow away the partial or corrupt sync data folder before doing any more 539 // Blow away the partial or corrupt sync data folder before doing any more
542 // initialization, if necessary. 540 // initialization, if necessary.
543 if (options.delete_sync_data_folder) { 541 if (options.delete_sync_data_folder) {
544 DeleteSyncDataFolder(); 542 DeleteSyncDataFolder();
545 } 543 }
546 544
547 // Make sure that the directory exists before initializing the backend. 545 // Make sure that the directory exists before initializing the backend.
548 // If it already exists, this will do no harm. 546 // If it already exists, this will do no harm.
549 bool success = file_util::CreateDirectory(host_->sync_data_folder_path()); 547 bool success = file_util::CreateDirectory(host_->sync_data_folder_path());
550 DCHECK(success); 548 DCHECK(success);
551 549
552 DCHECK(!registrar_); 550 DCHECK(!registrar_);
553 registrar_ = options.registrar; 551 registrar_ = options.registrar;
554 DCHECK(registrar_); 552 DCHECK(registrar_);
555 553
556 sync_manager_.reset(new sync_api::SyncManager(name_)); 554 sync_manager_.reset(new sync_api::SyncManager(name_));
557 sync_manager_->AddObserver(this); 555 sync_manager_->AddObserver(this);
558 const FilePath& path_str = host_->sync_data_folder_path(); 556 const FilePath& path_str = host_->sync_data_folder_path();
559 success = sync_manager_->Init( 557 success = sync_manager_->Init(
560 path_str, 558 path_str,
561 options.event_handler, 559 options.event_handler,
562 options.service_url.host() + options.service_url.path(), 560 options.service_url.host() + options.service_url.path(),
563 options.service_url.EffectiveIntPort(), 561 options.service_url.EffectiveIntPort(),
564 options.service_url.SchemeIsSecure(), 562 options.service_url.SchemeIsSecure(),
565 host_->MakeHttpBridgeFactory(options.request_context_getter), 563 host_->MakeHttpBridgeFactory(options.request_context_getter),
566 options.registrar, 564 options.registrar /* as ModelSafeWorkerRegistrar */,
565 options.registrar /* as SyncManager::ChangeDelegate */,
567 MakeUserAgentForSyncApi(), 566 MakeUserAgentForSyncApi(),
568 options.credentials, 567 options.credentials,
569 host_->sync_notifier_factory_.CreateSyncNotifier(), 568 host_->sync_notifier_factory_.CreateSyncNotifier(),
570 options.restored_key_for_bootstrapping, 569 options.restored_key_for_bootstrapping,
571 options.setup_for_test_mode); 570 options.setup_for_test_mode);
572 DCHECK(success) << "Syncapi initialization failed!"; 571 DCHECK(success) << "Syncapi initialization failed!";
573 } 572 }
574 573
575 void SyncBackendHost::Core::DoUpdateCredentials( 574 void SyncBackendHost::Core::DoUpdateCredentials(
576 const SyncCredentials& credentials) { 575 const SyncCredentials& credentials) {
577 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 576 DCHECK_EQ(MessageLoop::current(), sync_loop_);
578 sync_manager_->UpdateCredentials(credentials); 577 sync_manager_->UpdateCredentials(credentials);
579 } 578 }
580 579
581 void SyncBackendHost::Core::DoUpdateEnabledTypes() { 580 void SyncBackendHost::Core::DoUpdateEnabledTypes() {
582 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 581 DCHECK_EQ(MessageLoop::current(), sync_loop_);
583 sync_manager_->UpdateEnabledTypes(); 582 sync_manager_->UpdateEnabledTypes();
584 } 583 }
585 584
586 void SyncBackendHost::Core::DoStartSyncing() { 585 void SyncBackendHost::Core::DoStartSyncing() {
587 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 586 DCHECK_EQ(MessageLoop::current(), sync_loop_);
588 sync_manager_->StartSyncingNormally(); 587 sync_manager_->StartSyncingNormally();
589 } 588 }
590 589
591 void SyncBackendHost::Core::DoRequestClearServerData() { 590 void SyncBackendHost::Core::DoRequestClearServerData() {
591 DCHECK_EQ(MessageLoop::current(), sync_loop_);
592 sync_manager_->RequestClearServerData(); 592 sync_manager_->RequestClearServerData();
593 } 593 }
594 594
595 void SyncBackendHost::Core::DoRequestCleanupDisabledTypes() { 595 void SyncBackendHost::Core::DoRequestCleanupDisabledTypes() {
596 DCHECK_EQ(MessageLoop::current(), sync_loop_);
596 sync_manager_->RequestCleanupDisabledTypes(); 597 sync_manager_->RequestCleanupDisabledTypes();
597 } 598 }
598 599
599 void SyncBackendHost::Core::DoSetPassphrase(const std::string& passphrase, 600 void SyncBackendHost::Core::DoSetPassphrase(const std::string& passphrase,
600 bool is_explicit) { 601 bool is_explicit) {
601 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 602 DCHECK_EQ(MessageLoop::current(), sync_loop_);
602 sync_manager_->SetPassphrase(passphrase, is_explicit); 603 sync_manager_->SetPassphrase(passphrase, is_explicit);
603 } 604 }
604 605
605 void SyncBackendHost::Core::DoEnableEncryptEverything() { 606 void SyncBackendHost::Core::DoEnableEncryptEverything() {
606 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 607 DCHECK_EQ(MessageLoop::current(), sync_loop_);
607 sync_manager_->EnableEncryptEverything(); 608 sync_manager_->EnableEncryptEverything();
608 } 609 }
609 610
611 void SyncBackendHost::Core::DoRefreshEncryption(
612 const base::Closure& done_callback) {
613 DCHECK_EQ(MessageLoop::current(), sync_loop_);
614 sync_manager_->RefreshEncryption();
615 done_callback.Run();
616 }
617
610 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) { 618 void SyncBackendHost::Core::DoShutdown(bool sync_disabled) {
611 DCHECK(MessageLoop::current() == host_->sync_thread_.message_loop()); 619 DCHECK_EQ(MessageLoop::current(), sync_loop_);
612 620
613 save_changes_timer_.Stop(); 621 save_changes_timer_.Stop();
614 sync_manager_->Shutdown(); // Stops the SyncerThread. 622 sync_manager_->Shutdown(); // Stops the SyncerThread.
615 sync_manager_->RemoveObserver(this); 623 sync_manager_->RemoveObserver(this);
616 sync_manager_.reset(); 624 sync_manager_.reset();
617 registrar_->OnSyncerShutdownComplete(); 625 registrar_->OnSyncerShutdownComplete();
618 registrar_ = NULL; 626 registrar_ = NULL;
619 627
620 if (sync_disabled) 628 if (sync_disabled)
621 DeleteSyncDataFolder(); 629 DeleteSyncDataFolder();
622 630
631 sync_loop_ = NULL;
632
623 host_ = NULL; 633 host_ = NULL;
624 } 634 }
625 635
626 void SyncBackendHost::Core::DoRequestConfig( 636 void SyncBackendHost::Core::DoRequestConfig(
627 const syncable::ModelTypeBitSet& types_to_config, 637 const syncable::ModelTypeBitSet& types_to_config,
628 sync_api::ConfigureReason reason) { 638 sync_api::ConfigureReason reason) {
639 DCHECK_EQ(MessageLoop::current(), sync_loop_);
629 sync_manager_->RequestConfig(types_to_config, reason); 640 sync_manager_->RequestConfig(types_to_config, reason);
630 } 641 }
631 642
632 void SyncBackendHost::Core::DoStartConfiguration(Callback0::Type* callback) { 643 void SyncBackendHost::Core::DoStartConfiguration(Callback0::Type* callback) {
644 DCHECK_EQ(MessageLoop::current(), sync_loop_);
633 sync_manager_->StartConfigurationMode(callback); 645 sync_manager_->StartConfigurationMode(callback);
634 } 646 }
635 647
636 void SyncBackendHost::Core::DeleteSyncDataFolder() { 648 void SyncBackendHost::Core::DeleteSyncDataFolder() {
649 DCHECK_EQ(MessageLoop::current(), sync_loop_);
637 if (file_util::DirectoryExists(host_->sync_data_folder_path())) { 650 if (file_util::DirectoryExists(host_->sync_data_folder_path())) {
638 if (!file_util::Delete(host_->sync_data_folder_path(), true)) 651 if (!file_util::Delete(host_->sync_data_folder_path(), true))
639 SLOG(DFATAL) << "Could not delete the Sync Data folder."; 652 SLOG(DFATAL) << "Could not delete the Sync Data folder.";
640 } 653 }
641 } 654 }
642 655
643 void SyncBackendHost::Core::FinishConfigureDataTypes() { 656 void SyncBackendHost::Core::FinishConfigureDataTypes() {
657 DCHECK_EQ(MessageLoop::current(), sync_loop_);
644 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this, 658 host_->frontend_loop_->PostTask(FROM_HERE, NewRunnableMethod(this,
645 &SyncBackendHost::Core::FinishConfigureDataTypesOnFrontendLoop)); 659 &SyncBackendHost::Core::FinishConfigureDataTypesOnFrontendLoop));
646 } 660 }
647 661
648 void SyncBackendHost::Core::HandleInitializationCompletedOnFrontendLoop( 662 void SyncBackendHost::Core::HandleInitializationCompletedOnFrontendLoop(
649 const WeakHandle<JsBackend>& js_backend, 663 const WeakHandle<JsBackend>& js_backend,
650 bool success) { 664 bool success) {
651 if (!host_) 665 if (!host_)
652 return; 666 return;
653 host_->HandleInitializationCompletedOnFrontendLoop(js_backend, success); 667 host_->HandleInitializationCompletedOnFrontendLoop(js_backend, success);
654 } 668 }
655 669
656 void SyncBackendHost::Core::StartSavingChanges() { 670 void SyncBackendHost::Core::StartSavingChanges() {
671 // We may already be shut down.
672 if (!sync_loop_)
673 return;
674 DCHECK_EQ(MessageLoop::current(), sync_loop_);
657 save_changes_timer_.Start(FROM_HERE, 675 save_changes_timer_.Start(FROM_HERE,
658 base::TimeDelta::FromSeconds(kSaveChangesIntervalSeconds), 676 base::TimeDelta::FromSeconds(kSaveChangesIntervalSeconds),
659 this, &Core::SaveChanges); 677 this, &Core::SaveChanges);
660 } 678 }
661 679
662 void SyncBackendHost::Core::SaveChanges() { 680 void SyncBackendHost::Core::SaveChanges() {
681 DCHECK_EQ(MessageLoop::current(), sync_loop_);
663 sync_manager_->SaveChanges(); 682 sync_manager_->SaveChanges();
664 } 683 }
665 684
666 void SyncBackendHost::Core::HandleActionableErrorEventOnFrontendLoop( 685 void SyncBackendHost::Core::HandleActionableErrorEventOnFrontendLoop(
667 const browser_sync::SyncProtocolError& sync_error) { 686 const browser_sync::SyncProtocolError& sync_error) {
668 DCHECK_EQ(MessageLoop::current(), host_->frontend_loop_);
669 if (!host_ || !host_->frontend_) 687 if (!host_ || !host_->frontend_)
670 return; 688 return;
689 DCHECK_EQ(MessageLoop::current(), host_->frontend_loop_);
671 host_->frontend_->OnActionableError(sync_error); 690 host_->frontend_->OnActionableError(sync_error);
672 } 691 }
673 692
674 void SyncBackendHost::Core::HandleAuthErrorEventOnFrontendLoop( 693 void SyncBackendHost::Core::HandleAuthErrorEventOnFrontendLoop(
675 const GoogleServiceAuthError& new_auth_error) { 694 const GoogleServiceAuthError& new_auth_error) {
676 if (!host_ || !host_->frontend_) 695 if (!host_ || !host_->frontend_)
677 return; 696 return;
678 697
679 DCHECK_EQ(MessageLoop::current(), host_->frontend_loop_); 698 DCHECK_EQ(MessageLoop::current(), host_->frontend_loop_);
680 699
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
791 } 810 }
792 811
793 void SyncBackendHost::Core::FinishConfigureDataTypesOnFrontendLoop() { 812 void SyncBackendHost::Core::FinishConfigureDataTypesOnFrontendLoop() {
794 host_->FinishConfigureDataTypesOnFrontendLoop(); 813 host_->FinishConfigureDataTypesOnFrontendLoop();
795 } 814 }
796 815
797 void SyncBackendHost::HandleInitializationCompletedOnFrontendLoop( 816 void SyncBackendHost::HandleInitializationCompletedOnFrontendLoop(
798 const WeakHandle<JsBackend>& js_backend, bool success) { 817 const WeakHandle<JsBackend>& js_backend, bool success) {
799 if (!frontend_) 818 if (!frontend_)
800 return; 819 return;
820 DCHECK_EQ(MessageLoop::current(), frontend_loop_);
801 if (!success) { 821 if (!success) {
822 initialization_state_ = NOT_INITIALIZED;
802 frontend_->OnBackendInitialized(WeakHandle<JsBackend>(), false); 823 frontend_->OnBackendInitialized(WeakHandle<JsBackend>(), false);
803 return; 824 return;
804 } 825 }
805 826
806 bool setup_completed = 827 bool setup_completed =
807 profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted); 828 profile_->GetPrefs()->GetBoolean(prefs::kSyncHasSetupCompleted);
808 if (setup_completed || initialization_state_ == DOWNLOADING_NIGORI) { 829 if (setup_completed) {
809 // Now that the syncapi is initialized, we can update the cryptographer 830 initialization_state_ = REFRESHING_ENCRYPTION;
810 // and can handle any ON_PASSPHRASE_REQUIRED notifications that may arise.
811 // TODO(tim): Bug 87797. We should be able to RefreshEncryption
812 // unconditionally as soon as the UI supports having this info early on.
813 if (setup_completed)
814 core_->sync_manager()->RefreshEncryption();
815 initialization_state_ = INITIALIZED;
816 frontend_->OnBackendInitialized(js_backend, true);
817 return;
818 } 831 }
819 832
820 initialization_state_ = DOWNLOADING_NIGORI; 833 // Run initialization state machine.
821 ConfigureDataTypes( 834 switch (initialization_state_) {
822 syncable::ModelTypeSet(), 835 case NOT_INITIALIZED:
823 syncable::ModelTypeSet(), 836 initialization_state_ = DOWNLOADING_NIGORI;
824 sync_api::CONFIGURE_REASON_NEW_CLIENT, 837 ConfigureDataTypes(
825 base::Bind( 838 syncable::ModelTypeSet(),
826 &SyncBackendHost::Core::HandleInitializationCompletedOnFrontendLoop, 839 syncable::ModelTypeSet(),
827 core_.get(), js_backend), 840 sync_api::CONFIGURE_REASON_NEW_CLIENT,
828 true); 841 base::Bind(
842 &SyncBackendHost::Core::
843 HandleInitializationCompletedOnFrontendLoop,
844 core_.get(), js_backend),
845 true);
846 break;
847 case DOWNLOADING_NIGORI:
848 // Now that the syncapi is initialized, we can update the
849 // cryptographer and can handle any ON_PASSPHRASE_REQUIRED
850 // notifications that may arise. TODO(tim): Bug 87797. We
851 // should be able to RefreshEncryption unconditionally as soon
852 // as the UI supports having this info early on.
853 initialization_state_ = REFRESHING_ENCRYPTION;
854 RefreshEncryption(
855 base::Bind(
856 &SyncBackendHost::Core::
857 HandleInitializationCompletedOnFrontendLoop,
858 core_.get(), js_backend, true));
859 break;
860 case REFRESHING_ENCRYPTION:
861 initialization_state_ = INITIALIZED;
862 frontend_->OnBackendInitialized(js_backend, true);
863 break;
864 default:
865 NOTREACHED();
866 }
829 } 867 }
830 868
831 void SyncBackendHost::FinishConfigureDataTypesOnFrontendLoop() { 869 void SyncBackendHost::FinishConfigureDataTypesOnFrontendLoop() {
832 DCHECK_EQ(MessageLoop::current(), frontend_loop_); 870 DCHECK_EQ(MessageLoop::current(), frontend_loop_);
833 // Nudge the syncer. This is necessary for both datatype addition/deletion. 871 // Nudge the syncer. This is necessary for both datatype addition/deletion.
834 // 872 //
835 // Deletions need a nudge in order to ensure the deletion occurs in a timely 873 // Deletions need a nudge in order to ensure the deletion occurs in a timely
836 // manner (see issue 56416). 874 // manner (see issue 56416).
837 // 875 //
838 // In the case of additions, on the next sync cycle, the syncer should 876 // In the case of additions, on the next sync cycle, the syncer should
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
907 return token; 945 return token;
908 } 946 }
909 947
910 SyncBackendHost::PendingConfigureDataTypesState:: 948 SyncBackendHost::PendingConfigureDataTypesState::
911 PendingConfigureDataTypesState() 949 PendingConfigureDataTypesState()
912 : reason(sync_api::CONFIGURE_REASON_UNKNOWN) {} 950 : reason(sync_api::CONFIGURE_REASON_UNKNOWN) {}
913 951
914 SyncBackendHost::PendingConfigureDataTypesState:: 952 SyncBackendHost::PendingConfigureDataTypesState::
915 ~PendingConfigureDataTypesState() {} 953 ~PendingConfigureDataTypesState() {}
916 954
955 namespace {
956
957 // Needed because MessageLoop::PostTask is overloaded.
958 void PostClosure(MessageLoop* message_loop,
959 const tracked_objects::Location& from_here,
960 const base::Closure& callback) {
961 message_loop->PostTask(from_here, callback);
962 }
963
964 } // namespace
965
966 void SyncBackendHost::RefreshEncryption(const base::Closure& done_callback) {
967 DCHECK_EQ(MessageLoop::current(), frontend_loop_);
968 base::Closure sync_thread_done_callback =
969 base::Bind(&PostClosure,
970 MessageLoop::current(), FROM_HERE, done_callback);
971 sync_thread_.message_loop()->PostTask(
972 FROM_HERE,
973 base::Bind(&SyncBackendHost::Core::DoRefreshEncryption,
974 core_.get(), sync_thread_done_callback));
975 }
976
917 #undef SVLOG 977 #undef SVLOG
918 978
919 #undef SLOG 979 #undef SLOG
920 980
921 } // namespace browser_sync 981 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/sync_backend_host.h ('k') | chrome/browser/sync/glue/sync_backend_registrar.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698