Update device/bluetooth/OWNERS
[chromium-blink-merge.git] / sync / notifier / object_id_invalidation_map.cc
blob5fa253e1892fb9596dd3664f109c85b9c69b964f
1 // Copyright (c) 2012 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 "sync/notifier/object_id_invalidation_map.h"
7 #include <algorithm>
9 #include "base/compiler_specific.h"
10 #include "base/values.h"
12 namespace syncer {
14 ObjectIdSet ObjectIdInvalidationMapToSet(
15 const ObjectIdInvalidationMap& invalidation_map) {
16 ObjectIdSet ids;
17 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
18 it != invalidation_map.end(); ++it) {
19 ids.insert(it->first);
21 return ids;
24 ObjectIdInvalidationMap ObjectIdSetToInvalidationMap(
25 const ObjectIdSet& ids, const std::string& payload) {
26 ObjectIdInvalidationMap invalidation_map;
27 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
28 // TODO(dcheng): Do we need to provide a way to set AckHandle?
29 invalidation_map[*it].payload = payload;
31 return invalidation_map;
34 namespace {
36 struct ObjectIdInvalidationMapValueEquals {
37 bool operator()(const ObjectIdInvalidationMap::value_type& value1,
38 const ObjectIdInvalidationMap::value_type& value2) const {
39 return
40 (value1.first == value2.first) &&
41 value1.second.Equals(value2.second);
45 } // namespace
47 bool ObjectIdInvalidationMapEquals(
48 const ObjectIdInvalidationMap& invalidation_map1,
49 const ObjectIdInvalidationMap& invalidation_map2) {
50 return
51 (invalidation_map1.size() == invalidation_map2.size()) &&
52 std::equal(invalidation_map1.begin(), invalidation_map1.end(),
53 invalidation_map2.begin(),
54 ObjectIdInvalidationMapValueEquals());
57 scoped_ptr<base::ListValue> ObjectIdInvalidationMapToValue(
58 const ObjectIdInvalidationMap& invalidation_map) {
59 scoped_ptr<ListValue> value(new ListValue());
60 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
61 it != invalidation_map.end(); ++it) {
62 DictionaryValue* entry = new DictionaryValue();
63 entry->Set("objectId", ObjectIdToValue(it->first).release());
64 entry->Set("state", it->second.ToValue().release());
65 value->Append(entry);
67 return value.Pass();
70 bool ObjectIdInvalidationMapFromValue(const base::ListValue& value,
71 ObjectIdInvalidationMap* out) {
72 out->clear();
73 for (base::ListValue::const_iterator it = value.begin();
74 it != value.end(); ++it) {
75 const base::DictionaryValue* entry = NULL;
76 const base::DictionaryValue* id_value = NULL;
77 const base::DictionaryValue* invalidation_value = NULL;
78 invalidation::ObjectId id;
79 Invalidation invalidation;
80 if (!(*it)->GetAsDictionary(&entry) ||
81 !entry->GetDictionary("objectId", &id_value) ||
82 !entry->GetDictionary("state", &invalidation_value) ||
83 !ObjectIdFromValue(*id_value, &id) ||
84 !invalidation.ResetFromValue(*invalidation_value)) {
85 return false;
87 ignore_result(out->insert(std::make_pair(id, invalidation)));
89 return true;
92 ModelTypeInvalidationMap ObjectIdInvalidationMapToModelTypeInvalidationMap(
93 const ObjectIdInvalidationMap& invalidation_map) {
94 ModelTypeInvalidationMap type_invalidation_map;
95 for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
96 it != invalidation_map.end(); ++it) {
97 ModelType model_type;
98 if (!ObjectIdToRealModelType(it->first, &model_type)) {
99 DLOG(WARNING) << "Invalid object ID: "
100 << ObjectIdToString(it->first);
101 continue;
103 type_invalidation_map[model_type] = it->second;
105 return type_invalidation_map;
108 ObjectIdInvalidationMap ModelTypeInvalidationMapToObjectIdInvalidationMap(
109 const ModelTypeInvalidationMap& invalidation_map) {
110 ObjectIdInvalidationMap id_invalidation_map;
111 for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin();
112 it != invalidation_map.end(); ++it) {
113 invalidation::ObjectId id;
114 if (!RealModelTypeToObjectId(it->first, &id)) {
115 DLOG(WARNING) << "Invalid model type " << it->first;
116 continue;
118 id_invalidation_map[id] = it->second;
120 return id_invalidation_map;
123 } // namespace syncer