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 16 matching lines...) Expand all Loading... | |
37 COMPILE_ASSERT(arraysize(kProtoNames) == kNumProtoTypes, | 39 COMPILE_ASSERT(arraysize(kProtoNames) == kNumProtoTypes, |
38 ProtoNamesMustIncludeAllTags); | 40 ProtoNamesMustIncludeAllTags); |
39 | 41 |
40 // TODO(zea): replace these with proper values. | 42 // TODO(zea): replace these with proper values. |
41 const char kLoginId[] = "login-1"; | 43 const char kLoginId[] = "login-1"; |
42 const char kLoginDomain[] = "mcs.android.com"; | 44 const char kLoginDomain[] = "mcs.android.com"; |
43 const char kLoginDeviceIdPrefix[] = "android-"; | 45 const char kLoginDeviceIdPrefix[] = "android-"; |
44 const char kLoginSettingName[] = "new_vc"; | 46 const char kLoginSettingName[] = "new_vc"; |
45 const char kLoginSettingValue[] = "1"; | 47 const char kLoginSettingValue[] = "1"; |
46 | 48 |
49 // Maximum amount of time to save an unsent outgoing message for. | |
50 const int kMaxTTLSeconds = 4 * 7 * 24 * 60 * 60; // 4 weeks. | |
51 | |
47 } // namespace | 52 } // namespace |
48 | 53 |
49 scoped_ptr<mcs_proto::LoginRequest> BuildLoginRequest(uint64 auth_id, | 54 scoped_ptr<mcs_proto::LoginRequest> BuildLoginRequest(uint64 auth_id, |
50 uint64 auth_token) { | 55 uint64 auth_token) { |
51 // Create a hex encoded auth id for the device id field. | 56 // Create a hex encoded auth id for the device id field. |
52 std::string auth_id_hex; | 57 std::string auth_id_hex; |
53 auth_id_hex = base::StringPrintf("%" PRIx64, auth_id); | 58 auth_id_hex = base::StringPrintf("%" PRIx64, auth_id); |
54 | 59 |
55 std::string auth_id_str = base::Uint64ToString(auth_id); | 60 std::string auth_id_str = base::Uint64ToString(auth_id); |
56 std::string auth_token_str = base::Uint64ToString(auth_token); | 61 std::string auth_token_str = base::Uint64ToString(auth_token); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
202 } | 207 } |
203 // Not all message types have last stream ids. Just return 0. | 208 // Not all message types have last stream ids. Just return 0. |
204 return 0; | 209 return 0; |
205 } | 210 } |
206 | 211 |
207 void SetLastStreamIdReceived(uint32 val, | 212 void SetLastStreamIdReceived(uint32 val, |
208 google::protobuf::MessageLite* protobuf) { | 213 google::protobuf::MessageLite* protobuf) { |
209 if (protobuf->GetTypeName() == kProtoNames[kIqStanzaTag]) { | 214 if (protobuf->GetTypeName() == kProtoNames[kIqStanzaTag]) { |
210 reinterpret_cast<mcs_proto::IqStanza*>(protobuf)-> | 215 reinterpret_cast<mcs_proto::IqStanza*>(protobuf)-> |
211 set_last_stream_id_received(val); | 216 set_last_stream_id_received(val); |
212 return; | 217 return; |
213 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatPingTag]) { | 218 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatPingTag]) { |
214 reinterpret_cast<mcs_proto::HeartbeatPing*>(protobuf)-> | 219 reinterpret_cast<mcs_proto::HeartbeatPing*>(protobuf)-> |
215 set_last_stream_id_received(val); | 220 set_last_stream_id_received(val); |
216 return; | 221 return; |
217 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatAckTag]) { | 222 } else if (protobuf->GetTypeName() == kProtoNames[kHeartbeatAckTag]) { |
218 reinterpret_cast<mcs_proto::HeartbeatAck*>(protobuf)-> | 223 reinterpret_cast<mcs_proto::HeartbeatAck*>(protobuf)-> |
219 set_last_stream_id_received(val); | 224 set_last_stream_id_received(val); |
220 return; | 225 return; |
221 } else if (protobuf->GetTypeName() == kProtoNames[kDataMessageStanzaTag]) { | 226 } else if (protobuf->GetTypeName() == kProtoNames[kDataMessageStanzaTag]) { |
222 reinterpret_cast<mcs_proto::DataMessageStanza*>(protobuf)-> | 227 reinterpret_cast<mcs_proto::DataMessageStanza*>(protobuf)-> |
223 set_last_stream_id_received(val); | 228 set_last_stream_id_received(val); |
224 return; | 229 return; |
225 } else if (protobuf->GetTypeName() == kProtoNames[kLoginResponseTag]) { | 230 } else if (protobuf->GetTypeName() == kProtoNames[kLoginResponseTag]) { |
226 reinterpret_cast<mcs_proto::LoginResponse*>(protobuf)-> | 231 reinterpret_cast<mcs_proto::LoginResponse*>(protobuf)-> |
227 set_last_stream_id_received(val); | 232 set_last_stream_id_received(val); |
228 return; | 233 return; |
229 } | 234 } |
230 NOTREACHED(); | 235 NOTREACHED(); |
231 } | 236 } |
232 | 237 |
238 bool HasTTLExpired(const google::protobuf::MessageLite& protobuf, | |
239 base::Clock* clock) { | |
240 if (protobuf.GetTypeName() != kProtoNames[kDataMessageStanzaTag]) | |
241 return false; | |
242 uint64 ttl = GetTTL(protobuf); | |
243 uint64 sent = | |
244 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf)->sent(); | |
245 return ttl > 0 && | |
246 clock->Now() > | |
247 base::Time::FromInternalValue( | |
248 (sent + ttl) * base::Time::kMicrosecondsPerSecond); | |
249 } | |
250 | |
251 int GetTTL(const google::protobuf::MessageLite& protobuf) { | |
252 if (protobuf.GetTypeName() != kProtoNames[kDataMessageStanzaTag]) | |
253 return 0; | |
254 const mcs_proto::DataMessageStanza* data_message = | |
255 reinterpret_cast<const mcs_proto::DataMessageStanza*>(&protobuf); | |
256 if (!data_message->has_ttl()) | |
257 return kMaxTTLSeconds; | |
258 return data_message->ttl() <= kMaxTTLSeconds ? | |
259 data_message->ttl() : kMaxTTLSeconds; | |
fgorski
2014/01/02 21:47:35
I think sanitizing of the input is expected to hap
Nicolas Zea
2014/01/02 21:50:52
Yeah, I guess this implies it's valid, when it sho
| |
260 } | |
261 | |
233 } // namespace gcm | 262 } // namespace gcm |
OLD | NEW |