[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 5 | #ifndef COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_ |
| 6 | #define COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_ |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 7 | |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 8 | #include <set> |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 9 | |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
| 11 | #include "base/single_thread_task_runner.h" |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 12 | #include "components/invalidation/invalidation.h" |
[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 13 | #include "components/invalidation/invalidation_export.h" |
[email protected] | 51766bf | 2014-07-24 01:13:47 | [diff] [blame] | 14 | #include "components/invalidation/invalidation_util.h" |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | class DictionaryValue; |
| 18 | } // namespace base |
| 19 | |
| 20 | namespace syncer { |
| 21 | |
[email protected] | a7b1639 | 2013-11-26 22:46:26 | [diff] [blame] | 22 | namespace test_util { |
| 23 | class UnackedInvalidationSetEqMatcher; |
| 24 | } // test_util |
| 25 | |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 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. |
[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 33 | class INVALIDATION_EXPORT UnackedInvalidationSet { |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 34 | public: |
| 35 | static const size_t kMaxBufferedInvalidations; |
| 36 | |
| 37 | UnackedInvalidationSet(invalidation::ObjectId id); |
[email protected] | 4482877 | 2014-06-06 02:56:52 | [diff] [blame] | 38 | UnackedInvalidationSet(const UnackedInvalidationSet& other); |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 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(). |
[email protected] | 5fe215c | 2014-08-09 06:04:28 | [diff] [blame] | 57 | void ExportInvalidations( |
| 58 | base::WeakPtr<AckHandler> ack_handler, |
| 59 | scoped_refptr<base::SingleThreadTaskRunner> ack_handler_task_runner, |
| 60 | ObjectIdInvalidationMap* out) const; |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 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. |
[email protected] | a7b1639 | 2013-11-26 22:46:26 | [diff] [blame] | 99 | friend class test_util::UnackedInvalidationSetEqMatcher; |
[email protected] | bea426f | 2013-10-23 22:43:48 | [diff] [blame] | 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 | |
[email protected] | 001bbfdc | 2014-07-17 19:28:46 | [diff] [blame] | 121 | #endif // COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_ |