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.
5 #include "components/invalidation/public/object_id_invalidation_map.h"
7 #include "base/json/json_string_value_serializer.h"
12 ObjectIdInvalidationMap
ObjectIdInvalidationMap::InvalidateAll(
13 const ObjectIdSet
& ids
) {
14 ObjectIdInvalidationMap invalidate_all
;
15 for (ObjectIdSet::const_iterator it
= ids
.begin(); it
!= ids
.end(); ++it
) {
16 invalidate_all
.Insert(Invalidation::InitUnknownVersion(*it
));
18 return invalidate_all
;
21 ObjectIdInvalidationMap::ObjectIdInvalidationMap() {}
23 ObjectIdInvalidationMap::~ObjectIdInvalidationMap() {}
25 ObjectIdSet
ObjectIdInvalidationMap::GetObjectIds() const {
27 for (IdToListMap::const_iterator it
= map_
.begin(); it
!= map_
.end(); ++it
) {
28 ret
.insert(it
->first
);
33 bool ObjectIdInvalidationMap::Empty() const {
37 void ObjectIdInvalidationMap::Insert(const Invalidation
& invalidation
) {
38 map_
[invalidation
.object_id()].Insert(invalidation
);
41 ObjectIdInvalidationMap
ObjectIdInvalidationMap::GetSubsetWithObjectIds(
42 const ObjectIdSet
& ids
) const {
44 for (ObjectIdSet::const_iterator it
= ids
.begin(); it
!= ids
.end(); ++it
) {
45 IdToListMap::const_iterator lookup
= map_
.find(*it
);
46 if (lookup
!= map_
.end()) {
47 new_map
[*it
] = lookup
->second
;
50 return ObjectIdInvalidationMap(new_map
);
53 const SingleObjectInvalidationSet
& ObjectIdInvalidationMap::ForObject(
54 invalidation::ObjectId id
) const {
55 IdToListMap::const_iterator lookup
= map_
.find(id
);
56 DCHECK(lookup
!= map_
.end());
57 DCHECK(!lookup
->second
.IsEmpty());
58 return lookup
->second
;
61 void ObjectIdInvalidationMap::GetAllInvalidations(
62 std::vector
<syncer::Invalidation
>* out
) const {
63 for (IdToListMap::const_iterator it
= map_
.begin(); it
!= map_
.end(); ++it
) {
64 out
->insert(out
->begin(), it
->second
.begin(), it
->second
.end());
67 void ObjectIdInvalidationMap::AcknowledgeAll() const {
68 for (IdToListMap::const_iterator it1
= map_
.begin();
69 it1
!= map_
.end(); ++it1
) {
70 for (SingleObjectInvalidationSet::const_iterator it2
= it1
->second
.begin();
71 it2
!= it1
->second
.end(); ++it2
) {
77 bool ObjectIdInvalidationMap::operator==(
78 const ObjectIdInvalidationMap
& other
) const {
79 return map_
== other
.map_
;
82 scoped_ptr
<base::ListValue
> ObjectIdInvalidationMap::ToValue() const {
83 scoped_ptr
<base::ListValue
> value(new base::ListValue());
84 for (IdToListMap::const_iterator it1
= map_
.begin();
85 it1
!= map_
.end(); ++it1
) {
86 for (SingleObjectInvalidationSet::const_iterator it2
=
87 it1
->second
.begin(); it2
!= it1
->second
.end(); ++it2
) {
88 value
->Append(it2
->ToValue().release());
94 bool ObjectIdInvalidationMap::ResetFromValue(const base::ListValue
& value
) {
96 for (size_t i
= 0; i
< value
.GetSize(); ++i
) {
97 const base::DictionaryValue
* dict
;
98 if (!value
.GetDictionary(i
, &dict
)) {
101 scoped_ptr
<Invalidation
> invalidation
= Invalidation::InitFromValue(*dict
);
105 Insert(*invalidation
.get());
110 std::string
ObjectIdInvalidationMap::ToString() const {
112 JSONStringValueSerializer
serializer(&output
);
113 serializer
.set_pretty_print(true);
114 serializer
.Serialize(*ToValue().get());
118 ObjectIdInvalidationMap::ObjectIdInvalidationMap(const IdToListMap
& map
)
121 } // namespace syncer