OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 5 #ifndef SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
6 #define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 6 #define SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "sync/base/sync_export.h" |
14 #include "sync/internal_api/public/base/model_type.h" | 15 #include "sync/internal_api/public/base/model_type.h" |
15 #include "sync/internal_api/public/base/model_type_payload_map.h" | 16 #include "sync/internal_api/public/base/model_type_payload_map.h" |
16 #include "sync/internal_api/public/util/syncer_error.h" | 17 #include "sync/internal_api/public/util/syncer_error.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 class DictionaryValue; | 20 class DictionaryValue; |
20 } // namespace | 21 } // namespace |
21 | 22 |
22 namespace syncer { | 23 namespace syncer { |
23 | 24 |
24 typedef base::Callback<enum SyncerError(void)> WorkCallback; | 25 typedef base::Callback<enum SyncerError(void)> WorkCallback; |
25 | 26 |
26 enum ModelSafeGroup { | 27 enum ModelSafeGroup { |
27 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. |
28 // 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 |
29 // native model. | 30 // native model. |
30 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. |
31 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. |
32 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. |
33 GROUP_HISTORY, // Models that live on history thread and are being | 34 GROUP_HISTORY, // Models that live on history thread and are being |
34 // synced. | 35 // synced. |
35 GROUP_PASSWORD, // Models that live on the password thread and are | 36 GROUP_PASSWORD, // Models that live on the password thread and are |
36 // being synced. On windows and linux, this runs on the | 37 // being synced. On windows and linux, this runs on the |
37 // DB thread. | 38 // DB thread. |
38 MODEL_SAFE_GROUP_COUNT, | 39 MODEL_SAFE_GROUP_COUNT, |
39 }; | 40 }; |
40 | 41 |
41 std::string ModelSafeGroupToString(ModelSafeGroup group); | 42 SYNC_EXPORT std::string ModelSafeGroupToString(ModelSafeGroup group); |
42 | 43 |
43 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | 44 // The Syncer uses a ModelSafeWorker for all tasks that could potentially |
44 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker | 45 // modify syncable entries (e.g under a WriteTransaction). The ModelSafeWorker |
45 // only knows how to do one thing, and that is take some work (in a fully | 46 // only knows how to do one thing, and that is take some work (in a fully |
46 // pre-bound callback) and have it performed (as in Run()) from a thread which | 47 // pre-bound callback) and have it performed (as in Run()) from a thread which |
47 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to | 48 // is guaranteed to be "model-safe", where "safe" refers to not allowing us to |
48 // cause an embedding application model to fall out of sync with the | 49 // cause an embedding application model to fall out of sync with the |
49 // syncable::Directory due to a race. | 50 // syncable::Directory due to a race. |
50 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { | 51 class SYNC_EXPORT ModelSafeWorker |
| 52 : public base::RefCountedThreadSafe<ModelSafeWorker> { |
51 public: | 53 public: |
52 // Any time the Syncer performs model modifications (e.g employing a | 54 // Any time the Syncer performs model modifications (e.g employing a |
53 // WriteTransaction), it should be done by this method to ensure it is done | 55 // WriteTransaction), it should be done by this method to ensure it is done |
54 // from a model-safe thread. | 56 // from a model-safe thread. |
55 virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; | 57 virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; |
56 | 58 |
57 virtual ModelSafeGroup GetModelSafeGroup() = 0; | 59 virtual ModelSafeGroup GetModelSafeGroup() = 0; |
58 | 60 |
59 protected: | 61 protected: |
60 virtual ~ModelSafeWorker(); | 62 virtual ~ModelSafeWorker(); |
61 | 63 |
62 private: | 64 private: |
63 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | 65 friend class base::RefCountedThreadSafe<ModelSafeWorker>; |
64 }; | 66 }; |
65 | 67 |
66 // A map that details which ModelSafeGroup each syncer::ModelType | 68 // A map that details which ModelSafeGroup each syncer::ModelType |
67 // belongs to. Routing info can change in response to the user enabling / | 69 // belongs to. Routing info can change in response to the user enabling / |
68 // disabling sync for certain types, as well as model association completions. | 70 // disabling sync for certain types, as well as model association completions. |
69 typedef std::map<syncer::ModelType, ModelSafeGroup> | 71 typedef std::map<syncer::ModelType, ModelSafeGroup> |
70 ModelSafeRoutingInfo; | 72 ModelSafeRoutingInfo; |
71 | 73 |
72 // Caller takes ownership of return value. | 74 // Caller takes ownership of return value. |
73 base::DictionaryValue* ModelSafeRoutingInfoToValue( | 75 SYNC_EXPORT base::DictionaryValue* ModelSafeRoutingInfoToValue( |
74 const ModelSafeRoutingInfo& routing_info); | 76 const ModelSafeRoutingInfo& routing_info); |
75 | 77 |
76 std::string ModelSafeRoutingInfoToString( | 78 SYNC_EXPORT std::string ModelSafeRoutingInfoToString( |
77 const ModelSafeRoutingInfo& routing_info); | 79 const ModelSafeRoutingInfo& routing_info); |
78 | 80 |
79 // Make a ModelTypePayloadMap for all the enabled types in a | 81 // Make a ModelTypePayloadMap for all the enabled types in a |
80 // ModelSafeRoutingInfo using a default payload. | 82 // ModelSafeRoutingInfo using a default payload. |
81 syncer::ModelTypePayloadMap ModelSafeRoutingInfoToPayloadMap( | 83 SYNC_EXPORT syncer::ModelTypePayloadMap ModelSafeRoutingInfoToPayloadMap( |
82 const ModelSafeRoutingInfo& routes, | 84 const ModelSafeRoutingInfo& routes, |
83 const std::string& payload); | 85 const std::string& payload); |
84 | 86 |
85 syncer::ModelTypeSet GetRoutingInfoTypes( | 87 SYNC_EXPORT syncer::ModelTypeSet GetRoutingInfoTypes( |
86 const ModelSafeRoutingInfo& routing_info); | 88 const ModelSafeRoutingInfo& routing_info); |
87 | 89 |
88 ModelSafeGroup GetGroupForModelType(const syncer::ModelType type, | 90 SYNC_EXPORT ModelSafeGroup GetGroupForModelType( |
89 const ModelSafeRoutingInfo& routes); | 91 const syncer::ModelType type, |
| 92 const ModelSafeRoutingInfo& routes); |
90 | 93 |
91 } // namespace syncer | 94 } // namespace syncer |
92 | 95 |
93 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ | 96 #endif // SYNC_INTERNAL_API_PUBLIC_ENGINE_MODEL_SAFE_WORKER_H_ |
OLD | NEW |