| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "sync/notifier/object_id_state_map.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/values.h" | |
| 11 | |
| 12 namespace syncer { | |
| 13 | |
| 14 ObjectIdSet ObjectIdStateMapToSet(const ObjectIdStateMap& id_state_map) { | |
| 15 ObjectIdSet ids; | |
| 16 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | |
| 17 it != id_state_map.end(); ++it) { | |
| 18 ids.insert(it->first); | |
| 19 } | |
| 20 return ids; | |
| 21 } | |
| 22 | |
| 23 ObjectIdStateMap ObjectIdSetToStateMap(const ObjectIdSet& ids, | |
| 24 const std::string& payload) { | |
| 25 ObjectIdStateMap id_state_map; | |
| 26 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | |
| 27 // TODO(dcheng): Do we need to provide a way to set AckHandle? | |
| 28 id_state_map[*it].payload = payload; | |
| 29 } | |
| 30 return id_state_map; | |
| 31 } | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 struct ObjectIdStateMapValueEquals { | |
| 36 bool operator()(const ObjectIdStateMap::value_type& value1, | |
| 37 const ObjectIdStateMap::value_type& value2) const { | |
| 38 return | |
| 39 (value1.first == value2.first) && | |
| 40 value1.second.Equals(value2.second); | |
| 41 } | |
| 42 }; | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 bool ObjectIdStateMapEquals(const ObjectIdStateMap& id_state_map1, | |
| 47 const ObjectIdStateMap& id_state_map2) { | |
| 48 return | |
| 49 (id_state_map1.size() == id_state_map2.size()) && | |
| 50 std::equal(id_state_map1.begin(), id_state_map1.end(), | |
| 51 id_state_map2.begin(), ObjectIdStateMapValueEquals()); | |
| 52 } | |
| 53 | |
| 54 scoped_ptr<base::ListValue> ObjectIdStateMapToValue( | |
| 55 const ObjectIdStateMap& id_state_map) { | |
| 56 scoped_ptr<ListValue> value(new ListValue()); | |
| 57 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | |
| 58 it != id_state_map.end(); ++it) { | |
| 59 DictionaryValue* entry = new DictionaryValue(); | |
| 60 entry->Set("objectId", ObjectIdToValue(it->first).release()); | |
| 61 entry->Set("state", it->second.ToValue().release()); | |
| 62 value->Append(entry); | |
| 63 } | |
| 64 return value.Pass(); | |
| 65 } | |
| 66 | |
| 67 bool ObjectIdStateMapFromValue(const base::ListValue& value, | |
| 68 ObjectIdStateMap* out) { | |
| 69 out->clear(); | |
| 70 for (base::ListValue::const_iterator it = value.begin(); | |
| 71 it != value.end(); ++it) { | |
| 72 const base::DictionaryValue* entry = NULL; | |
| 73 const base::DictionaryValue* id_value = NULL; | |
| 74 const base::DictionaryValue* state_value = NULL; | |
| 75 invalidation::ObjectId id; | |
| 76 InvalidationState state; | |
| 77 if (!(*it)->GetAsDictionary(&entry) || | |
| 78 !entry->GetDictionary("objectId", &id_value) || | |
| 79 !entry->GetDictionary("state", &state_value) || | |
| 80 !ObjectIdFromValue(*id_value, &id) || | |
| 81 !state.ResetFromValue(*state_value)) { | |
| 82 return false; | |
| 83 } | |
| 84 ignore_result(out->insert(std::make_pair(id, state))); | |
| 85 } | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 ModelTypeStateMap ObjectIdStateMapToModelTypeStateMap( | |
| 90 const ObjectIdStateMap& id_state_map) { | |
| 91 ModelTypeStateMap type_state_map; | |
| 92 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | |
| 93 it != id_state_map.end(); ++it) { | |
| 94 ModelType model_type; | |
| 95 if (!ObjectIdToRealModelType(it->first, &model_type)) { | |
| 96 DLOG(WARNING) << "Invalid object ID: " | |
| 97 << ObjectIdToString(it->first); | |
| 98 continue; | |
| 99 } | |
| 100 type_state_map[model_type] = it->second; | |
| 101 } | |
| 102 return type_state_map; | |
| 103 } | |
| 104 | |
| 105 ObjectIdStateMap ModelTypeStateMapToObjectIdStateMap( | |
| 106 const ModelTypeStateMap& type_state_map) { | |
| 107 ObjectIdStateMap id_state_map; | |
| 108 for (ModelTypeStateMap::const_iterator it = type_state_map.begin(); | |
| 109 it != type_state_map.end(); ++it) { | |
| 110 invalidation::ObjectId id; | |
| 111 if (!RealModelTypeToObjectId(it->first, &id)) { | |
| 112 DLOG(WARNING) << "Invalid model type " << it->first; | |
| 113 continue; | |
| 114 } | |
| 115 id_state_map[id] = it->second; | |
| 116 } | |
| 117 return id_state_map; | |
| 118 } | |
| 119 | |
| 120 } // namespace syncer | |
| OLD | NEW |