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