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 "sync/base/sync_export.h"
     14 #include "sync/internal_api/public/base/model_type.h"
     15 #include "sync/internal_api/public/engine/model_safe_worker.h"
     16 #include "sync/internal_api/public/sessions/type_debug_info_observer.h"
     17 
     18 namespace syncer {
     19 
     20 namespace syncable {
     21 class Directory;
     22 }  // namespace syncable
     23 
     24 class CommitContributor;
     25 class DirectoryCommitContributor;
     26 class DirectoryUpdateHandler;
     27 class DirectoryTypeDebugInfoEmitter;
     28 class NonBlockingTypeProcessorCore;
     29 class NonBlockingTypeProcessor;
     30 class UpdateHandler;
     31 struct DataTypeState;
     32 
     33 typedef std::map<ModelType, UpdateHandler*> UpdateHandlerMap;
     34 typedef std::map<ModelType, CommitContributor*> CommitContributorMap;
     35 typedef std::map<ModelType, DirectoryTypeDebugInfoEmitter*>
     36     DirectoryTypeDebugInfoEmitterMap;
     37 
     38 // Keeps track of the sets of active update handlers and commit contributors.
     39 class SYNC_EXPORT_PRIVATE ModelTypeRegistry {
     40  public:
     41   // This alternative constructor does not support any directory types.
     42   // It is used only in tests.
     43   ModelTypeRegistry();
     44 
     45   // Constructs a ModelTypeRegistry that supports directory types.
     46   ModelTypeRegistry(
     47       const std::vector<scoped_refptr<ModelSafeWorker> >& workers,
     48       syncable::Directory* directory);
     49   ~ModelTypeRegistry();
     50 
     51   // Sets the set of enabled types.
     52   void SetEnabledDirectoryTypes(const ModelSafeRoutingInfo& routing_info);
     53 
     54   // Enables an off-thread type for syncing.  Connects the given processor
     55   // and its task_runner to the newly created processor core.
     56   //
     57   // Expects that the processor's ModelType is not currently enabled.
     58   void InitializeNonBlockingType(
     59       syncer::ModelType type,
     60       const DataTypeState& data_type_state,
     61       scoped_refptr<base::SequencedTaskRunner> type_task_runner,
     62       base::WeakPtr<NonBlockingTypeProcessor> processor);
     63 
     64   // Disables the syncing of an off-thread type.
     65   //
     66   // Expects that the type is currently enabled.
     67   // Deletes the processor core associated with the type.
     68   void RemoveNonBlockingType(syncer::ModelType type);
     69 
     70   // Gets the set of enabled types.
     71   ModelTypeSet GetEnabledTypes() const;
     72 
     73   // Simple getters.
     74   UpdateHandlerMap* update_handler_map();
     75   CommitContributorMap* commit_contributor_map();
     76   DirectoryTypeDebugInfoEmitterMap* directory_type_debug_info_emitter_map();
     77 
     78   void RegisterDirectoryTypeDebugInfoObserver(
     79       syncer::TypeDebugInfoObserver* observer);
     80   void UnregisterDirectoryTypeDebugInfoObserver(
     81       syncer::TypeDebugInfoObserver* observer);
     82   bool HasDirectoryTypeDebugInfoObserver(
     83       syncer::TypeDebugInfoObserver* observer);
     84   void RequestEmitDebugInfo();
     85 
     86  private:
     87   ModelTypeSet GetEnabledNonBlockingTypes() const;
     88   ModelTypeSet GetEnabledDirectoryTypes() const;
     89 
     90   // Sets of handlers and contributors.
     91   ScopedVector<DirectoryCommitContributor> directory_commit_contributors_;
     92   ScopedVector<DirectoryUpdateHandler> directory_update_handlers_;
     93   ScopedVector<DirectoryTypeDebugInfoEmitter>
     94       directory_type_debug_info_emitters_;
     95 
     96   ScopedVector<NonBlockingTypeProcessorCore> non_blocking_type_processor_cores_;
     97 
     98   // Maps of UpdateHandlers and CommitContributors.
     99   // They do not own any of the objects they point to.
    100   UpdateHandlerMap update_handler_map_;
    101   CommitContributorMap commit_contributor_map_;
    102 
    103   // Map of DebugInfoEmitters for directory types.
    104   // Non-blocking types handle debug info differently.
    105   // Does not own its contents.
    106   DirectoryTypeDebugInfoEmitterMap directory_type_debug_info_emitter_map_;
    107 
    108   // The known ModelSafeWorkers.
    109   std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_map_;
    110 
    111   // The directory.  Not owned.
    112   syncable::Directory* directory_;
    113 
    114   // The set of enabled directory types.
    115   ModelTypeSet enabled_directory_types_;
    116 
    117   // The set of observers of per-type debug info.
    118   //
    119   // Each of the DirectoryTypeDebugInfoEmitters needs such a list.  There's
    120   // a lot of them, and their lifetimes are unpredictable, so it makes the
    121   // book-keeping easier if we just store the list here.  That way it's
    122   // guaranteed to live as long as this sync backend.
    123   ObserverList<TypeDebugInfoObserver> type_debug_info_observers_;
    124 
    125   DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry);
    126 };
    127 
    128 }  // namespace syncer
    129 
    130 #endif // SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
    131 
    132