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.
5 #ifndef SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
6 #define SYNC_SESSIONS_DATA_TYPE_TRACKER_H_
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"
20 class InvalidationInterface
;
24 // A class to track the per-type scheduling data.
25 class DataTypeTracker
{
27 explicit DataTypeTracker();
30 // For STL compatibility, we do not forbid the creation of a default copy
31 // constructor and assignment operator.
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();
37 // Tracks that a local refresh request has been made for this type.
38 void RecordLocalRefreshRequest();
40 // Tracks that we received invalidation notifications for this type.
41 void RecordRemoteInvalidation(scoped_ptr
<InvalidationInterface
> incoming
);
43 // Takes note that initial sync is pending for this type.
44 void RecordInitialSyncRequired();
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
50 void RecordSuccessfulSyncCycle();
52 // Updates the size of the invalidations payload buffer.
53 void UpdatePayloadBufferSize(size_t new_size
);
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;
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;
64 // Returns true if there is an uncommitted local change.
65 bool HasLocalChangePending() const;
67 // Returns true if we've received an invalidation since we last fetched
69 bool HasPendingInvalidation() const;
71 // Returns true if an explicit refresh request is still outstanding.
72 bool HasRefreshRequestPending() const;
74 // Returns true if this type is requesting an initial sync.
75 bool IsInitialSyncRequired() const;
77 // Fills in the legacy invalidaiton payload information fields.
78 void SetLegacyNotificationHint(
79 sync_pb::DataTypeProgressMarker
* progress
) const;
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
84 void FillGetUpdatesTriggersMessage(sync_pb::GetUpdateTriggers
* msg
) const;
86 // Returns true if the type is currently throttled.
87 bool IsThrottled() const;
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;
94 // Throttles the type from |now| until |now| + |duration|.
95 void ThrottleType(base::TimeDelta duration
, base::TimeTicks now
);
97 // Unthrottles the type if |now| >= the throttle expiry time.
98 void UpdateThrottleState(base::TimeTicks now
);
100 // Update the local change nudge delay for this type.
101 void UpdateLocalNudgeDelay(base::TimeDelta delay
);
104 // Number of local change nudges received for this type since the last
105 // successful sync cycle.
106 int local_nudge_count_
;
108 // Number of local refresh requests received for this type since the last
109 // successful sync cycle.
110 int local_refresh_request_count_
;
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_.
116 // This list takes ownership of its contents.
117 ScopedVector
<InvalidationInterface
> pending_invalidations_
;
119 size_t payload_buffer_size_
;
121 // Set to true if this type is ready for, but has not yet completed initial
123 bool initial_sync_required_
;
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_
;
129 // A helper to keep track invalidations we dropped due to overflow.
130 scoped_ptr
<InvalidationInterface
> last_dropped_invalidation_
;
132 // The amount of time to delay a sync cycle by when a local change for this
134 base::TimeDelta nudge_delay_
;
136 DISALLOW_COPY_AND_ASSIGN(DataTypeTracker
);
139 } // namespace sessions
140 } // namespace syncer
142 #endif // SYNC_SESSIONS_DATA_TYPE_TRACKER_H_