sync_session.h revision 731df977c0511bca2206b5f333555b1205ff1f43
13345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Copyright (c) 2010 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// A class representing an attempt to synchronize the local syncable data
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// store with a sync server. A SyncSession instance is passed as a stateful
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// bundle to and from various SyncerCommands with the goal of converging the
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// client view of data with that of the server. The commands twiddle with
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// session status in response to events and hiccups along the way, set and
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// query session progress with regards to conflict resolution and applying
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// server updates, and access the SyncSessionContext for the current session
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// via SyncSession instances.
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_
163345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector>
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/basictypes.h"
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/scoped_ptr.h"
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/time.h"
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/sessions/ordered_commit_set.h"
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/sessions/session_state.h"
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/sessions/status_controller.h"
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/sessions/sync_session_context.h"
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/sync/util/extensions_activity_monitor.h"
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace syncable {
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass WriteTransaction;
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace browser_sync {
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass ModelSafeWorker;
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace sessions {
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SyncSession {
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The Delegate services events that occur during the session requiring an
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // explicit (and session-global) action, as opposed to events that are simply
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // recorded in per-session state.
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  class Delegate {
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch   public:
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // The client was throttled and should cease-and-desist syncing activity
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // until the specified time.
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual void OnSilencedUntil(const base::TimeTicks& silenced_until) = 0;
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // Silenced intervals can be out of phase with individual sessions, so the
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // delegate is the only thing that can give an authoritative answer for
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // "is syncing silenced right now". This shouldn't be necessary very often
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // as the delegate ensures no session is started if syncing is silenced.
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // ** Note **  This will return true if silencing commenced during this
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // session and the interval has not yet elapsed, but the contract here is
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // solely based on absolute time values. So, this cannot be used to infer
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // that any given session _instance_ is silenced.  An example of reasonable
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // use is for UI reporting.
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual bool IsSyncingCurrentlySilenced() = 0;
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // The client has been instructed to change its short poll interval.
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual void OnReceivedShortPollIntervalUpdate(
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch        const base::TimeDelta& new_interval) = 0;
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // The client has been instructed to change its long poll interval.
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual void OnReceivedLongPollIntervalUpdate(
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch        const base::TimeDelta& new_interval) = 0;
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // The client needs to cease and desist syncing at once.  This occurs when
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // the Syncer detects that the backend store has fundamentally changed or
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // is a different instance altogether (e.g. swapping from a test instance
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    // to production, or a global stop syncing operation has wiped the store).
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual void OnShouldStopSyncingPermanently() = 0;
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch   protected:
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    virtual ~Delegate() {}
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Creates a new SyncSession with mandatory context and delegate.
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SyncSession(SyncSessionContext* context, Delegate* delegate);
80731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  ~SyncSession();
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Builds a thread-safe and read-only copy of the current session state.
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SyncSessionSnapshot TakeSnapshot() const;
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if this session contains data that should go through the sync
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // engine again.
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool HasMoreToSync() const;
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SyncSessionContext* context() { return context_; }
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Delegate* delegate() { return delegate_; }
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  syncable::WriteTransaction* write_transaction() { return write_transaction_; }
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  StatusController* status_controller() { return status_controller_.get(); }
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const ExtensionsActivityMonitor::Records& extensions_activity() const {
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return extensions_activity_;
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ExtensionsActivityMonitor::Records* mutable_extensions_activity() {
98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return &extensions_activity_;
99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Volatile reader for the source member of the sync session object.  The
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // value is set to the SYNC_CYCLE_CONTINUATION value to signal that it has
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // been read.
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  sync_pb::GetUpdatesCallerInfo::GetUpdatesSource TestAndSetSource();
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void set_source(sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source) {
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    source_ = source;
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const std::vector<ModelSafeWorker*>& workers() const { return workers_; }
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const ModelSafeRoutingInfo& routing_info() const { return routing_info_; }
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Extend the encapsulation boundary to utilities for internal member
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // assignments. This way, the scope of these actions is explicit, they can't
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // be overridden, and assigning is always accompanied by unassigning.
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  friend class ScopedSetSessionWriteTransaction;
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The context for this session, guaranteed to outlive |this|.
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SyncSessionContext* const context_;
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The source for initiating this sync session.
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source_;
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Information about extensions activity since the last successful commit.
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ExtensionsActivityMonitor::Records extensions_activity_;
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Used to allow various steps to share a transaction. Can be NULL.
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  syncable::WriteTransaction* write_transaction_;
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The delegate for this session, must never be NULL.
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  Delegate* delegate_;
132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Our controller for various status and error counters.
134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  scoped_ptr<StatusController> status_controller_;
135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The set of active ModelSafeWorkers for the duration of this session.
137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const std::vector<ModelSafeWorker*> workers_;
138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The routing info for the duration of this session, dictating which
140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // datatypes should be synced and which workers should be used when working
141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // on those datatypes.
142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  const ModelSafeRoutingInfo routing_info_;
143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(SyncSession);
145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Installs a WriteTransaction to a given session and later clears it when the
148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// utility falls out of scope. Transactions are not nestable, so it is an error
149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// to try and use one of these if the session already has a transaction.
150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass ScopedSetSessionWriteTransaction {
151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ScopedSetSessionWriteTransaction(SyncSession* session,
153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                   syncable::WriteTransaction* trans)
154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      : session_(session) {
155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    DCHECK(!session_->write_transaction_);
156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    session_->write_transaction_ = trans;
157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ~ScopedSetSessionWriteTransaction() { session_->write_transaction_ = NULL; }
159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SyncSession* session_;
162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(ScopedSetSessionWriteTransaction);
163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace sessions
166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace browser_sync
167c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_SYNC_SESSIONS_SYNC_SESSION_H_
169