Automated Commit: Committing new LKGM version 6953.0.0 for chromeos.
[chromium-blink-merge.git] / sync / sessions / nudge_tracker.h
blobec2277c5052e2d4427bd9d48af8dc5c8874c5b92
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/memory/scoped_ptr.h"
15 #include "base/time/time.h"
16 #include "sync/base/sync_export.h"
17 #include "sync/internal_api/public/base/invalidation_interface.h"
18 #include "sync/internal_api/public/base/model_type.h"
19 #include "sync/protocol/sync.pb.h"
20 #include "sync/sessions/data_type_tracker.h"
22 namespace syncer {
24 class ObjectIdInvalidationMap;
26 namespace sessions {
28 class SYNC_EXPORT_PRIVATE NudgeTracker {
29 public:
30 static size_t kDefaultMaxPayloadsPerType;
32 NudgeTracker();
33 ~NudgeTracker();
35 // Returns true if there is a good reason for performing a sync cycle.
36 // This does not take into account whether or not this is a good *time* to
37 // perform a sync cycle; that's the scheduler's job.
38 bool IsSyncRequired() const;
40 // Returns true if there is a good reason for performing a get updates
41 // request as part of the next sync cycle.
42 bool IsGetUpdatesRequired() const;
44 // Return true if should perform a sync cycle for GU retry.
46 // This is sensitive to changes in 'current time'. Its value can be affected
47 // by SetSyncCycleStartTime(), SetNextRetryTime(), and
48 // RecordSuccessfulSyncCycle(). Please refer to those functions for more
49 // information on how this flag is maintained.
50 bool IsRetryRequired() const;
52 // Tells this class that all required update fetching or committing has
53 // completed successfully.
54 void RecordSuccessfulSyncCycle();
56 // Takes note of a local change.
57 // Returns the shortest nudge delay from the tracker of each type in |types|.
58 base::TimeDelta RecordLocalChange(ModelTypeSet types);
60 // Takes note of a locally issued request to refresh a data type.
61 // Returns the current nudge delay for a local refresh.
62 base::TimeDelta RecordLocalRefreshRequest(ModelTypeSet types);
64 // Takes note of the receipt of an invalidation notice from the server.
65 // Returns the current nudge delay for a remote invalidation.
66 base::TimeDelta RecordRemoteInvalidation(
67 syncer::ModelType type,
68 scoped_ptr<InvalidationInterface> invalidation);
70 // Take note that an initial sync is pending for this type.
71 void RecordInitialSyncRequired(syncer::ModelType type);
73 // Takes note that the conflict happended for this type, need to sync to
74 // resolve conflict locally.
75 void RecordCommitConflict(syncer::ModelType type);
77 // These functions should be called to keep this class informed of the status
78 // of the connection to the invalidations server.
79 void OnInvalidationsEnabled();
80 void OnInvalidationsDisabled();
82 // Marks |types| as being throttled from |now| until |now| + |length|.
83 void SetTypesThrottledUntil(ModelTypeSet types,
84 base::TimeDelta length,
85 base::TimeTicks now);
87 // Removes any throttling that have expired by time |now|.
88 void UpdateTypeThrottlingState(base::TimeTicks now);
90 // Returns the time of the next type unthrottling, relative to
91 // the input |now| value.
92 base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const;
94 // Returns true if any type is currenlty throttled.
95 bool IsAnyTypeThrottled() const;
97 // Returns true if |type| is currently throttled.
98 bool IsTypeThrottled(ModelType type) const;
100 // Returns the set of currently throttled types.
101 ModelTypeSet GetThrottledTypes() const;
103 // Returns the set of types with local changes pending.
104 ModelTypeSet GetNudgedTypes() const;
106 // Returns the set of types that have pending invalidations.
107 ModelTypeSet GetNotifiedTypes() const;
109 // Returns the set of types that have pending refresh requests.
110 ModelTypeSet GetRefreshRequestedTypes() const;
112 // Returns the 'source' of the GetUpdate request.
114 // This flag is deprecated, but still used by the server. There can be more
115 // than one reason to perform a particular sync cycle. The GetUpdatesTrigger
116 // message will contain more reliable information about the reasons for
117 // performing a sync.
119 // See the implementation for important information about the coalesce logic.
120 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource GetLegacySource() const;
122 // Fills a GetUpdatesTrigger message for the next GetUpdates request. This is
123 // used by the DownloadUpdatesCommand to dump lots of useful per-type state
124 // information into the GetUpdate request before sending it off to the server.
125 void FillProtoMessage(
126 ModelType type,
127 sync_pb::GetUpdateTriggers* msg) const;
129 // Fills a ProgressMarker with single legacy notification hint expected by the
130 // sync server. Newer servers will rely on the data set by FillProtoMessage()
131 // instead of this.
132 void SetLegacyNotificationHint(
133 ModelType type,
134 sync_pb::DataTypeProgressMarker* progress) const;
136 // Flips the flag if we're due for a retry.
137 void SetSyncCycleStartTime(base::TimeTicks now);
139 // Adjusts the number of hints that can be stored locally.
140 void SetHintBufferSize(size_t size);
142 // Schedules a retry GetUpdate request for some time in the future.
144 // This is a request sent to us as part of a server response requesting
145 // that the client perform a GetUpdate request at |next_retry_time| to
146 // fetch any updates it may have missed in the first attempt.
148 // To avoid strange results from IsRetryRequired() during a sync cycle, the
149 // effects of this change are not guaranteed to take effect until
150 // SetSyncCycleStartTime() is called at the start of the *next* sync cycle.
151 void SetNextRetryTime(base::TimeTicks next_retry_time);
153 // Update the per-datatype local change nudge delays.
154 void OnReceivedCustomNudgeDelays(
155 const std::map<ModelType, base::TimeDelta>& delay_map);
157 // Update the default nudge delay.
158 void SetDefaultNudgeDelay(base::TimeDelta nudge_delay);
160 private:
161 typedef std::map<ModelType, DataTypeTracker*> TypeTrackerMap;
163 TypeTrackerMap type_trackers_;
164 STLValueDeleter<TypeTrackerMap> type_tracker_deleter_;
166 // Tracks whether or not invalidations are currently enabled.
167 bool invalidations_enabled_;
169 // This flag is set if suspect that some technical malfunction or known bug
170 // may have left us with some unserviced invalidations.
172 // Keeps track of whether or not we're fully in sync with the invalidation
173 // server. This can be false even if invalidations are enabled and working
174 // correctly. For example, until we get ack-tracking working properly, we
175 // won't persist invalidations between restarts, so we may be out of sync when
176 // we restart. The only way to get back into sync is to have invalidations
177 // enabled, then complete a sync cycle to make sure we're fully up to date.
178 bool invalidations_out_of_sync_;
180 base::TimeTicks last_successful_sync_time_;
182 // A pending update to the current_retry_time_.
184 // The GU retry time is specified by a call to SetNextRetryTime, but we don't
185 // want that change to take effect right away, since it could happen in the
186 // middle of a sync cycle. We delay the update until the start of the next
187 // sync cycle, which is indicated by a call to SetSyncCycleStartTime().
188 base::TimeTicks next_retry_time_;
190 // The currently active retry GU time. Will be null if there is no retry GU
191 // pending at this time.
192 base::TimeTicks current_retry_time_;
194 // The time when the sync cycle started. This value is maintained by
195 // SetSyncCycleStartTime(). This may contain a stale value if we're not
196 // currently in a sync cycle.
197 base::TimeTicks sync_cycle_start_time_;
199 // Nudge delays for various events.
200 base::TimeDelta minimum_local_nudge_delay_;
201 base::TimeDelta local_refresh_nudge_delay_;
202 base::TimeDelta remote_invalidation_nudge_delay_;
204 DISALLOW_COPY_AND_ASSIGN(NudgeTracker);
207 } // namespace sessions
208 } // namespace syncer
210 #endif // SYNC_SESSIONS_NUDGE_TRACKER_H_