| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/notifier/object_id_state_map.h" | 5 #include "sync/notifier/object_id_state_map.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 9 #include "base/compiler_specific.h" |
| 10 #include "base/values.h" |
| 11 |
| 7 namespace syncer { | 12 namespace syncer { |
| 8 | 13 |
| 9 ObjectIdSet ObjectIdStateMapToSet(const ObjectIdStateMap& id_state_map) { | 14 ObjectIdSet ObjectIdStateMapToSet(const ObjectIdStateMap& id_state_map) { |
| 10 ObjectIdSet ids; | 15 ObjectIdSet ids; |
| 11 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | 16 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); |
| 12 it != id_state_map.end(); ++it) { | 17 it != id_state_map.end(); ++it) { |
| 13 ids.insert(it->first); | 18 ids.insert(it->first); |
| 14 } | 19 } |
| 15 return ids; | 20 return ids; |
| 16 } | 21 } |
| 17 | 22 |
| 18 ObjectIdStateMap ObjectIdSetToStateMap(const ObjectIdSet& ids, | 23 ObjectIdStateMap ObjectIdSetToStateMap(const ObjectIdSet& ids, |
| 19 const std::string& payload) { | 24 const std::string& payload) { |
| 20 ObjectIdStateMap id_state_map; | 25 ObjectIdStateMap id_state_map; |
| 21 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { | 26 for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) { |
| 22 // TODO(dcheng): Do we need to provide a way to set AckHandle? | 27 // TODO(dcheng): Do we need to provide a way to set AckHandle? |
| 23 id_state_map[*it].payload = payload; | 28 id_state_map[*it].payload = payload; |
| 24 } | 29 } |
| 25 return id_state_map; | 30 return id_state_map; |
| 26 } | 31 } |
| 27 | 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 |
| 28 ModelTypeStateMap ObjectIdStateMapToModelTypeStateMap( | 89 ModelTypeStateMap ObjectIdStateMapToModelTypeStateMap( |
| 29 const ObjectIdStateMap& id_state_map) { | 90 const ObjectIdStateMap& id_state_map) { |
| 30 ModelTypeStateMap type_state_map; | 91 ModelTypeStateMap type_state_map; |
| 31 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); | 92 for (ObjectIdStateMap::const_iterator it = id_state_map.begin(); |
| 32 it != id_state_map.end(); ++it) { | 93 it != id_state_map.end(); ++it) { |
| 33 ModelType model_type; | 94 ModelType model_type; |
| 34 if (!ObjectIdToRealModelType(it->first, &model_type)) { | 95 if (!ObjectIdToRealModelType(it->first, &model_type)) { |
| 35 DLOG(WARNING) << "Invalid object ID: " | 96 DLOG(WARNING) << "Invalid object ID: " |
| 36 << ObjectIdToString(it->first); | 97 << ObjectIdToString(it->first); |
| 37 continue; | 98 continue; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 50 if (!RealModelTypeToObjectId(it->first, &id)) { | 111 if (!RealModelTypeToObjectId(it->first, &id)) { |
| 51 DLOG(WARNING) << "Invalid model type " << it->first; | 112 DLOG(WARNING) << "Invalid model type " << it->first; |
| 52 continue; | 113 continue; |
| 53 } | 114 } |
| 54 id_state_map[id] = it->second; | 115 id_state_map[id] = it->second; |
| 55 } | 116 } |
| 56 return id_state_map; | 117 return id_state_map; |
| 57 } | 118 } |
| 58 | 119 |
| 59 } // namespace syncer | 120 } // namespace syncer |
| OLD | NEW |