single_object_invalidation_set.cc revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2014 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 "components/invalidation/single_object_invalidation_set.h"
6
7#include "base/values.h"
8#include "sync/internal_api/public/base/invalidation_util.h"
9
10namespace syncer {
11
12SingleObjectInvalidationSet::SingleObjectInvalidationSet() {}
13
14SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {}
15
16void SingleObjectInvalidationSet::Insert(const Invalidation& invalidation) {
17  invalidations_.insert(invalidation);
18}
19
20void SingleObjectInvalidationSet::InsertAll(
21    const SingleObjectInvalidationSet& other) {
22  invalidations_.insert(other.begin(), other.end());
23}
24
25void SingleObjectInvalidationSet::Clear() {
26  invalidations_.clear();
27}
28
29void SingleObjectInvalidationSet::Erase(const_iterator it) {
30  invalidations_.erase(*it);
31}
32
33bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const {
34  return !invalidations_.empty() &&
35      invalidations_.begin()->is_unknown_version();
36}
37
38size_t SingleObjectInvalidationSet::GetSize() const {
39  return invalidations_.size();
40}
41
42bool SingleObjectInvalidationSet::IsEmpty() const {
43  return invalidations_.empty();
44}
45
46namespace {
47
48struct InvalidationComparator {
49  bool operator()(const Invalidation& inv1, const Invalidation& inv2) {
50    return inv1.Equals(inv2);
51  }
52};
53
54}  // namespace
55
56bool SingleObjectInvalidationSet::operator==(
57    const SingleObjectInvalidationSet& other) const {
58  return std::equal(invalidations_.begin(),
59                    invalidations_.end(),
60                    other.invalidations_.begin(),
61                    InvalidationComparator());
62}
63
64SingleObjectInvalidationSet::const_iterator
65SingleObjectInvalidationSet::begin() const {
66  return invalidations_.begin();
67}
68
69SingleObjectInvalidationSet::const_iterator
70SingleObjectInvalidationSet::end() const {
71  return invalidations_.end();
72}
73
74SingleObjectInvalidationSet::const_reverse_iterator
75SingleObjectInvalidationSet::rbegin() const {
76  return invalidations_.rbegin();
77}
78
79SingleObjectInvalidationSet::const_reverse_iterator
80SingleObjectInvalidationSet::rend() const {
81  return invalidations_.rend();
82}
83
84const Invalidation& SingleObjectInvalidationSet::back() const {
85  return *invalidations_.rbegin();
86}
87
88scoped_ptr<base::ListValue> SingleObjectInvalidationSet::ToValue() const {
89  scoped_ptr<base::ListValue> value(new base::ListValue);
90  for (InvalidationsSet::const_iterator it = invalidations_.begin();
91       it != invalidations_.end(); ++it) {
92    value->Append(it->ToValue().release());
93  }
94  return value.Pass();
95}
96
97bool SingleObjectInvalidationSet::ResetFromValue(
98    const base::ListValue& list) {
99  for (size_t i = 0; i < list.GetSize(); ++i) {
100    const base::DictionaryValue* dict;
101    if (!list.GetDictionary(i, &dict)) {
102      DLOG(WARNING) << "Could not find invalidation at index " << i;
103      return false;
104    }
105    scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
106    if (!invalidation) {
107      DLOG(WARNING) << "Failed to parse invalidation at index " << i;
108      return false;
109    }
110    invalidations_.insert(*invalidation);
111  }
112  return true;
113}
114
115}  // namespace syncer
116