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