Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / chrome / browser / sync / glue / sync_backend_registrar.h
blobf46b77dc6a05614f66380b922df9557910bc6f66
1 // Copyright (c) 2012 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 CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_
6 #define CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_
8 #include <map>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h"
15 #include "base/threading/thread.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/sync_manager.h"
20 class Profile;
22 namespace base {
23 class MessageLoop;
26 namespace syncer {
27 struct UserShare;
28 } // namespace syncer
30 namespace sync_driver {
31 class ChangeProcessor;
34 namespace browser_sync {
36 class UIModelWorker;
38 // A class that keep track of the workers, change processors, and
39 // routing info for the enabled sync types, and also routes change
40 // events to the right processors.
41 class SyncBackendRegistrar : public syncer::SyncManager::ChangeDelegate,
42 public syncer::WorkerLoopDestructionObserver {
43 public:
44 // |name| is used for debugging. Does not take ownership of |profile|.
45 // Must be created on the UI thread.
46 SyncBackendRegistrar(
47 const std::string& name,
48 Profile* profile,
49 scoped_ptr<base::Thread> sync_thread,
50 const scoped_refptr<base::SingleThreadTaskRunner>& ui_thread,
51 const scoped_refptr<base::SingleThreadTaskRunner>& db_thread,
52 const scoped_refptr<base::SingleThreadTaskRunner>& file_thread);
54 // SyncBackendRegistrar must be destroyed as follows:
56 // 1) On the UI thread, call RequestWorkerStopOnUIThread().
57 // 2) UI posts task to shut down syncer on sync thread.
58 // 3) If sync is disabled, call ReleaseSyncThread() on the UI thread.
59 // 3) UI posts SyncBackendRegistrar::ShutDown() on sync thread to
60 // unregister workers from observing destruction of their working loops.
61 // 4) Workers notify registrar when unregistration finishes or working
62 // loops are destroyed. Registrar destroys itself on last worker
63 // notification. Sync thread will be stopped if ownership was not
64 // released.
65 ~SyncBackendRegistrar() override;
67 // Informs the SyncBackendRegistrar of the currently enabled set of types.
68 // These types will be placed in the passive group. This function should be
69 // called exactly once during startup.
70 void SetInitialTypes(syncer::ModelTypeSet initial_types);
72 // Returns whether or not we are currently syncing encryption keys.
73 // Must be called on the UI thread.
74 bool IsNigoriEnabled() const;
76 // Removes all types in |types_to_remove| from the routing info and
77 // adds all the types in |types_to_add| to the routing info that are
78 // not already there (initially put in the passive group).
79 // |types_to_remove| and |types_to_add| must be disjoint. Returns
80 // the set of newly-added types. Must be called on the UI thread.
81 syncer::ModelTypeSet ConfigureDataTypes(
82 syncer::ModelTypeSet types_to_add,
83 syncer::ModelTypeSet types_to_remove);
85 // Returns the set of enabled types as of the last configuration. Note that
86 // this might be different from the current types in the routing info due
87 // to DeactiveDataType being called separately from ConfigureDataTypes.
88 syncer::ModelTypeSet GetLastConfiguredTypes() const;
90 // Must be called from the UI thread. (See destructor comment.)
91 void RequestWorkerStopOnUIThread();
93 // Activates the given data type (which should belong to the given
94 // group) and starts the given change processor. Must be called
95 // from |group|'s native thread.
96 void ActivateDataType(syncer::ModelType type,
97 syncer::ModelSafeGroup group,
98 sync_driver::ChangeProcessor* change_processor,
99 syncer::UserShare* user_share);
101 // Deactivates the given type if necessary. Must be called from the
102 // UI thread and not |type|'s native thread. Yes, this is
103 // surprising: see http://crbug.com/92804.
104 void DeactivateDataType(syncer::ModelType type);
106 // Returns true only between calls to ActivateDataType(type, ...)
107 // and DeactivateDataType(type). Used only by tests.
108 bool IsTypeActivatedForTest(syncer::ModelType type) const;
110 // SyncManager::ChangeDelegate implementation. May be called from
111 // any thread.
112 void OnChangesApplied(
113 syncer::ModelType model_type,
114 int64 model_version,
115 const syncer::BaseTransaction* trans,
116 const syncer::ImmutableChangeRecordList& changes) override;
117 void OnChangesComplete(syncer::ModelType model_type) override;
119 void GetWorkers(std::vector<scoped_refptr<syncer::ModelSafeWorker> >* out);
120 void GetModelSafeRoutingInfo(syncer::ModelSafeRoutingInfo* out);
122 // syncer::WorkerLoopDestructionObserver implementation.
123 void OnWorkerLoopDestroyed(syncer::ModelSafeGroup group) override;
125 // Release ownership of |sync_thread_|. Called when sync is disabled.
126 scoped_ptr<base::Thread> ReleaseSyncThread();
128 // Unregister workers from loop destruction observation.
129 void Shutdown();
131 base::Thread* sync_thread();
133 private:
134 typedef std::map<syncer::ModelSafeGroup,
135 scoped_refptr<syncer::ModelSafeWorker> > WorkerMap;
136 typedef std::map<syncer::ModelType, sync_driver::ChangeProcessor*>
137 ProcessorMap;
139 // Callback after workers unregister from observing destruction of their
140 // working loops.
141 void OnWorkerUnregistrationDone(syncer::ModelSafeGroup group);
143 void RemoveWorker(syncer::ModelSafeGroup group);
145 // Returns the change processor for the given model, or NULL if none
146 // exists. Must be called from |group|'s native thread.
147 sync_driver::ChangeProcessor* GetProcessor(syncer::ModelType type) const;
149 // Must be called with |lock_| held. Simply returns the change
150 // processor for the given type, if it exists. May be called from
151 // any thread.
152 sync_driver::ChangeProcessor* GetProcessorUnsafe(
153 syncer::ModelType type) const;
155 // Return true if |model_type| lives on the current thread. Must be
156 // called with |lock_| held. May be called on any thread.
157 bool IsCurrentThreadSafeForModel(
158 syncer::ModelType model_type) const;
160 // Returns true if the current thread is the native thread for the
161 // given group (or if it is undeterminable).
162 bool IsOnThreadForGroup(syncer::ModelType type,
163 syncer::ModelSafeGroup group) const;
165 // Name used for debugging.
166 const std::string name_;
168 Profile* const profile_;
170 // Protects all variables below.
171 mutable base::Lock lock_;
173 // We maintain ownership of all workers. In some cases, we need to
174 // ensure shutdown occurs in an expected sequence by Stop()ing
175 // certain workers. They are guaranteed to be valid because we only
176 // destroy elements of |workers_| after the syncapi has been
177 // destroyed. Unless a worker is no longer needed because all types
178 // that get routed to it have been disabled (from syncing). In that
179 // case, we'll destroy on demand *after* routing any dependent types
180 // to syncer::GROUP_PASSIVE, so that the syncapi doesn't call into garbage.
181 // If a key is present, it means at least one ModelType that routes
182 // to that model safe group is being synced.
183 WorkerMap workers_;
184 syncer::ModelSafeRoutingInfo routing_info_;
186 // The change processors that handle the different data types.
187 ProcessorMap processors_;
189 // The types that were enabled as of the last configuration. Updated on each
190 // call to ConfigureDataTypes as well as SetInitialTypes.
191 syncer::ModelTypeSet last_configured_types_;
193 // Parks stopped workers because they may still be referenced by syncer.
194 std::vector<scoped_refptr<syncer::ModelSafeWorker> > stopped_workers_;
196 // References to the thread task runners that sync depends on.
197 const scoped_refptr<base::SingleThreadTaskRunner> ui_thread_;
198 const scoped_refptr<base::SingleThreadTaskRunner> db_thread_;
199 const scoped_refptr<base::SingleThreadTaskRunner> file_thread_;
201 // Declare |sync_thread_| at the end so that it will be destroyed before
202 // objects above because tasks on sync thread depend on those objects,
203 // e.g. Shutdown() depends on |lock_|, SyncManager::Init() depends on
204 // workers, etc.
205 scoped_ptr<base::Thread> sync_thread_;
207 DISALLOW_COPY_AND_ASSIGN(SyncBackendRegistrar);
210 } // namespace browser_sync
212 #endif // CHROME_BROWSER_SYNC_GLUE_SYNC_BACKEND_REGISTRAR_H_