Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / sync / sessions / sync_session.h
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.
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 throughout the sync cycle.  The SyncSession is not reused across
8 // sync cycles; each cycle starts with a new one.
9
10 #ifndef SYNC_SESSIONS_SYNC_SESSION_H_
11 #define SYNC_SESSIONS_SYNC_SESSION_H_
12
13 #include <map>
14 #include <set>
15 #include <string>
16 #include <utility>
17 #include <vector>
18
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/internal_api/public/base/model_type.h"
24 #include "sync/internal_api/public/engine/model_safe_worker.h"
25 #include "sync/internal_api/public/sessions/sync_session_snapshot.h"
26 #include "sync/sessions/status_controller.h"
27 #include "sync/sessions/sync_session_context.h"
28
29 namespace syncer {
30 class ModelSafeWorker;
31
32 namespace sessions {
33
34 class NudgeTracker;
35
36 class SYNC_EXPORT_PRIVATE SyncSession {
37  public:
38   // The Delegate services events that occur during the session requiring an
39   // explicit (and session-global) action, as opposed to events that are simply
40   // recorded in per-session state.
41   class SYNC_EXPORT_PRIVATE Delegate {
42    public:
43     // The client was throttled and should cease-and-desist syncing activity
44     // until the specified time.
45     virtual void OnThrottled(const base::TimeDelta& throttle_duration) = 0;
46
47     // Some of the client's types were throttled.
48     virtual void OnTypesThrottled(
49         ModelTypeSet types,
50         const base::TimeDelta& throttle_duration) = 0;
51
52     // Silenced intervals can be out of phase with individual sessions, so the
53     // delegate is the only thing that can give an authoritative answer for
54     // "is syncing silenced right now". This shouldn't be necessary very often
55     // as the delegate ensures no session is started if syncing is silenced.
56     // ** Note **  This will return true if silencing commenced during this
57     // session and the interval has not yet elapsed, but the contract here is
58     // solely based on absolute time values. So, this cannot be used to infer
59     // that any given session _instance_ is silenced.  An example of reasonable
60     // use is for UI reporting.
61     virtual bool IsCurrentlyThrottled() = 0;
62
63     // The client has been instructed to change its short poll interval.
64     virtual void OnReceivedShortPollIntervalUpdate(
65         const base::TimeDelta& new_interval) = 0;
66
67     // The client has been instructed to change its long poll interval.
68     virtual void OnReceivedLongPollIntervalUpdate(
69         const base::TimeDelta& new_interval) = 0;
70
71     // The client has been instructed to change its sessions commit
72     // delay.
73     virtual void OnReceivedSessionsCommitDelay(
74         const base::TimeDelta& new_delay) = 0;
75
76     // Called for the syncer to respond to the error sent by the server.
77     virtual void OnSyncProtocolError(
78         const sessions::SyncSessionSnapshot& snapshot) = 0;
79
80     // Called when the server wants to change the number of hints the client
81     // will buffer locally.
82     virtual void OnReceivedClientInvalidationHintBufferSize(int size) = 0;
83
84     // Called when server wants to schedule a retry GU.
85     virtual void OnReceivedGuRetryDelay(const base::TimeDelta& delay) = 0;
86
87    protected:
88     virtual ~Delegate() {}
89   };
90
91   // Build a session without a nudge tracker.  Used for poll or configure type
92   // sync cycles.
93   static SyncSession* Build(SyncSessionContext* context,
94                             Delegate* delegate);
95   ~SyncSession();
96
97   // Builds a thread-safe and read-only copy of the current session state.
98   SyncSessionSnapshot TakeSnapshot() const;
99   SyncSessionSnapshot TakeSnapshotWithSource(
100       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source)
101       const;
102
103   // Builds and sends a snapshot to the session context's listeners.
104   void SendSyncCycleEndEventNotification(
105       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
106   void SendEventNotification(SyncEngineEvent::EventCause cause);
107
108   // TODO(akalin): Split this into context() and mutable_context().
109   SyncSessionContext* context() const { return context_; }
110   Delegate* delegate() const { return delegate_; }
111   const StatusController& status_controller() const {
112     return *status_controller_.get();
113   }
114   StatusController* mutable_status_controller() {
115     return status_controller_.get();
116   }
117
118  private:
119   SyncSession(SyncSessionContext* context, Delegate* delegate);
120
121   // The context for this session, guaranteed to outlive |this|.
122   SyncSessionContext* const context_;
123
124   // The delegate for this session, must never be NULL.
125   Delegate* const delegate_;
126
127   // Our controller for various status and error counters.
128   scoped_ptr<StatusController> status_controller_;
129
130   DISALLOW_COPY_AND_ASSIGN(SyncSession);
131 };
132
133 }  // namespace sessions
134 }  // namespace syncer
135
136 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_