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
3 // found in the LICENSE file.
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 throughout the sync cycle. The SyncSession is not reused across
8 // sync cycles; each cycle starts with a new one.
10 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_
11 #define SYNC_SESSIONS_SYNC_SESSION_H_
19 #include "base/basictypes.h"
20 #include "base/memory/scoped_ptr.h"
21 #include "base/time/time.h"
22 #include "sync/base/sync_export.h"
23 #include "sync/engine/sync_cycle_event.h"
24 #include "sync/internal_api/public/base/model_type.h"
25 #include "sync/internal_api/public/engine/model_safe_worker.h"
26 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
27 #include "sync/protocol/sync_protocol_error.h"
28 #include "sync/sessions/status_controller.h"
29 #include "sync/sessions/sync_session_context.h"
32 class ModelSafeWorker;
38 class SYNC_EXPORT_PRIVATE SyncSession {
40 // The Delegate services events that occur during the session requiring an
41 // explicit (and session-global) action, as opposed to events that are simply
42 // recorded in per-session state.
43 class SYNC_EXPORT_PRIVATE Delegate {
45 // The client was throttled and should cease-and-desist syncing activity
46 // until the specified time.
47 virtual void OnThrottled(const base::TimeDelta& throttle_duration) = 0;
49 // Some of the client's types were throttled.
50 virtual void OnTypesThrottled(
52 const base::TimeDelta& throttle_duration) = 0;
54 // Silenced intervals can be out of phase with individual sessions, so the
55 // delegate is the only thing that can give an authoritative answer for
56 // "is syncing silenced right now". This shouldn't be necessary very often
57 // as the delegate ensures no session is started if syncing is silenced.
58 // ** Note ** This will return true if silencing commenced during this
59 // session and the interval has not yet elapsed, but the contract here is
60 // solely based on absolute time values. So, this cannot be used to infer
61 // that any given session _instance_ is silenced. An example of reasonable
62 // use is for UI reporting.
63 virtual bool IsCurrentlyThrottled() = 0;
65 // The client has been instructed to change its short poll interval.
66 virtual void OnReceivedShortPollIntervalUpdate(
67 const base::TimeDelta& new_interval) = 0;
69 // The client has been instructed to change its long poll interval.
70 virtual void OnReceivedLongPollIntervalUpdate(
71 const base::TimeDelta& new_interval) = 0;
73 // The client has been instructed to change its sessions commit
75 virtual void OnReceivedSessionsCommitDelay(
76 const base::TimeDelta& new_delay) = 0;
78 // Called for the syncer to respond to the error sent by the server.
79 virtual void OnSyncProtocolError(
80 const SyncProtocolError& sync_protocol_error) = 0;
82 // Called when the server wants to change the number of hints the client
83 // will buffer locally.
84 virtual void OnReceivedClientInvalidationHintBufferSize(int size) = 0;
86 // Called when server wants to schedule a retry GU.
87 virtual void OnReceivedGuRetryDelay(const base::TimeDelta& delay) = 0;
89 // Called when server requests a migration.
90 virtual void OnReceivedMigrationRequest(ModelTypeSet types) = 0;
93 virtual ~Delegate() {}
96 // Build a session without a nudge tracker. Used for poll or configure type
98 static SyncSession* Build(SyncSessionContext* context,
102 // Builds a thread-safe and read-only copy of the current session state.
103 SyncSessionSnapshot TakeSnapshot() const;
104 SyncSessionSnapshot TakeSnapshotWithSource(
105 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source)
108 // Builds and sends a snapshot to the session context's listeners.
109 void SendSyncCycleEndEventNotification(
110 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
111 void SendEventNotification(SyncCycleEvent::EventCause cause);
113 // TODO(akalin): Split this into context() and mutable_context().
114 SyncSessionContext* context() const { return context_; }
115 Delegate* delegate() const { return delegate_; }
116 const StatusController& status_controller() const {
117 return *status_controller_.get();
119 StatusController* mutable_status_controller() {
120 return status_controller_.get();
124 SyncSession(SyncSessionContext* context, Delegate* delegate);
126 // The context for this session, guaranteed to outlive |this|.
127 SyncSessionContext* const context_;
129 // The delegate for this session, must never be NULL.
130 Delegate* const delegate_;
132 // Our controller for various status and error counters.
133 scoped_ptr<StatusController> status_controller_;
135 DISALLOW_COPY_AND_ASSIGN(SyncSession);
138 } // namespace sessions
139 } // namespace syncer
141 #endif // SYNC_SESSIONS_SYNC_SESSION_H_