Home | History | Annotate | Download | only in sessions
      1 // Copyright 2013 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 #ifndef SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
      6 #define SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/scoped_vector.h"
     13 #include "base/time/time.h"
     14 #include "sync/internal_api/public/base/invalidation_interface.h"
     15 #include "sync/internal_api/public/base/model_type.h"
     16 #include "sync/protocol/sync.pb.h"
     17 
     18 namespace syncer {
     19 
     20 class InvalidationInterface;
     21 
     22 namespace sessions {
     23 
     24 // A class to track the per-type scheduling data.
     25 class DataTypeTracker {
     26  public:
     27   explicit DataTypeTracker();
     28   ~DataTypeTracker();
     29 
     30   // For STL compatibility, we do not forbid the creation of a default copy
     31   // constructor and assignment operator.
     32 
     33   // Tracks that a local change has been made to this type.
     34   // Returns the current local change nudge delay for this type.
     35   base::TimeDelta RecordLocalChange();
     36 
     37   // Tracks that a local refresh request has been made for this type.
     38   void RecordLocalRefreshRequest();
     39 
     40   // Tracks that we received invalidation notifications for this type.
     41   void RecordRemoteInvalidation(scoped_ptr<InvalidationInterface> incoming);
     42 
     43   // Takes note that initial sync is pending for this type.
     44   void RecordInitialSyncRequired();
     45 
     46   // Records that a sync cycle has been performed successfully.
     47   // Generally, this means that all local changes have been committed and all
     48   // remote changes have been downloaded, so we can clear any flags related to
     49   // pending work.
     50   void RecordSuccessfulSyncCycle();
     51 
     52   // Updates the size of the invalidations payload buffer.
     53   void UpdatePayloadBufferSize(size_t new_size);
     54 
     55   // Returns true if there is a good reason to perform a sync cycle.  This does
     56   // not take into account whether or not now is a good time to perform a sync
     57   // cycle.  That's for the scheduler to decide.
     58   bool IsSyncRequired() const;
     59 
     60   // Returns true if there is a good reason to fetch updates for this type as
     61   // part of the next sync cycle.
     62   bool IsGetUpdatesRequired() const;
     63 
     64   // Returns true if there is an uncommitted local change.
     65   bool HasLocalChangePending() const;
     66 
     67   // Returns true if we've received an invalidation since we last fetched
     68   // updates.
     69   bool HasPendingInvalidation() const;
     70 
     71   // Returns true if an explicit refresh request is still outstanding.
     72   bool HasRefreshRequestPending() const;
     73 
     74   // Returns true if this type is requesting an initial sync.
     75   bool IsInitialSyncRequired() const;
     76 
     77   // Fills in the legacy invalidaiton payload information fields.
     78   void SetLegacyNotificationHint(
     79       sync_pb::DataTypeProgressMarker* progress) const;
     80 
     81   // Fills some type-specific contents of a GetUpdates request protobuf.  These
     82   // messages provide the server with the information it needs to decide how to
     83   // handle a request.
     84   void FillGetUpdatesTriggersMessage(sync_pb::GetUpdateTriggers* msg) const;
     85 
     86   // Returns true if the type is currently throttled.
     87   bool IsThrottled() const;
     88 
     89   // Returns the time until this type's throttling interval expires.  Should not
     90   // be called unless IsThrottled() returns true.  The returned value will be
     91   // increased to zero if it would otherwise have been negative.
     92   base::TimeDelta GetTimeUntilUnthrottle(base::TimeTicks now) const;
     93 
     94   // Throttles the type from |now| until |now| + |duration|.
     95   void ThrottleType(base::TimeDelta duration, base::TimeTicks now);
     96 
     97   // Unthrottles the type if |now| >= the throttle expiry time.
     98   void UpdateThrottleState(base::TimeTicks now);
     99 
    100   // Update the local change nudge delay for this type.
    101   void UpdateLocalNudgeDelay(base::TimeDelta delay);
    102 
    103  private:
    104   // Number of local change nudges received for this type since the last
    105   // successful sync cycle.
    106   int local_nudge_count_;
    107 
    108   // Number of local refresh requests received for this type since the last
    109   // successful sync cycle.
    110   int local_refresh_request_count_;
    111 
    112   // The list of invalidations received since the last successful sync cycle.
    113   // This list may be incomplete.  See also:
    114   // drop_tracker_.IsRecoveringFromDropEvent() and server_payload_overflow_.
    115   //
    116   // This list takes ownership of its contents.
    117   ScopedVector<InvalidationInterface> pending_invalidations_;
    118 
    119   size_t payload_buffer_size_;
    120 
    121   // Set to true if this type is ready for, but has not yet completed initial
    122   // sync.
    123   bool initial_sync_required_;
    124 
    125   // If !unthrottle_time_.is_null(), this type is throttled and may not download
    126   // or commit data until the specified time.
    127   base::TimeTicks unthrottle_time_;
    128 
    129   // A helper to keep track invalidations we dropped due to overflow.
    130   scoped_ptr<InvalidationInterface> last_dropped_invalidation_;
    131 
    132   // The amount of time to delay a sync cycle by when a local change for this
    133   // type occurs.
    134   base::TimeDelta nudge_delay_;
    135 
    136   DISALLOW_COPY_AND_ASSIGN(DataTypeTracker);
    137 };
    138 
    139 }  // namespace sessions
    140 }  // namespace syncer
    141 
    142 #endif  // SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
    143