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