| 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 "google_apis/gcm/engine/account_info.h" | |
| 6 | |
| 7 #include "base/strings/string_number_conversions.h" | |
| 8 #include "base/strings/string_util.h" | |
| 9 | |
| 10 namespace gcm { | |
| 11 | |
| 12 namespace { | |
| 13 | |
| 14 const char kSeparator[] = "&"; | |
| 15 uint32 kEmailIndex = 0; | |
| 16 uint32 kMessageTypeIndex = 1; | |
| 17 uint32 kMessageIdIndex = 2; | |
| 18 uint32 kMessageTimestampIndex = 3; | |
| 19 uint32 kSizeWithNoMessage = kMessageTypeIndex + 1; | |
| 20 uint32 kSizeWithMessage = kMessageTimestampIndex + 1; | |
| 21 | |
| 22 std::string MessageTypeToString(AccountInfo::MessageType type) { | |
| 23 switch (type) { | |
| 24 case AccountInfo::MSG_NONE: | |
| 25 return "none"; | |
| 26 case AccountInfo::MSG_ADD: | |
| 27 return "add"; | |
| 28 case AccountInfo::MSG_REMOVE: | |
| 29 return "remove"; | |
| 30 default: | |
| 31 NOTREACHED(); | |
| 32 } | |
| 33 return ""; | |
| 34 } | |
| 35 | |
| 36 AccountInfo::MessageType StringToMessageType(const std::string& type) { | |
| 37 if (type.compare("add") == 0) | |
| 38 return AccountInfo::MSG_ADD; | |
| 39 if (type.compare("remove") == 0) | |
| 40 return AccountInfo::MSG_REMOVE; | |
| 41 return AccountInfo::MSG_NONE; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 AccountInfo::AccountInfo() { | |
| 47 } | |
| 48 | |
| 49 AccountInfo::~AccountInfo() { | |
| 50 } | |
| 51 | |
| 52 std::string AccountInfo::SerializeAsString() const { | |
| 53 std::string value; | |
| 54 value.append(email); | |
| 55 value.append(kSeparator); | |
| 56 value.append(MessageTypeToString(last_message_type)); | |
| 57 if (last_message_type != MSG_NONE) { | |
| 58 value.append(kSeparator); | |
| 59 value.append(last_message_id); | |
| 60 value.append(kSeparator); | |
| 61 value.append(base::Int64ToString(last_message_timestamp.ToInternalValue())); | |
| 62 } | |
| 63 | |
| 64 return value; | |
| 65 } | |
| 66 | |
| 67 bool AccountInfo::ParseFromString(const std::string& value) { | |
| 68 std::vector<std::string> values; | |
| 69 Tokenize(value, kSeparator, &values); | |
| 70 if (values.size() != kSizeWithNoMessage && | |
| 71 values.size() != kSizeWithMessage) { | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 if (values[kEmailIndex].empty() || | |
| 76 values[kMessageTypeIndex].empty()) { | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 if (values.size() == kSizeWithMessage && | |
| 81 (values[kMessageIdIndex].empty() || | |
| 82 values[kMessageTimestampIndex].empty())) { | |
| 83 return false; | |
| 84 } | |
| 85 | |
| 86 if (values.size() == kSizeWithMessage) { | |
| 87 int64 timestamp = 0LL; | |
| 88 if (!base::StringToInt64(values[kMessageTimestampIndex], ×tamp)) | |
| 89 return false; | |
| 90 | |
| 91 MessageType message_type = StringToMessageType(values[kMessageTypeIndex]); | |
| 92 if (message_type == MSG_NONE) | |
| 93 return false; | |
| 94 | |
| 95 last_message_type = message_type; | |
| 96 last_message_id = values[kMessageIdIndex]; | |
| 97 last_message_timestamp = base::Time::FromInternalValue(timestamp); | |
| 98 } else { | |
| 99 last_message_type = MSG_NONE; | |
| 100 last_message_id.clear(); | |
| 101 last_message_timestamp = base::Time(); | |
| 102 } | |
| 103 | |
| 104 email = values[kEmailIndex]; | |
| 105 | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 } // namespace gcm | |
| OLD | NEW |