Home | History | Annotate | Download | only in notifier
      1 // Copyright 2012 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 // An InvalidationStateTracker is an interface that handles persisting state
      6 // needed for invalidations. Currently, it is responsible for managing the
      7 // following information:
      8 // - Max version seen from the invalidation server to help dedupe invalidations.
      9 // - Bootstrap data for the invalidation client.
     10 // - Payloads and locally generated ack handles, to support local acking.
     11 
     12 #ifndef SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_
     13 #define SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_
     14 
     15 #include <map>
     16 #include <string>
     17 
     18 #include "base/basictypes.h"
     19 #include "base/callback_forward.h"
     20 #include "base/memory/ref_counted.h"
     21 #include "google/cacheinvalidation/include/types.h"
     22 #include "sync/base/sync_export.h"
     23 #include "sync/internal_api/public/base/invalidation.h"
     24 #include "sync/notifier/invalidation_util.h"
     25 
     26 namespace base {
     27 class TaskRunner;
     28 }  // namespace base
     29 
     30 namespace syncer {
     31 
     32 struct SYNC_EXPORT InvalidationState {
     33   InvalidationState();
     34   ~InvalidationState();
     35 
     36   int64 version;
     37   std::string payload;
     38   AckHandle expected;
     39   AckHandle current;
     40 };
     41 
     42 // TODO(dcheng): Remove this in favor of adding an Equals() method.
     43 SYNC_EXPORT_PRIVATE bool operator==(const InvalidationState& lhs,
     44                                     const InvalidationState& rhs);
     45 
     46 typedef std::map<invalidation::ObjectId, InvalidationState, ObjectIdLessThan>
     47     InvalidationStateMap;
     48 typedef std::map<invalidation::ObjectId, AckHandle, ObjectIdLessThan>
     49     AckHandleMap;
     50 
     51 class InvalidationStateTracker {
     52  public:
     53   InvalidationStateTracker() {}
     54 
     55   virtual InvalidationStateMap GetAllInvalidationStates() const = 0;
     56 
     57   // |max_version| should be strictly greater than any existing max
     58   // version for |model_type|.
     59   virtual void SetMaxVersionAndPayload(const invalidation::ObjectId& id,
     60                                        int64 max_version,
     61                                        const std::string& payload) = 0;
     62   // Removes all state tracked for |ids|.
     63   virtual void Forget(const ObjectIdSet& ids) = 0;
     64 
     65   // The per-client unique ID used to register the invalidation client with the
     66   // server.  This is used to squelch invalidation notifications that originate
     67   // from changes made by this client.
     68   virtual void SetInvalidatorClientId(const std::string& data) = 0;
     69   virtual std::string GetInvalidatorClientId() const = 0;
     70 
     71   // Used by invalidation::InvalidationClient for persistence. |data| is an
     72   // opaque blob that an invalidation client can use after a restart to
     73   // bootstrap itself. |data| is binary data (not valid UTF8, embedded nulls,
     74   // etc).
     75   virtual void SetBootstrapData(const std::string& data) = 0;
     76   virtual std::string GetBootstrapData() const = 0;
     77 
     78   // Erases invalidation versions, client ID, and state stored on disk.
     79   virtual void Clear() = 0;
     80 
     81   // Used for generating our own local ack handles. Generates a new ack handle
     82   // for each object id in |ids|. The result is returned via |callback| posted
     83   // to |task_runner|.
     84   virtual void GenerateAckHandles(
     85       const ObjectIdSet& ids,
     86       const scoped_refptr<base::TaskRunner>& task_runner,
     87       base::Callback<void(const AckHandleMap&)> callback) = 0;
     88 
     89   // Records an acknowledgement for |id|. Note that no attempt at ordering is
     90   // made. Acknowledge() only records the last ack_handle it received, even if
     91   // the last ack_handle it received was generated before the value currently
     92   // recorded.
     93   virtual void Acknowledge(const invalidation::ObjectId& id,
     94                            const AckHandle& ack_handle) = 0;
     95 
     96  protected:
     97   virtual ~InvalidationStateTracker() {}
     98 };
     99 
    100 }  // namespace syncer
    101 
    102 #endif  // SYNC_NOTIFIER_INVALIDATION_STATE_TRACKER_H_
    103