OLD | NEW |
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 Loading... |
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; |
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 return ttl > 0 && |
| 244 clock->Now() > |
| 245 base::Time::FromInternalValue( |
| 246 (sent + ttl) * base::Time::kMicrosecondsPerSecond); |
| 247 } |
| 248 |
| 249 int GetTTL(const google::protobuf::MessageLite& protobuf) { |
| 250 if (protobuf.GetTypeName() != kProtoNames[kDataMessageStanzaTag]) |
| 251 return 0; |
| 252 return reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf)-> |
| 253 ttl(); |
| 254 } |
| 255 |
233 } // namespace gcm | 256 } // namespace gcm |
OLD | NEW |