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