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