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 // A class to track the per-type scheduling data.
      6 #ifndef SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
      7 #define SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
      8 
      9 #include <deque>
     10 #include <string>
     11 
     12 #include "base/basictypes.h"
     13 #include "base/time/time.h"
     14 #include "sync/protocol/sync.pb.h"
     15 
     16 namespace syncer {
     17 
     18 class Invalidation;
     19 class SingleObjectInvalidationSet;
     20 
     21 namespace sessions {
     22 
     23 typedef std::deque<std::string> PayloadList;
     24 
     25 class DataTypeTracker {
     26  public:
     27   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   void RecordLocalChange();
     35 
     36   // Tracks that a local refresh request has been made for this type.
     37   void RecordLocalRefreshRequest();
     38 
     39   // Tracks that we received invalidation notifications for this type.
     40   void RecordRemoteInvalidations(
     41       const SingleObjectInvalidationSet& invalidations);
     42 
     43   // Records that a sync cycle has been performed successfully.
     44   // Generally, this means that all local changes have been committed and all
     45   // remote changes have been downloaded, so we can clear any flags related to
     46   // pending work.
     47   void RecordSuccessfulSyncCycle();
     48 
     49   // Updates the size of the invalidations payload buffer.
     50   void UpdatePayloadBufferSize(size_t new_size);
     51 
     52   // Returns true if there is a good reason to perform a sync cycle.  This does
     53   // not take into account whether or not now is a good time to perform a sync
     54   // cycle.  That's for the scheduler to decide.
     55   bool IsSyncRequired() const;
     56 
     57   // Returns true if there is a good reason to fetch updates for this type as
     58   // part of the next sync cycle.
     59   bool IsGetUpdatesRequired() const;
     60 
     61   // Returns true if there is an uncommitted local change.
     62   bool HasLocalChangePending() const;
     63 
     64   // Returns true if we've received an invalidation since we last fetched
     65   // updates.
     66   bool HasPendingInvalidation() const;
     67 
     68   // Returns the most recent invalidation payload.
     69   std::string GetMostRecentInvalidationPayload() const;
     70 
     71   // Fills in the legacy invalidaiton payload information fields.
     72   void SetLegacyNotificationHint(
     73       sync_pb::DataTypeProgressMarker* progress) const;
     74 
     75   // Fills some type-specific contents of a GetUpdates request protobuf.  These
     76   // messages provide the server with the information it needs to decide how to
     77   // handle a request.
     78   void FillGetUpdatesTriggersMessage(sync_pb::GetUpdateTriggers* msg) const;
     79 
     80   // Returns true if the type is currently throttled.
     81   bool IsThrottled() const;
     82 
     83   // Returns the time until this type's throttling interval expires.  Should not
     84   // be called unless IsThrottled() returns true.  The returned value will be
     85   // increased to zero if it would otherwise have been negative.
     86   base::TimeDelta GetTimeUntilUnthrottle(base::TimeTicks now) const;
     87 
     88   // Throttles the type from |now| until |now| + |duration|.
     89   void ThrottleType(base::TimeDelta duration, base::TimeTicks now);
     90 
     91   // Unthrottles the type if |now| >= the throttle expiry time.
     92   void UpdateThrottleState(base::TimeTicks now);
     93 
     94  private:
     95   // Number of local change nudges received for this type since the last
     96   // successful sync cycle.
     97   int local_nudge_count_;
     98 
     99   // Number of local refresh requests received for this type since the last
    100   // successful sync cycle.
    101   int local_refresh_request_count_;
    102 
    103   // The list of invalidation payloads received since the last successful sync
    104   // cycle.  This list may be incomplete.  See also: local_payload_overflow_ and
    105   // server_payload_overflow_.
    106   PayloadList pending_payloads_;
    107 
    108   // This flag is set if the the local buffer space was been exhausted, causing
    109   // us to prematurely discard the invalidation payloads stored locally.
    110   bool local_payload_overflow_;
    111 
    112   // This flag is set if the server buffer space was exchauseted, causing the
    113   // server to prematurely discard some invalidation payloads.
    114   bool server_payload_overflow_;
    115 
    116   size_t payload_buffer_size_;
    117 
    118   // If !unthrottle_time_.is_null(), this type is throttled and may not download
    119   // or commit data until the specified time.
    120   base::TimeTicks unthrottle_time_;
    121 };
    122 
    123 }  // namespace syncer
    124 }  // namespace sessions
    125 
    126 #endif  // SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
    127