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