Home | History | Annotate | Download | only in sessions
      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/internal_api/public/base/model_type_invalidation_map.h"
     17 #include "sync/protocol/sync.pb.h"
     18 #include "sync/sessions/data_type_tracker.h"
     19 
     20 namespace syncer {
     21 namespace sessions {
     22 
     23 class SYNC_EXPORT_PRIVATE NudgeTracker {
     24  public:
     25   static size_t kDefaultMaxPayloadsPerType;
     26 
     27   NudgeTracker();
     28   ~NudgeTracker();
     29 
     30   // Returns true if there is a good reason for performing a sync cycle.
     31   // This does not take into account whether or not this is a good *time* to
     32   // perform a sync cycle; that's the scheduler's job.
     33   bool IsSyncRequired() const;
     34 
     35   // Returns true if there is a good reason for performing a get updates
     36   // request as part of the next sync cycle.
     37   bool IsGetUpdatesRequired() const;
     38 
     39   // Tells this class that all required update fetching and committing has
     40   // completed successfully.
     41   void RecordSuccessfulSyncCycle();
     42 
     43   // Takes note of a local change.
     44   void RecordLocalChange(ModelTypeSet types);
     45 
     46   // Takes note of a locally issued request to refresh a data type.
     47   void RecordLocalRefreshRequest(ModelTypeSet types);
     48 
     49   // Takes note of the receipt of an invalidation notice from the server.
     50   void RecordRemoteInvalidation(
     51       const ModelTypeInvalidationMap& invalidation_map);
     52 
     53   // These functions should be called to keep this class informed of the status
     54   // of the connection to the invalidations server.
     55   void OnInvalidationsEnabled();
     56   void OnInvalidationsDisabled();
     57 
     58   // Marks |types| as being throttled from |now| until |now| + |length|.
     59   void SetTypesThrottledUntil(ModelTypeSet types,
     60                               base::TimeDelta length,
     61                               base::TimeTicks now);
     62 
     63   // Removes any throttling that have expired by time |now|.
     64   void UpdateTypeThrottlingState(base::TimeTicks now);
     65 
     66   // Returns the time of the next type unthrottling, relative to
     67   // the input |now| value.
     68   base::TimeDelta GetTimeUntilNextUnthrottle(base::TimeTicks now) const;
     69 
     70   // Returns true if any type is currenlty throttled.
     71   bool IsAnyTypeThrottled() const;
     72 
     73   // Returns true if |type| is currently throttled.
     74   bool IsTypeThrottled(ModelType type) const;
     75 
     76   // Returns the set of currently throttled types.
     77   ModelTypeSet GetThrottledTypes() const;
     78 
     79   // Returns the 'source' of the GetUpdate request.
     80   //
     81   // This flag is deprecated, but still used by the server.  There can be more
     82   // than one reason to perform a particular sync cycle.  The GetUpdatesTrigger
     83   // message will contain more reliable information about the reasons for
     84   // performing a sync.
     85   //
     86   // See the implementation for important information about the coalesce logic.
     87   sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source() const;
     88 
     89   // Fills a GetUpdatesTrigger message for the next GetUpdates request.  This is
     90   // used by the DownloadUpdatesCommand to dump lots of useful per-type state
     91   // information into the GetUpdate request before sending it off to the server.
     92   void FillProtoMessage(
     93       ModelType type,
     94       sync_pb::GetUpdateTriggers* msg) const;
     95 
     96   // Fills a ProgressMarker with single legacy notification hint expected by the
     97   // sync server.  Newer servers will rely on the data set by FillProtoMessage()
     98   // instead of this.
     99   void SetLegacyNotificationHint(
    100       ModelType type,
    101       sync_pb::DataTypeProgressMarker* progress) const;
    102 
    103   // Adjusts the number of hints that can be stored locally.
    104   void SetHintBufferSize(size_t size);
    105 
    106  private:
    107   typedef std::map<ModelType, DataTypeTracker> TypeTrackerMap;
    108 
    109   TypeTrackerMap type_trackers_;
    110 
    111   // Merged updates source.  This should be obsolete, but the server still
    112   // relies on it for some heuristics.
    113   sync_pb::GetUpdatesCallerInfo::GetUpdatesSource updates_source_;
    114 
    115   // Tracks whether or not invalidations are currently enabled.
    116   bool invalidations_enabled_;
    117 
    118   // This flag is set if suspect that some technical malfunction or known bug
    119   // may have left us with some unserviced invalidations.
    120   //
    121   // Keeps track of whether or not we're fully in sync with the invalidation
    122   // server.  This can be false even if invalidations are enabled and working
    123   // correctly.  For example, until we get ack-tracking working properly, we
    124   // won't persist invalidations between restarts, so we may be out of sync when
    125   // we restart.  The only way to get back into sync is to have invalidations
    126   // enabled, then complete a sync cycle to make sure we're fully up to date.
    127   bool invalidations_out_of_sync_;
    128 
    129   size_t num_payloads_per_type_;
    130 
    131   DISALLOW_COPY_AND_ASSIGN(NudgeTracker);
    132 };
    133 
    134 }  // namespace sessions
    135 }  // namespace syncer
    136 
    137 #endif  // SYNC_SESSIONS_NUDGE_TRACKER_H_
    138