Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / sync / sessions / nudge_tracker.h
blobd82a3714d5a15f2b4d9e942a4d80f97023300eec
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_
10 #include <list>
11 #include <map>
13 #include "base/compiler_specific.h"
14 #include "base/containers/scoped_ptr_map.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/time/time.h"
17 #include "sync/base/sync_export.h"
18 #include "sync/internal_api/public/base/invalidation_interface.h"
19 #include "sync/internal_api/public/base/model_type.h"
20 #include "sync/protocol/sync.pb.h"
21 #include "sync/sessions/data_type_tracker.h"
23 namespace syncer {
25 class ObjectIdInvalidationMap;
27 namespace sessions {
29 class SYNC_EXPORT_PRIVATE NudgeTracker {
30 public:
31 static size_t kDefaultMaxPayloadsPerType;
33 NudgeTracker();
34 ~NudgeTracker();
36 // Returns true if there is a good reason for performing a sync cycle.
37 // This does not take into account whether or not this is a good *time* to
38 // perform a sync cycle; that's the scheduler's job.
39 bool IsSyncRequired() const;
41 // Returns true if there is a good reason for performing a get updates
42 // request as part of the next sync cycle.
43 bool IsGetUpdatesRequired() const;
45 // Return true if should perform a sync cycle for GU retry.
47 // This is sensitive to changes in 'current time'. Its value can be affected
48 // by SetSyncCycleStartTime(), SetNextRetryTime(), and
49 // RecordSuccessfulSyncCycle(). Please refer to those functions for more
50 // information on how this flag is maintained.
51 bool IsRetryRequired() const;
53 // Tells this class that all required update fetching or committing has
54 // completed successfully.
55 void RecordSuccessfulSyncCycle();
57 // Takes note of a local change.
58 // Returns the shortest nudge delay from the tracker of each type in |types|.
59 base::TimeDelta RecordLocalChange(ModelTypeSet types);
61 // Takes note of a locally issued request to refresh a data type.
62 // Returns the current nudge delay for a local refresh.
63 base::TimeDelta RecordLocalRefreshRequest(ModelTypeSet types);
65 // Takes note of the receipt of an invalidation notice from the server.
66 // Returns the current nudge delay for a remote invalidation.
67 base::TimeDelta RecordRemoteInvalidation(
68 syncer::ModelType type,
69 scoped_ptr<InvalidationInterface> invalidation);
71 // Take note that an initial sync is pending for this type.
72 void RecordInitialSyncRequired(syncer::ModelType type);
74 // Takes note that the conflict happended for this type, need to sync to
75 // resolve conflict locally.
76 void RecordCommitConflict(syncer::ModelType type);
78 // These functions should be called to keep this class informed of the status
79 // of the connection to the invalidations server.
80 void OnInvalidationsEnabled();
81 void OnInvalidationsDisabled();
83 // Marks |types| as being throttled from |now| until |now| + |length|.
84 void SetTypesThrottledUntil(ModelTypeSet types,
85 base::TimeDelta length,
86 base::TimeTicks now);
88 // Removes any throttling that have expired by time |now|.
89 void UpdateTypeThrottlingState(base::TimeTicks now);
91 // Returns the time of the next type unthrottling, relative to
92 // the input |now| value.
93 base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const;
95 // Returns true if any type is currenlty throttled.
96 bool IsAnyTypeThrottled() const;
98 // Returns true if |type| is currently throttled.
99 bool IsTypeThrottled(ModelType type) const;
101 // Returns the set of currently throttled types.
102 ModelTypeSet GetThrottledTypes() const;
104 // Returns the set of types with local changes pending.
105 ModelTypeSet GetNudgedTypes() const;
107 // Returns the set of types that have pending invalidations.
108 ModelTypeSet GetNotifiedTypes() const;
110 // Returns the set of types that have pending refresh requests.
111 ModelTypeSet GetRefreshRequestedTypes() const;
113 // Returns the 'source' of the GetUpdate request.
115 // This flag is deprecated, but still used by the server. There can be more
116 // than one reason to perform a particular sync cycle. The GetUpdatesTrigger
117 // message will contain more reliable information about the reasons for
118 // performing a sync.
120 // See the implementation for important information about the coalesce logic.
121 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource GetLegacySource() const;
123 // Fills a GetUpdatesTrigger message for the next GetUpdates request. This is
124 // used by the DownloadUpdatesCommand to dump lots of useful per-type state
125 // information into the GetUpdate request before sending it off to the server.
126 void FillProtoMessage(
127 ModelType type,
128 sync_pb::GetUpdateTriggers* msg) const;
130 // Fills a ProgressMarker with single legacy notification hint expected by the
131 // sync server. Newer servers will rely on the data set by FillProtoMessage()
132 // instead of this.
133 void SetLegacyNotificationHint(
134 ModelType type,
135 sync_pb::DataTypeProgressMarker* progress) const;
137 // Flips the flag if we're due for a retry.
138 void SetSyncCycleStartTime(base::TimeTicks now);
140 // Adjusts the number of hints that can be stored locally.
141 void SetHintBufferSize(size_t size);
143 // Schedules a retry GetUpdate request for some time in the future.
145 // This is a request sent to us as part of a server response requesting
146 // that the client perform a GetUpdate request at |next_retry_time| to
147 // fetch any updates it may have missed in the first attempt.
149 // To avoid strange results from IsRetryRequired() during a sync cycle, the
150 // effects of this change are not guaranteed to take effect until
151 // SetSyncCycleStartTime() is called at the start of the *next* sync cycle.
152 void SetNextRetryTime(base::TimeTicks next_retry_time);
154 // Update the per-datatype local change nudge delays.
155 void OnReceivedCustomNudgeDelays(
156 const std::map<ModelType, base::TimeDelta>& delay_map);
158 // Update the default nudge delay.
159 void SetDefaultNudgeDelay(base::TimeDelta nudge_delay);
161 private:
162 typedef base::ScopedPtrMap<ModelType, scoped_ptr<DataTypeTracker>>
163 TypeTrackerMap;
165 TypeTrackerMap type_trackers_;
167 // Tracks whether or not invalidations are currently enabled.
168 bool invalidations_enabled_;
170 // This flag is set if suspect that some technical malfunction or known bug
171 // may have left us with some unserviced invalidations.
173 // Keeps track of whether or not we're fully in sync with the invalidation
174 // server. This can be false even if invalidations are enabled and working
175 // correctly. For example, until we get ack-tracking working properly, we
176 // won't persist invalidations between restarts, so we may be out of sync when
177 // we restart. The only way to get back into sync is to have invalidations
178 // enabled, then complete a sync cycle to make sure we're fully up to date.
179 bool invalidations_out_of_sync_;
181 base::TimeTicks last_successful_sync_time_;
183 // A pending update to the current_retry_time_.
185 // The GU retry time is specified by a call to SetNextRetryTime, but we don't
186 // want that change to take effect right away, since it could happen in the
187 // middle of a sync cycle. We delay the update until the start of the next
188 // sync cycle, which is indicated by a call to SetSyncCycleStartTime().
189 base::TimeTicks next_retry_time_;
191 // The currently active retry GU time. Will be null if there is no retry GU
192 // pending at this time.
193 base::TimeTicks current_retry_time_;
195 // The time when the sync cycle started. This value is maintained by
196 // SetSyncCycleStartTime(). This may contain a stale value if we're not
197 // currently in a sync cycle.
198 base::TimeTicks sync_cycle_start_time_;
200 // Nudge delays for various events.
201 base::TimeDelta minimum_local_nudge_delay_;
202 base::TimeDelta local_refresh_nudge_delay_;
203 base::TimeDelta remote_invalidation_nudge_delay_;
205 DISALLOW_COPY_AND_ASSIGN(NudgeTracker);
208 } // namespace sessions
209 } // namespace syncer
211 #endif // SYNC_SESSIONS_NUDGE_TRACKER_H_