Home | History | Annotate | Download | only in sessions
      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 SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
      6 #define SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
      7 
      8 #include <map>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/scoped_vector.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "sync/base/sync_export.h"
     15 #include "sync/engine/nudge_handler.h"
     16 #include "sync/internal_api/public/base/model_type.h"
     17 #include "sync/internal_api/public/engine/model_safe_worker.h"
     18 #include "sync/internal_api/public/non_blocking_sync_common.h"
     19 #include "sync/internal_api/public/sessions/type_debug_info_observer.h"
     20 #include "sync/internal_api/public/sync_context.h"
     21 #include "sync/internal_api/public/sync_encryption_handler.h"
     22 
     23 namespace syncer {
     24 
     25 namespace syncable {
     26 class Directory;
     27 }  // namespace syncable
     28 
     29 class CommitContributor;
     30 class DirectoryCommitContributor;
     31 class DirectoryUpdateHandler;
     32 class DirectoryTypeDebugInfoEmitter;
     33 class ModelTypeSyncWorkerImpl;
     34 class ModelTypeSyncProxyImpl;
     35 class UpdateHandler;
     36 
     37 typedef std::map<ModelType, UpdateHandler*> UpdateHandlerMap;
     38 typedef std::map<ModelType, CommitContributor*> CommitContributorMap;
     39 typedef std::map<ModelType, DirectoryTypeDebugInfoEmitter*>
     40     DirectoryTypeDebugInfoEmitterMap;
     41 
     42 // Keeps track of the sets of active update handlers and commit contributors.
     43 class SYNC_EXPORT_PRIVATE ModelTypeRegistry
     44     : public SyncContext,
     45       public SyncEncryptionHandler::Observer {
     46  public:
     47   // Constructs a ModelTypeRegistry that supports directory types.
     48   ModelTypeRegistry(const std::vector<scoped_refptr<ModelSafeWorker> >& workers,
     49                     syncable::Directory* directory,
     50                     NudgeHandler* nudge_handler);
     51   virtual ~ModelTypeRegistry();
     52 
     53   // Sets the set of enabled types.
     54   void SetEnabledDirectoryTypes(const ModelSafeRoutingInfo& routing_info);
     55 
     56   // Enables an off-thread type for syncing.  Connects the given proxy
     57   // and its task_runner to the newly created worker.
     58   //
     59   // Expects that the proxy's ModelType is not currently enabled.
     60   virtual void ConnectSyncTypeToWorker(
     61       syncer::ModelType type,
     62       const DataTypeState& data_type_state,
     63       const syncer::UpdateResponseDataList& saved_pending_updates,
     64       const scoped_refptr<base::SequencedTaskRunner>& type_task_runner,
     65       const base::WeakPtr<ModelTypeSyncProxyImpl>& proxy) OVERRIDE;
     66 
     67   // Disables the syncing of an off-thread type.
     68   //
     69   // Expects that the type is currently enabled.
     70   // Deletes the worker associated with the type.
     71   virtual void DisconnectSyncWorker(syncer::ModelType type) OVERRIDE;
     72 
     73   // Implementation of SyncEncryptionHandler::Observer.
     74   virtual void OnPassphraseRequired(
     75       PassphraseRequiredReason reason,
     76       const sync_pb::EncryptedData& pending_keys) OVERRIDE;
     77   virtual void OnPassphraseAccepted() OVERRIDE;
     78   virtual void OnBootstrapTokenUpdated(const std::string& bootstrap_token,
     79                                        BootstrapTokenType type) OVERRIDE;
     80   virtual void OnEncryptedTypesChanged(ModelTypeSet encrypted_types,
     81                                        bool encrypt_everything) OVERRIDE;
     82   virtual void OnEncryptionComplete() OVERRIDE;
     83   virtual void OnCryptographerStateChanged(
     84       Cryptographer* cryptographer) OVERRIDE;
     85   virtual void OnPassphraseTypeChanged(PassphraseType type,
     86                                        base::Time passphrase_time) OVERRIDE;
     87 
     88   // Gets the set of enabled types.
     89   ModelTypeSet GetEnabledTypes() const;
     90 
     91   // Simple getters.
     92   UpdateHandlerMap* update_handler_map();
     93   CommitContributorMap* commit_contributor_map();
     94   DirectoryTypeDebugInfoEmitterMap* directory_type_debug_info_emitter_map();
     95 
     96   void RegisterDirectoryTypeDebugInfoObserver(
     97       syncer::TypeDebugInfoObserver* observer);
     98   void UnregisterDirectoryTypeDebugInfoObserver(
     99       syncer::TypeDebugInfoObserver* observer);
    100   bool HasDirectoryTypeDebugInfoObserver(
    101       syncer::TypeDebugInfoObserver* observer);
    102   void RequestEmitDebugInfo();
    103 
    104   base::WeakPtr<SyncContext> AsWeakPtr();
    105 
    106  private:
    107   void OnEncryptionStateChanged();
    108 
    109   ModelTypeSet GetEnabledNonBlockingTypes() const;
    110   ModelTypeSet GetEnabledDirectoryTypes() const;
    111 
    112   // Sets of handlers and contributors.
    113   ScopedVector<DirectoryCommitContributor> directory_commit_contributors_;
    114   ScopedVector<DirectoryUpdateHandler> directory_update_handlers_;
    115   ScopedVector<DirectoryTypeDebugInfoEmitter>
    116       directory_type_debug_info_emitters_;
    117 
    118   ScopedVector<ModelTypeSyncWorkerImpl> model_type_sync_workers_;
    119 
    120   // Maps of UpdateHandlers and CommitContributors.
    121   // They do not own any of the objects they point to.
    122   UpdateHandlerMap update_handler_map_;
    123   CommitContributorMap commit_contributor_map_;
    124 
    125   // Map of DebugInfoEmitters for directory types.
    126   // Non-blocking types handle debug info differently.
    127   // Does not own its contents.
    128   DirectoryTypeDebugInfoEmitterMap directory_type_debug_info_emitter_map_;
    129 
    130   // The known ModelSafeWorkers.
    131   std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_map_;
    132 
    133   // The directory.  Not owned.
    134   syncable::Directory* directory_;
    135 
    136   // A copy of the directory's most recent cryptographer.
    137   scoped_ptr<Cryptographer> cryptographer_;
    138 
    139   // The set of encrypted types.
    140   ModelTypeSet encrypted_types_;
    141 
    142   // A helper that manages cryptography state and preferences.
    143   SyncEncryptionHandler* encryption_handler_;
    144 
    145   // The NudgeHandler.  Not owned.
    146   NudgeHandler* nudge_handler_;
    147 
    148   // The set of enabled directory types.
    149   ModelTypeSet enabled_directory_types_;
    150 
    151   // The set of observers of per-type debug info.
    152   //
    153   // Each of the DirectoryTypeDebugInfoEmitters needs such a list.  There's
    154   // a lot of them, and their lifetimes are unpredictable, so it makes the
    155   // book-keeping easier if we just store the list here.  That way it's
    156   // guaranteed to live as long as this sync backend.
    157   ObserverList<TypeDebugInfoObserver> type_debug_info_observers_;
    158 
    159   base::WeakPtrFactory<ModelTypeRegistry> weak_ptr_factory_;
    160 
    161   DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry);
    162 };
    163 
    164 }  // namespace syncer
    165 
    166 #endif // SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
    167