1 // Copyright 2014 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 COMPONENTS_INVALIDATION_PUBLIC_TOPIC_INVALIDATION_MAP_H_
6 #define COMPONENTS_INVALIDATION_PUBLIC_TOPIC_INVALIDATION_MAP_H_
7 
8 #include <map>
9 #include <memory>
10 #include <vector>
11 
12 #include "components/invalidation/public/invalidation.h"
13 #include "components/invalidation/public/invalidation_export.h"
14 #include "components/invalidation/public/invalidation_util.h"
15 #include "components/invalidation/public/single_object_invalidation_set.h"
16 
17 namespace base {
18 class ListValue;
19 }  // namespace base
20 
21 namespace syncer {
22 
23 // A set of notifications with some helper methods to organize them by Topic
24 // and version number.
25 class INVALIDATION_EXPORT TopicInvalidationMap {
26  public:
27   TopicInvalidationMap();
28   TopicInvalidationMap(const TopicInvalidationMap& other);
29   TopicInvalidationMap& operator=(const TopicInvalidationMap& other);
30   ~TopicInvalidationMap();
31 
32   // Returns set of Topics for which at least one invalidation is present.
33   TopicSet GetTopics() const;
34 
35   // Returns true if this map contains no invalidations.
36   bool Empty() const;
37 
38   // Returns true if both maps contain the same set of invalidations.
39   bool operator==(const TopicInvalidationMap& other) const;
40 
41   // Inserts a new invalidation into this map.
42   void Insert(const Invalidation& invalidation);
43 
44   // Returns a new map containing the subset of invaliations from this map
45   // whose topic were in the specified |topics|.
46   // TODO(crbug.com/1029698): replace all usages with the version below and
47   // remove this method.
48   TopicInvalidationMap GetSubsetWithTopics(const Topics& topics) const;
49 
50   // Returns a new map containing the subset of invaliations from this map
51   // whose topic were in the specified |topics|.
52   TopicInvalidationMap GetSubsetWithTopics(const TopicSet& topics) const;
53 
54   // Returns the subset of invalidations with Topic matching |topic|.
55   const SingleObjectInvalidationSet& ForTopic(Topic topic) const;
56 
57   // Returns the contents of this map in a single vector.
58   void GetAllInvalidations(std::vector<syncer::Invalidation>* out) const;
59 
60   // Call Acknowledge() on all contained Invalidations.
61   void AcknowledgeAll() const;
62 
63   // Serialize this map to a value. Used to expose value on
64   // chrome://invalidations page.
65   std::unique_ptr<base::ListValue> ToValue() const;
66 
67  private:
68   explicit TopicInvalidationMap(
69       const std::map<Topic, SingleObjectInvalidationSet>& map);
70 
71   std::map<Topic, SingleObjectInvalidationSet> map_;
72 };
73 
74 }  // namespace syncer
75 
76 #endif  // COMPONENTS_INVALIDATION_PUBLIC_TOPIC_INVALIDATION_MAP_H_
77