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 | 11 |
12 #include "base/callback_forward.h" | 12 #include "base/callback.h" |
| 13 #include "base/callback_helpers.h" |
13 #include "base/macros.h" | 14 #include "base/macros.h" |
14 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
15 #include "base/synchronization/atomic_flag.h" | 16 #include "base/synchronization/lock.h" |
| 17 #include "base/synchronization/waitable_event.h" |
16 #include "components/sync/base/model_type.h" | 18 #include "components/sync/base/model_type.h" |
17 #include "components/sync/base/syncer_error.h" | 19 #include "components/sync/base/syncer_error.h" |
18 | 20 |
19 namespace base { | 21 namespace base { |
20 class DictionaryValue; | 22 class DictionaryValue; |
21 } // namespace base | 23 } // namespace base |
22 | 24 |
23 namespace syncer { | 25 namespace syncer { |
24 | 26 |
25 using WorkCallback = base::Callback<enum SyncerError(void)>; | 27 using WorkCallback = base::OnceCallback<enum SyncerError(void)>; |
26 | 28 |
27 enum ModelSafeGroup { | 29 enum ModelSafeGroup { |
28 GROUP_PASSIVE = 0, // Models that are just "passively" being synced; e.g. | 30 GROUP_PASSIVE = 0, // Models that are just "passively" being synced; e.g. |
29 // changes to these models don't need to be pushed to a | 31 // changes to these models don't need to be pushed to a |
30 // native model. | 32 // native model. |
31 GROUP_UI, // Models that live on UI thread and are being synced. | 33 GROUP_UI, // Models that live on UI thread and are being synced. |
32 GROUP_DB, // Models that live on DB thread and are being synced. | 34 GROUP_DB, // Models that live on DB thread and are being synced. |
33 GROUP_FILE, // Models that live on FILE thread and are being synced. | 35 GROUP_FILE, // Models that live on FILE thread and are being synced. |
34 GROUP_HISTORY, // Models that live on history thread and are being | 36 GROUP_HISTORY, // Models that live on history thread and are being |
35 // synced. | 37 // synced. |
(...skipping 11 matching lines...) Expand all Loading... |
47 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | 49 // The Syncer uses a ModelSafeWorker for all tasks that could potentially |
48 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | 50 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker |
49 // only knows how to do one thing, and that is take some work (in a fully | 51 // only knows how to do one thing, and that is take some work (in a fully |
50 // pre-bound callback) and have it performed (as in Run()) from a thread which | 52 // pre-bound callback) and have it performed (as in Run()) from a thread which |
51 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to | 53 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to |
52 // cause an embedding application model to fall out of sync with the | 54 // cause an embedding application model to fall out of sync with the |
53 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with | 55 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with |
54 // a thread and does actual work on that thread. | 56 // a thread and does actual work on that thread. |
55 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { | 57 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { |
56 public: | 58 public: |
57 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise | 59 // If not stopped, calls ScheduleWork() to schedule |work| and waits until it |
58 // return CANNOT_DO_WORK. | 60 // is done or abandoned. Otherwise, returns CANNOT_DO_WORK. |
59 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); | 61 SyncerError DoWorkAndWaitUntilDone(WorkCallback work); |
60 | 62 |
61 // Soft stop worker by setting stopped_ flag. Called when sync is disabled | 63 // Soft stop worker by setting stopped_ flag. Called when sync is disabled |
62 // or browser is shutting down. Called on UI loop. | 64 // or browser is shutting down. Called on UI loop. |
63 virtual void RequestStop(); | 65 virtual void RequestStop(); |
64 | 66 |
65 // Return true if the worker was stopped. Thread safe. | |
66 bool IsStopped(); | |
67 | |
68 virtual ModelSafeGroup GetModelSafeGroup() = 0; | 67 virtual ModelSafeGroup GetModelSafeGroup() = 0; |
69 | 68 |
70 // Returns true if called on the thread this worker works on. | 69 // Returns true if called on the thread this worker works on. |
71 virtual bool IsOnModelThread() = 0; | 70 virtual bool IsOnModelThread() = 0; |
72 | 71 |
73 protected: | 72 protected: |
74 ModelSafeWorker(); | 73 ModelSafeWorker(); |
75 virtual ~ModelSafeWorker(); | 74 virtual ~ModelSafeWorker(); |
76 | 75 |
77 // Any time the Syncer performs model modifications (e.g employing a | |
78 // WriteTransaction), it should be done by this method to ensure it is done | |
79 // from a model-safe thread. | |
80 virtual SyncerError DoWorkAndWaitUntilDoneImpl(const WorkCallback& work) = 0; | |
81 | |
82 private: | 76 private: |
83 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | 77 friend class base::RefCountedThreadSafe<ModelSafeWorker>; |
84 | 78 |
85 // Whether the worker should do more work. Set when sync is disabled. | 79 // Schedules |work| on the appropriate thread. |
86 base::AtomicFlag stopped_; | 80 virtual void ScheduleWork(base::OnceClosure work) = 0; |
| 81 |
| 82 void DoWork(WorkCallback work, |
| 83 base::ScopedClosureRunner scoped_closure_runner, |
| 84 SyncerError* error, |
| 85 bool* did_run); |
| 86 |
| 87 // Synchronizes access to all members. |
| 88 base::Lock lock_; |
| 89 |
| 90 // Signaled when DoWorkAndWaitUntilDone() can return, either because the work |
| 91 // is done, the work has been abandoned or RequestStop() was called while no |
| 92 // work was running. Reset at the beginning of DoWorkAndWaitUntilDone(). |
| 93 base::WaitableEvent work_done_or_abandoned_; |
| 94 |
| 95 // Whether a WorkCallback is currently running. |
| 96 bool is_work_running_ = false; |
| 97 |
| 98 // Whether the worker was stopped. No WorkCallback can start running when this |
| 99 // is true. |
| 100 bool stopped_ = false; |
87 | 101 |
88 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker); | 102 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker); |
89 }; | 103 }; |
90 | 104 |
91 // A map that details which ModelSafeGroup each ModelType | 105 // A map that details which ModelSafeGroup each ModelType |
92 // belongs to. Routing info can change in response to the user enabling / | 106 // belongs to. Routing info can change in response to the user enabling / |
93 // disabling sync for certain types, as well as model association completions. | 107 // disabling sync for certain types, as well as model association completions. |
94 using ModelSafeRoutingInfo = std::map<ModelType, ModelSafeGroup>; | 108 using ModelSafeRoutingInfo = std::map<ModelType, ModelSafeGroup>; |
95 | 109 |
96 // Caller takes ownership of return value. | 110 // Caller takes ownership of return value. |
97 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( | 111 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( |
98 const ModelSafeRoutingInfo& routing_info); | 112 const ModelSafeRoutingInfo& routing_info); |
99 | 113 |
100 std::string ModelSafeRoutingInfoToString( | 114 std::string ModelSafeRoutingInfoToString( |
101 const ModelSafeRoutingInfo& routing_info); | 115 const ModelSafeRoutingInfo& routing_info); |
102 | 116 |
103 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); | 117 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); |
104 | 118 |
105 ModelSafeGroup GetGroupForModelType(const ModelType type, | 119 ModelSafeGroup GetGroupForModelType(const ModelType type, |
106 const ModelSafeRoutingInfo& routes); | 120 const ModelSafeRoutingInfo& routes); |
107 | 121 |
108 } // namespace syncer | 122 } // namespace syncer |
109 | 123 |
110 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 124 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
OLD | NEW |