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 // 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 | |
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 | |
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 | |
11 // server updates, and access the SyncSessionContext for the current session | |
12 // via SyncSession instances. | |
13 | |
14 #ifndef CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_ | |
15 #define CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_ | |
16 #pragma once | |
17 | |
18 #include <map> | |
19 #include <set> | |
20 #include <string> | |
21 #include <utility> | |
22 #include <vector> | |
23 | |
24 #include "base/basictypes.h" | |
25 #include "base/memory/scoped_ptr.h" | |
26 #include "base/time.h" | |
27 #include "chrome/browser/sync/engine/model_safe_worker.h" | |
28 #include "chrome/browser/sync/sessions/ordered_commit_set.h" | |
29 #include "chrome/browser/sync/sessions/session_state.h" | |
30 #include "chrome/browser/sync/sessions/status_controller.h" | |
31 #include "chrome/browser/sync/sessions/sync_session_context.h" | |
32 #include "chrome/browser/sync/syncable/model_type.h" | |
33 #include "chrome/browser/sync/util/extensions_activity_monitor.h" | |
34 | |
35 namespace syncable { | |
36 class WriteTransaction; | |
37 } | |
38 | |
39 namespace browser_sync { | |
40 class ModelSafeWorker; | |
41 | |
42 namespace sessions { | |
43 | |
44 class SyncSession { | |
45 public: | |
46 // The Delegate services events that occur during the session requiring an | |
47 // explicit (and session-global) action, as opposed to events that are simply | |
48 // recorded in per-session state. | |
49 class Delegate { | |
50 public: | |
51 // The client was throttled and should cease-and-desist syncing activity | |
52 // until the specified time. | |
53 virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) = 0; | |
54 | |
55 // Silenced intervals can be out of phase with individual sessions, so the | |
56 // delegate is the only thing that can give an authoritative answer for | |
57 // "is syncing silenced right now". This shouldn't be necessary very often | |
58 // as the delegate ensures no session is started if syncing is silenced. | |
59 // ** Note ** This will return true if silencing commenced during this | |
60 // session and the interval has not yet elapsed, but the contract here is | |
61 // solely based on absolute time values. So, this cannot be used to infer | |
62 // that any given session _instance_ is silenced. An example of reasonable | |
63 // use is for UI reporting. | |
64 virtual bool IsSyncingCurrentlySilenced() = 0; | |
65 | |
66 // The client has been instructed to change its short poll interval. | |
67 virtual void OnReceivedShortPollIntervalUpdate( | |
68 const base::TimeDelta& new_interval) = 0; | |
69 | |
70 // The client has been instructed to change its long poll interval. | |
71 virtual void OnReceivedLongPollIntervalUpdate( | |
72 const base::TimeDelta& new_interval) = 0; | |
73 | |
74 // The client has been instructed to change its sessions commit | |
75 // delay. | |
76 virtual void OnReceivedSessionsCommitDelay( | |
77 const base::TimeDelta& new_delay) = 0; | |
78 | |
79 // The client needs to cease and desist syncing at once. This occurs when | |
80 // the Syncer detects that the backend store has fundamentally changed or | |
81 // is a different instance altogether (e.g. swapping from a test instance | |
82 // to production, or a global stop syncing operation has wiped the store). | |
83 // TODO(lipalani) : Replace this function with the one below. This function | |
84 // stops the current sync cycle and purges the client. In the new model | |
85 // the former would be done by the |SyncProtocolError| and | |
86 // the latter(which is an action) would be done in ProfileSyncService | |
87 // along with the rest of the actions. | |
88 virtual void OnShouldStopSyncingPermanently() = 0; | |
89 | |
90 // Called for the syncer to respond to the error sent by the server. | |
91 virtual void OnSyncProtocolError( | |
92 const sessions::SyncSessionSnapshot& snapshot) = 0; | |
93 | |
94 protected: | |
95 virtual ~Delegate() {} | |
96 }; | |
97 | |
98 SyncSession(SyncSessionContext* context, | |
99 Delegate* delegate, | |
100 const SyncSourceInfo& source, | |
101 const ModelSafeRoutingInfo& routing_info, | |
102 const std::vector<ModelSafeWorker*>& workers); | |
103 ~SyncSession(); | |
104 | |
105 // Builds a thread-safe and read-only copy of the current session state. | |
106 SyncSessionSnapshot TakeSnapshot() const; | |
107 | |
108 // Builds and sends a snapshot to the session context's listeners. | |
109 void SendEventNotification(SyncEngineEvent::EventCause cause); | |
110 | |
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 there we did not detect any errors in this session. | |
116 // | |
117 // There are many errors that could prevent a sync cycle from succeeding. | |
118 // These include invalid local state, inability to contact the server, | |
119 // 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 | |
121 // 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 | |
123 // assume that the original action that triggered the sync cycle (ie. a nudge | |
124 // or a notification) has been properly serviced. | |
125 // | |
126 // 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 | |
128 // since the last call to SyncShare. | |
129 bool Succeeded() const; | |
130 | |
131 // Collects all state pertaining to how and why |s| originated and unions it | |
132 // with corresponding state in |this|, leaving |s| unchanged. Allows |this| | |
133 // to take on the responsibilities |s| had (e.g. certain data types) in the | |
134 // next SyncShare operation using |this|, rather than needed two separate | |
135 // sessions. | |
136 void Coalesce(const SyncSession& session); | |
137 | |
138 // Compares the routing_info_, workers and payload map with the passed in | |
139 // session. Purges types from the above 3 which are not in session. Useful | |
140 // to update the sync session when the user has disabled some types from | |
141 // syncing. | |
142 void RebaseRoutingInfoWithLatest(const SyncSession& session); | |
143 | |
144 // Should be called any time |this| is being re-used in a new call to | |
145 // SyncShare (e.g., HasMoreToSync returned true). | |
146 void PrepareForAnotherSyncCycle(); | |
147 | |
148 // TODO(akalin): Split this into context() and mutable_context(). | |
149 SyncSessionContext* context() const { return context_; } | |
150 Delegate* delegate() const { return delegate_; } | |
151 syncable::WriteTransaction* write_transaction() { return write_transaction_; } | |
152 const StatusController& status_controller() const { | |
153 return *status_controller_.get(); | |
154 } | |
155 StatusController* mutable_status_controller() { | |
156 return status_controller_.get(); | |
157 } | |
158 | |
159 const ExtensionsActivityMonitor::Records& extensions_activity() const { | |
160 return extensions_activity_; | |
161 } | |
162 ExtensionsActivityMonitor::Records* mutable_extensions_activity() { | |
163 return &extensions_activity_; | |
164 } | |
165 | |
166 const std::vector<ModelSafeWorker*>& workers() const { return workers_; } | |
167 const ModelSafeRoutingInfo& routing_info() const { return routing_info_; } | |
168 const SyncSourceInfo& source() const { return source_; } | |
169 | |
170 // Returns the set of groups which have enabled types. | |
171 const std::set<ModelSafeGroup>& GetEnabledGroups() const; | |
172 | |
173 // Returns the set of enabled groups that have conflicts. | |
174 std::set<ModelSafeGroup> GetEnabledGroupsWithConflicts() const; | |
175 | |
176 // Returns the set of enabled groups that have verified updates. | |
177 std::set<ModelSafeGroup> GetEnabledGroupsWithVerifiedUpdates() const; | |
178 | |
179 private: | |
180 // Extend the encapsulation boundary to utilities for internal member | |
181 // assignments. This way, the scope of these actions is explicit, they can't | |
182 // be overridden, and assigning is always accompanied by unassigning. | |
183 friend class ScopedSetSessionWriteTransaction; | |
184 | |
185 // The context for this session, guaranteed to outlive |this|. | |
186 SyncSessionContext* const context_; | |
187 | |
188 // The source for initiating this sync session. | |
189 SyncSourceInfo source_; | |
190 | |
191 // Information about extensions activity since the last successful commit. | |
192 ExtensionsActivityMonitor::Records extensions_activity_; | |
193 | |
194 // Used to allow various steps to share a transaction. Can be NULL. | |
195 syncable::WriteTransaction* write_transaction_; | |
196 | |
197 // The delegate for this session, must never be NULL. | |
198 Delegate* const delegate_; | |
199 | |
200 // Our controller for various status and error counters. | |
201 scoped_ptr<StatusController> status_controller_; | |
202 | |
203 // The set of active ModelSafeWorkers for the duration of this session. | |
204 // This can change if this session is Coalesce()'d with another. | |
205 std::vector<ModelSafeWorker*> workers_; | |
206 | |
207 // The routing info for the duration of this session, dictating which | |
208 // datatypes should be synced and which workers should be used when working | |
209 // on those datatypes. | |
210 ModelSafeRoutingInfo routing_info_; | |
211 | |
212 // The set of groups with enabled types. Computed from | |
213 // |routing_info_|. | |
214 std::set<ModelSafeGroup> enabled_groups_; | |
215 | |
216 DISALLOW_COPY_AND_ASSIGN(SyncSession); | |
217 }; | |
218 | |
219 // Installs a WriteTransaction to a given session and later clears it when the | |
220 // utility falls out of scope. Transactions are not nestable, so it is an error | |
221 // to try and use one of these if the session already has a transaction. | |
222 class ScopedSetSessionWriteTransaction { | |
223 public: | |
224 ScopedSetSessionWriteTransaction(SyncSession* session, | |
225 syncable::WriteTransaction* trans) | |
226 : session_(session) { | |
227 DCHECK(!session_->write_transaction_); | |
228 session_->write_transaction_ = trans; | |
229 } | |
230 ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; } | |
231 | |
232 private: | |
233 SyncSession* session_; | |
234 DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction); | |
235 }; | |
236 | |
237 } // namespace sessions | |
238 } // namespace browser_sync | |
239 | |
240 #endif // CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_ | |
OLD | NEW |