Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / sync / sessions / model_type_registry.h
blob0b4a5b46b4b7509ac500306746d68476f74f62f1
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_
8 #include <map>
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"
23 namespace syncer {
25 namespace syncable {
26 class Directory;
27 } // namespace syncable
29 class CommitContributor;
30 class DirectoryCommitContributor;
31 class DirectoryUpdateHandler;
32 class DirectoryTypeDebugInfoEmitter;
33 class ModelTypeSyncWorkerImpl;
34 class ModelTypeSyncProxyImpl;
35 class UpdateHandler;
37 typedef std::map<ModelType, UpdateHandler*> UpdateHandlerMap;
38 typedef std::map<ModelType, CommitContributor*> CommitContributorMap;
39 typedef std::map<ModelType, DirectoryTypeDebugInfoEmitter*>
40 DirectoryTypeDebugInfoEmitterMap;
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();
53 // Sets the set of enabled types.
54 void SetEnabledDirectoryTypes(const ModelSafeRoutingInfo& routing_info);
56 // Enables an off-thread type for syncing. Connects the given proxy
57 // and its task_runner to the newly created worker.
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;
67 // Disables the syncing of an off-thread type.
69 // Expects that the type is currently enabled.
70 // Deletes the worker associated with the type.
71 virtual void DisconnectSyncWorker(syncer::ModelType type) OVERRIDE;
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;
88 // Gets the set of enabled types.
89 ModelTypeSet GetEnabledTypes() const;
91 // Simple getters.
92 UpdateHandlerMap* update_handler_map();
93 CommitContributorMap* commit_contributor_map();
94 DirectoryTypeDebugInfoEmitterMap* directory_type_debug_info_emitter_map();
96 void RegisterDirectoryTypeDebugInfoObserver(
97 syncer::TypeDebugInfoObserver* observer);
98 void UnregisterDirectoryTypeDebugInfoObserver(
99 syncer::TypeDebugInfoObserver* observer);
100 bool HasDirectoryTypeDebugInfoObserver(
101 syncer::TypeDebugInfoObserver* observer);
102 void RequestEmitDebugInfo();
104 base::WeakPtr<SyncContext> AsWeakPtr();
106 private:
107 void OnEncryptionStateChanged();
109 ModelTypeSet GetEnabledNonBlockingTypes() const;
110 ModelTypeSet GetEnabledDirectoryTypes() const;
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_;
118 ScopedVector<ModelTypeSyncWorkerImpl> model_type_sync_workers_;
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_;
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_;
130 // The known ModelSafeWorkers.
131 std::map<ModelSafeGroup, scoped_refptr<ModelSafeWorker> > workers_map_;
133 // The directory. Not owned.
134 syncable::Directory* directory_;
136 // A copy of the directory's most recent cryptographer.
137 scoped_ptr<Cryptographer> cryptographer_;
139 // The set of encrypted types.
140 ModelTypeSet encrypted_types_;
142 // A helper that manages cryptography state and preferences.
143 SyncEncryptionHandler* encryption_handler_;
145 // The NudgeHandler. Not owned.
146 NudgeHandler* nudge_handler_;
148 // The set of enabled directory types.
149 ModelTypeSet enabled_directory_types_;
151 // The set of observers of per-type debug info.
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_;
159 base::WeakPtrFactory<ModelTypeRegistry> weak_ptr_factory_;
161 DISALLOW_COPY_AND_ASSIGN(ModelTypeRegistry);
164 } // namespace syncer
166 #endif // SYNC_ENGINE_MODEL_TYPE_REGISTRY_H_