blob: ac9b2c318e4a6984d0948236631b61ad6717b909 [file] [log] [blame]
[email protected]001bbfdc2014-07-17 19:28:461// Copyright 2014 The Chromium Authors. All rights reserved.
[email protected]bea426f2013-10-23 22:43:482// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]001bbfdc2014-07-17 19:28:465#ifndef COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
6#define COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
[email protected]bea426f2013-10-23 22:43:487
[email protected]51766bf2014-07-24 01:13:478#include <set>
[email protected]bea426f2013-10-23 22:43:489
[email protected]5fe215c2014-08-09 06:04:2810#include "base/memory/weak_ptr.h"
11#include "base/single_thread_task_runner.h"
[email protected]51766bf2014-07-24 01:13:4712#include "components/invalidation/invalidation.h"
[email protected]001bbfdc2014-07-17 19:28:4613#include "components/invalidation/invalidation_export.h"
[email protected]51766bf2014-07-24 01:13:4714#include "components/invalidation/invalidation_util.h"
[email protected]bea426f2013-10-23 22:43:4815
16namespace base {
17class DictionaryValue;
18} // namespace base
19
20namespace syncer {
21
[email protected]a7b16392013-11-26 22:46:2622namespace test_util {
23class UnackedInvalidationSetEqMatcher;
24} // test_util
25
[email protected]bea426f2013-10-23 22:43:4826class SingleObjectInvalidationSet;
27class ObjectIdInvalidationMap;
28class 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]001bbfdc2014-07-17 19:28:4633class INVALIDATION_EXPORT UnackedInvalidationSet {
[email protected]bea426f2013-10-23 22:43:4834 public:
35 static const size_t kMaxBufferedInvalidations;
36
37 UnackedInvalidationSet(invalidation::ObjectId id);
[email protected]44828772014-06-06 02:56:5238 UnackedInvalidationSet(const UnackedInvalidationSet& other);
[email protected]bea426f2013-10-23 22:43:4839 ~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]5fe215c2014-08-09 06:04:2857 void ExportInvalidations(
58 base::WeakPtr<AckHandler> ack_handler,
59 scoped_refptr<base::SingleThreadTaskRunner> ack_handler_task_runner,
60 ObjectIdInvalidationMap* out) const;
[email protected]bea426f2013-10-23 22:43:4861
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]a7b16392013-11-26 22:46:2699 friend class test_util::UnackedInvalidationSetEqMatcher;
[email protected]bea426f2013-10-23 22:43:48100
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
115typedef std::map<invalidation::ObjectId,
116 UnackedInvalidationSet,
117 ObjectIdLessThan> UnackedInvalidationsMap;
118
119} // namespace syncer
120
[email protected]001bbfdc2014-07-17 19:28:46121#endif // COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_