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 // A class representing an attempt to synchronize the local syncable data | 5 // A class representing an attempt to synchronize the local syncable data |
6 // store with a sync server. A SyncSession instance is passed as a stateful | 6 // store with a sync server. A SyncSession instance is passed as a stateful |
7 // bundle to and from various SyncerCommands with the goal of converging the | 7 // bundle to and from various SyncerCommands with the goal of converging the |
8 // client view of data with that of the server. The commands twiddle with | 8 // client view of data with that of the server. The commands twiddle with |
9 // session status in response to events and hiccups along the way, set and | 9 // session status in response to events and hiccups along the way, set and |
10 // query session progress with regards to conflict resolution and applying | 10 // query session progress with regards to conflict resolution and applying |
11 // server updates, and access the SyncSessionContext for the current session | 11 // server updates, and access the SyncSessionContext for the current session |
12 // via SyncSession instances. | 12 // via SyncSession instances. |
13 | 13 |
14 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_ | 14 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_ |
15 #define SYNC_SESSIONS_SYNC_SESSION_H_ | 15 #define SYNC_SESSIONS_SYNC_SESSION_H_ |
16 | 16 |
17 #include <map> | 17 #include <map> |
18 #include <set> | 18 #include <set> |
19 #include <string> | 19 #include <string> |
20 #include <utility> | 20 #include <utility> |
21 #include <vector> | 21 #include <vector> |
22 | 22 |
23 #include "base/basictypes.h" | 23 #include "base/basictypes.h" |
24 #include "base/memory/scoped_ptr.h" | 24 #include "base/memory/scoped_ptr.h" |
25 #include "base/time.h" | 25 #include "base/time.h" |
26 #include "sync/internal_api/public/base/model_type.h" | 26 #include "sync/internal_api/public/base/model_type.h" |
27 #include "sync/internal_api/public/engine/model_safe_worker.h" | 27 #include "sync/internal_api/public/engine/model_safe_worker.h" |
28 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" | 28 #include "sync/internal_api/public/sessions/sync_session_snapshot.h" |
29 #include "sync/sessions/ordered_commit_set.h" | 29 #include "sync/sessions/ordered_commit_set.h" |
30 #include "sync/sessions/session_state.h" | |
31 #include "sync/sessions/status_controller.h" | 30 #include "sync/sessions/status_controller.h" |
32 #include "sync/sessions/sync_session_context.h" | 31 #include "sync/sessions/sync_session_context.h" |
33 #include "sync/util/extensions_activity_monitor.h" | 32 #include "sync/util/extensions_activity_monitor.h" |
34 | 33 |
35 namespace syncer { | 34 namespace syncer { |
36 class ModelSafeWorker; | 35 class ModelSafeWorker; |
37 | 36 |
38 namespace syncable { | 37 namespace syncable { |
39 class WriteTransaction; | 38 class WriteTransaction; |
40 } | 39 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 const ModelSafeRoutingInfo& routing_info, | 100 const ModelSafeRoutingInfo& routing_info, |
102 const std::vector<ModelSafeWorker*>& workers); | 101 const std::vector<ModelSafeWorker*>& workers); |
103 ~SyncSession(); | 102 ~SyncSession(); |
104 | 103 |
105 // Builds a thread-safe and read-only copy of the current session state. | 104 // Builds a thread-safe and read-only copy of the current session state. |
106 SyncSessionSnapshot TakeSnapshot() const; | 105 SyncSessionSnapshot TakeSnapshot() const; |
107 | 106 |
108 // Builds and sends a snapshot to the session context's listeners. | 107 // Builds and sends a snapshot to the session context's listeners. |
109 void SendEventNotification(SyncEngineEvent::EventCause cause); | 108 void SendEventNotification(SyncEngineEvent::EventCause cause); |
110 | 109 |
111 // Returns true if this session contains data that should go through the sync | |
112 // engine again. | |
113 bool HasMoreToSync() const; | |
114 | |
115 // Returns true if we completely ran the session without errors. | 110 // Returns true if we completely ran the session without errors. |
116 // | 111 // |
117 // There are many errors that could prevent a sync cycle from succeeding. | 112 // There are many errors that could prevent a sync cycle from succeeding. |
118 // These include invalid local state, inability to contact the server, | 113 // These include invalid local state, inability to contact the server, |
119 // inability to authenticate with the server, and server errors. What they | 114 // inability to authenticate with the server, and server errors. What they |
120 // have in common is that the we either need to take some action and then | 115 // have in common is that the we either need to take some action and then |
121 // retry the sync cycle or, in the case of transient errors, retry after some | 116 // retry the sync cycle or, in the case of transient errors, retry after some |
122 // backoff timer has expired. Most importantly, the SyncScheduler should not | 117 // backoff timer has expired. Most importantly, the SyncScheduler should not |
123 // assume that the original action that triggered the sync cycle (ie. a nudge | 118 // assume that the original action that triggered the sync cycle (ie. a nudge |
124 // or a notification) has been properly serviced. | 119 // or a notification) has been properly serviced. |
125 // | 120 // |
126 // This function also returns false if SyncShare has not been called on this | 121 // This function also returns false if SyncShare has not been called on this |
127 // session yet, or if ResetTransientState() has been called on this session | 122 // session yet. |
128 // since the last call to SyncShare. | |
129 bool Succeeded() const; | 123 bool Succeeded() const; |
130 | 124 |
131 // Returns true if we reached the server successfully and the server did not | 125 // Returns true if we reached the server successfully and the server did not |
132 // return any error codes. Returns false if no connection was attempted. | 126 // return any error codes. Returns false if no connection was attempted. |
133 bool SuccessfullyReachedServer() const; | 127 bool SuccessfullyReachedServer() const; |
134 | 128 |
135 // Collects all state pertaining to how and why |s| originated and unions it | 129 // Collects all state pertaining to how and why |s| originated and unions it |
136 // with corresponding state in |this|, leaving |s| unchanged. Allows |this| | 130 // with corresponding state in |this|, leaving |s| unchanged. Allows |this| |
137 // to take on the responsibilities |s| had (e.g. certain data types) in the | 131 // to take on the responsibilities |s| had (e.g. certain data types) in the |
138 // next SyncShare operation using |this|, rather than needed two separate | 132 // next SyncShare operation using |this|, rather than needed two separate |
139 // sessions. | 133 // sessions. |
140 void Coalesce(const SyncSession& session); | 134 void Coalesce(const SyncSession& session); |
141 | 135 |
142 // Compares the routing_info_, workers and payload map with the passed in | 136 // Compares the routing_info_, workers and payload map with the passed in |
143 // session. Purges types from the above 3 which are not in session. Useful | 137 // session. Purges types from the above 3 which are not in session. Useful |
144 // to update the sync session when the user has disabled some types from | 138 // to update the sync session when the user has disabled some types from |
145 // syncing. | 139 // syncing. |
146 void RebaseRoutingInfoWithLatest(const SyncSession& session); | 140 void RebaseRoutingInfoWithLatest(const SyncSession& session); |
147 | 141 |
148 // Should be called any time |this| is being re-used in a new call to | |
149 // SyncShare (e.g., HasMoreToSync returned true). | |
150 void PrepareForAnotherSyncCycle(); | |
151 | |
152 // TODO(akalin): Split this into context() and mutable_context(). | 142 // TODO(akalin): Split this into context() and mutable_context(). |
153 SyncSessionContext* context() const { return context_; } | 143 SyncSessionContext* context() const { return context_; } |
154 Delegate* delegate() const { return delegate_; } | 144 Delegate* delegate() const { return delegate_; } |
155 syncable::WriteTransaction* write_transaction() { return write_transaction_; } | 145 syncable::WriteTransaction* write_transaction() { return write_transaction_; } |
156 const StatusController& status_controller() const { | 146 const StatusController& status_controller() const { |
157 return *status_controller_.get(); | 147 return *status_controller_.get(); |
158 } | 148 } |
159 StatusController* mutable_status_controller() { | 149 StatusController* mutable_status_controller() { |
160 return status_controller_.get(); | 150 return status_controller_.get(); |
161 } | 151 } |
162 | 152 |
163 const ExtensionsActivityMonitor::Records& extensions_activity() const { | 153 const ExtensionsActivityMonitor::Records& extensions_activity() const { |
164 return extensions_activity_; | 154 return extensions_activity_; |
165 } | 155 } |
166 ExtensionsActivityMonitor::Records* mutable_extensions_activity() { | 156 ExtensionsActivityMonitor::Records* mutable_extensions_activity() { |
167 return &extensions_activity_; | 157 return &extensions_activity_; |
168 } | 158 } |
169 | 159 |
170 const std::vector<ModelSafeWorker*>& workers() const { return workers_; } | 160 const std::vector<ModelSafeWorker*>& workers() const { return workers_; } |
171 const ModelSafeRoutingInfo& routing_info() const { return routing_info_; } | 161 const ModelSafeRoutingInfo& routing_info() const { return routing_info_; } |
172 const SyncSourceInfo& source() const { return source_; } | 162 const SyncSourceInfo& source() const { return source_; } |
173 | 163 |
174 // Returns the set of groups which have enabled types. | 164 // Returns the set of groups which have enabled types. |
175 const std::set<ModelSafeGroup>& GetEnabledGroups() const; | 165 const std::set<ModelSafeGroup>& GetEnabledGroups() const; |
176 | 166 |
177 // Returns the set of enabled groups that have conflicts. | |
178 std::set<ModelSafeGroup> GetEnabledGroupsWithConflicts() const; | |
179 | |
180 // Mark the session has having finished all the sync steps it needed. | 167 // Mark the session has having finished all the sync steps it needed. |
181 void SetFinished(); | 168 void SetFinished(); |
182 | 169 |
183 private: | 170 private: |
184 // Extend the encapsulation boundary to utilities for internal member | 171 // Extend the encapsulation boundary to utilities for internal member |
185 // assignments. This way, the scope of these actions is explicit, they can't | 172 // assignments. This way, the scope of these actions is explicit, they can't |
186 // be overridden, and assigning is always accompanied by unassigning. | 173 // be overridden, and assigning is always accompanied by unassigning. |
187 friend class ScopedSetSessionWriteTransaction; | 174 friend class ScopedSetSessionWriteTransaction; |
188 | 175 |
189 // The context for this session, guaranteed to outlive |this|. | 176 // The context for this session, guaranteed to outlive |this|. |
(...skipping 20 matching lines...) Expand all Loading... |
210 | 197 |
211 // The routing info for the duration of this session, dictating which | 198 // The routing info for the duration of this session, dictating which |
212 // datatypes should be synced and which workers should be used when working | 199 // datatypes should be synced and which workers should be used when working |
213 // on those datatypes. | 200 // on those datatypes. |
214 ModelSafeRoutingInfo routing_info_; | 201 ModelSafeRoutingInfo routing_info_; |
215 | 202 |
216 // The set of groups with enabled types. Computed from | 203 // The set of groups with enabled types. Computed from |
217 // |routing_info_|. | 204 // |routing_info_|. |
218 std::set<ModelSafeGroup> enabled_groups_; | 205 std::set<ModelSafeGroup> enabled_groups_; |
219 | 206 |
220 // Whether this session has reached its last step or not. Gets reset on each | 207 // Whether this session has reached its last step or not. |
221 // new cycle (via PrepareForAnotherSyncCycle). | |
222 bool finished_; | 208 bool finished_; |
223 | 209 |
224 DISALLOW_COPY_AND_ASSIGN(SyncSession); | 210 DISALLOW_COPY_AND_ASSIGN(SyncSession); |
225 }; | 211 }; |
226 | 212 |
227 // Installs a WriteTransaction to a given session and later clears it when the | 213 // Installs a WriteTransaction to a given session and later clears it when the |
228 // utility falls out of scope. Transactions are not nestable, so it is an error | 214 // utility falls out of scope. Transactions are not nestable, so it is an error |
229 // to try and use one of these if the session already has a transaction. | 215 // to try and use one of these if the session already has a transaction. |
230 class ScopedSetSessionWriteTransaction { | 216 class ScopedSetSessionWriteTransaction { |
231 public: | 217 public: |
232 ScopedSetSessionWriteTransaction(SyncSession* session, | 218 ScopedSetSessionWriteTransaction(SyncSession* session, |
233 syncable::WriteTransaction* trans) | 219 syncable::WriteTransaction* trans) |
234 : session_(session) { | 220 : session_(session) { |
235 DCHECK(!session_->write_transaction_); | 221 DCHECK(!session_->write_transaction_); |
236 session_->write_transaction_ = trans; | 222 session_->write_transaction_ = trans; |
237 } | 223 } |
238 ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; } | 224 ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; } |
239 | 225 |
240 private: | 226 private: |
241 SyncSession* session_; | 227 SyncSession* session_; |
242 DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction); | 228 DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction); |
243 }; | 229 }; |
244 | 230 |
245 } // namespace sessions | 231 } // namespace sessions |
246 } // namespace syncer | 232 } // namespace syncer |
247 | 233 |
248 #endif // SYNC_SESSIONS_SYNC_SESSION_H_ | 234 #endif // SYNC_SESSIONS_SYNC_SESSION_H_ |
OLD | NEW |