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