OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_BROWSER_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | |
6 #define CHROME_BROWSER_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/memory/ref_counted.h" | |
15 #include "chrome/browser/sync/internal_api/includes/syncer_error.h" | |
16 #include "chrome/browser/sync/syncable/model_type.h" | |
17 | |
18 namespace base { | |
19 class DictionaryValue; | |
20 } // namespace | |
21 | |
22 namespace browser_sync { | |
23 | |
24 typedef base::Callback<enum SyncerError(void)> WorkCallback; | |
25 | |
26 enum ModelSafeGroup { | |
27 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 // native model. | |
30 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_FILE, // Models that live on FILE thread and are being synced. | |
33 GROUP_HISTORY, // Models that live on history thread and are being | |
34 // synced. | |
35 GROUP_PASSWORD, // Models that live on the password thread and are | |
36 // being synced. On windows and linux, this runs on the | |
37 // DB thread. | |
38 MODEL_SAFE_GROUP_COUNT, | |
39 }; | |
40 | |
41 std::string ModelSafeGroupToString(ModelSafeGroup group); | |
42 | |
43 // The Syncer uses a ModelSafeWorker for all tasks that could potentially | |
44 // 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 // 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 // cause an embedding application model to fall out of sync with the | |
49 // syncable::Directory due to a race. | |
50 class ModelSafeWorker : public base::RefCountedThreadSafe<ModelSafeWorker> { | |
51 public: | |
52 // 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 | |
54 // from a model-safe thread. | |
55 virtual SyncerError DoWorkAndWaitUntilDone(const WorkCallback& work) = 0; | |
56 | |
57 virtual ModelSafeGroup GetModelSafeGroup() = 0; | |
58 | |
59 protected: | |
60 virtual ~ModelSafeWorker(); | |
61 | |
62 private: | |
63 friend class base::RefCountedThreadSafe<ModelSafeWorker>; | |
64 }; | |
65 | |
66 // A map that details which ModelSafeGroup each syncable::ModelType | |
67 // belongs to. Routing info can change in response to the user enabling / | |
68 // disabling sync for certain types, as well as model association completions. | |
69 typedef std::map<syncable::ModelType, ModelSafeGroup> | |
70 ModelSafeRoutingInfo; | |
71 | |
72 // Caller takes ownership of return value. | |
73 base::DictionaryValue* ModelSafeRoutingInfoToValue( | |
74 const ModelSafeRoutingInfo& routing_info); | |
75 | |
76 std::string ModelSafeRoutingInfoToString( | |
77 const ModelSafeRoutingInfo& routing_info); | |
78 | |
79 syncable::ModelTypeSet GetRoutingInfoTypes( | |
80 const ModelSafeRoutingInfo& routing_info); | |
81 | |
82 ModelSafeGroup GetGroupForModelType(const syncable::ModelType type, | |
83 const ModelSafeRoutingInfo& routes); | |
84 | |
85 // Maintain the up-to-date state regarding which ModelSafeWorkers exist and | |
86 // which types get routed to which worker. When a sync session begins, it will | |
87 // snapshot the state at that instant, and will use that for the entire | |
88 // session. This means if a model becomes synced (or unsynced) by the user | |
89 // during a sync session, that session will complete and be unaware of this | |
90 // change -- it will only get picked up for the next session. | |
91 // TODO(tim): That's really the only way I can make sense of it in the Syncer | |
92 // HOWEVER, it is awkward for running ModelAssociation. We need to make sure | |
93 // we don't run such a thing until an active session wraps up. | |
94 class ModelSafeWorkerRegistrar { | |
95 public: | |
96 ModelSafeWorkerRegistrar() { } | |
97 // Get the current list of active ModelSafeWorkers. Should be threadsafe. | |
98 virtual void GetWorkers(std::vector<ModelSafeWorker*>* out) = 0; | |
99 | |
100 // Get the current routing information for all enabled model types. | |
101 // If a model type is not enabled (that is, if the syncer should not | |
102 // be trying to sync it), it is not in this map. | |
103 virtual void GetModelSafeRoutingInfo(ModelSafeRoutingInfo* out) = 0; | |
104 protected: | |
105 virtual ~ModelSafeWorkerRegistrar() {} | |
106 private: | |
107 DISALLOW_COPY_AND_ASSIGN(ModelSafeWorkerRegistrar); | |
108 }; | |
109 | |
110 } // namespace browser_sync | |
111 | |
112 #endif // CHROME_BROWSER_SYNC_ENGINE_MODEL_SAFE_WORKER_H_ | |
OLD | NEW |