invalidation.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_PUBLIC_INVALIDATION_H_
  5. #define COMPONENTS_INVALIDATION_PUBLIC_INVALIDATION_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/task/sequenced_task_runner.h"
  11. #include "base/values.h"
  12. #include "components/invalidation/public/ack_handle.h"
  13. #include "components/invalidation/public/invalidation_export.h"
  14. #include "components/invalidation/public/invalidation_util.h"
  15. namespace invalidation {
  16. class AckHandler;
  17. // Represents a local invalidation. This class supports "local" ack-tracking
  18. // and simple serialization to pref values.
  19. class INVALIDATION_EXPORT Invalidation {
  20. public:
  21. // Factory functions.
  22. static Invalidation Init(const Topic& topic,
  23. int64_t version,
  24. const std::string& payload);
  25. static Invalidation InitUnknownVersion(const Topic& topic);
  26. static Invalidation InitFromDroppedInvalidation(const Invalidation& dropped);
  27. Invalidation(const Invalidation& other);
  28. Invalidation& operator=(const Invalidation& other);
  29. ~Invalidation();
  30. // Compares two invalidations. The comparison ignores ack-tracking state.
  31. bool operator==(const Invalidation& other) const;
  32. Topic topic() const;
  33. bool is_unknown_version() const;
  34. // Safe to call only if is_unknown_version() returns false.
  35. int64_t version() const;
  36. // Safe to call only if is_unknown_version() returns false.
  37. const std::string& payload() const;
  38. const AckHandle& ack_handle() const;
  39. // Sets the AckHandler to be used to track this Invalidation.
  40. //
  41. // This should be set by the class that generates the invalidation. Clients
  42. // of the Invalidations API should not need to call this.
  43. //
  44. // Note that some sources of invalidations do not support ack tracking, and do
  45. // not set the ack_handler. This will be hidden from users of this class.
  46. void SetAckHandler(
  47. base::WeakPtr<AckHandler> handler,
  48. scoped_refptr<base::SequencedTaskRunner> handler_task_runner);
  49. // Returns whether or not this instance supports ack tracking. This will
  50. // depend on whether or not the source of invaliadations supports
  51. // invalidations.
  52. //
  53. // Clients can safely ignore this flag. They can assume that all
  54. // invalidations support ack tracking. If they're wrong, then invalidations
  55. // will be less reliable, but their behavior will be no less correct.
  56. bool SupportsAcknowledgement() const;
  57. // Acknowledges the receipt of this invalidation.
  58. //
  59. // Clients should call this on a received invalidation when they have fully
  60. // processed the invalidation and persisted the results to disk. Once this
  61. // function is called, the invalidations system is under no obligation to
  62. // re-deliver this invalidation in the event of a crash or restart.
  63. void Acknowledge() const;
  64. // Informs the ack tracker that this invalidation will not be serviced.
  65. //
  66. // If a client's buffer reaches its limit and it is forced to start dropping
  67. // invalidations, it should call this function before dropping its
  68. // invalidations in order to allow the ack tracker to drop the invalidation,
  69. // too.
  70. //
  71. // To indicate recovery from a drop event, the client should call
  72. // Acknowledge() on the most recently dropped inavlidation.
  73. void Drop();
  74. base::Value::Dict ToValue() const;
  75. std::string ToString() const;
  76. private:
  77. Invalidation(const Topic& topic,
  78. bool is_unknown_version,
  79. int64_t version,
  80. const std::string& payload,
  81. AckHandle ack_handle);
  82. // The Topic to which this invalidation belongs.
  83. Topic topic_;
  84. // This flag is set to true if this is an unknown version invalidation.
  85. bool is_unknown_version_;
  86. // The version number of this invalidation. Should not be accessed if this is
  87. // an unkown version invalidation.
  88. int64_t version_;
  89. // The payaload associated with this invalidation. Should not be accessed if
  90. // this is an unknown version invalidation.
  91. std::string payload_;
  92. // A locally generated unique ID used to manage local acknowledgements.
  93. AckHandle ack_handle_;
  94. // The acknowledgement tracking handler and its thread.
  95. base::WeakPtr<AckHandler> ack_handler_;
  96. scoped_refptr<base::SequencedTaskRunner> ack_handler_task_runner_;
  97. };
  98. } // namespace invalidation
  99. #endif // COMPONENTS_INVALIDATION_PUBLIC_INVALIDATION_H_