OLD | NEW |
| (Empty) |
1 // Copyright 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. | |
4 | |
5 #ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | |
6 #define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "base/message_loop/message_loop.h" | |
16 #include "base/single_thread_task_runner.h" | |
17 #include "base/synchronization/lock.h" | |
18 #include "base/synchronization/waitable_event.h" | |
19 #include "sync/base/sync_export.h" | |
20 #include "sync/internal_api/public/base/model_type.h" | |
21 #include "sync/internal_api/public/util/syncer_error.h" | |
22 | |
23 namespace base { | |
24 class DictionaryValue; | |
25 } // namespace base | |
26 | |
27 namespace syncer { | |
28 | |
29 // TODO(akalin): Move the non-exported functions in this file to a | |
30 // private header. | |
31 | |
32 typedef base::Callback<enum SyncerError(void)> WorkCallback; | |
33 | |
34 enum ModelSafeGroup { | |
35 GROUP_PASSIVE = 0, // Models that are just "passively" being synced; e.g. | |
36 // changes to these models don't need to be pushed to a | |
37 // native model. | |
38 GROUP_UI, // Models that live on UI thread and are being synced. | |
39 GROUP_DB, // Models that live on DB thread and are being synced. | |
40 GROUP_FILE, // Models that live on FILE thread and are being synced. | |
41 GROUP_HISTORY, // Models that live on history thread and are being | |
42 // synced. | |
43 GROUP_PASSWORD, // Models that live on the password thread and are | |
44 // being synced. On windows and linux, this runs on the | |
45 // DB thread. | |
46 GROUP_NON_BLOCKING, // Models that correspond to non-blocking types. These | |
47 // models always stay in GROUP_NON_BLOCKING; changes are | |
48 // forwarded to these models without ModelSafeWorker/ | |
49 // SyncBackendRegistrar involvement. | |
50 }; | |
51 | |
52 SYNC_EXPORT std::string ModelSafeGroupToString(ModelSafeGroup group); | |
53 | |
54 // WorkerLoopDestructionObserver is notified when the thread where it works | |
55 // is going to be destroyed. | |
56 class WorkerLoopDestructionObserver { | |
57 public: | |
58 virtual void OnWorkerLoopDestroyed(ModelSafeGroup group) = 0; | |
59 }; | |
60 | |
61 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | |
62 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | |
63 // only knows how to do one thing, and that is take some work (in a fully | |
64 // pre-bound callback) and have it performed (as in Run()) from a thread which | |
65 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to | |
66 // cause an embedding application model to fall out of sync with the | |
67 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with | |
68 // a thread and does actual work on that thread. On the destruction of that | |
69 // thread, the affiliated worker is effectively disabled to do more | |
70 // work and will notify its observer. | |
71 class SYNC_EXPORT ModelSafeWorker | |
72 : public base::RefCountedThreadSafe<ModelSafeWorker>, | |
73 public base::MessageLoop::DestructionObserver { | |
74 public: | |
75 // Subclass should implement to observe destruction of the loop where | |
76 // it actually does work. Called on UI thread immediately after worker is | |
77 // created. | |
78 virtual void RegisterForLoopDestruction() = 0; | |
79 | |
80 // Called on sync loop from SyncBackendRegistrar::ShutDown(). Post task to | |
81 // working loop to stop observing loop destruction and invoke | |
82 // |unregister_done_callback|. | |
83 virtual void UnregisterForLoopDestruction( | |
84 base::Callback<void(ModelSafeGroup)> unregister_done_callback); | |
85 | |
86 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise | |
87 // return CANNOT_DO_WORK. | |
88 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); | |
89 | |
90 // Soft stop worker by setting stopped_ flag. Called when sync is disabled | |
91 // or browser is shutting down. Called on UI loop. | |
92 virtual void RequestStop(); | |
93 | |
94 virtual ModelSafeGroup GetModelSafeGroup() = 0; | |
95 | |
96 // MessageLoop::DestructionObserver implementation. | |
97 void WillDestroyCurrentMessageLoop() override; | |
98 | |
99 protected: | |
100 explicit ModelSafeWorker(WorkerLoopDestructionObserver* observer); | |
101 ~ModelSafeWorker() override; | |
102 | |
103 // Any time the Syncer performs model modifications (e.g employing a | |
104 // WriteTransaction), it should be done by this method to ensure it is done | |
105 // from a model-safe thread. | |
106 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0; | |
107 | |
108 base::WaitableEvent* work_done_or_stopped() { | |
109 return &work_done_or_stopped_; | |
110 } | |
111 | |
112 // Return true if the worker was stopped. Thread safe. | |
113 bool IsStopped(); | |
114 | |
115 // Subclass should call this in RegisterForLoopDestruction() from the loop | |
116 // where work is done. | |
117 void SetWorkingLoopToCurrent(); | |
118 | |
119 private: | |
120 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | |
121 | |
122 void UnregisterForLoopDestructionAsync( | |
123 base::Callback<void(ModelSafeGroup)> unregister_done_callback); | |
124 | |
125 // Whether the worker should/can do more work. Set when sync is disabled or | |
126 // when the worker's working thread is to be destroyed. | |
127 base::Lock stopped_lock_; | |
128 bool stopped_; | |
129 | |
130 // Signal set when work on native thread is finished or when native thread | |
131 // is to be destroyed so no more work can be done. | |
132 base::WaitableEvent work_done_or_stopped_; | |
133 | |
134 // Notified when working thread of the worker is to be destroyed. | |
135 WorkerLoopDestructionObserver* observer_; | |
136 | |
137 // Remember working loop for posting task to unregister destruction | |
138 // observation from sync thread when shutting down sync. | |
139 base::Lock working_task_runner_lock_; | |
140 scoped_refptr<base::SingleThreadTaskRunner> working_task_runner_; | |
141 | |
142 // Callback passed with UnregisterForLoopDestruction. Normally this | |
143 // remains unset/unused and is stored only if |working_task_runner_| isn't | |
144 // initialized by the time UnregisterForLoopDestruction is called. | |
145 // It is safe to copy and thread safe. | |
146 // See comments in model_safe_worker.cc for more details. | |
147 base::Callback<void(ModelSafeGroup)> unregister_done_callback_; | |
148 }; | |
149 | |
150 // A map that details which ModelSafeGroup each ModelType | |
151 // belongs to. Routing info can change in response to the user enabling / | |
152 // disabling sync for certain types, as well as model association completions. | |
153 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; | |
154 | |
155 // Caller takes ownership of return value. | |
156 SYNC_EXPORT std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( | |
157 const ModelSafeRoutingInfo& routing_info); | |
158 | |
159 SYNC_EXPORT std::string ModelSafeRoutingInfoToString( | |
160 const ModelSafeRoutingInfo& routing_info); | |
161 | |
162 SYNC_EXPORT ModelTypeSet GetRoutingInfoTypes( | |
163 const ModelSafeRoutingInfo& routing_info); | |
164 | |
165 SYNC_EXPORT ModelSafeGroup GetGroupForModelType( | |
166 const ModelType type, | |
167 const ModelSafeRoutingInfo& routes); | |
168 | |
169 } // namespace syncer | |
170 | |
171 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | |
OLD | NEW |