Upstream version 5.34.104.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/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"
30
31 namespace syncer {
32 class ModelSafeWorker;
33
34 namespace sessions {
35
36 class NudgeTracker;
37
38 class SYNC_EXPORT_PRIVATE SyncSession {
39  public:
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 {
44    public:
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;
48
49     // Some of the client's types were throttled.
50     virtual void OnTypesThrottled(
51         ModelTypeSet types,
52         const base::TimeDelta& throttle_duration) = 0;
53
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;
64
65     // The client has been instructed to change its short poll interval.
66     virtual void OnReceivedShortPollIntervalUpdate(
67         const base::TimeDelta& new_interval) = 0;
68
69     // The client has been instructed to change its long poll interval.
70     virtual void OnReceivedLongPollIntervalUpdate(
71         const base::TimeDelta& new_interval) = 0;
72
73     // The client has been instructed to change its sessions commit
74     // delay.
75     virtual void OnReceivedSessionsCommitDelay(
76         const base::TimeDelta& new_delay) = 0;
77
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;
81
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;
85
86     // Called when server wants to schedule a retry GU.
87     virtual void OnReceivedGuRetryDelay(const base::TimeDelta& delay) = 0;
88
89     // Called when server requests a migration.
90     virtual void OnReceivedMigrationRequest(ModelTypeSet types) = 0;
91
92    protected:
93     virtual ~Delegate() {}
94   };
95
96   // Build a session without a nudge tracker.  Used for poll or configure type
97   // sync cycles.
98   static SyncSession* Build(SyncSessionContext* context,
99                             Delegate* delegate);
100   ~SyncSession();
101
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)
106       const;
107
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);
112
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();
118   }
119   StatusController* mutable_status_controller() {
120     return status_controller_.get();
121   }
122
123  private:
124   SyncSession(SyncSessionContext* context, Delegate* delegate);
125
126   // The context for this session, guaranteed to outlive |this|.
127   SyncSessionContext* const context_;
128
129   // The delegate for this session, must never be NULL.
130   Delegate* const delegate_;
131
132   // Our controller for various status and error counters.
133   scoped_ptr<StatusController> status_controller_;
134
135   DISALLOW_COPY_AND_ASSIGN(SyncSession);
136 };
137
138 }  // namespace sessions
139 }  // namespace syncer
140
141 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_