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