Home | History | Annotate | Download | only in notifier
      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.
      4 
      5 #include "sync/notifier/single_object_invalidation_set.h"
      6 
      7 #include "base/values.h"
      8 #include "sync/notifier/invalidation_util.h"
      9 
     10 namespace syncer {
     11 
     12 SingleObjectInvalidationSet::SingleObjectInvalidationSet() {}
     13 
     14 SingleObjectInvalidationSet::~SingleObjectInvalidationSet() {}
     15 
     16 void SingleObjectInvalidationSet::Insert(const Invalidation& invalidation) {
     17   invalidations_.insert(invalidation);
     18 }
     19 
     20 void SingleObjectInvalidationSet::InsertAll(
     21     const SingleObjectInvalidationSet& other) {
     22   invalidations_.insert(other.begin(), other.end());
     23 }
     24 
     25 void SingleObjectInvalidationSet::Clear() {
     26   invalidations_.clear();
     27 }
     28 
     29 bool SingleObjectInvalidationSet::StartsWithUnknownVersion() const {
     30   return !invalidations_.empty() &&
     31       invalidations_.begin()->is_unknown_version();
     32 }
     33 
     34 size_t SingleObjectInvalidationSet::GetSize() const {
     35   return invalidations_.size();
     36 }
     37 
     38 bool SingleObjectInvalidationSet::IsEmpty() const {
     39   return invalidations_.empty();
     40 }
     41 
     42 namespace {
     43 
     44 struct InvalidationComparator {
     45   bool operator()(const Invalidation& inv1, const Invalidation& inv2) {
     46     return inv1.Equals(inv2);
     47   }
     48 };
     49 
     50 }  // namespace
     51 
     52 bool SingleObjectInvalidationSet::operator==(
     53     const SingleObjectInvalidationSet& other) const {
     54   return std::equal(invalidations_.begin(),
     55                     invalidations_.end(),
     56                     other.invalidations_.begin(),
     57                     InvalidationComparator());
     58 }
     59 
     60 SingleObjectInvalidationSet::const_iterator
     61 SingleObjectInvalidationSet::begin() const {
     62   return invalidations_.begin();
     63 }
     64 
     65 SingleObjectInvalidationSet::const_iterator
     66 SingleObjectInvalidationSet::end() const {
     67   return invalidations_.end();
     68 }
     69 
     70 SingleObjectInvalidationSet::const_reverse_iterator
     71 SingleObjectInvalidationSet::rbegin() const {
     72   return invalidations_.rbegin();
     73 }
     74 
     75 SingleObjectInvalidationSet::const_reverse_iterator
     76 SingleObjectInvalidationSet::rend() const {
     77   return invalidations_.rend();
     78 }
     79 
     80 const Invalidation& SingleObjectInvalidationSet::back() const {
     81   return *invalidations_.rbegin();
     82 }
     83 
     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());
     89   }
     90   return value.Pass();
     91 }
     92 
     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;
    100     }
    101     scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
    102     if (!invalidation) {
    103       DLOG(WARNING) << "Failed to parse invalidation at index " << i;
    104       return false;
    105     }
    106     invalidations_.insert(*invalidation);
    107   }
    108   return true;
    109 }
    110 
    111 }  // namespace syncer
    112