unacked_invalidation_set.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #ifndef COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_
  5. #define COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <set>
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/single_thread_task_runner.h"
  11. #include "components/invalidation/public/invalidation.h"
  12. #include "components/invalidation/public/invalidation_export.h"
  13. #include "components/invalidation/public/invalidation_util.h"
  14. namespace invalidation {
  15. class SingleTopicInvalidationSet;
  16. class TopicInvalidationMap;
  17. class AckHandle;
  18. class UnackedInvalidationSet;
  19. using UnackedInvalidationsMap = std::map<Topic, UnackedInvalidationSet>;
  20. // Manages the set of invalidations that are awaiting local acknowledgement for
  21. // a particular Topic. This set of invalidations will be persisted across
  22. // restarts, though this class is not directly responsible for that.
  23. class INVALIDATION_EXPORT UnackedInvalidationSet {
  24. public:
  25. static const size_t kMaxBufferedInvalidations;
  26. explicit UnackedInvalidationSet(const Topic& topic);
  27. UnackedInvalidationSet(const UnackedInvalidationSet& other);
  28. ~UnackedInvalidationSet();
  29. // Returns the Topic of the invalidations this class is tracking.
  30. const Topic& topic() const;
  31. // Adds a new invalidation to the set awaiting acknowledgement.
  32. void Add(const Invalidation& invalidation);
  33. // Adds many new invalidations to the set awaiting acknowledgement.
  34. void AddSet(const SingleTopicInvalidationSet& invalidations);
  35. // Exports the set of invalidations awaiting acknowledgement as an
  36. // TopicInvalidationMap. Each of these invalidations will be associated
  37. // with the given |ack_handler|.
  38. //
  39. // The contents of the UnackedInvalidationSet are not directly modified by
  40. // this procedure, but the AckHandles stored in those exported invalidations
  41. // are likely to end up back here in calls to Acknowledge() or Drop().
  42. void ExportInvalidations(
  43. base::WeakPtr<AckHandler> ack_handler,
  44. scoped_refptr<base::SingleThreadTaskRunner> ack_handler_task_runner,
  45. TopicInvalidationMap* out) const;
  46. // Given an AckHandle belonging to one of the contained invalidations, finds
  47. // the invalidation and drops it from the list. It is considered to be
  48. // acknowledged, so there is no need to continue maintaining its state.
  49. void Acknowledge(const AckHandle& handle);
  50. // Given an AckHandle belonging to one of the contained invalidations, finds
  51. // the invalidation, drops it from the list, and adds additional state to
  52. // indicate that this invalidation has been lost without being acted on.
  53. void Drop(const AckHandle& handle);
  54. private:
  55. typedef std::set<Invalidation, InvalidationVersionLessThan> InvalidationsSet;
  56. // Limits the list size to the given maximum. This function will correctly
  57. // update this class' internal data to indicate if invalidations have been
  58. // dropped.
  59. void Truncate(size_t max_size);
  60. const Topic topic_;
  61. InvalidationsSet invalidations_;
  62. };
  63. } // namespace invalidation
  64. #endif // COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_