summaryrefslogtreecommitdiffstats
path: root/sync/notifier/object_id_invalidation_map.cc
blob: 5fa253e1892fb9596dd3664f109c85b9c69b964f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "sync/notifier/object_id_invalidation_map.h"

#include <algorithm>

#include "base/compiler_specific.h"
#include "base/values.h"

namespace syncer {

ObjectIdSet ObjectIdInvalidationMapToSet(
    const ObjectIdInvalidationMap& invalidation_map) {
  ObjectIdSet ids;
  for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
       it != invalidation_map.end(); ++it) {
    ids.insert(it->first);
  }
  return ids;
}

ObjectIdInvalidationMap ObjectIdSetToInvalidationMap(
    const ObjectIdSet& ids, const std::string& payload) {
  ObjectIdInvalidationMap invalidation_map;
  for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
    // TODO(dcheng): Do we need to provide a way to set AckHandle?
    invalidation_map[*it].payload = payload;
  }
  return invalidation_map;
}

namespace {

struct ObjectIdInvalidationMapValueEquals {
  bool operator()(const ObjectIdInvalidationMap::value_type& value1,
                  const ObjectIdInvalidationMap::value_type& value2) const {
    return
        (value1.first == value2.first) &&
        value1.second.Equals(value2.second);
  }
};

}  // namespace

bool ObjectIdInvalidationMapEquals(
    const ObjectIdInvalidationMap& invalidation_map1,
    const ObjectIdInvalidationMap& invalidation_map2) {
  return
      (invalidation_map1.size() == invalidation_map2.size()) &&
      std::equal(invalidation_map1.begin(), invalidation_map1.end(),
                 invalidation_map2.begin(),
                 ObjectIdInvalidationMapValueEquals());
}

scoped_ptr<base::ListValue> ObjectIdInvalidationMapToValue(
    const ObjectIdInvalidationMap& invalidation_map) {
  scoped_ptr<ListValue> value(new ListValue());
  for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
       it != invalidation_map.end(); ++it) {
    DictionaryValue* entry = new DictionaryValue();
    entry->Set("objectId", ObjectIdToValue(it->first).release());
    entry->Set("state", it->second.ToValue().release());
    value->Append(entry);
  }
  return value.Pass();
}

bool ObjectIdInvalidationMapFromValue(const base::ListValue& value,
                               ObjectIdInvalidationMap* out) {
  out->clear();
  for (base::ListValue::const_iterator it = value.begin();
       it != value.end(); ++it) {
    const base::DictionaryValue* entry = NULL;
    const base::DictionaryValue* id_value = NULL;
    const base::DictionaryValue* invalidation_value = NULL;
    invalidation::ObjectId id;
    Invalidation invalidation;
    if (!(*it)->GetAsDictionary(&entry) ||
        !entry->GetDictionary("objectId", &id_value) ||
        !entry->GetDictionary("state", &invalidation_value) ||
        !ObjectIdFromValue(*id_value, &id) ||
        !invalidation.ResetFromValue(*invalidation_value)) {
      return false;
    }
    ignore_result(out->insert(std::make_pair(id, invalidation)));
  }
  return true;
}

ModelTypeInvalidationMap ObjectIdInvalidationMapToModelTypeInvalidationMap(
    const ObjectIdInvalidationMap& invalidation_map) {
  ModelTypeInvalidationMap type_invalidation_map;
  for (ObjectIdInvalidationMap::const_iterator it = invalidation_map.begin();
       it != invalidation_map.end(); ++it) {
    ModelType model_type;
    if (!ObjectIdToRealModelType(it->first, &model_type)) {
      DLOG(WARNING) << "Invalid object ID: "
                    << ObjectIdToString(it->first);
      continue;
    }
    type_invalidation_map[model_type] = it->second;
  }
  return type_invalidation_map;
}

ObjectIdInvalidationMap ModelTypeInvalidationMapToObjectIdInvalidationMap(
    const ModelTypeInvalidationMap& invalidation_map) {
  ObjectIdInvalidationMap id_invalidation_map;
  for (ModelTypeInvalidationMap::const_iterator it = invalidation_map.begin();
       it != invalidation_map.end(); ++it) {
    invalidation::ObjectId id;
    if (!RealModelTypeToObjectId(it->first, &id)) {
      DLOG(WARNING) << "Invalid model type " << it->first;
      continue;
    }
    id_invalidation_map[id] = it->second;
  }
  return id_invalidation_map;
}

}  // namespace syncer