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