Get foreground tab on Android
[chromium-blink-merge.git] / sync / notifier / single_object_invalidation_set.cc
blob6da3972fc7219e1567aefa3fc4845f8a98a84a22
1 // Copyright 2013 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.
5 #include "sync/notifier/single_object_invalidation_set.h"
7 #include "base/values.h"
8 #include "sync/notifier/invalidation_util.h"
10 namespace syncer {
12 SingleObjectInvalidationSet::SingleObjectInvalidationSet() {}
14 SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {}
16 void SingleObjectInvalidationSet::Insert(const Invalidation& invalidation) {
17 invalidations_.insert(invalidation);
20 void SingleObjectInvalidationSet::InsertAll(
21 const SingleObjectInvalidationSet& other) {
22 invalidations_.insert(other.begin(), other.end());
25 void SingleObjectInvalidationSet::Clear() {
26 invalidations_.clear();
29 bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const {
30 return !invalidations_.empty() &&
31 invalidations_.begin()->is_unknown_version();
34 size_t SingleObjectInvalidationSet::GetSize() const {
35 return invalidations_.size();
38 bool SingleObjectInvalidationSet::IsEmpty() const {
39 return invalidations_.empty();
42 namespace {
44 struct InvalidationComparator {
45 bool operator()(const Invalidation& inv1, const Invalidation& inv2) {
46 return inv1.Equals(inv2);
50 } // namespace
52 bool SingleObjectInvalidationSet::operator==(
53 const SingleObjectInvalidationSet& other) const {
54 return std::equal(invalidations_.begin(),
55 invalidations_.end(),
56 other.invalidations_.begin(),
57 InvalidationComparator());
60 SingleObjectInvalidationSet::const_iterator
61 SingleObjectInvalidationSet::begin() const {
62 return invalidations_.begin();
65 SingleObjectInvalidationSet::const_iterator
66 SingleObjectInvalidationSet::end() const {
67 return invalidations_.end();
70 SingleObjectInvalidationSet::const_reverse_iterator
71 SingleObjectInvalidationSet::rbegin() const {
72 return invalidations_.rbegin();
75 SingleObjectInvalidationSet::const_reverse_iterator
76 SingleObjectInvalidationSet::rend() const {
77 return invalidations_.rend();
80 const Invalidation& SingleObjectInvalidationSet::back() const {
81 return *invalidations_.rbegin();
84 scoped_ptr<base::ListValue> SingleObjectInvalidationSet::ToValue() const {
85 scoped_ptr<base::ListValue> value(new ListValue);
86 for (InvalidationsSet::const_iterator it = invalidations_.begin();
87 it != invalidations_.end(); ++it) {
88 value->Append(it->ToValue().release());
90 return value.Pass();
93 bool SingleObjectInvalidationSet::ResetFromValue(
94 const base::ListValue& list) {
95 for (size_t i = 0; i < list.GetSize(); ++i) {
96 const base::DictionaryValue* dict;
97 if (!list.GetDictionary(i, &dict)) {
98 DLOG(WARNING) << "Could not find invalidation at index " << i;
99 return false;
101 scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
102 if (!invalidation) {
103 DLOG(WARNING) << "Failed to parse invalidation at index " << i;
104 return false;
106 invalidations_.insert(*invalidation);
108 return true;
111 } // namespace syncer