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/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"
25 class ModelTypeSyncWorkerImpl
;
26 class ModelTypeProcessorImpl
;
33 } // namespace syncable
35 class CommitContributor
;
36 class DirectoryCommitContributor
;
37 class DirectoryUpdateHandler
;
38 class DirectoryTypeDebugInfoEmitter
;
41 typedef std::map
<ModelType
, UpdateHandler
*> UpdateHandlerMap
;
42 typedef std::map
<ModelType
, CommitContributor
*> CommitContributorMap
;
43 typedef std::map
<ModelType
, DirectoryTypeDebugInfoEmitter
*>
44 DirectoryTypeDebugInfoEmitterMap
;
46 // Keeps track of the sets of active update handlers and commit contributors.
47 class SYNC_EXPORT_PRIVATE ModelTypeRegistry
48 : public syncer_v2::SyncContext
,
49 public SyncEncryptionHandler::Observer
{
51 // Constructs a ModelTypeRegistry that supports directory types.
52 ModelTypeRegistry(const std::vector
<scoped_refptr
<ModelSafeWorker
> >& workers
,
53 syncable::Directory
* directory
,
54 NudgeHandler
* nudge_handler
);
55 ~ModelTypeRegistry() override
;
57 // Sets the set of enabled types.
58 void SetEnabledDirectoryTypes(const ModelSafeRoutingInfo
& routing_info
);
60 // Enables an off-thread type for syncing. Connects the given proxy
61 // and its task_runner to the newly created worker.
63 // Expects that the proxy's ModelType is not currently enabled.
64 void ConnectSyncTypeToWorker(
65 syncer::ModelType type
,
66 const syncer_v2::DataTypeState
& data_type_state
,
67 const syncer_v2::UpdateResponseDataList
& saved_pending_updates
,
68 const scoped_refptr
<base::SequencedTaskRunner
>& type_task_runner
,
69 const base::WeakPtr
<syncer_v2::ModelTypeProcessorImpl
>& proxy
) override
;
71 // Disables the syncing of an off-thread type.
73 // Expects that the type is currently enabled.
74 // Deletes the worker associated with the type.
75 void DisconnectSyncWorker(syncer::ModelType type
) override
;
77 // Implementation of SyncEncryptionHandler::Observer.
78 void OnPassphraseRequired(
79 PassphraseRequiredReason reason
,
80 const sync_pb::EncryptedData
& pending_keys
) override
;
81 void OnPassphraseAccepted() override
;
82 void OnBootstrapTokenUpdated(const std::string
& bootstrap_token
,
83 BootstrapTokenType type
) override
;
84 void OnEncryptedTypesChanged(ModelTypeSet encrypted_types
,
85 bool encrypt_everything
) override
;
86 void OnEncryptionComplete() override
;
87 void OnCryptographerStateChanged(Cryptographer
* cryptographer
) override
;
88 void OnPassphraseTypeChanged(PassphraseType type
,
89 base::Time passphrase_time
) override
;
90 void OnLocalSetPassphraseEncryption(
91 const SyncEncryptionHandler::NigoriState
& nigori_state
) override
;
93 // Gets the set of enabled types.
94 ModelTypeSet
GetEnabledTypes() const;
97 UpdateHandlerMap
* update_handler_map();
98 CommitContributorMap
* commit_contributor_map();
99 DirectoryTypeDebugInfoEmitterMap
* directory_type_debug_info_emitter_map();
101 void RegisterDirectoryTypeDebugInfoObserver(
102 syncer::TypeDebugInfoObserver
* observer
);
103 void UnregisterDirectoryTypeDebugInfoObserver(
104 syncer::TypeDebugInfoObserver
* observer
);
105 bool HasDirectoryTypeDebugInfoObserver(
106 const syncer::TypeDebugInfoObserver
* observer
) const;
107 void RequestEmitDebugInfo();
109 base::WeakPtr
<SyncContext
> AsWeakPtr();
112 void OnEncryptionStateChanged();
114 ModelTypeSet
GetEnabledNonBlockingTypes() const;
115 ModelTypeSet
GetEnabledDirectoryTypes() const;
117 // Sets of handlers and contributors.
118 ScopedVector
<DirectoryCommitContributor
> directory_commit_contributors_
;
119 ScopedVector
<DirectoryUpdateHandler
> directory_update_handlers_
;
120 ScopedVector
<DirectoryTypeDebugInfoEmitter
>
121 directory_type_debug_info_emitters_
;
123 ScopedVector
<syncer_v2::ModelTypeSyncWorkerImpl
> model_type_sync_workers_
;
125 // Maps of UpdateHandlers and CommitContributors.
126 // They do not own any of the objects they point to.
127 UpdateHandlerMap update_handler_map_
;
128 CommitContributorMap commit_contributor_map_
;
130 // Map of DebugInfoEmitters for directory types.
131 // Non-blocking types handle debug info differently.
132 // Does not own its contents.
133 DirectoryTypeDebugInfoEmitterMap directory_type_debug_info_emitter_map_
;
135 // The known ModelSafeWorkers.
136 std::map
<ModelSafeGroup
, scoped_refptr
<ModelSafeWorker
> > workers_map_
;
138 // The directory. Not owned.
139 syncable::Directory
* directory_
;
141 // A copy of the directory's most recent cryptographer.
142 scoped_ptr
<Cryptographer
> cryptographer_
;
144 // The set of encrypted types.
145 ModelTypeSet encrypted_types_
;
147 // The NudgeHandler. Not owned.
148 NudgeHandler
* nudge_handler_
;
150 // The set of enabled directory types.
151 ModelTypeSet enabled_directory_types_
;
153 // The set of observers of per-type debug info.
155 // Each of the DirectoryTypeDebugInfoEmitters needs such a list. There's
156 // a lot of them, and their lifetimes are unpredictable, so it makes the
157 // book-keeping easier if we just store the list here. That way it's
158 // guaranteed to live as long as this sync backend.
159 base::ObserverList
<TypeDebugInfoObserver
> type_debug_info_observers_
;
161 base::WeakPtrFactory
<ModelTypeRegistry
> weak_ptr_factory_
;
163 DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry
);
166 } // namespace syncer
168 #endif // SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_