OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "chrome/browser/sync/syncable/model_type_payload_map.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/json/json_writer.h" | |
10 #include "base/memory/scoped_ptr.h" | |
11 #include "base/values.h" | |
12 | |
13 using browser_sync::ModelSafeRoutingInfo; | |
14 namespace syncable { | |
15 | |
16 ModelTypePayloadMap ModelTypePayloadMapFromEnumSet( | |
17 syncable::ModelTypeSet types, | |
18 const std::string& payload) { | |
19 ModelTypePayloadMap types_with_payloads; | |
20 for (syncable::ModelTypeSet::Iterator it = types.First(); | |
21 it.Good(); it.Inc()) { | |
22 types_with_payloads[it.Get()] = payload; | |
23 } | |
24 return types_with_payloads; | |
25 } | |
26 | |
27 ModelTypeSet ModelTypePayloadMapToEnumSet( | |
28 const ModelTypePayloadMap& payload_map) { | |
29 ModelTypeSet types; | |
30 for (ModelTypePayloadMap::const_iterator it = payload_map.begin(); | |
31 it != payload_map.end(); ++it) { | |
32 types.Put(it->first); | |
33 } | |
34 return types; | |
35 } | |
36 | |
37 ModelTypePayloadMap ModelTypePayloadMapFromRoutingInfo( | |
38 const browser_sync::ModelSafeRoutingInfo& routes, | |
39 const std::string& payload) { | |
40 ModelTypePayloadMap types_with_payloads; | |
41 for (browser_sync::ModelSafeRoutingInfo::const_iterator i = routes.begin(); | |
42 i != routes.end(); ++i) { | |
43 types_with_payloads[i->first] = payload; | |
44 } | |
45 return types_with_payloads; | |
46 } | |
47 | |
48 std::string ModelTypePayloadMapToString( | |
49 const ModelTypePayloadMap& type_payloads) { | |
50 scoped_ptr<DictionaryValue> value( | |
51 ModelTypePayloadMapToValue(type_payloads)); | |
52 std::string json; | |
53 base::JSONWriter::Write(value.get(), false, &json); | |
54 return json; | |
55 } | |
56 | |
57 DictionaryValue* ModelTypePayloadMapToValue( | |
58 const ModelTypePayloadMap& type_payloads) { | |
59 DictionaryValue* value = new DictionaryValue(); | |
60 for (ModelTypePayloadMap::const_iterator it = type_payloads.begin(); | |
61 it != type_payloads.end(); ++it) { | |
62 value->SetString(syncable::ModelTypeToString(it->first), it->second); | |
63 } | |
64 return value; | |
65 } | |
66 | |
67 void CoalescePayloads(ModelTypePayloadMap* original, | |
68 const ModelTypePayloadMap& update) { | |
69 for (ModelTypePayloadMap::const_iterator i = update.begin(); | |
70 i != update.end(); ++i) { | |
71 if (original->count(i->first) == 0) { | |
72 // If this datatype isn't already in our map, add it with | |
73 // whatever payload it has. | |
74 (*original)[i->first] = i->second; | |
75 } else if (i->second.length() > 0) { | |
76 // If this datatype is already in our map, we only overwrite the | |
77 // payload if the new one is non-empty. | |
78 (*original)[i->first] = i->second; | |
79 } | |
80 } | |
81 } | |
82 | |
83 void PurgeStalePayload(ModelTypePayloadMap* original, | |
84 const ModelSafeRoutingInfo& routing_info) { | |
85 std::vector<ModelTypePayloadMap::iterator> iterators_to_delete; | |
86 for (ModelTypePayloadMap::iterator i = original->begin(); | |
87 i != original->end(); ++i) { | |
88 if (routing_info.end() == routing_info.find(i->first)) { | |
89 iterators_to_delete.push_back(i); | |
90 } | |
91 } | |
92 | |
93 for (std::vector<ModelTypePayloadMap::iterator>::iterator | |
94 it = iterators_to_delete.begin(); it != iterators_to_delete.end(); | |
95 ++it) { | |
96 original->erase(*it); | |
97 } | |
98 } | |
99 | |
100 } // namespace syncable | |
OLD | NEW |