Home | History | Annotate | Download | only in sessions
      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/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 
     34 namespace syncer {
     35 class ModelSafeWorker;
     36 
     37 namespace sessions {
     38 
     39 class NudgeTracker;
     40 
     41 class SYNC_EXPORT_PRIVATE SyncSession {
     42  public:
     43   // The Delegate services events that occur during the session requiring an
     44   // explicit (and session-global) action, as opposed to events that are simply
     45   // recorded in per-session state.
     46   class SYNC_EXPORT_PRIVATE Delegate {
     47    public:
     48     // The client was throttled and should cease-and-desist syncing activity
     49     // until the specified time.
     50     virtual void OnThrottled(const base::TimeDelta& throttle_duration) = 0;
     51 
     52     // Some of the client's types were throttled.
     53     virtual void OnTypesThrottled(
     54         ModelTypeSet types,
     55         const base::TimeDelta& throttle_duration) = 0;
     56 
     57     // Silenced intervals can be out of phase with individual sessions, so the
     58     // delegate is the only thing that can give an authoritative answer for
     59     // "is syncing silenced right now". This shouldn't be necessary very often
     60     // as the delegate ensures no session is started if syncing is silenced.
     61     // ** Note **  This will return true if silencing commenced during this
     62     // session and the interval has not yet elapsed, but the contract here is
     63     // solely based on absolute time values. So, this cannot be used to infer
     64     // that any given session _instance_ is silenced.  An example of reasonable
     65     // use is for UI reporting.
     66     virtual bool IsCurrentlyThrottled() = 0;
     67 
     68     // The client has been instructed to change its short poll interval.
     69     virtual void OnReceivedShortPollIntervalUpdate(
     70         const base::TimeDelta& new_interval) = 0;
     71 
     72     // The client has been instructed to change its long poll interval.
     73     virtual void OnReceivedLongPollIntervalUpdate(
     74         const base::TimeDelta& new_interval) = 0;
     75 
     76     // The client has been instructed to change its sessions commit
     77     // delay.
     78     virtual void OnReceivedSessionsCommitDelay(
     79         const base::TimeDelta& new_delay) = 0;
     80 
     81     // The client needs to cease and desist syncing at once.  This occurs when
     82     // the Syncer detects that the backend store has fundamentally changed or
     83     // is a different instance altogether (e.g. swapping from a test instance
     84     // to production, or a global stop syncing operation has wiped the store).
     85     // TODO(lipalani) : Replace this function with the one below. This function
     86     // stops the current sync cycle and purges the client. In the new model
     87     // the former would be done by the |SyncProtocolError| and
     88     // the latter(which is an action) would be done in ProfileSyncService
     89     // along with the rest of the actions.
     90     virtual void OnShouldStopSyncingPermanently() = 0;
     91 
     92     // Called for the syncer to respond to the error sent by the server.
     93     virtual void OnSyncProtocolError(
     94         const sessions::SyncSessionSnapshot& snapshot) = 0;
     95 
     96     // Called when the server wants to change the number of hints the client
     97     // will buffer locally.
     98     virtual void OnReceivedClientInvalidationHintBufferSize(int size) = 0;
     99 
    100    protected:
    101     virtual ~Delegate() {}
    102   };
    103 
    104   // Build a session without a nudge tracker.  Used for poll or configure type
    105   // sync cycles.
    106   static SyncSession* Build(SyncSessionContext* context,
    107                             Delegate* delegate);
    108   ~SyncSession();
    109 
    110   // Builds a thread-safe and read-only copy of the current session state.
    111   SyncSessionSnapshot TakeSnapshot() const;
    112   SyncSessionSnapshot TakeSnapshotWithSource(
    113       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source)
    114       const;
    115 
    116   // Builds and sends a snapshot to the session context's listeners.
    117   void SendSyncCycleEndEventNotification(
    118       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
    119   void SendEventNotification(SyncEngineEvent::EventCause cause);
    120 
    121   // TODO(akalin): Split this into context() and mutable_context().
    122   SyncSessionContext* context() const { return context_; }
    123   Delegate* delegate() const { return delegate_; }
    124   const StatusController& status_controller() const {
    125     return *status_controller_.get();
    126   }
    127   StatusController* mutable_status_controller() {
    128     return status_controller_.get();
    129   }
    130 
    131  private:
    132   SyncSession(SyncSessionContext* context, Delegate* delegate);
    133 
    134   // The context for this session, guaranteed to outlive |this|.
    135   SyncSessionContext* const context_;
    136 
    137   // The delegate for this session, must never be NULL.
    138   Delegate* const delegate_;
    139 
    140   // Our controller for various status and error counters.
    141   scoped_ptr<StatusController> status_controller_;
    142 
    143   DISALLOW_COPY_AND_ASSIGN(SyncSession);
    144 };
    145 
    146 }  // namespace sessions
    147 }  // namespace syncer
    148 
    149 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_
    150