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 "base/values.h"
8
9namespace syncable {
10
11ModelTypePayloadMap ModelTypePayloadMapFromBitSet(
12    const syncable::ModelTypeBitSet& types,
13    const std::string& payload) {
14  ModelTypePayloadMap types_with_payloads;
15  for (size_t i = syncable::FIRST_REAL_MODEL_TYPE;
16       i < types.size(); ++i) {
17    if (types[i]) {
18      types_with_payloads[syncable::ModelTypeFromInt(i)] = payload;
19    }
20  }
21  return types_with_payloads;
22}
23
24ModelTypePayloadMap ModelTypePayloadMapFromRoutingInfo(
25    const browser_sync::ModelSafeRoutingInfo& routes,
26    const std::string& payload) {
27  ModelTypePayloadMap types_with_payloads;
28  for (browser_sync::ModelSafeRoutingInfo::const_iterator i = routes.begin();
29       i != routes.end(); ++i) {
30    types_with_payloads[i->first] = payload;
31  }
32  return types_with_payloads;
33}
34
35DictionaryValue* ModelTypePayloadMapToValue(
36    const ModelTypePayloadMap& type_payloads) {
37  DictionaryValue* value = new DictionaryValue();
38  for (ModelTypePayloadMap::const_iterator it = type_payloads.begin();
39       it != type_payloads.end(); ++it) {
40    value->SetString(syncable::ModelTypeToString(it->first), it->second);
41  }
42  return value;
43}
44
45void CoalescePayloads(ModelTypePayloadMap* original,
46                      const ModelTypePayloadMap& update) {
47  for (ModelTypePayloadMap::const_iterator i = update.begin();
48       i != update.end(); ++i) {
49    if (original->count(i->first) == 0) {
50      // If this datatype isn't already in our map, add it with
51      // whatever payload it has.
52      (*original)[i->first] = i->second;
53    } else if (i->second.length() > 0) {
54      // If this datatype is already in our map, we only overwrite the
55      // payload if the new one is non-empty.
56      (*original)[i->first] = i->second;
57    }
58  }
59}
60
61}  // namespace syncable
62