Home | History | Annotate | Download | only in invalidation
      1 // Copyright 2014 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 #ifndef COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
      6 #define COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
      7 
      8 #include <set>
      9 
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/single_thread_task_runner.h"
     12 #include "components/invalidation/invalidation.h"
     13 #include "components/invalidation/invalidation_export.h"
     14 #include "components/invalidation/invalidation_util.h"
     15 
     16 namespace base {
     17 class DictionaryValue;
     18 }  // namespace base
     19 
     20 namespace syncer {
     21 
     22 namespace test_util {
     23 class UnackedInvalidationSetEqMatcher;
     24 }  // test_util
     25 
     26 class SingleObjectInvalidationSet;
     27 class ObjectIdInvalidationMap;
     28 class AckHandle;
     29 
     30 // Manages the set of invalidations that are awaiting local acknowledgement for
     31 // a particular ObjectId.  This set of invalidations will be persisted across
     32 // restarts, though this class is not directly responsible for that.
     33 class INVALIDATION_EXPORT UnackedInvalidationSet {
     34  public:
     35   static const size_t kMaxBufferedInvalidations;
     36 
     37   UnackedInvalidationSet(invalidation::ObjectId id);
     38   UnackedInvalidationSet(const UnackedInvalidationSet& other);
     39   ~UnackedInvalidationSet();
     40 
     41   // Returns the ObjectID of the invalidations this class is tracking.
     42   const invalidation::ObjectId& object_id() const;
     43 
     44   // Adds a new invalidation to the set awaiting acknowledgement.
     45   void Add(const Invalidation& invalidation);
     46 
     47   // Adds many new invalidations to the set awaiting acknowledgement.
     48   void AddSet(const SingleObjectInvalidationSet& invalidations);
     49 
     50   // Exports the set of invalidations awaiting acknowledgement as an
     51   // ObjectIdInvalidationMap.  Each of these invalidations will be associated
     52   // with the given |ack_handler|.
     53   //
     54   // The contents of the UnackedInvalidationSet are not directly modified by
     55   // this procedure, but the AckHandles stored in those exported invalidations
     56   // are likely to end up back here in calls to Acknowledge() or Drop().
     57   void ExportInvalidations(
     58       base::WeakPtr<AckHandler> ack_handler,
     59       scoped_refptr<base::SingleThreadTaskRunner> ack_handler_task_runner,
     60       ObjectIdInvalidationMap* out) const;
     61 
     62   // Removes all stored invalidations from this object.
     63   void Clear();
     64 
     65   // Indicates that a handler has registered to handle these invalidations.
     66   //
     67   // Registrations with the invalidations server persist across restarts, but
     68   // registrations from InvalidationHandlers to the InvalidationService are not.
     69   // In the time immediately after a restart, it's possible that the server
     70   // will send us invalidations, and we won't have a handler to send them to.
     71   //
     72   // The SetIsRegistered() call indicates that this period has come to an end.
     73   // There is now a handler that can receive these invalidations.  Once this
     74   // function has been called, the kMaxBufferedInvalidations limit will be
     75   // ignored.  It is assumed that the handler will manage its own buffer size.
     76   void SetHandlerIsRegistered();
     77 
     78   // Indicates that the handler has now unregistered itself.
     79   //
     80   // This causes the object to resume enforcement of the
     81   // kMaxBufferedInvalidations limit.
     82   void SetHandlerIsUnregistered();
     83 
     84   // Given an AckHandle belonging to one of the contained invalidations, finds
     85   // the invalidation and drops it from the list.  It is considered to be
     86   // acknowledged, so there is no need to continue maintaining its state.
     87   void Acknowledge(const AckHandle& handle);
     88 
     89   // Given an AckHandle belonging to one of the contained invalidations, finds
     90   // the invalidation, drops it from the list, and adds additional state to
     91   // indicate that this invalidation has been lost without being acted on.
     92   void Drop(const AckHandle& handle);
     93 
     94   scoped_ptr<base::DictionaryValue> ToValue() const;
     95   bool ResetFromValue(const base::DictionaryValue& value);
     96 
     97  private:
     98   // Allow this test helper to have access to our internals.
     99   friend class test_util::UnackedInvalidationSetEqMatcher;
    100 
    101   typedef std::set<Invalidation, InvalidationVersionLessThan> InvalidationsSet;
    102 
    103   bool ResetListFromValue(const base::ListValue& value);
    104 
    105   // Limits the list size to the given maximum.  This function will correctly
    106   // update this class' internal data to indicate if invalidations have been
    107   // dropped.
    108   void Truncate(size_t max_size);
    109 
    110   bool registered_;
    111   invalidation::ObjectId object_id_;
    112   InvalidationsSet invalidations_;
    113 };
    114 
    115 typedef std::map<invalidation::ObjectId,
    116                  UnackedInvalidationSet,
    117                  ObjectIdLessThan> UnackedInvalidationsMap;
    118 
    119 }  // namespace syncer
    120 
    121 #endif  // COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
    122