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