• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/unacked_invalidation_set.h"
6 
7 #include "base/strings/string_number_conversions.h"
8 #include "sync/internal_api/public/base/ack_handle.h"
9 #include "sync/notifier/object_id_invalidation_map.h"
10 
11 namespace {
12 
13 const char kSourceKey[] = "source";
14 const char kNameKey[] = "name";
15 const char kInvalidationListKey[] = "invalidation-list";
16 
17 }  // namespace
18 
19 namespace syncer {
20 
21 const size_t UnackedInvalidationSet::kMaxBufferedInvalidations = 5;
22 
23 // static
UnackedInvalidationSet(invalidation::ObjectId id)24 UnackedInvalidationSet::UnackedInvalidationSet(
25     invalidation::ObjectId id)
26     : registered_(false),
27       object_id_(id) {}
28 
UnackedInvalidationSet(const UnackedInvalidationSet & other)29 UnackedInvalidationSet::UnackedInvalidationSet(
30     const UnackedInvalidationSet& other)
31     : registered_(other.registered_),
32       object_id_(other.object_id_),
33       invalidations_(other.invalidations_) {
34 }
35 
~UnackedInvalidationSet()36 UnackedInvalidationSet::~UnackedInvalidationSet() {}
37 
object_id() const38 const invalidation::ObjectId& UnackedInvalidationSet::object_id() const {
39   return object_id_;
40 }
41 
Add(const Invalidation & invalidation)42 void UnackedInvalidationSet::Add(
43     const Invalidation& invalidation) {
44   SingleObjectInvalidationSet set;
45   set.Insert(invalidation);
46   AddSet(set);
47   if (!registered_)
48     Truncate(kMaxBufferedInvalidations);
49 }
50 
AddSet(const SingleObjectInvalidationSet & invalidations)51 void UnackedInvalidationSet::AddSet(
52     const SingleObjectInvalidationSet& invalidations) {
53   invalidations_.insert(invalidations.begin(), invalidations.end());
54   if (!registered_)
55     Truncate(kMaxBufferedInvalidations);
56 }
57 
ExportInvalidations(WeakHandle<AckHandler> ack_handler,ObjectIdInvalidationMap * out) const58 void UnackedInvalidationSet::ExportInvalidations(
59     WeakHandle<AckHandler> ack_handler,
60     ObjectIdInvalidationMap* out) const {
61   for (SingleObjectInvalidationSet::const_iterator it = invalidations_.begin();
62        it != invalidations_.end(); ++it) {
63     // Copy the invalidation and set the copy's ack_handler.
64     Invalidation inv(*it);
65     inv.set_ack_handler(ack_handler);
66     out->Insert(inv);
67   }
68 }
69 
Clear()70 void UnackedInvalidationSet::Clear() {
71   invalidations_.clear();
72 }
73 
SetHandlerIsRegistered()74 void UnackedInvalidationSet::SetHandlerIsRegistered() {
75   registered_ = true;
76 }
77 
SetHandlerIsUnregistered()78 void UnackedInvalidationSet::SetHandlerIsUnregistered() {
79   registered_ = false;
80   Truncate(kMaxBufferedInvalidations);
81 }
82 
83 // Removes the matching ack handle from the list.
Acknowledge(const AckHandle & handle)84 void UnackedInvalidationSet::Acknowledge(const AckHandle& handle) {
85   bool handle_found = false;
86   for (SingleObjectInvalidationSet::const_iterator it = invalidations_.begin();
87        it != invalidations_.end(); ++it) {
88     if (it->ack_handle().Equals(handle)) {
89       invalidations_.erase(*it);
90       handle_found = true;
91       break;
92     }
93   }
94   DLOG_IF(WARNING, !handle_found)
95       << "Unrecognized to ack for object " << ObjectIdToString(object_id_);
96   (void)handle_found;  // Silence unused variable warning in release builds.
97 }
98 
99 // Erase the invalidation with matching ack handle from the list.  Also creates
100 // an 'UnknownVersion' invalidation with the same ack handle and places it at
101 // the beginning of the list.  If an unknown version invalidation currently
102 // exists, it is replaced.
Drop(const AckHandle & handle)103 void UnackedInvalidationSet::Drop(const AckHandle& handle) {
104   SingleObjectInvalidationSet::const_iterator it;
105   for (it = invalidations_.begin(); it != invalidations_.end(); ++it) {
106     if (it->ack_handle().Equals(handle)) {
107       break;
108     }
109   }
110   if (it == invalidations_.end()) {
111     DLOG(WARNING) << "Unrecognized drop request for object "
112                   << ObjectIdToString(object_id_);
113     return;
114   }
115 
116   Invalidation unknown_version = Invalidation::InitFromDroppedInvalidation(*it);
117   invalidations_.erase(*it);
118 
119   // If an unknown version is in the list, we remove it so we can replace it.
120   if (!invalidations_.empty() && invalidations_.begin()->is_unknown_version()) {
121     invalidations_.erase(*invalidations_.begin());
122   }
123 
124   invalidations_.insert(unknown_version);
125 }
126 
ToValue() const127 scoped_ptr<base::DictionaryValue> UnackedInvalidationSet::ToValue() const {
128   scoped_ptr<base::DictionaryValue> value(new base::DictionaryValue);
129   value->SetString(kSourceKey, base::IntToString(object_id_.source()));
130   value->SetString(kNameKey, object_id_.name());
131 
132   scoped_ptr<base::ListValue> list_value(new base::ListValue);
133   for (InvalidationsSet::const_iterator it = invalidations_.begin();
134        it != invalidations_.end(); ++it) {
135     list_value->Append(it->ToValue().release());
136   }
137   value->Set(kInvalidationListKey, list_value.release());
138 
139   return value.Pass();
140 }
141 
ResetFromValue(const base::DictionaryValue & value)142 bool UnackedInvalidationSet::ResetFromValue(
143     const base::DictionaryValue& value) {
144   std::string source_str;
145   if (!value.GetString(kSourceKey, &source_str)) {
146     DLOG(WARNING) << "Unable to deserialize source";
147     return false;
148   }
149   int source = 0;
150   if (!base::StringToInt(source_str, &source)) {
151     DLOG(WARNING) << "Invalid source: " << source_str;
152     return false;
153   }
154   std::string name;
155   if (!value.GetString(kNameKey, &name)) {
156     DLOG(WARNING) << "Unable to deserialize name";
157     return false;
158   }
159   object_id_ = invalidation::ObjectId(source, name);
160   const base::ListValue* invalidation_list = NULL;
161   if (!value.GetList(kInvalidationListKey, &invalidation_list)
162       || !ResetListFromValue(*invalidation_list)) {
163     // Earlier versions of this class did not set this field, so we don't treat
164     // parsing errors here as a fatal failure.
165     DLOG(WARNING) << "Unable to deserialize invalidation list.";
166   }
167   return true;
168 }
169 
ResetListFromValue(const base::ListValue & list)170 bool UnackedInvalidationSet::ResetListFromValue(
171     const base::ListValue& list) {
172   for (size_t i = 0; i < list.GetSize(); ++i) {
173     const base::DictionaryValue* dict;
174     if (!list.GetDictionary(i, &dict)) {
175       DLOG(WARNING) << "Failed to get invalidation dictionary at index " << i;
176       return false;
177     }
178     scoped_ptr<Invalidation> invalidation = Invalidation::InitFromValue(*dict);
179     if (!invalidation) {
180       DLOG(WARNING) << "Failed to parse invalidation at index " << i;
181       return false;
182     }
183     invalidations_.insert(*invalidation.get());
184   }
185   return true;
186 }
187 
Truncate(size_t max_size)188 void UnackedInvalidationSet::Truncate(size_t max_size) {
189   DCHECK_GT(max_size, 0U);
190 
191   if (invalidations_.size() <= max_size) {
192     return;
193   }
194 
195   while (invalidations_.size() > max_size) {
196     invalidations_.erase(*invalidations_.begin());
197   }
198 
199   // We dropped some invalidations.  We remember the fact that an unknown
200   // amount of information has been lost by ensuring this list begins with
201   // an UnknownVersion invalidation.  We remove the oldest remaining
202   // invalidation to make room for it.
203   invalidation::ObjectId id = invalidations_.begin()->object_id();
204   invalidations_.erase(*invalidations_.begin());
205 
206   Invalidation unknown_version = Invalidation::InitUnknownVersion(id);
207   invalidations_.insert(unknown_version);
208 }
209 
210 }  // namespace syncer
211