OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 5 #ifndef COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
6 #define COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 6 #define COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
18 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
19 #include "components/sync/base/model_type.h" | 19 #include "components/sync/base/model_type.h" |
20 #include "components/sync/base/sync_export.h" | |
21 #include "components/sync/base/syncer_error.h" | 20 #include "components/sync/base/syncer_error.h" |
22 | 21 |
23 namespace base { | 22 namespace base { |
24 class DictionaryValue; | 23 class DictionaryValue; |
25 } // namespace base | 24 } // namespace base |
26 | 25 |
27 namespace syncer { | 26 namespace syncer { |
28 | 27 |
29 // TODO(akalin): Move the non-exported functions in this file to a | 28 // TODO(akalin): Move the non-exported functions in this file to a |
30 // private header. | 29 // private header. |
(...skipping 11 matching lines...) Expand all Loading... |
42 // synced. | 41 // synced. |
43 GROUP_PASSWORD, // Models that live on the password thread and are | 42 GROUP_PASSWORD, // Models that live on the password thread and are |
44 // being synced. On windows and linux, this runs on the | 43 // being synced. On windows and linux, this runs on the |
45 // DB thread. | 44 // DB thread. |
46 GROUP_NON_BLOCKING, // Models that correspond to non-blocking types. These | 45 GROUP_NON_BLOCKING, // Models that correspond to non-blocking types. These |
47 // models always stay in GROUP_NON_BLOCKING; changes are | 46 // models always stay in GROUP_NON_BLOCKING; changes are |
48 // forwarded to these models without ModelSafeWorker/ | 47 // forwarded to these models without ModelSafeWorker/ |
49 // SyncBackendRegistrar involvement. | 48 // SyncBackendRegistrar involvement. |
50 }; | 49 }; |
51 | 50 |
52 SYNC_EXPORT std::string ModelSafeGroupToString(ModelSafeGroup group); | 51 std::string ModelSafeGroupToString(ModelSafeGroup group); |
53 | 52 |
54 // WorkerLoopDestructionObserver is notified when the thread where it works | 53 // WorkerLoopDestructionObserver is notified when the thread where it works |
55 // is going to be destroyed. | 54 // is going to be destroyed. |
56 class WorkerLoopDestructionObserver { | 55 class WorkerLoopDestructionObserver { |
57 public: | 56 public: |
58 virtual void OnWorkerLoopDestroyed(ModelSafeGroup group) = 0; | 57 virtual void OnWorkerLoopDestroyed(ModelSafeGroup group) = 0; |
59 }; | 58 }; |
60 | 59 |
61 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | 60 // The Syncer uses a ModelSafeWorker for all tasks that could potentially |
62 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | 61 // 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 | 62 // 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 | 63 // 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 | 64 // 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 | 65 // cause an embedding application model to fall out of sync with the |
67 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with | 66 // 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 | 67 // 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 | 68 // thread, the affiliated worker is effectively disabled to do more |
70 // work and will notify its observer. | 69 // work and will notify its observer. |
71 class SYNC_EXPORT ModelSafeWorker | 70 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker>, |
72 : public base::RefCountedThreadSafe<ModelSafeWorker>, | 71 public base::MessageLoop::DestructionObserver { |
73 public base::MessageLoop::DestructionObserver { | |
74 public: | 72 public: |
75 // Subclass should implement to observe destruction of the loop where | 73 // Subclass should implement to observe destruction of the loop where |
76 // it actually does work. Called on UI thread immediately after worker is | 74 // it actually does work. Called on UI thread immediately after worker is |
77 // created. | 75 // created. |
78 virtual void RegisterForLoopDestruction() = 0; | 76 virtual void RegisterForLoopDestruction() = 0; |
79 | 77 |
80 // Called on sync loop from SyncBackendRegistrar::ShutDown(). Post task to | 78 // Called on sync loop from SyncBackendRegistrar::ShutDown(). Post task to |
81 // working loop to stop observing loop destruction and invoke | 79 // working loop to stop observing loop destruction and invoke |
82 // |unregister_done_callback|. | 80 // |unregister_done_callback|. |
83 virtual void UnregisterForLoopDestruction( | 81 virtual void UnregisterForLoopDestruction( |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 // See comments in model_safe_worker.cc for more details. | 142 // See comments in model_safe_worker.cc for more details. |
145 base::Callback<void(ModelSafeGroup)> unregister_done_callback_; | 143 base::Callback<void(ModelSafeGroup)> unregister_done_callback_; |
146 }; | 144 }; |
147 | 145 |
148 // A map that details which ModelSafeGroup each ModelType | 146 // A map that details which ModelSafeGroup each ModelType |
149 // belongs to. Routing info can change in response to the user enabling / | 147 // belongs to. Routing info can change in response to the user enabling / |
150 // disabling sync for certain types, as well as model association completions. | 148 // disabling sync for certain types, as well as model association completions. |
151 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; | 149 typedef std::map<ModelType, ModelSafeGroup> ModelSafeRoutingInfo; |
152 | 150 |
153 // Caller takes ownership of return value. | 151 // Caller takes ownership of return value. |
154 SYNC_EXPORT std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( | 152 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( |
155 const ModelSafeRoutingInfo& routing_info); | 153 const ModelSafeRoutingInfo& routing_info); |
156 | 154 |
157 SYNC_EXPORT std::string ModelSafeRoutingInfoToString( | 155 std::string ModelSafeRoutingInfoToString( |
158 const ModelSafeRoutingInfo& routing_info); | 156 const ModelSafeRoutingInfo& routing_info); |
159 | 157 |
160 SYNC_EXPORT ModelTypeSet | 158 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); |
161 GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); | |
162 | 159 |
163 SYNC_EXPORT ModelSafeGroup | 160 ModelSafeGroup GetGroupForModelType(const ModelType type, |
164 GetGroupForModelType(const ModelType type, const ModelSafeRoutingInfo& routes); | 161 const ModelSafeRoutingInfo& routes); |
165 | 162 |
166 } // namespace syncer | 163 } // namespace syncer |
167 | 164 |
168 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 165 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
OLD | NEW |