Home | History | Annotate | Download | only in sync_driver
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__
      6 #define COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__
      7 
      8 #include "components/sync_driver/data_type_manager.h"
      9 
     10 #include <map>
     11 #include <queue>
     12 #include <vector>
     13 
     14 #include "base/basictypes.h"
     15 #include "base/callback_forward.h"
     16 #include "base/compiler_specific.h"
     17 #include "base/memory/weak_ptr.h"
     18 #include "base/time/time.h"
     19 #include "components/sync_driver/backend_data_type_configurer.h"
     20 #include "components/sync_driver/model_association_manager.h"
     21 
     22 namespace syncer {
     23 struct DataTypeConfigurationStats;
     24 class DataTypeDebugInfoListener;
     25 template <typename T> class WeakHandle;
     26 }
     27 
     28 namespace browser_sync {
     29 
     30 class DataTypeController;
     31 class DataTypeEncryptionHandler;
     32 class DataTypeManagerObserver;
     33 class FailedDataTypesHandler;
     34 
     35 // List of data types grouped by priority and ordered from high priority to
     36 // low priority.
     37 typedef std::queue<syncer::ModelTypeSet> TypeSetPriorityList;
     38 
     39 class DataTypeManagerImpl : public DataTypeManager,
     40                             public ModelAssociationManagerDelegate {
     41  public:
     42   DataTypeManagerImpl(
     43       const base::Closure& unrecoverable_error_method,
     44       const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>&
     45           debug_info_listener,
     46       const DataTypeController::TypeMap* controllers,
     47       const DataTypeEncryptionHandler* encryption_handler,
     48       BackendDataTypeConfigurer* configurer,
     49       DataTypeManagerObserver* observer,
     50       FailedDataTypesHandler* failed_data_types_handler);
     51   virtual ~DataTypeManagerImpl();
     52 
     53   // DataTypeManager interface.
     54   virtual void Configure(syncer::ModelTypeSet desired_types,
     55                          syncer::ConfigureReason reason) OVERRIDE;
     56 
     57   // Needed only for backend migration.
     58   virtual void PurgeForMigration(
     59       syncer::ModelTypeSet undesired_types,
     60       syncer::ConfigureReason reason) OVERRIDE;
     61 
     62   virtual void Stop() OVERRIDE;
     63   virtual State state() const OVERRIDE;
     64 
     65   // |ModelAssociationManagerDelegate| implementation.
     66   virtual void OnSingleDataTypeAssociationDone(
     67       syncer::ModelType type,
     68       const syncer::DataTypeAssociationStats& association_stats) OVERRIDE;
     69   virtual void OnModelAssociationDone(
     70       const DataTypeManager::ConfigureResult& result) OVERRIDE;
     71   virtual void OnSingleDataTypeWillStop(syncer::ModelType type) OVERRIDE;
     72 
     73   // Used by unit tests. TODO(sync) : This would go away if we made
     74   // this class be able to do Dependency injection. crbug.com/129212.
     75   ModelAssociationManager* GetModelAssociationManagerForTesting() {
     76     return &model_association_manager_;
     77   }
     78 
     79  private:
     80   friend class TestDataTypeManager;
     81 
     82   // Abort configuration and stop all data types due to configuration errors.
     83   void Abort(ConfigureStatus status,
     84              const syncer::SyncError& error);
     85 
     86   // Returns the priority types (control + priority user types).
     87   // Virtual for overriding during tests.
     88   virtual syncer::ModelTypeSet GetPriorityTypes() const;
     89 
     90   // Divide |types| into sets by their priorities and return the sets from
     91   // high priority to low priority.
     92   TypeSetPriorityList PrioritizeTypes(const syncer::ModelTypeSet& types);
     93 
     94   // Post a task to reconfigure when no downloading or association are running.
     95   void ProcessReconfigure();
     96 
     97   void Restart(syncer::ConfigureReason reason);
     98   void DownloadReady(base::Time download_start_time,
     99                      syncer::ModelTypeSet types_to_download,
    100                      syncer::ModelTypeSet high_priority_types_before,
    101                      syncer::ModelTypeSet first_sync_types,
    102                      syncer::ModelTypeSet failed_configuration_types);
    103 
    104   // Notification from the SBH that download failed due to a transient
    105   // error and it will be retried.
    106   void OnDownloadRetry();
    107   void NotifyStart();
    108   void NotifyDone(const ConfigureResult& result);
    109 
    110   // Add to |configure_time_delta_| the time since we last called
    111   // Restart().
    112   void AddToConfigureTime();
    113 
    114   void ConfigureImpl(syncer::ModelTypeSet desired_types,
    115                      syncer::ConfigureReason reason);
    116 
    117   BackendDataTypeConfigurer::DataTypeConfigStateMap
    118   BuildDataTypeConfigStateMap(
    119       const syncer::ModelTypeSet& types_being_configured) const;
    120 
    121   // Start association of next batch of data types after association of
    122   // previous batch finishes.
    123   void StartNextAssociation();
    124 
    125   void StopImpl();
    126 
    127   BackendDataTypeConfigurer* configurer_;
    128   // Map of all data type controllers that are available for sync.
    129   // This list is determined at startup by various command line flags.
    130   const DataTypeController::TypeMap* controllers_;
    131   State state_;
    132   std::map<syncer::ModelType, int> start_order_;
    133   syncer::ModelTypeSet last_requested_types_;
    134 
    135   // Whether an attempt to reconfigure was made while we were busy configuring.
    136   // The |last_requested_types_| will reflect the newest set of requested types.
    137   bool needs_reconfigure_;
    138 
    139   // The reason for the last reconfigure attempt. Note: this will be set to a
    140   // valid value only when |needs_reconfigure_| is set.
    141   syncer::ConfigureReason last_configure_reason_;
    142 
    143   // The last time Restart() was called.
    144   base::Time last_restart_time_;
    145 
    146   // The accumulated time spent between calls to Restart() and going
    147   // to the DONE state.
    148   base::TimeDelta configure_time_delta_;
    149 
    150   // Sync's datatype debug info listener, which we pass model association
    151   // statistics to.
    152   const syncer::WeakHandle<syncer::DataTypeDebugInfoListener>
    153       debug_info_listener_;
    154 
    155   // The manager that handles the model association of the individual types.
    156   ModelAssociationManager model_association_manager_;
    157 
    158   // DataTypeManager must have only one observer -- the ProfileSyncService that
    159   // created it and manages its lifetime.
    160   DataTypeManagerObserver* const observer_;
    161 
    162   // For querying failed data types (having unrecoverable error) when
    163   // configuring backend.
    164   browser_sync::FailedDataTypesHandler* failed_data_types_handler_;
    165 
    166   // Types waiting to be downloaded.
    167   TypeSetPriorityList download_types_queue_;
    168 
    169   // Types waiting for association and related time tracking info.
    170   struct AssociationTypesInfo {
    171     AssociationTypesInfo();
    172     ~AssociationTypesInfo();
    173     syncer::ModelTypeSet types;
    174     syncer::ModelTypeSet first_sync_types;
    175     base::Time download_start_time;
    176     base::Time download_ready_time;
    177     base::Time association_request_time;
    178     syncer::ModelTypeSet high_priority_types_before;
    179     syncer::ModelTypeSet configured_types;
    180   };
    181   std::queue<AssociationTypesInfo> association_types_queue_;
    182 
    183   // The encryption handler lets the DataTypeManager know the state of sync
    184   // datatype encryption.
    185   const browser_sync::DataTypeEncryptionHandler* encryption_handler_;
    186 
    187   // Association and time stats of data type configuration.
    188   std::vector<syncer::DataTypeConfigurationStats> configuration_stats_;
    189 
    190   base::Closure unrecoverable_error_method_;
    191 
    192   base::WeakPtrFactory<DataTypeManagerImpl> weak_ptr_factory_;
    193 
    194   DISALLOW_COPY_AND_ASSIGN(DataTypeManagerImpl);
    195 };
    196 
    197 }  // namespace browser_sync
    198 
    199 #endif  // COMPONENTS_SYNC_DRIVER_DATA_TYPE_MANAGER_IMPL_H__
    200