Home | History | Annotate | Download | only in invalidation
      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/object_id_invalidation_map.h"
      6 
      7 #include "base/json/json_string_value_serializer.h"
      8 
      9 namespace syncer {
     10 
     11 // static
     12 ObjectIdInvalidationMap ObjectIdInvalidationMap::InvalidateAll(
     13     const ObjectIdSet& ids) {
     14   ObjectIdInvalidationMap invalidate_all;
     15   for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
     16     invalidate_all.Insert(Invalidation::InitUnknownVersion(*it));
     17   }
     18   return invalidate_all;
     19 }
     20 
     21 ObjectIdInvalidationMap::ObjectIdInvalidationMap() {}
     22 
     23 ObjectIdInvalidationMap::~ObjectIdInvalidationMap() {}
     24 
     25 ObjectIdSet ObjectIdInvalidationMap::GetObjectIds() const {
     26   ObjectIdSet ret;
     27   for (IdToListMap::const_iterator it = map_.begin(); it != map_.end(); ++it) {
     28     ret.insert(it->first);
     29   }
     30   return ret;
     31 }
     32 
     33 bool ObjectIdInvalidationMap::Empty() const {
     34   return map_.empty();
     35 }
     36 
     37 void ObjectIdInvalidationMap::Insert(const Invalidation& invalidation) {
     38   map_[invalidation.object_id()].Insert(invalidation);
     39 }
     40 
     41 ObjectIdInvalidationMap ObjectIdInvalidationMap::GetSubsetWithObjectIds(
     42     const ObjectIdSet& ids) const {
     43   IdToListMap new_map;
     44   for (ObjectIdSet::const_iterator it = ids.begin(); it != ids.end(); ++it) {
     45     IdToListMap::const_iterator lookup = map_.find(*it);
     46     if (lookup != map_.end()) {
     47       new_map[*it] = lookup->second;
     48     }
     49   }
     50   return ObjectIdInvalidationMap(new_map);
     51 }
     52 
     53 const SingleObjectInvalidationSet& ObjectIdInvalidationMap::ForObject(
     54     invalidation::ObjectId id) const {
     55   IdToListMap::const_iterator lookup = map_.find(id);
     56   DCHECK(lookup != map_.end());
     57   DCHECK(!lookup->second.IsEmpty());
     58   return lookup->second;
     59 }
     60 
     61 void ObjectIdInvalidationMap::GetAllInvalidations(
     62     std::vector<syncer::Invalidation>* out) const {
     63   for (IdToListMap::const_iterator it = map_.begin(); it != map_.end(); ++it) {
     64     out->insert(out->begin(), it->second.begin(), it->second.end());
     65   }
     66 }
     67 void ObjectIdInvalidationMap::AcknowledgeAll() const {
     68   for (IdToListMap::const_iterator it1 = map_.begin();
     69        it1 != map_.end(); ++it1) {
     70     for (SingleObjectInvalidationSet::const_iterator it2 = it1->second.begin();
     71          it2 != it1->second.end(); ++it2) {
     72       it2->Acknowledge();
     73     }
     74   }
     75 }
     76 
     77 bool ObjectIdInvalidationMap::operator==(
     78     const ObjectIdInvalidationMap& other) const {
     79   return map_ == other.map_;
     80 }
     81 
     82 scoped_ptr<base::ListValue> ObjectIdInvalidationMap::ToValue() const {
     83   scoped_ptr<base::ListValue> value(new base::ListValue());
     84   for (IdToListMap::const_iterator it1 = map_.begin();
     85        it1 != map_.end(); ++it1) {
     86     for (SingleObjectInvalidationSet::const_iterator it2 =
     87          it1->second.begin(); it2 != it1->second.end(); ++it2) {
     88       value->Append(it2->ToValue().release());
     89     }
     90   }
     91   return value.Pass();
     92 }
     93 
     94 bool ObjectIdInvalidationMap::ResetFromValue(const base::ListValue& value) {
     95   map_.clear();
     96   for (size_t i = 0; i < value.GetSize(); ++i) {
     97     const base::DictionaryValue* dict;
     98     if (!value.GetDictionary(i, &dict)) {
     99       return false;
    100     }
    101     scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
    102     if (!invalidation) {
    103       return false;
    104     }
    105     Insert(*invalidation.get());
    106   }
    107   return true;
    108 }
    109 
    110 std::string ObjectIdInvalidationMap::ToString() const {
    111   std::string output;
    112   JSONStringValueSerializer serializer(&output);
    113   serializer.set_pretty_print(true);
    114   serializer.Serialize(*ToValue().get());
    115   return output;
    116 }
    117 
    118 ObjectIdInvalidationMap::ObjectIdInvalidationMap(const IdToListMap& map)
    119   : map_(map) {}
    120 
    121 }  // namespace syncer
    122