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