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