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