Chromium Code Reviews| 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/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/synchronization/atomic_flag.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/synchronization/waitable_event.h" | |
| 16 #include "components/sync/base/model_type.h" | 17 #include "components/sync/base/model_type.h" |
| 17 #include "components/sync/base/syncer_error.h" | 18 #include "components/sync/base/syncer_error.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 class DictionaryValue; | 21 class DictionaryValue; |
| 21 } // namespace base | 22 } // namespace base |
| 22 | 23 |
| 23 namespace syncer { | 24 namespace syncer { |
| 24 | 25 |
| 25 using WorkCallback = base::Callback<enum SyncerError(void)>; | 26 using WorkCallback = base::Callback<enum SyncerError(void)>; |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 47 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | 48 // The Syncer uses a ModelSafeWorker for all tasks that could potentially |
| 48 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | 49 // 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 | 50 // 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 | 51 // 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 | 52 // 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 | 53 // cause an embedding application model to fall out of sync with the |
| 53 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with | 54 // syncable::Directory due to a race. Each ModelSafeWorker is affiliated with |
| 54 // a thread and does actual work on that thread. | 55 // a thread and does actual work on that thread. |
| 55 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { | 56 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { |
| 56 public: | 57 public: |
| 57 // If not stopped, call DoWorkAndWaitUntilDoneImpl() to do work. Otherwise | 58 class ScopedSignalWorkDoneOrAbandoned; |
| 58 // return CANNOT_DO_WORK. | 59 |
| 60 // If not stopped, calls ScheduleWork() to schedule |work| and waits until it | |
| 61 // is done or abandoned. Otherwise, returns CANNOT_DO_WORK. | |
| 59 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); | 62 SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work); |
| 60 | 63 |
| 61 // Soft stop worker by setting stopped_ flag. Called when sync is disabled | 64 // Soft stop worker by setting stopped_ flag. Called when sync is disabled |
| 62 // or browser is shutting down. Called on UI loop. | 65 // or browser is shutting down. Called on UI loop. |
| 63 virtual void RequestStop(); | 66 virtual void RequestStop(); |
| 64 | 67 |
| 65 // Return true if the worker was stopped. Thread safe. | |
| 66 bool IsStopped(); | |
| 67 | |
| 68 virtual ModelSafeGroup GetModelSafeGroup() = 0; | 68 virtual ModelSafeGroup GetModelSafeGroup() = 0; |
| 69 | 69 |
| 70 // Returns true if called on the thread this worker works on. | 70 // Returns true if called on the thread this worker works on. |
| 71 virtual bool IsOnModelThread() = 0; | 71 virtual bool IsOnModelThread() = 0; |
| 72 | 72 |
| 73 protected: | 73 protected: |
| 74 ModelSafeWorker(); | 74 ModelSafeWorker(); |
| 75 virtual ~ModelSafeWorker(); | 75 virtual ~ModelSafeWorker(); |
| 76 | 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 | |
| 82 private: | 77 private: |
| 83 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | 78 friend class base::RefCountedThreadSafe<ModelSafeWorker>; |
| 84 | 79 |
| 85 // Whether the worker should do more work. Set when sync is disabled. | 80 // Schedules |work| on the appropriate thread. |
| 86 base::AtomicFlag stopped_; | 81 virtual void ScheduleWork(base::Closure work) = 0; |
| 82 | |
| 83 void DoWork( | |
| 84 const WorkCallback& work, | |
|
skym
2017/04/06 00:34:54
The const& is back!
fdoray
2017/04/06 18:10:55
Done.
| |
| 85 ScopedSignalWorkDoneOrAbandoned scoped_signal_work_done_or_abandoned, | |
| 86 SyncerError* error, | |
| 87 bool* did_run); | |
| 88 | |
| 89 // Synchronizes access to all members. | |
| 90 base::Lock lock_; | |
| 91 | |
| 92 // Signaled when DoWorkAndWaitUntilDone() can return, either because the work | |
| 93 // is done, the work has been abandoned or RequestStop() was called while no | |
| 94 // work was running. Reset at the beginning of DoWorkAndWaitUntilDone(). | |
| 95 base::WaitableEvent work_done_or_abandoned_; | |
| 96 | |
| 97 // Whether a WorkCallback is currently running. | |
| 98 bool is_work_running_ = false; | |
| 99 | |
| 100 // Whether the worker was stopped. No WorkCallback can start running when this | |
| 101 // is true. | |
| 102 bool stopped_ = false; | |
| 87 | 103 |
| 88 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker); | 104 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorker); |
| 89 }; | 105 }; |
| 90 | 106 |
| 91 // A map that details which ModelSafeGroup each ModelType | 107 // A map that details which ModelSafeGroup each ModelType |
| 92 // belongs to. Routing info can change in response to the user enabling / | 108 // belongs to. Routing info can change in response to the user enabling / |
| 93 // disabling sync for certain types, as well as model association completions. | 109 // disabling sync for certain types, as well as model association completions. |
| 94 using ModelSafeRoutingInfo = std::map<ModelType, ModelSafeGroup>; | 110 using ModelSafeRoutingInfo = std::map<ModelType, ModelSafeGroup>; |
| 95 | 111 |
| 96 // Caller takes ownership of return value. | 112 // Caller takes ownership of return value. |
| 97 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( | 113 std::unique_ptr<base::DictionaryValue> ModelSafeRoutingInfoToValue( |
| 98 const ModelSafeRoutingInfo& routing_info); | 114 const ModelSafeRoutingInfo& routing_info); |
| 99 | 115 |
| 100 std::string ModelSafeRoutingInfoToString( | 116 std::string ModelSafeRoutingInfoToString( |
| 101 const ModelSafeRoutingInfo& routing_info); | 117 const ModelSafeRoutingInfo& routing_info); |
| 102 | 118 |
| 103 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); | 119 ModelTypeSet GetRoutingInfoTypes(const ModelSafeRoutingInfo& routing_info); |
| 104 | 120 |
| 105 ModelSafeGroup GetGroupForModelType(const ModelType type, | 121 ModelSafeGroup GetGroupForModelType(const ModelType type, |
| 106 const ModelSafeRoutingInfo& routes); | 122 const ModelSafeRoutingInfo& routes); |
| 107 | 123 |
| 108 } // namespace syncer | 124 } // namespace syncer |
| 109 | 125 |
| 110 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | 126 #endif // COMPONENTS_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ |
| OLD | NEW |