sync_session.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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
31namespace syncer {
32class ModelSafeWorker;
33class ProtocolEvent;
34
35namespace sessions {
36
37class NudgeTracker;
38
39class SYNC_EXPORT_PRIVATE SyncSession {
40 public:
41  // The Delegate services events that occur during the session requiring an
42  // explicit (and session-global) action, as opposed to events that are simply
43  // recorded in per-session state.
44  class SYNC_EXPORT_PRIVATE Delegate {
45   public:
46    // The client was throttled and should cease-and-desist syncing activity
47    // until the specified time.
48    virtual void OnThrottled(const base::TimeDelta& throttle_duration) = 0;
49
50    // Some of the client's types were throttled.
51    virtual void OnTypesThrottled(
52        ModelTypeSet types,
53        const base::TimeDelta& throttle_duration) = 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 IsCurrentlyThrottled() = 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 a nudge delay.
75    virtual void OnReceivedCustomNudgeDelays(
76        const std::map<ModelType, base::TimeDelta>& nudge_delays) = 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  void SendProtocolEvent(const ProtocolEvent& event);
114
115  // TODO(akalin): Split this into context() and mutable_context().
116  SyncSessionContext* context() const { return context_; }
117  Delegate* delegate() const { return delegate_; }
118  const StatusController& status_controller() const {
119    return *status_controller_.get();
120  }
121  StatusController* mutable_status_controller() {
122    return status_controller_.get();
123  }
124
125 private:
126  SyncSession(SyncSessionContext* context, Delegate* delegate);
127
128  // The context for this session, guaranteed to outlive |this|.
129  SyncSessionContext* const context_;
130
131  // The delegate for this session, must never be NULL.
132  Delegate* const delegate_;
133
134  // Our controller for various status and error counters.
135  scoped_ptr<StatusController> status_controller_;
136
137  DISALLOW_COPY_AND_ASSIGN(SyncSession);
138};
139
140}  // namespace sessions
141}  // namespace syncer
142
143#endif  // SYNC_SESSIONS_SYNC_SESSION_H_
144