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.
5 #ifndef SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
6 #define SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_
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/sessions/type_debug_info_observer.h"
19 #include "sync/internal_api/public/sync_context.h"
25 } // namespace syncable
27 class CommitContributor
;
28 class DirectoryCommitContributor
;
29 class DirectoryUpdateHandler
;
30 class DirectoryTypeDebugInfoEmitter
;
31 class ModelTypeSyncWorkerImpl
;
32 class ModelTypeSyncProxyImpl
;
36 typedef std::map
<ModelType
, UpdateHandler
*> UpdateHandlerMap
;
37 typedef std::map
<ModelType
, CommitContributor
*> CommitContributorMap
;
38 typedef std::map
<ModelType
, DirectoryTypeDebugInfoEmitter
*>
39 DirectoryTypeDebugInfoEmitterMap
;
41 // Keeps track of the sets of active update handlers and commit contributors.
42 class SYNC_EXPORT_PRIVATE ModelTypeRegistry
: public SyncContext
{
44 // Constructs a ModelTypeRegistry that supports directory types.
45 ModelTypeRegistry(const std::vector
<scoped_refptr
<ModelSafeWorker
> >& workers
,
46 syncable::Directory
* directory
,
47 NudgeHandler
* nudge_handler
);
48 virtual ~ModelTypeRegistry();
50 // Sets the set of enabled types.
51 void SetEnabledDirectoryTypes(const ModelSafeRoutingInfo
& routing_info
);
53 // Enables an off-thread type for syncing. Connects the given proxy
54 // and its task_runner to the newly created worker.
56 // Expects that the proxy's ModelType is not currently enabled.
57 virtual void ConnectSyncTypeToWorker(
58 syncer::ModelType type
,
59 const DataTypeState
& data_type_state
,
60 const scoped_refptr
<base::SequencedTaskRunner
>& type_task_runner
,
61 const base::WeakPtr
<ModelTypeSyncProxyImpl
>& proxy
) OVERRIDE
;
63 // Disables the syncing of an off-thread type.
65 // Expects that the type is currently enabled.
66 // Deletes the worker associated with the type.
67 virtual void DisconnectSyncWorker(syncer::ModelType type
) OVERRIDE
;
69 // Gets the set of enabled types.
70 ModelTypeSet
GetEnabledTypes() const;
73 UpdateHandlerMap
* update_handler_map();
74 CommitContributorMap
* commit_contributor_map();
75 DirectoryTypeDebugInfoEmitterMap
* directory_type_debug_info_emitter_map();
77 void RegisterDirectoryTypeDebugInfoObserver(
78 syncer::TypeDebugInfoObserver
* observer
);
79 void UnregisterDirectoryTypeDebugInfoObserver(
80 syncer::TypeDebugInfoObserver
* observer
);
81 bool HasDirectoryTypeDebugInfoObserver(
82 syncer::TypeDebugInfoObserver
* observer
);
83 void RequestEmitDebugInfo();
85 base::WeakPtr
<SyncContext
> AsWeakPtr();
88 ModelTypeSet
GetEnabledNonBlockingTypes() const;
89 ModelTypeSet
GetEnabledDirectoryTypes() const;
91 // Sets of handlers and contributors.
92 ScopedVector
<DirectoryCommitContributor
> directory_commit_contributors_
;
93 ScopedVector
<DirectoryUpdateHandler
> directory_update_handlers_
;
94 ScopedVector
<DirectoryTypeDebugInfoEmitter
>
95 directory_type_debug_info_emitters_
;
97 ScopedVector
<ModelTypeSyncWorkerImpl
> model_type_sync_workers_
;
99 // Maps of UpdateHandlers and CommitContributors.
100 // They do not own any of the objects they point to.
101 UpdateHandlerMap update_handler_map_
;
102 CommitContributorMap commit_contributor_map_
;
104 // Map of DebugInfoEmitters for directory types.
105 // Non-blocking types handle debug info differently.
106 // Does not own its contents.
107 DirectoryTypeDebugInfoEmitterMap directory_type_debug_info_emitter_map_
;
109 // The known ModelSafeWorkers.
110 std::map
<ModelSafeGroup
, scoped_refptr
<ModelSafeWorker
> > workers_map_
;
112 // The directory. Not owned.
113 syncable::Directory
* directory_
;
115 // The NudgeHandler. Not owned.
116 NudgeHandler
* nudge_handler_
;
118 // The set of enabled directory types.
119 ModelTypeSet enabled_directory_types_
;
121 // The set of observers of per-type debug info.
123 // Each of the DirectoryTypeDebugInfoEmitters needs such a list. There's
124 // a lot of them, and their lifetimes are unpredictable, so it makes the
125 // book-keeping easier if we just store the list here. That way it's
126 // guaranteed to live as long as this sync backend.
127 ObserverList
<TypeDebugInfoObserver
> type_debug_info_observers_
;
129 base::WeakPtrFactory
<ModelTypeRegistry
> weak_ptr_factory_
;
131 DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry
);
134 } // namespace syncer
136 #endif // SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_