summaryrefslogtreecommitdiffstats
path: root/components/invalidation/single_object_invalidation_set.cc
blob: c05c517eeeeb6d641ffee8eea669b2acb1207e74 (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
// Copyright 2014 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 "components/invalidation/single_object_invalidation_set.h"

#include "base/values.h"
#include "components/invalidation/invalidation_util.h"

namespace syncer {

SingleObjectInvalidationSet::SingleObjectInvalidationSet() {}

SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {}

void SingleObjectInvalidationSet::Insert(const Invalidation& invalidation) {
  invalidations_.insert(invalidation);
}

void SingleObjectInvalidationSet::InsertAll(
    const SingleObjectInvalidationSet& other) {
  invalidations_.insert(other.begin(), other.end());
}

void SingleObjectInvalidationSet::Clear() {
  invalidations_.clear();
}

void SingleObjectInvalidationSet::Erase(const_iterator it) {
  invalidations_.erase(*it);
}

bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const {
  return !invalidations_.empty() &&
      invalidations_.begin()->is_unknown_version();
}

size_t SingleObjectInvalidationSet::GetSize() const {
  return invalidations_.size();
}

bool SingleObjectInvalidationSet::IsEmpty() const {
  return invalidations_.empty();
}

namespace {

struct InvalidationComparator {
  bool operator()(const Invalidation& inv1, const Invalidation& inv2) {
    return inv1.Equals(inv2);
  }
};

}  // namespace

bool SingleObjectInvalidationSet::operator==(
    const SingleObjectInvalidationSet& other) const {
  return std::equal(invalidations_.begin(),
                    invalidations_.end(),
                    other.invalidations_.begin(),
                    InvalidationComparator());
}

SingleObjectInvalidationSet::const_iterator
SingleObjectInvalidationSet::begin() const {
  return invalidations_.begin();
}

SingleObjectInvalidationSet::const_iterator
SingleObjectInvalidationSet::end() const {
  return invalidations_.end();
}

SingleObjectInvalidationSet::const_reverse_iterator
SingleObjectInvalidationSet::rbegin() const {
  return invalidations_.rbegin();
}

SingleObjectInvalidationSet::const_reverse_iterator
SingleObjectInvalidationSet::rend() const {
  return invalidations_.rend();
}

const Invalidation& SingleObjectInvalidationSet::back() const {
  return *invalidations_.rbegin();
}

scoped_ptr<base::ListValue> SingleObjectInvalidationSet::ToValue() const {
  scoped_ptr<base::ListValue> value(new base::ListValue);
  for (InvalidationsSet::const_iterator it = invalidations_.begin();
       it != invalidations_.end(); ++it) {
    value->Append(it->ToValue().release());
  }
  return value.Pass();
}

bool SingleObjectInvalidationSet::ResetFromValue(
    const base::ListValue& list) {
  for (size_t i = 0; i < list.GetSize(); ++i) {
    const base::DictionaryValue* dict;
    if (!list.GetDictionary(i, &dict)) {
      DLOG(WARNING) << "Could not find invalidation at index " << i;
      return false;
    }
    scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
    if (!invalidation) {
      DLOG(WARNING) << "Failed to parse invalidation at index " << i;
      return false;
    }
    invalidations_.insert(*invalidation);
  }
  return true;
}

}  // namespace syncer