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.
5 #ifndef COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_
6 #define COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_
10 #include "base/memory/weak_ptr.h"
11 #include "base/single_thread_task_runner.h"
12 #include "components/invalidation/public/invalidation.h"
13 #include "components/invalidation/public/invalidation_export.h"
14 #include "components/invalidation/public/invalidation_util.h"
17 class DictionaryValue
;
23 class UnackedInvalidationSetEqMatcher
;
26 class SingleObjectInvalidationSet
;
27 class ObjectIdInvalidationMap
;
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
{
35 static const size_t kMaxBufferedInvalidations
;
37 UnackedInvalidationSet(invalidation::ObjectId id
);
38 UnackedInvalidationSet(const UnackedInvalidationSet
& other
);
39 ~UnackedInvalidationSet();
41 // Returns the ObjectID of the invalidations this class is tracking.
42 const invalidation::ObjectId
& object_id() const;
44 // Adds a new invalidation to the set awaiting acknowledgement.
45 void Add(const Invalidation
& invalidation
);
47 // Adds many new invalidations to the set awaiting acknowledgement.
48 void AddSet(const SingleObjectInvalidationSet
& invalidations
);
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|.
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;
62 // Removes all stored invalidations from this object.
65 // Indicates that a handler has registered to handle these invalidations.
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.
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();
78 // Indicates that the handler has now unregistered itself.
80 // This causes the object to resume enforcement of the
81 // kMaxBufferedInvalidations limit.
82 void SetHandlerIsUnregistered();
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
);
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
);
94 scoped_ptr
<base::DictionaryValue
> ToValue() const;
95 bool ResetFromValue(const base::DictionaryValue
& value
);
98 // Allow this test helper to have access to our internals.
99 friend class test_util::UnackedInvalidationSetEqMatcher
;
101 typedef std::set
<Invalidation
, InvalidationVersionLessThan
> InvalidationsSet
;
103 bool ResetListFromValue(const base::ListValue
& value
);
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
108 void Truncate(size_t max_size
);
111 invalidation::ObjectId object_id_
;
112 InvalidationsSet invalidations_
;
115 typedef std::map
<invalidation::ObjectId
,
116 UnackedInvalidationSet
,
117 ObjectIdLessThan
> UnackedInvalidationsMap
;
119 } // namespace syncer
121 #endif // COMPONENTS_INVALIDATION_IMPL_UNACKED_INVALIDATION_SET_H_