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 "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" 21 22 namespace syncer { 23 24 class ObjectIdInvalidationMap; 25 26 namespace sessions { 27 28 class SYNC_EXPORT_PRIVATE NudgeTracker { 29 public: 30 static size_t kDefaultMaxPayloadsPerType; 31 32 NudgeTracker(); 33 ~NudgeTracker(); 34 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; 39 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; 43 44 // Return true if should perform a sync cycle for GU retry. 45 // 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; 51 52 // Tells this class that all required update fetching or committing has 53 // completed successfully. 54 void RecordSuccessfulSyncCycle(); 55 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); 59 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); 63 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); 69 70 // Take note that an initial sync is pending for this type. 71 void RecordInitialSyncRequired(syncer::ModelType type); 72 73 // These functions should be called to keep this class informed of the status 74 // of the connection to the invalidations server. 75 void OnInvalidationsEnabled(); 76 void OnInvalidationsDisabled(); 77 78 // Marks |types| as being throttled from |now| until |now| + |length|. 79 void SetTypesThrottledUntil(ModelTypeSet types, 80 base::TimeDelta length, 81 base::TimeTicks now); 82 83 // Removes any throttling that have expired by time |now|. 84 void UpdateTypeThrottlingState(base::TimeTicks now); 85 86 // Returns the time of the next type unthrottling, relative to 87 // the input |now| value. 88 base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const; 89 90 // Returns true if any type is currenlty throttled. 91 bool IsAnyTypeThrottled() const; 92 93 // Returns true if |type| is currently throttled. 94 bool IsTypeThrottled(ModelType type) const; 95 96 // Returns the set of currently throttled types. 97 ModelTypeSet GetThrottledTypes() const; 98 99 // Returns the set of types with local changes pending. 100 ModelTypeSet GetNudgedTypes() const; 101 102 // Returns the set of types that have pending invalidations. 103 ModelTypeSet GetNotifiedTypes() const; 104 105 // Returns the set of types that have pending refresh requests. 106 ModelTypeSet GetRefreshRequestedTypes() const; 107 108 // Returns the 'source' of the GetUpdate request. 109 // 110 // This flag is deprecated, but still used by the server. There can be more 111 // than one reason to perform a particular sync cycle. The GetUpdatesTrigger 112 // message will contain more reliable information about the reasons for 113 // performing a sync. 114 // 115 // See the implementation for important information about the coalesce logic. 116 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource GetLegacySource() const; 117 118 // Fills a GetUpdatesTrigger message for the next GetUpdates request. This is 119 // used by the DownloadUpdatesCommand to dump lots of useful per-type state 120 // information into the GetUpdate request before sending it off to the server. 121 void FillProtoMessage( 122 ModelType type, 123 sync_pb::GetUpdateTriggers* msg) const; 124 125 // Fills a ProgressMarker with single legacy notification hint expected by the 126 // sync server. Newer servers will rely on the data set by FillProtoMessage() 127 // instead of this. 128 void SetLegacyNotificationHint( 129 ModelType type, 130 sync_pb::DataTypeProgressMarker* progress) const; 131 132 // Flips the flag if we're due for a retry. 133 void SetSyncCycleStartTime(base::TimeTicks now); 134 135 // Adjusts the number of hints that can be stored locally. 136 void SetHintBufferSize(size_t size); 137 138 // Schedules a retry GetUpdate request for some time in the future. 139 // 140 // This is a request sent to us as part of a server response requesting 141 // that the client perform a GetUpdate request at |next_retry_time| to 142 // fetch any updates it may have missed in the first attempt. 143 // 144 // To avoid strange results from IsRetryRequired() during a sync cycle, the 145 // effects of this change are not guaranteed to take effect until 146 // SetSyncCycleStartTime() is called at the start of the *next* sync cycle. 147 void SetNextRetryTime(base::TimeTicks next_retry_time); 148 149 // Update the per-datatype local change nudge delays. 150 void OnReceivedCustomNudgeDelays( 151 const std::map<ModelType, base::TimeDelta>& delay_map); 152 153 // Update the default nudge delay. 154 void SetDefaultNudgeDelay(base::TimeDelta nudge_delay); 155 156 private: 157 typedef std::map<ModelType, DataTypeTracker*> TypeTrackerMap; 158 159 TypeTrackerMap type_trackers_; 160 STLValueDeleter<TypeTrackerMap> type_tracker_deleter_; 161 162 // Merged updates source. This should be obsolete, but the server still 163 // relies on it for some heuristics. 164 sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source_; 165 166 // Tracks whether or not invalidations are currently enabled. 167 bool invalidations_enabled_; 168 169 // This flag is set if suspect that some technical malfunction or known bug 170 // may have left us with some unserviced invalidations. 171 // 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_; 179 180 size_t num_payloads_per_type_; 181 182 base::TimeTicks last_successful_sync_time_; 183 184 // A pending update to the current_retry_time_. 185 // 186 // The GU retry time is specified by a call to SetNextRetryTime, but we don't 187 // want that change to take effect right away, since it could happen in the 188 // middle of a sync cycle. We delay the update until the start of the next 189 // sync cycle, which is indicated by a call to SetSyncCycleStartTime(). 190 base::TimeTicks next_retry_time_; 191 192 // The currently active retry GU time. Will be null if there is no retry GU 193 // pending at this time. 194 base::TimeTicks current_retry_time_; 195 196 // The time when the sync cycle started. This value is maintained by 197 // SetSyncCycleStartTime(). This may contain a stale value if we're not 198 // currently in a sync cycle. 199 base::TimeTicks sync_cycle_start_time_; 200 201 // Nudge delays for various events. 202 base::TimeDelta minimum_local_nudge_delay_; 203 base::TimeDelta local_refresh_nudge_delay_; 204 base::TimeDelta remote_invalidation_nudge_delay_; 205 206 DISALLOW_COPY_AND_ASSIGN(NudgeTracker); 207 }; 208 209 } // namespace sessions 210 } // namespace syncer 211 212 #endif // SYNC_SESSIONS_NUDGE_TRACKER_H_ 213