Chromium Code Reviews| Index: google_apis/gcm/engine/account_info.cc |
| diff --git a/google_apis/gcm/engine/account_info.cc b/google_apis/gcm/engine/account_info.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..316c49ec5c85ad940bec5e07ee8b8251a0127eb8 |
| --- /dev/null |
| +++ b/google_apis/gcm/engine/account_info.cc |
| @@ -0,0 +1,97 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "google_apis/gcm/engine/account_info.h" |
| + |
| +#include "base/strings/string_number_conversions.h" |
| +#include "base/strings/string_util.h" |
| + |
| +namespace gcm { |
| + |
| +namespace { |
| +const char kSeparator[] = "&"; |
| +uint32 kEmailIndex = 0; |
| +uint32 kMessageTypeIndex = 1; |
| +uint32 kMessageIdIndex = 2; |
| +uint32 kMessageTimestampIndex = 3; |
| +uint32 kSizeWithNoMessage = kMessageTypeIndex + 1; |
| +uint32 kSizeWithMessage = kMessageTimestampIndex + 1; |
| + |
| +std::string MessageTypeToString(AccountInfo::MessageType type) { |
| + switch (type) { |
| + case AccountInfo::MSG_NONE: |
| + return "none"; |
| + case AccountInfo::MSG_ADD: |
| + return "add"; |
| + case AccountInfo::MSG_REMOVE: |
| + return "remove"; |
| + default: |
| + NOTREACHED(); |
| + } |
| + return ""; |
| +} |
| + |
| +AccountInfo::MessageType StringToMessageType(const std::string& type) { |
| + if (type.compare("add") == 0) |
| + return AccountInfo::MSG_ADD; |
| + if (type.compare("remove") == 0) |
| + return AccountInfo::MSG_REMOVE; |
| + DCHECK_EQ(type.compare("none"), 0); |
| + return AccountInfo::MSG_NONE; |
| +} |
| +} |
|
Nicolas Zea
2014/07/28 18:10:09
nit: // namespace
Also I think we normally have n
fgorski
2014/07/29 16:24:02
Done.
|
| + |
| +AccountInfo::AccountInfo() { |
| +} |
| + |
| +AccountInfo::~AccountInfo() { |
| +} |
| + |
| +std::string AccountInfo::SerializeAsString() const { |
| + std::string value; |
| + value.append(email); |
| + value.append(kSeparator); |
| + value.append(MessageTypeToString(last_message_type)); |
| + if (last_message_type != MSG_NONE) { |
| + value.append(kSeparator); |
| + value.append(last_message_id); |
| + value.append(kSeparator); |
| + value.append(base::Int64ToString(last_message_timestamp.ToInternalValue())); |
| + } |
| + |
| + return value; |
| +} |
| + |
| +bool AccountInfo::ParseFromString(const std::string& value) { |
| + std::vector<std::string> values; |
| + Tokenize(value, kSeparator, &values); |
| + DCHECK(values.size() == kSizeWithNoMessage || |
| + values.size() == kSizeWithMessage); |
| + if (values[kEmailIndex].empty() || values[kMessageTypeIndex].empty()) |
| + return false; |
| + |
| + if (values.size() == kSizeWithMessage && |
| + (values[kMessageIdIndex].empty() || |
| + values[kMessageTimestampIndex].empty())) |
|
Nicolas Zea
2014/07/28 18:10:09
nit: curly brace around body for multi-line condit
fgorski
2014/07/29 16:24:02
Done.
|
| + return false; |
| + |
| + if (values.size() == kSizeWithMessage) { |
| + int64 timestamp; |
|
Nicolas Zea
2014/07/28 18:10:09
nit: initialize to 0
fgorski
2014/07/29 16:24:02
Done.
|
| + if (!base::StringToInt64(values[kMessageTimestampIndex], ×tamp)) |
| + return false; |
| + last_message_timestamp = base::Time::FromInternalValue(timestamp); |
| + last_message_id = values[kMessageIdIndex]; |
| + } else { |
| + last_message_type = MSG_NONE; |
| + last_message_id.clear(); |
| + last_message_timestamp = base::Time(); |
| + } |
| + |
| + email = values[kEmailIndex]; |
| + last_message_type = StringToMessageType(values[kMessageTypeIndex]); |
| + |
| + return true; |
| +} |
| + |
| +} // namespace gcm |