Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / invalidation / object_id_invalidation_map.cc
blob139e8d5835910ef2fe3702a90e0c8a44978e542f
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/object_id_invalidation_map.h"
7 #include "base/json/json_string_value_serializer.h"
9 namespace syncer {
11 // static
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 {
26 ObjectIdSet ret;
27 for (IdToListMap::const_iterator it = map_.begin(); it != map_.end(); ++it) {
28 ret.insert(it->first);
30 return ret;
33 bool ObjectIdInvalidationMap::Empty() const {
34 return map_.empty();
37 void ObjectIdInvalidationMap::Insert(const Invalidation& invalidation) {
38 map_[invalidation.object_id()].Insert(invalidation);
41 ObjectIdInvalidationMap ObjectIdInvalidationMap::GetSubsetWithObjectIds(
42 const ObjectIdSet& ids) const {
43 IdToListMap new_map;
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) {
72 it2->Acknowledge();
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());
91 return value.Pass();
94 bool ObjectIdInvalidationMap::ResetFromValue(const base::ListValue& value) {
95 map_.clear();
96 for (size_t i = 0; i < value.GetSize(); ++i) {
97 const base::DictionaryValue* dict;
98 if (!value.GetDictionary(i, &dict)) {
99 return false;
101 scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
102 if (!invalidation) {
103 return false;
105 Insert(*invalidation.get());
107 return true;
110 std::string ObjectIdInvalidationMap::ToString() const {
111 std::string output;
112 JSONStringValueSerializer serializer(&output);
113 serializer.set_pretty_print(true);
114 serializer.Serialize(*ToValue().get());
115 return output;
118 ObjectIdInvalidationMap::ObjectIdInvalidationMap(const IdToListMap& map)
119 : map_(map) {}
121 } // namespace syncer