• 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 outstanding work required to bring the client back into
6 // sync with the server.
7 #ifndef SYNC_SESSIONS_NUDGE_TRACKER_H_
8 #define SYNC_SESSIONS_NUDGE_TRACKER_H_
9 
10 #include <list>
11 #include <map>
12 
13 #include "base/compiler_specific.h"
14 #include "sync/base/sync_export.h"
15 #include "sync/internal_api/public/base/model_type.h"
16 #include "sync/protocol/sync.pb.h"
17 #include "sync/sessions/data_type_tracker.h"
18 
19 namespace syncer {
20 
21 class ObjectIdInvalidationMap;
22 
23 namespace sessions {
24 
25 class SYNC_EXPORT_PRIVATE NudgeTracker {
26  public:
27   static size_t kDefaultMaxPayloadsPerType;
28 
29   NudgeTracker();
30   ~NudgeTracker();
31 
32   // Returns true if there is a good reason for performing a sync cycle.
33   // This does not take into account whether or not this is a good *time* to
34   // perform a sync cycle; that's the scheduler's job.
35   bool IsSyncRequired() const;
36 
37   // Returns true if there is a good reason for performing a get updates
38   // request as part of the next sync cycle.
39   bool IsGetUpdatesRequired() const;
40 
41   // Return true if should perform a sync cycle for GU retry.
42   //
43   // This is sensitive to changes in 'current time'.  Its value can be affected
44   // by SetSyncCycleStartTime(), SetNextRetryTime(), and
45   // RecordSuccessfulSyncCycle().  Please refer to those functions for more
46   // information on how this flag is maintained.
47   bool IsRetryRequired() const;
48 
49   // Tells this class that all required update fetching or committing has
50   // completed successfully.
51   void RecordSuccessfulSyncCycle();
52 
53   // Takes note of a local change.
54   void RecordLocalChange(ModelTypeSet types);
55 
56   // Takes note of a locally issued request to refresh a data type.
57   void RecordLocalRefreshRequest(ModelTypeSet types);
58 
59   // Takes note of the receipt of an invalidation notice from the server.
60   void RecordRemoteInvalidation(
61       const ObjectIdInvalidationMap& invalidation_map);
62 
63   // These functions should be called to keep this class informed of the status
64   // of the connection to the invalidations server.
65   void OnInvalidationsEnabled();
66   void OnInvalidationsDisabled();
67 
68   // Marks |types| as being throttled from |now| until |now| + |length|.
69   void SetTypesThrottledUntil(ModelTypeSet types,
70                               base::TimeDelta length,
71                               base::TimeTicks now);
72 
73   // Removes any throttling that have expired by time |now|.
74   void UpdateTypeThrottlingState(base::TimeTicks now);
75 
76   // Returns the time of the next type unthrottling, relative to
77   // the input |now| value.
78   base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const;
79 
80   // Returns true if any type is currenlty throttled.
81   bool IsAnyTypeThrottled() const;
82 
83   // Returns true if |type| is currently throttled.
84   bool IsTypeThrottled(ModelType type) const;
85 
86   // Returns the set of currently throttled types.
87   ModelTypeSet GetThrottledTypes() const;
88 
89   // Returns the set of types with local changes pending.
90   ModelTypeSet GetNudgedTypes() const;
91 
92   // Returns the set of types that have pending invalidations.
93   ModelTypeSet GetNotifiedTypes() const;
94 
95   // Returns the set of types that have pending refresh requests.
96   ModelTypeSet GetRefreshRequestedTypes() const;
97 
98   // Returns the 'source' of the GetUpdate request.
99   //
100   // This flag is deprecated, but still used by the server.  There can be more
101   // than one reason to perform a particular sync cycle.  The GetUpdatesTrigger
102   // message will contain more reliable information about the reasons for
103   // performing a sync.
104   //
105   // See the implementation for important information about the coalesce logic.
106   sync_pb::GetUpdatesCallerInfo::GetUpdatesSource GetLegacySource() const;
107 
108   // Fills a GetUpdatesTrigger message for the next GetUpdates request.  This is
109   // used by the DownloadUpdatesCommand to dump lots of useful per-type state
110   // information into the GetUpdate request before sending it off to the server.
111   void FillProtoMessage(
112       ModelType type,
113       sync_pb::GetUpdateTriggers* msg) const;
114 
115   // Fills a ProgressMarker with single legacy notification hint expected by the
116   // sync server.  Newer servers will rely on the data set by FillProtoMessage()
117   // instead of this.
118   void SetLegacyNotificationHint(
119       ModelType type,
120       sync_pb::DataTypeProgressMarker* progress) const;
121 
122   // Flips the flag if we're due for a retry.
123   void SetSyncCycleStartTime(base::TimeTicks now);
124 
125   // Adjusts the number of hints that can be stored locally.
126   void SetHintBufferSize(size_t size);
127 
128   // Schedules a retry GetUpdate request for some time in the future.
129   //
130   // This is a request sent to us as part of a server response requesting
131   // that the client perform a GetUpdate request at |next_retry_time| to
132   // fetch any updates it may have missed in the first attempt.
133   //
134   // To avoid strange results from IsRetryRequired() during a sync cycle, the
135   // effects of this change are not guaranteed to take effect until
136   // SetSyncCycleStartTime() is called at the start of the *next* sync cycle.
137   void SetNextRetryTime(base::TimeTicks next_retry_time);
138 
139  private:
140   typedef std::map<ModelType, DataTypeTracker> TypeTrackerMap;
141 
142   TypeTrackerMap type_trackers_;
143 
144   // Merged updates source.  This should be obsolete, but the server still
145   // relies on it for some heuristics.
146   sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source_;
147 
148   // Tracks whether or not invalidations are currently enabled.
149   bool invalidations_enabled_;
150 
151   // This flag is set if suspect that some technical malfunction or known bug
152   // may have left us with some unserviced invalidations.
153   //
154   // Keeps track of whether or not we're fully in sync with the invalidation
155   // server.  This can be false even if invalidations are enabled and working
156   // correctly.  For example, until we get ack-tracking working properly, we
157   // won't persist invalidations between restarts, so we may be out of sync when
158   // we restart.  The only way to get back into sync is to have invalidations
159   // enabled, then complete a sync cycle to make sure we're fully up to date.
160   bool invalidations_out_of_sync_;
161 
162   size_t num_payloads_per_type_;
163 
164   base::TimeTicks last_successful_sync_time_;
165 
166   // A pending update to the current_retry_time_.
167   //
168   // The GU retry time is specified by a call to SetNextRetryTime, but we don't
169   // want that change to take effect right away, since it could happen in the
170   // middle of a sync cycle.  We delay the update until the start of the next
171   // sync cycle, which is indicated by a call to SetSyncCycleStartTime().
172   base::TimeTicks next_retry_time_;
173 
174   // The currently active retry GU time.  Will be null if there is no retry GU
175   // pending at this time.
176   base::TimeTicks current_retry_time_;
177 
178   // The time when the sync cycle started.  This value is maintained by
179   // SetSyncCycleStartTime().  This may contain a stale value if we're not
180   // currently in a sync cycle.
181   base::TimeTicks sync_cycle_start_time_;
182 
183   DISALLOW_COPY_AND_ASSIGN(NudgeTracker);
184 };
185 
186 }  // namespace sessions
187 }  // namespace syncer
188 
189 #endif  // SYNC_SESSIONS_NUDGE_TRACKER_H_
190