sync_session.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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 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 SYNC_SESSIONS_SYNC_SESSION_H_
15#define SYNC_SESSIONS_SYNC_SESSION_H_
16
17#include <map>
18#include <set>
19#include <string>
20#include <utility>
21#include <vector>
22
23#include "base/basictypes.h"
24#include "base/memory/scoped_ptr.h"
25#include "base/time.h"
26#include "sync/base/sync_export.h"
27#include "sync/internal_api/public/base/model_type.h"
28#include "sync/internal_api/public/engine/model_safe_worker.h"
29#include "sync/internal_api/public/sessions/sync_session_snapshot.h"
30#include "sync/sessions/ordered_commit_set.h"
31#include "sync/sessions/status_controller.h"
32#include "sync/sessions/sync_session_context.h"
33
34namespace syncer {
35class ModelSafeWorker;
36
37namespace sessions {
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 OnSilencedUntil(const base::TimeTicks& silenced_until) = 0;
49
50    // Silenced intervals can be out of phase with individual sessions, so the
51    // delegate is the only thing that can give an authoritative answer for
52    // "is syncing silenced right now". This shouldn't be necessary very often
53    // as the delegate ensures no session is started if syncing is silenced.
54    // ** Note **  This will return true if silencing commenced during this
55    // session and the interval has not yet elapsed, but the contract here is
56    // solely based on absolute time values. So, this cannot be used to infer
57    // that any given session _instance_ is silenced.  An example of reasonable
58    // use is for UI reporting.
59    virtual bool IsSyncingCurrentlySilenced() = 0;
60
61    // The client has been instructed to change its short poll interval.
62    virtual void OnReceivedShortPollIntervalUpdate(
63        const base::TimeDelta& new_interval) = 0;
64
65    // The client has been instructed to change its long poll interval.
66    virtual void OnReceivedLongPollIntervalUpdate(
67        const base::TimeDelta& new_interval) = 0;
68
69    // The client has been instructed to change its sessions commit
70    // delay.
71    virtual void OnReceivedSessionsCommitDelay(
72        const base::TimeDelta& new_delay) = 0;
73
74    // The client needs to cease and desist syncing at once.  This occurs when
75    // the Syncer detects that the backend store has fundamentally changed or
76    // is a different instance altogether (e.g. swapping from a test instance
77    // to production, or a global stop syncing operation has wiped the store).
78    // TODO(lipalani) : Replace this function with the one below. This function
79    // stops the current sync cycle and purges the client. In the new model
80    // the former would be done by the |SyncProtocolError| and
81    // the latter(which is an action) would be done in ProfileSyncService
82    // along with the rest of the actions.
83    virtual void OnShouldStopSyncingPermanently() = 0;
84
85    // Called for the syncer to respond to the error sent by the server.
86    virtual void OnSyncProtocolError(
87        const sessions::SyncSessionSnapshot& snapshot) = 0;
88
89   protected:
90    virtual ~Delegate() {}
91  };
92
93  SyncSession(SyncSessionContext* context,
94              Delegate* delegate,
95              const SyncSourceInfo& source);
96  ~SyncSession();
97
98  // Builds a thread-safe and read-only copy of the current session state.
99  SyncSessionSnapshot TakeSnapshot() const;
100
101  // Builds and sends a snapshot to the session context's listeners.
102  void SendEventNotification(SyncEngineEvent::EventCause cause);
103
104  // TODO(akalin): Split this into context() and mutable_context().
105  SyncSessionContext* context() const { return context_; }
106  Delegate* delegate() const { return delegate_; }
107  const StatusController& status_controller() const {
108    return *status_controller_.get();
109  }
110  StatusController* mutable_status_controller() {
111    return status_controller_.get();
112  }
113
114  const SyncSourceInfo& source() const { return source_; }
115
116 private:
117  // The context for this session, guaranteed to outlive |this|.
118  SyncSessionContext* const context_;
119
120  // The source for initiating this sync session.
121  SyncSourceInfo source_;
122
123  // The delegate for this session, must never be NULL.
124  Delegate* const delegate_;
125
126  // Our controller for various status and error counters.
127  scoped_ptr<StatusController> status_controller_;
128
129  DISALLOW_COPY_AND_ASSIGN(SyncSession);
130};
131
132}  // namespace sessions
133}  // namespace syncer
134
135#endif  // SYNC_SESSIONS_SYNC_SESSION_H_
136