1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#ifndef COMPONENTS_INVALIDATION_OBJECT_ID_INVALIDATION_MAP_H_
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#define COMPONENTS_INVALIDATION_OBJECT_ID_INVALIDATION_MAP_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/invalidation_export.h"
135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/invalidation/invalidation_util.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/invalidation/single_object_invalidation_set.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace syncer {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// A set of notifications with some helper methods to organize them by object ID
194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// and version number.
20116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass INVALIDATION_EXPORT ObjectIdInvalidationMap {
214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  public:
224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Creates an invalidation map that includes an 'unknown version'
234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // invalidation for each specified ID in |ids|.
244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   static ObjectIdInvalidationMap InvalidateAll(const ObjectIdSet& ids);
254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   ObjectIdInvalidationMap();
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   ~ObjectIdInvalidationMap();
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
294e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns set of ObjectIds for which at least one invalidation is present.
304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   ObjectIdSet GetObjectIds() const;
314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns true if this map contains no invalidations.
334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   bool Empty() const;
344e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns true if both maps contain the same set of invalidations.
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   bool operator==(const ObjectIdInvalidationMap& other) const;
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Inserts a new invalidation into this map.
394e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   void Insert(const Invalidation& invalidation);
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
414e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns a new map containing the subset of invaliations from this map
424e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // whose IDs were in the specified |ids| set.
434e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   ObjectIdInvalidationMap GetSubsetWithObjectIds(const ObjectIdSet& ids) const;
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
454e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns the subset of invalidations with IDs matching |id|.
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   const SingleObjectInvalidationSet& ForObject(
474e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)       invalidation::ObjectId id) const;
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
494e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Returns the contents of this map in a single vector.
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   void GetAllInvalidations(std::vector<syncer::Invalidation>* out) const;
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   // Call Acknowledge() on all contained Invalidations.
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)   void AcknowledgeAll() const;
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
554e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Serialize this map to a value.
564e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   scoped_ptr<base::ListValue> ToValue() const;
574e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
584e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Deserialize the value into a map and use it to re-initialize this object.
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   bool ResetFromValue(const base::ListValue& value);
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   // Prints the contentes of this map as a human-readable string.
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   std::string ToString() const;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  private:
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   typedef std::map<invalidation::ObjectId,
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                    SingleObjectInvalidationSet,
674e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                    ObjectIdLessThan> IdToListMap;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   ObjectIdInvalidationMap(const IdToListMap& map);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)   IdToListMap map_;
724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)};
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace syncer
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#endif  // COMPONENTS_INVALIDATION_OBJECT_ID_INVALIDATION_MAP_H_
77