Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(144)

Side by Side Diff: google_apis/gcm/base/mcs_util.cc

Issue 117513004: [GCM] Add TTL support (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "google_apis/gcm/base/mcs_util.h" 5 #include "google_apis/gcm/base/mcs_util.h"
6 6
7 #include "base/format_macros.h" 7 #include "base/format_macros.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/time/clock.h"
12 #include "base/time/time.h"
11 13
12 namespace gcm { 14 namespace gcm {
13 15
14 namespace { 16 namespace {
15 17
16 // Type names corresponding to MCSProtoTags. Useful for identifying what type 18 // Type names corresponding to MCSProtoTags. Useful for identifying what type
17 // of MCS protobuf is contained within a google::protobuf::MessageLite object. 19 // of MCS protobuf is contained within a google::protobuf::MessageLite object.
18 // WARNING: must match the order in MCSProtoTag. 20 // WARNING: must match the order in MCSProtoTag.
19 const char* kProtoNames[] = { 21 const char* kProtoNames[] = {
20 "mcs_proto.HeartbeatPing", 22 "mcs_proto.HeartbeatPing",
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 } 204 }
203 // Not all message types have last stream ids. Just return 0. 205 // Not all message types have last stream ids. Just return 0.
204 return 0; 206 return 0;
205 } 207 }
206 208
207 void SetLastStreamIdReceived(uint32 val, 209 void SetLastStreamIdReceived(uint32 val,
208 google::protobuf::MessageLite* protobuf) { 210 google::protobuf::MessageLite* protobuf) {
209 if (protobuf->GetTypeName() == kProtoNames[kIqStanzaTag]) { 211 if (protobuf->GetTypeName() == kProtoNames[kIqStanzaTag]) {
210 reinterpret_cast<mcs_proto::IqStanza*>(protobuf)-> 212 reinterpret_cast<mcs_proto::IqStanza*>(protobuf)->
211 set_last_stream_id_received(val); 213 set_last_stream_id_received(val);
212 return; 214 return;
fgorski 2013/12/28 01:15:08 should be indented 2 to the left (under reinterpre
Nicolas Zea 2013/12/30 21:46:19 Done.
213 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatPingTag]) { 215 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatPingTag]) {
214 reinterpret_cast<mcs_proto::HeartbeatPing*>(protobuf)-> 216 reinterpret_cast<mcs_proto::HeartbeatPing*>(protobuf)->
215 set_last_stream_id_received(val); 217 set_last_stream_id_received(val);
216 return; 218 return;
217 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatAckTag]) { 219 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatAckTag]) {
218 reinterpret_cast<mcs_proto::HeartbeatAck*>(protobuf)-> 220 reinterpret_cast<mcs_proto::HeartbeatAck*>(protobuf)->
219 set_last_stream_id_received(val); 221 set_last_stream_id_received(val);
220 return; 222 return;
221 } else if (protobuf->GetTypeName() == kProtoNames[kDataMessageStanzaTag]) { 223 } else if (protobuf->GetTypeName() == kProtoNames[kDataMessageStanzaTag]) {
222 reinterpret_cast<mcs_proto::DataMessageStanza*>(protobuf)-> 224 reinterpret_cast<mcs_proto::DataMessageStanza*>(protobuf)->
223 set_last_stream_id_received(val); 225 set_last_stream_id_received(val);
224 return; 226 return;
225 } else if (protobuf->GetTypeName() == kProtoNames[kLoginResponseTag]) { 227 } else if (protobuf->GetTypeName() == kProtoNames[kLoginResponseTag]) {
226 reinterpret_cast<mcs_proto::LoginResponse*>(protobuf)-> 228 reinterpret_cast<mcs_proto::LoginResponse*>(protobuf)->
227 set_last_stream_id_received(val); 229 set_last_stream_id_received(val);
228 return; 230 return;
229 } 231 }
230 NOTREACHED(); 232 NOTREACHED();
231 } 233 }
232 234
235 bool HasTTLExpired(const google::protobuf::MessageLite& protobuf,
236 base::Clock* clock) {
237 if (protobuf.GetTypeName() != kProtoNames[kDataMessageStanzaTag])
238 return false;
239 uint64 ttl =
240 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf)->ttl();
241 uint64 sent =
242 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf)->sent();
243 if (ttl > 0 &&
fgorski 2013/12/28 01:15:08 would a single return in place of if statement wor
Nicolas Zea 2013/12/30 21:46:19 Done.
244 clock->Now() >
245 base::Time::FromInternalValue(
246 (sent + ttl) * base::Time::kMicrosecondsPerSecond)) {
247 return true;
248 }
249 return false;
250 }
251
252 int GetTTL(const google::protobuf::MessageLite& protobuf) {
253 if (protobuf.GetTypeName() != kProtoNames[kDataMessageStanzaTag])
254 return 0;
255 return reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf)->
fgorski 2013/12/28 01:15:08 ttl might not be set, return max in that case.
Nicolas Zea 2013/12/30 21:46:19 Should we default to max, or assume that the messa
fgorski 2014/01/02 18:09:27 The API allows the user to not set the TTL and a d
Nicolas Zea 2014/01/02 21:39:08 Done.
256 ttl();
257 }
258
233 } // namespace gcm 259 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698