1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
85f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include <set>
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/memory/weak_ptr.h"
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "base/single_thread_task_runner.h"
125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/invalidation_export.h"
145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_util.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace base {
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class DictionaryValue;
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace base
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace syncer {
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace test_util {
23f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class UnackedInvalidationSetEqMatcher;
24f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // test_util
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class SingleObjectInvalidationSet;
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class ObjectIdInvalidationMap;
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class AckHandle;
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Manages the set of invalidations that are awaiting local acknowledgement for
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// a particular ObjectId.  This set of invalidations will be persisted across
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// restarts, though this class is not directly responsible for that.
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass INVALIDATION_EXPORT UnackedInvalidationSet {
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  static const size_t kMaxBufferedInvalidations;
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  UnackedInvalidationSet(invalidation::ObjectId id);
3846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  UnackedInvalidationSet(const UnackedInvalidationSet& other);
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  ~UnackedInvalidationSet();
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns the ObjectID of the invalidations this class is tracking.
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  const invalidation::ObjectId& object_id() const;
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Adds a new invalidation to the set awaiting acknowledgement.
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Add(const Invalidation& invalidation);
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Adds many new invalidations to the set awaiting acknowledgement.
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void AddSet(const SingleObjectInvalidationSet& invalidations);
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Exports the set of invalidations awaiting acknowledgement as an
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // ObjectIdInvalidationMap.  Each of these invalidations will be associated
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // with the given |ack_handler|.
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The contents of the UnackedInvalidationSet are not directly modified by
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // this procedure, but the AckHandles stored in those exported invalidations
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // are likely to end up back here in calls to Acknowledge() or Drop().
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void ExportInvalidations(
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      base::WeakPtr<AckHandler> ack_handler,
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      scoped_refptr<base::SingleThreadTaskRunner> ack_handler_task_runner,
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ObjectIdInvalidationMap* out) const;
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Removes all stored invalidations from this object.
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Clear();
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Indicates that a handler has registered to handle these invalidations.
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Registrations with the invalidations server persist across restarts, but
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // registrations from InvalidationHandlers to the InvalidationService are not.
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // In the time immediately after a restart, it's possible that the server
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // will send us invalidations, and we won't have a handler to send them to.
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The SetIsRegistered() call indicates that this period has come to an end.
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // There is now a handler that can receive these invalidations.  Once this
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // function has been called, the kMaxBufferedInvalidations limit will be
751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // ignored.  It is assumed that the handler will manage its own buffer size.
761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SetHandlerIsRegistered();
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Indicates that the handler has now unregistered itself.
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  //
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // This causes the object to resume enforcement of the
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // kMaxBufferedInvalidations limit.
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void SetHandlerIsUnregistered();
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Given an AckHandle belonging to one of the contained invalidations, finds
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the invalidation and drops it from the list.  It is considered to be
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // acknowledged, so there is no need to continue maintaining its state.
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Acknowledge(const AckHandle& handle);
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Given an AckHandle belonging to one of the contained invalidations, finds
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the invalidation, drops it from the list, and adds additional state to
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // indicate that this invalidation has been lost without being acted on.
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Drop(const AckHandle& handle);
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  scoped_ptr<base::DictionaryValue> ToValue() const;
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool ResetFromValue(const base::DictionaryValue& value);
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private:
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Allow this test helper to have access to our internals.
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  friend class test_util::UnackedInvalidationSetEqMatcher;
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  typedef std::set<Invalidation, InvalidationVersionLessThan> InvalidationsSet;
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool ResetListFromValue(const base::ListValue& value);
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Limits the list size to the given maximum.  This function will correctly
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // update this class' internal data to indicate if invalidations have been
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // dropped.
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void Truncate(size_t max_size);
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool registered_;
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  invalidation::ObjectId object_id_;
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  InvalidationsSet invalidations_;
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)typedef std::map<invalidation::ObjectId,
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                 UnackedInvalidationSet,
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                 ObjectIdLessThan> UnackedInvalidationsMap;
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace syncer
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
121116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // COMPONENTS_INVALIDATION_UNACKED_INVALIDATION_SET_H_
122