Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / profile_sync_service.cc
index 51411f9..29deb31 100644 (file)
@@ -19,6 +19,7 @@
 #include "base/memory/ref_counted.h"
 #include "base/message_loop/message_loop.h"
 #include "base/metrics/histogram.h"
+#include "base/profiler/scoped_tracker.h"
 #include "base/strings/string16.h"
 #include "base/strings/stringprintf.h"
 #include "base/threading/thread_restrictions.h"
@@ -187,6 +188,15 @@ void ClearBrowsingData(BrowsingDataRemover::Observer* observer,
   password->RemoveLoginsSyncedBetween(start, end);
 }
 
+// Perform the actual sync data folder deletion.
+// This should only be called on the sync thread.
+void DeleteSyncDataFolder(const base::FilePath& directory_path) {
+  if (base::DirectoryExists(directory_path)) {
+    if (!base::DeleteFile(directory_path, true))
+      LOG(DFATAL) << "Could not delete the Sync Data folder.";
+  }
+}
+
 }  // anonymous namespace
 
 bool ShouldShowActionOnUI(
@@ -351,7 +361,7 @@ void ProfileSyncService::Initialize() {
 }
 
 void ProfileSyncService::TrySyncDatatypePrefRecovery() {
-  DCHECK(!sync_initialized());
+  DCHECK(!backend_initialized());
   if (!HasSyncSetupCompleted())
     return;
 
@@ -547,15 +557,6 @@ bool ProfileSyncService::IsEncryptedDatatypeEnabled() const {
   return !Intersection(preferred_types, encrypted_types).Empty();
 }
 
-void ProfileSyncService::OnSyncConfigureRetry() {
-  // Note: in order to handle auth failures that arise before the backend is
-  // initialized (e.g. from invalidation notifier, or downloading new control
-  // types), we have to gracefully handle configuration retries at all times.
-  // At this point an auth error badge should be shown, which once resolved
-  // will trigger a new sync cycle.
-  NotifyObservers();
-}
-
 void ProfileSyncService::OnProtocolEvent(
     const syncer::ProtocolEvent& event) {
   FOR_EACH_OBSERVER(browser_sync::ProtocolEventObserver,
@@ -679,6 +680,8 @@ void ProfileSyncService::StartUpSlowBackendComponents(
       invalidator = provider->GetInvalidationService();
   }
 
+  directory_path_ = profile_->GetPath().Append(sync_folder);
+
   backend_.reset(
       factory_->CreateSyncBackendHost(
           profile_->GetDebugName(),
@@ -693,8 +696,6 @@ void ProfileSyncService::StartUpSlowBackendComponents(
   InitializeBackend(ShouldDeleteSyncFolder());
 
   UpdateFirstSyncTimePref();
-
-  NotifyObservers();
 }
 
 void ProfileSyncService::OnGetTokenSuccess(
@@ -765,6 +766,11 @@ void ProfileSyncService::OnGetTokenFailure(
 
 void ProfileSyncService::OnRefreshTokenAvailable(
     const std::string& account_id) {
+  // TODO(vadimt): Remove ScopedTracker below once crbug.com/422460 is fixed.
+  tracked_objects::ScopedTracker tracking_profile(
+      FROM_HERE_WITH_EXPLICIT_FUNCTION(
+          "422460 ProfileSyncService::OnRefreshTokenAvailable"));
+
   if (account_id == signin_->GetAccountIdToUse())
     OnRefreshTokensLoaded();
 }
@@ -812,8 +818,15 @@ void ProfileSyncService::Shutdown() {
 }
 
 void ProfileSyncService::ShutdownImpl(syncer::ShutdownReason reason) {
-  if (!backend_)
+  if (!backend_) {
+    if (reason == syncer::ShutdownReason::DISABLE_SYNC && sync_thread_) {
+      // If the backend is already shut down when a DISABLE_SYNC happens,
+      // the data directory needs to be cleaned up here.
+      sync_thread_->message_loop()->PostTask(FROM_HERE,
+          base::Bind(&DeleteSyncDataFolder, directory_path_));
+    }
     return;
+  }
 
   non_blocking_data_type_manager_.DisconnectSyncBackend();
 
@@ -963,7 +976,6 @@ void ProfileSyncService::OnUnrecoverableErrorImpl(
   UMA_HISTOGRAM_ENUMERATION(kSyncUnrecoverableErrorHistogram,
                             unrecoverable_error_reason_,
                             ERROR_REASON_LIMIT);
-  NotifyObservers();
   std::string location;
   from_here.Write(true, true, &location);
   LOG(ERROR)
@@ -1567,14 +1579,6 @@ void ProfileSyncService::OnConfigureDone(
   }
 }
 
-void ProfileSyncService::OnConfigureRetry() {
-  // We should have cleared our cached passphrase before we get here (in
-  // OnBackendInitialized()).
-  DCHECK(cached_passphrase_.empty());
-
-  OnSyncConfigureRetry();
-}
-
 void ProfileSyncService::OnConfigureStart() {
   sync_configure_start_time_ = base::Time::Now();
   NotifyObservers();
@@ -1595,9 +1599,9 @@ ProfileSyncService::SyncStatusSummary
   } else if (
       backend_.get() && HasSyncSetupCompleted() &&
       directory_data_type_manager_.get() &&
-      directory_data_type_manager_->state() != DataTypeManager::CONFIGURED) {
+      directory_data_type_manager_->state() == DataTypeManager::STOPPED) {
     return DATATYPES_NOT_INITIALIZED;
-  } else if (ShouldPushChanges()) {
+  } else if (SyncActive()) {
     return INITIALIZED;
   }
   return UNKNOWN_ERROR;
@@ -1669,15 +1673,30 @@ void ProfileSyncService::SetSetupInProgress(bool setup_in_progress) {
     return;
 
   startup_controller_.set_setup_in_progress(setup_in_progress);
-  if (!setup_in_progress && sync_initialized())
+  if (!setup_in_progress && backend_initialized())
     ReconfigureDatatypeManager();
   NotifyObservers();
 }
 
-bool ProfileSyncService::sync_initialized() const {
+bool ProfileSyncService::SyncActive() const {
+  return backend_initialized_ && backend_mode_ == SYNC &&
+         directory_data_type_manager_ &&
+         directory_data_type_manager_->state() != DataTypeManager::STOPPED;
+}
+
+bool ProfileSyncService::backend_initialized() const {
   return backend_initialized_;
 }
 
+ProfileSyncService::BackendMode ProfileSyncService::backend_mode() const {
+  return backend_mode_;
+}
+
+bool ProfileSyncService::ConfigurationDone() const {
+  return directory_data_type_manager_ &&
+      directory_data_type_manager_->state() == DataTypeManager::CONFIGURED;
+}
+
 bool ProfileSyncService::waiting_for_auth() const {
   return is_auth_in_progress_;
 }
@@ -1737,10 +1756,11 @@ void ProfileSyncService::UpdateSelectedTypesHistogram(
     sync_driver::user_selectable_type::TYPED_URLS,
     sync_driver::user_selectable_type::EXTENSIONS,
     sync_driver::user_selectable_type::APPS,
-    sync_driver::user_selectable_type::PROXY_TABS
+    sync_driver::user_selectable_type::WIFI_CREDENTIAL,
+    sync_driver::user_selectable_type::PROXY_TABS,
   };
 
-  COMPILE_ASSERT(32 == syncer::MODEL_TYPE_COUNT, UpdateCustomConfigHistogram);
+  COMPILE_ASSERT(33 == syncer::MODEL_TYPE_COUNT, UpdateCustomConfigHistogram);
 
   if (!sync_everything) {
     const syncer::ModelTypeSet current_types = GetPreferredDataTypes();
@@ -1797,7 +1817,6 @@ void ProfileSyncService::OnUserChoseDatatypes(
     directory_data_type_manager_->ResetDataTypeErrors();
   ChangePreferredDataTypes(chosen_types);
   AcknowledgeSyncedTypes();
-  NotifyObservers();
 }
 
 void ProfileSyncService::ChangePreferredDataTypes(
@@ -1819,6 +1838,8 @@ void ProfileSyncService::ChangePreferredDataTypes(
 }
 
 syncer::ModelTypeSet ProfileSyncService::GetActiveDataTypes() const {
+  if (!SyncActive() || !ConfigurationDone())
+    return syncer::ModelTypeSet();
   const syncer::ModelTypeSet preferred_types = GetPreferredDataTypes();
   const syncer::ModelTypeSet failed_types =
       data_type_status_table_.GetFailedTypes();
@@ -1975,9 +1996,8 @@ syncer::UserShare* ProfileSyncService::GetUserShare() const {
 
 syncer::sessions::SyncSessionSnapshot
 ProfileSyncService::GetLastSessionSnapshot() const {
-  if (HasSyncingBackend() && backend_initialized_) {
+  if (backend_)
     return backend_->GetLastSessionSnapshot();
-  }
   return syncer::sessions::SyncSessionSnapshot();
 }
 
@@ -2111,7 +2131,7 @@ void ProfileSyncService::ConsumeCachedPassphraseIfPossible() {
   // If no cached passphrase, or sync backend hasn't started up yet, just exit.
   // If the backend isn't running yet, OnBackendInitialized() will call this
   // method again after the backend starts up.
-  if (cached_passphrase_.empty() || !sync_initialized())
+  if (cached_passphrase_.empty() || !backend_initialized())
     return;
 
   // Backend is up and running, so we can consume the cached passphrase.
@@ -2161,7 +2181,7 @@ void ProfileSyncService::RequestAccessToken() {
 void ProfileSyncService::SetEncryptionPassphrase(const std::string& passphrase,
                                                  PassphraseType type) {
   // This should only be called when the backend has been initialized.
-  DCHECK(sync_initialized());
+  DCHECK(backend_initialized());
   DCHECK(!(type == IMPLICIT && IsUsingSecondaryPassphrase())) <<
       "Data is already encrypted using an explicit passphrase";
   DCHECK(!(type == EXPLICIT &&
@@ -2199,17 +2219,17 @@ bool ProfileSyncService::EncryptEverythingAllowed() const {
 }
 
 void ProfileSyncService::SetEncryptEverythingAllowed(bool allowed) {
-  DCHECK(allowed || !backend_initialized_ || !EncryptEverythingEnabled());
+  DCHECK(allowed || !backend_initialized() || !EncryptEverythingEnabled());
   encrypt_everything_allowed_ = allowed;
 }
 
 void ProfileSyncService::EnableEncryptEverything() {
   DCHECK(EncryptEverythingAllowed());
 
-  // Tests override sync_initialized() to always return true, so we
+  // Tests override backend_initialized() to always return true, so we
   // must check that instead of |backend_initialized_|.
   // TODO(akalin): Fix the above. :/
-  DCHECK(sync_initialized());
+  DCHECK(backend_initialized());
   // TODO(atwilson): Persist the encryption_pending_ flag to address the various
   // problems around cancelling encryption in the background (crbug.com/119649).
   if (!encrypt_everything_)
@@ -2236,7 +2256,6 @@ syncer::ModelTypeSet ProfileSyncService::GetEncryptedDataTypes() const {
 }
 
 void ProfileSyncService::OnSyncManagedPrefChange(bool is_sync_managed) {
-  NotifyObservers();
   if (is_sync_managed) {
     DisableForUser();
   } else {
@@ -2258,7 +2277,7 @@ void ProfileSyncService::GoogleSigninSucceeded(const std::string& account_id,
 #if defined(OS_CHROMEOS)
   RefreshSpareBootstrapToken(password);
 #endif
-  if (!sync_initialized() || GetAuthError().state() != AuthError::NONE) {
+  if (!backend_initialized() || GetAuthError().state() != AuthError::NONE) {
     // Track the fact that we're still waiting for auth to complete.
     is_auth_in_progress_ = true;
   }
@@ -2461,19 +2480,6 @@ bool ProfileSyncService::IsManaged() const {
   return sync_prefs_.IsManaged() || sync_disabled_by_admin_;
 }
 
-bool ProfileSyncService::ShouldPushChanges() {
-  // True only after all bootstrapping has succeeded: the sync backend
-  // is initialized, all enabled data types are consistent with one
-  // another, and no unrecoverable error has transpired.
-  if (HasUnrecoverableError())
-    return false;
-
-  if (!directory_data_type_manager_)
-    return false;
-
-  return directory_data_type_manager_->state() == DataTypeManager::CONFIGURED;
-}
-
 void ProfileSyncService::StopAndSuppress() {
   sync_prefs_.SetStartSuppressed(true);
   if (HasSyncingBackend()) {
@@ -2742,3 +2748,24 @@ bool ProfileSyncService::NeedBackup() const {
 base::Time ProfileSyncService::GetDeviceBackupTimeForTesting() const {
   return device_info_sync_service_->GetLocalDeviceBackupTime();
 }
+
+void ProfileSyncService::FlushDirectory() const {
+  // backend_initialized_ implies backend_ isn't NULL and the manager exists.
+  // If sync is not initialized yet, we fail silently.
+  if (backend_initialized_)
+    backend_->FlushDirectory();
+}
+
+base::FilePath ProfileSyncService::GetDirectoryPathForTest() const {
+  return directory_path_;
+}
+
+base::MessageLoop* ProfileSyncService::GetSyncLoopForTest() const {
+  if (sync_thread_) {
+    return sync_thread_->message_loop();
+  } else if (backend_) {
+    return backend_->GetSyncLoopForTesting();
+  } else {
+    return NULL;
+  }
+}