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/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 
     31 namespace syncer {
     32 class ModelSafeWorker;
     33 class ProtocolEvent;
     34 
     35 namespace sessions {
     36 
     37 class NudgeTracker;
     38 
     39 class 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 its sessions commit
     75     // delay.
     76     virtual void OnReceivedSessionsCommitDelay(
     77         const base::TimeDelta& new_delay) = 0;
     78 
     79     // Called for the syncer to respond to the error sent by the server.
     80     virtual void OnSyncProtocolError(
     81         const SyncProtocolError& sync_protocol_error) = 0;
     82 
     83     // Called when the server wants to change the number of hints the client
     84     // will buffer locally.
     85     virtual void OnReceivedClientInvalidationHintBufferSize(int size) = 0;
     86 
     87     // Called when server wants to schedule a retry GU.
     88     virtual void OnReceivedGuRetryDelay(const base::TimeDelta& delay) = 0;
     89 
     90     // Called when server requests a migration.
     91     virtual void OnReceivedMigrationRequest(ModelTypeSet types) = 0;
     92 
     93    protected:
     94     virtual ~Delegate() {}
     95   };
     96 
     97   // Build a session without a nudge tracker.  Used for poll or configure type
     98   // sync cycles.
     99   static SyncSession* Build(SyncSessionContext* context,
    100                             Delegate* delegate);
    101   ~SyncSession();
    102 
    103   // Builds a thread-safe and read-only copy of the current session state.
    104   SyncSessionSnapshot TakeSnapshot() const;
    105   SyncSessionSnapshot TakeSnapshotWithSource(
    106       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource legacy_updates_source)
    107       const;
    108 
    109   // Builds and sends a snapshot to the session context's listeners.
    110   void SendSyncCycleEndEventNotification(
    111       sync_pb::GetUpdatesCallerInfo::GetUpdatesSource source);
    112   void SendEventNotification(SyncCycleEvent::EventCause cause);
    113 
    114   void SendProtocolEvent(const ProtocolEvent& event);
    115 
    116   // TODO(akalin): Split this into context() and mutable_context().
    117   SyncSessionContext* context() const { return context_; }
    118   Delegate* delegate() const { return delegate_; }
    119   const StatusController& status_controller() const {
    120     return *status_controller_.get();
    121   }
    122   StatusController* mutable_status_controller() {
    123     return status_controller_.get();
    124   }
    125 
    126  private:
    127   SyncSession(SyncSessionContext* context, Delegate* delegate);
    128 
    129   // The context for this session, guaranteed to outlive |this|.
    130   SyncSessionContext* const context_;
    131 
    132   // The delegate for this session, must never be NULL.
    133   Delegate* const delegate_;
    134 
    135   // Our controller for various status and error counters.
    136   scoped_ptr<StatusController> status_controller_;
    137 
    138   DISALLOW_COPY_AND_ASSIGN(SyncSession);
    139 };
    140 
    141 }  // namespace sessions
    142 }  // namespace syncer
    143 
    144 #endif  // SYNC_SESSIONS_SYNC_SESSION_H_
    145