Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / sync / profile_sync_service.h
index 417582a..4c99c4d 100644 (file)
@@ -5,9 +5,9 @@
 #ifndef CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_
 #define CHROME_BROWSER_SYNC_PROFILE_SYNC_SERVICE_H_
 
+#include <set>
 #include <string>
 #include <utility>
-#include <vector>
 
 #include "base/basictypes.h"
 #include "base/compiler_specific.h"
@@ -22,6 +22,7 @@
 #include "base/timer/timer.h"
 #include "chrome/browser/sync/backend_unrecoverable_error_handler.h"
 #include "chrome/browser/sync/backup_rollback_controller.h"
+#include "chrome/browser/sync/glue/local_device_info_provider.h"
 #include "chrome/browser/sync/glue/sync_backend_host.h"
 #include "chrome/browser/sync/glue/synced_device_tracker.h"
 #include "chrome/browser/sync/profile_sync_service_base.h"
 #include "net/base/backoff_entry.h"
 #include "sync/internal_api/public/base/model_type.h"
 #include "sync/internal_api/public/engine/model_safe_worker.h"
+#include "sync/internal_api/public/shutdown_reason.h"
 #include "sync/internal_api/public/sync_manager_factory.h"
 #include "sync/internal_api/public/util/experiments.h"
 #include "sync/internal_api/public/util/unrecoverable_error_handler.h"
 #include "sync/js/sync_js_controller.h"
 #include "url/gurl.h"
 
-class ManagedUserSigninManagerWrapper;
 class Profile;
 class ProfileOAuth2TokenService;
 class ProfileSyncComponentsFactory;
+class SupervisedUserSigninManagerWrapper;
 class SyncErrorController;
+class SyncTypePreferenceProvider;
 
 namespace base {
 class CommandLine;
@@ -62,8 +65,6 @@ class CommandLine;
 
 namespace browser_sync {
 class BackendMigrator;
-class ChangeProcessor;
-class DataTypeManager;
 class DeviceInfo;
 class FaviconCache;
 class JsController;
@@ -74,6 +75,11 @@ class SyncSessionSnapshot;
 }  // namespace sessions
 }  // namespace browser_sync
 
+namespace sync_driver {
+class ChangeProcessor;
+class DataTypeManager;
+}  // namespace sync_driver
+
 namespace syncer {
 class BaseTransaction;
 class NetworkResources;
@@ -88,6 +94,7 @@ namespace sync_pb {
 class EncryptedData;
 }  // namespace sync_pb
 
+using browser_sync::LocalDeviceInfoProvider;
 using browser_sync::SessionsSyncManager;
 
 // ProfileSyncService is the layer between browser subsystems like bookmarks,
@@ -174,15 +181,14 @@ using browser_sync::SessionsSyncManager;
 //   data from the sync server.
 //
 class ProfileSyncService : public ProfileSyncServiceBase,
-                           public browser_sync::SyncFrontend,
+                           public sync_driver::SyncFrontend,
                            public sync_driver::SyncPrefObserver,
-                           public browser_sync::DataTypeManagerObserver,
+                           public sync_driver::DataTypeManagerObserver,
                            public syncer::UnrecoverableErrorHandler,
                            public KeyedService,
-                           public browser_sync::DataTypeEncryptionHandler,
+                           public sync_driver::DataTypeEncryptionHandler,
                            public OAuth2TokenService::Consumer,
                            public OAuth2TokenService::Observer,
-                           public SessionsSyncManager::SyncInternalApiDelegate,
                            public SigninManagerBase::Observer {
  public:
   typedef browser_sync::SyncBackendHost::Status Status;
@@ -265,9 +271,9 @@ class ProfileSyncService : public ProfileSyncServiceBase,
 
   // Takes ownership of |factory| and |signin_wrapper|.
   ProfileSyncService(
-      ProfileSyncComponentsFactory* factory,
+      scoped_ptr<ProfileSyncComponentsFactory> factory,
       Profile* profile,
-      scoped_ptr<ManagedUserSigninManagerWrapper> signin_wrapper,
+      scoped_ptr<SupervisedUserSigninManagerWrapper> signin_wrapper,
       ProfileOAuth2TokenService* oauth2_token_service,
       browser_sync::ProfileSyncServiceStartBehavior start_behavior);
   virtual ~ProfileSyncService();
@@ -296,6 +302,15 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   void AddTypeDebugInfoObserver(syncer::TypeDebugInfoObserver* observer);
   void RemoveTypeDebugInfoObserver(syncer::TypeDebugInfoObserver* observer);
 
+  // Add a sync type preference provider. Each provider may only be added once.
+  void AddPreferenceProvider(SyncTypePreferenceProvider* provider);
+  // Remove a sync type preference provider. May only be called for providers
+  // that have been added. Providers must not remove themselves while being
+  // called back.
+  void RemovePreferenceProvider(SyncTypePreferenceProvider* provider);
+  // Check whether a given sync type preference provider has been added.
+  bool HasPreferenceProvider(SyncTypePreferenceProvider* provider) const;
+
   // Asynchronously fetches base::Value representations of all sync nodes and
   // returns them to the specified callback on this thread.
   //
@@ -327,7 +342,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // enable or activate the synchronization of the data type (see
   // ActivateDataType).  Takes ownership of the pointer.
   void RegisterDataTypeController(
-      browser_sync::DataTypeController* data_type_controller);
+      sync_driver::DataTypeController* data_type_controller);
 
   // Registers a type whose sync storage will not be managed by the
   // ProfileSyncService.  It declares that this sync type may be activated at
@@ -344,29 +359,22 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // in a message that allows the component to delete its local sync state.
   void InitializeNonBlockingType(
       syncer::ModelType type,
-      scoped_refptr<base::SequencedTaskRunner> task_runner,
-      base::WeakPtr<syncer::NonBlockingTypeProcessor> processor);
+      const scoped_refptr<base::SequencedTaskRunner>& task_runner,
+      const base::WeakPtr<syncer::ModelTypeSyncProxyImpl>& proxy);
 
   // Return the active OpenTabsUIDelegate. If sessions is not enabled or not
   // currently syncing, returns NULL.
   virtual browser_sync::OpenTabsUIDelegate* GetOpenTabsUIDelegate();
 
+  // Returns the SyncedWindowDelegatesGetter from the embedded sessions manager.
+  virtual browser_sync::SyncedWindowDelegatesGetter*
+  GetSyncedWindowDelegatesGetter() const;
+
   // Returns the SyncableService for syncer::SESSIONS.
   virtual syncer::SyncableService* GetSessionsSyncableService();
 
-  // SyncInternalApiDelegate implementation.
-  //
-  // Returns sync's representation of the local device info.
-  // Return value is an empty scoped_ptr if the device info is unavailable.
-  virtual scoped_ptr<browser_sync::DeviceInfo> GetLocalDeviceInfo()
-      const OVERRIDE;
-
-  // Gets the guid for the local device. Can be used by other layers to
-  // to distinguish sync data that belongs to the local device vs data
-  // that belongs to remote devices. Returns empty string if sync is not
-  // initialized. The GUID is not persistent across Chrome signout/signin.
-  // If you sign out of Chrome and sign in, a new GUID is generated.
-  virtual std::string GetLocalSyncCacheGUID() const OVERRIDE;
+  // Returns DeviceInfo provider for the local device.
+  virtual browser_sync::LocalDeviceInfoProvider* GetLocalDeviceInfoProvider();
 
   // Returns sync's representation of the device info for a client identified
   // by |client_id|. Return value is an empty scoped ptr if the device info
@@ -389,7 +397,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // Fills state_map with a map of current data types that are possible to
   // sync, as well as their states.
   void GetDataTypeControllerStates(
-    browser_sync::DataTypeController::StateMap* state_map) const;
+    sync_driver::DataTypeController::StateMap* state_map) const;
 
   // Disables sync for user. Use ShowLoginDialog to enable.
   virtual void DisableForUser();
@@ -402,6 +410,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
       const syncer::WeakHandle<syncer::JsBackend>& js_backend,
       const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
           debug_info_listener,
+      const std::string& cache_guid,
       bool success) OVERRIDE;
   virtual void OnSyncCycleCompleted() OVERRIDE;
   virtual void OnProtocolEvent(const syncer::ProtocolEvent& event) OVERRIDE;
@@ -434,7 +443,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
 
   // DataTypeManagerObserver implementation.
   virtual void OnConfigureDone(
-      const browser_sync::DataTypeManager::ConfigureResult& result) OVERRIDE;
+      const sync_driver::DataTypeManager::ConfigureResult& result) OVERRIDE;
   virtual void OnConfigureRetry() OVERRIDE;
   virtual void OnConfigureStart() OVERRIDE;
 
@@ -549,12 +558,6 @@ class ProfileSyncService : public ProfileSyncServiceBase,
       const tracked_objects::Location& from_here,
       const std::string& message) OVERRIDE;
 
-  // Called when a datatype wishes to disable itself. Note, this does not change
-  // preferred state of a datatype and is not persisted across restarts.
-  virtual void DisableDatatype(syncer::ModelType type,
-                               const tracked_objects::Location& from_here,
-                               std::string message);
-
   // Called to re-enable a type disabled by DisableDatatype(..). Note, this does
   // not change the preferred state of a datatype, and is not persisted across
   // restarts.
@@ -716,9 +719,9 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   }
 
   // TODO(sync): This is only used in tests.  Can we remove it?
-  const browser_sync::FailedDataTypesHandler& failed_data_types_handler() const;
+  const sync_driver::FailedDataTypesHandler& failed_data_types_handler() const;
 
-  browser_sync::DataTypeManager::ConfigureStatus configure_status() {
+  sync_driver::DataTypeManager::ConfigureStatus configure_status() {
     return configure_status_;
   }
 
@@ -768,8 +771,6 @@ class ProfileSyncService : public ProfileSyncServiceBase,
 
   virtual bool IsSessionsDataTypeControllerRunning() const;
 
-  void SetBackupStartDelayForTest(base::TimeDelta delay);
-
   BackendMode backend_mode() const {
     return backend_mode_;
   }
@@ -780,7 +781,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // Return the base URL of the Sync Server.
   static GURL GetSyncServiceURL(const base::CommandLine& command_line);
 
-  void StartStopBackupForTesting();
+  base::Time GetDeviceBackupTimeForTesting() const;
 
  protected:
   // Helper to configure the priority data types.
@@ -790,16 +791,16 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   void ConfigureDataTypeManager();
 
   // Shuts down the backend sync components.
-  // |option| indicates if syncing is being disabled or not, and whether
+  // |reason| dictates if syncing is being disabled or not, and whether
   // to claim ownership of sync thread from backend.
-  void ShutdownImpl(browser_sync::SyncBackendHost::ShutdownOption option);
+  void ShutdownImpl(syncer::ShutdownReason reason);
 
   // Return SyncCredentials from the OAuth2TokenService.
   syncer::SyncCredentials GetCredentials();
 
   virtual syncer::WeakHandle<syncer::JsEventHandler> GetJsEventHandler();
 
-  const browser_sync::DataTypeController::TypeMap&
+  const sync_driver::DataTypeController::TypeMap&
       directory_data_type_controllers() {
     return directory_data_type_controllers_;
   }
@@ -815,6 +816,8 @@ class ProfileSyncService : public ProfileSyncServiceBase,
       const std::string& message,
       bool delete_sync_database);
 
+  virtual bool NeedBackup() const;
+
   // This is a cache of the last authentication response we received from the
   // sync server. The UI queries this to display appropriate messaging to the
   // user.
@@ -920,6 +923,9 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // want to startup once more.
   virtual void ReconfigureDatatypeManager();
 
+  // Collects preferred sync data types from |preference_providers_|.
+  syncer::ModelTypeSet GetDataTypesFromPreferenceProviders() const;
+
   // Called when the user changes the sync configuration, to update the UMA
   // stats.
   void UpdateSelectedTypesHistogram(
@@ -957,6 +963,19 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // Clear browsing data since first sync during rollback.
   void ClearBrowsingDataSinceFirstSync();
 
+  // Post background task to check sync backup DB state if needed.
+  void CheckSyncBackupIfNeeded();
+
+  // Callback to receive backup DB check result.
+  void CheckSyncBackupCallback(base::Time backup_time);
+
+  // Callback function to call |startup_controller_|.TryStart() after
+  // backup/rollback finishes;
+  void TryStartSyncAfterBackup();
+
+  // Clean up prefs and backup DB when rollback is not needed.
+  void CleanUpBackup();
+
  // Factory used to create various dependent objects.
   scoped_ptr<ProfileSyncComponentsFactory> factory_;
 
@@ -985,7 +1004,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   bool is_first_time_sync_configure_;
 
   // List of available data type controllers for directory types.
-  browser_sync::DataTypeController::TypeMap directory_data_type_controllers_;
+  sync_driver::DataTypeController::TypeMap directory_data_type_controllers_;
 
   // Whether the SyncBackendHost has been initialized.
   bool backend_initialized_;
@@ -1001,7 +1020,7 @@ class ProfileSyncService : public ProfileSyncServiceBase,
 
   // Encapsulates user signin - used to set/get the user's authenticated
   // email address.
-  const scoped_ptr<ManagedUserSigninManagerWrapper> signin_;
+  const scoped_ptr<SupervisedUserSigninManagerWrapper> signin_;
 
   // Information describing an unrecoverable error.
   UnrecoverableErrorReason unrecoverable_error_reason_;
@@ -1009,15 +1028,17 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   tracked_objects::Location unrecoverable_error_location_;
 
   // Manages the start and stop of the directory data types.
-  scoped_ptr<browser_sync::DataTypeManager> directory_data_type_manager_;
+  scoped_ptr<sync_driver::DataTypeManager> directory_data_type_manager_;
 
   // Manager for the non-blocking data types.
-  browser_sync::NonBlockingDataTypeManager non_blocking_data_type_manager_;
+  sync_driver::NonBlockingDataTypeManager non_blocking_data_type_manager_;
 
   ObserverList<ProfileSyncServiceBase::Observer> observers_;
   ObserverList<browser_sync::ProtocolEventObserver> protocol_event_observers_;
   ObserverList<syncer::TypeDebugInfoObserver> type_debug_info_observers_;
 
+  std::set<SyncTypePreferenceProvider*> preference_providers_;
+
   syncer::SyncJsController sync_js_controller_;
 
   // This allows us to gracefully handle an ABORTED return code from the
@@ -1054,9 +1075,9 @@ class ProfileSyncService : public ProfileSyncServiceBase,
 
   // Tracks the set of failed data types (those that encounter an error
   // or must delay loading for some reason).
-  browser_sync::FailedDataTypesHandler failed_data_types_handler_;
+  sync_driver::FailedDataTypesHandler failed_data_types_handler_;
 
-  browser_sync::DataTypeManager::ConfigureStatus configure_status_;
+  sync_driver::DataTypeManager::ConfigureStatus configure_status_;
 
   // The set of currently enabled sync experiments.
   syncer::Experiments current_experiments_;
@@ -1106,6 +1127,8 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   GoogleServiceAuthError last_get_token_error_;
   base::Time next_token_request_time_;
 
+  scoped_ptr<LocalDeviceInfoProvider> local_device_;
+
   // Locally owned SyncableService implementations.
   scoped_ptr<SessionsSyncManager> sessions_sync_manager_;
 
@@ -1118,12 +1141,20 @@ class ProfileSyncService : public ProfileSyncServiceBase,
   // Mode of current backend.
   BackendMode backend_mode_;
 
-  // When browser starts, delay sync backup/rollback backend start for this
-  // time.
-  base::TimeDelta backup_start_delay_;
+  // Whether backup is needed before sync starts.
+  bool need_backup_;
+
+  // Whether backup is finished.
+  bool backup_finished_;
+
+  base::Time backup_start_time_;
 
   base::Callback<void(Profile*, base::Time, base::Time)> clear_browsing_data_;
 
+  // Last time when pre-sync data was saved. NULL pointer means backup data
+  // state is unknown. If time value is null, backup data doesn't exist.
+  scoped_ptr<base::Time> last_backup_time_;
+
   DISALLOW_COPY_AND_ASSIGN(ProfileSyncService);
 };